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: 34 20 31 34 3a 32 35 3a 31 39 20 30 38 32 62 38 4 14:25:19 082b8
5760: 64 61 30 30 35 31 32 38 66 34 37 66 36 33 65 39 da005128f47f63e9
5770: 35 62 36 62 37 30 32 62 66 34 35 31 37 32 32 31 5b6b702bf4517221
5780: 62 32 61 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 b2a"../*.** 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 0a 2a 2f yte boundary..*/
4a3d0 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f 42 .#define EIGHT_B
4a3e0 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29 YTE_ALIGNMENT(X)
4a3f0 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 29 ((((char*)(X)
4a400 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 3d - (char*)0)&7)=
4a410 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 =0).../*.** An i
4a420 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
4a430 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
4a440 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 re is used to st
4a450 6f 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 6e ore the busy-han
4a460 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b dler.** callback
4a470 20 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 6c for a given sql
4a480 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a 0a ite handle. .**.
4a490 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 75 ** The sqlite.bu
4a4a0 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 72 syHandler member
4a4b0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 73 of the sqlite s
4a4c0 74 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 74 truct contains t
4a4d0 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c 62 he busy.** callb
4a4e0 61 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 61 ack for the data
4a4f0 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 63 base handle. Eac
4a500 68 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 76 h pager opened v
4a510 69 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a ia the sqlite.**
4a520 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65 handle is passe
4a530 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 73 d a pointer to s
4a540 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 qlite.busyHandle
4a550 72 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e 64 r. The busy-hand
4a560 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 ler.** callback
4a570 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 76 is currently inv
4a580 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 oked only from w
4a590 69 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a 2a ithin pager.c..*
4a5a0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
4a5b0 20 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 73 BusyHandler Bus
4a5c0 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 74 yHandler;.struct
4a5d0 20 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a 20 BusyHandler {.
4a5e0 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f int (*xFunc)(vo
4a5f0 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 id *,int); /* T
4a600 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b he busy callback
4a610 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 */. void *pArg
4a620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4a630 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f /* First arg to
4a640 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a busy callback *
4a650 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 20 /. int nBusy;
4a660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4a670 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 * Incremented wi
4a680 74 68 20 65 61 63 68 20 62 75 73 79 20 63 61 6c th each busy cal
4a690 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e l */.};../*.** N
4a6a0 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 ame of the maste
4a6b0 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 r database table
4a6c0 2e 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 . The master da
4a6d0 74 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a 20 tabase table.**
4a6e0 69 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 62 is a special tab
4a6f0 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 le that holds th
4a700 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 72 e names and attr
4a710 69 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a 2a ibutes of all.**
4a720 20 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e 64 user tables and
4a730 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 65 indices..*/.#de
4a740 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d 45 fine MASTER_NAME
4a750 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f 6d "sqlite_m
4a760 61 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 54 aster".#define T
4a770 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 20 EMP_MASTER_NAME
4a780 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 "sqlite_temp_ma
4a790 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ster"../*.** The
4a7a0 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 root-page of th
4a7b0 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 e master databas
4a7c0 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 e table..*/.#def
4a7d0 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 20 ine MASTER_ROOT
4a7e0 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 1../*.** T
4a7f0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73 he name of the s
4a800 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a chema table..*/.
4a810 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f 54 #define SCHEMA_T
4a820 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 54 ABLE(x) ((!OMIT
4a830 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 29 _TEMPDB)&&(x==1)
4a840 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d ?TEMP_MASTER_NAM
4a850 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a 0a E:MASTER_NAME)..
4a860 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 65 /*.** A convenie
4a870 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 72 nce macro that r
4a880 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
4a890 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e r of elements in
4a8a0 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2f .** an array..*/
4a8b0 0a 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 69 .#define ArraySi
4a8c0 7a 65 28 58 29 20 20 20 20 28 28 69 6e 74 29 28 ze(X) ((int)(
4a8d0 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f 66 sizeof(X)/sizeof
4a8e0 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 (X[0])))../*.**
4a8f0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 The following va
4a900 6c 75 65 20 61 73 20 61 20 64 65 73 74 72 75 63 lue as a destruc
4a910 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 tor means to use
4a920 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 sqlite3DbFree()
4a930 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 ..** This is an
4a940 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73 69 internal extensi
4a950 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54 41 on to SQLITE_STA
4a960 54 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f 54 TIC and SQLITE_T
4a970 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64 65 RANSIENT..*/.#de
4a980 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e 41 fine SQLITE_DYNA
4a990 4d 49 43 20 20 20 28 28 73 71 6c 69 74 65 33 5f MIC ((sqlite3_
4a9a0 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 destructor_type)
4a9b0 73 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a 0a sqlite3DbFree)..
4a9c0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49 54 /*.** When SQLIT
4a9d0 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64 65 E_OMIT_WSD is de
4a9e0 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 fined, it means
4a9f0 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74 20 that the target
4aa00 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a 2a platform does.**
4aa10 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72 69 not support Wri
4aa20 74 61 62 6c 65 20 53 74 61 74 69 63 20 44 61 74 table Static Dat
4aa30 61 20 28 57 53 44 29 20 73 75 63 68 20 61 73 20 a (WSD) such as
4aa40 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74 69 global and stati
4aa50 63 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 20 c variables..**
4aa60 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 75 All variables mu
4aa70 73 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e 20 st either be on
4aa80 74 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79 6e the stack or dyn
4aa90 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 amically allocat
4aaa0 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 68 ed from.** the h
4aab0 65 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20 69 eap. When WSD i
4aac0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20 74 s unsupported, t
4aad0 68 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c he variable decl
4aae0 61 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65 72 arations scatter
4aaf0 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75 74 ed.** throughout
4ab00 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65 the SQLite code
4ab10 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f 6e must become con
4ab20 73 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e 20 stants instead.
4ab30 20 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44 0a The SQLITE_WSD.
4ab40 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 ** macro is used
4ab50 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 for this purpos
4ab60 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64 20 e. And instead
4ab70 6f 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20 74 of referencing t
4ab80 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 64 he variable.** d
4ab90 69 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65 20 irectly, we use
4aba0 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73 20 its constant as
4abb0 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 a key to lookup
4abc0 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c the run-time all
4abd0 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65 72 ocated.** buffer
4abe0 20 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61 6c that holds real
4abf0 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 20 variable. The
4ac00 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73 6f constant is also
4ac10 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 the initializer
4ac20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e 2d .** for the run-
4ac30 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 time allocated b
4ac40 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 uffer..**.** In
4ac50 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 20 77 the usual case w
4ac60 68 65 72 65 20 57 53 44 20 69 73 20 73 75 70 70 here WSD is supp
4ac70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49 54 orted, the SQLIT
4ac80 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41 4c E_WSD and GLOBAL
4ac90 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f 6d .** macros becom
4aca0 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61 76 e no-ops and hav
4acb0 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61 6e e zero performan
4acc0 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 69 ce impact..*/.#i
4acd0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
4ace0 5f 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20 53 _WSD. #define S
4acf0 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 0a QLITE_WSD const.
4ad00 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c #define GLOBAL
4ad10 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c 69 (t,v) (*(t*)sqli
4ad20 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f te3_wsd_find((vo
4ad30 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f 66 id*)&(v), sizeof
4ad40 28 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65 20 (v))). #define
4ad50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
4ad60 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 fig GLOBAL(struc
4ad70 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 2c t Sqlite3Config,
4ad80 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29 0a sqlite3Config).
4ad90 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e 74 SQLITE_API int
4ada0 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 sqlite3_wsd_ini
4adb0 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 3b t(int N, int J);
4adc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76 6f .SQLITE_API vo
4add0 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64 5f id *sqlite3_wsd_
4ade0 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69 6e find(void *K, in
4adf0 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 t L);.#else. #d
4ae00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 efine SQLITE_WSD
4ae10 20 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 . #define GLOB
4ae20 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65 66 AL(t,v) v. #def
4ae30 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 ine sqlite3Globa
4ae40 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 lConfig sqlite3C
4ae50 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f 2a onfig.#endif../*
4ae60 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
4ae70 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 g macros are use
4ae80 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 63 6f d to suppress co
4ae90 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 mpiler warnings
4aea0 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 69 and to.** make i
4aeb0 74 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61 6e t clear to human
4aec0 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 61 20 readers when a
4aed0 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 function paramet
4aee0 65 72 20 69 73 20 64 65 6c 69 62 65 72 61 74 65 er is deliberate
4aef0 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73 ly .** left unus
4af00 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f ed within the bo
4af10 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e dy of a function
4af20 2e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20 68 . This usually h
4af30 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 61 appens when.** a
4af40 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
4af50 6c 65 64 20 76 69 61 20 61 20 66 75 6e 63 74 69 led via a functi
4af60 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20 on pointer. For
4af70 65 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a 20 example the .**
4af80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
4af90 66 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 61 f an SQL aggrega
4afa0 74 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 6b te step callback
4afb0 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 65 may not use the
4afc0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e .** parameter in
4afd0 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 6d dicating the num
4afe0 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ber of arguments
4aff0 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 61 passed to the a
4b000 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66 20 ggregate,.** if
4b010 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 it knows that th
4b020 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20 65 is is enforced e
4b030 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 lsewhere..**.**
4b040 57 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 When a function
4b050 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 parameter is not
4b060 20 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69 74 used at all wit
4b070 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 hin the body of
4b080 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 69 a function,.** i
4b090 74 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20 6e t is generally n
4b0a0 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20 6f amed "NotUsed" o
4b0b0 72 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f 20 r "NotUsed2" to
4b0c0 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65 6e make things even
4b0d0 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f 77 clearer..** How
4b0e0 65 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63 72 ever, these macr
4b0f0 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 75 os may also be u
4b100 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 sed to suppress
4b110 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65 64 warnings related
4b120 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 to.** parameter
4b130 73 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d 61 s that may or ma
4b140 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64 65 y not be used de
4b150 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 69 pending on compi
4b160 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e 0a lation options..
4b170 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 ** For example t
4b180 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 hose parameters
4b190 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73 73 only used in ass
4b1a0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
4b1b0 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63 61 . In these.** ca
4b1c0 73 65 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 ses the paramete
4b1d0 72 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73 20 rs are named as
4b1e0 70 65 72 20 74 68 65 20 75 73 75 61 6c 20 63 6f per the usual co
4b1f0 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 nventions..*/.#d
4b200 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 efine UNUSED_PAR
4b210 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 29 AMETER(x) (void)
4b220 28 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 (x).#define UNUS
4b230 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78 2c ED_PARAMETER2(x,
4b240 79 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 y) UNUSED_PARAME
4b250 54 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50 41 TER(x),UNUSED_PA
4b260 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a 2a RAMETER(y)../*.*
4b270 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 * Forward refere
4b280 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74 75 72 nces to structur
4b290 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 es.*/.typedef st
4b2a0 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 67 ruct AggInfo Agg
4b2b0 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 Info;.typedef st
4b2c0 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 ruct AuthContext
4b2d0 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74 79 AuthContext;.ty
4b2e0 70 65 64 65 66 20 73 74 72 75 63 74 20 41 75 74 pedef struct Aut
4b2f0 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69 6e 63 oincInfo Autoinc
4b300 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 Info;.typedef st
4b310 72 75 63 74 20 42 69 74 76 65 63 20 42 69 74 76 ruct Bitvec Bitv
4b320 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ec;.typedef stru
4b330 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65 74 ct RowSet RowSet
4b340 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b350 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71 CollSeq CollSeq
4b360 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b370 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a Column Column;.
4b380 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 typedef struct D
4b390 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74 b Db;.typedef st
4b3a0 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65 ruct Schema Sche
4b3b0 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 ma;.typedef stru
4b3c0 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79 ct Expr Expr;.ty
4b3d0 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70 pedef struct Exp
4b3e0 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a rList ExprList;.
4b3f0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 typedef struct E
4b400 78 70 72 53 70 61 6e 20 45 78 70 72 53 70 61 6e xprSpan ExprSpan
4b410 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b420 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 FKey FKey;.type
4b430 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 def struct FuncD
4b440 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 ef FuncDef;.type
4b450 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 def struct FuncD
4b460 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 efHash FuncDefHa
4b470 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 sh;.typedef stru
4b480 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 ct IdList IdList
4b490 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b4a0 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 Index Index;.ty
4b4b0 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 64 pedef struct Ind
4b4c0 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53 61 exSample IndexSa
4b4d0 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 mple;.typedef st
4b4e0 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 ruct KeyClass Ke
4b4f0 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66 20 yClass;.typedef
4b500 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b struct KeyInfo K
4b510 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 eyInfo;.typedef
4b520 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 struct Lookaside
4b530 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70 65 Lookaside;.type
4b540 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 def struct Looka
4b550 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 69 sideSlot Lookasi
4b560 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66 20 deSlot;.typedef
4b570 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f struct Module Mo
4b580 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 dule;.typedef st
4b590 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ruct NameContext
4b5a0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 NameContext;.ty
4b5b0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 72 pedef struct Par
4b5c0 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64 65 se Parse;.typede
4b5d0 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f 69 f struct Savepoi
4b5e0 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74 79 nt Savepoint;.ty
4b5f0 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 6c pedef struct Sel
4b600 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70 65 ect Select;.type
4b610 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c 69 def struct SrcLi
4b620 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70 65 st SrcList;.type
4b630 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41 63 def struct StrAc
4b640 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74 79 cum StrAccum;.ty
4b650 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 pedef struct Tab
4b660 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 le Table;.typede
4b670 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c 6f f struct TableLo
4b680 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 79 ck TableLock;.ty
4b690 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f 6b pedef struct Tok
4b6a0 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 65 en Token;.typede
4b6b0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 f struct Trigger
4b6c0 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b 0a Prg TriggerPrg;.
4b6d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 typedef struct T
4b6e0 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 67 riggerStep Trigg
4b6f0 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 20 erStep;.typedef
4b700 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 54 struct Trigger T
4b710 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66 20 rigger;.typedef
4b720 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64 52 struct UnpackedR
4b730 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52 65 ecord UnpackedRe
4b740 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73 74 cord;.typedef st
4b750 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61 62 ruct VTable VTab
4b760 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 le;.typedef stru
4b770 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 72 ct Walker Walker
4b780 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
4b790 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 65 WherePlan Where
4b7a0 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 Plan;.typedef st
4b7b0 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 57 ruct WhereInfo W
4b7c0 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 65 hereInfo;.typede
4b7d0 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 f struct WhereLe
4b7e0 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b 0a vel WhereLevel;.
4b7f0 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f 75 ./*.** Defer sou
4b800 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e 64 rcing vdbe.h and
4b810 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 61 btree.h until a
4b820 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 6e fter the "u8" an
4b830 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 6c d .** "BusyHandl
4b840 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 64 er" typedefs. vd
4b850 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 72 be.h also requir
4b860 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 20 es a few of the
4b870 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 65 opaque.** pointe
4b880 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 75 r types (i.e. Fu
4b890 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 61 ncDef) defined a
4b8a0 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a bove..*/./******
4b8b0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
4b8c0 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 btree.h in the
4b8d0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
4b8e0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
4b8f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
4b900 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
4b910 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a ile btree.h ****
4b920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b940 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
4b950 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
4b960 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
4b970 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
4b980 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
4b990 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
4b9a0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
4b9b0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
4b9c0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
4b9d0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
4b9e0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
4b9f0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
4ba00 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
4ba10 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
4ba20 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
4ba30 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
4ba40 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
4ba50 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
4ba60 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
4ba70 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
4ba80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ba90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4bab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
4bac0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
4bad0 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ile defines the
4bae0 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 interface that t
4baf0 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 65 he sqlite B-Tree
4bb00 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 74 file.** subsyst
4bb10 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 em. See comment
4bb20 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 s in the source
4bb30 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 69 code for a detai
4bb40 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 0a led description.
4bb50 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 ** of what each
4bb60 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e interface routin
4bb70 65 20 64 6f 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 e does..*/.#ifnd
4bb80 65 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 ef _BTREE_H_.#de
4bb90 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a fine _BTREE_H_..
4bba0 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 /* TODO: This de
4bbb0 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 finition is just
4bbc0 20 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 included so oth
4bbd0 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 er modules compi
4bbe0 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 le. It.** needs
4bbf0 74 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 2e to be revisited.
4bc00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
4bc10 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 TE_N_BTREE_META
4bc20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 10../*.** If def
4bc30 69 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f ined as non-zero
4bc40 2c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 , auto-vacuum is
4bc50 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 enabled by defa
4bc60 75 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a ult. Otherwise.*
4bc70 2a 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 72 * it must be tur
4bc80 6e 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 ned on for each
4bc90 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 22 database using "
4bca0 50 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 PRAGMA auto_vacu
4bcb0 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e um = 1"..*/.#ifn
4bcc0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
4bcd0 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 LT_AUTOVACUUM.
4bce0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
4bcf0 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 EFAULT_AUTOVACUU
4bd00 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 M 0.#endif..#def
4bd10 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 ine BTREE_AUTOVA
4bd20 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 CUUM_NONE 0
4bd30 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 /* Do not do
4bd40 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 auto-vacuum */.#
4bd50 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 define BTREE_AUT
4bd60 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 OVACUUM_FULL 1
4bd70 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c /* Do full
4bd80 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a auto-vacuum */.
4bd90 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 #define BTREE_AU
4bda0 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 TOVACUUM_INCR 2
4bdb0 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d /* Increm
4bdc0 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a ental vacuum */.
4bdd0 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 ./*.** Forward d
4bde0 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 eclarations of s
4bdf0 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 tructure.*/.type
4be00 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 def struct Btree
4be10 20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20 Btree;.typedef
4be20 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 struct BtCursor
4be30 42 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 BtCursor;.typede
4be40 66 20 73 74 72 75 63 74 20 42 74 53 68 61 72 65 f struct BtShare
4be50 64 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 65 d BtShared;.type
4be60 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 def struct Btree
4be70 4d 75 74 65 78 41 72 72 61 79 20 42 74 72 65 65 MutexArray Btree
4be80 4d 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a MutexArray;../*.
4be90 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
4bea0 65 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 e records all of
4beb0 20 74 68 65 20 42 74 72 65 65 73 20 74 68 61 74 the Btrees that
4bec0 20 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a need to hold.**
4bed0 20 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 a mutex before
4bee0 77 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 33 we enter sqlite3
4bef0 56 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 VdbeExec(). The
4bf00 20 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 Btrees are.** a
4bf10 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 74 re placed in aBt
4bf20 72 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f ree[] in order o
4bf30 66 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e f aBtree[]->pBt.
4bf40 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 That way,.** w
4bf50 65 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 e can always loc
4bf60 6b 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 k and unlock the
4bf70 6d 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a m all quickly..*
4bf80 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 /.struct BtreeMu
4bf90 74 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 texArray {. int
4bfa0 20 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 65 nMutex;. Btree
4bfb0 20 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 5f *aBtree[SQLITE_
4bfc0 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b MAX_ATTACHED+1];
4bfd0 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 .};...SQLITE_PRI
4bfe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4bff0 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e BtreeOpen(. con
4c000 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
4c010 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 me, /* Name of
4c020 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
4c030 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 o open */. sqli
4c040 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
4c050 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 /* Associat
4c060 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ed database conn
4c070 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 ection */. Btre
4c080 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 e **ppBtree,
4c090 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f /* Return o
4c0a0 70 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 20 pen Btree* here
4c0b0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
4c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4c0d0 20 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 Flags */. int
4c0e0 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 vfsFlags
4c0f0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 /* Flags pa
4c100 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 ssed through to
4c110 56 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a VFS open */.);..
4c120 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 /* The flags par
4c130 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
4c140 33 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 3BtreeOpen can b
4c150 65 20 74 68 65 20 62 69 74 77 69 73 65 20 6f 72 e the bitwise or
4c160 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f of the.** follo
4c170 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a wing values..**.
4c180 2a 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 ** NOTE: These
4c190 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 values must matc
4c1a0 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 h the correspond
4c1b0 69 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 65 ing PAGER_ value
4c1c0 73 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e s in.** pager.h.
4c1d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 .*/.#define BTRE
4c1e0 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 E_OMIT_JOURNAL
4c1f0 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 1 /* Do not use
4c200 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 journal. No ar
4c210 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e gument */.#defin
4c220 65 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c e BTREE_NO_READL
4c230 4f 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 OCK 2 /* Omit
4c240 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 readlocks on re
4c250 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a adonly files */.
4c260 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 #define BTREE_ME
4c270 4d 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 2f MORY 4 /
4c280 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 * In-memory DB.
4c290 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a No argument */.
4c2a0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 #define BTREE_RE
4c2b0 41 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 2f ADONLY 8 /
4c2c0 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 * Open the datab
4c2d0 61 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 ase in read-only
4c2e0 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 mode */.#define
4c2f0 20 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 45 BTREE_READWRITE
4c300 20 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 16 /* Open
4c310 66 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 for both reading
4c320 20 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a and writing */.
4c330 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 52 #define BTREE_CR
4c340 45 41 54 45 20 20 20 20 20 20 20 33 32 20 20 2f EATE 32 /
4c350 2a 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74 * Create the dat
4c360 61 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 73 abase if it does
4c370 20 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 not exist */..S
4c380 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c390 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c t sqlite3BtreeCl
4c3a0 6f 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ose(Btree*);.SQL
4c3b0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c3c0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
4c3d0 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a 2c acheSize(Btree*,
4c3e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4c3f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c400 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 BtreeSetSafetyLe
4c410 76 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 vel(Btree*,int,i
4c420 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4c430 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c440 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 treeSyncDisabled
4c450 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c470 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 ite3BtreeSetPage
4c480 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 Size(Btree *p, i
4c490 6e 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 6e nt nPagesize, in
4c4a0 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 t nReserve, int
4c4b0 65 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 52 eFix);.SQLITE_PR
4c4c0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c4d0 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 3BtreeGetPageSiz
4c4e0 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 e(Btree*);.SQLIT
4c4f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c500 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 lite3BtreeMaxPag
4c510 65 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e eCount(Btree*,in
4c520 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4c530 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c540 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 reeGetReserve(Bt
4c550 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
4c560 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c570 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 3BtreeSetAutoVac
4c580 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 uum(Btree *, int
4c590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c5a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c5b0 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 eeGetAutoVacuum(
4c5c0 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Btree *);.SQLITE
4c5d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c5e0 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 ite3BtreeBeginTr
4c5f0 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b ans(Btree*,int);
4c600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c610 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c620 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 CommitPhaseOne(B
4c630 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 tree*, const cha
4c640 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c r *zMaster);.SQL
4c650 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c660 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
4c670 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 itPhaseTwo(Btree
4c680 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4c690 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c6a0 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a reeCommit(Btree*
4c6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c6c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c6d0 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 eeRollback(Btree
4c6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4c6f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c700 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 reeBeginStmt(Btr
4c710 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ee*,int);.SQLITE
4c720 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c730 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
4c740 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 able(Btree*, int
4c750 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 *, int flags);.S
4c760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c770 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
4c780 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b InTrans(Btree*);
4c790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c7a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c7b0 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 IsInReadTrans(Bt
4c7c0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
4c7d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c7e0 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 3BtreeIsInBackup
4c7f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c800 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
4c810 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d qlite3BtreeSchem
4c820 61 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 a(Btree *, int,
4c830 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29 void(*)(void *))
4c840 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c850 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c860 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 eSchemaLocked(Bt
4c870 72 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 51 ree *pBtree);.SQ
4c880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c890 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 sqlite3BtreeLoc
4c8a0 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 42 kTable(Btree *pB
4c8b0 74 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 tree, int iTab,
4c8c0 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b u8 isWriteLock);
4c8d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c8e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c8f0 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 Savepoint(Btree
4c900 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 *, int, int);..S
4c910 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
4c920 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
4c930 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 3BtreeGetFilenam
4c940 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 e(Btree *);.SQLI
4c950 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
4c960 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
4c970 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d reeGetJournalnam
4c980 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 e(Btree *);.SQLI
4c990 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c9a0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 qlite3BtreeCopyF
4c9b0 69 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 ile(Btree *, Btr
4c9c0 65 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ee *);..SQLITE_P
4c9d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c9e0 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 e3BtreeIncrVacuu
4c9f0 6d 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 m(Btree *);../*
4ca00 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 The flags parame
4ca10 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 ter to sqlite3Bt
4ca20 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 reeCreateTable c
4ca30 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 an be the bitwis
4ca40 65 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 e OR.** of the f
4ca50 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a ollowing flags:.
4ca60 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
4ca70 5f 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 _INTKEY 1
4ca80 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e /* Table has on
4ca90 6c 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 ly 64-bit signed
4caa0 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f integer keys */
4cab0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a .#define BTREE_Z
4cac0 45 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f ERODATA 2 /
4cad0 2a 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 * Table has keys
4cae0 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 only - no data
4caf0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
4cb00 5f 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 _LEAFDATA 4
4cb10 20 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 /* Data stored
4cb20 69 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 in leaves only.
4cb30 20 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 Implies INTKEY
4cb40 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */..SQLITE_PRIVA
4cb50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4cb60 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 reeDropTable(Btr
4cb70 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b ee*, int, int*);
4cb80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4cb90 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4cba0 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 ClearTable(Btree
4cbb0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 *, int, int*);.S
4cbc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4cbd0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 id sqlite3BtreeT
4cbe0 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 ripAllCursors(Bt
4cbf0 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c ree*, int);..SQL
4cc00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4cc10 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
4cc20 4d 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72 Meta(Btree *pBtr
4cc30 65 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 ee, int idx, u32
4cc40 20 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 *pValue);.SQLIT
4cc50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4cc60 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
4cc70 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 Meta(Btree*, int
4cc80 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 idx, u32 value)
4cc90 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 ;../*.** The sec
4cca0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f ond parameter to
4ccb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
4ccc0 4d 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 42 Meta or sqlite3B
4ccd0 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a 2a treeUpdateMeta.*
4cce0 2a 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 * should be one
4ccf0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
4cd00 20 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e 74 values. The int
4cd10 65 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 eger values are
4cd20 61 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f 20 assigned .** to
4cd30 63 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 61 constants so tha
4cd40 74 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 t the offset of
4cd50 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
4cd60 67 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a 2a g field in an.**
4cd70 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
4cd80 20 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 66 header may be f
4cd90 6f 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 66 ound using the f
4cda0 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c 61 ollowing formula
4cdb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 74 :.**.** offset
4cdc0 20 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 34 = 36 + (idx * 4
4cdd0 29 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ).**.** For exam
4cde0 70 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 61 ple, the free-pa
4cdf0 67 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 69 ge-count field i
4ce00 73 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 74 s located at byt
4ce10 65 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a 2a e offset 36 of.*
4ce20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
4ce30 69 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 20 ile header. The
4ce40 69 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 67 incr-vacuum-flag
4ce50 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 field is locate
4ce60 64 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 66 d at.** byte off
4ce70 73 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a set 64 (== 36+4*
4ce80 37 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 7)..*/.#define B
4ce90 54 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 TREE_FREE_PAGE_C
4cea0 4f 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 69 OUNT 0.#defi
4ceb0 6e 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f ne BTREE_SCHEMA_
4cec0 56 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a 23 VERSION 1.#
4ced0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 4c define BTREE_FIL
4cee0 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 20 E_FORMAT
4cef0 20 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 2.#define BTREE
4cf00 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
4cf10 49 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 42 IZE 3.#define B
4cf20 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f TREE_LARGEST_ROO
4cf30 54 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 69 T_PAGE 4.#defi
4cf40 6e 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e ne BTREE_TEXT_EN
4cf50 43 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a 23 CODING 5.#
4cf60 64 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 45 define BTREE_USE
4cf70 52 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 R_VERSION
4cf80 20 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 6.#define BTREE
4cf90 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 20 _INCR_VACUUM
4cfa0 20 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 50 7..SQLITE_P
4cfb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4cfc0 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 e3BtreeCursor(.
4cfd0 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 Btree*,
4cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cff0 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 /* BTree c
4d000 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 ontaining table
4d010 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 to open */. int
4d020 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 iTable,
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d040 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f /* Index of ro
4d050 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ot page */. int
4d060 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 wrFlag,
4d070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d080 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 /* 1 for writi
4d090 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d ng. 0 for read-
4d0a0 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 only */. struct
4d0b0 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 KeyInfo*,
4d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4d0d0 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 * First argument
4d0e0 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 to compare func
4d0f0 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 tion */. BtCurs
4d100 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 or *pCursor
4d110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4d120 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 * Space to write
4d130 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 cursor structur
4d140 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 e */.);.SQLITE_P
4d150 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d160 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a e3BtreeCursorSiz
4d170 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f e(void);.SQLITE_
4d180 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
4d190 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a ite3BtreeCursorZ
4d1a0 65 72 6f 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a ero(BtCursor*);.
4d1b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d1c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d1d0 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 CloseCursor(BtCu
4d1e0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
4d1f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d200 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
4d210 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f acked(. BtCurso
4d220 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 r*,. UnpackedRe
4d230 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a 20 20 cord *pUnKey,.
4d240 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20 69 6e i64 intKey,. in
4d250 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20 2a 70 t bias,. int *p
4d260 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Res.);.SQLITE_PR
4d270 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d280 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 3BtreeCursorHasM
4d290 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 oved(BtCursor*,
4d2a0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
4d2b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d2c0 33 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 3BtreeDelete(BtC
4d2d0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
4d2e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d2f0 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 42 te3BtreeInsert(B
4d300 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73 74 20 tCursor*, const
4d310 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 void *pKey, i64
4d320 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 nKey,.
4d330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d340 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f const vo
4d350 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e id *pData, int n
4d360 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 Data,.
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d380 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 int nZer
4d390 6f 2c 20 69 6e 74 20 62 69 61 73 2c 20 69 6e 74 o, int bias, int
4d3a0 20 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 53 51 seekResult);.SQ
4d3b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d3c0 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 sqlite3BtreeFir
4d3d0 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e st(BtCursor*, in
4d3e0 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 t *pRes);.SQLITE
4d3f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4d400 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 ite3BtreeLast(Bt
4d410 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 Cursor*, int *pR
4d420 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 es);.SQLITE_PRIV
4d430 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d440 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f treeNext(BtCurso
4d450 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a r*, int *pRes);.
4d460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d470 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 nt sqlite3BtreeE
4d480 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 of(BtCursor*);.S
4d490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d4a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 t sqlite3BtreePr
4d4b0 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72 2a evious(BtCursor*
4d4c0 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 , int *pRes);.SQ
4d4d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d4e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
4d4f0 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 Size(BtCursor*,
4d500 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c i64 *pSize);.SQL
4d510 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d520 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 sqlite3BtreeKey(
4d530 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f BtCursor*, u32 o
4d540 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
4d550 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 void*);.SQLITE_P
4d560 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
4d570 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b d *sqlite3BtreeK
4d580 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 eyFetch(BtCursor
4d590 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 *, int *pAmt);.S
4d5a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
4d5b0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
4d5c0 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 28 3BtreeDataFetch(
4d5d0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
4d5e0 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pAmt);.SQLITE_PR
4d5f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d600 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 3BtreeDataSize(B
4d610 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 2a 70 tCursor*, u32 *p
4d620 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Size);.SQLITE_PR
4d630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d640 33 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 3BtreeData(BtCur
4d650 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 sor*, u32 offset
4d660 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a , u32 amt, void*
4d670 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d680 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
4d690 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 reeSetCachedRowi
4d6a0 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73 71 6c d(BtCursor*, sql
4d6b0 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c ite3_int64);.SQL
4d6c0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
4d6d0 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
4d6e0 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 3BtreeGetCachedR
4d6f0 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a 29 3b owid(BtCursor*);
4d700 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
4d710 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
4d720 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
4d730 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 2a 61 k(Btree*, int *a
4d740 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f 74 2c Root, int nRoot,
4d750 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c int, int*);.SQL
4d760 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 72 75 ITE_PRIVATE stru
4d770 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 ct Pager *sqlite
4d780 33 42 74 72 65 65 50 61 67 65 72 28 42 74 72 65 3BtreePager(Btre
4d790 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 e*);..SQLITE_PRI
4d7a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d7b0 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43 BtreePutData(BtC
4d7c0 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 ursor*, u32 offs
4d7d0 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 et, u32 amt, voi
4d7e0 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d*);.SQLITE_PRIV
4d7f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4d800 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c BtreeCacheOverfl
4d810 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a ow(BtCursor *);.
4d820 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4d830 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4d840 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 ClearCursor(BtCu
4d850 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e 64 65 rsor *);..#ifnde
4d860 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f f NDEBUG.SQLITE_
4d870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d880 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 te3BtreeCursorIs
4d890 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 2a 29 Valid(BtCursor*)
4d8a0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 ;.#endif..#ifnde
4d8b0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 f SQLITE_OMIT_BT
4d8c0 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54 45 5f REECOUNT.SQLITE_
4d8d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d8e0 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 te3BtreeCount(Bt
4d8f0 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 Cursor *, i64 *)
4d900 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
4d910 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
4d920 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d930 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
4d940 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 2a orInfo(BtCursor*
4d950 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 , int*, int);.SQ
4d960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4d970 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 d sqlite3BtreeCu
4d980 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65 2a 29 rsorList(Btree*)
4d990 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
4d9a0 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 75 73 If we are not us
4d9b0 69 6e 67 20 73 68 61 72 65 64 20 63 61 63 68 65 ing shared cache
4d9c0 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 , then there is
4d9d0 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 75 73 no need to.** us
4d9e0 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61 63 63 e mutexes to acc
4d9f0 65 73 73 20 74 68 65 20 42 74 53 68 61 72 65 64 ess the BtShared
4da00 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 53 6f structures. So
4da10 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45 6e 74 make the.** Ent
4da20 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70 72 6f er and Leave pro
4da30 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73 2e 0a cedures no-ops..
4da40 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
4da50 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
4da60 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 CHE.SQLITE_PRIVA
4da70 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
4da80 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65 3BtreeEnter(Btre
4da90 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4daa0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
4dab0 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
4dac0 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 sqlite3*);.#else
4dad0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
4dae0 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 20 0a 3BtreeEnter(X) .
4daf0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4db00 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 BtreeEnterAll(X)
4db10 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 .#endif..#if !de
4db20 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
4db30 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 T_SHARED_CACHE)
4db40 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 && SQLITE_THREAD
4db50 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 SAFE.SQLITE_PRIV
4db60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
4db70 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 e3BtreeLeave(Btr
4db80 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4db90 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
4dba0 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 te3BtreeEnterCur
4dbb0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a sor(BtCursor*);.
4dbc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4dbd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4dbe0 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 eeLeaveCursor(Bt
4dbf0 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 Cursor*);.SQLITE
4dc00 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4dc10 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
4dc20 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a eAll(sqlite3*);.
4dc30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4dc40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4dc50 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 eeMutexArrayEnte
4dc60 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 r(BtreeMutexArra
4dc70 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 y*);.SQLITE_PRIV
4dc80 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
4dc90 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
4dca0 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 74 65 yLeave(BtreeMute
4dcb0 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 xArray*);.SQLITE
4dcc0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4dcd0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
4dce0 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72 xArrayInsert(Btr
4dcf0 65 65 4d 75 74 65 78 41 72 72 61 79 2a 2c 20 42 eeMutexArray*, B
4dd00 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 tree*);.#ifndef
4dd10 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 73 NDEBUG. /* Thes
4dd20 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 e routines are u
4dd30 73 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 sed inside asser
4dd40 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f t() statements o
4dd50 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly. */.SQLITE_P
4dd60 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
4dd70 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
4dd80 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c tex(Btree*);.SQL
4dd90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
4dda0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f t sqlite3BtreeHo
4ddb0 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 ldsAllMutexes(sq
4ddc0 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 69 66 0a lite3*);.#endif.
4ddd0 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e 65 20 #else..# define
4dde0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
4ddf0 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 e(X).# define sq
4de00 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43 lite3BtreeEnterC
4de10 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69 6e ursor(X).# defin
4de20 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 e sqlite3BtreeLe
4de30 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 20 64 aveCursor(X).# d
4de40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
4de50 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a 23 20 eeLeaveAll(X).#
4de60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4de70 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
4de80 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 er(X).# define s
4de90 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
4dea0 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23 20 ArrayLeave(X).#
4deb0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4dec0 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
4ded0 65 72 74 28 58 2c 59 29 0a 0a 23 20 64 65 66 69 ert(X,Y)..# defi
4dee0 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 ne sqlite3BtreeH
4def0 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a 23 oldsMutex(X) 1.#
4df00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4df10 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
4df20 78 65 73 28 58 29 20 31 0a 23 65 6e 64 69 66 0a xes(X) 1.#endif.
4df30 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 52 ..#endif /* _BTR
4df40 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a EE_H_ */../*****
4df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
4df60 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a btree.h *******
4df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4df80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
4dfb0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
4dfc0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
4dfd0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
4dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
4dff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
4e000 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 20 e vdbe.h in the
4e010 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
4e020 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
4e030 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
4e040 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
4e050 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a file vdbe.h ****
4e060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e080 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
4e090 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
4e0a0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
4e0b0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
4e0c0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
4e0d0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
4e0e0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
4e0f0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
4e100 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
4e110 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
4e120 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
4e130 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
4e140 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
4e150 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
4e160 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
4e170 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
4e180 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
4e190 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
4e1a0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
4e1b0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
4e1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e200 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 20 .** Header file
4e210 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c 20 for the Virtual
4e220 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 20 DataBase Engine
4e230 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 69 (VDBE).**.** Thi
4e240 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 73 s header defines
4e250 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 the interface t
4e260 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 61 o the virtual da
4e270 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a 2a tabase engine.**
4e280 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 56 or VDBE. The V
4e290 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 DBE implements a
4e2a0 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 69 n abstract machi
4e2b0 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a 2a ne that runs a.*
4e2c0 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d * simple program
4e2d0 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 6d to access and m
4e2e0 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 6c odify the underl
4e2f0 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a 2a ying database..*
4e300 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 /.#ifndef _SQLIT
4e310 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 6e E_VDBE_H_.#defin
4e320 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 e _SQLITE_VDBE_H
4e330 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c _../*.** A singl
4e340 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 61 e VDBE is an opa
4e350 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 que structure na
4e360 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e 6c med "Vdbe". Onl
4e370 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e y routines.** in
4e380 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65 the source file
4e390 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 72 sqliteVdbe.c ar
4e3a0 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 e allowed to see
4e3b0 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a 20 the insides.**
4e3c0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
4e3d0 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 e..*/.typedef st
4e3e0 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b 0a ruct Vdbe Vdbe;.
4e3f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 73 ./*.** The names
4e400 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
4e410 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 64 g types declared
4e420 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 72 in vdbeInt.h ar
4e430 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 6f e required.** fo
4e440 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 66 r the VdbeOp def
4e450 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 inition..*/.type
4e460 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 46 def struct VdbeF
4e470 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 79 unc VdbeFunc;.ty
4e480 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
4e490 20 4d 65 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 Mem;.typedef st
4e4a0 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d 20 ruct SubProgram
4e4b0 53 75 62 50 72 6f 67 72 61 6d 3b 0a 0a 2f 2a 0a SubProgram;../*.
4e4c0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 73 74 ** A single inst
4e4d0 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ruction of the v
4e4e0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 irtual machine h
4e4f0 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 as an opcode.**
4e500 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73 20 74 and as many as t
4e510 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e 20 20 hree operands.
4e520 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 The instruction
4e530 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a 20 61 is recorded.** a
4e540 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
4e550 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
4e560 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73 74 72 tructure:.*/.str
4e570 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20 20 75 uct VdbeOp {. u
4e580 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 8 opcode;
4e590 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 /* What opera
4e5a0 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 tion to perform
4e5b0 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 */. signed char
4e5c0 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e 65 20 p4type; /* One
4e5d0 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20 63 6f of the P4_xxx co
4e5e0 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34 20 2a nstants for p4 *
4e5f0 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73 3b 20 /. u8 opflags;
4e600 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 /* Mask
4e610 6f 66 20 74 68 65 20 4f 50 46 4c 47 5f 2a 20 66 of the OPFLG_* f
4e620 6c 61 67 73 20 69 6e 20 6f 70 63 6f 64 65 73 2e lags in opcodes.
4e630 68 20 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 h */. u8 p5;
4e640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
4e650 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 fth parameter is
4e660 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 an unsigned cha
4e670 72 61 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 racter */. int
4e680 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p1;
4e690 2f 2a 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 /* First operand
4e6a0 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 */. int p2;
4e6b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 /* Sec
4e6c0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f ond parameter (o
4e6d0 66 74 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 ften the jump de
4e6e0 73 74 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 stination) */.
4e6f0 69 6e 74 20 70 33 3b 20 20 20 20 20 20 20 20 20 int p3;
4e700 20 20 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64 /* The third
4e710 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 parameter */.
4e720 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 union {
4e730 20 20 20 20 2f 2a 20 66 6f 75 72 74 68 20 70 61 /* fourth pa
4e740 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 rameter */. i
4e750 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
4e760 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 /* Integer
4e770 20 76 61 6c 75 65 20 69 66 20 70 34 74 79 70 65 value if p4type
4e780 3d 3d 50 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 ==P4_INT32 */.
4e790 20 20 76 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 void *p;
4e7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 /* Gene
4e7b0 72 69 63 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ric pointer */.
4e7c0 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 char *z;
4e7d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
4e7e0 6e 74 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 nter to data for
4e7f0 20 73 74 72 69 6e 67 20 28 63 68 61 72 20 61 72 string (char ar
4e800 72 61 79 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 ray) types */.
4e810 20 20 69 36 34 20 2a 70 49 36 34 3b 20 20 20 20 i64 *pI64;
4e820 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
4e830 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e840 50 34 5f 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 P4_INT64 */.
4e850 64 6f 75 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 double *pReal;
4e860 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 /* Used w
4e870 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 hen p4type is P4
4e880 5f 52 45 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e _REAL */. Fun
4e890 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 cDef *pFunc;
4e8a0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4e8b0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 46 55 p4type is P4_FU
4e8c0 4e 43 44 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 NCDEF */. Vdb
4e8d0 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 eFunc *pVdbeFunc
4e8e0 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e ; /* Used when
4e8f0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 56 44 p4type is P4_VD
4e900 42 45 46 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f BEFUNC */. Co
4e910 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 llSeq *pColl;
4e920 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
4e930 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 43 n p4type is P4_C
4e940 4f 4c 4c 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 OLLSEQ */. Me
4e950 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
4e960 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
4e970 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4d n p4type is P4_M
4e980 45 4d 20 2a 2f 0a 20 20 20 20 56 54 61 62 6c 65 EM */. VTable
4e990 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 20 20 *pVtab;
4e9a0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 /* Used when p4
4e9b0 74 79 70 65 20 69 73 20 50 34 5f 56 54 41 42 20 type is P4_VTAB
4e9c0 2a 2f 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a */. KeyInfo *
4e9d0 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a pKeyInfo; /*
4e9e0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
4e9f0 65 20 69 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 e is P4_KEYINFO
4ea00 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 */. int *ai;
4ea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4ea20 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
4ea30 65 20 69 73 20 50 34 5f 49 4e 54 41 52 52 41 59 e is P4_INTARRAY
4ea40 20 2a 2f 0a 20 20 20 20 53 75 62 50 72 6f 67 72 */. SubProgr
4ea50 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 2f am *pProgram; /
4ea60 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4ea70 70 65 20 69 73 20 50 34 5f 53 55 42 50 52 4f 47 pe is P4_SUBPROG
4ea80 52 41 4d 20 2a 2f 0a 20 20 7d 20 70 34 3b 0a 23 RAM */. } p4;.#
4ea90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
4eaa0 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d UG. char *zComm
4eab0 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ent; /*
4eac0 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d 70 72 Comment to impr
4ead0 6f 76 65 20 72 65 61 64 61 62 69 6c 69 74 79 20 ove readability
4eae0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 */.#endif.#ifdef
4eaf0 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 VDBE_PROFILE.
4eb00 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 int cnt;
4eb10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
4eb20 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 69 73 er of times this
4eb30 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 instruction was
4eb40 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 75 executed */. u
4eb50 36 34 20 63 79 63 6c 65 73 3b 20 20 20 20 20 20 64 cycles;
4eb60 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
4eb70 20 74 69 6d 65 20 73 70 65 6e 74 20 65 78 65 63 time spent exec
4eb80 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 uting this instr
4eb90 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 uction */.#endif
4eba0 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 .};.typedef stru
4ebb0 63 74 20 56 64 62 65 4f 70 20 56 64 62 65 4f 70 ct VdbeOp VdbeOp
4ebc0 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 75 62 2d ;.../*.** A sub-
4ebd0 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 routine used to
4ebe0 69 6d 70 6c 65 6d 65 6e 74 20 61 20 74 72 69 67 implement a trig
4ebf0 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a ger program..*/.
4ec00 73 74 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 struct SubProgra
4ec10 6d 20 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f m {. VdbeOp *aO
4ec20 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
4ec30 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 /* Array of
4ec40 6f 70 63 6f 64 65 73 20 66 6f 72 20 73 75 62 2d opcodes for sub-
4ec50 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 program */. int
4ec60 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 nOp;
4ec70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6c /* El
4ec80 65 6d 65 6e 74 73 20 69 6e 20 61 4f 70 5b 5d 20 ements in aOp[]
4ec90 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 */. int nMem;
4eca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ecb0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
4ecc0 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 72 65 71 memory cells req
4ecd0 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e uired */. int n
4ece0 43 73 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Csr;
4ecf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
4ed00 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 72 65 er of cursors re
4ed10 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 quired */. int
4ed20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
4ed30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
4ed40 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 ber of pointers
4ed50 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
4ed60 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b e */. void *tok
4ed70 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 en;
4ed80 20 20 20 20 20 2f 2a 20 69 64 20 74 68 61 74 20 /* id that
4ed90 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 may be used to r
4eda0 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65 72 ecursive trigger
4edb0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 s */.};../*.** A
4edc0 20 73 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e smaller version
4edd0 20 6f 66 20 56 64 62 65 4f 70 20 75 73 65 64 20 of VdbeOp used
4ede0 66 6f 72 20 74 68 65 20 56 64 62 65 41 64 64 4f for the VdbeAddO
4edf0 70 4c 69 73 74 28 29 20 66 75 6e 63 74 69 6f 6e pList() function
4ee00 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 because.** it t
4ee10 61 6b 65 73 20 75 70 20 6c 65 73 73 20 73 70 61 akes up less spa
4ee20 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 ce..*/.struct Vd
4ee30 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 beOpList {. u8
4ee40 6f 70 63 6f 64 65 3b 20 20 20 20 20 20 20 20 20 opcode;
4ee50 20 2f 2a 20 57 68 61 74 20 6f 70 65 72 61 74 69 /* What operati
4ee60 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f on to perform */
4ee70 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 . signed char p
4ee80 31 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 1; /* First
4ee90 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 operand */. sig
4eea0 6e 65 64 20 63 68 61 72 20 70 32 3b 20 20 20 20 ned char p2;
4eeb0 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d /* Second param
4eec0 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 eter (often the
4eed0 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e jump destination
4eee0 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 ) */. signed ch
4eef0 61 72 20 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 ar p3; /* Th
4ef00 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a 2f ird parameter */
4ef10 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 .};.typedef stru
4ef20 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 56 64 ct VdbeOpList Vd
4ef30 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a beOpList;../*.**
4ef40 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 Allowed values
4ef50 6f 66 20 56 64 62 65 4f 70 2e 70 34 74 79 70 65 of VdbeOp.p4type
4ef60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e .*/.#define P4_N
4ef70 4f 54 55 53 45 44 20 20 20 20 30 20 20 20 2f 2a OTUSED 0 /*
4ef80 20 54 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 The P4 paramete
4ef90 72 20 69 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f r is not used */
4efa0 0a 23 64 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 .#define P4_DYNA
4efb0 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f MIC (-1) /* Po
4efc0 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e inter to a strin
4efd0 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 g obtained from
4efe0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a sqliteMalloc() *
4eff0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 54 41 /.#define P4_STA
4f000 54 49 43 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 TIC (-2) /* P
4f010 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 ointer to a stat
4f020 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 ic string */.#de
4f030 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 fine P4_COLLSEQ
4f040 20 28 2d 34 29 20 20 2f 2a 20 50 34 20 69 73 20 (-4) /* P4 is
4f050 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 a pointer to a C
4f060 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 ollSeq structure
4f070 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 */.#define P4_F
4f080 55 4e 43 44 45 46 20 20 28 2d 35 29 20 20 2f 2a UNCDEF (-5) /*
4f090 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 P4 is a pointer
4f0a0 20 74 6f 20 61 20 46 75 6e 63 44 65 66 20 73 74 to a FuncDef st
4f0b0 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 ructure */.#defi
4f0c0 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 ne P4_KEYINFO (
4f0d0 2d 36 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 -6) /* P4 is a
4f0e0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 pointer to a Key
4f0f0 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a Info structure *
4f100 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 44 42 /.#define P4_VDB
4f110 45 46 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 EFUNC (-7) /* P
4f120 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
4f130 6f 20 61 20 56 64 62 65 46 75 6e 63 20 73 74 72 o a VdbeFunc str
4f140 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e ucture */.#defin
4f150 65 20 50 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d e P4_MEM (-
4f160 38 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 8) /* P4 is a p
4f170 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a ointer to a Mem*
4f180 20 20 20 20 73 74 72 75 63 74 75 72 65 20 2a 2f structure */
4f190 0a 23 64 65 66 69 6e 65 20 50 34 5f 54 52 41 4e .#define P4_TRAN
4f1a0 53 49 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 SIENT (-9) /* P4
4f1b0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4f1c0 20 61 20 74 72 61 6e 73 69 65 6e 74 20 73 74 72 a transient str
4f1d0 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ing */.#define P
4f1e0 34 5f 56 54 41 42 20 20 20 20 20 28 2d 31 30 29 4_VTAB (-10)
4f1f0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4f200 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 ter to an sqlite
4f210 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 3_vtab structure
4f220 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d */.#define P4_M
4f230 50 52 49 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a PRINTF (-11) /*
4f240 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 P4 is a string
4f250 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
4f260 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 lite3_mprintf()
4f270 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 52 45 */.#define P4_RE
4f280 41 4c 20 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 AL (-12) /*
4f290 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 66 P4 is a 64-bit f
4f2a0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
4f2b0 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 lue */.#define P
4f2c0 34 5f 49 4e 54 36 34 20 20 20 20 28 2d 31 33 29 4_INT64 (-13)
4f2d0 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 /* P4 is a 64-b
4f2e0 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 it signed intege
4f2f0 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f r */.#define P4_
4f300 49 4e 54 33 32 20 20 20 20 28 2d 31 34 29 20 2f INT32 (-14) /
4f310 2a 20 50 34 20 69 73 20 61 20 33 32 2d 62 69 74 * P4 is a 32-bit
4f320 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
4f330 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e */.#define P4_IN
4f340 54 41 52 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 TARRAY (-15) /*
4f350 50 34 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f P4 is a vector o
4f360 66 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 f 32-bit integer
4f370 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f s */.#define P4_
4f380 53 55 42 50 52 4f 47 52 41 4d 20 20 28 2d 31 38 SUBPROGRAM (-18
4f390 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 ) /* P4 is a poi
4f3a0 6e 74 65 72 20 74 6f 20 61 20 53 75 62 50 72 6f nter to a SubPro
4f3b0 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 20 2a gram structure *
4f3c0 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e /../* When addin
4f3d0 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 g a P4 argument
4f3e0 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f using P4_KEYINFO
4f3f0 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 , a copy of the
4f400 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
4f410 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 e.** is made. T
4f420 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 hat copy is free
4f430 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
4f440 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 is finalized. B
4f450 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 ut if the.** arg
4f460 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 ument is P4_KEYI
4f470 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 NFO_HANDOFF, the
4f480 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 passed in point
4f490 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 er is used. It
4f4a0 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 still.** gets fr
4f4b0 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 eed when the Vdb
4f4c0 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 e is finalized s
4f4d0 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c o it still shoul
4f4e0 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a d be obtained.**
4f4f0 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 from a single s
4f500 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 qliteMalloc().
4f510 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d But no copy is m
4f520 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c ade and the call
4f530 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ing.** function
4f540 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 should *not* try
4f550 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 to free the Key
4f560 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 Info..*/.#define
4f570 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 P4_KEYINFO_HAND
4f580 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e OFF (-16).#defin
4f590 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 e P4_KEYINFO_STA
4f5a0 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a TIC (-17)../*.*
4f5b0 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e * The Vdbe.aColN
4f5c0 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 ame array contai
4f5d0 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 ns 5n Mem struct
4f5e0 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 ures, where n is
4f5f0 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 the .** number
4f600 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 of columns of da
4f610 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ta returned by t
4f620 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f he statement..*/
4f630 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
4f640 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 _NAME 0.#def
4f650 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c ine COLNAME_DECL
4f660 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 TYPE 1.#define C
4f670 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 OLNAME_DATABASE
4f680 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 2.#define COLNAM
4f690 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 E_TABLE 3.#de
4f6a0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c fine COLNAME_COL
4f6b0 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 UMN 4.#ifdef S
4f6c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
4f6d0 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 UMN_METADATA.# d
4f6e0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4f6f0 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 5 /*
4f700 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 Number of COLNA
4f710 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a ME_xxx symbols *
4f720 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 /.#else.# ifdef
4f730 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
4f740 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 TYPE.# define
4f750 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 COLNAME_N 1
4f760 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f /* Store o
4f770 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a nly the name */.
4f780 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e # else.# defin
4f790 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 e COLNAME_N
4f7a0 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 2 /* Store
4f7b0 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 the name and de
4f7c0 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 cltype */.# endi
4f7d0 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 f.#endif../*.**
4f7e0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 The following ma
4f7f0 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 cro converts a r
4f800 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20 elative address
4f810 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a in the p2 field.
4f820 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 ** of a VdbeOp s
4f830 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 tructure into a
4f840 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 negative number
4f850 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 so that .** sqli
4f860 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 te3VdbeAddOpList
4f870 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 () knows that th
4f880 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c e address is rel
4f890 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a ative. Calling.
4f8a0 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 ** the macro aga
4f8b0 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 in restores the
4f8c0 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 address..*/.#def
4f8d0 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 ine ADDR(X) (-1
4f8e0 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 -(X))../*.** The
4f8f0 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 makefile scans
4f900 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 the vdbe.c sourc
4f910 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 e file and creat
4f920 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e es the "opcodes.
4f930 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c h".** header fil
4f940 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 e that defines a
4f950 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 number for each
4f960 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 opcode used by
4f970 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a the VDBE..*/./**
4f980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 ************ Inc
4f990 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 lude opcodes.h i
4f9a0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
4f9b0 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h *********
4f9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
4f9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
4f9e0 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e in file opcodes.
4f9f0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4fa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4fa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 ***********/./*
4fa20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 Automatically ge
4fa30 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 nerated. Do not
4fa40 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 edit */./* See
4fa50 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 the mkopcodeh.aw
4fa60 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 k script for det
4fa70 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ails */.#define
4fa80 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 20 OP_Goto
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faa0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
4fab0 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 e OP_Gosub
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fad0 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 2.#def
4fae0 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 ine OP_Return
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb00 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 3.#d
4fb10 65 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 efine OP_Yield
4fb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 4.
4fb40 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 #define OP_HaltI
4fb50 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 fNull
4fb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb70 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 5.#define OP_Hal
4fb80 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fba0 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6.#define OP_I
4fbb0 6e 74 65 67 65 72 20 20 20 20 20 20 20 20 20 20 nteger
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbd0 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 7.#define OP
4fbe0 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 _Int64
4fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc00 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
4fc10 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 OP_Real
4fc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc30 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 130 /* s
4fc40 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 ame as TK_FLOAT
4fc50 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4fc60 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 20 20 _String8
4fc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc80 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 61 6d 94 /* sam
4fc90 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 e as TK_STRING
4fca0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
4fcb0 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 tring
4fcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcd0 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 9.#define OP
4fce0 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 _Null
4fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd00 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 10.#define
4fd10 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 OP_Blob
4fd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd30 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 11.#defin
4fd40 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 20 20 e OP_Variable
4fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd60 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 12.#def
4fd70 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 ine OP_Move
4fd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd90 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 13.#d
4fda0 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 efine OP_Copy
4fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 14.
4fdd0 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 70 79 #define OP_SCopy
4fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4fe00 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 5.#define OP_Res
4fe10 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 20 ultRow
4fe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe30 20 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 16.#define OP_C
4fe40 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 20 oncat
4fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe60 20 20 20 39 31 20 20 20 2f 2a 20 73 61 6d 65 20 91 /* same
4fe70 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 2a as TK_CONCAT *
4fe80 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 /.#define OP_Add
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 20 20 20 20 20
4feb0 20 38 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 86 /* same as
4fec0 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a TK_PLUS */.
4fed0 23 64 65 66 69 6e 65 20 4f 50 5f 53 75 62 74 72 #define OP_Subtr
4fee0 61 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 act
4fef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
4ff00 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 7 /* same as T
4ff10 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f 0a 23 64 K_MINUS */.#d
4ff20 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c efine OP_Multipl
4ff30 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20 88
4ff50 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4ff60 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 STAR */.#def
4ff70 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 ine OP_Divide
4ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff90 20 20 20 20 20 20 20 20 20 20 20 38 39 20 20 20 89
4ffa0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c /* same as TK_SL
4ffb0 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e ASH */.#defin
4ffc0 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 20 e OP_Remainder
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffe0 20 20 20 20 20 20 20 20 20 39 30 20 20 20 2f 2a 90 /*
4fff0 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 20 same as TK_REM
50000 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
50010 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 OP_CollSeq
50020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50030 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 17.#defin
50040 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 e OP_Function
50050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50060 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 18.#def
50070 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 ine OP_BitAnd
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50090 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20 20 82
500a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 /* same as TK_BI
500b0 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e TAND */.#defin
500c0 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 20 e OP_BitOr
500d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500e0 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f 2a 83 /*
500f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f same as TK_BITO
50100 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 R */.#define
50110 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 20 OP_ShiftLeft
50120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50130 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 73 84 /* s
50140 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 54 ame as TK_LSHIFT
50150 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
50160 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20 20 _ShiftRight
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50180 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 85 /* sam
50190 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20 20 e as TK_RSHIFT
501a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 */.#define OP_A
501b0 64 64 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20 ddImm
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501d0 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 20.#define OP
501e0 5f 4d 75 73 74 42 65 49 6e 74 20 20 20 20 20 20 _MustBeInt
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50200 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 21.#define
50210 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 OP_RealAffinity
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50230 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 22.#defin
50240 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 e OP_ToText
50250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50260 20 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 2a 141 /*
50270 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 same as TK_TO_T
50280 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 EXT */.#define
50290 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20 OP_ToBlob
502a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502b0 20 20 20 20 20 20 31 34 32 20 20 20 2f 2a 20 73 142 /* s
502c0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f ame as TK_TO_BLO
502d0 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 B */.#define OP
502e0 5f 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20 20 _ToNumeric
502f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50300 20 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 143 /* sam
50310 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 e as TK_TO_NUMER
50320 49 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f IC*/.#define OP_
50330 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 ToInt
50340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50350 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d 65 144 /* same
50360 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 as TK_TO_INT
50370 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f */.#define OP_To
50380 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 Real
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503a0 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 145 /* same a
503b0 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a 2f s TK_TO_REAL */
503c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71 20 20 .#define OP_Eq
503d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503f0 37 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 76 /* same as
50400 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a 23 TK_EQ */.#
50410 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 define OP_Ne
50420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 75
50440 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50450 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 _NE */.#de
50460 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 fine OP_Lt
50470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50480 20 20 20 20 20 20 20 20 20 20 20 20 37 39 20 20 79
50490 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
504a0 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 T */.#defi
504b0 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20 20 ne OP_Le
504c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504d0 20 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f 78 /
504e0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 20 * same as TK_LE
504f0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
50500 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 20 OP_Gt
50510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50520 20 20 20 20 20 20 20 20 37 37 20 20 20 2f 2a 20 77 /*
50530 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 20 same as TK_GT
50540 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
50550 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20 20 P_Ge
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50570 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 80 /* sa
50580 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20 20 me as TK_GE
50590 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
505a0 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20 20 20 Permutation
505b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505c0 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 23.#define O
505d0 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 P_Compare
505e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505f0 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 24.#define
50600 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 OP_Jump
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 32 35 0a 23 64 65 66 69 25.#defi
50630 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 ne OP_And
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50650 20 20 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 69 /
50660 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 * same as TK_AND
50670 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
50680 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 20 OP_Or
50690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506a0 20 20 20 20 20 20 20 20 36 38 20 20 20 2f 2a 20 68 /*
506b0 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 20 same as TK_OR
506c0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
506d0 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 P_Not
506e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506f0 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 73 61 19 /* sa
50700 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 me as TK_NOT
50710 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
50720 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 BitNot
50730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50740 20 20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 93 /* same
50750 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 as TK_BITNOT
50760 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 */.#define OP_If
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50790 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 26.#define OP_
507a0 49 66 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 IfNot
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507c0 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 27.#define O
507d0 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20 20 20 20 P_IsNull
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507f0 20 20 20 20 20 20 37 33 20 20 20 2f 2a 20 73 61 73 /* sa
50800 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 20 me as TK_ISNULL
50810 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
50820 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 NotNull
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50840 20 20 20 20 37 34 20 20 20 2f 2a 20 73 61 6d 65 74 /* same
50850 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 as TK_NOTNULL
50860 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f */.#define OP_Co
50870 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 lumn
50880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50890 20 20 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 28.#define OP_
508a0 41 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 Affinity
508b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508c0 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20 4f 29.#define O
508d0 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 P_MakeRecord
508e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508f0 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65 30.#define
50900 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 20 20 20 OP_Count
50910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50920 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 31.#defi
50930 6e 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 ne OP_Savepoint
50940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50950 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 64 65 32.#de
50960 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d fine OP_AutoComm
50970 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 it
50980 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 33.#
50990 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 6e 73 61 define OP_Transa
509a0 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 ction
509b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 34 34
509c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 .#define OP_Read
509d0 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 Cookie
509e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509f0 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 35.#define OP_Se
50a00 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 tCookie
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 36.#define OP_
50a30 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 VerifyCookie
50a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a50 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 4f 37.#define O
50a60 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20 P_OpenRead
50a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a80 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65 38.#define
50a90 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 OP_OpenWrite
50aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ab0 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 39.#defi
50ac0 6e 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 ne OP_OpenEpheme
50ad0 72 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 ral
50ae0 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65 40.#de
50af0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 fine OP_OpenPseu
50b00 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 do
50b10 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23 41.#
50b20 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20 define OP_Close
50b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32 42
50b50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b .#define OP_Seek
50b60 4c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Lt
50b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b80 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 43.#define OP_Se
50b90 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 ekLe
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bb0 20 20 34 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44.#define OP_
50bc0 53 65 65 6b 47 65 20 20 20 20 20 20 20 20 20 20 SeekGe
50bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50be0 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20 4f 45.#define O
50bf0 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 20 20 P_SeekGt
50c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c10 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 46.#define
50c20 20 4f 50 5f 53 65 65 6b 20 20 20 20 20 20 20 20 OP_Seek
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c40 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69 47.#defi
50c50 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 ne OP_NotFound
50c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c70 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 48.#de
50c80 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20 20 20 fine OP_Found
50c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ca0 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 49.#
50cb0 64 65 66 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 define OP_IsUniq
50cc0 75 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ue
50cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 30 50
50ce0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 .#define OP_NotE
50cf0 78 69 73 74 73 20 20 20 20 20 20 20 20 20 20 20 xists
50d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d10 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 51.#define OP_Se
50d20 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20 20 quence
50d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d40 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52.#define OP_
50d50 4e 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 NewRowid
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d70 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 53.#define O
50d80 50 5f 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 P_Insert
50d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50da0 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 54.#define
50db0 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 20 20 20 OP_InsertInt
50dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50dd0 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 55.#defi
50de0 6e 65 20 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 ne OP_Delete
50df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e00 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 56.#de
50e10 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 fine OP_ResetCou
50e20 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
50e30 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 57.#
50e40 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79 define OP_RowKey
50e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38 58
50e70 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 44 .#define OP_RowD
50e80 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 20 ata
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ea0 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 59.#define OP_Ro
50eb0 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20 wid
50ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ed0 20 20 36 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 60.#define OP_
50ee0 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 NullRow
50ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f00 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20 4f 61.#define O
50f10 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 P_Last
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f30 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65 62.#define
50f40 20 4f 50 5f 53 6f 72 74 20 20 20 20 20 20 20 20 OP_Sort
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f60 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 69 63.#defi
50f70 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 ne OP_Rewind
50f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f90 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64 65 64.#de
50fa0 66 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 fine OP_Prev
50fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fc0 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a 23 65.#
50fd0 64 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 define OP_Next
50fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 66
51000 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 .#define OP_IdxI
51010 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 nsert
51020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51030 36 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 67.#define OP_Id
51040 78 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 xDelete
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51060 20 20 37 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 70.#define OP_
51070 49 64 78 52 6f 77 69 64 20 20 20 20 20 20 20 20 IdxRowid
51080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51090 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20 4f 71.#define O
510a0 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 20 P_IdxLT
510b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510c0 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 72.#define
510d0 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 OP_IdxGE
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510f0 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66 69 81.#defi
51100 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 ne OP_Destroy
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51120 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 92.#de
51130 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 fine OP_Clear
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51150 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 95.#
51160 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 define OP_Create
51170 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20 Index
51180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 36 96
51190 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 .#define OP_Crea
511a0 74 65 54 61 62 6c 65 20 20 20 20 20 20 20 20 20 teTable
511b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511c0 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 97.#define OP_Pa
511d0 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 20 rseSchema
511e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511f0 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 98.#define OP_
51200 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20 20 20 LoadAnalysis
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51220 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 99.#define O
51230 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20 P_DropTable
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51250 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 100.#define
51260 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 OP_DropIndex
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51280 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 101.#defi
51290 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 ne OP_DropTrigge
512a0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r
512b0 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 102.#de
512c0 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 fine OP_Integrit
512d0 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 yCk
512e0 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 103.#
512f0 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 define OP_RowSet
51300 41 64 64 20 20 20 20 20 20 20 20 20 20 20 20 20 Add
51310 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 34 104
51320 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 .#define OP_RowS
51330 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20 20 etRead
51340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51350 30 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 05.#define OP_Ro
51360 77 53 65 74 54 65 73 74 20 20 20 20 20 20 20 20 wSetTest
51370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51380 20 31 30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 106.#define OP_
51390 50 72 6f 67 72 61 6d 20 20 20 20 20 20 20 20 20 Program
513a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513b0 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 4f 107.#define O
513c0 50 5f 50 61 72 61 6d 20 20 20 20 20 20 20 20 20 P_Param
513d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513e0 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 108.#define
513f0 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 20 20 OP_FkCounter
51400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51410 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 69 109.#defi
51420 6e 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 20 20 ne OP_FkIfZero
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51440 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 110.#de
51450 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 20 fine OP_MemMax
51460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51470 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 111.#
51480 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f 73 20 define OP_IfPos
51490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 32 112
514b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e 65 .#define OP_IfNe
514c0 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g
514d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
514e0 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 13.#define OP_If
514f0 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 Zero
51500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51510 20 31 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 114.#define OP_
51520 41 67 67 53 74 65 70 20 20 20 20 20 20 20 20 20 AggStep
51530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51540 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 4f 115.#define O
51550 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 P_AggFinal
51560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51570 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 116.#define
51580 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 OP_Vacuum
51590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515a0 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66 69 117.#defi
515b0 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d ne OP_IncrVacuum
515c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515d0 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 118.#de
515e0 66 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 20 20 fine OP_Expire
515f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51600 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 119.#
51610 64 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c define OP_TableL
51620 6f 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 ock
51630 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 30 120
51640 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 65 67 .#define OP_VBeg
51650 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 in
51660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51670 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 21.#define OP_VC
51680 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20 reate
51690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516a0 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 122.#define OP_
516b0 56 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 VDestroy
516c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516d0 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 4f 123.#define O
516e0 50 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 P_VOpen
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51700 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 124.#define
51710 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20 OP_VFilter
51720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51730 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 125.#defi
51740 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 ne OP_VColumn
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51760 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 126.#de
51770 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 fine OP_VNext
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51790 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 127.#
517a0 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e 61 6d define OP_VRenam
517b0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
517c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38 128
517d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 .#define OP_VUpd
517e0 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ate
517f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51800 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 29.#define OP_Pa
51810 67 65 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 gecount
51820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51830 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 131.#define OP_
51840 54 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 Trace
51850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51860 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 4f 132.#define O
51870 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 P_Noop
51880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51890 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 133.#define
518a0 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20 OP_Explain
518b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518c0 20 20 20 20 20 20 20 31 33 34 0a 0a 2f 2a 20 54 134../* T
518d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 he following opc
518e0 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e ode values are n
518f0 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 ever used */.#de
51900 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
51910 31 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20 135
51920 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 135.#
51930 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
51940 64 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20 d_136
51950 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 136
51960 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
51970 73 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 sed_137
51980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51990 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 37.#define OP_No
519a0 74 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 tUsed_138
519b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
519c0 20 31 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 138.#define OP_
519d0 4e 6f 74 55 73 65 64 5f 31 33 39 20 20 20 20 20 NotUsed_139
519e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
519f0 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 139.#define O
51a00 50 5f 4e 6f 74 55 73 65 64 5f 31 34 30 20 20 20 P_NotUsed_140
51a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a20 20 20 20 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72 140.../* Pr
51a30 6f 70 65 72 74 69 65 73 20 73 75 63 68 20 61 73 operties such as
51a40 20 22 6f 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 "out2" or "jump
51a50 22 20 74 68 61 74 20 61 72 65 20 73 70 65 63 69 " that are speci
51a60 66 69 65 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 fied in.** comme
51a70 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 nts following th
51a80 65 20 22 63 61 73 65 22 20 66 6f 72 20 65 61 63 e "case" for eac
51a90 68 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 h opcode in the
51aa0 76 64 62 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e vdbe.c.** are en
51ab0 63 6f 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65 coded into bitve
51ac0 63 74 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ctors as follows
51ad0 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 :.*/.#define OPF
51ae0 4c 47 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20 LG_JUMP
51af0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 0x0001 /* ju
51b00 6d 70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d mp: P2 holds jm
51b10 70 20 74 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 p target */.#def
51b20 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 ine OPFLG_OUT2_P
51b30 52 45 52 45 4c 45 41 53 45 20 30 78 30 30 30 32 RERELEASE 0x0002
51b40 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
51b50 65 61 73 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 ease: */.#define
51b60 20 4f 50 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 OPFLG_IN1
51b70 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 0x0004 /
51b80 2a 20 69 6e 31 3a 20 20 20 50 31 20 69 73 20 61 * in1: P1 is a
51b90 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 n input */.#defi
51ba0 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 ne OPFLG_IN2
51bb0 20 20 20 20 20 20 20 20 20 30 78 30 30 30 38 20 0x0008
51bc0 20 2f 2a 20 69 6e 32 3a 20 20 20 50 32 20 69 73 /* in2: P2 is
51bd0 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 an input */.#de
51be0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 fine OPFLG_IN3
51bf0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 31 0x001
51c00 30 20 20 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 0 /* in3: P3
51c10 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 is an input */.#
51c20 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 define OPFLG_OUT
51c30 32 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 2 0x0
51c40 30 32 30 20 20 2f 2a 20 6f 75 74 32 3a 20 20 50 020 /* out2: P
51c50 32 20 69 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 2 is an output *
51c60 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
51c70 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20 20 OUT3
51c80 30 78 30 30 34 30 20 20 2f 2a 20 6f 75 74 33 3a 0x0040 /* out3:
51c90 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70 75 P3 is an outpu
51ca0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 t */.#define OPF
51cb0 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b LG_INITIALIZER {
51cc0 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30 30 \./* 0 */ 0x00
51cd0 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20 30 78 , 0x01, 0x05, 0x
51ce0 30 34 2c 20 30 78 30 34 2c 20 30 78 31 30 2c 20 04, 0x04, 0x10,
51cf0 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 0x00, 0x02,\./*
51d00 20 20 38 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 8 */ 0x02, 0x0
51d10 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
51d20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c x00, 0x00, 0x24,
51d30 20 30 78 32 34 2c 5c 0a 2f 2a 20 20 31 36 20 2a 0x24,\./* 16 *
51d40 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 / 0x00, 0x00, 0x
51d50 30 30 2c 20 30 78 32 34 2c 20 30 78 30 34 2c 20 00, 0x24, 0x04,
51d60 30 78 30 35 2c 20 30 78 30 34 2c 20 30 78 30 30 0x05, 0x04, 0x00
51d70 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78 30 ,\./* 24 */ 0x0
51d80 30 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20 30 0, 0x01, 0x05, 0
51d90 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x05, 0x00, 0x00,
51da0 20 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 0x00, 0x02,\./*
51db0 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30 78 32 */ 0x00, 0x
51dc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 00, 0x00, 0x02,
51dd0 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x10, 0x00, 0x00
51de0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 34 30 20 , 0x00,\./* 40
51df0 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 */ 0x00, 0x00, 0
51e00 78 30 30 2c 20 30 78 31 31 2c 20 30 78 31 31 2c x00, 0x11, 0x11,
51e10 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30 0x11, 0x11, 0x0
51e20 38 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 78 8,\./* 48 */ 0x
51e30 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 11, 0x11, 0x11,
51e40 30 78 31 31 2c 20 30 78 30 32 2c 20 30 78 30 32 0x11, 0x02, 0x02
51e50 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f , 0x00, 0x00,\./
51e60 2a 20 20 35 36 20 2a 2f 20 30 78 30 30 2c 20 30 * 56 */ 0x00, 0
51e70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51e80 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
51e90 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 20 36 34 1, 0x01,\./* 64
51ea0 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 31 2c 20 */ 0x01, 0x01,
51eb0 30 78 30 31 2c 20 30 78 30 38 2c 20 30 78 34 63 0x01, 0x08, 0x4c
51ec0 2c 20 30 78 34 63 2c 20 30 78 30 30 2c 20 30 78 , 0x4c, 0x00, 0x
51ed0 30 32 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 30 02,\./* 72 */ 0
51ee0 78 30 31 2c 20 30 78 30 35 2c 20 30 78 30 35 2c x01, 0x05, 0x05,
51ef0 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 0x15, 0x15, 0x1
51f00 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a 5, 0x15, 0x15,\.
51f10 2f 2a 20 20 38 30 20 2a 2f 20 30 78 31 35 2c 20 /* 80 */ 0x15,
51f20 30 78 30 31 2c 20 30 78 34 63 2c 20 30 78 34 63 0x01, 0x4c, 0x4c
51f30 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 , 0x4c, 0x4c, 0x
51f40 34 63 2c 20 30 78 34 63 2c 5c 0a 2f 2a 20 20 38 4c, 0x4c,\./* 8
51f50 38 20 2a 2f 20 30 78 34 63 2c 20 30 78 34 63 2c 8 */ 0x4c, 0x4c,
51f60 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 30 0x4c, 0x4c, 0x0
51f70 32 2c 20 30 78 32 34 2c 20 30 78 30 32 2c 20 30 2, 0x24, 0x02, 0
51f80 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 20 x00,\./* 96 */
51f90 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30 0x02, 0x02, 0x00
51fa0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51fb0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 00, 0x00, 0x00,\
51fc0 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 63 2c ./* 104 */ 0x0c,
51fd0 20 30 78 34 35 2c 20 30 78 31 35 2c 20 30 78 30 0x45, 0x15, 0x0
51fe0 31 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 1, 0x02, 0x00, 0
51ff0 78 30 31 2c 20 30 78 30 38 2c 5c 0a 2f 2a 20 31 x01, 0x08,\./* 1
52000 31 32 20 2a 2f 20 30 78 30 35 2c 20 30 78 30 35 12 */ 0x05, 0x05
52010 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 , 0x05, 0x00, 0x
52020 30 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 00, 0x00, 0x01,
52030 30 78 30 30 2c 5c 0a 2f 2a 20 31 32 30 20 2a 2f 0x00,\./* 120 */
52040 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
52050 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
52060 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 31 2c x01, 0x00, 0x01,
52070 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 30 30 \./* 128 */ 0x00
52080 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 , 0x00, 0x02, 0x
52090 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 02, 0x00, 0x00,
520a0 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 0x00, 0x00,\./*
520b0 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 136 */ 0x00, 0x0
520c0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
520d0 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 2c x00, 0x04, 0x04,
520e0 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 2a 0x04,\./* 144 *
520f0 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a 0a / 0x04, 0x04,}..
52100 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
52110 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e 68 End of opcodes.h
52120 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
52130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
52150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
52160 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
52170 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
52180 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h *********
52190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
521a0 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 ./*.** Prototype
521b0 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20 69 s for the VDBE i
521c0 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20 63 nterface. See c
521d0 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 69 omments on the i
521e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a mplementation.**
521f0 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 for a descripti
52200 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68 20 on of what each
52210 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 of these routine
52220 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 s does..*/.SQLIT
52230 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a E_PRIVATE Vdbe *
52240 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 sqlite3VdbeCreat
52250 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c e(sqlite3*);.SQL
52260 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
52270 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
52280 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 0(Vdbe*,int);.SQ
52290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
522a0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
522b0 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 p1(Vdbe*,int,int
522c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
522d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
522e0 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69 6e eAddOp2(Vdbe*,in
522f0 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 t,int,int);.SQLI
52300 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
52310 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
52320 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 (Vdbe*,int,int,i
52330 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
52340 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
52350 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56 64 te3VdbeAddOp4(Vd
52360 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c be*,int,int,int,
52370 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a int,const char *
52380 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 zP4,int);.SQLITE
52390 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
523a0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e ite3VdbeAddOp4In
523b0 74 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c t(Vdbe*,int,int,
523c0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 int,int,int);.SQ
523d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
523e0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
523f0 70 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 pList(Vdbe*, int
52400 20 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 nOp, VdbeOpList
52410 20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 const *aOp);.SQ
52420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52430 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
52440 6e 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 ngeP1(Vdbe*, int
52450 20 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a addr, int P1);.
52460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52470 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
52480 68 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 hangeP2(Vdbe*, i
52490 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 nt addr, int P2)
524a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
524b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
524c0 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c eChangeP3(Vdbe*,
524d0 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 int addr, int P
524e0 33 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 3);.SQLITE_PRIVA
524f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
52500 64 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 dbeChangeP5(Vdbe
52510 2a 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 *, u8 P5);.SQLIT
52520 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52530 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
52540 72 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 re(Vdbe*, int ad
52550 64 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 dr);.SQLITE_PRIV
52560 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
52570 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 VdbeChangeToNoop
52580 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
52590 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 , int N);.SQLITE
525a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
525b0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
525c0 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 4(Vdbe*, int add
525d0 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
525e0 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 P4, int N);.SQLI
525f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52600 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
52610 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 tree(Vdbe*, int)
52620 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52630 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 VdbeOp *sqlite3
52640 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c VdbeGetOp(Vdbe*,
52650 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
52660 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
52670 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 3VdbeMakeLabel(V
52680 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
52690 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
526a0 65 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 e3VdbeDelete(Vdb
526b0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
526c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
526d0 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 VdbeMakeReady(Vd
526e0 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c be*,int,int,int,
526f0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 int,int,int);.SQ
52700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
52710 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 sqlite3VdbeFina
52720 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c lize(Vdbe*);.SQL
52730 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
52740 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f sqlite3VdbeReso
52750 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 lveLabel(Vdbe*,
52760 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
52770 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
52780 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
52790 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 Vdbe*);.#ifdef S
527a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
527b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
527c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 sqlite3VdbeAsse
527d0 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 rtMayAbort(Vdbe
527e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
527f0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
52800 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 qlite3VdbeTrace(
52810 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 Vdbe*,FILE*);.#e
52820 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
52830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
52840 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 VdbeResetStepRes
52850 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 ult(Vdbe*);.SQLI
52860 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
52870 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 qlite3VdbeReset(
52880 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
52890 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
528a0 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c te3VdbeSetNumCol
528b0 73 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 s(Vdbe*,int);.SQ
528c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
528d0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 sqlite3VdbeSetC
528e0 6f 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e olName(Vdbe*, in
528f0 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 t, int, const ch
52900 61 72 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f ar *, void(*)(vo
52910 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 id*));.SQLITE_PR
52920 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52930 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 e3VdbeCountChang
52940 65 73 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 es(Vdbe*);.SQLIT
52950 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
52960 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 3 *sqlite3VdbeDb
52970 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
52980 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
52990 69 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 ite3VdbeSetSql(V
529a0 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 dbe*, const char
529b0 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 *z, int n, int)
529c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
529d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
529e0 65 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65 eSwap(Vdbe*,Vdbe
529f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
52a00 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 TE VdbeOp *sqlit
52a10 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 e3VdbeTakeOpArra
52a20 79 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 y(Vdbe*, int*, i
52a30 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
52a40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
52a50 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 3VdbeProgramDele
52a60 74 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 75 te(sqlite3 *, Su
52a70 62 50 72 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 29 bProgram *, int)
52a80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52a90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
52aa0 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 56 61 sqlite3VdbeGetVa
52ab0 6c 75 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 lue(Vdbe*, int,
52ac0 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 u8);.SQLITE_PRIV
52ad0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
52ae0 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28 56 VdbeSetVarmask(V
52af0 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e dbe*, int);.#ifn
52b00 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
52b10 54 52 41 43 45 0a 53 51 4c 49 54 45 5f 50 52 49 TRACE.SQLITE_PRI
52b20 56 41 54 45 20 20 20 63 68 61 72 20 2a 73 71 6c VATE char *sql
52b30 69 74 65 33 56 64 62 65 45 78 70 61 6e 64 53 71 ite3VdbeExpandSq
52b40 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 l(Vdbe*, const c
52b50 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 har*);.#endif..S
52b60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e QLITE_PRIVATE Un
52b70 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 packedRecord *sq
52b80 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 lite3VdbeRecordU
52b90 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c 69 npack(KeyInfo*,i
52ba0 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 63 nt,const void*,c
52bb0 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 har*,int);.SQLIT
52bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52bd0 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
52be0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 UnpackedRecord(U
52bf0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b npackedRecord*);
52c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52c10 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 int sqlite3VdbeR
52c20 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 ecordCompare(int
52c30 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 ,const void*,Unp
52c40 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a ackedRecord*);..
52c50 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
52c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
52c70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
52c80 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 eComment(Vdbe*,
52c90 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e const char*, ...
52ca0 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 );.# define Vdbe
52cb0 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 Comment(X) sqli
52cc0 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 te3VdbeComment X
52cd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52ce0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
52cf0 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 beNoopComment(Vd
52d00 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a be*, const char*
52d10 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 , ...);.# define
52d20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 VdbeNoopComment
52d30 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 (X) sqlite3Vdbe
52d40 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 NoopComment X.#e
52d50 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 lse.# define Vdb
52d60 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 eComment(X).# de
52d70 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d fine VdbeNoopCom
52d80 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a ment(X).#endif..
52d90 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
52da0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 ******* End of v
52db0 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.h **********
52dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
52df0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
52e00 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
52e10 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
52e20 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
52e30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
52e40 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
52e50 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d pager.h in the m
52e60 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 iddle of sqliteI
52e70 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
52e80 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
52e90 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
52ea0 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a le pager.h *****
52eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ed0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
52ee0 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 01 September 15.
52ef0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
52f00 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
52f10 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
52f20 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
52f30 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
52f40 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
52f50 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
52f60 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
52f70 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
52f80 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
52f90 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
52fa0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
52fb0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
52fc0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
52fd0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
52fe0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
52ff0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
53000 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
53010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
53040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
53050 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
53060 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 le defines the i
53070 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68 nterface that th
53080 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 e sqlite page ca
53090 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d che.** subsystem
530a0 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68 . The page cach
530b0 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 e subsystem read
530c0 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 s and writes a f
530d0 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 ile a page.** at
530e0 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 a time and prov
530f0 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 ides a journal f
53100 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a or rollback..*/.
53110 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f .#ifndef _PAGER_
53120 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45 H_.#define _PAGE
53130 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 R_H_../*.** Defa
53140 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 ult maximum size
53150 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 for persistent
53160 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41 journal files. A
53170 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61 negative .** va
53180 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d lue means no lim
53190 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d it. This value m
531a0 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ay be overridden
531b0 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73 using the .** s
531c0 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
531d0 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50 alSizeLimit() AP
531e0 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41 I. See also "PRA
531f0 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 GMA journal_size
53200 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e _limit"..*/.#ifn
53210 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
53220 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f LT_JOURNAL_SIZE_
53230 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20 LIMIT. #define
53240 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a SQLITE_DEFAULT_J
53250 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 OURNAL_SIZE_LIMI
53260 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a T -1.#endif../*.
53270 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64 ** The type used
53280 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 to represent a
53290 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 page number. Th
532a0 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 e first page in
532b0 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c a file.** is cal
532c0 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69 led page 1. 0 i
532d0 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 s used to repres
532e0 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22 ent "not a page"
532f0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32 ..*/.typedef u32
53300 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 Pgno;../*.** Ea
53310 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 ch open file is
53320 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 managed by a sep
53330 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f arate instance o
53340 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 f the "Pager" st
53350 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 ructure..*/.type
53360 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 def struct Pager
53370 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48 Pager;../*.** H
53380 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70 andle type for p
53390 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ages..*/.typedef
533a0 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62 struct PgHdr Db
533b0 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 Page;../*.** Pag
533c0 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d e number PAGER_M
533d0 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 J_PGNO is never
533e0 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 used in an SQLit
533f0 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 e database (it i
53400 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f s.** reserved fo
53410 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 r working around
53420 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 a windows/posix
53430 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 incompatibility
53440 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 ). It is.** used
53450 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
53460 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 to signify that
53470 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 the remainder of
53480 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
53490 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 e .** is devoted
534a0 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 to storing a ma
534b0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
534c0 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f e - there are no
534d0 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a more pages to.*
534e0 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 * roll back. See
534f0 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 comments for fu
53500 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 nction writeMast
53510 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70 erJournal() in p
53520 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64 ager.c .** for d
53530 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 etails..*/.#defi
53540 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f ne PAGER_MJ_PGNO
53550 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e (x) ((Pgno)((PEN
53560 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e DING_BYTE/((x)->
53570 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a pageSize))+1))..
53580 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
53590 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61 lues for the fla
535a0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 gs parameter to
535b0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
535c0 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 ()..**.** NOTE:
535d0 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 These values mus
535e0 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 t match the corr
535f0 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f esponding BTREE_
53600 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65 values in btree
53610 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 .h..*/.#define P
53620 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 AGER_OMIT_JOURNA
53630 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 L 0x0001 /*
53640 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c Do not use a rol
53650 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f lback journal */
53660 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e .#define PAGER_N
53670 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30 O_READLOCK 0x0
53680 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72 002 /* Omit r
53690 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 eadlocks on read
536a0 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f only files */../
536b0 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 *.** Valid value
536c0 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 s for the second
536d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
536e0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
536f0 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 Mode()..*/.#defi
53700 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 ne PAGER_LOCKING
53710 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 MODE_QUERY
53720 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 -1.#define PAGER
53730 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
53740 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69 MAL 0.#defi
53750 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 ne PAGER_LOCKING
53760 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20 MODE_EXCLUSIVE
53770 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 1../*.** Valid
53780 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 values for the s
53790 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
537a0 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f o sqlite3PagerJo
537b0 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a urnalMode()..*/.
537c0 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f #define PAGER_JO
537d0 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 URNALMODE_QUERY
537e0 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 -1.#define
537f0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
53800 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 20 E_DELETE 0
53810 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 64 /* Commit by d
53820 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 eleting journal
53830 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 file */.#define
53840 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
53850 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 20 E_PERSIST 1
53860 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 7a /* Commit by z
53870 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 eroing journal h
53880 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 eader */.#define
53890 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
538a0 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 32 DE_OFF 2
538b0 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d /* Journal om
538c0 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 69 itted. */.#defi
538d0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
538e0 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 20 MODE_TRUNCATE
538f0 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 3 /* Commit b
53900 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 y truncating jou
53910 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 rnal */.#define
53920 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
53930 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20 E_MEMORY 4
53940 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a /* In-memory j
53950 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a ournal file */..
53960 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e /*.** The remain
53970 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65 der of this file
53980 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65 contains the de
53990 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68 clarations of th
539a0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 e functions.** t
539b0 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 hat make up the
539c0 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d Pager sub-system
539d0 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65 API. See source
539e0 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 code comments f
539f0 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65 or .** a detaile
53a00 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 d description of
53a10 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a each routine..*
53a20 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 /../* Open and c
53a30 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e lose a Pager con
53a40 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c nection. */ .SQL
53a50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53a60 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
53a70 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 2a (. sqlite3_vfs*
53a80 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 ,. Pager **ppPa
53a90 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 ger,. const cha
53aa0 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 r*,. int,. int
53ab0 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 28 ,. int,. void(
53ac0 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a 53 *)(DbPage*).);.S
53ad0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53ae0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c t sqlite3PagerCl
53af0 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ose(Pager *pPage
53b00 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
53b10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53b20 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 gerReadFileheade
53b30 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 r(Pager*, int, u
53b40 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a nsigned char*);.
53b50 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 ./* Functions us
53b60 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 ed to configure
53b70 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 a Pager object.
53b80 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
53b90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 E void sqlite3Pa
53ba0 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 gerSetBusyhandle
53bb0 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 r(Pager*, int(*)
53bc0 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a (void *), void *
53bd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53be0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53bf0 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 erSetPagesize(Pa
53c00 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e 74 29 ger*, u16*, int)
53c10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53c20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53c30 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 rMaxPageCount(Pa
53c40 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ger*, int);.SQLI
53c50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
53c60 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
53c70 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c achesize(Pager*,
53c80 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
53c90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
53ca0 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 e3PagerSetSafety
53cb0 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 Level(Pager*,int
53cc0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
53cd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53ce0 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 3PagerLockingMod
53cf0 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b e(Pager *, int);
53d00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53d10 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
53d20 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 JournalMode(Page
53d30 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 r *, int);.SQLIT
53d40 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 E_PRIVATE i64 sq
53d50 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
53d60 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 lSizeLimit(Pager
53d70 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 *, i64);.SQLITE
53d80 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
53d90 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 _backup **sqlite
53da0 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 3PagerBackupPtr(
53db0 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e Pager*);../* Fun
53dc0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6f ctions used to o
53dd0 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 73 btain and releas
53de0 65 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 e page reference
53df0 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 s. */ .SQLITE_PR
53e00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53e10 33 50 61 67 65 72 41 63 71 75 69 72 65 28 50 61 3PagerAcquire(Pa
53e20 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e ger *pPager, Pgn
53e30 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 2a o pgno, DbPage *
53e40 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c 72 *ppPage, int clr
53e50 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 73 Flag);.#define s
53e60 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 41 qlite3PagerGet(A
53e70 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 67 ,B,C) sqlite3Pag
53e80 65 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 2c erAcquire(A,B,C,
53e90 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 0).SQLITE_PRIVAT
53ea0 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 E DbPage *sqlite
53eb0 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 3PagerLookup(Pag
53ec0 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
53ed0 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 pgno);.SQLITE_P
53ee0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
53ef0 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 te3PagerRef(DbPa
53f00 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ge*);.SQLITE_PRI
53f10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
53f20 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 3PagerUnref(DbPa
53f30 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 74 ge*);../* Operat
53f40 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 66 ions on page ref
53f50 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c 49 erences. */.SQLI
53f60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
53f70 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
53f80 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 (DbPage*);.SQLIT
53f90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
53fa0 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 qlite3PagerDontW
53fb0 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 rite(DbPage*);.S
53fc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53fd0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f t sqlite3PagerMo
53fe0 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62 vepage(Pager*,Db
53ff0 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b Page*,Pgno,int);
54000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54010 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
54020 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 PageRefcount(DbP
54030 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 age*);.SQLITE_PR
54040 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
54050 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
54060 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 DbPage *); .SQLI
54070 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
54080 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 *sqlite3PagerGet
54090 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b Extra(DbPage *);
540a0 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 ../* Functions
540b0 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 70 used to manage p
540c0 61 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e ager transaction
540d0 73 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 73 s and savepoints
540e0 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 . */.SQLITE_PRIV
540f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
54100 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 agerPagecount(Pa
54110 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c ger*, int*);.SQL
54120 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
54130 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 sqlite3PagerBegi
54140 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 78 n(Pager*, int ex
54150 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Flag, int);.SQLI
54160 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
54170 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
54180 74 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a tPhaseOne(Pager*
54190 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 ,const char *zMa
541a0 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ster, int);.SQLI
541b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
541c0 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
541d0 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a Pager *pPager);.
541e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
541f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 nt sqlite3PagerC
54200 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 ommitPhaseTwo(Pa
54210 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
54220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
54230 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 3PagerRollback(P
54240 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
54250 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
54260 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 e3PagerOpenSavep
54270 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 oint(Pager *pPag
54280 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 er, int n);.SQLI
54290 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
542a0 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 qlite3PagerSavep
542b0 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 oint(Pager *pPag
542c0 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 er, int op, int
542d0 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 51 4c iSavepoint);.SQL
542e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
542f0 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 sqlite3PagerShar
54300 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 edLock(Pager *pP
54310 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 ager);../* Funct
54320 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 75 65 ions used to que
54330 72 79 20 70 61 67 65 72 20 73 74 61 74 65 20 61 ry pager state a
54340 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e nd configuration
54350 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 . */.SQLITE_PRIV
54360 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 ATE u8 sqlite3Pa
54370 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 gerIsreadonly(Pa
54380 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
54390 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
543a0 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 3PagerRefcount(P
543b0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
543c0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
543d0 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 r *sqlite3PagerF
543e0 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b ilename(Pager*);
543f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54400 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 const sqlite3_vf
54410 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 s *sqlite3PagerV
54420 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 fs(Pager*);.SQLI
54430 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
54440 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 e3_file *sqlite3
54450 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 2a PagerFile(Pager*
54460 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
54470 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 E const char *sq
54480 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
54490 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 lname(Pager*);.S
544a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
544b0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f t sqlite3PagerNo
544c0 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53 51 sync(Pager*);.SQ
544d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
544e0 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 d *sqlite3PagerT
544f0 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a 29 empSpace(Pager*)
54500 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54510 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54520 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 2a 29 rIsMemdb(Pager*)
54530 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 ;../* Functions
54540 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74 65 used to truncate
54550 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
54560 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le. */.SQLITE_PR
54570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
54580 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 e3PagerTruncateI
54590 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f mage(Pager*,Pgno
545a0 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 );../* Functions
545b0 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65 73 74 to support test
545c0 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e ing and debuggin
545d0 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e g. */.#if !defin
545e0 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 ed(NDEBUG) || de
545f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
54600 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 T).SQLITE_PRIVAT
54610 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 E Pgno sqlite3
54620 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 PagerPagenumber(
54630 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
54640 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
54650 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
54660 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a 29 3b teable(DbPage*);
54670 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
54680 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
54690 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
546a0 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 *sqlite3PagerSta
546b0 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 ts(Pager*);.SQLI
546c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
546d0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 d sqlite3PagerRe
546e0 66 64 75 6d 70 28 50 61 67 65 72 2a 29 3b 0a 20 fdump(Pager*);.
546f0 20 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 void disable_si
54700 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
54710 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 s(void);. void
54720 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 enable_simulated
54730 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 _io_errors(void)
54740 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
54750 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 disable_simulat
54760 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 ed_io_errors().#
54770 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 define enable_s
54780 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
54790 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e rs().#endif..#en
547a0 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 5f dif /* _PAGER_H_
547b0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
547c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 **** End of page
547d0 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.h ************
547e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
547f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54800 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
54810 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
54820 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
54830 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 f in sqliteInt.h
54840 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
54850 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
54860 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63 61 **** Include pca
54870 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 che.h in the mid
54880 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
54890 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
548a0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
548b0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
548c0 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a pcache.h *******
548d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
548e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
548f0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
54900 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 August 05.**.**
54910 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
54920 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
54930 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
54940 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
54950 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
54960 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
54970 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
54980 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
54990 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
549a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
549b0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
549c0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
549d0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
549e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
549f0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
54a00 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
54a10 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
54a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54a60 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
54a70 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 header file def
54a80 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 ines the interfa
54a90 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ce that the sqli
54aa0 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a te page cache.**
54ab0 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2f 0a subsystem. .*/.
54ac0 0a 23 69 66 6e 64 65 66 20 5f 50 43 41 43 48 45 .#ifndef _PCACHE
54ad0 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 74 72 _H_..typedef str
54ae0 75 63 74 20 50 67 48 64 72 20 50 67 48 64 72 3b uct PgHdr PgHdr;
54af0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
54b00 50 43 61 63 68 65 20 50 43 61 63 68 65 3b 0a 0a PCache PCache;..
54b10 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 70 61 67 65 /*.** Every page
54b20 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 69 73 in the cache is
54b30 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 61 controlled by a
54b40 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
54b50 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 e following.** s
54b60 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 tructure..*/.str
54b70 75 63 74 20 50 67 48 64 72 20 7b 0a 20 20 76 6f uct PgHdr {. vo
54b80 69 64 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 id *pData;
54b90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
54ba0 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 Content of this
54bb0 70 61 67 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a page */. void *
54bc0 70 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 pExtra;
54bd0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
54be0 61 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 a content */. P
54bf0 67 48 64 72 20 2a 70 44 69 72 74 79 3b 20 20 20 gHdr *pDirty;
54c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
54c10 20 54 72 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 Transient list
54c20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 2a of dirty pages *
54c30 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 /. Pgno pgno;
54c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54c50 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 /* Page numbe
54c60 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 r for this page
54c70 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 */. Pager *pPag
54c80 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 er;
54c90 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 /* The pager
54ca0 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61 this page is pa
54cb0 72 74 20 6f 66 20 2a 2f 0a 23 69 66 64 65 66 20 rt of */.#ifdef
54cc0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
54cd0 45 53 0a 20 20 75 33 32 20 70 61 67 65 48 61 73 ES. u32 pageHas
54ce0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
54cf0 20 20 20 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 /* Hash of p
54d00 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 age content */.#
54d10 65 6e 64 69 66 0a 20 20 75 31 36 20 66 6c 61 67 endif. u16 flag
54d20 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
54d30 20 20 20 20 20 20 20 2f 2a 20 50 47 48 44 52 20 /* PGHDR
54d40 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 62 65 flags defined be
54d50 6c 6f 77 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a low */.. /*****
54d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54da0 2a 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 *. ** Elements
54db0 61 62 6f 76 65 20 61 72 65 20 70 75 62 6c 69 63 above are public
54dc0 2e 20 20 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c . All that foll
54dd0 6f 77 73 20 69 73 20 70 72 69 76 61 74 65 20 74 ows is private t
54de0 6f 20 70 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 o pcache.c. **
54df0 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 and should not b
54e00 65 20 61 63 63 65 73 73 65 64 20 62 79 20 6f 74 e accessed by ot
54e10 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a her modules.. *
54e20 2f 0a 20 20 69 31 36 20 6e 52 65 66 3b 20 20 20 /. i16 nRef;
54e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54e40 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
54e50 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 70 61 users of this pa
54e60 67 65 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a ge */. PCache *
54e70 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 pCache;
54e80 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 /* Cache
54e90 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20 70 that owns this p
54ea0 61 67 65 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 age */.. PgHdr
54eb0 2a 70 44 69 72 74 79 4e 65 78 74 3b 20 20 20 20 *pDirtyNext;
54ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
54ed0 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 element in list
54ee0 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 of dirty pages
54ef0 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 */. PgHdr *pDir
54f00 74 79 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 tyPrev;
54f10 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 /* Previous
54f20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 element in list
54f30 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 2a of dirty pages *
54f40 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c /.};../* Bit val
54f50 75 65 73 20 66 6f 72 20 50 67 48 64 72 2e 66 6c ues for PgHdr.fl
54f60 61 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ags */.#define P
54f70 47 48 44 52 5f 44 49 52 54 59 20 20 20 20 20 20 GHDR_DIRTY
54f80 20 20 20 20 20 20 20 30 78 30 30 32 20 20 2f 2a 0x002 /*
54f90 20 50 61 67 65 20 68 61 73 20 63 68 61 6e 67 65 Page has change
54fa0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 d */.#define PGH
54fb0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 DR_NEED_SYNC
54fc0 20 20 20 20 20 30 78 30 30 34 20 20 2f 2a 20 46 0x004 /* F
54fd0 73 79 6e 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 sync the rollbac
54fe0 6b 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 k journal before
54ff0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
55000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55010 20 20 20 20 20 20 20 20 2a 2a 20 77 72 69 74 69 ** writi
55020 6e 67 20 74 68 69 73 20 70 61 67 65 20 74 6f 20 ng this page to
55030 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
55040 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 #define PGHDR_NE
55050 45 44 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 ED_READ
55060 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 0x008 /* Conten
55070 74 20 69 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 t is unread */.#
55080 64 65 66 69 6e 65 20 50 47 48 44 52 5f 52 45 55 define PGHDR_REU
55090 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 SE_UNLIKELY 0
550a0 78 30 31 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 x010 /* A hint
550b0 74 68 61 74 20 72 65 75 73 65 20 69 73 20 75 6e that reuse is un
550c0 6c 69 6b 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e likely */.#defin
550d0 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 e PGHDR_DONT_WRI
550e0 54 45 20 20 20 20 20 20 20 20 30 78 30 32 30 20 TE 0x020
550f0 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 /* Do not write
55100 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b content to disk
55110 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 */../* Initiali
55120 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 ze and shutdown
55130 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 73 the page cache s
55140 75 62 73 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 ubsystem */.SQLI
55150 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
55160 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 qlite3PcacheInit
55170 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 ialize(void);.SQ
55180 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55190 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
551a0 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a hutdown(void);..
551b0 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 62 75 /* Page cache bu
551c0 66 66 65 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a ffer management:
551d0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
551e0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c es implement SQL
551f0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
55200 41 43 48 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ACHE..*/.SQLITE_
55210 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
55220 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 ite3PCacheBuffer
55230 53 65 74 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e Setup(void *, in
55240 74 20 73 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f t sz, int n);../
55250 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 70 * Create a new p
55260 61 67 65 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 ager cache..** U
55270 6e 64 65 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 nder memory stre
55280 73 73 2c 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 ss, invoke xStre
55290 73 73 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b ss to try to mak
552a0 65 20 70 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a e pages clean..*
552b0 2a 20 4f 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 * Only clean and
552c0 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 unpinned pages
552d0 63 61 6e 20 62 65 20 72 65 63 6c 61 69 6d 65 64 can be reclaimed
552e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
552f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55300 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e PcacheOpen(. in
55310 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 t szPage,
55320 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
55330 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 Size of every pa
55340 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 ge */. int szEx
55350 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 tra,
55360 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 /* Extra
55370 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 64 space associated
55380 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 with each page
55390 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 */. int bPurgea
553a0 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ble,
553b0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
553c0 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b ages are on back
553d0 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 ing store */. i
553e0 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f nt (*xStress)(vo
553f0 69 64 2a 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a id*, PgHdr*), /*
55400 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 Call to try to
55410 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e make pages clean
55420 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 */. void *pStr
55430 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ess,
55440 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
55450 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 to xStress */.
55460 20 50 43 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 PCache *pToInit
55470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
55480 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 /* Preallocated
55490 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 space for the PC
554a0 61 63 68 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d ache */.);../* M
554b0 6f 64 69 66 79 20 74 68 65 20 70 61 67 65 2d 73 odify the page-s
554c0 69 7a 65 20 61 66 74 65 72 20 74 68 65 20 63 61 ize after the ca
554d0 63 68 65 20 68 61 73 20 62 65 65 6e 20 63 72 65 che has been cre
554e0 61 74 65 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f ated. */.SQLITE_
554f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
55500 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 ite3PcacheSetPag
55510 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 eSize(PCache *,
55520 69 6e 74 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e int);../* Return
55530 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 the size in byt
55540 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 6f es of a PCache o
55550 62 6a 65 63 74 2e 20 20 55 73 65 64 20 74 6f 20 bject. Used to
55560 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 preallocate.** s
55570 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f torage space..*/
55580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55590 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
555a0 65 53 69 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a eSize(void);../*
555b0 20 4f 6e 65 20 72 65 6c 65 61 73 65 20 70 65 72 One release per
555c0 20 73 75 63 63 65 73 73 66 75 6c 20 66 65 74 63 successful fetc
555d0 68 2e 20 20 50 61 67 65 20 69 73 20 70 69 6e 6e h. Page is pinn
555e0 65 64 20 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 ed until release
555f0 64 2e 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 d..** Reference
55600 63 6f 75 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c counted. .*/.SQL
55610 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
55620 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
55630 63 68 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f ch(PCache*, Pgno
55640 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 , int createFlag
55650 2c 20 50 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 , PgHdr**);.SQLI
55660 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
55670 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
55680 65 61 73 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 ease(PgHdr*);..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 44 72 6f 70 28 50 67 48 64 72 2a 29 3b 20 20 20 Drop(PgHdr*);
556c0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
556d0 70 61 67 65 20 66 72 6f 6d 20 63 61 63 68 65 20 page from cache
556e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
556f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
55700 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 acheMakeDirty(Pg
55710 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b Hdr*); /* Mak
55720 65 20 73 75 72 65 20 70 61 67 65 20 69 73 20 6d e sure page is m
55730 61 72 6b 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 arked dirty */.S
55740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
55750 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
55760 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 2a MakeClean(PgHdr*
55770 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 ); /* Mark a
55780 73 69 6e 67 6c 65 20 70 61 67 65 20 61 73 20 63 single page as c
55790 6c 65 61 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 lean */.SQLITE_P
557a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
557b0 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c te3PcacheCleanAl
557c0 6c 28 50 43 61 63 68 65 2a 29 3b 20 20 20 20 2f l(PCache*); /
557d0 2a 20 4d 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 * Mark all dirty
557e0 20 6c 69 73 74 20 70 61 67 65 73 20 61 73 20 63 list pages as c
557f0 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e lean */../* Chan
55800 67 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 ge a page number
55810 2e 20 20 55 73 65 64 20 62 79 20 69 6e 63 72 2d . Used by incr-
55820 76 61 63 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 vacuum. */.SQLIT
55830 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
55840 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
55850 28 50 67 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a (PgHdr*, Pgno);.
55860 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 ./* Remove all p
55870 61 67 65 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 ages with pgno>x
55880 2e 20 20 52 65 73 65 74 20 74 68 65 20 63 61 63 . Reset the cac
55890 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 he if x==0 */.SQ
558a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
558b0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 d sqlite3PcacheT
558c0 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 2a 2c runcate(PCache*,
558d0 20 50 67 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 Pgno x);../* Ge
558e0 74 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 t a list of all
558f0 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 dirty pages in t
55900 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 he cache, sorted
55910 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 20 by page number
55920 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
55930 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 E PgHdr *sqlite3
55940 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
55950 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 PCache*);../* Re
55960 73 65 74 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 set and close th
55970 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a e cache object *
55980 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55990 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
559a0 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65 2a cheClose(PCache*
559b0 29 3b 0a 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 );../* Clear fla
559c0 67 73 20 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 gs from pages of
559d0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
559e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
559f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
55a00 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 acheClearSyncFla
55a10 67 73 28 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f gs(PCache *);../
55a20 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f * Discard the co
55a30 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 ntents of the ca
55a40 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 che */.SQLITE_PR
55a50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
55a60 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 e3PcacheClear(PC
55a70 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 ache*);../* Retu
55a80 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d rn the total num
55a90 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 ber of outstandi
55aa0 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 ng page referenc
55ab0 65 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 es */.SQLITE_PRI
55ac0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
55ad0 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 50 PcacheRefCount(P
55ae0 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 Cache*);../* Inc
55af0 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 rement the refer
55b00 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e ence count of an
55b10 20 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 2a existing page *
55b20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55b30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55b40 63 68 65 52 65 66 28 50 67 48 64 72 2a 29 3b 0a cheRef(PgHdr*);.
55b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55b60 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
55b70 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 50 67 ePageRefcount(Pg
55b80 48 64 72 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 Hdr*);../* Retur
55b90 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 n the total numb
55ba0 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72 er of pages stor
55bb0 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 ed in the cache
55bc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
55bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
55be0 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61 chePagecount(PCa
55bf0 63 68 65 2a 29 3b 0a 0a 23 69 66 20 64 65 66 69 che*);..#if defi
55c00 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45 43 4b ned(SQLITE_CHECK
55c10 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66 69 6e _PAGES) || defin
55c20 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
55c30 0a 2f 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f ./* Iterate thro
55c40 75 67 68 20 61 6c 6c 20 64 69 72 74 79 20 70 61 ugh all dirty pa
55c50 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 ges currently st
55c60 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 ored in the cach
55c70 65 2e 20 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 e. This.** inter
55c80 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 face is only ava
55c90 69 6c 61 62 6c 65 20 69 66 20 53 51 4c 49 54 45 ilable if SQLITE
55ca0 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 _CHECK_PAGES is
55cb0 64 65 66 69 6e 65 64 20 77 68 65 6e 20 74 68 65 defined when the
55cc0 20 0a 2a 2a 20 6c 69 62 72 61 72 79 20 69 73 20 .** library is
55cd0 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 built..*/.SQLITE
55ce0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55cf0 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61 lite3PcacheItera
55d00 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 2a teDirty(PCache *
55d10 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 pCache, void (*x
55d20 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 3b Iter)(PgHdr *));
55d30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 .#endif../* Set
55d40 61 6e 64 20 67 65 74 20 74 68 65 20 73 75 67 67 and get the sugg
55d50 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 ested cache-size
55d60 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
55d70 65 64 20 70 61 67 65 72 2d 63 61 63 68 65 2e 0a ed pager-cache..
55d80 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 **.** If no glob
55d90 61 6c 20 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f al maximum is co
55da0 6e 66 69 67 75 72 65 64 2c 20 74 68 65 6e 20 74 nfigured, then t
55db0 68 65 20 73 79 73 74 65 6d 20 61 74 74 65 6d 70 he system attemp
55dc0 74 73 20 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 ts to limit.** t
55dd0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
55de0 6f 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 of pages cached
55df0 62 79 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 by purgeable pag
55e00 65 72 2d 63 61 63 68 65 73 20 74 6f 20 74 68 65 er-caches to the
55e10 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 sum.** of the s
55e20 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 uggested cache-s
55e30 69 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f izes..*/.SQLITE_
55e40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
55e50 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 ite3PcacheSetCac
55e60 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 2c hesize(PCache *,
55e70 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 int);.#ifdef SQ
55e80 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
55e90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
55ea0 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63 ite3PcacheGetCac
55eb0 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 29 hesize(PCache *)
55ec0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
55ed0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
55ee0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
55ef0 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65 74 75 72 ./* Try to retur
55f00 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 n memory used by
55f10 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 the pcache modu
55f20 6c 65 20 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d le to the main m
55f30 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 emory heap */.SQ
55f40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
55f50 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
55f60 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 leaseMemory(int)
55f70 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ;.#endif..#ifdef
55f80 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
55f90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
55fa0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 sqlite3PcacheSt
55fb0 61 74 73 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e ats(int*,int*,in
55fc0 74 2a 2c 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 t*,int*);.#endif
55fd0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
55fe0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 void sqlite3PCa
55ff0 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f cheSetDefault(vo
56000 69 64 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 id);..#endif /*
56010 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f _PCACHE_H_ */../
56020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
56030 6e 64 20 6f 66 20 70 63 61 63 68 65 2e 68 20 2a nd of pcache.h *
56040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
56070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
56080 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
56090 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 we left off in s
560a0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
560b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
560c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
560d0 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 Include os.h in
560e0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 the middle of sq
560f0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
56100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
56110 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
56120 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 Begin file os.h
56130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
56160 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
56170 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 mber 16.**.** Th
56180 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
56190 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
561a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
561b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
561c0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
561d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
561e0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
561f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
56200 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
56210 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
56220 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
56230 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
56240 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
56250 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
56260 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
56270 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
56280 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
56290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
562d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
562e0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
562f0 6c 65 20 28 74 6f 67 65 74 68 65 72 20 77 69 74 le (together wit
56300 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 h is companion C
56310 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 66 69 6c source-code fil
56320 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 e.** "os.c") att
56330 65 6d 70 74 20 74 6f 20 61 62 73 74 72 61 63 74 empt to abstract
56340 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
56350 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
56360 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 so that.** the
56370 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 SQLite library w
56380 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 ill work on both
56390 20 50 4f 53 49 58 20 61 6e 64 20 77 69 6e 64 6f POSIX and windo
563a0 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a ws systems..**.*
563b0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 * This header fi
563c0 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 le is #include-e
563d0 64 20 62 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 d by sqliteInt.h
563e0 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20 75 and thus ends u
563f0 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 p.** being inclu
56400 64 65 64 20 62 79 20 65 76 65 72 79 20 73 6f 75 ded by every sou
56410 72 63 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 rce file..*/.#if
56420 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f ndef _SQLITE_OS_
56430 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 H_.#define _SQLI
56440 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 TE_OS_H_../*.**
56450 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 Figure out if we
56460 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
56470 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c h Unix, Windows,
56480 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a or some other.*
56490 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 * operating syst
564a0 65 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 em. After the f
564b0 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f ollowing block o
564c0 66 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 f preprocess mac
564d0 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 ros,.** all of S
564e0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 QLITE_OS_UNIX, S
564f0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 QLITE_OS_WIN, SQ
56500 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 LITE_OS_OS2, and
56510 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 SQLITE_OS_OTHER
56520 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 .** will define
56530 64 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 d to either 1 or
56540 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 0. One of the
56550 66 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 four will be 1.
56560 20 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 The other .** t
56570 68 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a hree will be 0..
56580 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
56590 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a QLITE_OS_OTHER).
565a0 23 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f # if SQLITE_OS_O
565b0 54 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 THER==1.# unde
565c0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 f SQLITE_OS_UNIX
565d0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 .# define SQLI
565e0 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 TE_OS_UNIX 0.#
565f0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 undef SQLITE_OS
56600 5f 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 _WIN.# define
56610 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a SQLITE_OS_WIN 0.
56620 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 # undef SQLITE
56630 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 _OS_OS2.# defi
56640 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 ne SQLITE_OS_OS2
56650 20 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 0.# else.# un
56660 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 def SQLITE_OS_OT
56670 48 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 HER.# endif.#end
56680 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 if.#if !defined(
56690 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 SQLITE_OS_UNIX)
566a0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
566b0 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 TE_OS_OTHER).# d
566c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
566d0 4f 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 OTHER 0.# ifndef
566e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 SQLITE_OS_WIN.#
566f0 20 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 if defined(_W
56700 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 IN32) || defined
56710 28 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e (WIN32) || defin
56720 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c ed(__CYGWIN__) |
56730 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 | defined(__MING
56740 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 W32__) || define
56750 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a d(__BORLANDC__).
56760 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c # define SQL
56770 49 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 ITE_OS_WIN 1.#
56780 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
56790 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 _OS_UNIX 0.#
567a0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
567b0 53 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 S_OS2 0.# elif
567c0 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f defined(__EMX__
567d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 ) || defined(_OS
567e0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 2) || defined(OS
567f0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 2) || defined(_O
56800 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 S2_) || defined(
56810 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 __OS2__).# d
56820 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56830 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 WIN 0.# defi
56840 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 ne SQLITE_OS_UNI
56850 58 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 X 0.# define
56860 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 SQLITE_OS_OS2 1
56870 0a 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 .# else.#
56880 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
56890 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 _WIN 0.# def
568a0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e ine SQLITE_OS_UN
568b0 49 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e IX 1.# defin
568c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 e SQLITE_OS_OS2
568d0 30 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 0.# endif.# els
568e0 65 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 e.# define SQLI
568f0 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 TE_OS_UNIX 0.#
56900 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
56910 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 _OS2 0.# endif.#
56920 65 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 else.# ifndef SQ
56930 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 LITE_OS_WIN.# d
56940 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56950 57 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 WIN 0.# endif.#e
56960 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 ndif../*.** Dete
56970 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 rmine if we are
56980 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e dealing with Win
56990 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 dowsCE - which h
569a0 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 as a much.** red
569b0 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 uced API..*/.#if
569c0 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f defined(_WIN32_
569d0 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 WCE).# define SQ
569e0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a LITE_OS_WINCE 1.
569f0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
56a00 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 QLITE_OS_WINCE 0
56a10 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
56a20 44 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d Define the maxim
56a30 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d um size of a tem
56a40 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a porary filename.
56a50 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
56a60 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c _WIN.# include <
56a70 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 windows.h>.# def
56a80 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e ine SQLITE_TEMPN
56a90 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 AME_SIZE (MAX_PA
56aa0 54 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c TH+50).#elif SQL
56ab0 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 ITE_OS_OS2.# if
56ac0 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c (__GNUC__ > 3 ||
56ad0 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 __GNUC__ == 3 &
56ae0 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f & __GNUC_MINOR__
56af0 20 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 >= 3) && define
56b00 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 d(OS2_HIGH_MEMOR
56b10 59 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f Y).# include <o
56b20 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 s2safe.h> /* has
56b30 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 to be included
56b40 62 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 before os2.h for
56b50 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b linking to work
56b60 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 */.# endif.# de
56b70 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 fine INCL_DOSDAT
56b80 45 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 ETIME.# define I
56b90 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 NCL_DOSFILEMGR.#
56ba0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 define INCL_DOS
56bb0 45 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 ERRORS.# define
56bc0 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 INCL_DOSMISC.# d
56bd0 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 efine INCL_DOSPR
56be0 4f 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 OCESS.# define I
56bf0 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 NCL_DOSMODULEMGR
56c00 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
56c10 4f 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 OSSEMAPHORES.# i
56c20 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 nclude <os2.h>.#
56c30 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e include <uconv.
56c40 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 h>.# define SQLI
56c50 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 TE_TEMPNAME_SIZE
56c60 20 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 (CCHMAXPATHCOMP
56c70 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
56c80 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 SQLITE_TEMPNAME
56c90 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 _SIZE 200.#endif
56ca0 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f ../* If the SET_
56cb0 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 FULLSYNC macro i
56cc0 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 s not defined ab
56cd0 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 ove, then make i
56ce0 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a t.** a no-op.*/.
56cf0 23 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c #ifndef SET_FULL
56d00 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 SYNC.# define SE
56d10 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a T_FULLSYNC(x,y).
56d20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
56d30 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f e default size o
56d40 66 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a f a disk sector.
56d50 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
56d60 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
56d70 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 _SIZE.# define S
56d80 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
56d90 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 CTOR_SIZE 512.#e
56da0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 ndif../*.** Temp
56db0 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 orary files are
56dc0 6e 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 named starting w
56dd0 69 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 ith this prefix
56de0 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 followed by 16 r
56df0 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 andom.** alphanu
56e00 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 meric characters
56e10 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 , and no file ex
56e20 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 tension. They ar
56e30 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a e stored in the.
56e40 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 ** OS's standard
56e50 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
56e60 64 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 directory, and a
56e70 72 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 re deleted prior
56e80 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 to exit..** If
56e90 73 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 sqlite is being
56ea0 65 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 embedded in anot
56eb0 68 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 her program, you
56ec0 20 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 may wish to cha
56ed0 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 nge the.** prefi
56ee0 78 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 x to reflect you
56ef0 72 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 r program's name
56f00 2c 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 , so that if you
56f10 72 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a r program exits.
56f20 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 ** prematurely,
56f30 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 old temporary fi
56f40 6c 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c les can be easil
56f50 79 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 y identified. Th
56f60 69 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a is can be done.*
56f70 2a 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 * using -DSQLITE
56f80 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 _TEMP_FILE_PREFI
56f90 58 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 X=myprefix_ on t
56fa0 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d he compiler comm
56fb0 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 and line..**.**
56fc0 32 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 2006-10-31: The
56fd0 20 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 default prefix
56fe0 75 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 used to be "sqli
56ff0 74 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a te_". But then.
57000 2a 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 ** Mcafee starte
57010 64 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 d using SQLite i
57020 6e 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 n their anti-vir
57030 75 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 us product and i
57040 74 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 t.** started put
57050 74 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 ting files with
57060 74 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d the "sqlite" nam
57070 65 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 e in the c:/temp
57080 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 folder..** This
57090 20 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 annoyed many wi
570a0 6e 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 ndows users. Th
570b0 6f 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 ose users would
570c0 74 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f then do a .** Go
570d0 6f 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 ogle search for
570e0 22 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 "sqlite", find t
570f0 68 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d he telephone num
57100 62 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 bers of the.** d
57110 65 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 evelopers and ca
57120 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 ll to wake them
57130 75 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 up at night and
57140 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 complain..** For
57150 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 this reason, th
57160 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 e default name p
57170 72 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 refix is changed
57180 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 to be "sqlite"
57190 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b .** spelled back
571a0 77 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 wards. So the t
571b0 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 emp files are st
571c0 69 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 ill identified,
571d0 62 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 but.** anybody s
571e0 6d 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 mart enough to f
571f0 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f igure out the co
57200 64 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c de is also likel
57210 79 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 y smart.** enoug
57220 68 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 h to know that c
57230 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c alling the devel
57240 6f 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 oper will not he
57250 6c 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 lp get rid.** of
57260 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 the file..*/.#i
57270 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d fndef SQLITE_TEM
57280 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 P_FILE_PREFIX.#
57290 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
572a0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 MP_FILE_PREFIX "
572b0 65 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a etilqs_".#endif.
572c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
572d0 77 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 wing values may
572e0 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
572f0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
57300 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 to.** sqlite3Os
57310 4c 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 Lock(). The vari
57320 6f 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 ous locks exhibi
57330 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 t the following
57340 73 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a semantics:.**.**
57350 20 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 SHARED: Any
57360 6e 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 number of proces
57370 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 ses may hold a S
57380 48 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c HARED lock simul
57390 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 taneously..** RE
573a0 53 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c SERVED: A singl
573b0 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f e process may ho
573c0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f ld a RESERVED lo
573d0 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a ck on a file at.
573e0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e ** an
573f0 79 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 y time. Other pr
57400 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 ocesses may hold
57410 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 and obtain new
57420 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a SHARED locks..**
57430 20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 PENDING: A si
57440 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 ngle process may
57450 20 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 hold a PENDING
57460 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 lock on a file a
57470 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
57480 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 any one time. Ex
57490 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f isting SHARED lo
574a0 63 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c cks may persist,
574b0 20 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 but no new.**
574c0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 SHARED
574d0 20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 locks may be ob
574e0 74 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 tained by other
574f0 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 processes..** EX
57500 43 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c CLUSIVE: An EXCL
57510 55 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c USIVE lock precl
57520 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c udes all other l
57530 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 ocks..**.** PEND
57540 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 ING_LOCK may not
57550 20 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 be passed direc
57560 74 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 tly to sqlite3Os
57570 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c Lock(). Instead,
57580 20 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 a.** process th
57590 61 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 at requests an E
575a0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 XCLUSIVE lock ma
575b0 79 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 y actually obtai
575c0 6e 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c n a PENDING.** l
575d0 6f 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 ock. This can be
575e0 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 upgraded to an
575f0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 EXCLUSIVE lock b
57600 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 y a subsequent c
57610 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
57620 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 3OsLock()..*/.#d
57630 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 efine NO_LOCK
57640 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
57650 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 SHARED_LOCK
57660 31 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 1.#define RESERV
57670 45 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 ED_LOCK 2.#def
57680 69 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b ine PENDING_LOCK
57690 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 3.#define EX
576a0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a CLUSIVE_LOCK 4.
576b0 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b ./*.** File Lock
576c0 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 ing Notes: (Mos
576d0 74 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 tly about window
576e0 73 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 s but also some
576f0 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a info for Unix).*
57700 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 *.** We cannot u
57710 73 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 se LockFileEx()
57720 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 or UnlockFileEx(
57730 29 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 ) on Win95/98/ME
57740 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 because.** thos
57750 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
57760 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 not available.
57770 53 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c So we use only L
57780 6f 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a ockFile() and.**
57790 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a UnlockFile()..*
577a0 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 *.** LockFile()
577b0 70 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 prevents not jus
577c0 74 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c t writing but al
577d0 73 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 so reading by ot
577e0 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a her processes..*
577f0 2a 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 * A SHARED_LOCK
57800 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c is obtained by l
57810 6f 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 ocking a single
57820 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 randomly-chosen
57830 0a 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 .** byte out of
57840 61 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 a specific range
57850 20 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c of bytes. The l
57860 6f 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 ock byte is obta
57870 69 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 ined at .** rand
57880 6f 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 om so two separa
57890 74 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 te readers can p
578a0 72 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 robably access t
578b0 68 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a he file at the .
578c0 2a 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e ** same time, un
578d0 6c 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e less they are un
578e0 6c 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 lucky and choose
578f0 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 the same lock b
57900 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 yte..** An EXCLU
57910 53 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 SIVE_LOCK is obt
57920 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
57930 20 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 all bytes in th
57940 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 e range..** Ther
57950 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e e can only be on
57960 65 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 e writer. A RES
57970 45 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 ERVED_LOCK is ob
57980 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e tained by lockin
57990 67 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 g.** a single by
579a0 74 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 te of the file t
579b0 68 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 hat is designate
579c0 64 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 d as the reserve
579d0 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 d lock byte..**
579e0 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 A PENDING_LOCK i
579f0 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f s obtained by lo
57a00 63 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 cking a designat
57a10 65 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e ed byte differen
57a20 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 t from.** the RE
57a30 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 SERVED_LOCK byte
57a40 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 ..**.** On WinNT
57a50 2f 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 /2K/XP systems,
57a60 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 LockFileEx() and
57a70 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 UnlockFileEx()
57a80 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a are available,.*
57a90 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 * which means we
57aa0 20 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f can use reader/
57ab0 77 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 writer locks. W
57ac0 68 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 hen reader/write
57ad0 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 r locks.** are u
57ae0 73 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 sed, the lock is
57af0 20 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 placed on the s
57b00 61 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 ame range of byt
57b10 65 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a es that is used.
57b20 2a 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 ** for probabili
57b30 73 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 stic locking in
57b40 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 Win95/98/ME. He
57b50 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 nce, the locking
57b60 20 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 scheme.** will
57b70 73 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d support two or m
57b80 6f 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 ore Win95 reader
57b90 73 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 s or two or more
57ba0 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a WinNT readers..
57bb0 2a 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 ** But a single
57bc0 57 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c Win95 reader wil
57bd0 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 l lock out all W
57be0 69 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 inNT readers and
57bf0 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e a single.** Win
57c00 4e 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c NT reader will l
57c10 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 ock out all othe
57c20 72 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e r Win95 readers.
57c30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
57c40 77 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 wing #defines sp
57c50 65 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 ecify the range
57c60 6f 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f of bytes used fo
57c70 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 r locking..** SH
57c80 41 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 ARED_SIZE is the
57c90 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
57ca0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
57cb0 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 e pool from whic
57cc0 68 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 h.** a random by
57cd0 74 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 te is selected f
57ce0 6f 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b or a shared lock
57cf0 2e 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 . The pool of b
57d00 79 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 ytes for.** shar
57d10 65 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 ed locks begins
57d20 61 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e at SHARED_FIRST.
57d30 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 .**.** The same
57d40 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
57d50 79 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 y and.** byte ra
57d60 6e 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f nges are used fo
57d70 72 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 r Unix. This le
57d80 61 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f aves open the po
57d90 73 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 ssiblity of havi
57da0 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e ng.** clients on
57db0 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 win95, winNT, a
57dc0 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b nd unix all talk
57dd0 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ing to the same
57de0 73 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 shared file.** a
57df0 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 nd all locking c
57e00 6f 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f orrectly. To do
57e10 20 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 so would requir
57e20 65 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 e that samba (or
57e30 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f whatever.** too
57e40 6c 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 l is being used
57e50 66 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 for file sharing
57e60 29 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 ) implements loc
57e70 6b 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 ks correctly bet
57e80 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 ween.** windows
57e90 61 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 and unix. I'm g
57ea0 75 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e uessing that isn
57eb0 27 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 't likely to hap
57ec0 70 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 pen, but by.** u
57ed0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f sing the same lo
57ee0 63 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 cking range we a
57ef0 72 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e re at least open
57f00 20 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c to the possibil
57f10 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 ity..**.** Locki
57f20 6e 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 ng in windows is
57f30 20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 manditory. For
57f40 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 this reason, we
57f50 20 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a cannot store.**
57f60 20 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 actual data in
57f70 74 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66 the bytes used f
57f80 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 or locking. The
57f90 20 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c pager never all
57fa0 6f 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 ocates.** the pa
57fb0 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 ges involved in
57fc0 6c 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 locking therefor
57fd0 65 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 e. SHARED_SIZE
57fe0 69 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a is selected so.*
57ff0 2a 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 * that all locks
58000 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 will fit on a s
58010 69 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 ingle page even
58020 61 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 at the minimum p
58030 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e age size..** PEN
58040 44 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 DING_BYTE define
58050 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 s the beginning
58060 6f 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 of the locks. B
58070 79 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e y default PENDIN
58080 47 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 G_BYTE.** is set
58090 20 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 high so that we
580a0 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 don't have to a
580b0 6c 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 llocate an unuse
580c0 64 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a d page except.**
580d0 20 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 for very large
580e0 64 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 databases. But
580f0 6f 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 one should test
58100 74 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e the page skippin
58110 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 g logic .** by s
58120 65 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 etting PENDING_B
58130 59 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e YTE low and runn
58140 69 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 ing the entire r
58150 65 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e egression suite.
58160 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 .**.** Changing
58170 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e the value of PEN
58180 44 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 DING_BYTE result
58190 73 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e s in a subtly in
581a0 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 compatible.** fi
581b0 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 le format. Depe
581c0 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 nding on how it
581d0 69 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 is changed, you
581e0 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 might not notice
581f0 0a 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 .** the incompat
58200 69 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 ibility right aw
58210 61 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 ay, even running
58220 20 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 a full regressi
58230 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 on test..** The
58240 64 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e default location
58250 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 of PENDING_BYTE
58260 20 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79 is the first by
58270 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 te past the.** 1
58280 47 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a GB boundary..**.
58290 2a 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 */.#define PENDI
582a0 4e 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c NG_BYTE sql
582b0 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a ite3PendingByte.
582c0 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 #define RESERVED
582d0 5f 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 _BYTE (PENDI
582e0 4e 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 NG_BYTE+1).#defi
582f0 6e 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 ne SHARED_FIRST
58300 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 (PENDING_BY
58310 54 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 TE+2).#define SH
58320 41 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 ARED_SIZE
58330 35 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 510../*.** Wrapp
58340 65 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 er around OS spe
58350 63 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 cific sqlite3_os
58360 5f 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e _init() function
58370 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
58380 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58390 73 49 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a sInit(void);../*
583a0 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 .** Functions f
583b0 6f 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c or accessing sql
583c0 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 ite3_file method
583d0 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s .*/.SQLITE_PRI
583e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
583f0 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f OsClose(sqlite3_
58400 66 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 file*);.SQLITE_P
58410 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58420 65 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 e3OsRead(sqlite3
58430 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 _file*, void*, i
58440 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 nt amt, i64 offs
58450 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 et);.SQLITE_PRIV
58460 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58470 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 sWrite(sqlite3_f
58480 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ile*, const void
58490 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 *, int amt, i64
584a0 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f offset);.SQLITE_
584b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
584c0 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 te3OsTruncate(sq
584d0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 lite3_file*, i64
584e0 20 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 size);.SQLITE_P
584f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58500 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 e3OsSync(sqlite3
58510 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 _file*, int);.SQ
58520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
58530 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
58540 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ze(sqlite3_file*
58550 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 , i64 *pSize);.S
58560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
58570 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 t sqlite3OsLock(
58580 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
58590 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
585a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
585b0 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f sUnlock(sqlite3_
585c0 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c file*, int);.SQL
585d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
585e0 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 sqlite3OsCheckRe
585f0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
58600 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
58610 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 *pResOut);.SQLI
58620 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58630 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 qlite3OsFileCont
58640 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 rol(sqlite3_file
58650 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 *,int,void*);.#d
58660 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e efine SQLITE_FCN
58670 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 TL_DB_UNCHANGED
58680 30 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 0xca093fa0.SQLIT
58690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
586a0 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a lite3OsSectorSiz
586b0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
586c0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
586d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
586e0 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
586f0 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 istics(sqlite3_f
58700 69 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a ile *id);../* .*
58710 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 * Functions for
58720 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 accessing sqlite
58730 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 3_vfs methods .*
58740 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
58750 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 int sqlite3OsOp
58760 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a en(sqlite3_vfs *
58770 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
58780 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
58790 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 nt, int *);.SQLI
587a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
587b0 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 qlite3OsDelete(s
587c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
587d0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 nst char *, int)
587e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
587f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 int sqlite3OsAc
58800 63 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 cess(sqlite3_vfs
58810 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
58820 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 , int, int *pRes
58830 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Out);.SQLITE_PRI
58840 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
58850 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 OsFullPathname(s
58860 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
58870 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c nst char *, int,
58880 20 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 char *);.#ifnde
58890 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
588a0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c AD_EXTENSION.SQL
588b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
588c0 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 *sqlite3OsDlOpe
588d0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
588e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a const char *);.
588f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
58900 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 oid sqlite3OsDlE
58910 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
58920 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 *, int, char *)
58930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
58940 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f void (*sqlite3O
58950 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 sDlSym(sqlite3_v
58960 66 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f fs *, void *, co
58970 6e 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 nst char *))(voi
58980 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
58990 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
589a0 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 sDlClose(sqlite3
589b0 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b _vfs *, void *);
589c0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
589d0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
589e0 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f NSION */.SQLITE_
589f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
58a00 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 te3OsRandomness(
58a10 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 sqlite3_vfs *, i
58a20 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nt, char *);.SQL
58a30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58a40 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 sqlite3OsSleep(s
58a50 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e qlite3_vfs *, in
58a60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
58a70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
58a80 43 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 CurrentTime(sqli
58a90 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c te3_vfs *, doubl
58aa0 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 e*);../*.** Conv
58ab0 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e enience function
58ac0 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e s for opening an
58ad0 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 d closing files
58ae0 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 using .** sqlite
58af0 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 3_malloc() to ob
58b00 74 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 tain space for t
58b10 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 he file-handle s
58b20 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c tructure..*/.SQL
58b30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58b40 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c sqlite3OsOpenMal
58b50 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 loc(sqlite3_vfs
58b60 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
58b70 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a sqlite3_file **
58b80 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c , int,int*);.SQL
58b90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58ba0 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 sqlite3OsCloseFr
58bb0 65 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ee(sqlite3_file
58bc0 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f *);..#endif /* _
58bd0 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a SQLITE_OS_H_ */.
58be0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
58bf0 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a End of os.h ***
58c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
58c30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
58c40 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
58c50 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
58c60 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
58c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
58c80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
58c90 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 Include mutex.h
58ca0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
58cb0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
58cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
58cd0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
58ce0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
58cf0 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.h ************
58d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
58d20 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
58d30 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 28.**.** The
58d40 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
58d50 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
58d60 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
58d70 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
58d80 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
58d90 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
58da0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
58db0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
58dc0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
58dd0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
58de0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
58df0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
58e00 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
58e10 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
58e20 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
58e30 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
58e40 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
58e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58e90 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
58ea0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
58eb0 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 e common header
58ec0 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d for all mutex im
58ed0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
58ee0 2a 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e * The sqliteInt.
58ef0 68 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 h header #includ
58f00 65 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 es this file so
58f10 74 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c that it is avail
58f20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 able.** to all s
58f30 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 ource files. We
58f40 20 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e break it out in
58f50 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 an effort to ke
58f60 65 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 ep the code.** b
58f70 65 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e etter organized.
58f80 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f .**.** NOTE: so
58f90 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c urce files shoul
58fa0 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 d *not* #include
58fb0 20 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c this header fil
58fc0 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 e directly..** S
58fd0 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 ource files shou
58fe0 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 ld #include the
58ff0 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 sqliteInt.h file
59000 20 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 and let that fi
59010 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 le.** include th
59020 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c is one indirectl
59030 79 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 y..*/.../*.** Fi
59040 67 75 72 65 20 6f 75 74 20 77 68 61 74 20 76 65 gure out what ve
59050 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 64 rsion of the cod
59060 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65 20 63 e to use. The c
59070 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a 2a 2a hoices are.**.**
59080 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f SQLITE_MUTEX_
59090 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e 6f 20 OMIT No
590a0 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20 4e 6f mutex logic. No
590b0 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20 20 54 t even stubs. T
590c0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
590d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
590e0 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c 65 6d mutexes implem
590f0 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 ention cannot be
59100 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a 20 20 overridden.**
59110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59120 20 20 20 20 20 20 20 20 20 20 20 61 74 20 73 74 at st
59130 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 art-time..**.**
59140 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e SQLITE_MUTEX_N
59150 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f 72 20 OOP For
59160 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
59170 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 4e applications. N
59180 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 o.**
59190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
591a0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f mutual exclusio
591b0 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20 n is provided.
591c0 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20 20 20 But this.**
591d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
591e0 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e implemen
591f0 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 6f 76 tation can be ov
59200 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a 20 20 erridden at.**
59210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59220 20 20 20 20 20 20 20 20 20 20 20 73 74 61 72 74 start
59230 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 -time..**.** S
59240 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 QLITE_MUTEX_PTHR
59250 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c EADS For mul
59260 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c ti-threaded appl
59270 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78 ications on Unix
59280 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 ..**.** SQLITE
59290 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20 20 20 _MUTEX_W32
592a0 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 For multi-th
592b0 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 readed applicati
592c0 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a 2a 2a ons on Win32..**
592d0 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 .** SQLITE_MUT
592e0 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20 20 20 EX_OS2
592f0 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 For multi-thread
59300 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ed applications
59310 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20 on OS/2..*/.#if
59320 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 !SQLITE_THREADSA
59330 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 FE.# define SQLI
59340 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 TE_MUTEX_OMIT.#e
59350 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
59360 54 48 52 45 41 44 53 41 46 45 20 26 26 20 21 64 THREADSAFE && !d
59370 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 efined(SQLITE_MU
59380 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 TEX_NOOP).# if
59390 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 SQLITE_OS_UNIX.#
593a0 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
593b0 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 E_MUTEX_PTHREADS
593c0 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f .# elif SQLITE_
593d0 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 OS_WIN.# defi
593e0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
593f0 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 W32.# elif SQLI
59400 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 20 64 TE_OS_OS2.# d
59410 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 efine SQLITE_MUT
59420 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65 0a 23 EX_OS2.# else.#
59430 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
59440 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 20 E_MUTEX_NOOP.#
59450 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 23 69 endif.#endif..#i
59460 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 fdef SQLITE_MUTE
59470 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 66 20 X_OMIT./*.** If
59480 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 this is a no-op
59490 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
594a0 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72 79 74 implement everyt
594b0 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73 2e 0a hing as macros..
594c0 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 */.#define sqlit
594d0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 58 e3_mutex_alloc(X
594e0 29 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d ) ((sqlite3_m
594f0 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 utex*)8).#define
59500 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 sqlite3_mutex_f
59510 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 ree(X).#define s
59520 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
59530 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 er(X).#define sq
59540 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
59550 58 29 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f X) SQLITE_O
59560 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 K.#define sqlite
59570 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 3_mutex_leave(X)
59580 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
59590 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29 20 20 _mutex_held(X)
595a0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73 71 6c 1.#define sql
595b0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
595c0 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69 6e 65 ld(X) 1.#define
595d0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
595e0 6f 63 28 58 29 20 20 20 20 20 20 28 28 73 71 6c oc(X) ((sql
595f0 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 ite3_mutex*)8).#
59600 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 define sqlite3Mu
59610 74 65 78 49 6e 69 74 28 29 20 20 20 20 20 20 20 texInit()
59620 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 SQLITE_OK.#defi
59630 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 ne sqlite3MutexE
59640 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a 20 64 nd().#endif /* d
59650 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 efined(SQLITE_MU
59660 54 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a 0a 2f 2a TEX_OMIT) */../*
59670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
59680 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a d of mutex.h ***
59690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
596a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
596b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
596c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
596d0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
596e0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
596f0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
59700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a ************/...
59710 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 /*.** Each datab
59720 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 ase file to be a
59730 63 63 65 73 73 65 64 20 62 79 20 74 68 65 20 73 ccessed by the s
59740 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e 73 74 ystem is an inst
59750 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 ance.** of the f
59760 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
59770 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 6e re. There are n
59780 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74 ormally two of t
59790 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 0a hese structures.
597a0 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 ** in the sqlite
597b0 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61 .aDb[] array. a
597c0 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69 Db[0] is the mai
597d0 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
597e0 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73 and.** aDb[1] is
597f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
59800 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 le used to hold
59810 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
59820 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a . Additional.**
59830 20 64 61 74 61 62 61 73 65 73 20 6d 61 79 20 62 databases may b
59840 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 e attached..*/.s
59850 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63 68 61 truct Db {. cha
59860 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
59870 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 /* Name of thi
59880 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 s database */.
59890 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 Btree *pBt;
598a0 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a 54 72 /* The B*Tr
598b0 65 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 ee structure for
598c0 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 this database f
598d0 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 ile */. u8 inTr
598e0 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ans; /*
598f0 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62 6c 65 0: not writable
59900 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74 69 6f . 1: Transactio
59910 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f 69 6e n. 2: Checkpoin
59920 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65 74 79 t */. u8 safety
59930 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 48 _level; /* H
59940 6f 77 20 61 67 67 72 65 73 73 69 76 65 20 61 74 ow aggressive at
59950 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20 74 6f syncing data to
59960 20 64 69 73 6b 20 2a 2f 0a 20 20 53 63 68 65 6d disk */. Schem
59970 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 a *pSchema;
59980 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 /* Pointer to da
59990 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 28 70 tabase schema (p
599a0 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64 29 20 ossibly shared)
599b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
599c0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
599d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
599e0 75 72 65 20 73 74 6f 72 65 73 20 61 20 64 61 74 ure stores a dat
599f0 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a abase schema..**
59a00 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
59a10 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c no virtual tabl
59a20 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20 69 6e es configured in
59a30 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20 74 68 this schema, th
59a40 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62 20 76 e.** Schema.db v
59a50 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
59a60 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20 74 68 o NULL. After th
59a70 65 20 66 69 72 73 74 20 76 69 72 74 75 61 6c 20 e first virtual
59a80 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 table.** has bee
59a90 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73 20 73 n added, it is s
59aa0 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 et to point to t
59ab0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
59ac0 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 64 20 ection .** used
59ad0 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 63 6f to create the co
59ae0 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 20 61 nnection. Once a
59af0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 virtual table h
59b00 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64 65 64 as been.** added
59b10 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61 20 73 to the Schema s
59b20 74 72 75 63 74 75 72 65 20 61 6e 64 20 74 68 65 tructure and the
59b30 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72 69 61 Schema.db varia
59b40 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c 20 0a ble populated, .
59b50 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64 61 74 ** only that dat
59b60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
59b70 20 6d 61 79 20 75 73 65 20 74 68 65 20 53 63 68 may use the Sch
59b80 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65 20 0a ema to prepare .
59b90 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a ** statements..*
59ba0 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 /.struct Schema
59bb0 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61 5f 63 {. int schema_c
59bc0 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61 74 61 ookie; /* Data
59bd0 62 61 73 65 20 73 63 68 65 6d 61 20 76 65 72 73 base schema vers
59be0 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 ion number for t
59bf0 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 61 his file */. Ha
59c00 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20 20 20 sh tblHash;
59c10 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 /* All tables
59c20 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 indexed by name
59c30 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78 48 61 */. Hash idxHa
59c40 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c sh; /* Al
59c50 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69 63 65 l (named) indice
59c60 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d s indexed by nam
59c70 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72 69 67 e */. Hash trig
59c80 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 Hash; /* A
59c90 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e 64 65 ll triggers inde
59ca0 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 xed by name */.
59cb0 20 48 61 73 68 20 66 6b 65 79 48 61 73 68 3b 20 Hash fkeyHash;
59cc0 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66 6f 72 /* All for
59cd0 65 69 67 6e 20 6b 65 79 73 20 62 79 20 72 65 66 eign keys by ref
59ce0 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 6e 61 erenced table na
59cf0 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 me */. Table *p
59d00 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 SeqTab; /*
59d10 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 The sqlite_seque
59d20 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 nce table used b
59d30 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 y AUTOINCREMENT
59d40 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 */. u8 file_for
59d50 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 mat; /* Sch
59d60 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69 ema format versi
59d70 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 on for this file
59d80 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 */. u8 enc;
59d90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
59da0 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 xt encoding used
59db0 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73 by this databas
59dc0 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 e */. u16 flags
59dd0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ; /* F
59de0 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 lags associated
59df0 77 69 74 68 20 74 68 69 73 20 73 63 68 65 6d 61 with this schema
59e00 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f */. int cache_
59e10 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 size; /* Nu
59e20 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f mber of pages to
59e30 20 75 73 65 20 69 6e 20 74 68 65 20 63 61 63 68 use in the cach
59e40 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c e */.#ifndef SQL
59e50 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
59e60 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 TABLE. sqlite3
59e70 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 *db; /*
59e80 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 "Owner" connecti
59e90 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 on. See comment
59ea0 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a above */.#endif.
59eb0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 };../*.** These
59ec0 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 macros can be us
59ed0 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c ed to test, set,
59ee0 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 or clear bits i
59ef0 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 n the .** Db.fla
59f00 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 gs field..*/.#de
59f10 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72 fine DbHasProper
59f20 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28 ty(D,I,P) ((
59f30 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 (D)->aDb[I].pSch
59f40 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d ema->flags&(P))=
59f50 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62 =(P)).#define Db
59f60 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44 HasAnyProperty(D
59f70 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44 ,I,P) (((D)->aD
59f80 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c b[I].pSchema->fl
59f90 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 ags&(P))!=0).#de
59fa0 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72 fine DbSetProper
59fb0 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44 ty(D,I,P) (D
59fc0 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d )->aDb[I].pSchem
59fd0 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 a->flags|=(P).#d
59fe0 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f efine DbClearPro
59ff0 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28 perty(D,I,P) (
5a000 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 D)->aDb[I].pSche
5a010 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a ma->flags&=~(P).
5a020 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
5a030 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 alues for the DB
5a040 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a .flags field..**
5a050 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d .** The DB_Schem
5a060 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 aLoaded flag is
5a070 73 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61 set after the da
5a080 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 tabase schema ha
5a090 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 s been.** read i
5a0a0 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 nto internal has
5a0b0 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 h tables..**.**
5a0c0 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 DB_UnresetViews
5a0d0 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f means that one o
5a0e0 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 r more views hav
5a0f0 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 e column names t
5a100 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e hat.** have been
5a110 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 filled out. If
5a120 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e the schema chan
5a130 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d ges, these colum
5a140 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a n names might.**
5a150 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 changes and so
5a160 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 the view will ne
5a170 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a ed to be reset..
5a180 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 */.#define DB_Sc
5a190 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 hemaLoaded 0x
5a1a0 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 0001 /* The sch
5a1b0 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 ema has been loa
5a1c0 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 ded */.#define D
5a1d0 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 B_UnresetViews
5a1e0 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 0x0002 /* Som
5a1f0 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66 e views have def
5a200 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 ined column name
5a210 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f s */.#define DB_
5a220 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20 Empty
5a230 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 0x0004 /* The f
5a240 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 ile is empty (le
5a250 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f ngth 0 bytes) */
5a260 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 ../*.** The numb
5a270 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 er of different
5a280 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 kinds of things
5a290 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 that can be limi
5a2a0 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ted.** using the
5a2b0 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 sqlite3_limit()
5a2c0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
5a2d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f define SQLITE_N_
5a2e0 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 LIMIT (SQLITE_LI
5a2f0 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 MIT_TRIGGER_DEPT
5a300 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b H+1)../*.** Look
5a310 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 aside malloc is
5a320 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 a set of fixed-s
5a330 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 ize buffers that
5a340 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 can be used.**
5a350 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c to satisfy small
5a360 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 transient memor
5a370 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
5a380 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 uests for object
5a390 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 s.** associated
5a3a0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
5a3b0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
5a3c0 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 ction. The use
5a3d0 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 of.** lookaside
5a3e0 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 malloc provides
5a3f0 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65 a significant pe
5a400 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 rformance enhanc
5a410 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 ement.** (approx
5a420 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e 10%) by avoidin
5a430 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f g numerous mallo
5a440 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20 c/free requests
5a450 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a while parsing.**
5a460 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e SQL statements.
5a470 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 .**.** The Looka
5a480 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68 side structure h
5a490 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69 olds configurati
5a4a0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 on information a
5a4b0 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b bout the.** look
5a4c0 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 aside malloc sub
5a4d0 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76 system. Each av
5a4e0 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 ailable memory a
5a4f0 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 llocation in.**
5a500 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 the lookaside su
5a510 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65 bsystem is store
5a520 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d on a linked li
5a530 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 st of LookasideS
5a540 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a lot.** objects..
5a550 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 **.** Lookaside
5a560 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 allocations are
5a570 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 only allowed for
5a580 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 objects that ar
5a590 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 e associated.**
5a5a0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 with a particula
5a5b0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
5a5c0 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 ction. Hence, s
5a5d0 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f chema informatio
5a5e0 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 n cannot.** be s
5a5f0 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 tored in lookasi
5a600 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68 de because in sh
5a610 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20 ared cache mode
5a620 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 the schema infor
5a630 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 mation.** is sha
5a640 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 red by multiple
5a650 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
5a660 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65 ions. Therefore
5a670 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a , while parsing.
5a680 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d ** schema inform
5a690 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 ation, the Looka
5a6a0 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c side.bEnabled fl
5a6b0 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f ag is cleared so
5a6c0 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 that.** lookasi
5a6d0 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 de allocations a
5a6e0 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 re not used to c
5a6f0 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68 onstruct the sch
5a700 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a ema objects..*/.
5a710 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 struct Lookaside
5a720 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20 {. u16 sz;
5a730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5a740 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66 Size of each buf
5a750 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
5a760 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 u8 bEnabled;
5a770 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c /* Fal
5a780 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 se to disable ne
5a790 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f w lookaside allo
5a7a0 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 cations */. u8
5a7b0 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 bMalloced;
5a7c0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
5a7d0 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20 pStart obtained
5a7e0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
5a7f0 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e loc() */. int n
5a800 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
5a810 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5a820 62 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c buffers currentl
5a830 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f y checked out */
5a840 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 . int mxOut;
5a850 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 /* Hi
5a860 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 ghwater mark for
5a870 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 nOut */. Looka
5a880 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b sideSlot *pFree;
5a890 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 /* List of av
5a8a0 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20 ailable buffers
5a8b0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72 */. void *pStar
5a8c0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
5a8d0 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76 First byte of av
5a8e0 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 ailable memory s
5a8f0 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a pace */. void *
5a900 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 pEnd;
5a910 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
5a920 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61 69 past end of avai
5a930 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d lable space */.}
5a940 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 ;.struct Lookasi
5a950 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 deSlot {. Looka
5a960 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b sideSlot *pNext;
5a970 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 /* Next buff
5a980 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 6f er in the list o
5a990 66 20 66 72 65 65 20 62 75 66 66 65 72 73 20 2a f free buffers *
5a9a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 /.};../*.** A ha
5a9b0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e sh table for fun
5a9c0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e ction definition
5a9d0 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 s..**.** Hash ea
5a9e0 63 68 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 ch FuncDef struc
5a9f0 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 ture into one of
5aa00 20 74 68 65 20 46 75 6e 63 44 65 66 48 61 73 68 the FuncDefHash
5aa10 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 .a[] slots..** C
5aa20 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e ollisions are on
5aa30 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70 48 61 the FuncDef.pHa
5aa40 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 sh chain..*/.str
5aa50 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20 uct FuncDefHash
5aa60 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 {. FuncDef *a[2
5aa70 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73 3]; /* Has
5aa80 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 h table for func
5aa90 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a tions */.};../*.
5aaa0 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 ** Each database
5aab0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
5aac0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
5aad0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
5aae0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 * The sqlite.las
5aaf0 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74 tRowid records t
5ab00 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 he last insert r
5ab10 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62 owid generated b
5ab20 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 y an.** insert s
5ab30 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 tatement. Inser
5ab40 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e ts on views do n
5ab50 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61 ot affect its va
5ab60 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 lue. Each.** tr
5ab70 69 67 67 65 72 20 68 61 73 20 69 74 73 20 6f 77 igger has its ow
5ab80 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 n context, so th
5ab90 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61 6e at lastRowid can
5aba0 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 73 69 be updated insi
5abb0 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61 de.** triggers a
5abc0 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70 72 s usual. The pr
5abd0 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69 6c evious value wil
5abe0 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f 6e l be restored on
5abf0 63 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a ce the trigger.*
5ac00 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 * exits. Upon e
5ac10 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72 65 ntering a before
5ac20 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20 74 or instead of t
5ac30 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 rigger, lastRowi
5ac40 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 d is no.** longe
5ac50 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 20 76 r (since after v
5ac60 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 ersion 2.8.12) r
5ac70 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a eset to -1..**.*
5ac80 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 * The sqlite.nCh
5ac90 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f ange does not co
5aca0 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74 68 unt changes with
5acb0 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 in triggers and
5acc0 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 keeps no.** cont
5acd0 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 73 65 ext. It is rese
5ace0 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 73 71 t at start of sq
5acf0 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 lite3_exec..** T
5ad00 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e he sqlite.lsChan
5ad10 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 ge represents th
5ad20 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e e number of chan
5ad30 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 ges made by the
5ad40 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 last.** insert,
5ad50 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 update, or delet
5ad60 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 e statement. It
5ad70 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e remains constan
5ad80 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 t throughout the
5ad90 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 .** length of a
5ada0 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 statement and is
5adb0 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 62 79 then updated by
5adc0 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 OP_SetCounts.
5add0 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f It keeps a.** co
5ade0 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73 74 ntext stack just
5adf0 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 like lastRowid
5ae00 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e so that the coun
5ae10 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 t of changes.**
5ae20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 within a trigger
5ae30 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 is not seen out
5ae40 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72 side the trigger
5ae50 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76 69 . Changes to vi
5ae60 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 ews do not.** af
5ae70 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f fect the value o
5ae80 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 f lsChange..** T
5ae90 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e he sqlite.csChan
5aea0 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f ge keeps track o
5aeb0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
5aec0 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20 current changes
5aed0 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 (since.** the la
5aee0 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e st statement) an
5aef0 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64 d is used to upd
5af00 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 ate sqlite_lsCha
5af10 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d nge..**.** The m
5af20 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20 ember variables
5af30 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 sqlite.errCode,
5af40 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 sqlite.zErrMsg a
5af50 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 nd sqlite.zErrMs
5af60 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 g16.** store the
5af70 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 most recent err
5af80 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 or code and, if
5af90 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 applicable, stri
5afa0 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 ng. The.** inter
5afb0 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c nal function sql
5afc0 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 20 75 ite3Error() is u
5afd0 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 73 65 sed to set these
5afe0 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f variables.** co
5aff0 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 nsistently..*/.s
5b000 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b 0a truct sqlite3 {.
5b010 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
5b020 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 Vfs;
5b030 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 /* OS Interface
5b040 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 */. int nDb;
5b050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b060 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5b070 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 backends current
5b080 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 ly in use */. D
5b090 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20 20 b *aDb;
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b0b0 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a All backends */.
5b0c0 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b0e0 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 /* Miscellaneous
5b0f0 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f flags. See belo
5b100 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 w */. int openF
5b110 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
5b120 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 /* Flags pa
5b130 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f ssed to sqlite3_
5b140 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 vfs.xOpen() */.
5b150 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 int errCode;
5b160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b170 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
5b180 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45 ror code (SQLITE
5b190 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 _*) */. int err
5b1a0 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Mask;
5b1b0 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75 /* & resu
5b1c0 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68 lt codes with th
5b1d0 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e is before return
5b1e0 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f ing */. u8 auto
5b1f0 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20 Commit;
5b200 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75 /* The au
5b210 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 to-commit flag.
5b220 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f */. u8 temp_sto
5b230 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 re;
5b240 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a /* 1: file 2:
5b250 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 memory 0: defau
5b260 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f lt */. u8 mallo
5b270 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20 cFailed;
5b280 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
5b290 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20 we have seen a
5b2a0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a malloc failure *
5b2b0 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d /. u8 dfltLockM
5b2c0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ode;
5b2d0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 /* Default loc
5b2e0 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 king-mode for at
5b2f0 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 tached dbs */.
5b300 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f u8 dfltJournalMo
5b310 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a de; /*
5b320 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c Default journal
5b330 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 mode for attach
5b340 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e ed dbs */. sign
5b350 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f ed char nextAuto
5b360 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 vac; /* Aut
5b370 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 66 74 ovac setting aft
5b380 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 er VACUUM if >=0
5b390 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 50 61 */. int nextPa
5b3a0 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 gesize;
5b3b0 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a 65 20 /* Pagesize
5b3c0 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 after VACUUM if
5b3d0 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 >0 */. int nTab
5b3e0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
5b3f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
5b400 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 of tables in the
5b410 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 43 database */. C
5b420 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c ollSeq *pDfltCol
5b430 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l; /*
5b440 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c The default coll
5b450 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28 ating sequence (
5b460 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 36 34 BINARY) */. i64
5b470 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 lastRowid;
5b480 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f /* RO
5b490 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 63 65 WID of most rece
5b4a0 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 20 61 nt insert (see a
5b4b0 62 6f 76 65 29 20 2a 2f 0a 20 20 75 33 32 20 6d bove) */. u32 m
5b4c0 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 agic;
5b4d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 /* Magi
5b4e0 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 65 74 c number for det
5b4f0 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69 73 75 ect library misu
5b500 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 se */. int nCha
5b510 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 nge;
5b520 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 /* Value r
5b530 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
5b540 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a e3_changes() */.
5b550 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e int nTotalChan
5b560 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
5b570 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 /* Value returne
5b580 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 d by sqlite3_tot
5b590 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a al_changes() */.
5b5a0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
5b5b0 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 *mutex;
5b5c0 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 /* Connection mu
5b5d0 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 tex */. int aLi
5b5e0 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d mit[SQLITE_N_LIM
5b5f0 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 IT]; /* Limits
5b600 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c */. struct sql
5b610 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 ite3InitInfo {
5b620 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 /* Informati
5b630 6f 6e 20 75 73 65 64 20 64 75 72 69 6e 67 20 69 on used during i
5b640 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f nitialization */
5b650 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 20 . int iDb;
5b660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b670 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 69 73 /* When back is
5b680 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a being initializ
5b690 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 ed */. int ne
5b6a0 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 wTnum;
5b6b0 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 /* Rootpag
5b6c0 65 20 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 e of table being
5b6d0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
5b6e0 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20 u8 busy;
5b6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b700 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65 /* TRUE if curre
5b710 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e ntly initializin
5b720 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 70 68 g */. u8 orph
5b730 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 anTrigger;
5b740 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61 /* Last sta
5b750 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e tement is orphan
5b760 65 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20 ed TEMP trigger
5b770 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 */. } init;. i
5b780 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20 nt nExtension;
5b790 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b7a0 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 65 64 Number of loaded
5b7b0 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 extensions */.
5b7c0 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e 73 69 void **aExtensi
5b7d0 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f on; /
5b7e0 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61 72 65 * Array of share
5b7f0 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65 d library handle
5b800 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 56 64 s */. struct Vd
5b810 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 be *pVdbe;
5b820 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
5b830 61 63 74 69 76 65 20 76 69 72 74 75 61 6c 20 6d active virtual m
5b840 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 achines */. int
5b850 20 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 20 activeVdbeCnt;
5b860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5b870 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20 63 75 mber of VDBEs cu
5b880 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e rrently executin
5b890 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 74 65 g */. int write
5b8a0 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20 VdbeCnt;
5b8b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5b8c0 66 20 61 63 74 69 76 65 20 56 44 42 45 73 20 74 f active VDBEs t
5b8d0 68 61 74 20 61 72 65 20 77 72 69 74 69 6e 67 20 hat are writing
5b8e0 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 61 */. void (*xTra
5b8f0 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 ce)(void*,const
5b900 63 68 61 72 2a 29 3b 20 20 20 20 20 20 20 20 2f char*); /
5b910 2a 20 54 72 61 63 65 20 66 75 6e 63 74 69 6f 6e * Trace function
5b920 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 */. void *pTra
5b930 63 65 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 ceArg;
5b940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b950 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 /* Argument to t
5b960 68 65 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f he trace functio
5b970 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 n */. void (*xP
5b980 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f rofile)(void*,co
5b990 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 nst char*,u64);
5b9a0 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 /* Profiling fu
5b9b0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
5b9c0 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 *pProfileArg;
5b9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b9e0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
5b9f0 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e t to profile fun
5ba00 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
5ba10 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 *pCommitArg;
5ba20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5ba30 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d Argument to xCom
5ba40 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f mitCallback() */
5ba50 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d . int (*xCom
5ba60 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 mitCallback)(voi
5ba70 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b d*); /* Invok
5ba80 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d ed at every comm
5ba90 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 it. */. void *p
5baa0 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 RollbackArg;
5bab0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
5bac0 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 gument to xRollb
5bad0 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f ackCallback() */
5bae0 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f . void (*xRo
5baf0 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 llbackCallback)(
5bb00 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b void*); /* Invok
5bb10 65 64 20 61 74 20 65 76 65 72 79 20 63 6f 6d 6d ed at every comm
5bb20 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 it. */. void *p
5bb30 55 70 64 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 UpdateArg;. voi
5bb40 64 20 28 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 d (*xUpdateCallb
5bb50 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 ack)(void*,int,
5bb60 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 const char*,cons
5bb70 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 t char*,sqlite_i
5bb80 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 nt64);. void(*x
5bb90 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 CollNeeded)(void
5bba0 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 *,sqlite3*,int e
5bbb0 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 TextRep,const ch
5bbc0 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 ar*);. void(*xC
5bbd0 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 ollNeeded16)(voi
5bbe0 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 d*,sqlite3*,int
5bbf0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 eTextRep,const v
5bc00 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 oid*);. void *p
5bc10 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 CollNeededArg;.
5bc20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
5bc30 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 2f pErr; /
5bc40 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
5bc50 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 ror message */.
5bc60 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 char *zErrMsg;
5bc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5bc80 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
5bc90 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 ror message (UTF
5bca0 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 -8 encoded) */.
5bcb0 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 char *zErrMsg16
5bcc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
5bcd0 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 * Most recent er
5bce0 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 54 46 ror message (UTF
5bcf0 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a -16 encoded) */.
5bd00 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f union {. vo
5bd10 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 6e 74 latile int isInt
5bd20 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 72 75 errupted; /* Tru
5bd30 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 e if sqlite3_int
5bd40 65 72 72 75 70 74 20 68 61 73 20 62 65 65 6e 20 errupt has been
5bd50 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f called */. do
5bd60 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 uble notUsed1;
5bd70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 /* Spa
5bd80 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20 cer */. } u1;.
5bd90 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61 Lookaside looka
5bda0 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f side; /
5bdb0 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c * Lookaside mall
5bdc0 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e oc configuration
5bdd0 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
5bde0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
5bdf0 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41 ATION. int (*xA
5be00 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 uth)(void*,int,c
5be10 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
5be20 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 char*,const cha
5be30 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b r*,const char*);
5be40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
5be50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5be60 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74 68 6f /* Access autho
5be70 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f rization functio
5be80 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 n */. void *pAu
5be90 74 68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 thArg;
5bea0 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75 /* 1st argu
5beb0 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63 63 65 ment to the acce
5bec0 73 73 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e ss auth function
5bed0 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 */.#endif.#ifnd
5bee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 ef SQLITE_OMIT_P
5bef0 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b ROGRESS_CALLBACK
5bf00 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 . int (*xProgre
5bf10 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 ss)(void *);
5bf20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 /* The progress
5bf30 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 callback */. v
5bf40 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 41 72 oid *pProgressAr
5bf50 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g; /*
5bf60 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 Argument to the
5bf70 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
5bf80 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67 k */. int nProg
5bf90 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 20 20 ressOps;
5bfa0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5bfb0 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 70 72 f opcodes for pr
5bfc0 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
5bfd0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 */.#endif.#ifnde
5bfe0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
5bff0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 61 73 RTUALTABLE. Has
5c000 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 h aModule;
5c010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 6f /* po
5c020 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c 69 74 pulated by sqlit
5c030 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
5c040 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 () */. Table *p
5c050 56 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 VTab;
5c060 20 20 20 20 20 20 2f 2a 20 76 74 61 62 20 77 69 /* vtab wi
5c070 74 68 20 61 63 74 69 76 65 20 43 6f 6e 6e 65 63 th active Connec
5c080 74 2f 43 72 65 61 74 65 20 6d 65 74 68 6f 64 20 t/Create method
5c090 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 */. VTable **aV
5c0a0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 Trans;
5c0b0 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 /* Virtual ta
5c0c0 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e 20 74 bles with open t
5c0d0 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 ransactions */.
5c0e0 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 int nVTrans;
5c0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5c100 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 * Allocated size
5c110 20 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 of aVTrans */.
5c120 20 56 54 61 62 6c 65 20 2a 70 44 69 73 63 6f 6e VTable *pDiscon
5c130 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 69 73 63 nect; /* Disc
5c140 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 69 6e 20 onnect these in
5c150 6e 65 78 74 20 73 71 6c 69 74 65 33 5f 70 72 65 next sqlite3_pre
5c160 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64 69 66 pare() */.#endif
5c170 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 . FuncDefHash a
5c180 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Func;
5c190 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f /* Hash table o
5c1a0 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e f connection fun
5c1b0 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 ctions */. Hash
5c1c0 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 aCollSeq;
5c1d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c /* All
5c1e0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5c1f0 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 nces */. BusyHa
5c200 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 ndler busyHandle
5c210 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 r; /* Busy
5c220 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e callback */. in
5c230 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 t busyTimeout;
5c240 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
5c250 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 usy handler time
5c260 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a out, in msec */.
5c270 20 20 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 Db aDbStatic[2
5c280 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ];
5c290 2f 2a 20 53 74 61 74 69 63 20 73 70 61 63 65 20 /* Static space
5c2a0 66 6f 72 20 74 68 65 20 32 20 64 65 66 61 75 6c for the 2 defaul
5c2b0 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 t backends */.
5c2c0 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 Savepoint *pSave
5c2d0 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a point; /*
5c2e0 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 List of active
5c2f0 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 savepoints */.
5c300 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 int nSavepoint;
5c310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5c320 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 Number of non-t
5c330 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 ransaction savep
5c340 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e oints */. int n
5c350 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 Statement;
5c360 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5c370 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74 61 er of nested sta
5c380 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 tement-transacti
5c390 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 73 54 ons */. u8 isT
5c3a0 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f ransactionSavepo
5c3b0 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 int; /* True
5c3c0 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 if the outermost
5c3d0 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 20 savepoint is a
5c3e0 54 53 20 2a 2f 0a 20 20 69 36 34 20 6e 44 65 66 TS */. i64 nDef
5c3f0 65 72 72 65 64 43 6f 6e 73 3b 20 20 20 20 20 20 erredCons;
5c400 20 20 20 20 20 20 2f 2a 20 4e 65 74 20 64 65 66 /* Net def
5c410 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 erred constraint
5c420 73 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 s this transacti
5c430 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 on. */..#ifdef S
5c440 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c QLITE_ENABLE_UNL
5c450 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f 2a 20 OCK_NOTIFY. /*
5c460 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 The following va
5c470 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c 6c 20 riables are all
5c480 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 protected by the
5c490 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 0a STATIC_MASTER .
5c4a0 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f 74 20 ** mutex, not
5c4b0 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 by sqlite3.mutex
5c4c0 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 64 20 . They are used
5c4d0 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 by code in notif
5c4e0 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 y.c. . **. **
5c4f0 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f When X.pUnlockCo
5c500 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 nnection==Y, tha
5c510 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58 20 69 t means that X i
5c520 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20 59 20 s waiting for Y
5c530 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73 to. ** unlock s
5c540 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 70 72 o that it can pr
5c550 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a oceed.. **. **
5c560 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e When X.pBlockin
5c570 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 gConnection==Y,
5c580 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
5c590 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 58 something that X
5c5a0 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72 69 65 tried. ** trie
5c5b0 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74 6c 79 d to do recently
5c5c0 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20 failed with an
5c5d0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 SQLITE_LOCKED er
5c5e0 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63 6b 73 ror due to locks
5c5f0 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e . ** held by Y.
5c600 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 . */. sqlite3
5c610 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 *pBlockingConnec
5c620 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 tion; /* Connect
5c630 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64 20 ion that caused
5c640 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f SQLITE_LOCKED */
5c650 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c . sqlite3 *pUnl
5c660 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20 ockConnection;
5c670 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e /* Conn
5c680 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20 ection to watch
5c690 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 for unlock */.
5c6a0 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72 67 void *pUnlockArg
5c6b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5c6c0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
5c6d0 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 t to xUnlockNoti
5c6e0 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 fy */. void (*x
5c6f0 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f UnlockNotify)(vo
5c700 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a id **, int); /*
5c710 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 20 63 Unlock notify c
5c720 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c allback */. sql
5c730 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f 63 6b ite3 *pNextBlock
5c740 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 ed; /* Ne
5c750 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c xt in list of al
5c760 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 l blocked connec
5c770 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a tions */.#endif.
5c780 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 };../*.** A macr
5c790 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 o to discover th
5c7a0 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 e encoding of a
5c7b0 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 database..*/.#de
5c7c0 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 28 64 fine ENC(db) ((d
5c7d0 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 b)->aDb[0].pSche
5c7e0 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 ma->enc)../*.**
5c7f0 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 Possible values
5c800 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 66 for the sqlite.f
5c810 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 2e 66 lags and or Db.f
5c820 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a lags fields..**.
5c830 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 6c 61 ** On sqlite.fla
5c840 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 49 gs, the SQLITE_I
5c850 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d 65 61 nTrans value mea
5c860 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 65 0a ns that we have.
5c870 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 42 45 ** executed a BE
5c880 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c 61 67 GIN. On Db.flag
5c890 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e s, SQLITE_InTran
5c8a0 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 65 6d s means a statem
5c8b0 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ent.** transacti
5c8c0 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f 6e 20 on is active on
5c8d0 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 72 20 that particular
5c8e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
5c8f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5c900 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 20 _VdbeTrace
5c910 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 54 0x00000001 /* T
5c920 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 44 42 rue to trace VDB
5c930 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 E execution */.#
5c940 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e define SQLITE_In
5c950 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 78 30 Trans 0x0
5c960 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 75 65 0000008 /* True
5c970 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 61 63 if in a transac
5c980 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tion */.#define
5c990 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 SQLITE_InternCha
5c9a0 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 31 30 nges 0x00000010
5c9b0 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64 /* Uncommitted
5c9c0 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e Hash table chan
5c9d0 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ges */.#define S
5c9e0 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d QLITE_FullColNam
5c9f0 65 73 20 20 20 30 78 30 30 30 30 30 30 32 30 20 es 0x00000020
5ca00 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f /* Show full co
5ca10 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45 lumn names on SE
5ca20 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LECT */.#define
5ca30 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e SQLITE_ShortColN
5ca40 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 34 30 ames 0x00000040
5ca50 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 20 /* Show short
5ca60 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f columns names */
5ca70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5ca80 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 20 30 CountRows 0
5ca90 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 43 6f x00000080 /* Co
5caa0 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 65 64 unt rows changed
5cab0 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 by INSERT, */.
5cac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cae0 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 44 45 /* DE
5caf0 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 LETE, or UPDATE
5cb00 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 and return */.
5cb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb30 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 68 65 /* the
5cb40 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 63 count using a c
5cb50 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 allback. */.#def
5cb60 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 ine SQLITE_NullC
5cb70 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 30 30 allback 0x0000
5cb80 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 0100 /* Invoke
5cb90 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 the callback onc
5cba0 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 20 e if the */.
5cbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cbd0 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 75 6c /* resul
5cbe0 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 20 2a t set is empty *
5cbf0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5cc00 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 20 _SqlTrace
5cc10 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 44 0x00000200 /* D
5cc20 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c 20 61 ebug print SQL a
5cc30 73 20 69 74 20 65 78 65 63 75 74 65 73 20 2a 2f s it executes */
5cc40 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cc50 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 20 30 VdbeListing 0
5cc60 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 44 65 x00000400 /* De
5cc70 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 20 bug listings of
5cc80 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 2a 2f VDBE programs */
5cc90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cca0 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 20 30 WriteSchema 0
5ccb0 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 4f 4b x00000800 /* OK
5ccc0 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c 49 54 to update SQLIT
5ccd0 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 65 66 E_MASTER */.#def
5cce0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 ine SQLITE_NoRea
5ccf0 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 30 30 dlock 0x0000
5cd00 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 6f 63 1000 /* Readloc
5cd10 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 ks are omitted w
5cd20 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 hen .
5cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
5cd50 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 61 64 * accessing read
5cd60 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 20 -only databases
5cd70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5cd80 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 E_IgnoreChecks
5cd90 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 0x00002000 /*
5cda0 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63 Do not enforce c
5cdb0 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 heck constraints
5cdc0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5cdd0 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
5cde0 65 64 20 30 78 30 30 30 30 34 30 30 30 20 2f 2a ed 0x00004000 /*
5cdf0 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 For shared-cach
5ce00 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e e mode */.#defin
5ce10 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 e SQLITE_LegacyF
5ce20 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 38 30 ileFmt 0x000080
5ce30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 00 /* Create ne
5ce40 77 20 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 w databases in f
5ce50 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69 ormat 1 */.#defi
5ce60 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 ne SQLITE_FullFS
5ce70 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 31 30 ync 0x00010
5ce80 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 000 /* Use full
5ce90 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 fsync on the ba
5cea0 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ckend */.#define
5ceb0 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 SQLITE_LoadExte
5cec0 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 30 30 nsion 0x0002000
5ced0 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 0 /* Enable loa
5cee0 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a d_extension */..
5cef0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
5cf00 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 30 78 ecoveryMode 0x
5cf10 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 67 6e 00040000 /* Ign
5cf20 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 6f 72 ore schema error
5cf30 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
5cf40 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 ITE_ReverseOrder
5cf50 20 20 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 0x00100000 /
5cf60 2a 20 52 65 76 65 72 73 65 20 75 6e 6f 72 64 65 * Reverse unorde
5cf70 72 65 64 20 53 45 4c 45 43 54 73 20 2a 2f 0a 23 red SELECTs */.#
5cf80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 define SQLITE_Re
5cf90 63 54 72 69 67 67 65 72 73 20 20 20 20 30 78 30 cTriggers 0x0
5cfa0 30 32 30 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 0200000 /* Enab
5cfb0 6c 65 20 72 65 63 75 72 73 69 76 65 20 74 72 69 le recursive tri
5cfc0 67 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 ggers */.#define
5cfd0 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b SQLITE_ForeignK
5cfe0 65 79 73 20 20 20 20 30 78 30 30 34 30 30 30 30 eys 0x0040000
5cff0 30 20 20 2f 2a 20 45 6e 66 6f 72 63 65 20 66 6f 0 /* Enforce fo
5d000 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 reign key constr
5d010 61 69 6e 74 73 20 20 2a 2f 0a 0a 2f 2a 0a 2a 2a aints */../*.**
5d020 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 Possible values
5d030 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e for the sqlite.
5d040 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a 2a 20 magic field..**
5d050 54 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 The numbers are
5d060 6f 62 74 61 69 6e 65 64 20 61 74 20 72 61 6e 64 obtained at rand
5d070 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f 20 73 om and have no s
5d080 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 2c 20 pecial meaning,
5d090 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 62 65 other.** than be
5d0a0 69 6e 67 20 64 69 73 74 69 6e 63 74 20 66 72 6f ing distinct fro
5d0b0 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e 0a 2a m one another..*
5d0c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5d0d0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 20 20 _MAGIC_OPEN
5d0e0 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a 20 44 0xa029a697 /* D
5d0f0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 atabase is open
5d100 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5d110 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 20 20 E_MAGIC_CLOSED
5d120 20 30 78 39 66 33 63 32 64 33 33 20 20 2f 2a 20 0x9f3c2d33 /*
5d130 44 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 Database is clos
5d140 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ed */.#define SQ
5d150 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 LITE_MAGIC_SICK
5d160 20 20 20 20 30 78 34 62 37 37 31 32 39 30 20 20 0x4b771290
5d170 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 77 61 /* Error and awa
5d180 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f 0a 23 iting close */.#
5d190 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
5d1a0 47 49 43 5f 42 55 53 59 20 20 20 20 20 30 78 66 GIC_BUSY 0xf
5d1b0 30 33 62 37 39 30 36 20 20 2f 2a 20 44 61 74 61 03b7906 /* Data
5d1c0 62 61 73 65 20 63 75 72 72 65 6e 74 6c 79 20 69 base currently i
5d1d0 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 n use */.#define
5d1e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 SQLITE_MAGIC_ER
5d1f0 52 4f 52 20 20 20 20 30 78 62 35 33 35 37 39 33 ROR 0xb535793
5d200 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 0 /* An SQLITE_
5d210 4d 49 53 55 53 45 20 65 72 72 6f 72 20 6f 63 63 MISUSE error occ
5d220 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 urred */../*.**
5d230 45 61 63 68 20 53 51 4c 20 66 75 6e 63 74 69 6f Each SQL functio
5d240 6e 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 n is defined by
5d250 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
5d260 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 he following.**
5d270 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 70 6f structure. A po
5d280 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 inter to this st
5d290 72 75 63 74 75 72 65 20 69 73 20 73 74 6f 72 65 ructure is store
5d2a0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e d in the sqlite.
5d2b0 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 74 61 aFunc.** hash ta
5d2c0 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c 74 69 ble. When multi
5d2d0 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 ple functions ha
5d2e0 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 ve the same name
5d2f0 2c 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 , the hash table
5d300 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 .** points to a
5d310 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 linked list of t
5d320 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e hese structures.
5d330 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 .*/.struct FuncD
5d340 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 67 3b ef {. i16 nArg;
5d350 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5d360 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
5d370 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 75 6e ts. -1 means un
5d380 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 38 20 limited */. u8
5d390 69 50 72 65 66 45 6e 63 3b 20 20 20 20 20 20 20 iPrefEnc;
5d3a0 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 /* Preferred t
5d3b0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 53 51 ext encoding (SQ
5d3c0 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 4c 45 2c LITE_UTF8, 16LE,
5d3d0 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 20 66 16BE) */. u8 f
5d3e0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
5d3f0 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 /* Some combina
5d400 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 5f 46 tion of SQLITE_F
5d410 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 64 20 UNC_* */. void
5d420 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 20 20 *pUserData;
5d430 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 61 72 /* User data par
5d440 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 6e 63 ameter */. Func
5d450 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 Def *pNext;
5d460 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 69 6f /* Next functio
5d470 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61 6d 65 n with same name
5d480 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 */. void (*xFu
5d490 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 nc)(sqlite3_cont
5d4a0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
5d4b0 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 52 65 _value**); /* Re
5d4c0 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 2a gular function *
5d4d0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 /. void (*xStep
5d4e0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
5d4f0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
5d500 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 67 72 alue**); /* Aggr
5d510 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a 20 20 egate step */.
5d520 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 7a 65 void (*xFinalize
5d530 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
5d540 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 t*);
5d550 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 /* Aggregate
5d560 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a 20 20 finalizer */.
5d570 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5d580 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 6d 65 /* SQL name
5d590 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
5d5a0 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a . */. FuncDef *
5d5b0 70 48 61 73 68 3b 20 20 20 20 20 20 2f 2a 20 4e pHash; /* N
5d5c0 65 78 74 20 77 69 74 68 20 61 20 64 69 66 66 65 ext with a diffe
5d5d0 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 74 68 rent name but th
5d5e0 65 20 73 61 6d 65 20 68 61 73 68 20 2a 2f 0a 7d e same hash */.}
5d5f0 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c ;../*.** Possibl
5d600 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e e values for Fun
5d610 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 cDef.flags.*/.#d
5d620 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e efine SQLITE_FUN
5d630 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 31 20 C_LIKE 0x01
5d640 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 66 6f 72 /* Candidate for
5d650 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 the LIKE optimi
5d660 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e zation */.#defin
5d670 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 e SQLITE_FUNC_CA
5d680 53 45 20 20 20 20 20 30 78 30 32 20 2f 2a 20 43 SE 0x02 /* C
5d690 61 73 65 2d 73 65 6e 73 69 74 69 76 65 20 4c 49 ase-sensitive LI
5d6a0 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 69 6f 6e KE-type function
5d6b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5d6c0 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 20 20 TE_FUNC_EPHEM
5d6d0 20 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 65 72 0x04 /* Ephemer
5d6e0 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 74 68 al. Delete with
5d6f0 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 6e 65 VDBE */.#define
5d700 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
5d710 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 73 71 DCOLL 0x08 /* sq
5d720 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c lite3GetFuncColl
5d730 53 65 71 28 29 20 6d 69 67 68 74 20 62 65 20 63 Seq() might be c
5d740 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 alled */.#define
5d750 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 52 49 SQLITE_FUNC_PRI
5d760 56 41 54 45 20 20 30 78 31 30 20 2f 2a 20 41 6c VATE 0x10 /* Al
5d770 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 72 6e lowed for intern
5d780 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 al use only */.#
5d790 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d7a0 4e 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 32 30 NC_COUNT 0x20
5d7b0 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 /* Built-in cou
5d7c0 6e 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 nt(*) aggregate
5d7d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5d7e0 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 20 E_FUNC_COALESCE
5d7f0 30 78 34 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 0x40 /* Built-in
5d800 20 63 6f 61 6c 65 73 63 65 28 29 20 6f 72 20 69 coalesce() or i
5d810 66 6e 75 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e fnull() function
5d820 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 */../*.** The f
5d830 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 6d ollowing three m
5d840 61 63 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e 28 acros, FUNCTION(
5d850 29 2c 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 6e ), LIKEFUNC() an
5d860 64 20 41 47 47 52 45 47 41 54 45 28 29 20 61 72 d AGGREGATE() ar
5d870 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 65 e.** used to cre
5d880 61 74 65 20 74 68 65 20 69 6e 69 74 69 61 6c 69 ate the initiali
5d890 7a 65 72 73 20 66 6f 72 20 74 68 65 20 46 75 6e zers for the Fun
5d8a0 63 44 65 66 20 73 74 72 75 63 74 75 72 65 73 2e cDef structures.
5d8b0 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 4f .**.** FUNCTIO
5d8c0 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 N(zName, nArg, i
5d8d0 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 Arg, bNC, xFunc)
5d8e0 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 .** Used to
5d8f0 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 create a scalar
5d900 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 function definit
5d910 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ion of a functio
5d920 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 n zName .**
5d930 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 43 implemented by C
5d940 20 66 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 function xFunc
5d950 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 that accepts nAr
5d960 67 20 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 g arguments. The
5d970 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65 20 70 61 .** value pa
5d980 73 73 65 64 20 61 73 20 69 41 72 67 20 69 73 20 ssed as iArg is
5d990 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 2a cast to a (void*
5d9a0 29 20 61 6e 64 20 6d 61 64 65 20 61 76 61 69 6c ) and made avail
5d9b0 61 62 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 74 able.** as t
5d9c0 68 65 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 he user-data (sq
5d9d0 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 lite3_user_data(
5d9e0 29 29 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 )) for the funct
5d9f0 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 20 ion. If .**
5da00 61 72 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 20 argument bNC is
5da10 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 53 true, then the S
5da20 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 QLITE_FUNC_NEEDC
5da30 4f 4c 4c 20 66 6c 61 67 20 69 73 20 73 65 74 2e OLL flag is set.
5da40 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52 45 47 41 .**.** AGGREGA
5da50 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 TE(zName, nArg,
5da60 69 41 72 67 2c 20 62 4e 43 2c 20 78 53 74 65 70 iArg, bNC, xStep
5da70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 20 , xFinal).**
5da80 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 Used to create
5da90 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e an aggregate fun
5daa0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e ction definition
5dab0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 0a implemented by.
5dac0 2a 2a 20 20 20 20 20 74 68 65 20 43 20 66 75 6e ** the C fun
5dad0 63 74 69 6f 6e 73 20 78 53 74 65 70 20 61 6e 64 ctions xStep and
5dae0 20 78 46 69 6e 61 6c 2e 20 54 68 65 20 66 69 72 xFinal. The fir
5daf0 73 74 20 66 6f 75 72 20 70 61 72 61 6d 65 74 65 st four paramete
5db00 72 73 0a 2a 2a 20 20 20 20 20 61 72 65 20 69 6e rs.** are in
5db10 74 65 72 70 72 65 74 65 64 20 69 6e 20 74 68 65 terpreted in the
5db20 20 73 61 6d 65 20 77 61 79 20 61 73 20 74 68 65 same way as the
5db30 20 66 69 72 73 74 20 34 20 70 61 72 61 6d 65 74 first 4 paramet
5db40 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 20 46 55 ers to.** FU
5db50 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a 20 NCTION()..**.**
5db60 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 LIKEFUNC(zName
5db70 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 66 6c , nArg, pArg, fl
5db80 61 67 73 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 ags).** Used
5db90 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61 to create a sca
5dba0 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 lar function def
5dbb0 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e inition of a fun
5dbc0 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 ction zName .**
5dbd0 20 20 20 20 74 68 61 74 20 61 63 63 65 70 74 73 that accepts
5dbe0 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 20 nArg arguments
5dbf0 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 and is implement
5dc00 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 ed by a call to
5dc10 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e 63 74 69 C .** functi
5dc20 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 41 72 67 on likeFunc. Arg
5dc30 75 6d 65 6e 74 20 70 41 72 67 20 69 73 20 63 61 ument pArg is ca
5dc40 73 74 20 74 6f 20 61 20 28 76 6f 69 64 20 2a 29 st to a (void *)
5dc50 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20 20 20 20 and made.**
5dc60 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 74 68 available as th
5dc70 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 72 2d e function user-
5dc80 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f 75 73 data (sqlite3_us
5dc90 65 72 5f 64 61 74 61 28 29 29 2e 20 54 68 65 0a er_data()). The.
5dca0 2a 2a 20 20 20 20 20 46 75 6e 63 44 65 66 2e 66 ** FuncDef.f
5dcb0 6c 61 67 73 20 76 61 72 69 61 62 6c 65 20 69 73 lags variable is
5dcc0 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 set to the valu
5dcd0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
5dce0 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20 70 61 72 flags.** par
5dcf0 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 ameter..*/.#defi
5dd00 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d ne FUNCTION(zNam
5dd10 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 e, nArg, iArg, b
5dd20 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b NC, xFunc) \. {
5dd30 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 nArg, SQLITE_UTF
5dd40 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55 8, bNC*SQLITE_FU
5dd50 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 NC_NEEDCOLL, \.
5dd60 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f SQLITE_INT_TO_
5dd70 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 46 PTR(iArg), 0, xF
5dd80 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d unc, 0, 0, #zNam
5dd90 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53 54 e, 0}.#define ST
5dda0 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 R_FUNCTION(zName
5ddb0 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62 4e , nArg, pArg, bN
5ddc0 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b 6e C, xFunc) \. {n
5ddd0 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Arg, SQLITE_UTF8
5dde0 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55 4e , bNC*SQLITE_FUN
5ddf0 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 C_NEEDCOLL, \.
5de00 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63 2c pArg, 0, xFunc,
5de10 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 0, 0, #zName, 0
5de20 7d 0a 23 64 65 66 69 6e 65 20 4c 49 4b 45 46 55 }.#define LIKEFU
5de30 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 NC(zName, nArg,
5de40 61 72 67 2c 20 66 6c 61 67 73 29 20 5c 0a 20 20 arg, flags) \.
5de50 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 {nArg, SQLITE_UT
5de60 46 38 2c 20 66 6c 61 67 73 2c 20 28 76 6f 69 64 F8, flags, (void
5de70 20 2a 29 61 72 67 2c 20 30 2c 20 6c 69 6b 65 46 *)arg, 0, likeF
5de80 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d unc, 0, 0, #zNam
5de90 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 41 47 e, 0}.#define AG
5dea0 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e GREGATE(zName, n
5deb0 41 72 67 2c 20 61 72 67 2c 20 6e 63 2c 20 78 53 Arg, arg, nc, xS
5dec0 74 65 70 2c 20 78 46 69 6e 61 6c 29 20 5c 0a 20 tep, xFinal) \.
5ded0 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 {nArg, SQLITE_U
5dee0 54 46 38 2c 20 6e 63 2a 53 51 4c 49 54 45 5f 46 TF8, nc*SQLITE_F
5def0 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a UNC_NEEDCOLL, \.
5df00 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f SQLITE_INT_TO
5df10 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30 2c _PTR(arg), 0, 0,
5df20 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23 7a xStep,xFinal,#z
5df30 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20 41 Name,0}../*.** A
5df40 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65 70 ll current savep
5df50 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64 oints are stored
5df60 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 in a linked lis
5df70 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a 2a 2a t starting at.**
5df80 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f sqlite3.pSavepo
5df90 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20 65 int. The first e
5dfa0 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c 69 lement in the li
5dfb0 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20 72 st is the most r
5dfc0 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 6e 65 ecently.** opene
5dfd0 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61 76 d savepoint. Sav
5dfe0 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64 65 epoints are adde
5dff0 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62 79 d to the list by
5e000 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f 50 5f the vdbe.** OP_
5e010 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 75 Savepoint instru
5e020 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ction..*/.struct
5e030 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 63 Savepoint {. c
5e040 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
5e050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e060 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e 74 20 /* Savepoint
5e070 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d 69 6e name (nul-termin
5e080 61 74 65 64 29 20 2a 2f 0a 20 20 69 36 34 20 6e ated) */. i64 n
5e090 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20 DeferredCons;
5e0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5e0b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 65 * Number of defe
5e0c0 72 72 65 64 20 66 6b 20 76 69 6f 6c 61 74 69 6f rred fk violatio
5e0d0 6e 73 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e ns */. Savepoin
5e0e0 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 t *pNext;
5e0f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
5e100 61 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 20 arent savepoint
5e110 28 69 66 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a 0a (if any) */.};..
5e120 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
5e130 69 6e 67 20 61 72 65 20 75 73 65 64 20 61 73 20 ing are used as
5e140 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
5e150 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 53 eter to sqlite3S
5e160 61 76 65 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 61 avepoint(),.** a
5e170 6e 64 20 61 73 20 74 68 65 20 50 31 20 61 72 67 nd as the P1 arg
5e180 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4f 50 5f ument to the OP_
5e190 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 75 Savepoint instru
5e1a0 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e ction..*/.#defin
5e1b0 65 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 e SAVEPOINT_BEGI
5e1c0 4e 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 N 0.#define
5e1d0 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
5e1e0 53 45 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 SE 1.#define
5e1f0 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
5e200 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 45 CK 2.../*.** E
5e210 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c ach SQLite modul
5e220 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e (virtual table
5e230 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73 20 definition) is
5e240 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a defined by an.**
5e250 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
5e260 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
5e270 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e 20 ture, stored in
5e280 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64 the sqlite3.aMod
5e290 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c ule.** hash tabl
5e2a0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64 e..*/.struct Mod
5e2b0 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 ule {. const sq
5e2c0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
5e2d0 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 odule; /*
5e2e0 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72 Callback pointer
5e2f0 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 s */. const cha
5e300 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
5e310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5e320 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63 72 ame passed to cr
5e330 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f eate_module() */
5e340 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 . void *pAux;
5e350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e360 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78 20 /* pAux
5e370 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 65 passed to create
5e380 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 _module() */. v
5e390 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 oid (*xDestroy)(
5e3a0 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20 20 void *);
5e3b0 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 /* Module de
5e3c0 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f structor functio
5e3d0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 n */.};../*.** i
5e3e0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
5e3f0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 each column of
5e400 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 an SQL table is
5e410 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 held in an insta
5e420 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 nce.** of this s
5e430 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 tructure..*/.str
5e440 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 63 uct Column {. c
5e450 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
5e460 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 /* Name of this
5e470 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 column */. Expr
5e480 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20 *pDflt; /*
5e490 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 Default value of
5e4a0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a this column */.
5e4b0 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 3b 20 20 char *zDflt;
5e4c0 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74 /* Original t
5e4d0 65 78 74 20 6f 66 20 74 68 65 20 64 65 66 61 75 ext of the defau
5e4e0 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68 lt value */. ch
5e4f0 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 20 2f ar *zType; /
5e500 2a 20 44 61 74 61 20 74 79 70 65 20 66 6f 72 20 * Data type for
5e510 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 this column */.
5e520 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 char *zColl;
5e530 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 /* Collating s
5e540 65 71 75 65 6e 63 65 2e 20 20 49 66 20 4e 55 4c equence. If NUL
5e550 4c 2c 20 75 73 65 20 74 68 65 20 64 65 66 61 75 L, use the defau
5e560 6c 74 20 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 lt */. u8 notNu
5e570 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ll; /* True
5e580 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 4e if there is a N
5e590 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 OT NULL constrai
5e5a0 6e 74 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 69 nt */. u8 isPri
5e5b0 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 54 72 75 65 mKey; /* True
5e5c0 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 if this column
5e5d0 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 is part of the P
5e5e0 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 RIMARY KEY */.
5e5f0 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 char affinity;
5e600 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 /* One of the S
5e610 51 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 QLITE_AFF_... va
5e620 6c 75 65 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 lues */.#ifndef
5e630 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
5e640 55 41 4c 54 41 42 4c 45 0a 20 20 75 38 20 69 73 UALTABLE. u8 is
5e650 48 69 64 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54 Hidden; /* T
5e660 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75 rue if this colu
5e670 6d 6e 20 69 73 20 27 68 69 64 64 65 6e 27 20 2a mn is 'hidden' *
5e680 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a /.#endif.};../*.
5e690 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e 67 20 ** A "Collating
5e6a0 53 65 71 75 65 6e 63 65 22 20 69 73 20 64 65 66 Sequence" is def
5e6b0 69 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 ined by an insta
5e6c0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
5e6d0 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
5e6e0 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c e. Conceptually,
5e6f0 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 a collating seq
5e700 75 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20 6f uence consists o
5e710 66 20 61 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 f a name and.**
5e720 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 6f 75 a comparison rou
5e730 74 69 6e 65 20 74 68 61 74 20 64 65 66 69 6e 65 tine that define
5e740 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 s the order of t
5e750 68 61 74 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a hat sequence..**
5e760 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 74 77 .** There may tw
5e770 6f 20 73 65 70 61 72 61 74 65 20 69 6d 70 6c 65 o separate imple
5e780 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 mentations of th
5e790 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 e collation func
5e7a0 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 tion, one.** tha
5e7b0 74 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 74 t processes text
5e7c0 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 in UTF-8 encodi
5e7d0 6e 67 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 ng (CollSeq.xCmp
5e7e0 29 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 74 68 ) and another th
5e7f0 61 74 0a 2a 2a 20 70 72 6f 63 65 73 73 65 73 20 at.** processes
5e800 74 65 78 74 20 65 6e 63 6f 64 65 64 20 69 6e 20 text encoded in
5e810 55 54 46 2d 31 36 20 28 43 6f 6c 6c 53 65 71 2e UTF-16 (CollSeq.
5e820 78 43 6d 70 31 36 29 2c 20 75 73 69 6e 67 20 74 xCmp16), using t
5e830 68 65 20 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 he machine.** na
5e840 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e tive byte order.
5e850 20 57 68 65 6e 20 61 20 63 6f 6c 6c 61 74 69 6f When a collatio
5e860 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e n sequence is in
5e870 76 6f 6b 65 64 2c 20 53 51 4c 69 74 65 20 73 65 voked, SQLite se
5e880 6c 65 63 74 73 0a 2a 2a 20 74 68 65 20 76 65 72 lects.** the ver
5e890 73 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 72 sion that will r
5e8a0 65 71 75 69 72 65 20 74 68 65 20 6c 65 61 73 74 equire the least
5e8b0 20 65 78 70 65 6e 73 69 76 65 20 65 6e 63 6f 64 expensive encod
5e8c0 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 ing.** translati
5e8d0 6f 6e 73 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a ons, if any..**.
5e8e0 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70 ** The CollSeq.p
5e8f0 55 73 65 72 20 6d 65 6d 62 65 72 20 76 61 72 69 User member vari
5e900 61 62 6c 65 20 69 73 20 61 6e 20 65 78 74 72 61 able is an extra
5e910 20 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 20 parameter that
5e920 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20 61 73 20 passed in.** as
5e930 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
5e940 6e 74 20 74 6f 20 74 68 65 20 55 54 46 2d 38 20 nt to the UTF-8
5e950 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 comparison funct
5e960 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f ion, xCmp..** Co
5e970 6c 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69 73 llSeq.pUser16 is
5e980 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 the equivalent
5e990 66 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20 63 for the UTF-16 c
5e9a0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 omparison functi
5e9b0 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a on,.** xCmp16..*
5e9c0 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43 6f 6c *.** If both Col
5e9d0 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64 20 43 6f lSeq.xCmp and Co
5e9e0 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20 61 72 65 llSeq.xCmp16 are
5e9f0 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 NULL, it means
5ea00 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c that the.** coll
5ea10 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 ating sequence i
5ea20 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 6e s undefined. In
5ea30 64 69 63 65 73 20 62 75 69 6c 74 20 6f 6e 20 61 dices built on a
5ea40 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 63 n undefined.** c
5ea50 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5ea60 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 61 e may not be rea
5ea70 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2f d or written..*/
5ea80 0a 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 .struct CollSeq
5ea90 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
5eaa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
5eab0 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 e of the collati
5eac0 6e 67 20 73 65 71 75 65 6e 63 65 2c 20 55 54 46 ng sequence, UTF
5ead0 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 -8 encoded */.
5eae0 75 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 u8 enc;
5eaf0 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e /* Text en
5eb00 63 6f 64 69 6e 67 20 68 61 6e 64 6c 65 64 20 62 coding handled b
5eb10 79 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 75 38 y xCmp() */. u8
5eb20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 type;
5eb30 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 /* One of th
5eb40 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e e SQLITE_COLL_..
5eb50 2e 20 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a . values below *
5eb60 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 3b /. void *pUser;
5eb70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
5eb80 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 st argument to x
5eb90 43 6d 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 Cmp() */. int (
5eba0 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 *xCmp)(void*,int
5ebb0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
5ebc0 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 nt, const void*)
5ebd0 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 ;. void (*xDel)
5ebe0 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 (void*); /* Des
5ebf0 74 72 75 63 74 6f 72 20 66 6f 72 20 70 55 73 65 tructor for pUse
5ec00 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 r */.};../*.** A
5ec10 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 llowed values of
5ec20 20 43 6f 6c 6c 53 65 71 2e 74 79 70 65 3a 0a 2a CollSeq.type:.*
5ec30 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5ec40 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 20 31 20 _COLL_BINARY 1
5ec50 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 /* The default
5ec60 6d 65 6d 63 6d 70 28 29 20 63 6f 6c 6c 61 74 69 memcmp() collati
5ec70 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 ng sequence */.#
5ec80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
5ec90 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 20 20 2f 2a LL_NOCASE 2 /*
5eca0 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f The built-in NO
5ecb0 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 CASE collating s
5ecc0 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 equence */.#defi
5ecd0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 ne SQLITE_COLL_R
5ece0 45 56 45 52 53 45 20 33 20 20 2f 2a 20 54 68 65 EVERSE 3 /* The
5ecf0 20 62 75 69 6c 74 2d 69 6e 20 52 45 56 45 52 53 built-in REVERS
5ed00 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 E collating sequ
5ed10 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ence */.#define
5ed20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 SQLITE_COLL_USER
5ed30 20 20 20 20 30 20 20 2f 2a 20 41 6e 79 20 6f 74 0 /* Any ot
5ed40 68 65 72 20 75 73 65 72 2d 64 65 66 69 6e 65 64 her user-defined
5ed50 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
5ed60 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 nce */../*.** A
5ed70 73 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 62 sort order can b
5ed80 65 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 20 e either ASC or
5ed90 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 DESC..*/.#define
5eda0 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 SQLITE_SO_ASC
5edb0 20 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 0 /* Sort
5edc0 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 in ascending ord
5edd0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 er */.#define SQ
5ede0 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 LITE_SO_DESC
5edf0 20 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 1 /* Sort in
5ee00 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 ascending order
5ee10 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e */../*.** Column
5ee20 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e affinity types.
5ee30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 .**.** These use
5ee40 64 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e d to have mnemon
5ee50 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 ic name like 'i'
5ee60 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f for SQLITE_AFF_
5ee70 49 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 INTEGER and.** '
5ee80 74 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 t' for SQLITE_AF
5ee90 46 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 20 F_TEXT. But we
5eea0 63 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 6c can save a littl
5eeb0 65 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 72 e space and impr
5eec0 6f 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 ove.** the speed
5eed0 20 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d a little by num
5eee0 62 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 bering the value
5eef0 73 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e s consecutively.
5ef00 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 .**.** But rat
5ef10 68 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 77 her than start w
5ef20 69 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 62 ith 0 or 1, we b
5ef30 65 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 20 egin with 'a'.
5ef40 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 That way,.** whe
5ef50 6e 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 6e n multiple affin
5ef60 69 74 79 20 74 79 70 65 73 20 61 72 65 20 63 6f ity types are co
5ef70 6e 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 ncatenated into
5ef80 61 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 a string and.**
5ef90 75 73 65 64 20 61 73 20 74 68 65 20 50 34 20 6f used as the P4 o
5efa0 70 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 6c perand, they wil
5efb0 6c 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 62 l be more readab
5efc0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 le..**.** Note a
5efd0 6c 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 6d lso that the num
5efe0 65 72 69 63 20 74 79 70 65 73 20 61 72 65 20 67 eric types are g
5eff0 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 20 rouped together
5f000 73 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 0a so that testing.
5f010 2a 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 ** for a numeric
5f020 20 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 6c type is a singl
5f030 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f e comparison..*/
5f040 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f050 41 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 27 AFF_TEXT 'a'
5f060 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f070 41 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 AFF_NONE 'b'
5f080 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f090 41 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 AFF_NUMERIC 'c'
5f0a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f0b0 41 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27 AFF_INTEGER 'd'
5f0c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5f0d0 41 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 27 AFF_REAL 'e'
5f0e0 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ..#define sqlite
5f0f0 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 3IsNumericAffini
5f100 74 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 4c ty(X) ((X)>=SQL
5f110 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 ITE_AFF_NUMERIC)
5f120 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
5f130 54 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 TE_AFF_MASK valu
5f140 65 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 es masks off the
5f150 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 significant bit
5f160 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e s of an.** affin
5f170 69 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 ity value. .*/.#
5f180 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 define SQLITE_AF
5f190 46 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 0a F_MASK 0x67.
5f1a0 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 ./*.** Additiona
5f1b0 6c 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 61 l bit values tha
5f1c0 74 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 69 t can be ORed wi
5f1d0 74 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 77 th an affinity w
5f1e0 69 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 ithout.** changi
5f1f0 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 2e ng the affinity.
5f200 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5f210 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 TE_JUMPIFNULL
5f220 30 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 0x08 /* jumps i
5f230 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 f either operand
5f240 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 is NULL */.#def
5f250 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 ine SQLITE_STORE
5f260 50 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a P2 0x10 /*
5f270 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e Store result in
5f280 20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20 reg[P2] rather
5f290 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 23 64 65 than jump */.#de
5f2a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c fine SQLITE_NULL
5f2b0 45 51 20 20 20 20 20 20 20 30 78 38 30 20 20 2f EQ 0x80 /
5f2c0 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0a 0a * NULL=NULL */..
5f2d0 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 20 /*.** An object
5f2e0 6f 66 20 74 68 69 73 20 74 79 70 65 20 69 73 20 of this type is
5f2f0 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 68 created for each
5f300 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 virtual table p
5f310 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 resent in.** the
5f320 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
5f330 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
5f340 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
5f350 69 73 20 73 68 61 72 65 64 2c 20 74 68 65 6e 20 is shared, then
5f360 74 68 65 72 65 20 69 73 20 6f 6e 65 20 69 6e 73 there is one ins
5f370 74 61 6e 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a tance of this.**
5f380 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65 structure for e
5f390 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e ach database con
5f3a0 6e 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 nection (sqlite3
5f3b0 2a 29 20 74 68 61 74 20 75 73 65 73 20 74 68 65 *) that uses the
5f3c0 20 73 68 61 72 65 64 0a 2a 2a 20 73 63 68 65 6d shared.** schem
5f3d0 61 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 a. This is becau
5f3e0 73 65 20 65 61 63 68 20 64 61 74 61 62 61 73 65 se each database
5f3f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 71 75 connection requ
5f400 69 72 65 73 20 69 74 73 20 6f 77 6e 20 75 6e 69 ires its own uni
5f410 71 75 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 que.** instance
5f420 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 of the sqlite3_v
5f430 74 61 62 2a 20 68 61 6e 64 6c 65 20 75 73 65 64 tab* handle used
5f440 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76 to access the v
5f450 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a irtual table .**
5f460 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
5f470 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 sqlite3_vtab* h
5f480 61 6e 64 6c 65 73 20 63 61 6e 20 6e 6f 74 20 62 andles can not b
5f490 65 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e e shared between
5f4a0 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f .** database co
5f4b0 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 76 65 6e 20 nnections, even
5f4c0 77 68 65 6e 20 74 68 65 20 72 65 73 74 20 6f 66 when the rest of
5f4d0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 the in-memory d
5f4e0 61 74 61 62 61 73 65 20 0a 2a 2a 20 73 63 68 65 atabase .** sche
5f4f0 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 61 73 ma is shared, as
5f500 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
5f510 69 6f 6e 20 6f 66 74 65 6e 20 73 74 6f 72 65 73 ion often stores
5f520 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
5f530 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 connection hand
5f540 6c 65 20 70 61 73 73 65 64 20 74 6f 20 69 74 20 le passed to it
5f550 76 69 61 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 via the xConnect
5f560 28 29 20 6f 72 20 78 43 72 65 61 74 65 28 29 20 () or xCreate()
5f570 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 72 69 6e 67 method.** during
5f580 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
5f590 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73 internally. This
5f5a0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
5f5b0 74 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0a tion handle may.
5f5c0 2a 2a 20 74 68 65 6e 20 75 73 65 64 20 62 79 20 ** then used by
5f5d0 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
5f5e0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
5f5f0 20 74 6f 20 61 63 63 65 73 73 20 72 65 61 6c 20 to access real
5f600 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 74 68 69 tables .** withi
5f610 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
5f620 53 6f 20 74 68 61 74 20 74 68 65 79 20 61 70 70 So that they app
5f630 65 61 72 20 61 73 20 70 61 72 74 20 6f 66 20 74 ear as part of t
5f640 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a 2a 20 74 he callers .** t
5f650 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 73 ransaction, thes
5f660 65 20 61 63 63 65 73 73 65 73 20 6e 65 65 64 20 e accesses need
5f670 74 6f 20 62 65 20 6d 61 64 65 20 76 69 61 20 74 to be made via t
5f680 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
5f690 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 .** connection
5f6a0 61 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 as that used to
5f6b0 65 78 65 63 75 74 65 20 53 51 4c 20 6f 70 65 72 execute SQL oper
5f6c0 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 76 69 ations on the vi
5f6d0 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a rtual table..**.
5f6e0 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 20 6f 62 ** All VTable ob
5f6f0 6a 65 63 74 73 20 74 68 61 74 20 63 6f 72 72 65 jects that corre
5f700 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 6e 67 6c spond to a singl
5f710 65 20 74 61 62 6c 65 20 69 6e 20 61 20 73 68 61 e table in a sha
5f720 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 red.** database
5f730 73 63 68 65 6d 61 20 61 72 65 20 69 6e 69 74 69 schema are initi
5f740 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61 ally stored in a
5f750 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 6f 69 linked-list poi
5f760 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 74 68 nted to by.** th
5f770 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c 65 20 e Table.pVTable
5f780 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 20 member variable
5f790 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e of the correspon
5f7a0 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 6a 65 63 ding Table objec
5f7b0 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 73 71 t..** When an sq
5f7c0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 lite3_prepare()
5f7d0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71 operation is req
5f7e0 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 uired to access
5f7f0 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 the virtual.** t
5f800 61 62 6c 65 2c 20 69 74 20 73 65 61 72 63 68 65 able, it searche
5f810 73 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 74 s the list for t
5f820 68 65 20 56 54 61 62 6c 65 20 74 68 61 74 20 63 he VTable that c
5f830 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 orresponds to th
5f840 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f e.** database co
5f850 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e 67 20 74 nnection doing t
5f860 68 65 20 70 72 65 70 61 72 69 6e 67 20 73 6f 20 he preparing so
5f870 61 73 20 74 6f 20 75 73 65 20 74 68 65 20 63 6f as to use the co
5f880 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 74 65 33 rrect.** sqlite3
5f890 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 69 6e _vtab* handle in
5f8a0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 75 the compiled qu
5f8b0 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ery..**.** When
5f8c0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54 61 62 an in-memory Tab
5f8d0 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64 65 6c le object is del
5f8e0 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c eted (for exampl
5f8f0 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 73 63 e when the.** sc
5f900 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20 72 65 hema is being re
5f910 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d 65 20 loaded for some
5f920 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56 54 61 reason), the VTa
5f930 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 20 ble objects are
5f940 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 65 64 20 not .** deleted
5f950 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f and the sqlite3_
5f960 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 61 72 vtab* handles ar
5f970 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e 65 63 e not xDisconnec
5f980 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d 65 64 69 t()ed .** immedi
5f990 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64 2c 20 ately. Instead,
5f9a0 74 68 65 79 20 61 72 65 20 6d 6f 76 65 64 20 66 they are moved f
5f9b0 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 2e 70 56 rom the Table.pV
5f9c0 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 0a 2a 2a Table list to.**
5f9d0 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b 65 64 20 another linked
5f9e0 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20 74 list headed by t
5f9f0 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 he sqlite3.pDisc
5fa00 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 20 6f 66 onnect member of
5fa10 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f the.** correspo
5fa20 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 20 73 74 nding sqlite3 st
5fa30 72 75 63 74 75 72 65 2e 20 54 68 65 79 20 61 72 ructure. They ar
5fa40 65 20 74 68 65 6e 20 64 65 6c 65 74 65 64 2f 78 e then deleted/x
5fa50 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 0a 2a 2a Disconnected .**
5fa60 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 74 61 next time a sta
5fa70 74 65 6d 65 6e 74 20 69 73 20 70 72 65 70 61 72 tement is prepar
5fa80 65 64 20 75 73 69 6e 67 20 73 61 69 64 20 73 71 ed using said sq
5fa90 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 69 73 20 lite3*. This is
5faa0 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 done.** to avoid
5fab0 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 75 65 73 deadlock issues
5fac0 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c 74 69 involving multi
5fad0 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 ple sqlite3.mute
5fae0 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 20 52 65 x mutexes..** Re
5faf0 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 fer to comments
5fb00 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 above function s
5fb10 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b qlite3VtabUnlock
5fb20 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 0a 2a 2a List() for an.**
5fb30 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 61 73 20 explanation as
5fb40 74 6f 20 77 68 79 20 69 74 20 69 73 20 73 61 66 to why it is saf
5fb50 65 20 74 6f 20 61 64 64 20 61 6e 20 65 6e 74 72 e to add an entr
5fb60 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 2e y to an sqlite3.
5fb70 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20 6c pDisconnect.** l
5fb80 69 73 74 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 ist without hold
5fb90 69 6e 67 20 74 68 65 20 63 6f 72 72 65 73 70 6f ing the correspo
5fba0 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 nding sqlite3.mu
5fbb0 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a tex mutex..**.**
5fbc0 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 The memory for
5fbd0 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20 objects of this
5fbe0 74 79 70 65 20 69 73 20 61 6c 77 61 79 73 20 61 type is always a
5fbf0 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 llocated by .**
5fc00 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 sqlite3DbMalloc(
5fc10 29 2c 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6e ), using the con
5fc20 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 73 nection handle s
5fc30 74 6f 72 65 64 20 69 6e 20 56 54 61 62 6c 65 2e tored in VTable.
5fc40 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 20 66 69 db as .** the fi
5fc50 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f rst argument..*/
5fc60 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 7b .struct VTable {
5fc70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
5fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5fc90 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 Database connect
5fca0 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 ion associated w
5fcb0 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20 2a ith this table *
5fcc0 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 /. Module *pMod
5fcd0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
5fce0 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f 64 75 Pointer to modu
5fcf0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
5fd00 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 n */. sqlite3_v
5fd10 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20 tab *pVtab;
5fd20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
5fd30 74 61 62 20 69 6e 73 74 61 6e 63 65 20 2a 2f 0a tab instance */.
5fd40 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
5fd50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5fd60 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 umber of pointer
5fd70 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 s to this struct
5fd80 75 72 65 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 ure */. VTable
5fd90 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
5fda0 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 /* Next in li
5fdb0 6e 6b 65 64 20 6c 69 73 74 20 28 73 65 65 20 61 nked list (see a
5fdc0 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a bove) */.};../*.
5fdd0 2a 2a 20 45 61 63 68 20 53 51 4c 20 74 61 62 6c ** Each SQL tabl
5fde0 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 e is represented
5fdf0 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e in memory by an
5fe00 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
5fe10 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 .** following st
5fe20 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
5fe30 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73 20 74 68 able.zName is th
5fe40 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
5fe50 62 6c 65 2e 20 20 54 68 65 20 63 61 73 65 20 6f ble. The case o
5fe60 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a f the original.*
5fe70 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 * CREATE TABLE s
5fe80 74 61 74 65 6d 65 6e 74 20 69 73 20 73 74 6f 72 tatement is stor
5fe90 65 64 2c 20 62 75 74 20 63 61 73 65 20 69 73 20 ed, but case is
5fea0 6e 6f 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 not significant
5feb0 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f for.** compariso
5fec0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e ns..**.** Table.
5fed0 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 nCol is the numb
5fee0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
5fef0 20 74 68 69 73 20 74 61 62 6c 65 2e 20 20 54 61 this table. Ta
5ff00 62 6c 65 2e 61 43 6f 6c 20 69 73 20 61 0a 2a 2a ble.aCol is a.**
5ff10 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 pointer to an a
5ff20 72 72 61 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 73 rray of Column s
5ff30 74 72 75 63 74 75 72 65 73 2c 20 6f 6e 65 20 66 tructures, one f
5ff40 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a or each column..
5ff50 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 **.** If the tab
5ff60 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 le has an INTEGE
5ff70 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 R PRIMARY KEY, t
5ff80 68 65 6e 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 hen Table.iPKey
5ff90 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a is the index of.
5ffa0 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 ** the column th
5ffb0 61 74 20 69 73 20 74 68 61 74 20 6b 65 79 2e 20 at is that key.
5ffc0 20 20 4f 74 68 65 72 77 69 73 65 20 54 61 62 6c Otherwise Tabl
5ffd0 65 2e 69 50 4b 65 79 20 69 73 20 6e 65 67 61 74 e.iPKey is negat
5ffe0 69 76 65 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 68 ive. Note.** th
5fff0 61 74 20 74 68 65 20 64 61 74 61 74 79 70 65 20 at the datatype
60000 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b of the PRIMARY K
60010 45 59 20 6d 75 73 74 20 62 65 20 49 4e 54 45 47 EY must be INTEG
60020 45 52 20 66 6f 72 20 74 68 69 73 20 66 69 65 6c ER for this fiel
60030 64 20 74 6f 0a 2a 2a 20 62 65 20 73 65 74 2e 20 d to.** be set.
60040 20 41 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d An INTEGER PRIM
60050 41 52 59 20 4b 45 59 20 69 73 20 75 73 65 64 20 ARY KEY is used
60060 61 73 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 as the rowid for
60070 20 65 61 63 68 20 72 6f 77 20 6f 66 0a 2a 2a 20 each row of.**
60080 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 61 the table. If a
60090 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20 49 4e table has no IN
600a0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
600b0 59 2c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d Y, then a random
600c0 20 72 6f 77 69 64 0a 2a 2a 20 69 73 20 67 65 6e rowid.** is gen
600d0 65 72 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 erated for each
600e0 72 6f 77 20 6f 66 20 74 68 65 20 74 61 62 6c 65 row of the table
600f0 2e 20 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 . TF_HasPrimary
60100 4b 65 79 20 69 73 20 73 65 74 20 69 66 0a 2a 2a Key is set if.**
60110 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 the table has a
60120 6e 79 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ny PRIMARY KEY,
60130 49 4e 54 45 47 45 52 20 6f 72 20 6f 74 68 65 72 INTEGER or other
60140 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c wise..**.** Tabl
60150 65 2e 74 6e 75 6d 20 69 73 20 74 68 65 20 70 61 e.tnum is the pa
60160 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ge number for th
60170 65 20 72 6f 6f 74 20 42 54 72 65 65 20 70 61 67 e root BTree pag
60180 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 e of the table i
60190 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 n the.** databas
601a0 65 20 66 69 6c 65 2e 20 20 49 66 20 54 61 62 6c e file. If Tabl
601b0 65 2e 69 44 62 20 69 73 20 74 68 65 20 69 6e 64 e.iDb is the ind
601c0 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ex of the databa
601d0 73 65 20 74 61 62 6c 65 20 62 61 63 6b 65 6e 64 se table backend
601e0 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 2e 61 44 .** in sqlite.aD
601f0 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f 72 20 74 b[]. 0 is for t
60200 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
60210 20 61 6e 64 20 31 20 69 73 20 66 6f 72 20 74 68 and 1 is for th
60220 65 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 68 e file that.** h
60230 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72 79 20 74 olds temporary t
60240 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 ables and indice
60250 73 2e 20 20 49 66 20 54 46 5f 45 70 68 65 6d 65 s. If TF_Epheme
60260 72 61 6c 20 69 73 20 73 65 74 0a 2a 2a 20 74 68 ral is set.** th
60270 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 en the table is
60280 73 74 6f 72 65 64 20 69 6e 20 61 20 66 69 6c 65 stored in a file
60290 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 that is automat
602a0 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 0a 2a ically deleted.*
602b0 2a 20 77 68 65 6e 20 74 68 65 20 56 44 42 45 20 * when the VDBE
602c0 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 74 61 cursor to the ta
602d0 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 ble is closed.
602e0 49 6e 20 74 68 69 73 20 63 61 73 65 20 54 61 62 In this case Tab
602f0 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 65 le.tnum .** refe
60300 72 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e rs VDBE cursor n
60310 75 6d 62 65 72 20 74 68 61 74 20 68 6f 6c 64 73 umber that holds
60320 20 74 68 65 20 74 61 62 6c 65 20 6f 70 65 6e 2c the table open,
60330 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 not to the root
60340 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 2e .** page number.
60350 20 20 54 72 61 6e 73 69 65 6e 74 20 74 61 62 6c Transient tabl
60360 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 68 es are used to h
60370 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73 20 old the results
60380 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 75 65 72 of a.** sub-quer
60390 79 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69 y that appears i
603a0 6e 73 74 65 61 64 20 6f 66 20 61 20 72 65 61 6c nstead of a real
603b0 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 table name in t
603c0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a he FROM clause .
603d0 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 ** of a SELECT s
603e0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 tatement..*/.str
603f0 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 20 73 71 uct Table {. sq
60400 6c 69 74 65 33 20 2a 64 62 4d 65 6d 3b 20 20 20 lite3 *dbMem;
60410 20 20 20 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 74 /* DB connect
60420 69 6f 6e 20 75 73 65 64 20 66 6f 72 20 6c 6f 6f ion used for loo
60430 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f kaside allocatio
60440 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ns. */. char *z
60450 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Name; /*
60460 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 Name of the tab
60470 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 le or view */.
60480 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 20 20 20 int iPKey;
60490 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e /* If not n
604a0 65 67 61 74 69 76 65 2c 20 75 73 65 20 61 43 6f egative, use aCo
604b0 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 68 65 20 l[iPKey] as the
604c0 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 primary key */.
604d0 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 int nCol;
604e0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
604f0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
60500 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 43 6f is table */. Co
60510 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 20 lumn *aCol;
60520 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
60530 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c n about each col
60540 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a umn */. Index *
60550 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 2f 2a pIndex; /*
60560 20 4c 69 73 74 20 6f 66 20 53 51 4c 20 69 6e 64 List of SQL ind
60570 65 78 65 73 20 6f 6e 20 74 68 69 73 20 74 61 62 exes on this tab
60580 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 le. */. int tnu
60590 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a m; /*
605a0 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f 64 65 Root BTree node
605b0 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 for this table
605c0 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76 65 29 (see note above)
605d0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 */. Select *pS
605e0 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 elect; /* NU
605f0 4c 4c 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 LL for tables.
60600 50 6f 69 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 Points to defini
60610 74 69 6f 6e 20 69 66 20 61 20 76 69 65 77 2e 20 tion if a view.
60620 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 3b 20 20 */. u16 nRef;
60630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
60640 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 ber of pointers
60650 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20 2a 2f to this Table */
60660 0a 20 20 75 38 20 74 61 62 46 6c 61 67 73 3b 20 . u8 tabFlags;
60670 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 /* Mask
60680 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 73 20 2a of TF_* values *
60690 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b 20 /. u8 keyConf;
606a0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 /* What
606b0 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f to do in case o
606c0 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e f uniqueness con
606d0 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79 20 2a flict on iPKey *
606e0 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b /. FKey *pFKey;
606f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b /* Link
60700 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66 ed list of all f
60710 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 74 oreign keys in t
60720 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 his table */. c
60730 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 har *zColAff;
60740 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 /* String de
60750 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e fining the affin
60760 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 ity of each colu
60770 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 mn */.#ifndef SQ
60780 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a LITE_OMIT_CHECK.
60790 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 3b 20 Expr *pCheck;
607a0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 41 4e /* The AN
607b0 44 20 6f 66 20 61 6c 6c 20 43 48 45 43 4b 20 63 D of all CHECK c
607c0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 23 65 onstraints */.#e
607d0 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
607e0 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 ITE_OMIT_ALTERTA
607f0 42 4c 45 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c BLE. int addCol
60800 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 Offset; /* Of
60810 66 73 65 74 20 69 6e 20 43 52 45 41 54 45 20 54 fset in CREATE T
60820 41 42 4c 45 20 73 74 6d 74 20 74 6f 20 61 64 64 ABLE stmt to add
60830 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f a new column */
60840 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
60850 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
60860 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62 6c UALTABLE. VTabl
60870 65 20 2a 70 56 54 61 62 6c 65 3b 20 20 20 20 20 e *pVTable;
60880 2f 2a 20 4c 69 73 74 20 6f 66 20 56 54 61 62 6c /* List of VTabl
60890 65 20 6f 62 6a 65 63 74 73 2e 20 2a 2f 0a 20 20 e objects. */.
608a0 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 int nModuleArg;
608b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
608c0 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 f arguments to t
608d0 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 he module */. c
608e0 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 har **azModuleAr
608f0 67 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 61 g; /* Text of a
60900 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67 73 2e 20 ll module args.
60910 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65 20 6e 61 [0] is module na
60920 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 me */.#endif. T
60930 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 rigger *pTrigger
60940 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 ; /* List of t
60950 72 69 67 67 65 72 73 20 73 74 6f 72 65 64 20 69 riggers stored i
60960 6e 20 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 n pSchema */. S
60970 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 chema *pSchema;
60980 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 /* Schema th
60990 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 at contains this
609a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c table */. Tabl
609b0 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 e *pNextZombie;
609c0 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 /* Next on the
609d0 50 61 72 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 Parse.pZombieTab
609e0 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a list */.};../*.
609f0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
60a00 73 20 66 6f 72 20 54 61 62 65 2e 74 61 62 46 6c s for Tabe.tabFl
60a10 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ags..*/.#define
60a20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 TF_Readonly
60a30 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 52 65 0x01 /* Re
60a40 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 ad-only system t
60a50 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 able */.#define
60a60 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 20 20 20 TF_Ephemeral
60a70 20 20 20 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 0x02 /* An
60a80 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 ephemeral table
60a90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 */.#define TF_H
60aa0 61 73 50 72 69 6d 61 72 79 4b 65 79 20 20 20 30 asPrimaryKey 0
60ab0 78 30 34 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 x04 /* Table
60ac0 68 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 has a primary ke
60ad0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f y */.#define TF_
60ae0 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 Autoincrement
60af0 30 78 30 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 0x08 /* Integ
60b00 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 er primary key i
60b10 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 s autoincrement
60b20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 56 69 */.#define TF_Vi
60b30 72 74 75 61 6c 20 20 20 20 20 20 20 20 20 30 78 rtual 0x
60b40 31 30 20 20 20 20 2f 2a 20 49 73 20 61 20 76 69 10 /* Is a vi
60b50 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 rtual table */.#
60b60 64 65 66 69 6e 65 20 54 46 5f 4e 65 65 64 4d 65 define TF_NeedMe
60b70 74 61 64 61 74 61 20 20 20 20 30 78 32 30 20 20 tadata 0x20
60b80 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 70 /* aCol[].zTyp
60b90 65 20 61 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 6f e and aCol[].pCo
60ba0 6c 6c 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a 0a ll missing */...
60bb0 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 ./*.** Test to s
60bc0 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ee whether or no
60bd0 74 20 61 20 74 61 62 6c 65 20 69 73 20 61 20 76 t a table is a v
60be0 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 irtual table. T
60bf0 68 69 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 his is.** done a
60c00 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 s a macro so tha
60c10 74 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 t it will be opt
60c20 69 6d 69 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 imized out when
60c30 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 virtual.** table
60c40 20 73 75 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 support is omit
60c50 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69 ted from the bui
60c60 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ld..*/.#ifndef S
60c70 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
60c80 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e ALTABLE.# defin
60c90 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 20 e IsVirtual(X)
60ca0 20 20 20 20 28 28 28 58 29 2d 3e 74 61 62 46 6c (((X)->tabFl
60cb0 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c ags & TF_Virtual
60cc0 29 21 3d 30 29 0a 23 20 20 64 65 66 69 6e 65 20 )!=0).# define
60cd0 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 IsHiddenColumn(X
60ce0 29 20 28 28 58 29 2d 3e 69 73 48 69 64 64 65 6e ) ((X)->isHidden
60cf0 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e ).#else.# defin
60d00 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 20 e IsVirtual(X)
60d10 20 20 20 20 30 0a 23 20 20 64 65 66 69 6e 65 20 0.# define
60d20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 IsHiddenColumn(X
60d30 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ) 0.#endif../*.*
60d40 2a 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20 6b * Each foreign k
60d50 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 ey constraint is
60d60 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
60d70 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
60d80 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 ructure..**.** A
60d90 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 foreign key is
60da0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
60db0 74 77 6f 20 74 61 62 6c 65 73 2e 20 20 54 68 65 two tables. The
60dc0 20 22 66 72 6f 6d 22 20 74 61 62 6c 65 20 69 73 "from" table is
60dd0 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74 68 .** the table th
60de0 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
60df0 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 REFERENCES claus
60e00 65 20 74 68 61 74 20 63 72 65 61 74 65 73 20 74 e that creates t
60e10 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 he foreign.** ke
60e20 79 2e 20 20 54 68 65 20 22 74 6f 22 20 74 61 62 y. The "to" tab
60e30 6c 65 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 le is the table
60e40 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69 6e that is named in
60e50 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 the REFERENCES
60e60 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 69 clause..** Consi
60e70 64 65 72 20 74 68 69 73 20 65 78 61 6d 70 6c 65 der this example
60e80 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 :.**.** CREA
60e90 54 45 20 54 41 42 4c 45 20 65 78 31 28 0a 2a 2a TE TABLE ex1(.**
60ea0 20 20 20 20 20 20 20 61 20 49 4e 54 45 47 45 52 a INTEGER
60eb0 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 0a 2a 2a PRIMARY KEY,.**
60ec0 20 20 20 20 20 20 20 62 20 49 4e 54 45 47 45 52 b INTEGER
60ed0 20 43 4f 4e 53 54 52 41 49 4e 54 20 66 6b 31 20 CONSTRAINT fk1
60ee0 52 45 46 45 52 45 4e 43 45 53 20 65 78 32 28 78 REFERENCES ex2(x
60ef0 29 0a 2a 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a ).** );.**.*
60f00 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 * For foreign ke
60f10 79 20 22 66 6b 31 22 2c 20 74 68 65 20 66 72 6f y "fk1", the fro
60f20 6d 2d 74 61 62 6c 65 20 69 73 20 22 65 78 31 22 m-table is "ex1"
60f30 20 61 6e 64 20 74 68 65 20 74 6f 2d 74 61 62 6c and the to-tabl
60f40 65 20 69 73 20 22 65 78 32 22 2e 0a 2a 2a 0a 2a e is "ex2"..**.*
60f50 2a 20 45 61 63 68 20 52 45 46 45 52 45 4e 43 45 * Each REFERENCE
60f60 53 20 63 6c 61 75 73 65 20 67 65 6e 65 72 61 74 S clause generat
60f70 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f es an instance o
60f80 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
60f90 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 68 69 structure.** whi
60fa0 63 68 20 69 73 20 61 74 74 61 63 68 65 64 20 74 ch is attached t
60fb0 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 o the from-table
60fc0 2e 20 20 54 68 65 20 74 6f 2d 74 61 62 6c 65 20 . The to-table
60fd0 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 20 77 need not exist w
60fe0 68 65 6e 0a 2a 2a 20 74 68 65 20 66 72 6f 6d 2d hen.** the from-
60ff0 74 61 62 6c 65 20 69 73 20 63 72 65 61 74 65 64 table is created
61000 2e 20 20 54 68 65 20 65 78 69 73 74 65 6e 63 65 . The existence
61010 20 6f 66 20 74 68 65 20 74 6f 2d 74 61 62 6c 65 of the to-table
61020 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 2e is not checked.
61030 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b 65 79 20 .*/.struct FKey
61040 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d {. Table *pFrom
61050 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 ; /* Table c
61060 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 ontaining the RE
61070 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 FERENCES clause
61080 28 61 6b 61 3a 20 43 68 69 6c 64 29 20 2a 2f 0a (aka: Child) */.
61090 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72 6f FKey *pNextFro
610a0 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 65 m; /* Next fore
610b0 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f 6d ign key in pFrom
610c0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 3b */. char *zTo;
610d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
610e0 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 of table that th
610f0 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 e key points to
61100 28 61 6b 61 3a 20 50 61 72 65 6e 74 29 20 2a 2f (aka: Parent) */
61110 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 54 6f . FKey *pNextTo
61120 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 ; /* Next for
61130 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61 62 6c eign key on tabl
61140 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f 0a 20 e named zTo */.
61150 20 46 4b 65 79 20 2a 70 50 72 65 76 54 6f 3b 20 FKey *pPrevTo;
61160 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 66 /* Previous f
61170 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61 oreign key on ta
61180 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f ble named zTo */
61190 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 . int nCol;
611a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
611b0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 f columns in thi
611c0 73 20 6b 65 79 20 2a 2f 0a 20 20 2f 2a 20 45 56 s key */. /* EV
611d0 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20 : R-30323-21917
611e0 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 72 */. u8 isDeferr
611f0 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ed; /* True i
61200 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 f constraint che
61210 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 cking is deferre
61220 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f d till COMMIT */
61230 0a 20 20 75 38 20 61 41 63 74 69 6f 6e 5b 32 5d . u8 aAction[2]
61240 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e ; /* ON
61250 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 DELETE and ON U
61260 50 44 41 54 45 20 61 63 74 69 6f 6e 73 2c 20 72 PDATE actions, r
61270 65 73 70 65 63 74 69 76 65 6c 79 20 2a 2f 0a 20 espectively */.
61280 20 54 72 69 67 67 65 72 20 2a 61 70 54 72 69 67 Trigger *apTrig
61290 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54 72 69 67 ger[2]; /* Trig
612a0 67 65 72 73 20 66 6f 72 20 61 41 63 74 69 6f 6e gers for aAction
612b0 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 [] actions */.
612c0 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 7b struct sColMap {
612d0 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 20 /* Mapping of
612e0 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f 6d columns in pFrom
612f0 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a to columns in z
61300 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 46 To */. int iF
61310 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 rom; /*
61320 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 Index of column
61330 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 in pFrom */.
61340 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 char *zCol;
61350 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f /* Name of co
61360 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 66 lumn in zTo. If
61370 20 30 20 75 73 65 20 50 52 49 4d 41 52 59 20 4b 0 use PRIMARY K
61380 45 59 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 5b 31 EY */. } aCol[1
61390 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 ]; /* One
613a0 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 entry for each
613b0 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 of nCol column s
613c0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 51 */.};../*.** SQ
613d0 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 6d 61 Lite supports ma
613e0 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 ny different way
613f0 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 63 s to resolve a c
61400 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 onstraint.** err
61410 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 70 72 or. ROLLBACK pr
61420 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 ocessing means t
61430 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 hat a constraint
61440 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 63 61 violation.** ca
61450 75 73 65 73 20 74 68 65 20 6f 70 65 72 61 74 69 uses the operati
61460 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20 74 6f on in process to
61470 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20 74 68 fail and for th
61480 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
61490 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 ction.** to be r
614a0 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 42 4f olled back. ABO
614b0 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 RT processing me
614c0 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 69 6f ans the operatio
614d0 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a 2a 20 n in process.**
614e0 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 70 72 fails and any pr
614f0 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72 6f 6d ior changes from
61500 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 61 74 that one operat
61510 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 20 6f ion are backed o
61520 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 ut,.** but the t
61530 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f ransaction is no
61540 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 t rolled back.
61550 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e 67 20 FAIL processing
61560 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 means that.** th
61570 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 e operation in p
61580 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 61 6e rogress stops an
61590 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 d returns an err
615a0 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 70 72 or code. But pr
615b0 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73 20 64 ior.** changes d
615c0 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6f ue to the same o
615d0 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e 6f 74 peration are not
615e0 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 backed out and
615f0 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f no rollback.** o
61600 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45 20 6d ccurs. IGNORE m
61610 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 eans that the pa
61620 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74 68 61 rticular row tha
61630 74 20 63 61 75 73 65 64 20 74 68 65 20 63 6f 6e t caused the con
61640 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 straint.** error
61650 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74 65 64 is not inserted
61660 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 50 72 or updated. Pr
61670 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 ocessing continu
61680 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 0a es and no error.
61690 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 ** is returned.
616a0 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73 20 74 REPLACE means t
616b0 68 61 74 20 70 72 65 65 78 69 73 74 69 6e 67 20 hat preexisting
616c0 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 68 database rows th
616d0 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61 20 55 at caused.** a U
616e0 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 NIQUE constraint
616f0 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65 20 72 violation are r
61700 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74 20 74 emoved so that t
61710 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20 6f 72 he new insert or
61720 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e 20 70 .** update can p
61730 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65 73 73 roceed. Process
61740 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e ing continues an
61750 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 d no error is re
61760 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 45 ported..**.** RE
61770 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c 4c 2c STRICT, SETNULL,
61780 20 61 6e 64 20 43 41 53 43 41 44 45 20 61 63 74 and CASCADE act
61790 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c 79 20 ions apply only
617a0 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e to foreign keys.
617b0 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69 73 20 .** RESTRICT is
617c0 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 the same as ABOR
617d0 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 20 T for IMMEDIATE
617e0 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 6e 64 foreign keys and
617f0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 the.** same as
61800 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44 45 46 ROLLBACK for DEF
61810 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53 45 54 ERRED keys. SET
61820 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 NULL means that
61830 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b the foreign.** k
61840 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c ey is set to NUL
61850 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65 61 6e L. CASCADE mean
61860 73 20 74 68 61 74 20 61 20 44 45 4c 45 54 45 20 s that a DELETE
61870 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74 68 65 or UPDATE of the
61880 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 74 .** referenced t
61890 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72 6f 70 able row is prop
618a0 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68 65 20 agated into the
618b0 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73 20 74 row that holds t
618c0 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 he.** foreign ke
618d0 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 66 6f y..** .** The fo
618e0 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 69 63 llowing symbolic
618f0 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 65 64 values are used
61900 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 68 to record which
61910 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63 74 69 type.** of acti
61920 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f 0a 23 on to take..*/.#
61930 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 20 20 define OE_None
61940 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72 65 20 0 /* There
61950 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69 6e 74 is no constraint
61960 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 64 65 to check */.#de
61970 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b fine OE_Rollback
61980 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74 68 65 1 /* Fail the
61990 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 operation and r
619a0 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e ollback the tran
619b0 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 saction */.#defi
619c0 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20 20 32 ne OE_Abort 2
619d0 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74 20 63 /* Back out c
619e0 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20 6e 6f hanges but do no
619f0 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 rollback transa
61a00 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 ction */.#define
61a10 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33 20 20 OE_Fail 3
61a20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f 70 65 /* Stop the ope
61a30 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61 76 65 ration but leave
61a40 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61 6e 67 all prior chang
61a50 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 es */.#define OE
61a60 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20 2f 2a _Ignore 4 /*
61a70 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f Ignore the erro
61a80 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 65 r. Do not do the
61a90 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 INSERT or UPDAT
61aa0 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f E */.#define OE_
61ab0 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f 2a 20 Replace 5 /*
61ac0 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e 67 20 Delete existing
61ad0 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64 6f 20 record, then do
61ae0 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 INSERT or UPDATE
61af0 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f */..#define OE_
61b00 52 65 73 74 72 69 63 74 20 36 20 20 20 2f 2a 20 Restrict 6 /*
61b10 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49 4d 4d OE_Abort for IMM
61b20 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c 6c 62 EDIATE, OE_Rollb
61b30 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52 45 44 ack for DEFERRED
61b40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53 */.#define OE_S
61b50 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a 20 53 etNull 7 /* S
61b60 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b et the foreign k
61b70 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55 4c 4c ey value to NULL
61b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 53 */.#define OE_S
61b90 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a 20 53 etDflt 8 /* S
61ba0 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b et the foreign k
61bb0 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 ey value to its
61bc0 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65 66 69 default */.#defi
61bd0 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20 20 39 ne OE_Cascade 9
61be0 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20 74 68 /* Cascade th
61bf0 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a 23 64 e changes */..#d
61c00 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 6c 74 efine OE_Default
61c10 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 61 74 99 /* Do what
61c20 65 76 65 72 20 74 68 65 20 64 65 66 61 75 6c 74 ever the default
61c30 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a 0a 0a action is */...
61c40 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
61c50 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
61c60 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
61c70 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
61c80 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
61c90 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4b 65 to sqlite3VdbeKe
61ca0 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69 73 20 yCompare and is
61cb0 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 used to control
61cc0 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 the .** comparis
61cd0 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20 69 6e on of the two in
61ce0 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73 74 72 dex keys..*/.str
61cf0 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a 20 20 uct KeyInfo {.
61d00 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
61d10 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
61d20 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
61d30 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 /. u8 enc;
61d40 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 /* Text
61d50 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 20 6f encoding - one o
61d60 66 20 74 68 65 20 54 45 58 54 5f 55 74 66 2a 20 f the TEXT_Utf*
61d70 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31 36 20 values */. u16
61d80 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 nField;
61d90 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
61da0 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b 5d 20 ries in aColl[]
61db0 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 */. u8 *aSortOr
61dc0 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66 20 64 der; /* If d
61dd0 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72 74 4f efined an aSortO
61de0 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75 65 2c rder[i] is true,
61df0 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a 20 20 sort DESC */.
61e00 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 5b 31 CollSeq *aColl[1
61e10 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 ]; /* Collating
61e20 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 61 sequence for ea
61e30 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6b ch term of the k
61e40 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ey */.};../*.**
61e50 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
61e60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
61e70 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 ucture holds inf
61e80 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
61e90 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 65 78 .** single index
61ea0 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73 record that has
61eb0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 61 already been pa
61ec0 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 69 6e rsed out into in
61ed0 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 6c 75 dividual.** valu
61ee0 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 63 6f es..**.** A reco
61ef0 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20 rd is an object
61f00 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e that contains on
61f10 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64 73 e or more fields
61f20 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 65 63 of data..** Rec
61f30 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74 6f ords are used to
61f40 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65 store the conte
61f50 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72 6f nt of a table ro
61f60 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0a 2a w and to store.*
61f70 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 6e 20 * the key of an
61f80 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 20 65 index. A blob e
61f90 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63 ncoding of a rec
61fa0 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 20 62 ord is created b
61fb0 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 6b 65 y.** the OP_Make
61fc0 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 Record opcode of
61fd0 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 69 73 the VDBE and is
61fe0 20 64 69 73 61 73 73 65 6d 62 6c 65 64 20 62 79 disassembled by
61ff0 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d the.** OP_Colum
62000 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 n opcode..**.**
62010 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 This structure h
62020 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 20 74 68 olds a record th
62030 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 at has already b
62040 65 65 6e 20 64 69 73 61 73 73 65 6d 62 6c 65 64 een disassembled
62050 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 63 6f 6e .** into its con
62060 73 74 69 74 75 65 6e 74 20 66 69 65 6c 64 73 2e stituent fields.
62070 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 70 61 63 .*/.struct Unpac
62080 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 20 4b 65 kedRecord {. Ke
62090 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
620a0 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 61 /* Collation a
620b0 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 20 69 6e nd sort-order in
620c0 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 formation */. u
620d0 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 16 nField;
620e0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
620f0 65 6e 74 72 69 65 73 20 69 6e 20 61 70 4d 65 6d entries in apMem
62100 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 [] */. u16 flag
62110 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 s; /* B
62120 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e 67 73 2e oolean settings.
62130 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e 2e 20 62 UNPACKED_... b
62140 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 20 72 6f elow */. i64 ro
62150 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a wid; /*
62160 20 55 73 65 64 20 62 79 20 55 4e 50 41 43 4b 45 Used by UNPACKE
62170 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 D_PREFIX_SEARCH
62180 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 */. Mem *aMem;
62190 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
621a0 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 es */.};../*.**
621b0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f Allowed values o
621c0 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 f UnpackedRecord
621d0 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e .flags.*/.#defin
621e0 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f e UNPACKED_NEED_
621f0 46 52 45 45 20 20 20 20 20 30 78 30 30 30 31 20 FREE 0x0001
62200 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20 66 72 /* Memory is fr
62210 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 om sqlite3Malloc
62220 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e () */.#define UN
62230 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 PACKED_NEED_DEST
62240 52 4f 59 20 20 30 78 30 30 30 32 20 20 2f 2a 20 ROY 0x0002 /*
62250 61 70 4d 65 6d 5b 5d 73 20 73 68 6f 75 6c 64 20 apMem[]s should
62260 61 6c 6c 20 62 65 20 64 65 73 74 72 6f 79 65 64 all be destroyed
62270 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 */.#define UNPA
62280 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 CKED_IGNORE_ROWI
62290 44 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 67 D 0x0004 /* Ig
622a0 6e 6f 72 65 20 74 72 61 69 6c 69 6e 67 20 72 6f nore trailing ro
622b0 77 69 64 20 6f 6e 20 6b 65 79 31 20 2a 2f 0a 23 wid on key1 */.#
622c0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
622d0 49 4e 43 52 4b 45 59 20 20 20 20 20 20 20 30 78 INCRKEY 0x
622e0 30 30 30 38 20 20 2f 2a 20 4d 61 6b 65 20 74 68 0008 /* Make th
622f0 69 73 20 6b 65 79 20 61 6e 20 65 70 73 69 6c 6f is key an epsilo
62300 6e 20 6c 61 72 67 65 72 20 2a 2f 0a 23 64 65 66 n larger */.#def
62310 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 ine UNPACKED_PRE
62320 46 49 58 5f 4d 41 54 43 48 20 20 30 78 30 30 31 FIX_MATCH 0x001
62330 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20 6d 0 /* A prefix m
62340 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65 72 atch is consider
62350 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 ed OK */.#define
62360 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 UNPACKED_PREFIX
62370 5f 53 45 41 52 43 48 20 30 78 30 30 32 30 20 20 _SEARCH 0x0020
62380 2f 2a 20 41 20 70 72 65 66 69 78 20 6d 61 74 63 /* A prefix matc
62390 68 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 h is considered
623a0 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 OK */../*.** Eac
623b0 68 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20 72 h SQL index is r
623c0 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 epresented in me
623d0 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e mory by an.** in
623e0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
623f0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
62400 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c e..**.** The col
62410 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c umns of the tabl
62420 65 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65 e that are to be
62430 20 69 6e 64 65 78 65 64 20 61 72 65 20 64 65 73 indexed are des
62440 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68 65 cribed.** by the
62450 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c aiColumn[] fiel
62460 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 d of this struct
62470 75 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c ure. For exampl
62480 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65 e, suppose.** we
62490 20 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77 have the follow
624a0 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69 6e ing table and in
624b0 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 dex:.**.** C
624c0 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 28 REATE TABLE Ex1(
624d0 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20 c1 int, c2 int,
624e0 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20 c3 text);.**
624f0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45 78 CREATE INDEX Ex
62500 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29 3b 2 ON Ex1(c3,c1);
62510 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61 .**.** In the Ta
62520 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 64 65 ble structure de
62530 73 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e 43 scribing Ex1, nC
62540 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74 68 ol==3 because th
62550 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65 65 ere are.** three
62560 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
62570 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20 49 table. In the I
62580 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 64 ndex structure d
62590 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32 escribing.** Ex2
625a0 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e , nColumn==2 sin
625b0 63 65 20 32 20 6f 66 20 74 68 65 20 33 20 63 6f ce 2 of the 3 co
625c0 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72 65 lumns of Ex1 are
625d0 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65 indexed..** The
625e0 20 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75 value of aiColu
625f0 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61 mn is {2, 0}. a
62600 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 iColumn[0]==2 be
62610 63 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66 69 cause the .** fi
62620 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 rst column to be
62630 20 69 6e 64 65 78 65 64 20 28 63 33 29 20 68 61 indexed (c3) ha
62640 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32 20 s an index of 2
62650 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a in Ex1.aCol[]..*
62660 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c * The second col
62670 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65 umn to be indexe
62680 64 20 28 63 31 29 20 68 61 73 20 61 6e 20 69 6e d (c1) has an in
62690 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 dex of 0 in.** E
626a0 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 x1.aCol[], hence
626b0 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d Ex2.aiColumn[1]
626c0 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 ==0..**.** The I
626d0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65 ndex.onError fie
626e0 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 ld determines wh
626f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
62700 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 indexed columns
62710 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69 71 .** must be uniq
62720 75 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20 64 ue and what to d
62730 6f 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f o if they are no
62740 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f t. When Index.o
62750 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a nError=OE_None,.
62760 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 ** it means this
62770 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75 65 is not a unique
62780 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 69 index. Otherwi
62790 73 65 20 69 74 20 69 73 20 61 20 75 6e 69 71 75 se it is a uniqu
627a0 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 e index.** and t
627b0 68 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 65 he value of Inde
627c0 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 61 x.onError indica
627d0 74 65 20 74 68 65 20 77 68 69 63 68 20 63 6f 6e te the which con
627e0 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e flict resolution
627f0 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74 .** algorithm t
62800 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65 o employ wheneve
62810 72 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 r an attempt is
62820 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 61 made to insert a
62830 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 non-unique.** e
62840 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 lement..*/.struc
62850 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61 72 t Index {. char
62860 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 *zName; /*
62870 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e 64 Name of this ind
62880 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c ex */. int nCol
62890 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 umn; /* Numb
628a0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
628b0 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 64 20 the table used
628c0 62 79 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f by this index */
628d0 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e . int *aiColumn
628e0 3b 20 20 20 2f 2a 20 57 68 69 63 68 20 63 6f 6c ; /* Which col
628f0 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 62 79 umns are used by
62900 20 74 68 69 73 20 69 6e 64 65 78 2e 20 20 31 73 this index. 1s
62910 74 20 69 73 20 30 20 2a 2f 0a 20 20 75 6e 73 69 t is 0 */. unsi
62920 67 6e 65 64 20 2a 61 69 52 6f 77 45 73 74 3b 20 gned *aiRowEst;
62930 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 41 4e 41 /* Result of ANA
62940 4c 59 5a 45 3a 20 45 73 74 2e 20 72 6f 77 73 20 LYZE: Est. rows
62950 73 65 6c 65 63 74 65 64 20 62 79 20 65 61 63 68 selected by each
62960 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 column */. Tab
62970 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 2f 2a le *pTable; /*
62980 20 54 68 65 20 53 51 4c 20 74 61 62 6c 65 20 62 The SQL table b
62990 65 69 6e 67 20 69 6e 64 65 78 65 64 20 2a 2f 0a eing indexed */.
629a0 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 int tnum;
629b0 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 /* Page conta
629c0 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 20 74 68 ining root of th
629d0 69 73 20 69 6e 64 65 78 20 69 6e 20 64 61 74 61 is index in data
629e0 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 base file */. u
629f0 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20 20 20 8 onError;
62a00 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f /* OE_Abort, OE_
62a10 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 Ignore, OE_Repla
62a20 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a ce, or OE_None *
62a30 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e 64 65 78 /. u8 autoIndex
62a40 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 ; /* True if
62a50 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 is automatically
62a60 20 63 72 65 61 74 65 64 20 28 65 78 3a 20 62 79 created (ex: by
62a70 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20 20 63 68 UNIQUE) */. ch
62a80 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 2f ar *zColAff; /
62a90 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e * String definin
62aa0 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f g the affinity o
62ab0 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f f each column */
62ac0 0a 20 20 49 6e 64 65 78 20 2a 70 4e 65 78 74 3b . Index *pNext;
62ad0 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 /* The next
62ae0 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 index associated
62af0 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 74 with the same t
62b00 61 62 6c 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 able */. Schema
62b10 20 2a 70 53 63 68 65 6d 61 3b 20 2f 2a 20 53 63 *pSchema; /* Sc
62b20 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 hema containing
62b30 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 this index */.
62b40 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 u8 *aSortOrder;
62b50 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69 7a /* Array of siz
62b60 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e e Index.nColumn.
62b70 20 54 72 75 65 3d 3d 44 45 53 43 2c 20 46 61 6c True==DESC, Fal
62b80 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 20 63 68 61 se==ASC */. cha
62b90 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a r **azColl; /*
62ba0 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 6c 61 74 Array of collat
62bb0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d ion sequence nam
62bc0 65 73 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a es for index */.
62bd0 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 61 IndexSample *a
62be0 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 41 72 Sample; /* Ar
62bf0 72 61 79 20 6f 66 20 53 51 4c 49 54 45 5f 49 4e ray of SQLITE_IN
62c00 44 45 58 5f 53 41 4d 50 4c 45 53 20 73 61 6d 70 DEX_SAMPLES samp
62c10 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a les */.};../*.**
62c20 20 45 61 63 68 20 73 61 6d 70 6c 65 20 73 74 6f Each sample sto
62c30 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 red in the sqlit
62c40 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73 e_stat2 table is
62c50 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 represented in
62c60 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73 69 6e 67 memory .** using
62c70 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 a structure of
62c80 74 68 69 73 20 74 79 70 65 2e 0a 2a 2f 0a 73 74 this type..*/.st
62c90 72 75 63 74 20 49 6e 64 65 78 53 61 6d 70 6c 65 ruct IndexSample
62ca0 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 {. union {.
62cb0 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 char *z;
62cc0 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54 79 /* Value if eTy
62cd0 70 65 20 69 73 20 53 51 4c 49 54 45 5f 54 45 58 pe is SQLITE_TEX
62ce0 54 20 6f 72 20 53 51 4c 49 54 45 5f 42 4c 4f 42 T or SQLITE_BLOB
62cf0 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 */. double r
62d00 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 ; /* Value
62d10 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c if eType is SQL
62d20 49 54 45 5f 46 4c 4f 41 54 20 6f 72 20 53 51 4c ITE_FLOAT or SQL
62d30 49 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0a 20 ITE_INTEGER */.
62d40 20 7d 20 75 3b 0a 20 20 75 38 20 65 54 79 70 65 } u;. u8 eType
62d50 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c ; /* SQL
62d60 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 ITE_NULL, SQLITE
62d70 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65 74 63 _INTEGER ... etc
62d80 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 79 74 65 3b . */. u8 nByte;
62d90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
62da0 20 69 6e 20 62 79 74 65 20 6f 66 20 74 65 78 74 in byte of text
62db0 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a 7d 3b 0a or blob. */.};.
62dc0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 ./*.** Each toke
62dd0 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 n coming out of
62de0 74 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 the lexer is an
62df0 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 instance of.** t
62e00 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
62e10 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 Tokens are also
62e20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
62e30 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a an expression..*
62e40 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b *.** Note if Tok
62e50 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b en.z==0 then Tok
62e60 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e en.dyn and Token
62e70 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 .n are undefined
62e80 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 and.** may cont
62e90 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 ain random value
62ea0 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 s. Do not make
62eb0 61 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 any assumptions
62ec0 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a about Token.dyn.
62ed0 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 ** and Token.n w
62ee0 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a hen Token.z==0..
62ef0 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 */.struct Token
62f00 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a {. const char *
62f10 7a 3b 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f z; /* Text o
62f20 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f f the token. No
62f30 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 t NULL-terminate
62f40 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 d! */. unsigned
62f50 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a 20 4e 75 int n; /* Nu
62f60 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
62f70 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65 6e rs in this token
62f80 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
62f90 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
62fa0 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 s structure cont
62fb0 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ains information
62fc0 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 needed to gener
62fd0 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 ate.** code for
62fe0 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 63 6f a SELECT that co
62ff0 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 ntains aggregate
63000 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a functions..**.*
63010 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d 3d 54 4b * If Expr.op==TK
63020 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f 72 20 54 _AGG_COLUMN or T
63030 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 74 K_AGG_FUNCTION t
63040 68 65 6e 20 45 78 70 72 2e 70 41 67 67 49 6e 66 hen Expr.pAggInf
63050 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 o is a.** pointe
63060 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 r to this struct
63070 75 72 65 2e 20 20 54 68 65 20 45 78 70 72 2e 69 ure. The Expr.i
63080 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 69 73 20 Column field is
63090 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 the index in.**
630a0 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 5d 20 6f AggInfo.aCol[] o
630b0 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b r AggInfo.aFunc[
630c0 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ] of information
630d0 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 needed to gener
630e0 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 ate.** code for
630f0 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a that node..**.**
63100 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 42 AggInfo.pGroupB
63110 79 20 61 6e 64 20 41 67 67 49 6e 66 6f 2e 61 46 y and AggInfo.aF
63120 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 6e 74 20 unc.pExpr point
63130 74 6f 20 66 69 65 6c 64 73 20 77 69 74 68 69 6e to fields within
63140 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c the.** original
63150 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 Select structur
63160 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 e that describes
63170 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 the SELECT stat
63180 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 0a 2a 2a ement. These.**
63190 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20 6e fields do not n
631a0 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 20 eed to be freed
631b0 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69 6e when deallocatin
631c0 67 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73 74 g the AggInfo st
631d0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
631e0 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a 20 20 75 ct AggInfo {. u
631f0 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20 20 8 directMode;
63200 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 /* Direct
63210 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65 20 rendering mode
63220 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61 20 means take data
63230 64 69 72 65 63 74 6c 79 0a 20 20 20 20 20 20 20 directly.
63240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63250 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75 72 63 ** from sourc
63260 65 20 74 61 62 6c 65 73 20 72 61 74 68 65 72 20 e tables rather
63270 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75 6d 75 than from accumu
63280 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 38 20 75 lators */. u8 u
63290 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 20 20 20 seSortingIdx;
632a0 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 65 63 74 /* In direct
632b0 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 6e 63 65 mode, reference
632c0 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 the sorting ind
632d0 65 78 20 72 61 74 68 65 72 0a 20 20 20 20 20 20 ex rather.
632e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
632f0 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 ** than the
63300 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a source table */.
63310 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64 78 int sortingIdx
63320 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ; /* Cur
63330 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 sor number of th
63340 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 e sorting index
63350 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
63360 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 GroupBy; /*
63370 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c 61 The group by cla
63380 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6f use */. int nSo
63390 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 20 20 20 rtingColumn;
633a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f /* Number of co
633b0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 72 lumns in the sor
633c0 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 ting index */.
633d0 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 struct AggInfo_c
633e0 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f 72 20 65 ol { /* For e
633f0 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 65 64 20 ach column used
63400 69 6e 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 in source tables
63410 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 */. Table *p
63420 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 Tab;
63430 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 62 6c 65 /* Source table
63440 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 61 62 */. int iTab
63450 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 le;
63460 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 /* Cursor numbe
63470 72 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 r of the source
63480 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 table */. int
63490 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 iColumn;
634a0 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 /* Column
634b0 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 number within th
634c0 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a e source table *
634d0 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 65 /. int iSorte
634e0 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f rColumn; /
634f0 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 * Column number
63500 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 in the sorting i
63510 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ndex */. int
63520 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 iMem;
63530 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c /* Memory l
63540 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 61 63 74 ocation that act
63550 73 20 61 73 20 61 63 63 75 6d 75 6c 61 74 6f 72 s as accumulator
63560 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 */. Expr *pE
63570 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 xpr;
63580 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c /* The original
63590 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
635a0 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 6e 74 20 } *aCol;. int
635b0 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 nColumn;
635c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
635d0 20 75 73 65 64 20 65 6e 74 72 69 65 73 20 69 6e used entries in
635e0 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 aCol[] */. int
635f0 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20 20 nColumnAlloc;
63600 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
63610 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 f slots allocate
63620 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f 0a d for aCol[] */.
63630 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61 74 int nAccumulat
63640 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d or; /* Num
63650 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 ber of columns t
63660 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67 68 hat show through
63670 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 0a to the output..
63680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63690 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64 64 ** Add
636a0 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 20 itional columns
636b0 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 are used only as
636c0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 20 parameters to.
636d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
636e0 20 20 20 20 20 20 20 20 20 2a 2a 20 61 67 67 72 ** aggr
636f0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
63700 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 */. struct AggI
63710 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 2f 2a 20 nfo_func { /*
63720 46 6f 72 20 65 61 63 68 20 61 67 67 72 65 67 61 For each aggrega
63730 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 te function */.
63740 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 Expr *pExpr;
63750 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
63760 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 6f 64 69 xpression encodi
63770 6e 67 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ng the function
63780 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a */. FuncDef *
63790 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 pFunc;
637a0 2f 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 65 /* The aggregate
637b0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d function implem
637c0 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 entation */.
637d0 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 int iMem;
637e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
637f0 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 ry location that
63800 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c acts as accumul
63810 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ator */. int
63820 69 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20 iDistinct;
63830 20 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72 61 /* Ephemera
63840 6c 20 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 l table used to
63850 65 6e 66 6f 72 63 65 20 44 49 53 54 49 4e 43 54 enforce DISTINCT
63860 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e 63 3b 0a */. } *aFunc;.
63870 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 20 20 20 int nFunc;
63880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
63890 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
638a0 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 69 n aFunc[] */. i
638b0 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 3b 20 20 nt nFuncAlloc;
638c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
638d0 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 of slots alloca
638e0 74 65 64 20 66 6f 72 20 61 46 75 6e 63 5b 5d 20 ted for aFunc[]
638f0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
63900 20 64 61 74 61 74 79 70 65 20 79 6e 56 61 72 20 datatype ynVar
63910 69 73 20 61 20 73 69 67 6e 65 64 20 69 6e 74 65 is a signed inte
63920 67 65 72 2c 20 65 69 74 68 65 72 20 31 36 2d 62 ger, either 16-b
63930 69 74 20 6f 72 20 33 32 2d 62 69 74 2e 0a 2a 2a it or 32-bit..**
63940 20 55 73 75 61 6c 6c 79 20 69 74 20 69 73 20 31 Usually it is 1
63950 36 2d 62 69 74 73 2e 20 20 42 75 74 20 69 66 20 6-bits. But if
63960 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 SQLITE_MAX_VARIA
63970 42 4c 45 5f 4e 55 4d 42 45 52 20 69 73 20 67 72 BLE_NUMBER is gr
63980 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 33 32 eater.** than 32
63990 37 36 37 20 77 65 20 68 61 76 65 20 74 6f 20 6d 767 we have to m
639a0 61 6b 65 20 69 74 20 33 32 2d 62 69 74 2e 20 20 ake it 32-bit.
639b0 31 36 2d 62 69 74 20 69 73 20 70 72 65 66 65 72 16-bit is prefer
639c0 72 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 red because.** i
639d0 74 20 75 73 65 73 20 6c 65 73 73 20 6d 65 6d 6f t uses less memo
639e0 72 79 20 69 6e 20 74 68 65 20 45 78 70 72 20 6f ry in the Expr o
639f0 62 6a 65 63 74 2c 20 77 68 69 63 68 20 69 73 20 bject, which is
63a00 61 20 62 69 67 20 6d 65 6d 6f 72 79 20 75 73 65 a big memory use
63a10 72 0a 2a 2a 20 69 6e 20 73 79 73 74 65 6d 73 20 r.** in systems
63a20 77 69 74 68 20 6c 6f 74 73 20 6f 66 20 70 72 65 with lots of pre
63a30 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
63a40 2e 20 20 41 6e 64 20 66 65 77 20 61 70 70 6c 69 . And few appli
63a50 63 61 74 69 6f 6e 73 0a 2a 2a 20 6e 65 65 64 20 cations.** need
63a60 6d 6f 72 65 20 74 68 61 6e 20 61 62 6f 75 74 20 more than about
63a70 31 30 20 6f 72 20 32 30 20 76 61 72 69 61 62 6c 10 or 20 variabl
63a80 65 73 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 78 es. But some ex
63a90 74 72 65 6d 65 20 75 73 65 72 73 20 77 61 6e 74 treme users want
63aa0 0a 2a 2a 20 74 6f 20 68 61 76 65 20 70 72 65 70 .** to have prep
63ab0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 ared statements
63ac0 77 69 74 68 20 6f 76 65 72 20 33 32 37 36 37 20 with over 32767
63ad0 76 61 72 69 61 62 6c 65 73 2c 20 61 6e 64 20 66 variables, and f
63ae0 6f 72 20 74 68 65 6d 0a 2a 2a 20 74 68 65 20 6f or them.** the o
63af0 70 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 ption is availab
63b00 6c 65 20 28 61 74 20 63 6f 6d 70 69 6c 65 2d 74 le (at compile-t
63b10 69 6d 65 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ime)..*/.#if SQL
63b20 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 ITE_MAX_VARIABLE
63b30 5f 4e 55 4d 42 45 52 3c 3d 33 32 37 36 37 0a 74 _NUMBER<=32767.t
63b40 79 70 65 64 65 66 20 69 31 36 20 79 6e 56 61 72 ypedef i16 ynVar
63b50 3b 0a 23 65 6c 73 65 0a 74 79 70 65 64 65 66 20 ;.#else.typedef
63b60 69 6e 74 20 79 6e 56 61 72 3b 0a 23 65 6e 64 69 int ynVar;.#endi
63b70 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f f../*.** Each no
63b80 64 65 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 de of an express
63b90 69 6f 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65 ion in the parse
63ba0 20 74 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74 tree is an inst
63bb0 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 ance.** of this
63bc0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
63bd0 20 45 78 70 72 2e 6f 70 20 69 73 20 74 68 65 20 Expr.op is the
63be0 6f 70 63 6f 64 65 2e 20 54 68 65 20 69 6e 74 65 opcode. The inte
63bf0 67 65 72 20 70 61 72 73 65 72 20 74 6f 6b 65 6e ger parser token
63c00 20 63 6f 64 65 73 20 61 72 65 20 72 65 75 73 65 codes are reuse
63c10 64 0a 2a 2a 20 61 73 20 6f 70 63 6f 64 65 73 20 d.** as opcodes
63c20 68 65 72 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c here. For exampl
63c30 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 64 65 e, the parser de
63c40 66 69 6e 65 73 20 54 4b 5f 47 45 20 74 6f 20 62 fines TK_GE to b
63c50 65 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 e an integer.**
63c60 63 6f 64 65 20 72 65 70 72 65 73 65 6e 74 69 6e code representin
63c70 67 20 74 68 65 20 22 3e 3d 22 20 6f 70 65 72 61 g the ">=" opera
63c80 74 6f 72 2e 20 54 68 69 73 20 73 61 6d 65 20 69 tor. This same i
63c90 6e 74 65 67 65 72 20 63 6f 64 65 20 69 73 20 72 nteger code is r
63ca0 65 75 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72 eused.** to repr
63cb0 65 73 65 6e 74 20 74 68 65 20 67 72 65 61 74 65 esent the greate
63cc0 72 2d 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d r-than-or-equal-
63cd0 74 6f 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74 to operator in t
63ce0 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
63cf0 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 tree..**.** If
63d00 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
63d10 73 20 61 6e 20 53 51 4c 20 6c 69 74 65 72 61 6c s an SQL literal
63d20 20 28 54 4b 5f 49 4e 54 45 47 45 52 2c 20 54 4b (TK_INTEGER, TK
63d30 5f 46 4c 4f 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c _FLOAT, TK_BLOB,
63d40 20 0a 2a 2a 20 6f 72 20 54 4b 5f 53 54 52 49 4e .** or TK_STRIN
63d50 47 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f G), then Expr.to
63d60 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ken contains the
63d70 20 74 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c text of the SQL
63d80 20 6c 69 74 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 literal. If.**
63d90 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
63da0 73 20 61 20 76 61 72 69 61 62 6c 65 20 28 54 4b s a variable (TK
63db0 5f 56 41 52 49 41 42 4c 45 29 2c 20 74 68 65 6e _VARIABLE), then
63dc0 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 Expr.token cont
63dd0 61 69 6e 73 20 74 68 65 20 0a 2a 2a 20 76 61 72 ains the .** var
63de0 69 61 62 6c 65 20 6e 61 6d 65 2e 20 46 69 6e 61 iable name. Fina
63df0 6c 6c 79 2c 20 69 66 20 74 68 65 20 65 78 70 72 lly, if the expr
63e00 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c ession is an SQL
63e10 20 66 75 6e 63 74 69 6f 6e 20 28 54 4b 5f 46 55 function (TK_FU
63e20 4e 43 54 49 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e NCTION),.** then
63e30 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 Expr.token cont
63e40 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 ains the name of
63e50 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a the function..*
63e60 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74 *.** Expr.pRight
63e70 20 61 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20 and Expr.pLeft
63e80 61 72 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 are the left and
63e90 20 72 69 67 68 74 20 73 75 62 65 78 70 72 65 73 right subexpres
63ea0 73 69 6f 6e 73 20 6f 66 20 61 0a 2a 2a 20 62 69 sions of a.** bi
63eb0 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45 nary operator. E
63ec0 69 74 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61 ither or both ma
63ed0 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a y be NULL..**.**
63ee0 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 Expr.x.pList is
63ef0 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d a list of argum
63f00 65 6e 74 73 20 69 66 20 74 68 65 20 65 78 70 72 ents if the expr
63f10 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c ession is an SQL
63f20 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 function,.** a
63f30 43 41 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20 CASE expression
63f40 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73 73 or an IN express
63f50 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ion of the form
63f60 22 3c 6c 68 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 "<lhs> IN (<y>,
63f70 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 <z>...)"..** Exp
63f80 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 75 r.x.pSelect is u
63f90 73 65 64 20 69 66 20 74 68 65 20 65 78 70 72 65 sed if the expre
63fa0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 ssion is a sub-s
63fb0 65 6c 65 63 74 20 6f 72 20 61 6e 20 65 78 70 72 elect or an expr
63fc0 65 73 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 ession of.** the
63fd0 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 form "<lhs> IN
63fe0 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 20 49 (SELECT ...)". I
63ff0 66 20 74 68 65 20 45 50 5f 78 49 73 53 65 6c 65 f the EP_xIsSele
64000 63 74 20 62 69 74 20 69 73 20 73 65 74 20 69 6e ct bit is set in
64010 20 74 68 65 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 the.** Expr.fla
64020 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 45 78 gs mask, then Ex
64030 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 pr.x.pSelect is
64040 76 61 6c 69 64 2e 20 4f 74 68 65 72 77 69 73 65 valid. Otherwise
64050 2c 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 , Expr.x.pList i
64060 73 20 0a 2a 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a s .** valid..**.
64070 2a 2a 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e ** An expression
64080 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 49 44 20 of the form ID
64090 6f 72 20 49 44 2e 49 44 20 72 65 66 65 72 73 20 or ID.ID refers
640a0 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 to a column in a
640b0 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 table..** For s
640c0 75 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73 2c uch expressions,
640d0 20 45 78 70 72 2e 6f 70 20 69 73 20 73 65 74 20 Expr.op is set
640e0 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 to TK_COLUMN and
640f0 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 0a Expr.iTable is.
64100 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 ** the integer c
64110 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 ursor number of
64120 61 20 56 44 42 45 20 63 75 72 73 6f 72 20 70 6f a VDBE cursor po
64130 69 6e 74 69 6e 67 20 74 6f 20 74 68 61 74 20 74 inting to that t
64140 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 78 70 72 able and.** Expr
64150 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 .iColumn is the
64160 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f column number fo
64170 72 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63 r the specific c
64180 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 0a 2a olumn. If the.*
64190 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 * expression is
641a0 75 73 65 64 20 61 73 20 61 20 72 65 73 75 6c 74 used as a result
641b0 20 69 6e 20 61 6e 20 61 67 67 72 65 67 61 74 65 in an aggregate
641c0 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 74 68 SELECT, then th
641d0 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 61 6c e.** value is al
641e0 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 so stored in the
641f0 20 45 78 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d Expr.iAgg colum
64200 6e 20 69 6e 20 74 68 65 20 61 67 67 72 65 67 61 n in the aggrega
64210 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 te so that.** it
64220 20 63 61 6e 20 62 65 20 61 63 63 65 73 73 65 64 can be accessed
64230 20 61 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65 after all aggre
64240 67 61 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74 gates are comput
64250 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
64260 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
64270 6e 20 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62 n unbound variab
64280 6c 65 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65 le marker (a que
64290 73 74 69 6f 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 stion mark .** c
642a0 68 61 72 61 63 74 65 72 20 27 3f 27 20 69 6e 20 haracter '?' in
642b0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c the original SQL
642c0 29 20 74 68 65 6e 20 74 68 65 20 45 78 70 72 2e ) then the Expr.
642d0 69 54 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 iTable holds the
642e0 20 69 6e 64 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 index .** numbe
642f0 72 20 66 6f 72 20 74 68 61 74 20 76 61 72 69 61 r for that varia
64300 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ble..**.** If th
64310 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
64320 61 20 73 75 62 71 75 65 72 79 20 74 68 65 6e 20 a subquery then
64330 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c Expr.iColumn hol
64340 64 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a ds an integer.**
64350 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
64360 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
64370 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 73 75 result of the su
64380 62 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 0a bquery. If the.
64390 2a 2a 20 73 75 62 71 75 65 72 79 20 67 69 76 65 ** subquery give
643a0 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 72 65 73 s a constant res
643b0 75 6c 74 2c 20 74 68 65 6e 20 69 54 61 62 6c 65 ult, then iTable
643c0 20 69 73 20 2d 31 2e 20 20 49 66 20 74 68 65 20 is -1. If the
643d0 73 75 62 71 75 65 72 79 0a 2a 2a 20 67 69 76 65 subquery.** give
643e0 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 6e s a different an
643f0 73 77 65 72 20 61 74 20 64 69 66 66 65 72 65 6e swer at differen
64400 74 20 74 69 6d 65 73 20 64 75 72 69 6e 67 20 73 t times during s
64410 74 61 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73 tatement process
64420 69 6e 67 0a 2a 2a 20 74 68 65 6e 20 69 54 61 62 ing.** then iTab
64430 6c 65 20 69 73 20 74 68 65 20 61 64 64 72 65 73 le is the addres
64440 73 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e s of a subroutin
64450 65 20 74 68 61 74 20 63 6f 6d 70 75 74 65 73 20 e that computes
64460 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a the subquery..**
64470 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72 20 .** If the Expr
64480 69 73 20 6f 66 20 74 79 70 65 20 4f 50 5f 43 6f is of type OP_Co
64490 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65 20 74 61 lumn, and the ta
644a0 62 6c 65 20 69 74 20 69 73 20 73 65 6c 65 63 74 ble it is select
644b0 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 ing from.** is a
644c0 20 64 69 73 6b 20 74 61 62 6c 65 20 6f 72 20 74 disk table or t
644d0 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73 65 75 64 he "old.*" pseud
644e0 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 54 o-table, then pT
644f0 61 62 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 ab points to the
64500 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
64510 67 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 g table definiti
64520 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 on..**.** ALLOCA
64530 54 49 4f 4e 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a TION NOTES:.**.*
64540 2a 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63 * Expr objects c
64550 61 6e 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20 an use a lot of
64560 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20 memory space in
64570 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e database schema.
64580 20 20 54 6f 0a 2a 2a 20 68 65 6c 70 20 72 65 64 To.** help red
64590 75 63 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 uce memory requi
645a0 72 65 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69 6d rements, sometim
645b0 65 73 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 es an Expr objec
645c0 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 t will be.** tru
645d0 6e 63 61 74 65 64 2e 20 20 41 6e 64 20 74 6f 20 ncated. And to
645e0 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 reduce the numbe
645f0 72 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f r of memory allo
64600 63 61 74 69 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d cations, sometim
64610 65 73 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 es.** two or mor
64620 65 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 77 e Expr objects w
64630 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 69 6e ill be stored in
64640 20 61 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 a single memory
64650 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 allocation,.**
64660 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 45 78 together with Ex
64670 70 72 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 pr.zToken string
64680 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 s..**.** If the
64690 45 50 5f 52 65 64 75 63 65 64 20 61 6e 64 20 45 EP_Reduced and E
646a0 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 P_TokenOnly flag
646b0 73 20 61 72 65 20 73 65 74 20 77 68 65 6e 0a 2a s are set when.*
646c0 2a 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 * an Expr object
646d0 20 69 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 is truncated.
646e0 57 68 65 6e 20 45 50 5f 52 65 64 75 63 65 64 20 When EP_Reduced
646f0 69 73 20 73 65 74 2c 20 74 68 65 6e 20 61 6c 6c is set, then all
64700 0a 2a 2a 20 74 68 65 20 63 68 69 6c 64 20 45 78 .** the child Ex
64710 70 72 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 pr objects in th
64720 65 20 45 78 70 72 2e 70 4c 65 66 74 20 61 6e 64 e Expr.pLeft and
64730 20 45 78 70 72 2e 70 52 69 67 68 74 20 73 75 62 Expr.pRight sub
64740 74 72 65 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e trees.** are con
64750 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 tained within th
64760 65 20 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c e same memory al
64770 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c location. Note,
64780 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 2a however, that.*
64790 2a 20 74 68 65 20 73 75 62 74 72 65 65 73 20 69 * the subtrees i
647a0 6e 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f n Expr.x.pList o
647b0 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 r Expr.x.pSelect
647c0 20 61 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 are always sepa
647d0 72 61 74 65 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 rately.** alloca
647e0 74 65 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 ted, regardless
647f0 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f of whether or no
64800 74 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 t EP_Reduced is
64810 73 65 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 set..*/.struct E
64820 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b 20 20 xpr {. u8 op;
64830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
64840 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66 * Operation perf
64850 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f ormed by this no
64860 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 de */. char aff
64870 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f inity; /
64880 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f * The affinity o
64890 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 f the column or
648a0 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 0 if not a colum
648b0 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 n */. u16 flags
648c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
648d0 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20 Various flags.
648e0 20 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20 EP_* See below
648f0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 */. union {.
64900 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 char *zToken;
64910 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e /* Token
64920 20 76 61 6c 75 65 2e 20 5a 65 72 6f 20 74 65 72 value. Zero ter
64930 6d 69 6e 61 74 65 64 20 61 6e 64 20 64 65 71 75 minated and dequ
64940 6f 74 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 oted */. int
64950 69 56 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 iValue;
64960 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 /* Integer va
64970 6c 75 65 20 69 66 20 45 50 5f 49 6e 74 56 61 6c lue if EP_IntVal
64980 75 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 ue */. } u;..
64990 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 54 6f 6b /* If the EP_Tok
649a0 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73 20 73 enOnly flag is s
649b0 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 et in the Expr.f
649c0 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 lags mask, then
649d0 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 no. ** space is
649e0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
649f0 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 he fields below
64a00 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 this point. An a
64a10 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 ttempt to. ** a
64a20 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 ccess them will
64a30 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 result in a segf
64a40 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 ault or malfunct
64a50 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a ion. . ********
64a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
64aa0 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 Expr *pLeft;
64ab0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 /* Left
64ac0 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 subnode */. Ex
64ad0 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 pr *pRight;
64ae0 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 73 75 /* Right su
64af0 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e bnode */. union
64b00 20 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 {. ExprList
64b10 2a 70 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 46 *pList; /* F
64b20 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 unction argument
64b30 73 20 6f 72 20 69 6e 20 22 3c 65 78 70 72 3e 20 s or in "<expr>
64b40 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 74 29 22 IN (<expr-list)"
64b50 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a */. Select *
64b60 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 pSelect; /*
64b70 55 73 65 64 20 66 6f 72 20 73 75 62 2d 73 65 6c Used for sub-sel
64b80 65 63 74 73 20 61 6e 64 20 22 3c 65 78 70 72 3e ects and "<expr>
64b90 20 49 4e 20 28 3c 73 65 6c 65 63 74 3e 29 22 20 IN (<select>)"
64ba0 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c */. } x;. Coll
64bb0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 Seq *pColl;
64bc0 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 /* The collat
64bd0 69 6f 6e 20 74 79 70 65 20 6f 66 20 74 68 65 20 ion type of the
64be0 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a column or 0 */..
64bf0 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 52 /* If the EP_R
64c00 65 64 75 63 65 64 20 66 6c 61 67 20 69 73 20 73 educed flag is s
64c10 65 74 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 et in the Expr.f
64c20 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 lags mask, then
64c30 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20 69 73 no. ** space is
64c40 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
64c50 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f 77 20 he fields below
64c60 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e 20 61 this point. An a
64c70 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a 20 61 ttempt to. ** a
64c80 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c 6c 20 ccess them will
64c90 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 66 result in a segf
64ca0 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e 63 74 ault or malfunct
64cb0 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ion.. *********
64cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 ************/..
64d00 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 int iTable;
64d10 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f /* TK_CO
64d20 4c 55 4d 4e 3a 20 63 75 72 73 6f 72 20 6e 75 6d LUMN: cursor num
64d30 62 65 72 20 6f 66 20 74 61 62 6c 65 20 68 6f 6c ber of table hol
64d40 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 ding column.
64d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64d60 20 20 20 20 20 2a 2a 20 54 4b 5f 52 45 47 49 53 ** TK_REGIS
64d70 54 45 52 3a 20 72 65 67 69 73 74 65 72 20 6e 75 TER: register nu
64d80 6d 62 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 mber.
64d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
64da0 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 31 20 2d TK_TRIGGER: 1 -
64db0 3e 20 6e 65 77 2c 20 30 20 2d 3e 20 6f 6c 64 20 > new, 0 -> old
64dc0 2a 2f 0a 20 20 79 6e 56 61 72 20 69 43 6f 6c 75 */. ynVar iColu
64dd0 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 mn; /* T
64de0 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 6f 6c 75 6d 6e K_COLUMN: column
64df0 20 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20 index. -1 for
64e00 72 6f 77 69 64 2e 0a 20 20 20 20 20 20 20 20 20 rowid..
64e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64e20 2a 2a 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 ** TK_VARIABLE:
64e30 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 variable number
64e40 28 61 6c 77 61 79 73 20 3e 3d 20 31 29 2e 20 2a (always >= 1). *
64e50 2f 0a 20 20 69 31 36 20 69 41 67 67 3b 20 20 20 /. i16 iAgg;
64e60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 /* Wh
64e70 69 63 68 20 65 6e 74 72 79 20 69 6e 20 70 41 67 ich entry in pAg
64e80 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72 gInfo->aCol[] or
64e90 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 ->aFunc[] */.
64ea0 69 31 36 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 i16 iRightJoinTa
64eb0 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 45 50 5f ble; /* If EP_
64ec0 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 20 72 69 FromJoin, the ri
64ed0 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 ght table of the
64ee0 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 38 20 66 6c join */. u8 fl
64ef0 61 67 73 32 3b 20 20 20 20 20 20 20 20 20 20 20 ags2;
64f00 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73 65 74 20 /* Second set
64f10 6f 66 20 66 6c 61 67 73 2e 20 20 45 50 32 5f 2e of flags. EP2_.
64f20 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f 70 32 3b 20 .. */. u8 op2;
64f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
64f40 2a 20 49 66 20 61 20 54 4b 5f 52 45 47 49 53 54 * If a TK_REGIST
64f50 45 52 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c ER, the original
64f60 20 76 61 6c 75 65 20 6f 66 20 45 78 70 72 2e 6f value of Expr.o
64f70 70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a p */. AggInfo *
64f80 70 41 67 67 49 6e 66 6f 3b 20 20 20 20 20 2f 2a pAggInfo; /*
64f90 20 55 73 65 64 20 62 79 20 54 4b 5f 41 47 47 5f Used by TK_AGG_
64fa0 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b 5f 41 47 COLUMN and TK_AG
64fb0 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f 0a 20 20 G_FUNCTION */.
64fc0 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
64fd0 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 /* Table
64fe0 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 for TK_COLUMN ex
64ff0 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 pressions. */.#i
65000 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 f SQLITE_MAX_EXP
65010 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 R_DEPTH>0. int
65020 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 nHeight;
65030 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 /* Height of
65040 74 68 65 20 74 72 65 65 20 68 65 61 64 65 64 20 the tree headed
65050 62 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a by this node */.
65060 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
65070 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 The following a
65080 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 re the meanings
65090 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45 of bits in the E
650a0 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e xpr.flags field.
650b0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 .*/.#define EP_F
650c0 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 romJoin 0x0001
650d0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 /* Originated
650e0 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 in ON or USING c
650f0 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 lause of a join
65100 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 67 */.#define EP_Ag
65110 67 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20 g 0x0002
65120 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 /* Contains one
65130 20 6f 72 20 6d 6f 72 65 20 61 67 67 72 65 67 61 or more aggrega
65140 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a te functions */.
65150 23 64 65 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c #define EP_Resol
65160 76 65 64 20 20 20 30 78 30 30 30 34 20 20 2f 2a ved 0x0004 /*
65170 20 49 44 73 20 68 61 76 65 20 62 65 65 6e 20 72 IDs have been r
65180 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d esolved to COLUM
65190 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 Ns */.#define EP
651a0 5f 45 72 72 6f 72 20 20 20 20 20 20 30 78 30 30 _Error 0x00
651b0 30 38 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 08 /* Expressio
651c0 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f n contains one o
651d0 72 20 6d 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f r more errors */
651e0 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 69 73 74 .#define EP_Dist
651f0 69 6e 63 74 20 20 20 30 78 30 30 31 30 20 20 2f inct 0x0010 /
65200 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 * Aggregate func
65210 74 69 6f 6e 20 77 69 74 68 20 44 49 53 54 49 4e tion with DISTIN
65220 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 CT keyword */.#d
65230 65 66 69 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 efine EP_VarSele
65240 63 74 20 20 30 78 30 30 32 30 20 20 2f 2a 20 70 ct 0x0020 /* p
65250 53 65 6c 65 63 74 20 69 73 20 63 6f 72 72 65 6c Select is correl
65260 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 ated, not consta
65270 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 nt */.#define EP
65280 5f 44 62 6c 51 75 6f 74 65 64 20 20 30 78 30 30 _DblQuoted 0x00
65290 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77 40 /* token.z w
652a0 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69 6e as originally in
652b0 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 66 69 6e "..." */.#defin
652c0 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 20 20 e EP_InfixFunc
652d0 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75 65 20 0x0080 /* True
652e0 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66 75 6e for an infix fun
652f0 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f ction: LIKE, GLO
65300 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66 69 6e B, etc */.#defin
65310 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 e EP_ExpCollate
65320 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 0x0100 /* Colla
65330 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70 ting sequence sp
65340 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74 ecified explicit
65350 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ly */.#define EP
65360 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78 30 32 _AnyAff 0x02
65370 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b 65 20 00 /* Can take
65380 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 a cached column
65390 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20 of any affinity
653a0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 69 */.#define EP_Fi
653b0 78 65 64 44 65 73 74 20 20 30 78 30 34 30 30 20 xedDest 0x0400
653c0 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65 64 65 /* Result neede
653d0 64 20 69 6e 20 61 20 73 70 65 63 69 66 69 63 20 d in a specific
653e0 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64 65 66 register */.#def
653f0 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 ine EP_IntValue
65400 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49 6e 74 0x0800 /* Int
65410 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e 74 61 eger value conta
65420 69 6e 65 64 20 69 6e 20 75 2e 69 56 61 6c 75 65 ined in u.iValue
65430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 78 */.#define EP_x
65440 49 73 53 65 6c 65 63 74 20 20 30 78 31 30 30 30 IsSelect 0x1000
65450 20 20 2f 2a 20 78 2e 70 53 65 6c 65 63 74 20 69 /* x.pSelect i
65460 73 20 76 61 6c 69 64 20 28 6f 74 68 65 72 77 69 s valid (otherwi
65470 73 65 20 78 2e 70 4c 69 73 74 20 69 73 29 20 2a se x.pList is) *
65480 2f 0a 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65 /..#define EP_Re
65490 64 75 63 65 64 20 20 20 20 30 78 32 30 30 30 20 duced 0x2000
654a0 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 74 20 /* Expr struct
654b0 69 73 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 is EXPR_REDUCEDS
654c0 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a IZE bytes only *
654d0 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 54 6f 6b /.#define EP_Tok
654e0 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30 30 20 20 enOnly 0x4000
654f0 2f 2a 20 45 78 70 72 20 73 74 72 75 63 74 20 69 /* Expr struct i
65500 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 s EXPR_TOKENONLY
65510 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c 79 20 SIZE bytes only
65520 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 53 74 */.#define EP_St
65530 61 74 69 63 20 20 20 20 20 30 78 38 30 30 30 20 atic 0x8000
65540 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d 65 6d 6f /* Held in memo
65550 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 ry not obtained
65560 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20 2a 2f from malloc() */
65570 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
65580 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65 owing are the me
65590 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69 anings of bits i
655a0 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 n the Expr.flags
655b0 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 2 field..*/.#def
655c0 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 ine EP2_Malloced
655d0 54 6f 6b 65 6e 20 20 30 78 30 30 30 31 20 20 2f Token 0x0001 /
655e0 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c 69 74 65 * Need to sqlite
655f0 33 44 62 46 72 65 65 28 29 20 45 78 70 72 2e 7a 3DbFree() Expr.z
65600 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 Token */.#define
65610 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 EP2_Irreducible
65620 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43 0x0002 /* C
65630 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 5f 52 45 annot EXPRDUP_RE
65640 44 55 43 45 20 74 68 69 73 20 45 78 70 72 20 2a DUCE this Expr *
65650 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 73 65 /../*.** The pse
65660 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73 71 6c 69 udo-routine sqli
65670 74 65 33 45 78 70 72 53 65 74 49 72 72 65 64 75 te3ExprSetIrredu
65680 63 69 62 6c 65 20 73 65 74 73 20 74 68 65 20 45 cible sets the E
65690 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 0a 2a P2_Irreducible.*
656a0 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20 65 78 70 * flag on an exp
656b0 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 ression structur
656c0 65 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 e. This flag is
656d0 20 75 73 65 64 20 66 6f 72 20 56 56 26 41 20 6f used for VV&A o
656e0 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 72 6f 75 nly. The.** rou
656f0 74 69 6e 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e tine is implemen
65700 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f 20 74 ted as a macro t
65710 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 77 hat only works w
65720 68 65 6e 20 69 6e 20 64 65 62 75 67 67 69 6e 67 hen in debugging
65730 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20 61 73 20 mode,.** so as
65740 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e 20 70 72 not to burden pr
65750 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65 2e 0a 2a oduction code..*
65760 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
65770 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 45 DEBUG.# define E
65780 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c xprSetIrreducibl
65790 65 28 58 29 20 20 28 58 29 2d 3e 66 6c 61 67 73 e(X) (X)->flags
657a0 32 20 7c 3d 20 45 50 32 5f 49 72 72 65 64 75 63 2 |= EP2_Irreduc
657b0 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20 64 65 66 ible.#else.# def
657c0 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 65 64 ine ExprSetIrred
657d0 75 63 69 62 6c 65 28 58 29 0a 23 65 6e 64 69 66 ucible(X).#endif
657e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 ../*.** These ma
657f0 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 cros can be used
65800 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f to test, set, o
65810 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 r clear bits in
65820 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 the .** Expr.fla
65830 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 gs field..*/.#de
65840 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 6f 70 fine ExprHasProp
65850 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28 28 erty(E,P) ((
65860 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d (E)->flags&(P))=
65870 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 45 78 =(P)).#define Ex
65880 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
65890 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e 66 6c (E,P) (((E)->fl
658a0 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 ags&(P))!=0).#de
658b0 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 6f 70 fine ExprSetProp
658c0 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28 45 erty(E,P) (E
658d0 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 )->flags|=(P).#d
658e0 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 72 50 efine ExprClearP
658f0 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 28 roperty(E,P) (
65900 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a E)->flags&=~(P).
65910 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f ./*.** Macros to
65920 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e determine the n
65930 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
65940 65 71 75 69 72 65 64 20 62 79 20 61 20 6e 6f 72 equired by a nor
65950 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 73 74 72 mal Expr .** str
65960 75 63 74 2c 20 61 6e 20 45 78 70 72 20 73 74 72 uct, an Expr str
65970 75 63 74 20 77 69 74 68 20 74 68 65 20 45 50 5f uct with the EP_
65980 52 65 64 75 63 65 64 20 66 6c 61 67 20 73 65 74 Reduced flag set
65990 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73 20 0a in Expr.flags .
659a0 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 72 20 73 ** and an Expr s
659b0 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 45 truct with the E
659c0 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 P_TokenOnly flag
659d0 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 set..*/.#define
659e0 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 20 20 EXPR_FULLSIZE
659f0 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 sizeof(
65a00 45 78 70 72 29 20 20 20 20 20 20 20 20 20 20 20 Expr)
65a10 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 2a 2f 0a /* Full size */.
65a20 23 64 65 66 69 6e 65 20 45 58 50 52 5f 52 45 44 #define EXPR_RED
65a30 55 43 45 44 53 49 5a 45 20 20 20 20 20 20 20 20 UCEDSIZE
65a40 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 69 54 offsetof(Expr,iT
65a50 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d 6d 6f 6e able) /* Common
65a60 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 64 65 features */.#de
65a70 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f fine EXPR_TOKENO
65a80 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 6f 66 66 NLYSIZE off
65a90 73 65 74 6f 66 28 45 78 70 72 2c 70 4c 65 66 74 setof(Expr,pLeft
65aa0 29 20 20 20 2f 2a 20 46 65 77 65 72 20 66 65 61 ) /* Fewer fea
65ab0 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 tures */../*.**
65ac0 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 Flags passed to
65ad0 74 68 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 the sqlite3ExprD
65ae0 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 53 up() function. S
65af0 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f ee the header co
65b00 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f 76 65 20 mment .** above
65b10 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 sqlite3ExprDup()
65b20 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f for details..*/
65b30 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 55 50 .#define EXPRDUP
65b40 5f 52 45 44 55 43 45 20 20 20 20 20 20 20 20 20 _REDUCE
65b50 30 78 30 30 30 31 20 20 2f 2a 20 55 73 65 64 20 0x0001 /* Used
65b60 72 65 64 75 63 65 64 2d 73 69 7a 65 20 45 78 70 reduced-size Exp
65b70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a r nodes */../*.*
65b80 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 * A list of expr
65b90 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 68 20 65 essions. Each e
65ba0 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20 6f 70 xpression may op
65bb0 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 20 61 0a tionally have a.
65bc0 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 65 78 70 ** name. An exp
65bd0 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e 61 74 69 r/name combinati
65be0 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 on can be used i
65bf0 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73 2c 20 n several ways,
65c00 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 65 20 6c such.** as the l
65c10 69 73 74 20 6f 66 20 22 65 78 70 72 20 41 53 20 ist of "expr AS
65c20 49 44 22 20 66 69 65 6c 64 73 20 66 6f 6c 6c 6f ID" fields follo
65c30 77 69 6e 67 20 61 20 22 53 45 4c 45 43 54 22 20 wing a "SELECT"
65c40 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 69 73 or in the.** lis
65c50 74 20 6f 66 20 22 49 44 20 3d 20 65 78 70 72 22 t of "ID = expr"
65c60 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 55 50 44 items in an UPD
65c70 41 54 45 2e 20 20 41 20 6c 69 73 74 20 6f 66 20 ATE. A list of
65c80 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 0a expressions can.
65c90 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 ** also be used
65ca0 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 as the argument
65cb0 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 69 to a function, i
65cc0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
65cd0 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c a.zName.** fiel
65ce0 64 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a d is not used..*
65cf0 2f 0a 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 /.struct ExprLis
65d00 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b t {. int nExpr;
65d10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65d20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 72 65 73 Number of expres
65d30 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c 69 73 sions on the lis
65d40 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f t */. int nAllo
65d50 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a c; /*
65d60 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
65d70 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 6c es allocated bel
65d80 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 45 43 75 ow */. int iECu
65d90 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f rsor; /
65da0 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 61 73 * VDBE Cursor as
65db0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
65dc0 69 73 20 45 78 70 72 4c 69 73 74 20 2a 2f 0a 20 is ExprList */.
65dd0 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
65de0 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45 78 70 72 _item {. Expr
65df0 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 *pExpr;
65e00 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 6f /* The list o
65e10 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f f expressions */
65e20 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 . char *zName
65e30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
65e40 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74 65 64 20 oken associated
65e50 77 69 74 68 20 74 68 69 73 20 65 78 70 72 65 73 with this expres
65e60 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 sion */. char
65e70 20 2a 7a 53 70 61 6e 3b 20 20 20 20 20 20 20 20 *zSpan;
65e80 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74 /* Original t
65e90 65 78 74 20 6f 66 20 74 68 65 20 65 78 70 72 65 ext of the expre
65ea0 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 38 20 ssion */. u8
65eb0 73 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 20 sortOrder;
65ec0 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 44 45 53 /* 1 for DES
65ed0 43 20 6f 72 20 30 20 66 6f 72 20 41 53 43 20 2a C or 0 for ASC *
65ee0 2f 0a 20 20 20 20 75 38 20 64 6f 6e 65 3b 20 20 /. u8 done;
65ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65f00 41 20 66 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 A flag to indica
65f10 74 65 20 77 68 65 6e 20 70 72 6f 63 65 73 73 69 te when processi
65f20 6e 67 20 69 73 20 66 69 6e 69 73 68 65 64 20 2a ng is finished *
65f30 2f 0a 20 20 20 20 75 31 36 20 69 43 6f 6c 3b 20 /. u16 iCol;
65f40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
65f50 46 6f 72 20 4f 52 44 45 52 20 42 59 2c 20 63 6f For ORDER BY, co
65f60 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 72 lumn number in r
65f70 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 esult set */.
65f80 20 75 31 36 20 69 41 6c 69 61 73 3b 20 20 20 20 u16 iAlias;
65f90 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
65fa0 20 69 6e 74 6f 20 50 61 72 73 65 2e 61 41 6c 69 into Parse.aAli
65fb0 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 6d 65 20 2a as[] for zName *
65fc0 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 20 20 20 20 /. } *a;
65fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
65fe0 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 e entry for each
65ff0 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d expression */.}
66000 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 ;../*.** An inst
66010 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
66020 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 62 ucture is used b
66030 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 y the parser to
66040 72 65 63 6f 72 64 20 62 6f 74 68 0a 2a 2a 20 74 record both.** t
66050 68 65 20 70 61 72 73 65 20 74 72 65 65 20 66 6f he parse tree fo
66060 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 r an expression
66070 61 6e 64 20 74 68 65 20 73 70 61 6e 20 6f 66 20 and the span of
66080 69 6e 70 75 74 20 74 65 78 74 20 66 6f 72 20 61 input text for a
66090 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e n.** expression.
660a0 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 53 .*/.struct ExprS
660b0 70 61 6e 20 7b 0a 20 20 45 78 70 72 20 2a 70 45 pan {. Expr *pE
660c0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a xpr; /*
660d0 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
660e0 70 61 72 73 65 20 74 72 65 65 20 2a 2f 0a 20 20 parse tree */.
660f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 const char *zSta
66100 72 74 3b 20 20 20 2f 2a 20 46 69 72 73 74 20 63 rt; /* First c
66110 68 61 72 61 63 74 65 72 20 6f 66 20 69 6e 70 75 haracter of inpu
66120 74 20 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 t text */. cons
66130 74 20 63 68 61 72 20 2a 7a 45 6e 64 3b 20 20 20 t char *zEnd;
66140 20 20 2f 2a 20 4f 6e 65 20 63 68 61 72 61 63 74 /* One charact
66150 65 72 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 er past the end
66160 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 2a 2f of input text */
66170 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e .};../*.** An in
66180 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
66190 74 72 75 63 74 75 72 65 20 63 61 6e 20 68 6f 6c tructure can hol
661a0 64 20 61 20 73 69 6d 70 6c 65 20 6c 69 73 74 20 d a simple list
661b0 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 2c 0a of identifiers,.
661c0 2a 2a 20 73 75 63 68 20 61 73 20 74 68 65 20 6c ** such as the l
661d0 69 73 74 20 22 61 2c 62 2c 63 22 20 69 6e 20 74 ist "a,b,c" in t
661e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 he following sta
661f0 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 tements:.**.**
66200 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 INSERT INTO
66210 74 28 61 2c 62 2c 63 29 20 56 41 4c 55 45 53 20 t(a,b,c) VALUES
66220 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 ...;.** CRE
66230 41 54 45 20 49 4e 44 45 58 20 69 64 78 20 4f 4e ATE INDEX idx ON
66240 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a 20 20 20 t(a,b,c);.**
66250 20 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 CREATE TRIGGE
66260 52 20 74 72 69 67 20 42 45 46 4f 52 45 20 55 50 R trig BEFORE UP
66270 44 41 54 45 20 4f 4e 20 74 28 61 2c 62 2c 63 29 DATE ON t(a,b,c)
66280 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ...;.**.** The
66290 49 64 4c 69 73 74 2e 61 2e 69 64 78 20 66 69 65 IdList.a.idx fie
662a0 6c 64 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 ld is used when
662b0 74 68 65 20 49 64 4c 69 73 74 20 72 65 70 72 65 the IdList repre
662c0 73 65 6e 74 73 20 74 68 65 20 6c 69 73 74 20 6f sents the list o
662d0 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 f.** column name
662e0 73 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 20 s after a table
662f0 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e 53 45 52 name in an INSER
66300 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e T statement. In
66310 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a the statement.*
66320 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20 *.** INSERT
66330 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 2e 2e INTO t(a,b,c) ..
66340 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61 22 20 69 ..**.** If "a" i
66350 73 20 74 68 65 20 6b 2d 74 68 20 63 6f 6c 75 6d s the k-th colum
66360 6e 20 6f 66 20 74 61 62 6c 65 20 22 74 22 2c 20 n of table "t",
66370 74 68 65 6e 20 49 64 4c 69 73 74 2e 61 5b 30 5d then IdList.a[0]
66380 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73 74 72 75 .idx==k..*/.stru
66390 63 74 20 49 64 4c 69 73 74 20 7b 0a 20 20 73 74 ct IdList {. st
663a0 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d ruct IdList_item
663b0 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 {. char *zNa
663c0 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 me; /* Name
663d0 20 6f 66 20 74 68 65 20 69 64 65 6e 74 69 66 69 of the identifi
663e0 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 64 er */. int id
663f0 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 x; /* I
66400 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20 54 61 62 ndex in some Tab
66410 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20 61 20 63 le.aCol[] of a c
66420 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a 4e 61 6d olumn named zNam
66430 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a 20 20 69 e */. } *a;. i
66440 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20 nt nId;
66450 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 64 65 /* Number of ide
66460 6e 74 69 66 69 65 72 73 20 6f 6e 20 74 68 65 20 ntifiers on the
66470 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 list */. int nA
66480 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 lloc; /* Nu
66490 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
664a0 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 5b allocated for a[
664b0 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f ] below */.};../
664c0 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d 61 73 6b *.** The bitmask
664d0 20 64 61 74 61 74 79 70 65 20 64 65 66 69 6e 65 datatype define
664e0 64 20 62 65 6c 6f 77 20 69 73 20 75 73 65 64 20 d below is used
664f0 66 6f 72 20 76 61 72 69 6f 75 73 20 6f 70 74 69 for various opti
66500 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a mizations..**.**
66510 20 43 68 61 6e 67 69 6e 67 20 74 68 69 73 20 66 Changing this f
66520 72 6f 6d 20 61 20 36 34 2d 62 69 74 20 74 6f 20 rom a 64-bit to
66530 61 20 33 32 2d 62 69 74 20 74 79 70 65 20 6c 69 a 32-bit type li
66540 6d 69 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 mits the number
66550 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 of.** tables in
66560 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20 69 6e 73 a join to 32 ins
66570 74 65 61 64 20 6f 66 20 36 34 2e 20 20 42 75 74 tead of 64. But
66580 20 69 74 20 61 6c 73 6f 20 72 65 64 75 63 65 73 it also reduces
66590 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 6f 66 20 the size.** of
665a0 74 68 65 20 6c 69 62 72 61 72 79 20 62 79 20 37 the library by 7
665b0 33 38 20 62 79 74 65 73 20 6f 6e 20 69 78 38 36 38 bytes on ix86
665c0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 36 34 ..*/.typedef u64
665d0 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a 0a 2a 2a Bitmask;../*.**
665e0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 The number of b
665f0 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b its in a Bitmask
66600 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e 73 20 22 . "BMS" means "
66610 42 69 74 4d 61 73 6b 20 53 69 7a 65 22 2e 0a 2a BitMask Size"..*
66620 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53 20 20 28 /.#define BMS (
66630 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 42 69 74 (int)(sizeof(Bit
66640 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a 0a 2a 2a mask)*8))../*.**
66650 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 The following s
66660 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 tructure describ
66670 65 73 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 es the FROM clau
66680 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 se of a SELECT s
66690 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 45 61 63 tatement..** Eac
666a0 68 20 74 61 62 6c 65 20 6f 72 20 73 75 62 71 75 h table or subqu
666b0 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 ery in the FROM
666c0 63 6c 61 75 73 65 20 69 73 20 61 20 73 65 70 61 clause is a sepa
666d0 72 61 74 65 20 65 6c 65 6d 65 6e 74 20 6f 66 0a rate element of.
666e0 2a 2a 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 ** the SrcList.a
666f0 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 [] array..**.**
66700 57 69 74 68 20 74 68 65 20 61 64 64 69 74 69 6f With the additio
66710 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65 20 64 61 n of multiple da
66720 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 2c 20 tabase support,
66730 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
66740 72 75 63 74 75 72 65 0a 2a 2a 20 63 61 6e 20 61 ructure.** can a
66750 6c 73 6f 20 62 65 20 75 73 65 64 20 74 6f 20 64 lso be used to d
66760 65 73 63 72 69 62 65 20 61 20 70 61 72 74 69 63 escribe a partic
66770 75 6c 61 72 20 74 61 62 6c 65 20 73 75 63 68 20 ular table such
66780 61 73 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 as the table tha
66790 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66 69 65 64 t.** is modified
667a0 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44 by an INSERT, D
667b0 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 ELETE, or UPDATE
667c0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 statement. In
667d0 73 74 61 6e 64 61 72 64 20 53 51 4c 2c 0a 2a 2a standard SQL,.**
667e0 20 73 75 63 68 20 61 20 74 61 62 6c 65 20 6d 75 such a table mu
667f0 73 74 20 62 65 20 61 20 73 69 6d 70 6c 65 20 6e st be a simple n
66800 61 6d 65 3a 20 49 44 2e 20 20 42 75 74 20 69 6e ame: ID. But in
66810 20 53 51 4c 69 74 65 2c 20 74 68 65 20 74 61 62 SQLite, the tab
66820 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77 20 62 65 le can.** now be
66830 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 61 identified by a
66840 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 database name,
66850 61 20 64 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 a dot, then the
66860 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 49 table name: ID.I
66870 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 69 D..**.** The joi
66880 6e 74 79 70 65 20 73 74 61 72 74 73 20 6f 75 74 ntype starts out
66890 20 73 68 6f 77 69 6e 67 20 74 68 65 20 6a 6f 69 showing the joi
668a0 6e 20 74 79 70 65 20 62 65 74 77 65 65 6e 20 74 n type between t
668b0 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 he current table
668c0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 65 78 74 .** and the next
668d0 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 69 table on the li
668e0 73 74 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 st. The parser
668f0 62 75 69 6c 64 73 20 74 68 65 20 6c 69 73 74 20 builds the list
66900 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20 42 75 74 this way..** But
66910 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 sqlite3SrcListS
66920 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 29 20 6c hiftJoinType() l
66930 61 74 65 72 20 73 68 69 66 74 73 20 74 68 65 20 ater shifts the
66940 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20 74 68 61 jointypes so tha
66950 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69 6e 74 79 t each.** jointy
66960 70 65 20 65 78 70 72 65 73 73 65 73 20 74 68 65 pe expresses the
66970 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68 join between th
66980 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 e table and the
66990 70 72 65 76 69 6f 75 73 20 74 61 62 6c 65 2e 0a previous table..
669a0 2a 2f 0a 73 74 72 75 63 74 20 53 72 63 4c 69 73 */.struct SrcLis
669b0 74 20 7b 0a 20 20 69 31 36 20 6e 53 72 63 3b 20 t {. i16 nSrc;
669c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
669d0 20 6f 66 20 74 61 62 6c 65 73 20 6f 72 20 73 75 of tables or su
669e0 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 20 bqueries in the
669f0 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 FROM clause */.
66a00 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 i16 nAlloc;
66a10 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
66a20 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 ntries allocated
66a30 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f in a[] below */
66a40 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
66a50 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 t_item {. cha
66a60 72 20 2a 7a 44 61 74 61 62 61 73 65 3b 20 20 2f r *zDatabase; /
66a70 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 * Name of databa
66a80 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 se holding this
66a90 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 61 table */. cha
66aa0 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f r *zName; /
66ab0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 * Name of the ta
66ac0 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 ble */. char
66ad0 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20 2f 2a 20 *zAlias; /*
66ae0 54 68 65 20 22 42 22 20 70 61 72 74 20 6f 66 20 The "B" part of
66af0 61 20 22 41 20 41 53 20 42 22 20 70 68 72 61 73 a "A AS B" phras
66b00 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 e. zName is the
66b10 20 22 41 22 20 2a 2f 0a 20 20 20 20 54 61 62 6c "A" */. Tabl
66b20 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 2f 2a e *pTab; /*
66b30 20 41 6e 20 53 51 4c 20 74 61 62 6c 65 20 63 6f An SQL table co
66b40 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a rresponding to z
66b50 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53 65 6c 65 Name */. Sele
66b60 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 2f 2a ct *pSelect; /*
66b70 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d A SELECT statem
66b80 65 6e 74 20 75 73 65 64 20 69 6e 20 70 6c 61 63 ent used in plac
66b90 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d e of a table nam
66ba0 65 20 2a 2f 0a 20 20 20 20 75 38 20 69 73 50 6f e */. u8 isPo
66bb0 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a 20 54 65 pulated; /* Te
66bc0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 61 73 mporary table as
66bd0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 45 sociated with SE
66be0 4c 45 43 54 20 69 73 20 70 6f 70 75 6c 61 74 65 LECT is populate
66bf0 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a 6f 69 6e d */. u8 join
66c00 74 79 70 65 3b 20 20 20 20 20 20 2f 2a 20 54 79 type; /* Ty
66c10 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65 74 77 65 pe of join betwe
66c20 65 6e 20 74 68 69 73 20 61 62 6c 65 20 61 6e 64 en this able and
66c30 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 2a 2f the previous */
66c40 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e 64 65 78 . u8 notIndex
66c50 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ed; /* True i
66c60 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 f there is a NOT
66c70 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 INDEXED clause
66c80 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75 72 73 */. int iCurs
66c90 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 or; /* The
66ca0 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 VDBE cursor numb
66cb0 65 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 er used to acces
66cc0 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a s this table */.
66cd0 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b 20 20 Expr *pOn;
66ce0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 /* The ON
66cf0 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e clause of a join
66d00 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74 20 2a */. IdList *
66d10 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54 68 65 pUsing; /* The
66d20 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 USING clause of
66d30 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 42 a join */. B
66d40 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64 3b 20 itmask colUsed;
66d50 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c 4e 29 /* Bit N (1<<N)
66d60 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e 20 4e set if column N
66d70 20 6f 66 20 70 54 61 62 20 69 73 20 75 73 65 64 of pTab is used
66d80 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 */. char *zI
66d90 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20 49 64 65 ndex; /* Ide
66da0 6e 74 69 66 69 65 72 20 66 72 6f 6d 20 22 49 4e ntifier from "IN
66db0 44 45 58 45 44 20 42 59 20 3c 7a 49 6e 64 65 78 DEXED BY <zIndex
66dc0 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 >" clause */.
66dd0 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 Index *pIndex;
66de0 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 /* Index stru
66df0 63 74 75 72 65 20 63 6f 72 72 65 73 70 6f 6e 64 cture correspond
66e00 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78 2c 20 69 ing to zIndex, i
66e10 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20 61 5b 31 f any */. } a[1
66e20 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ]; /
66e30 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 * One entry for
66e40 65 61 63 68 20 69 64 65 6e 74 69 66 69 65 72 20 each identifier
66e50 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 7d on the list */.}
66e60 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d 69 74 74 ;../*.** Permitt
66e70 65 64 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 ed values of the
66e80 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f 69 6e 74 SrcList.a.joint
66e90 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a 23 64 65 ype field.*/.#de
66ea0 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 20 20 20 fine JT_INNER
66eb0 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 41 0x0001 /* A
66ec0 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e 6e 65 72 ny kind of inner
66ed0 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 6e 20 2a or cross join *
66ee0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 43 52 4f /.#define JT_CRO
66ef0 53 53 20 20 20 20 20 30 78 30 30 30 32 20 20 20 SS 0x0002
66f00 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 75 73 65 /* Explicit use
66f10 20 6f 66 20 74 68 65 20 43 52 4f 53 53 20 6b 65 of the CROSS ke
66f20 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65 yword */.#define
66f30 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 20 30 78 JT_NATURAL 0x
66f40 30 30 30 34 20 20 20 20 2f 2a 20 54 72 75 65 20 0004 /* True
66f50 66 6f 72 20 61 20 22 6e 61 74 75 72 61 6c 22 20 for a "natural"
66f60 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 join */.#define
66f70 4a 54 5f 4c 45 46 54 20 20 20 20 20 20 30 78 30 JT_LEFT 0x0
66f80 30 30 38 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 008 /* Left o
66f90 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 uter join */.#de
66fa0 66 69 6e 65 20 4a 54 5f 52 49 47 48 54 20 20 20 fine JT_RIGHT
66fb0 20 20 30 78 30 30 31 30 20 20 20 20 2f 2a 20 52 0x0010 /* R
66fc0 69 67 68 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 ight outer join
66fd0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4f 55 */.#define JT_OU
66fe0 54 45 52 20 20 20 20 20 30 78 30 30 32 30 20 20 TER 0x0020
66ff0 20 20 2f 2a 20 54 68 65 20 22 4f 55 54 45 52 22 /* The "OUTER"
67000 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 keyword is pres
67010 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a ent */.#define J
67020 54 5f 45 52 52 4f 52 20 20 20 20 20 30 78 30 30 T_ERROR 0x00
67030 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e 6f 77 6e 40 /* unknown
67040 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 or unsupported
67050 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f join type */.../
67060 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50 6c 61 6e *.** A WherePlan
67070 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 20 69 6e object holds in
67080 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 64 formation that d
67090 65 73 63 72 69 62 65 73 20 61 20 6c 6f 6f 6b 75 escribes a looku
670a0 70 0a 2a 2a 20 73 74 72 61 74 65 67 79 2e 0a 2a p.** strategy..*
670b0 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 *.** This object
670c0 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 is intended to
670d0 62 65 20 6f 70 61 71 75 65 20 6f 75 74 73 69 64 be opaque outsid
670e0 65 20 6f 66 20 74 68 65 20 77 68 65 72 65 2e 63 e of the where.c
670f0 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49 74 20 69 module..** It i
67100 73 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20 s included here
67110 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20 74 68 61 only so that tha
67120 74 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 t compiler will
67130 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 69 74 0a know how big it.
67140 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20 6f 66 20 ** is. None of
67150 74 68 65 20 66 69 65 6c 64 73 20 69 6e 20 74 68 the fields in th
67160 69 73 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 is object should
67170 20 62 65 20 75 73 65 64 20 6f 75 74 73 69 64 65 be used outside
67180 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68 65 72 65 of.** the where
67190 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a .c module..**.**
671a0 20 57 69 74 68 69 6e 20 74 68 65 20 75 6e 69 6f Within the unio
671b0 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e 6c 79 20 n, pIdx is only
671c0 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 used when wsFlag
671d0 73 26 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20 s&WHERE_INDEXED
671e0 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70 54 65 72 is true..** pTer
671f0 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 m is only used w
67200 68 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 hen wsFlags&WHER
67210 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73 20 74 72 E_MULTI_OR is tr
67220 75 65 2e 20 20 41 6e 64 20 70 56 74 61 62 49 64 ue. And pVtabId
67230 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73 65 x.** is only use
67240 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57 d when wsFlags&W
67250 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c HERE_VIRTUALTABL
67260 45 20 69 73 20 74 72 75 65 2e 20 20 49 74 20 69 E is true. It i
67270 73 20 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 63 s never the.** c
67280 61 73 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68 ase that more th
67290 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 an one of these
672a0 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 74 72 conditions is tr
672b0 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 ue..*/.struct Wh
672c0 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75 33 32 20 erePlan {. u32
672d0 77 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 wsFlags;
672e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 /* WH
672f0 45 52 45 5f 2a 20 66 6c 61 67 73 20 74 68 61 74 ERE_* flags that
67300 20 64 65 73 63 72 69 62 65 20 74 68 65 20 73 74 describe the st
67310 72 61 74 65 67 79 20 2a 2f 0a 20 20 75 33 32 20 rategy */. u32
67320 6e 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 nEq;
67330 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
67340 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f 6e 73 74 mber of == const
67350 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75 6e 69 6f raints */. unio
67360 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 n {. Index *p
67370 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Idx;
67380 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
67390 77 68 65 6e 20 57 48 45 52 45 5f 49 4e 44 45 58 when WHERE_INDEX
673a0 45 44 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 ED is true */.
673b0 20 20 73 74 72 75 63 74 20 57 68 65 72 65 54 65 struct WhereTe
673c0 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 rm *pTerm;
673d0 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 /* WHERE clause
673e0 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d 73 65 61 term for OR-sea
673f0 72 63 68 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 rch */. sqlit
67400 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
67410 56 74 61 62 49 64 78 3b 20 20 2f 2a 20 56 69 72 VtabIdx; /* Vir
67420 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 tual table index
67430 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 7d 20 75 to use */. } u
67440 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 ;.};../*.** For
67450 65 61 63 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70 each nested loop
67460 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 in a WHERE clau
67470 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f se implementatio
67480 6e 2c 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f n, the WhereInfo
67490 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f .** structure co
674a0 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 ntains a single
674b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
674c0 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 structure. Thi
674d0 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 s structure.** i
674e0 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 s intended to be
674f0 20 70 72 69 76 61 74 65 20 74 68 65 20 74 68 65 private the the
67500 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 20 where.c module
67510 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 and should not b
67520 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f 72 20 6d e.** access or m
67530 6f 64 69 66 69 65 64 20 62 79 20 6f 74 68 65 72 odified by other
67540 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 modules..**.**
67550 54 68 65 20 70 49 64 78 49 6e 66 6f 20 66 69 65 The pIdxInfo fie
67560 6c 64 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 ld is used to he
67570 6c 70 20 70 69 63 6b 20 74 68 65 20 62 65 73 74 lp pick the best
67580 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a 2a 20 76 index on a.** v
67590 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 irtual table. T
675a0 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e he pIdxInfo poin
675b0 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 69 6e 64 ter contains ind
675c0 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 exing.** informa
675d0 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74 tion for the i-t
675e0 68 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 h table in the F
675f0 52 4f 4d 20 63 6c 61 75 73 65 20 62 65 66 6f 72 ROM clause befor
67600 65 20 72 65 6f 72 64 65 72 69 6e 67 2e 0a 2a 2a e reordering..**
67610 20 41 6c 6c 20 74 68 65 20 70 49 64 78 49 6e 66 All the pIdxInf
67620 6f 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 66 o pointers are f
67630 72 65 65 64 20 62 79 20 77 68 65 72 65 49 6e 66 reed by whereInf
67640 6f 46 72 65 65 28 29 20 69 6e 20 77 68 65 72 65 oFree() in where
67650 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 .c..** All other
67660 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 information in
67670 74 68 65 20 69 2d 74 68 20 57 68 65 72 65 4c 65 the i-th WhereLe
67680 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 vel object for t
67690 68 65 20 69 2d 74 68 20 74 61 62 6c 65 0a 2a 2a he i-th table.**
676a0 20 61 66 74 65 72 20 46 52 4f 4d 20 63 6c 61 75 after FROM clau
676b0 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a 2a 2f 0a se ordering..*/.
676c0 73 74 72 75 63 74 20 57 68 65 72 65 4c 65 76 65 struct WhereLeve
676d0 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c 61 6e 20 l {. WherePlan
676e0 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f 2a 20 71 plan; /* q
676f0 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 74 68 uery plan for th
67700 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 is element of th
67710 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f e FROM clause */
67720 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e . int iLeftJoin
67730 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f ; /* Memo
67740 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 ry cell used to
67750 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f implement LEFT O
67760 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69 UTER JOIN */. i
67770 6e 74 20 69 54 61 62 43 75 72 3b 20 20 20 20 20 nt iTabCur;
67780 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 /* The VDBE
67790 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 cursor used to
677a0 61 63 63 65 73 73 20 74 68 65 20 74 61 62 6c 65 access the table
677b0 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 */. int iIdxCu
677c0 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 r; /* T
677d0 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 he VDBE cursor u
677e0 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 70 49 sed to access pI
677f0 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 dx */. int addr
67800 42 72 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Brk; /*
67810 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 Jump here to br
67820 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c eak out of the l
67830 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 oop */. int add
67840 72 4e 78 74 3b 20 20 20 20 20 20 20 20 20 20 2f rNxt; /
67850 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 * Jump here to s
67860 74 61 72 74 20 74 68 65 20 6e 65 78 74 20 49 4e tart the next IN
67870 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a combination */.
67880 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20 int addrCont;
67890 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 /* Jump
678a0 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 here to continue
678b0 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 6c with the next l
678c0 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 oop cycle */. i
678d0 6e 74 20 61 64 64 72 46 69 72 73 74 3b 20 20 20 nt addrFirst;
678e0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 69 6e /* First in
678f0 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 69 6e 74 struction of int
67900 65 72 69 6f 72 20 6f 66 20 74 68 65 20 6c 6f 6f erior of the loo
67910 70 20 2a 2f 0a 20 20 75 38 20 69 46 72 6f 6d 3b p */. u8 iFrom;
67920 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67930 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 74 Which entry in t
67940 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a he FROM clause *
67950 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35 3b 20 20 /. u8 op, p5;
67960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 /* Opc
67970 6f 64 65 20 61 6e 64 20 50 35 20 6f 66 20 74 68 ode and P5 of th
67980 65 20 6f 70 63 6f 64 65 20 74 68 61 74 20 65 6e e opcode that en
67990 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 ds the loop */.
679a0 20 69 6e 74 20 70 31 2c 20 70 32 3b 20 20 20 20 int p1, p2;
679b0 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 6e /* Operan
679c0 64 73 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 ds of the opcode
679d0 20 75 73 65 64 20 74 6f 20 65 6e 64 73 20 74 68 used to ends th
679e0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f e loop */. unio
679f0 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 n {
67a00 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
67a10 20 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e that depends on
67a20 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 2a 2f plan.wsFlags */
67a30 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
67a40 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20 int nIn;
67a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
67a60 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
67a70 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20 n aInLoop[] */.
67a80 20 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f struct InLo
67a90 6f 70 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 op {. int
67aa0 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 iCur;
67ab0 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
67ac0 63 75 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 cursor used by t
67ad0 68 69 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 his IN operator
67ae0 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 */. int a
67af0 64 64 72 49 6e 54 6f 70 3b 20 20 20 20 20 20 20 ddrInTop;
67b00 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 /* Top of the
67b10 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 IN loop */.
67b20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 } *aInLoop;
67b30 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d /* Inform
67b40 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 ation about each
67b50 20 6e 65 73 74 65 64 20 49 4e 20 6f 70 65 72 61 nested IN opera
67b60 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b tor */. } in;
67b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67b80 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 6c /* Used when pl
67b90 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 an.wsFlags&WHERE
67ba0 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20 20 7d 20 _IN_ABLE */. }
67bb0 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c u;.. /* The fol
67bc0 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69 73 20 lowing field is
67bd0 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72 74 20 really not part
67be0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c of the current l
67bf0 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a 2a 20 evel. But. **
67c00 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63 65 20 we need a place
67c10 74 6f 20 63 61 63 68 65 20 76 69 72 74 75 61 6c to cache virtual
67c20 20 74 61 62 6c 65 20 69 6e 64 65 78 20 69 6e 66 table index inf
67c30 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 ormation for eac
67c40 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74 h. ** virtual t
67c50 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d able in the FROM
67c60 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 clause and the
67c70 57 68 65 72 65 4c 65 76 65 6c 20 73 74 72 75 63 WhereLevel struc
67c80 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 61 20 63 ture is. ** a c
67c90 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 63 65 20 onvenient place
67ca0 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 6f since there is o
67cb0 6e 65 20 57 68 65 72 65 4c 65 76 65 6c 20 66 6f ne WhereLevel fo
67cc0 72 20 65 61 63 68 20 46 52 4f 4d 20 63 6c 61 75 r each FROM clau
67cd0 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e se. ** element.
67ce0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f . */. sqlite3_
67cf0 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 index_info *pIdx
67d00 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 65 78 20 Info; /* Index
67d10 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 20 73 6f info for n-th so
67d20 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b urce table */.};
67d30 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 61 70 ../*.** Flags ap
67d40 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 propriate for th
67d50 65 20 77 63 74 72 6c 46 6c 61 67 73 20 70 61 72 e wctrlFlags par
67d60 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 65 ameter of sqlite
67d70 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a 2a 2a 3WhereBegin().**
67d80 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 49 6e and the WhereIn
67d90 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73 20 6d 65 fo.wctrlFlags me
67da0 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mber..*/.#define
67db0 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e WHERE_ORDERBY_N
67dc0 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 30 20 2f ORMAL 0x0000 /
67dd0 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 65 66 69 * No-op */.#defi
67de0 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 ne WHERE_ORDERBY
67df0 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 30 30 31 _MIN 0x0001
67e00 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 72 6f /* ORDER BY pro
67e10 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 69 6e 28 cessing for min(
67e20 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 69 6e ) func */.#defin
67e30 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f e WHERE_ORDERBY_
67e40 4d 41 58 20 20 20 20 20 20 30 78 30 30 30 32 20 MAX 0x0002
67e50 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 72 6f 63 /* ORDER BY proc
67e60 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 78 28 29 essing for max()
67e70 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 69 6e 65 func */.#define
67e80 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 WHERE_ONEPASS_D
67e90 45 53 49 52 45 44 20 20 30 78 30 30 30 34 20 2f ESIRED 0x0004 /
67ea0 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 6f 6e 65 * Want to do one
67eb0 2d 70 61 73 73 20 55 50 44 41 54 45 2f 44 45 4c -pass UPDATE/DEL
67ec0 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 ETE */.#define W
67ed0 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f HERE_DUPLICATES_
67ee0 4f 4b 20 20 20 20 30 78 30 30 30 38 20 2f 2a 20 OK 0x0008 /*
67ef0 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 61 20 72 Ok to return a r
67f00 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ow more than onc
67f10 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 e */.#define WHE
67f20 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 20 20 20 RE_OMIT_OPEN
67f30 20 20 20 20 30 78 30 30 31 30 20 2f 2a 20 54 61 0x0010 /* Ta
67f40 62 6c 65 20 63 75 72 73 6f 72 20 61 72 65 20 61 ble cursor are a
67f50 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a 2f 0a 23 lready open */.#
67f60 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 define WHERE_OMI
67f70 54 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 30 78 T_CLOSE 0x
67f80 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20 63 6c 6f 0020 /* Omit clo
67f90 73 65 20 6f 66 20 74 61 62 6c 65 20 26 20 69 6e se of table & in
67fa0 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 23 dex cursors */.#
67fb0 64 65 66 69 6e 65 20 57 48 45 52 45 5f 46 4f 52 define WHERE_FOR
67fc0 43 45 5f 54 41 42 4c 45 20 20 20 20 20 20 30 78 CE_TABLE 0x
67fd0 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 0040 /* Do not u
67fe0 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f 6e 6c 79 se an index-only
67ff0 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a search */../*.*
68000 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 * The WHERE clau
68010 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 72 6f se processing ro
68020 75 74 69 6e 65 20 68 61 73 20 74 77 6f 20 68 61 utine has two ha
68030 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 lves. The.** fi
68040 72 73 74 20 70 61 72 74 20 64 6f 65 73 20 74 68 rst part does th
68050 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 57 e start of the W
68060 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64 20 74 68 HERE loop and th
68070 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 e second.** half
68080 20 64 6f 65 73 20 74 68 65 20 74 61 69 6c 20 6f does the tail o
68090 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 f the WHERE loop
680a0 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f . An instance o
680b0 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 f.** this struct
680c0 75 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 ure is returned
680d0 62 79 20 74 68 65 20 66 69 72 73 74 20 68 61 6c by the first hal
680e0 66 20 61 6e 64 20 70 61 73 73 65 64 0a 2a 2a 20 f and passed.**
680f0 69 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 into the second
68100 68 61 6c 66 20 74 6f 20 67 69 76 65 20 73 6f 6d half to give som
68110 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f e continuity..*/
68120 0a 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 .struct WhereInf
68130 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 o {. Parse *pPa
68140 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 rse; /* Pa
68150 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 rsing and code g
68160 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 enerating contex
68170 74 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c t */. u16 wctrl
68180 46 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 46 Flags; /* F
68190 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 lags originally
681a0 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
681b0 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 3WhereBegin() */
681c0 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61 73 73 3b . u8 okOnePass;
681d0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b 20 74 6f /* Ok to
681e0 20 75 73 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c use one-pass al
681f0 67 6f 72 69 74 68 6d 20 66 6f 72 20 55 50 44 41 gorithm for UPDA
68200 54 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a TE or DELETE */.
68210 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c SrcList *pTabL
68220 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ist;
68230 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c /* List of tabl
68240 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a es in the join *
68250 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 /. int iTop;
68260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68270 20 20 20 2f 2a 20 54 68 65 20 76 65 72 79 20 62 /* The very b
68280 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
68290 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20 WHERE loop */.
682a0 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 20 20 int iContinue;
682b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
682c0 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 * Jump here to c
682d0 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78 ontinue with nex
682e0 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e t record */. in
682f0 74 20 69 42 72 65 61 6b 3b 20 20 20 20 20 20 20 t iBreak;
68300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
68310 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 Jump here to bre
68320 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f ak out of the lo
68330 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 op */. int nLev
68340 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 el;
68350 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
68360 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 of nested loop
68370 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 68 65 72 */. struct Wher
68380 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20 eClause *pWC;
68390 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 /* Decomposi
683a0 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 48 45 52 tion of the WHER
683b0 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 E clause */. Wh
683c0 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 ereLevel a[1];
683d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
683e0 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 Information abou
683f0 74 20 65 61 63 68 20 6e 65 73 74 20 6c 6f 6f 70 t each nest loop
68400 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a in WHERE */.};.
68410 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 43 6f 6e ./*.** A NameCon
68420 74 65 78 74 20 64 65 66 69 6e 65 73 20 61 20 63 text defines a c
68430 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 ontext in which
68440 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 to resolve table
68450 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e and column.** n
68460 61 6d 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 ames. The conte
68470 78 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 xt consists of a
68480 20 6c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 list of tables
68490 28 74 68 65 20 70 53 72 63 4c 69 73 74 29 20 66 (the pSrcList) f
684a0 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 20 6c 69 ield and.** a li
684b0 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72 st of named expr
684c0 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 74 29 2e ession (pEList).
684d0 20 20 54 68 65 20 6e 61 6d 65 64 20 65 78 70 72 The named expr
684e0 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d 61 79 0a ession list may.
684f0 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 54 68 65 ** be NULL. The
68500 20 70 53 72 63 20 63 6f 72 72 65 73 70 6f 6e 64 pSrc correspond
68510 73 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c s to the FROM cl
68520 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 ause of a SELECT
68530 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 20 74 61 or.** to the ta
68540 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 72 61 74 ble being operat
68550 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 52 54 2c ed on by INSERT,
68560 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
68570 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 45 4c 69 TE. The.** pELi
68580 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 st corresponds t
68590 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 o the result set
685a0 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 of a SELECT and
685b0 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a 2a 2a 20 is NULL for.**
685c0 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73 other statements
685d0 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 ..**.** NameCont
685e0 65 78 74 73 20 63 61 6e 20 62 65 20 6e 65 73 74 exts can be nest
685f0 65 64 2e 20 20 57 68 65 6e 20 72 65 73 6f 6c 76 ed. When resolv
68600 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 65 20 69 ing names, the i
68610 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a 20 63 6f nner-most .** co
68620 6e 74 65 78 74 20 69 73 20 73 65 61 72 63 68 65 ntext is searche
68630 64 20 66 69 72 73 74 2e 20 20 49 66 20 6e 6f 20 d first. If no
68640 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 match is found,
68650 74 68 65 20 6e 65 78 74 20 6f 75 74 65 72 0a 2a the next outer.*
68660 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 63 68 65 * context is che
68670 63 6b 65 64 2e 20 20 49 66 20 74 68 65 72 65 20 cked. If there
68680 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d 61 74 63 is still no matc
68690 68 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 74 h, the next cont
686a0 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 63 6b 65 ext.** is checke
686b0 64 2e 20 20 54 68 69 73 20 70 72 6f 63 65 73 73 d. This process
686c0 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e 74 69 6c continues until
686d0 20 65 69 74 68 65 72 20 61 20 6d 61 74 63 68 20 either a match
686e0 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f 72 20 61 is found.** or a
686f0 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 72 65 20 ll contexts are
68700 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 61 20 6d check. When a m
68710 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 74 atch is found, t
68720 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 72 20 6f he nRef member o
68730 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 78 74 f.** the context
68740 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
68750 6d 61 74 63 68 20 69 73 20 69 6e 63 72 65 6d 65 match is increme
68760 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 nted. .**.** Eac
68770 68 20 73 75 62 71 75 65 72 79 20 67 65 74 73 20 h subquery gets
68780 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e 74 65 78 a new NameContex
68790 74 2e 20 20 54 68 65 20 70 4e 65 78 74 20 66 69 t. The pNext fi
687a0 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 eld points to th
687b0 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 e.** NameContext
687c0 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 71 in the parent q
687d0 75 65 72 79 2e 20 20 54 68 75 73 20 74 68 65 20 uery. Thus the
687e0 70 72 6f 63 65 73 73 20 6f 66 20 73 63 61 6e 6e process of scann
687f0 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 ing the.** NameC
68800 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 6f 72 72 ontext list corr
68810 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 61 72 63 esponds to searc
68820 68 69 6e 67 20 74 68 72 6f 75 67 68 20 73 75 63 hing through suc
68830 63 65 73 73 69 76 65 6c 79 20 6f 75 74 65 72 0a cessively outer.
68840 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 6c 6f ** subqueries lo
68850 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d 61 74 63 oking for a matc
68860 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4e 61 6d h..*/.struct Nam
68870 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 50 61 72 eContext {. Par
68880 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
68890 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 /* The parser
688a0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 */. SrcList *pS
688b0 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 4f 6e 65 rcList; /* One
688c0 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c 65 73 20 or more tables
688d0 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 used to resolve
688e0 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c names */. ExprL
688f0 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 ist *pEList;
68900 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c 69 73 74 /* Optional list
68910 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 72 65 73 of named expres
68920 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e sions */. int n
68930 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
68940 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 61 6d /* Number of nam
68950 65 73 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 es resolved by t
68960 68 69 73 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 his context */.
68970 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 int nErr;
68980 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
68990 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e of errors encoun
689a0 74 65 72 65 64 20 77 68 69 6c 65 20 72 65 73 6f tered while reso
689b0 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a 2f 0a 20 lving names */.
689c0 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b 20 20 20 u8 allowAgg;
689d0 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 /* Aggrega
689e0 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c te functions all
689f0 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 75 owed here */. u
68a00 38 20 68 61 73 41 67 67 3b 20 20 20 20 20 20 20 8 hasAgg;
68a10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
68a20 67 67 72 65 67 61 74 65 73 20 61 72 65 20 73 65 ggregates are se
68a30 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 43 68 65 en */. u8 isChe
68a40 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ck; /*
68a50 54 72 75 65 20 69 66 20 72 65 73 6f 6c 76 69 6e True if resolvin
68a60 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 43 48 45 g names in a CHE
68a70 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f CK constraint */
68a80 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 3b 20 20 . int nDepth;
68a90 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 /* Depth
68aa0 20 6f 66 20 73 75 62 71 75 65 72 79 20 72 65 63 of subquery rec
68ab0 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 20 6e 6f ursion. 1 for no
68ac0 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 20 20 recursion */.
68ad0 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 AggInfo *pAggInf
68ae0 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 o; /* Informat
68af0 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 72 65 67 ion about aggreg
68b00 61 74 65 73 20 61 74 20 74 68 69 73 20 6c 65 76 ates at this lev
68b10 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 el */. NameCont
68b20 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 ext *pNext; /*
68b30 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 6d 65 20 Next outer name
68b40 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c 4c 20 66 context. NULL f
68b50 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 2a 2f 0a or outermost */.
68b60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 };../*.** An ins
68b70 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
68b80 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
68b90 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 69 6e contains all in
68ba0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6e 65 65 formation.** nee
68bb0 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 ded to generate
68bc0 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c code for a singl
68bd0 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
68be0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 6d 69 74 nt..**.** nLimit
68bf0 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66 is set to -1 if
68c00 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4c 49 4d there is no LIM
68c10 49 54 20 63 6c 61 75 73 65 2e 20 20 6e 4f 66 66 IT clause. nOff
68c20 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 30 2e set is set to 0.
68c30 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 .** If there is
68c40 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 a LIMIT clause,
68c50 74 68 65 20 70 61 72 73 65 72 20 73 65 74 73 20 the parser sets
68c60 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 20 76 61 nLimit to the va
68c70 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 lue of the.** li
68c80 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 20 mit and nOffset
68c90 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 to the value of
68ca0 74 68 65 20 6f 66 66 73 65 74 20 28 6f 72 20 30 the offset (or 0
68cb0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 if there is not
68cc0 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 20 42 75 .** offset). Bu
68cd0 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e 4c 69 6d t later on, nLim
68ce0 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 74 20 62 it and nOffset b
68cf0 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d 6f 72 79 ecome the memory
68d00 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 69 6e locations.** in
68d10 20 74 68 65 20 56 44 42 45 20 74 68 61 74 20 72 the VDBE that r
68d20 65 63 6f 72 64 20 74 68 65 20 6c 69 6d 69 74 20 ecord the limit
68d30 61 6e 64 20 6f 66 66 73 65 74 20 63 6f 75 6e 74 and offset count
68d40 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 64 72 4f ers..**.** addrO
68d50 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 72 69 65 penEphm[] entrie
68d60 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 61 64 s contain the ad
68d70 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e dress of OP_Open
68d80 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 Ephemeral opcode
68d90 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 64 64 72 s..** These addr
68da0 65 73 73 65 73 20 6d 75 73 74 20 62 65 20 73 74 esses must be st
68db0 6f 72 65 64 20 73 6f 20 74 68 61 74 20 77 65 20 ored so that we
68dc0 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 6e 64 20 can go back and
68dd0 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 65 20 50 fill in.** the P
68de0 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 20 50 32 4_KEYINFO and P2
68df0 20 70 61 72 61 6d 65 74 65 72 73 20 6c 61 74 65 parameters late
68e00 72 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20 r. Neither the
68e10 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a 2a 20 74 KeyInfo nor.** t
68e20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
68e30 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 6e 20 62 umns in P2 can b
68e40 65 20 63 6f 6d 70 75 74 65 64 20 61 74 20 74 68 e computed at th
68e50 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a 2a 20 61 e same time.** a
68e60 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 s the OP_OpenEph
68e70 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 m instruction is
68e80 20 63 6f 64 65 64 20 62 65 63 61 75 73 65 20 6e coded because n
68e90 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 69 6e 66 ot.** enough inf
68ea0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
68eb0 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 he compound quer
68ec0 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 74 68 y is known at th
68ed0 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 at point..** The
68ee0 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 KeyInfo for add
68ef0 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 61 6e 64 rOpenTran[0] and
68f00 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f [1] contains co
68f10 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
68f20 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 65 73 s.** for the res
68f30 75 6c 74 20 73 65 74 2e 20 20 54 68 65 20 4b 65 ult set. The Ke
68f40 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 yInfo for addrOp
68f50 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e 74 61 69 enTran[2] contai
68f60 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 ns collating.**
68f70 73 65 71 75 65 6e 63 65 73 20 66 6f 72 20 74 68 sequences for th
68f80 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
68f90 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 6c e..*/.struct Sel
68fa0 65 63 74 20 7b 0a 20 20 45 78 70 72 4c 69 73 74 ect {. ExprList
68fb0 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f *pEList; /
68fc0 2a 20 54 68 65 20 66 69 65 6c 64 73 20 6f 66 20 * The fields of
68fd0 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 the result */.
68fe0 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 u8 op;
68ff0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
69000 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b 5f 41 4c : TK_UNION TK_AL
69010 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 54 L TK_INTERSECT T
69020 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 20 63 68 K_EXCEPT */. ch
69030 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 ar affinity;
69040 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 65 63 6f /* MakeReco
69050 72 64 20 77 69 74 68 20 74 68 69 73 20 61 66 66 rd with this aff
69060 69 6e 69 74 79 20 66 6f 72 20 53 52 54 5f 53 65 inity for SRT_Se
69070 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 6c 46 6c t */. u16 selFl
69080 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ags; /*
69090 20 56 61 72 69 6f 75 73 20 53 46 5f 2a 20 76 61 Various SF_* va
690a0 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73 lues */. SrcLis
690b0 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 t *pSrc;
690c0 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 /* The FROM cla
690d0 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 use */. Expr *p
690e0 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20 Where;
690f0 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 /* The WHERE cla
69100 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 use */. ExprLis
69110 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 t *pGroupBy;
69120 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 /* The GROUP BY
69130 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
69140 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20 20 20 *pHaving;
69150 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49 4e 47 /* The HAVING
69160 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 clause */. Exp
69170 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b rList *pOrderBy;
69180 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 /* The ORDER
69190 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
691a0 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 Select *pPrior;
691b0 20 20 20 20 20 20 20 2f 2a 20 50 72 69 6f 72 20 /* Prior
691c0 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 select in a comp
691d0 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 ound select stat
691e0 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 ement */. Selec
691f0 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 t *pNext;
69200 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c 65 63 74 /* Next select
69210 20 74 6f 20 74 68 65 20 6c 65 66 74 20 69 6e 20 to the left in
69220 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 a compound */.
69230 53 65 6c 65 63 74 20 2a 70 52 69 67 68 74 6d 6f Select *pRightmo
69240 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 2d st; /* Right-
69250 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20 61 most select in a
69260 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 compound select
69270 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
69280 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20 20 Expr *pLimit;
69290 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 54 20 /* LIMIT
692a0 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c expression. NULL
692b0 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e means not used.
692c0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 */. Expr *pOff
692d0 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 set; /*
692e0 4f 46 46 53 45 54 20 65 78 70 72 65 73 73 69 6f OFFSET expressio
692f0 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f n. NULL means no
69300 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 t used. */. int
69310 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 iLimit, iOffset
69320 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 ; /* Memory re
69330 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 gisters holding
69340 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 54 20 63 LIMIT & OFFSET c
69350 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 ounters */. int
69360 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 33 5d addrOpenEphm[3]
69370 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70 ; /* OP_OpenEp
69380 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 65 6c 61 hem opcodes rela
69390 74 65 64 20 74 6f 20 74 68 69 73 20 73 65 6c 65 ted to this sele
693a0 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ct */.};../*.**
693b0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 Allowed values f
693c0 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c 46 6c 61 or Select.selFla
693d0 67 73 2e 20 20 54 68 65 20 22 53 46 22 20 70 72 gs. The "SF" pr
693e0 65 66 69 78 20 73 74 61 6e 64 73 20 66 6f 72 0a efix stands for.
693f0 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c 61 67 22 ** "Select Flag"
69400 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f ..*/.#define SF_
69410 44 69 73 74 69 6e 63 74 20 20 20 20 20 20 20 20 Distinct
69420 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 74 70 75 0x0001 /* Outpu
69430 74 20 73 68 6f 75 6c 64 20 62 65 20 44 49 53 54 t should be DIST
69440 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20 INCT */.#define
69450 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 20 20 20 SF_Resolved
69460 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 49 64 0x0002 /* Id
69470 65 6e 74 69 66 69 65 72 73 20 68 61 76 65 20 62 entifiers have b
69480 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 2a 2f 0a een resolved */.
69490 23 64 65 66 69 6e 65 20 53 46 5f 41 67 67 72 65 #define SF_Aggre
694a0 67 61 74 65 20 20 20 20 20 20 20 30 78 30 30 30 gate 0x000
694b0 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 61 4 /* Contains a
694c0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
694d0 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 ns */.#define SF
694e0 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20 20 _UsesEphemeral
694f0 20 30 78 30 30 30 38 20 20 2f 2a 20 55 73 65 73 0x0008 /* Uses
69500 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65 72 the OpenEphemer
69510 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 64 65 al opcode */.#de
69520 66 69 6e 65 20 53 46 5f 45 78 70 61 6e 64 65 64 fine SF_Expanded
69530 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20 20 0x0010
69540 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 /* sqlite3Select
69550 45 78 70 61 6e 64 28 29 20 63 61 6c 6c 65 64 20 Expand() called
69560 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 65 66 69 on this */.#defi
69570 6e 65 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 ne SF_HasTypeInf
69580 6f 20 20 20 20 20 30 78 30 30 32 30 20 20 2f 2a o 0x0020 /*
69590 20 46 52 4f 4d 20 73 75 62 71 75 65 72 69 65 73 FROM subqueries
695a0 20 68 61 76 65 20 54 61 62 6c 65 20 6d 65 74 61 have Table meta
695b0 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 data */.../*.**
695c0 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 The results of a
695d0 20 73 65 6c 65 63 74 20 63 61 6e 20 62 65 20 64 select can be d
695e0 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 73 65 istributed in se
695f0 76 65 72 61 6c 20 77 61 79 73 2e 20 20 54 68 65 veral ways. The
69600 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 66 69 78 .** "SRT" prefix
69610 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 54 20 52 means "SELECT R
69620 65 73 75 6c 74 20 54 79 70 65 22 2e 0a 2a 2f 0a esult Type"..*/.
69630 23 64 65 66 69 6e 65 20 53 52 54 5f 55 6e 69 6f #define SRT_Unio
69640 6e 20 20 20 20 20 20 20 20 31 20 20 2f 2a 20 53 n 1 /* S
69650 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 20 6b tore result as k
69660 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 eys in an index
69670 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 */.#define SRT_E
69680 78 63 65 70 74 20 20 20 20 20 20 20 32 20 20 2f xcept 2 /
69690 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 20 * Remove result
696a0 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 69 6e 64 from a UNION ind
696b0 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 ex */.#define SR
696c0 54 5f 45 78 69 73 74 73 20 20 20 20 20 20 20 33 T_Exists 3
696d0 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 69 66 20 /* Store 1 if
696e0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f the result is no
696f0 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 t empty */.#defi
69700 6e 65 20 53 52 54 5f 44 69 73 63 61 72 64 20 20 ne SRT_Discard
69710 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 6e 6f 74 4 /* Do not
69720 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 save the result
69730 73 20 61 6e 79 77 68 65 72 65 20 2a 2f 0a 0a 2f s anywhere */../
69740 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 * The ORDER BY c
69750 6c 61 75 73 65 20 69 73 20 69 67 6e 6f 72 65 64 lause is ignored
69760 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 for all of the
69770 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 69 6e 65 above */.#define
69780 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 IgnorableOrderb
69790 79 28 58 29 20 28 28 58 2d 3e 65 44 65 73 74 29 y(X) ((X->eDest)
697a0 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 29 0a 0a <=SRT_Discard)..
697b0 23 64 65 66 69 6e 65 20 53 52 54 5f 4f 75 74 70 #define SRT_Outp
697c0 75 74 20 20 20 20 20 20 20 35 20 20 2f 2a 20 4f ut 5 /* O
697d0 75 74 70 75 74 20 65 61 63 68 20 72 6f 77 20 6f utput each row o
697e0 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 64 65 66 f result */.#def
697f0 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 20 20 20 ine SRT_Mem
69800 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6f 72 65 6 /* Store
69810 20 72 65 73 75 6c 74 20 69 6e 20 61 20 6d 65 6d result in a mem
69820 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 64 65 66 ory cell */.#def
69830 69 6e 65 20 53 52 54 5f 53 65 74 20 20 20 20 20 ine SRT_Set
69840 20 20 20 20 20 37 20 20 2f 2a 20 53 74 6f 72 65 7 /* Store
69850 20 72 65 73 75 6c 74 73 20 61 73 20 6b 65 79 73 results as keys
69860 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a in an index */.
69870 23 64 65 66 69 6e 65 20 53 52 54 5f 54 61 62 6c #define SRT_Tabl
69880 65 20 20 20 20 20 20 20 20 38 20 20 2f 2a 20 53 e 8 /* S
69890 74 6f 72 65 20 72 65 73 75 6c 74 20 61 73 20 64 tore result as d
698a0 61 74 61 20 77 69 74 68 20 61 6e 20 61 75 74 6f ata with an auto
698b0 6d 61 74 69 63 20 72 6f 77 69 64 20 2a 2f 0a 23 matic rowid */.#
698c0 64 65 66 69 6e 65 20 53 52 54 5f 45 70 68 65 6d define SRT_Ephem
698d0 54 61 62 20 20 20 20 20 39 20 20 2f 2a 20 43 72 Tab 9 /* Cr
698e0 65 61 74 65 20 74 72 61 6e 73 69 65 6e 74 20 74 eate transient t
698f0 61 62 20 61 6e 64 20 73 74 6f 72 65 20 6c 69 6b ab and store lik
69900 65 20 53 52 54 5f 54 61 62 6c 65 20 2a 2f 0a 23 e SRT_Table */.#
69910 64 65 66 69 6e 65 20 53 52 54 5f 43 6f 72 6f 75 define SRT_Corou
69920 74 69 6e 65 20 20 20 31 30 20 20 2f 2a 20 47 65 tine 10 /* Ge
69930 6e 65 72 61 74 65 20 61 20 73 69 6e 67 6c 65 20 nerate a single
69940 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f row of result */
69950 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 ../*.** A struct
69960 75 72 65 20 75 73 65 64 20 74 6f 20 63 75 73 74 ure used to cust
69970 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 61 76 69 omize the behavi
69980 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 53 65 6c or of sqlite3Sel
69990 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a 20 63 6f ect(). See.** co
699a0 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 73 71 6c mments above sql
699b0 69 74 65 33 53 65 6c 65 63 74 28 29 20 66 6f 72 ite3Select() for
699c0 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 74 79 70 details..*/.typ
699d0 65 64 65 66 20 73 74 72 75 63 74 20 53 65 6c 65 edef struct Sele
699e0 63 74 44 65 73 74 20 53 65 6c 65 63 74 44 65 73 ctDest SelectDes
699f0 74 3b 0a 73 74 72 75 63 74 20 53 65 6c 65 63 74 t;.struct Select
69a00 44 65 73 74 20 7b 0a 20 20 75 38 20 65 44 65 73 Dest {. u8 eDes
69a10 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f t; /* Ho
69a20 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 w to dispose of
69a30 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 the results */.
69a40 20 75 38 20 61 66 66 69 6e 69 74 79 3b 20 20 20 u8 affinity;
69a50 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 75 /* Affinity u
69a60 73 65 64 20 77 68 65 6e 20 65 44 65 73 74 3d 3d sed when eDest==
69a70 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 69 6e 74 SRT_Set */. int
69a80 20 69 50 61 72 6d 3b 20 20 20 20 20 20 20 20 2f iParm; /
69a90 2a 20 41 20 70 61 72 61 6d 65 74 65 72 20 75 73 * A parameter us
69aa0 65 64 20 62 79 20 74 68 65 20 65 44 65 73 74 20 ed by the eDest
69ab0 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 disposal method
69ac0 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 */. int iMem;
69ad0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
69ae0 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 65 egister where re
69af0 73 75 6c 74 73 20 61 72 65 20 77 72 69 74 74 65 sults are writte
69b00 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b n */. int nMem;
69b10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
69b20 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 er of registers
69b30 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 7d 3b 0a allocated */.};.
69b40 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 63 6f ./*.** During co
69b50 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 de generation of
69b60 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 statements that
69b70 20 64 6f 20 69 6e 73 65 72 74 73 20 69 6e 74 6f do inserts into
69b80 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 0a AUTOINCREMENT .
69b90 2a 2a 20 74 61 62 6c 65 73 2c 20 74 68 65 20 66 ** tables, the f
69ba0 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 ollowing informa
69bb0 74 69 6f 6e 20 69 73 20 61 74 74 61 63 68 65 64 tion is attached
69bc0 20 74 6f 20 74 68 65 20 54 61 62 6c 65 2e 75 2e to the Table.u.
69bd0 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a 20 70 6f 69 autoInc.p.** poi
69be0 6e 74 65 72 20 6f 66 20 65 61 63 68 20 61 75 74 nter of each aut
69bf0 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 oincrement table
69c00 20 74 6f 20 72 65 63 6f 72 64 20 73 6f 6d 65 20 to record some
69c10 73 69 64 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e side information
69c20 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 6f 64 that.** the cod
69c30 65 20 67 65 6e 65 72 61 74 6f 72 20 6e 65 65 64 e generator need
69c40 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 6b s. We have to k
69c50 65 65 70 20 70 65 72 2d 74 61 62 6c 65 20 61 75 eep per-table au
69c60 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 69 toincrement.** i
69c70 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 63 61 nformation in ca
69c80 73 65 20 69 6e 73 65 72 74 73 20 61 72 65 20 64 se inserts are d
69c90 6f 77 6e 20 77 69 74 68 69 6e 20 74 72 69 67 67 own within trigg
69ca0 65 72 73 2e 20 20 54 72 69 67 67 65 72 73 20 64 ers. Triggers d
69cb0 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c o not.** normall
69cc0 79 20 63 6f 6f 72 64 69 6e 61 74 65 20 74 68 65 y coordinate the
69cd0 69 72 20 61 63 74 69 76 69 74 69 65 73 2c 20 62 ir activities, b
69ce0 75 74 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f ut we do need to
69cf0 20 63 6f 6f 72 64 69 6e 61 74 65 20 74 68 65 0a coordinate the.
69d00 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 6e 64 20 73 ** loading and s
69d10 61 76 69 6e 67 20 6f 66 20 61 75 74 6f 69 6e 63 aving of autoinc
69d20 72 65 6d 65 6e 74 20 69 6e 66 6f 72 6d 61 74 69 rement informati
69d30 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 on..*/.struct Au
69d40 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a 20 20 41 75 toincInfo {. Au
69d50 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e 65 78 74 toincInfo *pNext
69d60 3b 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 66 6f ; /* Next info
69d70 20 62 6c 6f 63 6b 20 69 6e 20 61 20 6c 69 73 74 block in a list
69d80 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a of them all */.
69d90 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 Table *pTab;
69da0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
69db0 20 74 68 69 73 20 69 6e 66 6f 20 62 6c 6f 63 6b this info block
69dc0 20 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 20 20 refers to */.
69dd0 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
69de0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
69df0 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 n sqlite3.aDb[]
69e00 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 of database hold
69e10 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e ing pTab */. in
69e20 74 20 72 65 67 43 74 72 3b 20 20 20 20 20 20 20 t regCtr;
69e30 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 /* Memory re
69e40 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 gister holding t
69e50 68 65 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 he rowid counter
69e60 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 */.};../*.** Si
69e70 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ze of the column
69e80 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66 6e 64 65 cache.*/.#ifnde
69e90 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 f SQLITE_N_COLCA
69ea0 43 48 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c CHE.# define SQL
69eb0 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 20 31 ITE_N_COLCACHE 1
69ec0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
69ed0 41 74 20 6c 65 61 73 74 20 6f 6e 65 20 69 6e 73 At least one ins
69ee0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
69ef0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
69f00 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 is created for
69f10 65 61 63 68 20 0a 2a 2a 20 74 72 69 67 67 65 72 each .** trigger
69f20 20 74 68 61 74 20 6d 61 79 20 62 65 20 66 69 72 that may be fir
69f30 65 64 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 ed while parsing
69f40 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 an INSERT, UPDA
69f50 54 45 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 TE or DELETE.**
69f60 73 74 61 74 65 6d 65 6e 74 2e 20 41 6c 6c 20 73 statement. All s
69f70 75 63 68 20 6f 62 6a 65 63 74 73 20 61 72 65 20 uch objects are
69f80 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6c 69 stored in the li
69f90 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 nked list headed
69fa0 20 61 74 0a 2a 2a 20 50 61 72 73 65 2e 70 54 72 at.** Parse.pTr
69fb0 69 67 67 65 72 50 72 67 20 61 6e 64 20 64 65 6c iggerPrg and del
69fc0 65 74 65 64 20 6f 6e 63 65 20 73 74 61 74 65 6d eted once statem
69fd0 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 ent compilation
69fe0 68 61 73 20 62 65 65 6e 0a 2a 2a 20 63 6f 6d 70 has been.** comp
69ff0 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 56 leted..**.** A V
6a000 64 62 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 dbe sub-program
6a010 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 that implements
6a020 74 68 65 20 62 6f 64 79 20 61 6e 64 20 57 48 45 the body and WHE
6a030 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 72 69 67 N clause of trig
6a040 67 65 72 0a 2a 2a 20 54 72 69 67 67 65 72 50 72 ger.** TriggerPr
6a050 67 2e 70 54 72 69 67 67 65 72 2c 20 61 73 73 75 g.pTrigger, assu
6a060 6d 69 6e 67 20 61 20 64 65 66 61 75 6c 74 20 4f ming a default O
6a070 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 N CONFLICT claus
6a080 65 20 6f 66 0a 2a 2a 20 54 72 69 67 67 65 72 50 e of.** TriggerP
6a090 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 73 20 73 74 rg.orconf, is st
6a0a0 6f 72 65 64 20 69 6e 20 74 68 65 20 54 72 69 67 ored in the Trig
6a0b0 67 65 72 50 72 67 2e 70 50 72 6f 67 72 61 6d 20 gerPrg.pProgram
6a0c0 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 variable..** The
6a0d0 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50 Parse.pTriggerP
6a0e0 72 67 20 6c 69 73 74 20 6e 65 76 65 72 20 63 6f rg list never co
6a0f0 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69 ntains two entri
6a100 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 es with the same
6a110 0a 2a 2a 20 76 61 6c 75 65 73 20 66 6f 72 20 62 .** values for b
6a120 6f 74 68 20 70 54 72 69 67 67 65 72 20 61 6e 64 oth pTrigger and
6a130 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a 2a 2a 20 54 orconf..**.** T
6a140 68 65 20 54 72 69 67 67 65 72 50 72 67 2e 61 43 he TriggerPrg.aC
6a150 6f 6c 6d 61 73 6b 5b 30 5d 20 76 61 72 69 61 62 olmask[0] variab
6a160 6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d le is set to a m
6a170 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c ask of old.* col
6a180 75 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64 umns.** accessed
6a190 20 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f (or set to 0 fo
6a1a0 72 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64 r triggers fired
6a1b0 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 as a result of
6a1c0 49 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65 INSERT .** state
6a1d0 6d 65 6e 74 73 29 2e 20 53 69 6d 69 6c 61 72 6c ments). Similarl
6a1e0 79 2c 20 74 68 65 20 54 72 69 67 67 65 72 50 72 y, the TriggerPr
6a1f0 67 2e 61 43 6f 6c 6d 61 73 6b 5b 31 5d 20 76 61 g.aColmask[1] va
6a200 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f riable is set to
6a210 0a 2a 2a 20 61 20 6d 61 73 6b 20 6f 66 20 6e 65 .** a mask of ne
6a220 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64 w.* columns used
6a230 20 62 79 20 74 68 65 20 70 72 6f 67 72 61 6d 2e by the program.
6a240 0a 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 .*/.struct Trigg
6a250 65 72 50 72 67 20 7b 0a 20 20 54 72 69 67 67 65 erPrg {. Trigge
6a260 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 r *pTrigger;
6a270 20 20 2f 2a 20 54 72 69 67 67 65 72 20 74 68 69 /* Trigger thi
6a280 73 20 70 72 6f 67 72 61 6d 20 77 61 73 20 63 6f s program was co
6a290 64 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e ded from */. in
6a2a0 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 t orconf;
6a2b0 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 /* Default
6a2c0 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c ON CONFLICT pol
6a2d0 69 63 79 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 icy */. SubProg
6a2e0 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 ram *pProgram;
6a2f0 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70 6c /* Program impl
6a300 65 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67 65 ementing pTrigge
6a310 72 2f 6f 72 63 6f 6e 66 20 2a 2f 0a 20 20 75 33 r/orconf */. u3
6a320 32 20 61 43 6f 6c 6d 61 73 6b 5b 32 5d 3b 20 20 2 aColmask[2];
6a330 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 73 20 6f /* Masks o
6a340 66 20 6f 6c 64 2e 2a 2c 20 6e 65 77 2e 2a 20 63 f old.*, new.* c
6a350 6f 6c 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 olumns accessed
6a360 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 */. TriggerPrg
6a370 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 *pNext; /*
6a380 4e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 Next entry in Pa
6a390 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 rse.pTriggerPrg
6a3a0 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a list */.};../*.*
6a3b0 2a 20 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 * An SQL parser
6a3c0 63 6f 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 context. A copy
6a3d0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
6a3e0 72 65 20 69 73 20 70 61 73 73 65 64 20 74 68 72 re is passed thr
6a3f0 6f 75 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 ough.** the pars
6a400 65 72 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f er and down into
6a410 20 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 all the parser
6a420 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 action routine i
6a430 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 n order to.** ca
6a440 72 72 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 rry around infor
6a450 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 mation that is g
6a460 6c 6f 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 lobal to the ent
6a470 69 72 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a ire parse..**.**
6a480 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 The structure i
6a490 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 s divided into t
6a4a0 77 6f 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 wo parts. When
6a4b0 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 the parser and c
6a4c0 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 ode.** generate
6a4d0 63 61 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 call themselves
6a4e0 72 65 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 recursively, the
6a4f0 20 66 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 first part of t
6a500 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 he structure.**
6a510 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 is constant but
6a520 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 the second part
6a530 69 73 20 72 65 73 65 74 20 61 74 20 74 68 65 20 is reset at the
6a540 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e beginning and en
6a550 64 20 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 d of.** each rec
6a560 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ursion..**.** Th
6a570 65 20 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 e nTableLock and
6a580 20 61 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 aTableLock vari
6a590 61 62 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 ables are only u
6a5a0 73 65 64 20 69 66 20 74 68 65 20 73 68 61 72 65 sed if the share
6a5b0 64 2d 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 d-cache .** feat
6a5c0 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 ure is enabled (
6a5d0 69 66 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d if sqlite3Tsd()-
6a5e0 3e 75 73 65 53 68 61 72 65 64 44 61 74 61 20 69 >useSharedData i
6a5f0 73 20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 s true). They ar
6a600 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f e.** used to sto
6a610 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 re the set of ta
6a620 62 6c 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 ble-locks requir
6a630 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d ed by the statem
6a640 65 6e 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d ent being.** com
6a650 70 69 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 piled. Function
6a660 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b sqlite3TableLock
6a670 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 () is used to ad
6a680 64 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 d entries to the
6a690 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 .** list..*/.str
6a6a0 75 63 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 uct Parse {. sq
6a6b0 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
6a6c0 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 /* The main d
6a6d0 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 atabase structur
6a6e0 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
6a6f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
6a700 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
6a710 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 execution */. c
6a720 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 har *zErrMsg;
6a730 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 /* An error
6a740 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 message */. Vdb
6a750 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 e *pVdbe;
6a760 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 /* An engine f
6a770 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 or executing dat
6a780 61 62 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a abase bytecode *
6a790 2f 0a 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 /. u8 colNamesS
6a7a0 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 et; /* TRUE
6a7b0 20 61 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e after OP_Column
6a7c0 4e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 Name has been is
6a7d0 73 75 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f sued to pVdbe */
6a7e0 0a 20 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b . u8 nameClash;
6a7f0 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 /* A per
6a800 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d manent table nam
6a810 65 20 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 e clashes with t
6a820 65 6d 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a emp table name *
6a830 2f 0a 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 /. u8 checkSche
6a840 6d 61 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 ma; /* Caus
6a850 65 73 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 es schema cookie
6a860 20 63 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 check after an
6a870 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 error */. u8 ne
6a880 73 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 sted;
6a890 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 /* Number of nes
6a8a0 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 ted calls to the
6a8b0 20 70 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e parser/code gen
6a8c0 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 erator */. u8 p
6a8d0 61 72 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 arseError;
6a8e0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 /* True after a
6a8f0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 parsing error.
6a900 20 54 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f Ticket #1794 */
6a910 0a 20 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 . u8 nTempReg;
6a920 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6a930 72 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 r of temporary r
6a940 65 67 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d egisters in aTem
6a950 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e pReg[] */. u8 n
6a960 54 65 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 TempInUse;
6a970 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 /* Number of aT
6a980 65 6d 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 empReg[] current
6a990 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a ly checked out *
6a9a0 2f 0a 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 /. int aTempReg
6a9b0 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 [8]; /* Hold
6a9c0 69 6e 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d ing area for tem
6a9d0 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 porary registers
6a9e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 */. int nRange
6a9f0 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 Reg; /* Si
6aa00 7a 65 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 ze of the tempor
6aa10 61 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f ary register blo
6aa20 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e ck */. int iRan
6aa30 67 65 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 geReg; /*
6aa40 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69 First register i
6aa50 6e 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 n temporary regi
6aa60 73 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 ster block */.
6aa70 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 int nErr;
6aa80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6aa90 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f f errors seen */
6aaa0 0a 20 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 . int nTab;
6aab0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6aac0 72 20 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 r of previously
6aad0 61 6c 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 allocated VDBE c
6aae0 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 ursors */. int
6aaf0 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 nMem;
6ab00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
6ab10 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 mory cells used
6ab20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
6ab30 6e 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 nSet;
6ab40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 /* Number of se
6ab50 74 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a ts used so far *
6ab60 2f 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 /. int ckBase;
6ab70 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 /* Base
6ab80 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 register of dat
6ab90 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 a during check c
6aba0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 onstraints */.
6abb0 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b int iCacheLevel;
6abc0 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 /* ColCache
6abd0 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c valid when aCol
6abe0 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d Cache[].iLevel<=
6abf0 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 iCacheLevel */.
6ac00 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 int iCacheCnt;
6ac10 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
6ac20 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
6ac30 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 e aColCache[].lr
6ac40 75 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 u values */. u8
6ac50 20 6e 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 nColCache;
6ac60 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6ac70 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 entries in the c
6ac80 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 olumn cache */.
6ac90 20 75 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 u8 iColCache;
6aca0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e /* Next en
6acb0 74 72 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 try of the cache
6acc0 20 74 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 to replace */.
6acd0 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
6ace0 65 20 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 e {. int iTab
6acf0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a le; /*
6ad00 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 Table cursor nu
6ad10 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 mber */. int
6ad20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 iColumn;
6ad30 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d /* Table colum
6ad40 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 n number */.
6ad50 75 38 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 u8 affChange;
6ad60 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
6ad70 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 68 this register h
6ad80 61 73 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 as had an affini
6ad90 74 79 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 ty change */.
6ada0 20 75 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 u8 tempReg;
6adb0 20 20 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 /* iReg i
6adc0 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 s a temp registe
6add0 72 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 r that needs to
6ade0 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 be freed */.
6adf0 69 6e 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 int iLevel;
6ae00 20 20 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 /* Nesting
6ae10 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e level */. in
6ae20 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 t iReg;
6ae30 20 20 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 /* Reg with
6ae40 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f value of this co
6ae50 6c 75 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f lumn. 0 means no
6ae60 6e 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c ne. */. int l
6ae70 72 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ru;
6ae80 20 2f 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 /* Least recent
6ae90 6c 79 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 ly used entry ha
6aea0 73 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 s the smallest v
6aeb0 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c alue */. } aCol
6aec0 43 61 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 Cache[SQLITE_N_C
6aed0 4f 4c 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e OLCACHE]; /* On
6aee0 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d e for each colum
6aef0 6e 20 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f n cache entry */
6af00 0a 20 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b . u32 writeMask
6af10 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 ; /* Start
6af20 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 a write transac
6af30 74 69 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 tion on these da
6af40 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 tabases */. u32
6af50 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 cookieMask;
6af60 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 /* Bitmask of
6af70 73 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 schema verified
6af80 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 databases */. u
6af90 38 20 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 8 isMultiWrite;
6afa0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 /* True if s
6afb0 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 tatement may aff
6afc0 65 63 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 ect/insert multi
6afd0 70 6c 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 ple rows */. u8
6afe0 20 6d 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 mayAbort;
6aff0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 /* True if st
6b000 61 74 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f atement may thro
6b010 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 w an ABORT excep
6b020 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f tion */. int co
6b030 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f okieGoto; /
6b040 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f * Address of OP_
6b050 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 Goto to cookie v
6b060 65 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 erifier subrouti
6b070 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b ne */. int cook
6b080 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d ieValue[SQLITE_M
6b090 41 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 AX_ATTACHED+2];
6b0a0 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f /* Values of co
6b0b0 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 okies to verify
6b0c0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
6b0d0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
6b0e0 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 CHE. int nTable
6b0f0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
6b100 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 Number of locks
6b110 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f in aTableLock */
6b120 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 . TableLock *aT
6b130 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 ableLock; /* Req
6b140 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b uired table lock
6b150 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 s for shared-cac
6b160 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 he mode */.#endi
6b170 66 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 f. int regRowid
6b180 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 ; /* Regi
6b190 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 ster holding row
6b1a0 69 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 id of CREATE TAB
6b1b0 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e LE entry */. in
6b1c0 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 t regRoot;
6b1d0 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
6b1e0 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 olding root page
6b1f0 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 number for new
6b200 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 objects */. Aut
6b210 6f 69 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b oincInfo *pAinc;
6b220 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e /* Information
6b230 20 61 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 about AUTOINCRE
6b240 4d 45 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f MENT counters */
6b250 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 . int nMaxArg;
6b260 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 /* Max a
6b270 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 rgs passed to us
6b280 65 72 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 er function by s
6b290 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 ub-program */..
6b2a0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
6b2b0 75 73 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e used while codin
6b2c0 67 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 g trigger progra
6b2d0 6d 73 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a ms. */. Parse *
6b2e0 70 54 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a pToplevel; /*
6b2f0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 Parse structure
6b300 20 66 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 for main progra
6b310 6d 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 m (or NULL) */.
6b320 20 54 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 Table *pTrigger
6b330 54 61 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 Tab; /* Table t
6b340 72 69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e riggers are bein
6b350 67 20 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 g coded for */.
6b360 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 u32 oldmask;
6b370 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 /* Mask of
6b380 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 old.* columns r
6b390 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 eferenced */. u
6b3a0 33 32 20 6e 65 77 6d 61 73 6b 3b 20 20 20 20 20 32 newmask;
6b3b0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6e /* Mask of n
6b3c0 65 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66 ew.* columns ref
6b3d0 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 erenced */. u8
6b3e0 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 20 eTriggerOp;
6b3f0 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20 /* TK_UPDATE,
6b400 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f TK_INSERT or TK_
6b410 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 65 DELETE */. u8 e
6b420 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 Orconf;
6b430 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 /* Default ON C
6b440 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 ONFLICT policy f
6b450 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73 or trigger steps
6b460 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 62 6c 65 */. u8 disable
6b470 54 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54 72 Triggers; /* Tr
6b480 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72 ue to disable tr
6b490 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 iggers */.. /*
6b4a0 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e Above is constan
6b4b0 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 t between recurs
6b4c0 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 ions. Below is
6b4d0 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 reset before and
6b4e0 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 after. ** each
6b4f0 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 recursion */..
6b500 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 int nVar;
6b510 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6b520 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 of '?' variables
6b530 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c seen in the SQL
6b540 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 so far */. int
6b550 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 nVarExpr;
6b560 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
6b570 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 sed slots in apV
6b580 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e arExpr[] */. in
6b590 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b t nVarExprAlloc;
6b5a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6b5b0 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 allocated slots
6b5c0 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a in apVarExpr[] *
6b5d0 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 /. Expr **apVar
6b5e0 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e Expr; /* Poin
6b5f0 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 ters to :aaa and
6b600 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 $aaaa wildcard
6b610 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 expressions */.
6b620 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 61 72 Vdbe *pReprepar
6b630 65 3b 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e e; /* VM bein
6b640 67 20 72 65 70 72 65 70 61 72 65 64 20 28 73 71 g reprepared (sq
6b650 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29 lite3Reprepare()
6b660 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61 ) */. int nAlia
6b670 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e s; /* N
6b680 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64 umber of aliased
6b690 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 result set colu
6b6a0 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c mns */. int nAl
6b6b0 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a iasAlloc; /*
6b6c0 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 Number of alloc
6b6d0 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 61 ated slots for a
6b6e0 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e 74 Alias[] */. int
6b6f0 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20 20 *aAlias;
6b700 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 /* Register us
6b710 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 73 ed to hold alias
6b720 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75 ed result */. u
6b730 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 8 explain;
6b740 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
6b750 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20 he EXPLAIN flag
6b760 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 is found on the
6b770 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e query */. Token
6b780 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 sNameToken;
6b790 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75 6e /* Token with un
6b7a0 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d 61 qualified schema
6b7b0 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a object name */.
6b7c0 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f 6b Token sLastTok
6b7d0 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 61 en; /* The la
6b7e0 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64 20 st token parsed
6b7f0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
6b800 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c *zTail; /* All
6b810 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 74 SQL text past t
6b820 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c 6f he last semicolo
6b830 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 61 n parsed */. Ta
6b840 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20 ble *pNewTable;
6b850 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 65 /* A table be
6b860 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 ing constructed
6b870 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 by CREATE TABLE
6b880 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e */. Trigger *pN
6b890 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f ewTrigger; /
6b8a0 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 20 * Trigger under
6b8b0 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 43 construct by a C
6b8c0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f REATE TRIGGER */
6b8d0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
6b8e0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 AuthContext; /*
6b8f0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 The 6th paramete
6b900 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 r to db->xAuth c
6b910 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e allbacks */.#ifn
6b920 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6b930 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 VIRTUALTABLE. T
6b940 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 oken sArg;
6b950 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d /* Com
6b960 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20 plete text of a
6b970 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 module argument
6b980 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56 */. u8 declareV
6b990 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 tab;
6b9a0 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64 /* True if insid
6b9b0 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 e sqlite3_declar
6b9c0 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e e_vtab() */. in
6b9d0 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 t nVtabLock;
6b9e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
6b9f0 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 er of virtual ta
6ba00 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a bles to lock */.
6ba10 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 Table **apVtab
6ba20 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
6ba30 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75 Pointer to virtu
6ba40 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e al tables needin
6ba50 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e g locking */.#en
6ba60 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 dif. int nHeigh
6ba70 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
6ba80 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 Expression tree
6ba90 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65 height of curre
6baa0 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f nt sub-select */
6bab0 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69 . Table *pZombi
6bac0 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69 eTab; /* Li
6bad0 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65 st of Table obje
6bae0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66 cts to delete af
6baf0 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a ter code gen */.
6bb00 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 54 TriggerPrg *pT
6bb10 72 69 67 67 65 72 50 72 67 3b 20 20 20 20 2f 2a riggerPrg; /*
6bb20 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 Linked list of
6bb30 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 20 2a coded triggers *
6bb40 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c /.};..#ifdef SQL
6bb50 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
6bb60 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 TABLE. #define
6bb70 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
6bb80 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 0.#else. #defin
6bb90 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 e IN_DECLARE_VTA
6bba0 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 B (pParse->decla
6bbb0 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a reVtab).#endif..
6bbc0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
6bbd0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
6bbe0 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e ng structure can
6bbf0 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 be declared on
6bc00 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 a stack and used
6bc10 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 .** to save the
6bc20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 Parse.zAuthConte
6bc30 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 xt value so that
6bc40 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f it can be resto
6bc50 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 red later..*/.st
6bc60 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 ruct AuthContext
6bc70 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 {. const char
6bc80 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 *zAuthContext;
6bc90 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 /* Put saved Pa
6bca0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 rse.zAuthContext
6bcb0 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 here */. Parse
6bcc0 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
6bcd0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 /* The Pa
6bce0 72 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f rse structure */
6bcf0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 .};../*.** Bitfi
6bd00 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50 35 eld flags for P5
6bd10 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 value in OP_Ins
6bd20 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 ert and OP_Delet
6bd30 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 e.*/.#define OPF
6bd40 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 20 20 LAG_NCHANGE
6bd50 20 20 30 78 30 31 20 20 20 20 2f 2a 20 53 65 74 0x01 /* Set
6bd60 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e 6e to update db->n
6bd70 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e Change */.#defin
6bd80 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 e OPFLAG_LASTROW
6bd90 49 44 20 20 20 20 20 30 78 30 32 20 20 20 20 2f ID 0x02 /
6bda0 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 * Set to update
6bdb0 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f db->lastRowid */
6bdc0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
6bdd0 49 53 55 50 44 41 54 45 20 20 20 20 20 20 30 78 ISUPDATE 0x
6bde0 30 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 50 04 /* This OP
6bdf0 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 71 _Insert is an sq
6be00 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 l UPDATE */.#def
6be10 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e ine OPFLAG_APPEN
6be20 44 20 20 20 20 20 20 20 20 30 78 30 38 20 20 20 D 0x08
6be30 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 /* This is like
6be40 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 ly to be an appe
6be50 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 nd */.#define OP
6be60 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 FLAG_USESEEKRESU
6be70 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54 72 LT 0x10 /* Tr
6be80 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65 65 y to avoid a see
6be90 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72 74 k in BtreeInsert
6bea0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 () */.#define OP
6beb0 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 20 FLAG_CLEARCACHE
6bec0 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43 6c 0x20 /* Cl
6bed0 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 ear pseudo-table
6bee0 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f 6c cache in OP_Col
6bef0 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 umn */../*. * Ea
6bf00 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 65 ch trigger prese
6bf10 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 nt in the databa
6bf20 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f se schema is sto
6bf30 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e red as an instan
6bf40 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20 ce of. * struct
6bf50 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 Trigger. . *. *
6bf60 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74 Pointers to inst
6bf70 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 ances of struct
6bf80 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f 72 Trigger are stor
6bf90 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a ed in two ways..
6bfa0 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72 * 1. In the "tr
6bfb0 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 62 igHash" hash tab
6bfc0 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 le (part of the
6bfd0 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65 sqlite3* that re
6bfe0 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a presents the . *
6bff0 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 54 database). T
6c000 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67 his allows Trigg
6c010 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 6f er structures to
6c020 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 79 be retrieved by
6c030 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c name.. * 2. All
6c040 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69 triggers associ
6c050 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 ated with a sing
6c060 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20 le table form a
6c070 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69 linked list, usi
6c080 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65 ng the. * pNe
6c090 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72 xt member of str
6c0a0 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 70 uct Trigger. A p
6c0b0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 ointer to the fi
6c0c0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 rst element of t
6c0d0 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20 he. * linked
6c0e0 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 61 list is stored a
6c0f0 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 22 s the "pTrigger"
6c100 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61 member of the a
6c110 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20 ssociated. *
6c120 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a struct Table.. *
6c130 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69 . * The "step_li
6c140 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 st" member point
6c150 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 s to the first e
6c160 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b lement of a link
6c170 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61 ed list. * conta
6c180 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 ining the SQL st
6c190 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69 atements specifi
6c1a0 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 65 ed as the trigge
6c1b0 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 r program.. */.s
6c1c0 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a truct Trigger {.
6c1d0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
6c1e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6c1f0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 name of the tri
6c200 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 gger
6c210 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */.
6c220 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20 char *table;
6c230 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
6c240 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f table or view to
6c250 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 67 which the trigg
6c260 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20 er applies */.
6c270 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 u8 op;
6c280 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
6c290 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f f TK_DELETE, TK_
6c2a0 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 UPDATE, TK_INSER
6c2b0 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75 T */. u
6c2c0 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20 8 tr_tm;
6c2d0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
6c2e0 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c TRIGGER_BEFORE,
6c2f0 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a TRIGGER_AFTER *
6c300 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b /. Expr *pWhen;
6c310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6c320 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f he WHEN clause o
6c330 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
6c340 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a (may be NULL) *
6c350 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c /. IdList *pCol
6c360 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 umns; /* I
6c370 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 f this is an UPD
6c380 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c ATE OF <column-l
6c390 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 ist> trigger,.
6c3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c3b0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c the <
6c3c0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 column-list> is
6c3d0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 stored here */.
6c3e0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
6c3f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 ; /* Sche
6c400 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ma containing th
6c410 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 e trigger */. S
6c420 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d chema *pTabSchem
6c430 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 a; /* Schema
6c440 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
6c450 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 table */. Trigg
6c460 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 erStep *step_lis
6c470 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 t; /* Link list
6c480 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 of trigger progr
6c490 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20 am steps
6c4a0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 */. Trigge
6c4b0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 r *pNext;
6c4c0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 /* Next trigge
6c4d0 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 r associated wit
6c4e0 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d h the table */.}
6c4f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 ;../*.** A trigg
6c500 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42 er is either a B
6c510 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 EFORE or an AFTE
6c520 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 R trigger. The
6c530 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 following consta
6c540 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 nts.** determine
6c550 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 which. .**.** I
6c560 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 f there are mult
6c570 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 iple triggers, y
6c580 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 ou might of some
6c590 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 BEFORE and some
6c5a0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 AFTER..** In th
6c5b0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f at cases, the co
6c5c0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 nstants below ca
6c5d0 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 n be ORed togeth
6c5e0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 er..*/.#define T
6c5f0 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 RIGGER_BEFORE 1
6c600 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 .#define TRIGGER
6c610 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 _AFTER 2../*.
6c620 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
6c630 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 struct TriggerS
6c640 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 tep is used to s
6c650 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 tore a single SQ
6c660 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 L statement. * t
6c670 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 hat is a part of
6c680 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 a trigger-progr
6c690 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 am. . *. * Insta
6c6a0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 nces of struct T
6c6b0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73 riggerStep are s
6c6c0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c tored in a singl
6c6d0 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c y linked list (l
6c6e0 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 inked. * using t
6c6f0 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 he "pNext" membe
6c700 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 r) referenced by
6c710 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 the "step_list"
6c720 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a member of the .
6c730 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 * associated st
6c740 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 ruct Trigger ins
6c750 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 tance. The first
6c760 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
6c770 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 linked list is.
6c780 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 * the first step
6c790 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d of the trigger-
6c7a0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 program.. * . *
6c7b0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 The "op" member
6c7c0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65 indicates whethe
6c7d0 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c r this is a "DEL
6c7e0 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 ETE", "INSERT",
6c7f0 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 "UPDATE" or. * "
6c800 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e SELECT" statemen
6c810 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 t. The meanings
6c820 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d of the other mem
6c830 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e bers is determin
6c840 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 ed by the . * va
6c850 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 lue of "op" as f
6c860 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f ollows:. *. * (o
6c870 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a p == TK_INSERT).
6c880 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 * orconf ->
6c890 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f stores the ON CO
6c8a0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d NFLICT algorithm
6c8b0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e . * pSelect ->
6c8c0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 If this is an I
6c8d0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 NSERT INTO ... S
6c8e0 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d ELECT ... statem
6c8f0 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 ent, then. *
6c900 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 this s
6c910 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 tores a pointer
6c920 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 to the SELECT st
6c930 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 atement. Otherwi
6c940 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 se NULL.. * targ
6c950 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e et -> A token
6c960 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f holding the quo
6c970 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 ted name of the
6c980 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 table to insert
6c990 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69 into.. * pExprLi
6c9a0 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 st -> If this is
6c9b0 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 an INSERT INTO
6c9c0 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73 ... VALUES ... s
6c9d0 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 tatement, then.
6c9e0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 * t
6c9f0 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65 his stores value
6ca00 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 s to be inserted
6ca10 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c . Otherwise NULL
6ca20 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d .. * pIdList -
6ca30 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 > If this is an
6ca40 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 INSERT INTO ...
6ca50 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29 (<column-names>)
6ca60 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20 VALUES ... . *
6ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
6ca80 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 tement, then thi
6ca90 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c s stores the col
6caa0 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a umn-names to be.
6cab0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6cac0 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20 inserted into..
6cad0 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44 *. * (op == TK_D
6cae0 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74 ELETE). * target
6caf0 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 -> A token h
6cb00 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f 74 65 olding the quote
6cb10 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 d name of the ta
6cb20 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72 ble to delete fr
6cb30 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 om.. * pWhere
6cb40 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c -> The WHERE cl
6cb50 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45 ause of the DELE
6cb60 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 TE statement if
6cb70 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 one is specified
6cb80 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 .. *
6cb90 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
6cba0 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20 .. * . * (op ==
6cbb0 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61 TK_UPDATE). * ta
6cbc0 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b rget -> A tok
6cbd0 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 en holding the q
6cbe0 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 uoted name of th
6cbf0 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 74 e table to updat
6cc00 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 57 e rows of.. * pW
6cc10 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 here -> The W
6cc20 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
6cc30 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d he UPDATE statem
6cc40 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 ent if one is sp
6cc50 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 ecified.. *
6cc60 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 Otherwi
6cc70 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 70 se NULL.. * pExp
6cc80 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 20 rList -> A list
6cc90 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 74 of the columns t
6cca0 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 65 o update and the
6ccb0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 expressions to
6ccc0 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 20 update. *
6ccd0 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e 20 them to.
6cce0 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 74 See sqlite3Updat
6ccf0 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f e() documentatio
6cd00 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 0a n of "pChanges".
6cd10 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6cd20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a argument.. * . *
6cd30 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 /.struct Trigger
6cd40 53 74 65 70 20 7b 0a 20 20 75 38 20 6f 70 3b 20 Step {. u8 op;
6cd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6cd60 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 One of TK_DELET
6cd70 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b E, TK_UPDATE, TK
6cd80 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c 45 _INSERT, TK_SELE
6cd90 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e CT */. u8 orcon
6cda0 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 f; /*
6cdb0 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 2e OE_Rollback etc.
6cdc0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 */. Trigger *p
6cdd0 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 68 Trig; /* Th
6cde0 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 74 e trigger that t
6cdf0 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 61 his step is a pa
6ce00 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 6c 65 63 rt of */. Selec
6ce10 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 t *pSelect;
6ce20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d 65 /* SELECT statme
6ce30 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 49 4e 53 nt or RHS of INS
6ce40 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 45 4c 45 ERT INTO .. SELE
6ce50 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 CT ... */. Toke
6ce60 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20 n target;
6ce70 20 2f 2a 20 54 61 72 67 65 74 20 74 61 62 6c 65 /* Target table
6ce80 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 for DELETE, UPD
6ce90 41 54 45 2c 20 49 4e 53 45 52 54 20 2a 2f 0a 20 ATE, INSERT */.
6cea0 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 Expr *pWhere;
6ceb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 /* The WHE
6cec0 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 44 45 RE clause for DE
6ced0 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73 LETE or UPDATE s
6cee0 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 teps */. ExprLi
6cef0 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f st *pExprList; /
6cf00 2a 20 53 45 54 20 63 6c 61 75 73 65 20 66 6f 72 * SET clause for
6cf10 20 55 50 44 41 54 45 2e 20 20 56 41 4c 55 45 53 UPDATE. VALUES
6cf20 20 63 6c 61 75 73 65 20 66 6f 72 20 49 4e 53 45 clause for INSE
6cf30 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a RT */. IdList *
6cf40 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 pIdList; /*
6cf50 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 Column names for
6cf60 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 54 72 69 INSERT */. Tri
6cf70 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b ggerStep *pNext;
6cf80 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 /* Next in the
6cf90 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 link-list */.
6cfa0 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 TriggerStep *pLa
6cfb0 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 st; /* Last ele
6cfc0 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 ment in link-lis
6cfd0 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 t. Valid for 1st
6cfe0 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b elem only */.};
6cff0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
6d000 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
6d010 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 contains informa
6d020 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 tion used by the
6d030 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a sqliteFix....**
6d040 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 routines as the
6d050 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 y walk the parse
6d060 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 tree to make da
6d070 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 tabase reference
6d080 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 s.** explicit.
6d090 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
6d0a0 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78 ct DbFixer DbFix
6d0b0 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78 er;.struct DbFix
6d0c0 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 er {. Parse *pP
6d0d0 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 arse; /* Th
6d0e0 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 e parsing contex
6d0f0 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67 t. Error messag
6d100 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 es written here
6d110 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
6d120 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 *zDb; /* Make
6d130 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 sure all object
6d140 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 s are contained
6d150 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 in this database
6d160 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
6d170 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 *zType; /* Typ
6d180 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e e of the contain
6d190 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 er - used for er
6d1a0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
6d1b0 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 const Token *p
6d1c0 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 Name; /* Name of
6d1d0 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d the container -
6d1e0 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 used for error
6d1f0 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a messages */.};..
6d200 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 /*.** An objecte
6d210 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 d used to accumu
6d220 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 late the text of
6d230 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 a string where
6d240 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 we.** do not nec
6d250 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f essarily know ho
6d260 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 w big the string
6d270 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 will be in the
6d280 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 end..*/.struct S
6d290 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 trAccum {. sqli
6d2a0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
6d2b0 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 /* Optional dat
6d2c0 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 abase for lookas
6d2d0 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c ide. Can be NUL
6d2e0 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 L */. char *zBa
6d2f0 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 se; /* A
6d300 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e base allocation
6d310 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c . Not from mall
6d320 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a oc. */. char *z
6d330 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Text; /*
6d340 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c The string coll
6d350 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a ected so far */.
6d360 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 int nChar;
6d370 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
6d380 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 of the string s
6d390 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 o far */. int
6d3a0 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
6d3b0 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 /* Amount of spa
6d3c0 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 ce allocated in
6d3d0 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 zText */. int
6d3e0 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 mxAlloc;
6d3f0 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 /* Maximum allow
6d400 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 ed string length
6d410 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f */. u8 mallo
6d420 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 cFailed; /* Be
6d430 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e comes true if an
6d440 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 y memory allocat
6d450 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 ion fails */. u
6d460 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 8 useMalloc;
6d470 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a /* True if z
6d480 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 65 61 Text is enlargea
6d490 62 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f ble using reallo
6d4a0 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 c */. u8 tooB
6d4b0 69 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 ig; /* B
6d4c0 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 ecomes true if s
6d4d0 74 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 tring size excee
6d4e0 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a ds limits */.};.
6d4f0 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 ./*.** A pointer
6d500 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
6d510 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f re is used to co
6d520 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d mmunicate inform
6d530 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 ation.** from sq
6d540 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 lite3Init and OP
6d550 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 _ParseSchema int
6d560 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 o the sqlite3Ini
6d570 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 tCallback..*/.ty
6d580 70 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 pedef struct {.
6d590 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
6d5a0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
6d5b0 62 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 base being initi
6d5c0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 alized */. int
6d5d0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
6d5e0 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 /* 0 for main da
6d5f0 74 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 tabase. 1 for T
6d600 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 EMP, 2.. for ATT
6d610 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 ACHed */. char
6d620 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f **pzErrMsg; /
6d630 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 * Error message
6d640 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 stored here */.
6d650 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
6d660 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 /* Result c
6d670 6f 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 ode stored here
6d680 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a */.} InitData;..
6d690 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 /*.** Structure
6d6a0 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 containing globa
6d6b0 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 l configuration
6d6c0 64 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c data for the SQL
6d6d0 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a ite library..**.
6d6e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
6d6f0 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 e also contains
6d700 73 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 some state infor
6d710 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 mation..*/.struc
6d720 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 t Sqlite3Config
6d730 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 {. int bMemstat
6d740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d750 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
6d760 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 enable memory s
6d770 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 tatus */. int b
6d780 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 CoreMutex;
6d790 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d7a0 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 True to enable c
6d7b0 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a ore mutexing */.
6d7c0 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 int bFullMutex
6d7d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d7e0 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 /* True to e
6d7f0 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 nable full mutex
6d800 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 ing */. int mxS
6d810 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 trlen;
6d820 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
6d830 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e ximum string len
6d840 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c gth */. int szL
6d850 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 ookaside;
6d860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
6d870 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 fault lookaside
6d880 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 buffer size */.
6d890 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b int nLookaside;
6d8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d8b0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f /* Default lo
6d8c0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63 okaside buffer c
6d8d0 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ount */. sqlite
6d8e0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 3_mem_methods m;
6d8f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
6d900 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
6d910 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 allocation inter
6d920 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 face */. sqlite
6d930 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
6d940 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c mutex; /* L
6d950 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 ow-level mutex i
6d960 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 nterface */. sq
6d970 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
6d980 68 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 20 hods pcache;
6d990 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67 /* Low-level pag
6d9a0 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 e-cache interfac
6d9b0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65 e */. void *pHe
6d9c0 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ap;
6d9d0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70 /* Heap
6d9e0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a storage space *
6d9f0 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20 /. int nHeap;
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da10 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
6da20 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e pHeap[] */. in
6da30 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20 t mnReq, mxReq;
6da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da50 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68 /* Min and max h
6da60 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69 7a eap requests siz
6da70 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 es */. void *pS
6da80 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 cratch;
6da90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72 /* Scr
6daa0 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 atch memory */.
6dab0 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 int szScratch;
6dac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dad0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 /* Size of ea
6dae0 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65 ch scratch buffe
6daf0 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61 r */. int nScra
6db00 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 tch;
6db10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
6db20 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 er of scratch bu
6db30 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 ffers */. void
6db40 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 *pPage;
6db50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6db60 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 Page cache memor
6db70 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 y */. int szPag
6db80 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
6db90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
6dba0 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e of each page in
6dbb0 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e pPage[] */. in
6dbc0 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 t nPage;
6dbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dbe0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
6dbf0 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f es in pPage[] */
6dc00 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53 . int mxParserS
6dc10 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 tack;
6dc20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 /* maximum
6dc30 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 depth of the par
6dc40 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 ser stack */. i
6dc50 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 6e nt sharedCacheEn
6dc60 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 abled;
6dc70 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 72 /* true if shar
6dc80 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e ed-cache mode en
6dc90 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 68 abled */. /* Th
6dca0 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62 65 e above might be
6dcb0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 initialized to
6dcc0 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 66 non-zero. The f
6dcd0 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 6f ollowing need to
6dce0 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e 69 always. ** ini
6dcf0 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c 20 tially be zero,
6dd00 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 6e however. */. in
6dd10 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 t isInit;
6dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd30 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69 6e /* True after in
6dd40 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73 itialization has
6dd50 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 69 finished */. i
6dd60 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 20 nt inProgress;
6dd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dd80 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20 69 /* True while i
6dd90 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e nitialization in
6dda0 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 69 progress */. i
6ddb0 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b 20 nt isMutexInit;
6ddc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ddd0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d /* True after m
6dde0 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74 69 utexes are initi
6ddf0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 alized */. int
6de00 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20 isMallocInit;
6de10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6de20 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c True after mall
6de30 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 oc is initialize
6de40 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43 61 d */. int isPCa
6de50 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20 20 cheInit;
6de60 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
6de70 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73 after malloc is
6de80 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6de90 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
6dea0 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 20 *pInitMutex;
6deb0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 /* Mutex use
6dec0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 d by sqlite3_ini
6ded0 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69 tialize() */. i
6dee0 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 nt nRefInitMutex
6def0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6df00 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
6df10 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 65 ers of pInitMute
6df20 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 x */.};../*.** C
6df30 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 70 ontext pointer p
6df40 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f 75 assed down throu
6df50 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c 6b gh the tree-walk
6df60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b ..*/.struct Walk
6df70 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78 er {. int (*xEx
6df80 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b prCallback)(Walk
6df90 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 20 er*, Expr*);
6dfa0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 /* Callback for
6dfb0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a expressions */.
6dfc0 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 43 int (*xSelectC
6dfd0 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a allback)(Walker*
6dfe0 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43 ,Select*); /* C
6dff0 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c 45 allback for SELE
6e000 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a CTs */. Parse *
6e010 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
6e020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e030 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e /* Parser con
6e040 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f text. */. unio
6e050 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 n {
6e060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e070 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 64 /* Extra d
6e080 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b ata for callback
6e090 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 */. NameCont
6e0a0 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 ext *pNC;
6e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e0c0 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e /* Naming con
6e0d0 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 text */. int
6e0e0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
6e0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e100 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
6e110 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 r value */. } u
6e120 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 ;.};../* Forward
6e130 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f declarations */
6e140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e150 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 int sqlite3WalkE
6e160 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 xpr(Walker*, Exp
6e170 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6e180 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
6e190 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b alkExprList(Walk
6e1a0 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b er*, ExprList*);
6e1b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e1c0 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
6e1d0 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53 elect(Walker*, S
6e1e0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
6e1f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6e200 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
6e210 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 r(Walker*, Selec
6e220 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6e230 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
6e240 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 alkSelectFrom(Wa
6e250 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b lker*, Select*);
6e260 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63 ../*.** Return c
6e270 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 ode from the par
6e280 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 20 se-tree walking
6e290 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 74 primitives and t
6e2a0 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b heir.** callback
6e2b0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 s..*/.#define WR
6e2c0 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 20 C_Continue 0
6e2d0 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f /* Continue do
6e2e0 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e wn into children
6e2f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f */.#define WRC_
6e300 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 20 Prune 1
6e310 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e /* Omit children
6e320 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 61 but continue wa
6e330 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a lking siblings *
6e340 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 62 /.#define WRC_Ab
6e350 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f 2a ort 2 /*
6e360 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 Abandon the tre
6e370 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a e walk */../*.**
6e380 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f Assuming zIn po
6e390 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
6e3a0 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d t byte of a UTF-
6e3b0 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 8 character,.**
6e3c0 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 advance zIn to p
6e3d0 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 oint to the firs
6e3e0 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 t byte of the ne
6e3f0 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 xt UTF-8 charact
6e400 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 er..*/.#define S
6e410 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 QLITE_SKIP_UTF8(
6e420 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20 zIn) {
6e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
6e440 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 if( (*(zIn++))
6e450 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 >=0xc0 ){
6e460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e470 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 \. whi
6e480 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 le( (*zIn & 0xc0
6e490 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b )==0x80 ){ zIn++
6e4a0 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 ; }
6e4b0 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 \. }
6e4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e4e0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a \.}../*
6e4f0 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 .** The SQLITE_C
6e500 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 ORRUPT_BKPT macr
6e510 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 o can be either
6e520 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 a constant (for
6e530 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 production.** bu
6e540 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74 ilds) or a funct
6e550 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 ion call (for de
6e560 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74 bugging). If it
6e570 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 is a function c
6e580 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 all,.** it allow
6e590 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 s the operator t
6e5a0 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 o set a breakpoi
6e5b0 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77 nt at the spot w
6e5c0 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a here database.**
6e5d0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 corruption is f
6e5e0 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a irst detected..*
6e5f0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
6e600 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
6e610 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6e620 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b e3Corrupt(void);
6e630 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
6e640 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71 _CORRUPT_BKPT sq
6e650 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23 lite3Corrupt().#
6e660 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
6e670 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
6e680 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 T SQLITE_CORRUPT
6e690 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
6e6a0 68 65 20 63 74 79 70 65 2e 68 20 68 65 61 64 65 he ctype.h heade
6e6b0 72 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72 20 r is needed for
6e6c0 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 74 65 6d non-ASCII system
6e6d0 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 0a 2a s. It is also.*
6e6e0 2a 20 6e 65 65 64 65 64 20 62 79 20 46 54 53 33 * needed by FTS3
6e6f0 20 77 68 65 6e 20 46 54 53 33 20 69 73 20 69 6e when FTS3 is in
6e700 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 61 6d cluded in the am
6e710 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 algamation..*/.#
6e720 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
6e730 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 5c 0a 20 TE_ASCII) || \.
6e740 20 20 20 28 64 65 66 69 6e 65 64 28 53 51 4c 49 (defined(SQLI
6e750 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 TE_ENABLE_FTS3)
6e760 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
6e770 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 29 29 E_AMALGAMATION))
6e780 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 .# include <ctyp
6e790 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e.h>.#endif../*.
6e7a0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
6e7b0 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68 macros mimic th
6e7c0 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 e standard libra
6e7d0 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75 ry functions tou
6e7e0 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 61 pper(),.** isspa
6e7f0 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c ce(), isalnum(),
6e800 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69 isdigit() and i
6e810 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65 sxdigit(), respe
6e820 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 ctively. The.**
6e830 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20 sqlite versions
6e840 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53 only work for AS
6e850 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20 CII characters,
6e860 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f regardless of lo
6e870 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 cale..*/.#ifdef
6e880 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64 SQLITE_ASCII.# d
6e890 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 75 efine sqlite3Tou
6e8a0 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e 28 pper(x) ((x)&~(
6e8b0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
6e8c0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
6e8d0 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65 66 x)]&0x20)).# def
6e8e0 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 ine sqlite3Isspa
6e8f0 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 ce(x) (sqlite3
6e900 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
6e910 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
6e920 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 1).# define sqli
6e930 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 te3Isalnum(x)
6e940 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 (sqlite3CtypeMap
6e950 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
6e960 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 66 (x)]&0x06).# def
6e970 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70 ine sqlite3Isalp
6e980 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 ha(x) (sqlite3
6e990 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
6e9a0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
6e9b0 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 2).# define sqli
6e9c0 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 20 te3Isdigit(x)
6e9d0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 (sqlite3CtypeMap
6e9e0 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
6e9f0 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 66 (x)]&0x04).# def
6ea00 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 69 ine sqlite3Isxdi
6ea10 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 33 git(x) (sqlite3
6ea20 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
6ea30 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
6ea40 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 8).# define sqli
6ea50 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20 te3Tolower(x)
6ea60 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c (sqlite3UpperToL
6ea70 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 ower[(unsigned c
6ea80 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 0a har)(x)]).#else.
6ea90 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6eaa0 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 75 Toupper(x) tou
6eab0 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63 pper((unsigned c
6eac0 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e har)(x)).# defin
6ead0 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 e sqlite3Isspace
6eae0 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 75 (x) isspace((u
6eaf0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 nsigned char)(x)
6eb00 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
6eb10 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 69 e3Isalnum(x) i
6eb20 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 64 salnum((unsigned
6eb30 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 char)(x)).# def
6eb40 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70 ine sqlite3Isalp
6eb50 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 28 ha(x) isalpha(
6eb60 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
6eb70 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c x)).# define sql
6eb80 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 ite3Isdigit(x)
6eb90 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 6e isdigit((unsign
6eba0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
6ebb0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 efine sqlite3Isx
6ebc0 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 67 digit(x) isxdig
6ebd0 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 it((unsigned cha
6ebe0 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 r)(x)).# define
6ebf0 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 sqlite3Tolower(x
6ec00 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e 73 ) tolower((uns
6ec10 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a igned char)(x)).
6ec20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
6ec30 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 ternal function
6ec40 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 prototypes.*/.SQ
6ec50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ec60 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
6ec70 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f const char *, co
6ec80 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
6ec90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6eca0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
6ecb0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
6ecc0 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 *, u8);.SQLITE_P
6ecd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6ece0 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 e3Strlen30(const
6ecf0 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 69 6e 65 char*);.#define
6ed00 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 sqlite3StrNICmp
6ed10 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d sqlite3_strnicm
6ed20 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 p..SQLITE_PRIVAT
6ed30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c E int sqlite3Mal
6ed40 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 locInit(void);.S
6ed50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ed60 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 id sqlite3Malloc
6ed70 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 End(void);.SQLIT
6ed80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
6ed90 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e sqlite3Malloc(in
6eda0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6edb0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6edc0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b MallocZero(int);
6edd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ede0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d void *sqlite3DbM
6edf0 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 allocZero(sqlite
6ee00 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 3*, int);.SQLITE
6ee10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6ee20 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
6ee30 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 w(sqlite3*, int)
6ee40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ee50 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 char *sqlite3Db
6ee60 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c StrDup(sqlite3*,
6ee70 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
6ee80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
6ee90 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e r *sqlite3DbStrN
6eea0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e Dup(sqlite3*,con
6eeb0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a st char*, int);.
6eec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6eed0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c oid *sqlite3Real
6eee0 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b loc(void*, int);
6eef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ef00 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 void *sqlite3DbR
6ef10 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c eallocOrFree(sql
6ef20 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 ite3 *, void *,
6ef30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6ef40 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
6ef50 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 e3DbRealloc(sqli
6ef60 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 te3 *, void *, i
6ef70 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6ef80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6ef90 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 2c DbFree(sqlite3*,
6efa0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
6efb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6efc0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f te3MallocSize(vo
6efd0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
6efe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6eff0 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c DbMallocSize(sql
6f000 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 ite3*, void*);.S
6f010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6f020 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74 id *sqlite3Scrat
6f030 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 chMalloc(int);.S
6f040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6f050 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 id sqlite3Scratc
6f060 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 hFree(void*);.SQ
6f070 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f080 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 d *sqlite3PageMa
6f090 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
6f0a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f0b0 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 qlite3PageFree(v
6f0c0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
6f0d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f0e0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
6f0f0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
6f100 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f110 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f e3BenignMallocHo
6f120 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 oks(void (*)(voi
6f130 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 d), void (*)(voi
6f140 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d));.SQLITE_PRIV
6f150 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
6f160 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 20 emoryAlarm(void
6f170 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 (*)(void*, sqlit
6f180 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c 20 e3_int64, int),
6f190 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 void*, sqlite3_i
6f1a0 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 6e nt64);../*.** On
6f1b0 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61 6d systems with am
6f1c0 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65 20 ple stack space
6f1d0 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72 74 and that support
6f1e0 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65 0a alloca(), make.
6f1f0 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63 61 ** use of alloca
6f200 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 () to obtain spa
6f210 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75 74 ce for large aut
6f220 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e 20 omatic objects.
6f230 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 By default,.**
6f240 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f obtain space fro
6f250 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a m malloc()..**.*
6f260 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 20 72 * The alloca() r
6f270 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 65 74 outine never ret
6f280 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 69 73 urns NULL. This
6f290 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f 64 65 will cause code
6f2a0 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 20 64 paths.** that d
6f2b0 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 65 33 eal with sqlite3
6f2c0 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 61 69 StackAlloc() fai
6f2d0 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e 72 65 lures to be unre
6f2e0 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 64 achable..*/.#ifd
6f2f0 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 41 4c ef SQLITE_USE_AL
6f300 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 73 71 LOCA.# define sq
6f310 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 lite3StackAllocR
6f320 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f 63 61 aw(D,N) alloca
6f330 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c (N).# define sql
6f340 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 ite3StackAllocZe
6f350 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 74 28 ro(D,N) memset(
6f360 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 4e 29 alloca(N), 0, N)
6f370 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6f380 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 29 20 3StackFree(D,P)
6f390 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 20 64 .#else.# d
6f3a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 efine sqlite3Sta
6f3b0 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 ckAllocRaw(D,N)
6f3c0 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f sqlite3DbMallo
6f3d0 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 66 69 cRaw(D,N).# defi
6f3e0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 ne sqlite3StackA
6f3f0 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 73 llocZero(D,N) s
6f400 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
6f410 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 6e 65 ro(D,N).# define
6f420 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
6f430 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 71 6c e(D,P) sql
6f440 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 29 0a ite3DbFree(D,P).
6f450 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
6f460 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
6f470 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 SYS3.SQLITE_PRIV
6f480 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
6f490 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
6f4a0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
6f4b0 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 ys3(void);.#endi
6f4c0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
6f4d0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 53 ENABLE_MEMSYS5.S
6f4e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
6f4f0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
6f500 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
6f510 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f MemGetMemsys5(vo
6f520 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 id);.#endif...#i
6f530 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 fndef SQLITE_MUT
6f540 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50 EX_OMIT.SQLITE_P
6f550 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 RIVATE sqlite3
6f560 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
6f570 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 sqlite3DefaultMu
6f580 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 tex(void);.SQLIT
6f590 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
6f5a0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
6f5b0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 e3MutexAlloc(int
6f5c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f5d0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d E int sqlite3M
6f5e0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a utexInit(void);.
6f5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6f600 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 int sqlite3Mute
6f610 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 xEnd(void);.#end
6f620 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
6f630 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
6f640 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a atusValue(int);.
6f650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f660 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
6f670 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a sAdd(int, int);.
6f680 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f690 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
6f6a0 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a sSet(int, int);.
6f6b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f6c0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e int sqlite3IsNaN
6f6d0 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 (double);..SQLIT
6f6e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f6f0 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 53 qlite3VXPrintf(S
6f700 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 trAccum*, int, c
6f710 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c onst char*, va_l
6f720 69 73 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ist);.#ifndef SQ
6f730 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a LITE_OMIT_TRACE.
6f740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f750 6f 69 64 20 73 71 6c 69 74 65 33 58 50 72 69 6e oid sqlite3XPrin
6f760 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 6f tf(StrAccum*, co
6f770 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
6f780 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
6f790 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
6f7a0 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 ite3MPrintf(sqli
6f7b0 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a te3*,const char*
6f7c0 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 , ...);.SQLITE_P
6f7d0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
6f7e0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c ite3VMPrintf(sql
6f7f0 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
6f800 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c *, va_list);.SQL
6f810 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
6f820 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 *sqlite3MAppend
6f830 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a f(sqlite3*,char*
6f840 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e ,const char*,...
6f850 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 );.#if defined(S
6f860 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 QLITE_TEST) || d
6f870 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
6f880 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 BUG).SQLITE_PRIV
6f890 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
6f8a0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f e3DebugPrintf(co
6f8b0 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
6f8c0 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 .#endif.#if defi
6f8d0 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
6f8e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f8f0 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 void *sqlite3T
6f900 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e estTextToPtr(con
6f910 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 st char*);.#endi
6f920 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
6f930 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 void sqlite3Set
6f940 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 String(char **,
6f950 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 sqlite3*, const
6f960 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c char*, ...);.SQL
6f970 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f980 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
6f990 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 (Parse*, const c
6f9a0 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 har*, ...);.SQLI
6f9b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f9c0 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 sqlite3ErrorClea
6f9d0 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 r(Parse*);.SQLIT
6f9e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6f9f0 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 lite3Dequote(cha
6fa00 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6fa10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b ATE int sqlite3K
6fa20 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 eywordCode(const
6fa30 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c unsigned char*,
6fa40 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6fa50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6fa60 33 52 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 3RunParser(Parse
6fa70 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
6fa80 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 char **);.SQLITE
6fa90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6faa0 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e lite3FinishCodin
6fab0 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 g(Parse*);.SQLIT
6fac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6fad0 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
6fae0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6faf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fb00 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
6fb10 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a eg(Parse*,int);.
6fb20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6fb30 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d nt sqlite3GetTem
6fb40 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e pRange(Parse*,in
6fb50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6fb60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
6fb70 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 eleaseTempRange(
6fb80 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b Parse*,int,int);
6fb90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fba0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
6fbb0 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c rAlloc(sqlite3*,
6fbc0 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a int,const Token*
6fbd0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6fbe0 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
6fbf0 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a te3Expr(sqlite3*
6fc00 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
6fc10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fc20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
6fc30 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 prAttachSubtrees
6fc40 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c (sqlite3*,Expr*,
6fc50 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 Expr*,Expr*);.SQ
6fc60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
6fc70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 r *sqlite3PExpr(
6fc80 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 Parse*, int, Exp
6fc90 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 r*, Expr*, const
6fca0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
6fcb0 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
6fcc0 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 qlite3ExprAnd(sq
6fcd0 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 lite3*,Expr*, Ex
6fce0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
6fcf0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
6fd00 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 e3ExprFunction(P
6fd10 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c arse*,ExprList*,
6fd20 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
6fd30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fd40 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 lite3ExprAssignV
6fd50 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c arNumber(Parse*,
6fd60 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6fd70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fd80 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 ite3ExprDelete(s
6fd90 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b qlite3*, Expr*);
6fda0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fdb0 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 ExprList *sqlite
6fdc0 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 3ExprListAppend(
6fdd0 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a Parse*,ExprList*
6fde0 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ,Expr*);.SQLITE_
6fdf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fe00 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e ite3ExprListSetN
6fe10 61 6d 65 28 50 61 72 73 65 2a 2c 45 78 70 72 4c ame(Parse*,ExprL
6fe20 69 73 74 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 ist*,Token*,int)
6fe30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fe40 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
6fe50 72 4c 69 73 74 53 65 74 53 70 61 6e 28 50 61 72 rListSetSpan(Par
6fe60 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 se*,ExprList*,Ex
6fe70 70 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 prSpan*);.SQLITE
6fe80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fe90 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
6fea0 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 ete(sqlite3*, Ex
6feb0 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 prList*);.SQLITE
6fec0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6fed0 69 74 65 33 49 6e 69 74 28 73 71 6c 69 74 65 33 ite3Init(sqlite3
6fee0 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 *, char**);.SQLI
6fef0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6ff00 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 qlite3InitCallba
6ff10 63 6b 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 ck(void*, int, c
6ff20 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a har**, char**);.
6ff30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ff40 6f 69 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d oid sqlite3Pragm
6ff50 61 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c a(Parse*,Token*,
6ff60 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e Token*,Token*,in
6ff70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6ff80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
6ff90 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 esetInternalSche
6ffa0 6d 61 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ma(sqlite3*, int
6ffb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ffc0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
6ffd0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c ginParse(Parse*,
6ffe0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6fff0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70000 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 3CommitInternalC
70010 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 hanges(sqlite3*)
70020 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70030 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 Table *sqlite3R
70040 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 esultSetOfSelect
70050 28 50 61 72 73 65 2a 2c 53 65 6c 65 63 74 2a 29 (Parse*,Select*)
70060 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 void sqlite3Ope
70080 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72 nMasterTable(Par
70090 73 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 se *, int);.SQLI
700a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
700b0 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c sqlite3StartTabl
700c0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c e(Parse*,Token*,
700d0 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69 Token*,int,int,i
700e0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f nt,int);.SQLITE_
700f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70100 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61 ite3AddColumn(Pa
70110 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 rse*,Token*);.SQ
70120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70130 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e d sqlite3AddNotN
70140 75 6c 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 ull(Parse*, int)
70150 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70160 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
70170 50 72 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65 PrimaryKey(Parse
70180 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
70190 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
701a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
701b0 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 d sqlite3AddChec
701c0 6b 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 kConstraint(Pars
701d0 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 e*, Expr*);.SQLI
701e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
701f0 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e sqlite3AddColumn
70200 54 79 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 Type(Parse*,Toke
70210 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
70220 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70230 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 AddDefaultValue(
70240 50 61 72 73 65 2a 2c 45 78 70 72 53 70 61 6e 2a Parse*,ExprSpan*
70250 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70260 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
70270 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72 dCollateType(Par
70280 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 se*, Token*);.SQ
70290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
702a0 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c d sqlite3EndTabl
702b0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c e(Parse*,Token*,
702c0 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 29 3b Token*,Select*);
702d0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
702e0 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 65 33 Bitvec *sqlite3
702f0 42 69 74 76 65 63 43 72 65 61 74 65 28 75 33 32 BitvecCreate(u32
70300 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70310 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 E int sqlite3Bit
70320 76 65 63 54 65 73 74 28 42 69 74 76 65 63 2a 2c vecTest(Bitvec*,
70330 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u32);.SQLITE_PR
70340 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
70350 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 3BitvecSet(Bitve
70360 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 c*, u32);.SQLITE
70370 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70380 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
70390 28 42 69 74 76 65 63 2a 2c 20 75 33 32 2c 20 76 (Bitvec*, u32, v
703a0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
703b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
703c0 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
703d0 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 Bitvec*);.SQLITE
703e0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
703f0 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 ite3BitvecSize(B
70400 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f itvec*);.SQLITE_
70410 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
70420 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e te3BitvecBuiltin
70430 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a Test(int,int*);.
70440 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70450 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52 RowSet *sqlite3R
70460 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65 owSetInit(sqlite
70470 33 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 3*, void*, unsig
70480 6e 65 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ned int);.SQLITE
70490 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
704a0 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 lite3RowSetClear
704b0 28 52 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 54 (RowSet*);.SQLIT
704c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
704d0 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
704e0 72 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 29 rt(RowSet*, i64)
704f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70500 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 int sqlite3RowS
70510 65 74 54 65 73 74 28 52 6f 77 53 65 74 2a 2c 20 etTest(RowSet*,
70520 75 38 20 69 42 61 74 63 68 2c 20 69 36 34 29 3b u8 iBatch, i64);
70530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70540 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 int sqlite3RowSe
70550 74 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c 20 69 tNext(RowSet*, i
70560 36 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 64*);..SQLITE_PR
70570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70580 65 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 e3CreateView(Par
70590 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e se*,Token*,Token
705a0 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a *,Token*,Select*
705b0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 ,int,int);..#if
705c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
705d0 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 OMIT_VIEW) || !d
705e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
705f0 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 IT_VIRTUALTABLE)
70600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70610 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 int sqlite3Vie
70620 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 wGetColumnNames(
70630 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a Parse*,Table*);.
70640 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
70650 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c qlite3ViewGetCol
70660 75 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a umnNames(A,B) 0.
70670 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
70680 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70690 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 te3DropTable(Par
706a0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 se*, SrcList*, i
706b0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
706c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
706d0 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 lite3DeleteTable
706e0 28 54 61 62 6c 65 2a 29 3b 0a 23 69 66 6e 64 65 (Table*);.#ifnde
706f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
70700 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 53 51 4c 49 TOINCREMENT.SQLI
70710 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
70720 64 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 d sqlite3Autoinc
70730 72 65 6d 65 6e 74 42 65 67 69 6e 28 50 61 72 73 rementBegin(Pars
70740 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 e *pParse);.SQLI
70750 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
70760 64 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 d sqlite3Autoinc
70770 72 65 6d 65 6e 74 45 6e 64 28 50 61 72 73 65 20 rementEnd(Parse
70780 2a 70 50 61 72 73 65 29 3b 0a 23 65 6c 73 65 0a *pParse);.#else.
70790 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
707a0 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 AutoincrementBeg
707b0 69 6e 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 in(X).# define s
707c0 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d qlite3Autoincrem
707d0 65 6e 74 45 6e 64 28 58 29 0a 23 65 6e 64 69 66 entEnd(X).#endif
707e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
707f0 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65 void sqlite3Inse
70800 72 74 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 rt(Parse*, SrcLi
70810 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 st*, ExprList*,
70820 53 65 6c 65 63 74 2a 2c 20 49 64 4c 69 73 74 2a Select*, IdList*
70830 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70840 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
70850 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 ite3ArrayAllocat
70860 65 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a e(sqlite3*,void*
70870 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e ,int,int,int*,in
70880 74 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 t*,int*);.SQLITE
70890 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 _PRIVATE IdList
708a0 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 *sqlite3IdListAp
708b0 70 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c 20 49 pend(sqlite3*, I
708c0 64 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b dList*, Token*);
708d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
708e0 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69 73 int sqlite3IdLis
708f0 74 49 6e 64 65 78 28 49 64 4c 69 73 74 2a 2c 63 tIndex(IdList*,c
70900 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
70910 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c ITE_PRIVATE SrcL
70920 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ist *sqlite3SrcL
70930 69 73 74 45 6e 6c 61 72 67 65 28 73 71 6c 69 74 istEnlarge(sqlit
70940 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 e3*, SrcList*, i
70950 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70960 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 _PRIVATE SrcList
70970 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 *sqlite3SrcList
70980 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33 2a 2c Append(sqlite3*,
70990 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e SrcList*, Token
709a0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
709b0 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 TE_PRIVATE SrcLi
709c0 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 st *sqlite3SrcLi
709d0 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d stAppendFromTerm
709e0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 (Parse*, SrcList
709f0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
70a00 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 *,.
70a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70a20 20 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c Token*,
70a30 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 2a 2c Select*, Expr*,
70a40 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 IdList*);.SQLIT
70a50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70a60 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 qlite3SrcListInd
70a70 65 78 65 64 42 79 28 50 61 72 73 65 20 2a 2c 20 exedBy(Parse *,
70a80 53 72 63 4c 69 73 74 20 2a 2c 20 54 6f 6b 65 6e SrcList *, Token
70a90 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
70aa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
70ab0 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 50 ndexedByLookup(P
70ac0 61 72 73 65 20 2a 2c 20 73 74 72 75 63 74 20 53 arse *, struct S
70ad0 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 29 3b 0a rcList_item *);.
70ae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70af0 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 oid sqlite3SrcLi
70b00 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 stShiftJoinType(
70b10 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 SrcList*);.SQLIT
70b20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70b30 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 qlite3SrcListAss
70b40 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65 ignCursors(Parse
70b50 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 *, SrcList*);.SQ
70b60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70b70 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 d sqlite3IdListD
70b80 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 elete(sqlite3*,
70b90 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 IdList*);.SQLITE
70ba0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70bb0 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 lite3SrcListDele
70bc0 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 te(sqlite3*, Src
70bd0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
70be0 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 RIVATE Index *sq
70bf0 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 lite3CreateIndex
70c00 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 (Parse*,Token*,T
70c10 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 oken*,SrcList*,E
70c20 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54 6f 6b xprList*,int,Tok
70c30 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 en*,.
70c40 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b Tok
70c50 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a en*, int, int);.
70c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70c70 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49 oid sqlite3DropI
70c80 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53 72 63 ndex(Parse*, Src
70c90 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c List*, int);.SQL
70ca0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
70cb0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 50 61 sqlite3Select(Pa
70cc0 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 53 rse*, Select*, S
70cd0 65 6c 65 63 74 44 65 73 74 2a 29 3b 0a 53 51 4c electDest*);.SQL
70ce0 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
70cf0 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
70d00 74 4e 65 77 28 50 61 72 73 65 2a 2c 45 78 70 72 tNew(Parse*,Expr
70d10 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 List*,SrcList*,E
70d20 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 0a xpr*,ExprList*,.
70d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70d40 20 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 45 Expr*,E
70d50 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45 78 70 xprList*,int,Exp
70d60 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 r*,Expr*);.SQLIT
70d70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70d80 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 qlite3SelectDele
70d90 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c te(sqlite3*, Sel
70da0 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ect*);.SQLITE_PR
70db0 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
70dc0 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 ite3SrcListLooku
70dd0 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 p(Parse*, SrcLis
70de0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
70df0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
70e00 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 2a sReadOnly(Parse*
70e10 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a , Table*, int);.
70e20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70e30 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 oid sqlite3OpenT
70e40 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 able(Parse*, int
70e50 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62 2c 20 iCur, int iDb,
70e60 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 Table*, int);.#i
70e70 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
70e80 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 _ENABLE_UPDATE_D
70e90 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 ELETE_LIMIT) &&
70ea0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
70eb0 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 53 OMIT_SUBQUERY).S
70ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
70ed0 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d 69 74 pr *sqlite3Limit
70ee0 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c 20 53 Where(Parse *, S
70ef0 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a rcList *, Expr *
70f00 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20 45 78 , ExprList *, Ex
70f10 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20 63 68 pr *, Expr *, ch
70f20 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 ar *);.#endif.SQ
70f30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70f40 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 d sqlite3DeleteF
70f50 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72 63 4c rom(Parse*, SrcL
70f60 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 ist*, Expr*);.SQ
70f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70f80 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 d sqlite3Update(
70f90 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
70fa0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 , ExprList*, Exp
70fb0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
70fc0 5f 50 52 49 56 41 54 45 20 57 68 65 72 65 49 6e _PRIVATE WhereIn
70fd0 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65 fo *sqlite3Where
70fe0 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20 53 72 Begin(Parse*, Sr
70ff0 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 45 cList*, Expr*, E
71000 78 70 72 4c 69 73 74 2a 2a 2c 20 75 31 36 29 3b xprList**, u16);
71010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71020 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72 void sqlite3Wher
71030 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a 29 eEnd(WhereInfo*)
71040 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71050 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
71060 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50 61 CodeGetColumn(Pa
71070 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e rse*, Table*, in
71080 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 t, int, int, int
71090 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
710a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
710b0 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 prCodeMove(Parse
710c0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
710d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
710e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
710f0 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 prCodeCopy(Parse
71100 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
71110 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71120 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
71130 70 72 43 61 63 68 65 53 74 6f 72 65 28 50 61 72 prCacheStore(Par
71140 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 se*, int, int, i
71150 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71160 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71170 45 78 70 72 43 61 63 68 65 50 75 73 68 28 50 61 ExprCachePush(Pa
71180 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 rse*);.SQLITE_PR
71190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
711a0 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 50 e3ExprCachePop(P
711b0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
711c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
711d0 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
711e0 65 52 65 6d 6f 76 65 28 50 61 72 73 65 2a 2c 20 eRemove(Parse*,
711f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71200 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71210 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 3ExprCacheClear(
71220 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
71230 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71240 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 ite3ExprCacheAff
71250 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73 inityChange(Pars
71260 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 e*, int, int);.S
71270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71280 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 id sqlite3ExprHa
71290 72 64 43 6f 70 79 28 50 61 72 73 65 2a 2c 69 6e rdCopy(Parse*,in
712a0 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 t,int);.SQLITE_P
712b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
712c0 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 e3ExprCode(Parse
712d0 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
712e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
712f0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
71300 64 65 54 65 6d 70 28 50 61 72 73 65 2a 2c 20 45 deTemp(Parse*, E
71310 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c xpr*, int*);.SQL
71320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
71330 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
71340 61 72 67 65 74 28 50 61 72 73 65 2a 2c 20 45 78 arget(Parse*, Ex
71350 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 pr*, int);.SQLIT
71360 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
71370 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 lite3ExprCodeAnd
71380 43 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78 Cache(Parse*, Ex
71390 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 pr*, int);.SQLIT
713a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
713b0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f qlite3ExprCodeCo
713c0 6e 73 74 61 6e 74 73 28 50 61 72 73 65 2a 2c 20 nstants(Parse*,
713d0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
713e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
713f0 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 e3ExprCodeExprLi
71400 73 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c st(Parse*, ExprL
71410 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b ist*, int, int);
71420 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71430 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
71440 49 66 54 72 75 65 28 50 61 72 73 65 2a 2c 20 45 IfTrue(Parse*, E
71450 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b xpr*, int, int);
71460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71470 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
71480 49 66 46 61 6c 73 65 28 50 61 72 73 65 2a 2c 20 IfFalse(Parse*,
71490 45 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 Expr*, int, int)
714a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
714b0 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 Table *sqlite3F
714c0 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 indTable(sqlite3
714d0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 *,const char*, c
714e0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
714f0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c ITE_PRIVATE Tabl
71500 65 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 e *sqlite3Locate
71510 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74 Table(Parse*,int
71520 20 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68 isView,const ch
71530 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a ar*, const char*
71540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71550 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 E Index *sqlite3
71560 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 FindIndex(sqlite
71570 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
71580 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
71590 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
715a0 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 d sqlite3UnlinkA
715b0 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71 ndDeleteTable(sq
715c0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 lite3*,int,const
715d0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
715e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
715f0 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c ite3UnlinkAndDel
71600 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 eteIndex(sqlite3
71610 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 *,int,const char
71620 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71630 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
71640 61 63 75 75 6d 28 50 61 72 73 65 2a 29 3b 0a 53 acuum(Parse*);.S
71650 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
71660 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 t sqlite3RunVacu
71670 75 6d 28 63 68 61 72 2a 2a 2c 20 73 71 6c 69 74 um(char**, sqlit
71680 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
71690 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
716a0 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
716b0 73 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a sqlite3*, Token*
716c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
716d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
716e0 72 43 6f 6d 70 61 72 65 28 45 78 70 72 2a 2c 20 rCompare(Expr*,
716f0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
71700 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71710 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 te3ExprAnalyzeAg
71720 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e gregates(NameCon
71730 74 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 text*, Expr*);.S
71740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71750 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e id sqlite3ExprAn
71760 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d alyzeAggList(Nam
71770 65 43 6f 6e 74 65 78 74 2a 2c 45 78 70 72 4c 69 eContext*,ExprLi
71780 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
71790 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 VATE Vdbe *sqlit
717a0 65 33 47 65 74 56 64 62 65 28 50 61 72 73 65 2a e3GetVdbe(Parse*
717b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
717c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 E void sqlite3Pr
717d0 6e 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 ngSaveState(void
717e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
717f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 E void sqlite3Pr
71800 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 76 ngRestoreState(v
71810 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
71820 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71830 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 3PrngResetState(
71840 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
71850 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71860 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 e3RollbackAll(sq
71870 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
71880 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71890 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 ite3CodeVerifySc
718a0 68 65 6d 61 28 50 61 72 73 65 2a 2c 20 69 6e 74 hema(Parse*, int
718b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
718c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
718d0 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 ginTransaction(P
718e0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
718f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71900 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 sqlite3CommitTr
71910 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a ansaction(Parse*
71920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71930 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f E void sqlite3Ro
71940 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f llbackTransactio
71950 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 n(Parse*);.SQLIT
71960 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71970 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 qlite3Savepoint(
71980 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 54 6f 6b Parse*, int, Tok
71990 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
719a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
719b0 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 3CloseSavepoints
719c0 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c (sqlite3 *);.SQL
719d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
719e0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
719f0 73 74 61 6e 74 28 45 78 70 72 2a 29 3b 0a 53 51 stant(Expr*);.SQ
71a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71a10 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f sqlite3ExprIsCo
71a20 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 nstantNotJoin(Ex
71a30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
71a40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
71a50 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 ExprIsConstantOr
71a60 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 2a 29 3b Function(Expr*);
71a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71a80 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 int sqlite3ExprI
71a90 73 49 6e 74 65 67 65 72 28 45 78 70 72 2a 2c 20 sInteger(Expr*,
71aa0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
71ab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
71ac0 33 45 78 70 72 43 61 6e 42 65 4e 75 6c 6c 28 63 3ExprCanBeNull(c
71ad0 6f 6e 73 74 20 45 78 70 72 2a 29 3b 0a 53 51 4c onst Expr*);.SQL
71ae0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71af0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
71b00 49 73 4e 75 6c 6c 4a 75 6d 70 28 56 64 62 65 2a IsNullJump(Vdbe*
71b10 2c 20 63 6f 6e 73 74 20 45 78 70 72 2a 2c 20 69 , const Expr*, i
71b20 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
71b30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
71b40 69 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 ite3ExprNeedsNoA
71b50 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 63 6f ffinityChange(co
71b60 6e 73 74 20 45 78 70 72 2a 2c 20 63 68 61 72 29 nst Expr*, char)
71b70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71b80 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f int sqlite3IsRo
71b90 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 wid(const char*)
71ba0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71bb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e void sqlite3Gen
71bc0 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50 erateRowDelete(P
71bd0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
71be0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72 nt, int, int, Tr
71bf0 69 67 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 igger *, int);.S
71c00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71c10 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 id sqlite3Genera
71c20 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 teRowIndexDelete
71c30 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
71c40 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c int, 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 47 65 6e 65 72 61 74 65 49 sqlite3GenerateI
71c70 6e 64 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 ndexKey(Parse*,
71c80 49 6e 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 Index*, int, int
71c90 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
71ca0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71cb0 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 te3GenerateConst
71cc0 72 61 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73 raintChecks(Pars
71cd0 65 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e e*,Table*,int,in
71ce0 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
71cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71d00 20 20 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 int*,int
71d10 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
71d20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71d30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
71d40 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e ompleteInsertion
71d50 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
71d60 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c int, int, int*,
71d70 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b int, int, int);
71d80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71d90 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 int sqlite3OpenT
71da0 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 ableAndIndices(P
71db0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
71dc0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
71dd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71de0 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f lite3BeginWriteO
71df0 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c peration(Parse*,
71e00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
71e10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71e20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 sqlite3MultiWrit
71e30 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 e(Parse*);.SQLIT
71e40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71e50 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50 qlite3MayAbort(P
71e60 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
71e70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71e80 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e te3HaltConstrain
71e90 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 t(Parse*, int, c
71ea0 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 har*, int);.SQLI
71eb0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
71ec0 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 *sqlite3ExprDup(
71ed0 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 sqlite3*,Expr*,i
71ee0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
71ef0 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 ATE ExprList *sq
71f00 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
71f10 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 (sqlite3*,ExprLi
71f20 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 st*,int);.SQLITE
71f30 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 _PRIVATE SrcList
71f40 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 *sqlite3SrcList
71f50 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 Dup(sqlite3*,Src
71f60 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 List*,int);.SQLI
71f70 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 TE_PRIVATE IdLis
71f80 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 t *sqlite3IdList
71f90 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c Dup(sqlite3*,IdL
71fa0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
71fb0 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 IVATE Select *sq
71fc0 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 lite3SelectDup(s
71fd0 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c qlite3*,Select*,
71fe0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71ff0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
72000 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46 3FuncDefInsert(F
72010 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e uncDefHash*, Fun
72020 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 cDef*);.SQLITE_P
72030 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
72040 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 sqlite3FindFunct
72050 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e ion(sqlite3*,con
72060 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 st char*,int,int
72070 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ,u8,int);.SQLITE
72080 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
72090 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69 lite3RegisterBui
720a0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 ltinFunctions(sq
720b0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
720c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
720d0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 ite3RegisterDate
720e0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f TimeFunctions(vo
720f0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
72100 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
72110 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 RegisterGlobalFu
72120 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 nctions(void);.#
72130 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
72140 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
72150 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 E int sqlite3S
72160 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a afetyOn(sqlite3*
72170 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72180 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 E int sqlite3S
72190 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 afetyOff(sqlite3
721a0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 *);.#else.# defi
721b0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ne sqlite3Safety
721c0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65 On(A) 0.# define
721d0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
721e0 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 f(A) 0.#endif.SQ
721f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
72200 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 sqlite3SafetyCh
72210 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b eckOk(sqlite3*);
72220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72230 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
72240 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 yCheckSickOrOk(s
72250 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
72260 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
72270 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 lite3ChangeCooki
72280 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a e(Parse*, int);.
72290 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
722a0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
722b0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 && !defined(SQLI
722c0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
722d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
722e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 void sqlite3Mate
722f0 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73 rializeView(Pars
72300 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 e*, Table*, Expr
72310 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a *, int);.#endif.
72320 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
72330 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c OMIT_TRIGGER.SQL
72340 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
72350 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 id sqlite3BeginT
72360 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 rigger(Parse*, T
72370 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 oken*,Token*,int
72380 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 ,int,IdList*,Src
72390 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 List*,.
723a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
723b0 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 Expr*,int, int
723c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
723d0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
723e0 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 61 FinishTrigger(Pa
723f0 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65 rse*, TriggerSte
72400 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c p*, Token*);.SQL
72410 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
72420 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 id sqlite3DropTr
72430 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72 igger(Parse*, Sr
72440 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 cList*, int);.SQ
72450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
72460 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 oid sqlite3DropT
72470 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 2a riggerPtr(Parse*
72480 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c , Trigger*);.SQL
72490 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 ITE_PRIVATE Tr
724a0 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 igger *sqlite3Tr
724b0 69 67 67 65 72 73 45 78 69 73 74 28 50 61 72 73 iggersExist(Pars
724c0 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 e *, Table*, int
724d0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
724e0 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 *pMask);.SQLITE
724f0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
72500 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 er *sqlite3Trigg
72510 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20 erList(Parse *,
72520 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
72530 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
72540 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 sqlite3CodeRowTr
72550 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 igger(Parse*, Tr
72560 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 igger *, int, Ex
72570 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 prList*, int, Ta
72580 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 ble *,.
72590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
725a0 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 int, int, int
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 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 CodeRowTriggerDi
725e0 72 65 63 74 28 50 61 72 73 65 20 2a 2c 20 54 72 rect(Parse *, Tr
725f0 69 67 67 65 72 20 2a 2c 20 54 61 62 6c 65 20 2a igger *, Table *
72600 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 , int, int, int)
72610 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 74 65 56 ;. void sqliteV
72620 69 65 77 54 72 69 67 67 65 72 73 28 50 61 72 73 iewTriggers(Pars
72630 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 e*, Table*, Expr
72640 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 *, int, ExprList
72650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72660 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72670 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 3DeleteTriggerSt
72680 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 ep(sqlite3*, Tri
72690 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 ggerStep*);.SQLI
726a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 TE_PRIVATE Tri
726b0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 ggerStep *sqlite
726c0 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 3TriggerSelectSt
726d0 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 ep(sqlite3*,Sele
726e0 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ct*);.SQLITE_PRI
726f0 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74 VATE TriggerSt
72700 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 ep *sqlite3Trigg
72710 65 72 49 6e 73 65 72 74 53 74 65 70 28 73 71 6c erInsertStep(sql
72720 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64 ite3*,Token*, Id
72730 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 List*,.
72740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
72760 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a xprList*,Select*
72770 2c 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ,u8);.SQLITE_PRI
72780 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74 VATE TriggerSt
72790 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 ep *sqlite3Trigg
727a0 65 72 55 70 64 61 74 65 53 74 65 70 28 73 71 6c erUpdateStep(sql
727b0 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 ite3*,Token*,Exp
727c0 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 75 rList*, Expr*, u
727d0 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 8);.SQLITE_PRIVA
727e0 54 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 TE TriggerStep
727f0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 *sqlite3Trigger
72800 44 65 6c 65 74 65 53 74 65 70 28 73 71 6c 69 74 DeleteStep(sqlit
72810 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 e3*,Token*, Expr
72820 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72830 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72840 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 3DeleteTrigger(s
72850 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72 qlite3*, Trigger
72860 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72870 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72880 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
72890 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a Trigger(sqlite3*
728a0 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a ,int,const char*
728b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
728c0 45 20 20 20 75 33 32 20 73 71 6c 69 74 65 33 54 E u32 sqlite3T
728d0 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b 28 50 61 riggerColmask(Pa
728e0 72 73 65 2a 2c 54 72 69 67 67 65 72 2a 2c 45 78 rse*,Trigger*,Ex
728f0 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 69 6e 74 2c prList*,int,int,
72900 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a 23 20 64 Table*,int);.# d
72910 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 efine sqlite3Par
72920 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 28 28 seToplevel(p) ((
72930 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3f 20 p)->pToplevel ?
72940 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3a (p)->pToplevel :
72950 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 (p)).#else.# de
72960 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 fine sqlite3Trig
72970 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c 44 2c gersExist(B,C,D,
72980 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e 65 20 E,F) 0.# define
72990 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
729a0 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 66 69 gger(A,B).# defi
729b0 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 ne sqlite3DropTr
729c0 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23 20 iggerPtr(A,B).#
729d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 6e define sqlite3Un
729e0 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 linkAndDeleteTri
729f0 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64 65 gger(A,B,C).# de
72a00 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65 fine sqlite3Code
72a10 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c 43 RowTrigger(A,B,C
72a20 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 29 0a 23 20 ,D,E,F,G,H,I).#
72a30 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
72a40 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 72 65 deRowTriggerDire
72a50 63 74 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 29 0a ct(A,B,C,D,E,F).
72a60 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
72a70 54 72 69 67 67 65 72 4c 69 73 74 28 58 2c 20 59 TriggerList(X, Y
72a80 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ) 0.# define sql
72a90 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 ite3ParseTopleve
72aa0 6c 28 70 29 20 70 0a 23 20 64 65 66 69 6e 65 20 l(p) p.# define
72ab0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f sqlite3TriggerCo
72ac0 6c 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 2c 45 2c lmask(A,B,C,D,E,
72ad0 46 2c 47 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 F,G) 0.#endif..S
72ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
72af0 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 t sqlite3JoinTyp
72b00 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
72b10 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a , Token*, Token*
72b20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72b30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 E void sqlite3Cr
72b40 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 eateForeignKey(P
72b50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a arse*, ExprList*
72b60 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 , Token*, ExprLi
72b70 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 st*, int);.SQLIT
72b80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
72b90 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 qlite3DeferForei
72ba0 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e gnKey(Parse*, in
72bb0 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 t);.#ifndef SQLI
72bc0 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
72bd0 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 ATION.SQLITE_PRI
72be0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
72bf0 74 65 33 41 75 74 68 52 65 61 64 28 50 61 72 73 te3AuthRead(Pars
72c00 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a e*,Expr*,Schema*
72c10 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 ,SrcList*);.SQLI
72c20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
72c30 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
72c40 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f k(Parse*,int, co
72c50 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 nst char*, const
72c60 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 char*, const ch
72c70 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
72c80 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
72c90 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 te3AuthContextPu
72ca0 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 sh(Parse*, AuthC
72cb0 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 ontext*, const c
72cc0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
72cd0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
72ce0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 ite3AuthContextP
72cf0 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 op(AuthContext*)
72d00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72d10 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 int sqlite3Au
72d20 74 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65 2a thReadCol(Parse*
72d30 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 , const char *,
72d40 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e const char *, in
72d50 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 t);.#else.# defi
72d60 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 ne sqlite3AuthRe
72d70 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 65 ad(a,b,c,d).# de
72d80 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 fine sqlite3Auth
72d90 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 Check(a,b,c,d,e)
72da0 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 SQLITE_OK.#
72db0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 define sqlite3Au
72dc0 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 2c thContextPush(a,
72dd0 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 71 b,c).# define sq
72de0 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
72df0 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 28 Pop(a) ((void)(
72e00 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 a)).#endif.SQLIT
72e10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
72e20 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 qlite3Attach(Par
72e30 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 se*, Expr*, Expr
72e40 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
72e50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
72e60 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72 qlite3Detach(Par
72e70 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c se*, Expr*);.SQL
72e80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72e90 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 sqlite3BtreeFact
72ea0 6f 72 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ory(sqlite3 *db,
72eb0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
72ec0 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 lename,.
72ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
72ee0 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 nt omitJournal,
72ef0 69 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e 74 20 int nCache, int
72f00 66 6c 61 67 73 2c 20 42 74 72 65 65 20 2a 2a 70 flags, Btree **p
72f10 70 42 74 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f pBtree);.SQLITE_
72f20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72f30 74 65 33 46 69 78 49 6e 69 74 28 44 62 46 69 78 te3FixInit(DbFix
72f40 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69 6e 74 er*, Parse*, int
72f50 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 , const char*, c
72f60 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 onst Token*);.SQ
72f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
72f80 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 sqlite3FixSrcLi
72f90 73 74 28 44 62 46 69 78 65 72 2a 2c 20 53 72 63 st(DbFixer*, Src
72fa0 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
72fb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
72fc0 65 33 46 69 78 53 65 6c 65 63 74 28 44 62 46 69 e3FixSelect(DbFi
72fd0 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a xer*, Select*);.
72fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
72ff0 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 nt sqlite3FixExp
73000 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 r(DbFixer*, Expr
73010 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
73030 78 45 78 70 72 4c 69 73 74 28 44 62 46 69 78 65 xExprList(DbFixe
73040 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a r*, ExprList*);.
73050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
73060 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 nt sqlite3FixTri
73070 67 67 65 72 53 74 65 70 28 44 62 46 69 78 65 72 ggerStep(DbFixer
73080 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 *, TriggerStep*)
73090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
730a0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 int sqlite3AtoF
730b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 (const char *z,
730c0 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 double*);.SQLITE
730d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
730e0 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e ite3GetInt32(con
730f0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2a 29 st char *, int*)
73100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73110 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 73 int sqlite3Fits
73120 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 63 In64Bits(const c
73130 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c har *, int);.SQL
73140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
73150 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 sqlite3Utf16Byte
73160 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a Len(const void *
73170 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68 61 72 pData, int nChar
73180 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73190 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 E int sqlite3Utf
731a0 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 8CharLen(const c
731b0 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 har *pData, int
731c0 6e 42 79 74 65 29 3b 0a 53 51 4c 49 54 45 5f 50 nByte);.SQLITE_P
731d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
731e0 65 33 55 74 66 38 52 65 61 64 28 63 6f 6e 73 74 e3Utf8Read(const
731f0 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a u8*, const u8**
73200 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e );../*.** Routin
73210 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64 20 77 es to read and w
73220 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 rite variable-le
73230 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 ngth integers.
73240 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a These used to.**
73250 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 be defined loca
73260 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 lly, but now we
73270 75 73 65 20 74 68 65 20 76 61 72 69 6e 74 20 72 use the varint r
73280 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 75 outines in the u
73290 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 til.c.** file.
732a0 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 20 Code should use
732b0 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 the MACRO forms
732c0 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 56 61 below, as the Va
732d0 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e 73 0a rint32 versions.
732e0 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74 6f 20 ** are coded to
732f0 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e 67 6c assume the singl
73300 65 20 62 79 74 65 20 63 61 73 65 20 69 73 20 61 e byte case is a
73310 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64 20 28 lready handled (
73320 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 which .** the MA
73330 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a CRO form does)..
73340 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
73350 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 E int sqlite3Put
73360 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 Varint(unsigned
73370 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c char*, u64);.SQL
73380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
73390 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
733a0 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 32(unsigned char
733b0 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f *, u32);.SQLITE_
733c0 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 PRIVATE u8 sqlit
733d0 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 e3GetVarint(cons
733e0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
733f0 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 *, u64 *);.SQLIT
73400 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c E_PRIVATE u8 sql
73410 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 ite3GetVarint32(
73420 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
73430 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 har *, u32 *);.S
73440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73450 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c t sqlite3VarintL
73460 65 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a en(u64 v);../*.*
73470 2a 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20 * The header of
73480 61 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 a record consist
73490 73 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 20 s of a sequence
734a0 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 variable-length
734b0 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 integers..** The
734c0 73 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 se integers are
734d0 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 6d almost always sm
734e0 61 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 6f all and are enco
734f0 64 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 ded as a single
73500 62 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c byte..** The fol
73510 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 lowing macros ta
73520 6b 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 69 ke advantage thi
73530 73 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 64 s fact to provid
73540 65 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 0a e a fast encode.
73550 2a 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 ** and decode of
73560 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e the integers in
73570 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 a record header
73580 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20 . It is faster
73590 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a for the common.*
735a0 2a 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 * case where the
735b0 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 integer is a si
735c0 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 ngle byte. It i
735d0 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 s a little slowe
735e0 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e r when the.** in
735f0 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 teger is two or
73600 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 more bytes. But
73610 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 overall it is f
73620 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 aster..**.** The
73630 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 following expre
73640 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 ssions are equiv
73650 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 alent:.**.**
73660 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 x = sqlite3GetV
73670 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 arint32( A, &B )
73680 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c ;.** x = sql
73690 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 ite3PutVarint32(
736a0 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 A, B );.**.**
736b0 20 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74 x = getVarint
736c0 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 32( A, B );.**
736d0 20 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e 74 x = putVarint
736e0 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 32( A, B );.**.*
736f0 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 /.#define getVar
73700 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 int32(A,B) (u8)
73710 28 28 2a 28 41 29 3c 28 75 38 29 30 78 38 30 29 ((*(A)<(u8)0x80)
73720 20 3f 20 28 28 42 29 20 3d 20 28 75 33 32 29 2a ? ((B) = (u32)*
73730 28 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 (A)),1 : sqlite3
73740 47 65 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c GetVarint32((A),
73750 20 28 75 33 32 20 2a 29 26 28 42 29 29 29 0a 23 (u32 *)&(B))).#
73760 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74 define putVarint
73770 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 28 28 32(A,B) (u8)(((
73780 75 33 32 29 28 42 29 3c 28 75 33 32 29 30 78 38 u32)(B)<(u32)0x8
73790 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 75 6e 0) ? (*(A) = (un
737a0 73 69 67 6e 65 64 20 63 68 61 72 29 28 42 29 29 signed char)(B))
737b0 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75 74 56 ,1 : sqlite3PutV
737c0 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 42 29 arint32((A), (B)
737d0 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 )).#define getVa
737e0 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 47 rint sqlite3G
737f0 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69 6e 65 etVarint.#define
73800 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 73 71 putVarint sq
73810 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 0a 0a lite3PutVarint..
73820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73830 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
73840 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 te3IndexAffinity
73850 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e 64 65 Str(Vdbe *, Inde
73860 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 x *);.SQLITE_PRI
73870 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
73880 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 3TableAffinitySt
73890 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65 20 r(Vdbe *, Table
738a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
738b0 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 TE char sqlite3C
738c0 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 45 ompareAffinity(E
738d0 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 xpr *pExpr, char
738e0 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 5f 50 aff2);.SQLITE_P
738f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73900 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f e3IndexAffinityO
73910 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 k(Expr *pExpr, c
73920 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 har idx_affinity
73930 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73940 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 E char sqlite3Ex
73950 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 prAffinity(Expr
73960 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f *pExpr);.SQLITE_
73970 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73980 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20 te3Atoi64(const
73990 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a 53 51 char*, i64*);.SQ
739a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
739b0 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 d sqlite3Error(s
739c0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f qlite3*, int, co
739d0 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a nst char*,...);.
739e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
739f0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 oid *sqlite3HexT
73a00 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a 2c 20 oBlob(sqlite3*,
73a10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
73a20 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt n);.SQLITE_PR
73a30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73a40 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 61 72 3TwoPartName(Par
73a50 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 se *, Token *, T
73a60 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2a oken *, Token **
73a70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73a80 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 E const char *sq
73a90 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 29 lite3ErrStr(int)
73aa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73ab0 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64 int sqlite3Read
73ac0 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 Schema(Parse *pP
73ad0 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 arse);.SQLITE_PR
73ae0 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
73af0 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 qlite3FindCollSe
73b00 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 65 6e q(sqlite3*,u8 en
73b10 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 c, const char*,i
73b20 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
73b30 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c ATE CollSeq *sql
73b40 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 ite3LocateCollSe
73b50 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c q(Parse *pParse,
73b60 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61 6d const char*zNam
73b70 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
73b80 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 TE CollSeq *sqli
73b90 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 te3ExprCollSeq(P
73ba0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
73bb0 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 pr *pExpr);.SQLI
73bc0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
73bd0 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 *sqlite3ExprSetC
73be0 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 oll(Parse *pPars
73bf0 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b 65 6e e, Expr *, Token
73c00 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
73c10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 ATE int sqlite3C
73c20 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 heckCollSeq(Pars
73c30 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a 29 3b e *, CollSeq *);
73c40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73c50 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b int sqlite3Check
73c60 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 ObjectName(Parse
73c70 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
73c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73c90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
73ca0 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c beSetChanges(sql
73cb0 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a 0a 53 ite3 *, int);..S
73cc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
73cd0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
73ce0 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 3ValueText(sqlit
73cf0 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a e3_value*, u8);.
73d00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
73d10 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 nt sqlite3ValueB
73d20 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
73d30 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 ue*, u8);.SQLITE
73d40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
73d50 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
73d60 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c (sqlite3_value*,
73d70 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 int, const void
73d80 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 20 20 *,u8, .
73d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73da0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
73db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73dc0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 void sqlite3Valu
73dd0 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 eFree(sqlite3_va
73de0 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 lue*);.SQLITE_PR
73df0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 IVATE sqlite3_va
73e00 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 lue *sqlite3Valu
73e10 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b eNew(sqlite3 *);
73e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73e30 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 char *sqlite3Utf
73e40 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 2c 16to8(sqlite3 *,
73e50 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
73e60 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 t);.#ifdef SQLIT
73e70 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 E_ENABLE_STAT2.S
73e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
73e90 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 ar *sqlite3Utf8t
73ea0 6f 31 36 28 73 71 6c 69 74 65 33 20 2a 2c 20 75 o16(sqlite3 *, u
73eb0 38 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 8, char *, int,
73ec0 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 int *);.#endif.S
73ed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73ee0 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 t sqlite3ValueFr
73ef0 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a omExpr(sqlite3 *
73f00 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75 38 , Expr *, u8, u8
73f10 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
73f20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
73f30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73f40 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 ValueApplyAffini
73f50 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ty(sqlite3_value
73f60 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69 66 *, u8, u8);.#if
73f70 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c ndef SQLITE_AMAL
73f80 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f GAMATION.SQLITE_
73f90 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e PRIVATE const un
73fa0 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 signed char sqli
73fb0 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 te3OpcodePropert
73fc0 79 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 y[];.SQLITE_PRIV
73fd0 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ATE const unsign
73fe0 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 ed char sqlite3U
73ff0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 pperToLower[];.S
74000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
74010 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
74020 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 r sqlite3CtypeMa
74030 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 p[];.SQLITE_PRIV
74040 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 ATE SQLITE_WSD s
74050 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e truct Sqlite3Con
74060 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 fig sqlite3Confi
74070 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 g;.SQLITE_PRIVAT
74080 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e E SQLITE_WSD Fun
74090 63 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 cDefHash sqlite3
740a0 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b GlobalFunctions;
740b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
740c0 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 int sqlite3Pendi
740d0 6e 67 42 79 74 65 3b 0a 23 65 6e 64 69 66 0a 53 ngByte;.#endif.S
740e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
740f0 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 id sqlite3RootPa
74100 67 65 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74 geMoved(Db*, int
74110 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
74120 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74130 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 te3Reindex(Parse
74140 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
74150 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74160 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
74170 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 lterFunctions(sq
74180 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
74190 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
741a0 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 ite3AlterRenameT
741b0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 able(Parse*, Src
741c0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a List*, Token*);.
741d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
741e0 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b nt sqlite3GetTok
741f0 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 en(const unsigne
74200 64 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 d char *, int *)
74210 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74220 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 void sqlite3Nes
74230 74 65 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c tedParse(Parse*,
74240 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e const char*, ..
74250 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .);.SQLITE_PRIVA
74260 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
74270 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 xpirePreparedSta
74280 74 65 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a tements(sqlite3*
74290 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
742a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 E int sqlite3Cod
742b0 65 53 75 62 73 65 6c 65 63 74 28 50 61 72 73 65 eSubselect(Parse
742c0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c *, Expr *, int,
742d0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
742e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
742f0 65 33 53 65 6c 65 63 74 50 72 65 70 28 50 61 72 e3SelectPrep(Par
74300 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 se*, Select*, Na
74310 6d 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c meContext*);.SQL
74320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
74330 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
74340 70 72 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 prNames(NameCont
74350 65 78 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 ext*, Expr*);.SQ
74360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74370 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 d sqlite3Resolve
74380 53 65 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 73 SelectNames(Pars
74390 65 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d e*, Select*, Nam
743a0 65 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 eContext*);.SQLI
743b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
743c0 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 qlite3ResolveOrd
743d0 65 72 47 72 6f 75 70 42 79 28 50 61 72 73 65 2a erGroupBy(Parse*
743e0 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c , Select*, ExprL
743f0 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ist*, const char
74400 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74410 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
74420 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 olumnDefault(Vdb
74430 65 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e e *, Table *, in
74440 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
74450 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
74460 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 ite3AlterFinishA
74470 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a ddColumn(Parse *
74480 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 , Token *);.SQLI
74490 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
744a0 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 sqlite3AlterBegi
744b0 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 nAddColumn(Parse
744c0 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a *, SrcList *);.
744d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
744e0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 ollSeq *sqlite3G
744f0 65 74 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 etCollSeq(sqlite
74500 33 2a 2c 20 75 38 2c 20 43 6f 6c 6c 53 65 71 20 3*, u8, CollSeq
74510 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b *, const char*);
74520 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74530 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 char sqlite3Affi
74540 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63 nityType(const c
74550 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
74560 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74570 65 33 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a e3Analyze(Parse*
74580 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a , Token*, Token*
74590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
745a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 E int sqlite3Inv
745b0 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 okeBusyHandler(B
745c0 75 73 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 usyHandler*);.SQ
745d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
745e0 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 sqlite3FindDb(s
745f0 71 6c 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 qlite3*, Token*)
74600 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74610 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 int sqlite3Find
74620 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a DbName(sqlite3 *
74630 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
74640 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74650 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 int sqlite3Analy
74660 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a sisLoad(sqlite3*
74670 2c 69 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 ,int iDB);.SQLIT
74680 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74690 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 qlite3DeleteInde
746a0 78 53 61 6d 70 6c 65 73 28 49 6e 64 65 78 2a 29 xSamples(Index*)
746b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
746c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 void sqlite3Def
746d0 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78 aultRowEst(Index
746e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
746f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
74700 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 egisterLikeFunct
74710 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 ions(sqlite3*, i
74720 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
74730 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
74740 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 sLikeFunction(sq
74750 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 lite3*,Expr*,int
74760 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 *,char*);.SQLITE
74770 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74780 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 lite3MinimumFile
74790 46 6f 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 Format(Parse*, i
747a0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
747b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
747c0 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 lite3SchemaFree(
747d0 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f void *);.SQLITE_
747e0 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a PRIVATE Schema *
747f0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 sqlite3SchemaGet
74800 28 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65 (sqlite3 *, Btre
74810 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
74820 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
74830 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 SchemaToIndex(sq
74840 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d lite3 *db, Schem
74850 61 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 a *);.SQLITE_PRI
74860 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 VATE KeyInfo *sq
74870 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 lite3IndexKeyinf
74880 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 o(Parse *, Index
74890 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
748a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 ATE int sqlite3C
748b0 72 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 reateFunc(sqlite
748c0 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 3 *, const char
748d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 *, int, int, voi
748e0 64 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 d *, . void (*)
748f0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
74900 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
74910 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 lue **),. void
74920 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (*)(sqlite3_cont
74930 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
74940 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 _value **), void
74950 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e (*)(sqlite3_con
74960 74 65 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 5f text*));.SQLITE_
74970 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74980 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 te3ApiExit(sqlit
74990 65 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 e3 *db, int);.SQ
749a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
749b0 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 sqlite3OpenTemp
749c0 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20 2a Database(Parse *
749d0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
749e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
749f0 74 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 trAccumInit(StrA
74a00 63 63 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 6e ccum*, char*, in
74a10 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
74a20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
74a30 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
74a40 6e 64 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f 6e nd(StrAccum*,con
74a50 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 st char*,int);.S
74a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
74a70 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 ar *sqlite3StrAc
74a80 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 cumFinish(StrAcc
74a90 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 um*);.SQLITE_PRI
74aa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74ab0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 3StrAccumReset(S
74ac0 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 trAccum*);.SQLIT
74ad0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74ae0 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 qlite3SelectDest
74af0 49 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74 2a Init(SelectDest*
74b00 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
74b10 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
74b20 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c sqlite3CreateCol
74b30 75 6d 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20 umnExpr(sqlite3
74b40 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 69 6e *, SrcList *, in
74b50 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 t, int);..SQLITE
74b60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74b70 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 lite3BackupResta
74b80 72 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 rt(sqlite3_backu
74b90 70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 p *);.SQLITE_PRI
74ba0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74bb0 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 3BackupUpdate(sq
74bc0 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 lite3_backup *,
74bd0 50 67 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a Pgno, const u8 *
74be0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e );../*.** The in
74bf0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c terface to the L
74c00 45 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 EMON-generated p
74c10 61 72 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f arser.*/.SQLITE_
74c20 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
74c30 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 lite3ParserAlloc
74c40 28 76 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 (void*(*)(size_t
74c50 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ));.SQLITE_PRIVA
74c60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
74c70 61 72 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c arserFree(void*,
74c80 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 void(*)(void*))
74c90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74ca0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 void sqlite3Par
74cb0 73 65 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 ser(void*, int,
74cc0 54 6f 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a Token, Parse*);.
74cd0 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 #ifdef YYTRACKMA
74ce0 58 53 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 XSTACKDEPTH.SQLI
74cf0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
74d00 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 sqlite3ParserSt
74d10 61 63 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a ackPeak(void*);.
74d20 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
74d30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74d40 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e te3AutoLoadExten
74d50 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b sions(sqlite3*);
74d60 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
74d70 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
74d80 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ION.SQLITE_PRIVA
74d90 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
74da0 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 3CloseExtensions
74db0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 (sqlite3*);.#els
74dc0 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 e.# define sqlit
74dd0 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e e3CloseExtension
74de0 73 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 s(X).#endif..#if
74df0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
74e00 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 _SHARED_CACHE.SQ
74e10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
74e20 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 oid sqlite3Table
74e30 4c 6f 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e Lock(Parse *, in
74e40 74 2c 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 t, int, u8, cons
74e50 74 20 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 t char *);.#else
74e60 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
74e70 65 33 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c e3TableLock(v,w,
74e80 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 x,y,z).#endif..#
74e90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
74ea0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
74eb0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 int sqlite3Ut
74ec0 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 f8To8(unsigned c
74ed0 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 har*);.#endif..#
74ee0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
74ef0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 T_VIRTUALTABLE.#
74f00 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
74f10 56 74 61 62 43 6c 65 61 72 28 59 29 0a 23 20 20 VtabClear(Y).#
74f20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 define sqlite3Vt
74f30 61 62 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 abSync(X,Y) SQLI
74f40 54 45 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 TE_OK.# define
74f50 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 sqlite3VtabRollb
74f60 61 63 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 ack(X).# define
74f70 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d sqlite3VtabComm
74f80 69 74 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 it(X).# define
74f90 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e sqlite3VtabInSyn
74fa0 63 28 64 62 29 20 30 0a 23 20 20 64 65 66 69 6e c(db) 0.# defin
74fb0 65 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 e sqlite3VtabLoc
74fc0 6b 28 58 29 20 0a 23 20 20 64 65 66 69 6e 65 20 k(X) .# define
74fd0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
74fe0 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 k(X).# define s
74ff0 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b qlite3VtabUnlock
75000 4c 69 73 74 28 58 29 0a 23 65 6c 73 65 0a 53 51 List(X).#else.SQ
75010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 LITE_PRIVATE
75020 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
75030 43 6c 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 Clear(Table*);.S
75040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
75050 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
75060 53 79 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 Sync(sqlite3 *db
75070 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 , char **);.SQLI
75080 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e TE_PRIVATE in
75090 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c t sqlite3VtabRol
750a0 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 lback(sqlite3 *d
750b0 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 b);.SQLITE_PRIVA
750c0 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 TE int sqlite
750d0 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 3VtabCommit(sqli
750e0 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 te3 *db);.SQLITE
750f0 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 _PRIVATE void
75100 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b sqlite3VtabLock
75110 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 (VTable *);.SQLI
75120 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f TE_PRIVATE vo
75130 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e id sqlite3VtabUn
75140 6c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a lock(VTable *);.
75150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75160 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 void sqlite3Vt
75170 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c abUnlockList(sql
75180 69 74 65 33 2a 29 3b 0a 23 20 20 64 65 66 69 6e ite3*);.# defin
75190 65 20 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 e sqlite3VtabInS
751a0 79 6e 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e ync(db) ((db)->n
751b0 56 54 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 VTrans>0 && (db)
751c0 2d 3e 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 ->aVTrans==0).#e
751d0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
751e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
751f0 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 VtabMakeWritable
75200 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b (Parse*,Table*);
75210 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75220 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
75230 42 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 BeginParse(Parse
75240 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
75250 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
75260 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75270 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 sqlite3VtabFinis
75280 68 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 hParse(Parse*, T
75290 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
752a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
752b0 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 te3VtabArgInit(P
752c0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
752d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
752e0 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 te3VtabArgExtend
752f0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 (Parse*, Token*)
75300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75310 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
75320 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 CallCreate(sqlit
75330 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 e3*, int, const
75340 63 68 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 char *, char **)
75350 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75360 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 int sqlite3Vtab
75370 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 CallConnect(Pars
75380 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c e*, Table*);.SQL
75390 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
753a0 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 sqlite3VtabCallD
753b0 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c estroy(sqlite3*,
753c0 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 int, const char
753d0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
753e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
753f0 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 tabBegin(sqlite3
75400 20 2a 2c 20 56 54 61 62 6c 65 20 2a 29 3b 0a 53 *, VTable *);.S
75410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 QLITE_PRIVATE Fu
75420 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 ncDef *sqlite3Vt
75430 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 abOverloadFuncti
75440 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e on(sqlite3 *,Fun
75450 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c cDef*, int nArg,
75460 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
75470 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
75480 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 ite3InvalidFunct
75490 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ion(sqlite3_cont
754a0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
754b0 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 _value**);.SQLIT
754c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
754d0 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74 lite3VdbeParamet
754e0 65 72 49 6e 64 65 78 28 56 64 62 65 2a 2c 20 63 erIndex(Vdbe*, c
754f0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 onst char*, int)
75500 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75510 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e int sqlite3Tran
75520 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c sferBindings(sql
75530 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c ite3_stmt *, sql
75540 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 ite3_stmt *);.SQ
75550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
75560 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 sqlite3Reprepar
75570 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 e(Vdbe*);.SQLITE
75580 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75590 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 lite3ExprListChe
755a0 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 2a 2c ckLength(Parse*,
755b0 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 ExprList*, cons
755c0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
755d0 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 _PRIVATE CollSeq
755e0 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 *sqlite3BinaryC
755f0 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 ompareCollSeq(Pa
75600 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 rse *, Expr *, E
75610 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 xpr *);.SQLITE_P
75620 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
75630 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 e3TempInMemory(c
75640 6f 6e 73 74 20 73 71 6c 69 74 65 33 2a 29 3b 0a onst sqlite3*);.
75650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 SQLITE_PRIVATE V
75660 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 47 65 Table *sqlite3Ge
75670 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a tVTable(sqlite3*
75680 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a 2f 2a 20 44 , Table*);../* D
75690 65 63 6c 61 72 61 74 69 6f 6e 73 20 66 6f 72 20 eclarations for
756a0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 66 6b 65 functions in fke
756b0 79 2e 63 2e 20 41 6c 6c 20 6f 66 20 74 68 65 73 y.c. All of thes
756c0 65 20 61 72 65 20 72 65 70 6c 61 63 65 64 20 62 e are replaced b
756d0 79 0a 2a 2a 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f y.** no-op macro
756e0 73 20 69 66 20 4f 4d 49 54 5f 46 4f 52 45 49 47 s if OMIT_FOREIG
756f0 4e 5f 4b 45 59 20 69 73 20 64 65 66 69 6e 65 64 N_KEY is defined
75700 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6e . In this case n
75710 6f 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 o foreign.** key
75720 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 functionality i
75730 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 49 66 20 s available. If
75740 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 69 73 20 OMIT_TRIGGER is
75750 64 65 66 69 6e 65 64 20 62 75 74 0a 2a 2a 20 4f defined but.** O
75760 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 MIT_FOREIGN_KEY
75770 69 73 20 6e 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d is not, only som
75780 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f e of the functio
75790 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 65 64 2e 20 ns are no-oped.
757a0 49 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73 65 20 In.** this case
757b0 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 72 65 foreign keys are
757c0 20 70 61 72 73 65 64 2c 20 62 75 74 20 6e 6f 20 parsed, but no
757d0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 61 6c other functional
757e0 69 74 79 20 69 73 20 0a 2a 2a 20 70 72 6f 76 69 ity is .** provi
757f0 64 65 64 20 28 65 6e 66 6f 72 63 65 6d 65 6e 74 ded (enforcement
75800 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e of FK constrain
75810 74 73 20 72 65 71 75 69 72 65 73 20 74 68 65 20 ts requires the
75820 74 72 69 67 67 65 72 73 20 73 75 62 2d 73 79 73 triggers sub-sys
75830 74 65 6d 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 tem)..*/.#if !de
75840 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
75850 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 T_FOREIGN_KEY) &
75860 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
75870 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a E_OMIT_TRIGGER).
75880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75890 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 43 void sqlite3FkC
758a0 68 65 63 6b 28 50 61 72 73 65 2a 2c 20 54 61 62 heck(Parse*, Tab
758b0 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a le*, int, int);.
758c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
758d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44 void sqlite3FkD
758e0 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c ropTable(Parse*,
758f0 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 61 62 6c SrcList *, Tabl
75900 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
75910 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
75920 65 33 46 6b 41 63 74 69 6f 6e 73 28 50 61 72 73 e3FkActions(Pars
75930 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 e*, Table*, Expr
75940 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c List*, int);.SQL
75950 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
75960 74 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 t sqlite3FkRequi
75970 72 65 64 28 50 61 72 73 65 2a 2c 20 54 61 62 6c red(Parse*, Tabl
75980 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a e*, int*, int);.
75990 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
759a0 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b 4f 6c u32 sqlite3FkOl
759b0 64 6d 61 73 6b 28 50 61 72 73 65 2a 2c 20 54 61 dmask(Parse*, Ta
759c0 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ble*);.SQLITE_PR
759d0 49 56 41 54 45 20 20 20 46 4b 65 79 20 2a 73 71 IVATE FKey *sq
759e0 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 lite3FkReference
759f0 73 28 54 61 62 6c 65 20 2a 29 3b 0a 23 65 6c 73 s(Table *);.#els
75a00 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 e. #define sqli
75a10 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 61 2c 62 te3FkActions(a,b
75a20 2c 63 2c 64 29 0a 20 20 23 64 65 66 69 6e 65 20 ,c,d). #define
75a30 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 61 sqlite3FkCheck(a
75a40 2c 62 2c 63 2c 64 29 0a 20 20 23 64 65 66 69 6e ,b,c,d). #defin
75a50 65 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 e sqlite3FkDropT
75a60 61 62 6c 65 28 61 2c 62 2c 63 29 0a 20 20 23 64 able(a,b,c). #d
75a70 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 4f efine sqlite3FkO
75a80 6c 64 6d 61 73 6b 28 61 2c 62 29 20 20 20 20 20 ldmask(a,b)
75a90 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 0. #define sql
75aa0 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 61 ite3FkRequired(a
75ab0 2c 62 2c 63 2c 64 29 20 30 0a 23 65 6e 64 69 66 ,b,c,d) 0.#endif
75ac0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
75ad0 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 OMIT_FOREIGN_KEY
75ae0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75af0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b void sqlite3Fk
75b00 44 65 6c 65 74 65 28 54 61 62 6c 65 2a 29 3b 0a Delete(Table*);.
75b10 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
75b20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 sqlite3FkDelete(
75b30 61 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a a).#endif.../*.*
75b40 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75 6c * Available faul
75b50 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53 68 t injectors. Sh
75b60 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65 64 ould be numbered
75b70 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
75b80 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 0..*/.#define SQ
75b90 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 LITE_FAULTINJECT
75ba0 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30 0a OR_MALLOC 0.
75bb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
75bc0 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f 55 AULTINJECTOR_COU
75bd0 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a NT 1../*.**
75be0 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 The interface t
75bf0 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66 61 o the code in fa
75c00 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20 69 ult.c used for i
75c10 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e 69 dentifying "beni
75c20 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 gn".** malloc fa
75c30 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73 20 ilures. This is
75c40 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66 20 only present if
75c50 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
75c60 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20 6e TIN_TEST.** is n
75c70 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 ot defined..*/.#
75c80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
75c90 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
75ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75cb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 void sqlite3Beg
75cc0 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 inBenignMalloc(v
75cd0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 oid);.SQLITE_PRI
75ce0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
75cf0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
75d00 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a oc(void);.#else.
75d10 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
75d20 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
75d30 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20 73 oc(). #define s
75d40 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
75d50 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a 0a alloc().#endif..
75d60 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 #define IN_INDEX
75d70 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20 20 _ROWID
75d80 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 1.#define IN_IN
75d90 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20 20 DEX_EPH
75da0 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49 4e 2.#define IN
75db0 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20 20 _INDEX_INDEX
75dc0 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45 5f 3.SQLITE_
75dd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
75de0 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 te3FindInIndex(P
75df0 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c 20 arse *, Expr *,
75e00 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 int*);..#ifdef S
75e10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
75e20 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54 45 MIC_WRITE.SQLITE
75e30 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
75e40 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
75e50 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c n(sqlite3_vfs *,
75e60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 const char *, s
75e70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20 69 qlite3_file *, i
75e80 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
75e90 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
75ea0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a qlite3JournalSiz
75eb0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 29 e(sqlite3_vfs *)
75ec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75ed0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f int sqlite3Jo
75ee0 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c 69 urnalCreate(sqli
75ef0 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65 6c te3_file *);.#el
75f00 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c se. #define sql
75f10 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 ite3JournalSize(
75f20 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e 73 pVfs) ((pVfs)->s
75f30 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66 0a zOsFile).#endif.
75f40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75f50 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a void sqlite3MemJ
75f60 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 ournalOpen(sqlit
75f70 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c 49 e3_file *);.SQLI
75f80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
75f90 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c qlite3MemJournal
75fa0 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 Size(void);.SQLI
75fb0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
75fc0 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
75fd0 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al(sqlite3_file
75fe0 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f *);..#if SQLITE_
75ff0 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 MAX_EXPR_DEPTH>0
76000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76010 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 void sqlite3Ex
76020 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73 prSetHeight(Pars
76030 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
76040 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *p);.SQLITE_PRIV
76050 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
76060 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 3SelectExprHeigh
76070 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51 4c t(Select *);.SQL
76080 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
76090 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 t sqlite3ExprChe
760a0 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a 2c ckHeight(Parse*,
760b0 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 23 int);.#else. #
760c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 define sqlite3Ex
760d0 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79 29 prSetHeight(x,y)
760e0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
760f0 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 e3SelectExprHeig
76100 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69 6e ht(x) 0. #defin
76110 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 e sqlite3ExprChe
76120 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23 65 ckHeight(x,y).#e
76130 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
76140 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
76150 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 Get4byte(const u
76160 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 8*);.SQLITE_PRIV
76170 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
76180 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75 33 Put4byte(u8*, u3
76190 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 2);..#ifdef SQLI
761a0 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b TE_ENABLE_UNLOCK
761b0 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 5f 50 _NOTIFY.SQLITE_P
761c0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
761d0 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
761e0 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a locked(sqlite3 *
761f0 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 , sqlite3 *);.SQ
76200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
76210 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 oid sqlite3Conne
76220 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 ctionUnlocked(sq
76230 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 lite3 *db);.SQLI
76240 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
76250 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 d sqlite3Connect
76260 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74 65 ionClosed(sqlite
76270 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a 20 20 3 *db);.#else.
76280 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 #define sqlite3C
76290 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 onnectionBlocked
762a0 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20 (x,y). #define
762b0 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
762c0 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 20 23 nUnlocked(x). #
762d0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
762e0 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 78 nnectionClosed(x
762f0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 ).#endif..#ifdef
76300 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 SQLITE_DEBUG.SQ
76310 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
76320 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 oid sqlite3Parse
76330 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68 rTrace(FILE*, ch
76340 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f ar *);.#endif../
76350 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 *.** If the SQLI
76360 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43 TE_ENABLE IOTRAC
76370 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68 E exists then th
76380 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c e global variabl
76390 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72 e.** sqlite3IoTr
763a0 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ace is a pointer
763b0 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b to a printf-lik
763c0 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74 e routine used t
763d0 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74 o.** print I/O t
763e0 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e racing messages.
763f0 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 .*/.#ifdef SQLI
76400 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 TE_ENABLE_IOTRAC
76410 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41 E.# define IOTRA
76420 43 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74 CE(A) if( sqlit
76430 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c e3IoTrace ){ sql
76440 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d ite3IoTrace A; }
76450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
76460 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
76470 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 beIOTraceSql(Vdb
76480 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
76490 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 ATE void (*sqlit
764a0 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 e3IoTrace)(const
764b0 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c char*,...);.#el
764c0 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 se.# define IOTR
764d0 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20 ACE(A).# define
764e0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 sqlite3VdbeIOTra
764f0 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a ceSql(X).#endif.
76500 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
76510 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
76520 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
76530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76550 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
76560 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
76570 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a ile global.c ***
76580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
765a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
765b0 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a 008 June 13.**.*
765c0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
765d0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
765e0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
765f0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
76600 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
76610 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
76620 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
76630 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
76640 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
76650 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
76660 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
76670 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
76680 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
76690 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
766a0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
766b0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
766c0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
766d0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
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 2a 2a 2a ****************
76700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
76720 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
76730 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 ins definitions
76740 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 of global variab
76750 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73 les and contants
76760 2e 0a 2a 2f 0a 0a 2f 2a 20 41 6e 20 61 72 72 61 ..*/../* An arra
76770 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75 70 70 y to map all upp
76780 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 er-case characte
76790 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20 63 6f rs into their co
767a0 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6c rresponding.** l
767b0 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 ower-case charac
767c0 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 ter. .**.** SQLi
767d0 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 te only consider
767e0 73 20 55 53 2d 41 53 43 49 49 20 28 6f 72 20 45 s US-ASCII (or E
767f0 42 43 44 49 43 29 20 63 68 61 72 61 63 74 65 72 BCDIC) character
76800 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 2a 2a s. We do not.**
76810 20 68 61 6e 64 6c 65 20 63 61 73 65 20 63 6f 6e handle case con
76820 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 74 68 65 versions for the
76830 20 55 54 46 20 63 68 61 72 61 63 74 65 72 20 73 UTF character s
76840 65 74 20 73 69 6e 63 65 20 74 68 65 20 74 61 62 et since the tab
76850 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64 20 les.** involved
76860 61 72 65 20 6e 65 61 72 6c 79 20 61 73 20 62 69 are nearly as bi
76870 67 20 6f 72 20 62 69 67 67 65 72 20 74 68 61 6e g or bigger than
76880 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 2e 0a SQLite itself..
76890 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
768a0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 E const unsigned
768b0 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 char sqlite3Upp
768c0 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b 0a erToLower[] = {.
768d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 #ifdef SQLITE_AS
768e0 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20 31 2c CII. 0, 1,
768f0 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 2, 3, 4, 5,
76900 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 6, 7, 8, 9,
76910 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 10, 11, 12, 13,
76920 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31 37 2c 14, 15, 16, 17,
76930 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20 32 30 . 18, 19, 20
76940 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 , 21, 22, 23, 24
76950 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 , 25, 26, 27, 28
76960 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 33 32 , 29, 30, 31, 32
76970 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20 20 , 33, 34, 35,.
76980 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 36, 37, 38, 3
76990 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 9, 40, 41, 42, 4
769a0 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 3, 44, 45, 46, 4
769b0 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 7, 48, 49, 50, 5
769c0 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20 20 20 1, 52, 53,.
769d0 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 20 54, 55, 56, 57,
769e0 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 20 58, 59, 60, 61,
769f0 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c 20 62, 63, 64, 97,
76a00 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 98, 99,100,101,1
76a10 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30 34 2c 02,103,. 104,
76a20 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 105,106,107,108,
76a30 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 109,110,111,112,
76a40 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 113,114,115,116,
76a50 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 117,118,119,120,
76a60 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20 39 31 121,. 122, 91
76a70 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 , 92, 93, 94, 95
76a80 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20 39 39 , 96, 97, 98, 99
76a90 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 ,100,101,102,103
76aa0 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 ,104,105,106,107
76ab0 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c 31 31 ,. 108,109,11
76ac0 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 0,111,112,113,11
76ad0 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 4,115,116,117,11
76ae0 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31 32 8,119,120,121,12
76af0 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a 20 2,123,124,125,.
76b00 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38 2c 31 126,127,128,1
76b10 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 29,130,131,132,1
76b20 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33,134,135,136,1
76b30 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 37,138,139,140,1
76b40 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20 20 41,142,143,.
76b50 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 144,145,146,147,
76b60 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 148,149,150,151,
76b70 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 152,153,154,155,
76b80 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35 39 2c 156,157,158,159,
76b90 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31 36 32 160,161,. 162
76ba0 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 ,163,164,165,166
76bb0 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 ,167,168,169,170
76bc0 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 37 34 ,171,172,173,174
76bd0 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 37 38 ,175,176,177,178
76be0 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c 31 38 ,179,. 180,18
76bf0 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 1,182,183,184,18
76c00 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 5,186,187,188,18
76c10 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 31 39 9,190,191,192,19
76c20 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 31 39 3,194,195,196,19
76c30 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39 2c 32 7,. 198,199,2
76c40 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c 32 00,201,202,203,2
76c50 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 32 04,205,206,207,2
76c60 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c 32 08,209,210,211,2
76c70 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c 0a 12,213,214,215,.
76c80 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31 38 2c 216,217,218,
76c90 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 219,220,221,222,
76ca0 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32 36 2c 223,224,225,226,
76cb0 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33 30 2c 227,228,229,230,
76cc0 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20 20 231,232,233,.
76cd0 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32 33 37 234,235,236,237
76ce0 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32 34 31 ,238,239,240,241
76cf0 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 ,242,243,244,245
76d00 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 ,246,247,248,249
76d10 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20 32 35 ,250,251,. 25
76d20 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a 23 65 2,253,254,255.#e
76d30 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
76d40 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20 20 20 TE_EBCDIC.
76d50 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 0, 1, 2, 3,
76d60 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 4, 5, 6, 7,
76d70 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 8, 9, 10, 11, 1
76d80 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 2f 2, 13, 14, 15, /
76d90 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31 36 2c * 0x */. 16,
76da0 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32 30 2c 17, 18, 19, 20,
76db0 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34 2c 21, 22, 23, 24,
76dc0 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38 2c 25, 26, 27, 28,
76dd0 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a 20 29, 30, 31, /*
76de0 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20 33 1x */. 32, 3
76df0 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c 20 33 3, 34, 35, 36, 3
76e00 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 7, 38, 39, 40, 4
76e10 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 1, 42, 43, 44, 4
76e20 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32 78 5, 46, 47, /* 2x
76e30 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34 39 2c */. 48, 49,
76e40 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 50, 51, 52, 53,
76e50 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c 54, 55, 56, 57,
76e60 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c 58, 59, 60, 61,
76e70 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20 2a 62, 63, /* 3x *
76e80 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c 20 36 /. 64, 65, 6
76e90 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 6, 67, 68, 69, 7
76ea0 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 20 37 0, 71, 72, 73, 7
76eb0 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c 20 37 4, 75, 76, 77, 7
76ec0 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f 0a 8, 79, /* 4x */.
76ed0 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38 32 2c 80, 81, 82,
76ee0 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 83, 84, 85, 86,
76ef0 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39 30 2c 87, 88, 89, 90,
76f00 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 91, 92, 93, 94,
76f10 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 95, /* 5x */.
76f20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c 20 36 96, 97, 66, 6
76f30 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 7, 68, 69, 70, 7
76f40 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c 31 30 1, 72, 73,106,10
76f50 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 7,108,109,110,11
76f60 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 1, /* 6x */.
76f70 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 112, 81, 82, 83,
76f80 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 84, 85, 86, 87,
76f90 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32 33 2c 88, 89,122,123,
76fa0 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32 37 2c 124,125,126,127,
76fb0 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31 32 /* 7x */. 12
76fc0 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 8,129,130,131,13
76fd0 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 2,133,134,135,13
76fe0 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 6,137,138,139,14
76ff0 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 20 2f 0,141,142,143, /
77000 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34 34 2c * 8x */. 144,
77010 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 145,146,147,148,
77020 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 149,150,151,152,
77030 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 153,154,155,156,
77040 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a 20 157,156,159, /*
77050 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31 36 9x */. 160,16
77060 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 1,162,163,164,16
77070 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 5,166,167,168,16
77080 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 31 34 9,170,171,140,14
77090 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41 78 1,142,175, /* Ax
770a0 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37 37 2c */. 176,177,
770b0 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38 31 2c 178,179,180,181,
770c0 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 182,183,184,185,
770d0 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 186,187,188,189,
770e0 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20 2a 190,191, /* Bx *
770f0 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c 31 33 /. 192,129,13
77100 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 0,131,132,133,13
77110 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 32 30 4,135,136,137,20
77120 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 2,203,204,205,20
77130 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f 0a 6,207, /* Cx */.
77140 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34 36 2c 208,145,146,
77150 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 147,148,149,150,
77160 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31 38 2c 151,152,153,218,
77170 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 219,220,221,222,
77180 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 223, /* Dx */.
77190 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c 31 36 224,225,162,16
771a0 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 3,164,165,166,16
771b0 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c 32 30 7,168,169,232,20
771c0 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 3,204,205,206,20
771d0 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 7, /* Ex */.
771e0 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 239,240,241,242,
771f0 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 243,244,245,246,
77200 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31 39 2c 247,248,249,219,
77210 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35 35 2c 220,221,222,255,
77220 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69 66 /* Fx */.#endif
77230 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
77240 6f 6c 6c 6f 77 69 6e 67 20 32 35 36 20 62 79 74 ollowing 256 byt
77250 65 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 e lookup table i
77260 73 20 75 73 65 64 20 74 6f 20 73 75 70 70 6f 72 s used to suppor
77270 74 20 53 51 4c 69 74 65 73 20 62 75 69 6c 74 2d t SQLites built-
77280 69 6e 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 in.** equivalent
77290 73 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s to the followi
772a0 6e 67 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 ng standard libr
772b0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a ary functions:.*
772c0 2a 0a 2a 2a 20 20 20 69 73 73 70 61 63 65 28 29 *.** isspace()
772d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
772e0 20 20 20 20 20 20 20 20 30 78 30 31 0a 2a 2a 20 0x01.**
772f0 20 20 69 73 61 6c 70 68 61 28 29 20 20 20 20 20 isalpha()
77300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77310 20 20 20 30 78 30 32 0a 2a 2a 20 20 20 69 73 64 0x02.** isd
77320 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20 igit()
77330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 0x
77340 30 34 0a 2a 2a 20 20 20 69 73 61 6c 6e 75 6d 28 04.** isalnum(
77350 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
77360 20 20 20 20 20 20 20 20 20 30 78 30 36 0a 2a 2a 0x06.**
77370 20 20 20 69 73 78 64 69 67 69 74 28 29 20 20 20 isxdigit()
77380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77390 20 20 20 20 30 78 30 38 0a 2a 2a 20 20 20 74 6f 0x08.** to
773a0 75 70 70 65 72 28 29 20 20 20 20 20 20 20 20 20 upper()
773b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
773c0 78 32 30 0a 2a 2a 20 20 20 53 51 4c 69 74 65 20 x20.** SQLite
773d0 69 64 65 6e 74 69 66 69 65 72 20 63 68 61 72 61 identifier chara
773e0 63 74 65 72 20 20 20 20 20 20 30 78 34 30 0a 2a cter 0x40.*
773f0 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73 *.** Bit 0x20 is
77400 20 73 65 74 20 69 66 20 74 68 65 20 6d 61 70 70 set if the mapp
77410 65 64 20 63 68 61 72 61 63 74 65 72 20 72 65 71 ed character req
77420 75 69 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f uires translatio
77430 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 n to upper.** ca
77440 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20 se. i.e. if the
77450 63 68 61 72 61 63 74 65 72 20 69 73 20 61 20 6c character is a l
77460 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 ower-case ASCII
77470 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 character..** If
77480 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 x is a lower-ca
77490 73 65 20 41 53 43 49 49 20 63 68 61 72 61 63 74 se ASCII charact
774a0 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70 er, then its upp
774b0 65 72 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65 er-case equivale
774c0 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 nt.** is (x - 0x
774d0 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65 20 74 20). Therefore t
774e0 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62 65 20 oupper() can be
774f0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a implemented as:.
77500 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d **.** (x & ~(m
77510 61 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a ap[x]&0x20)).**.
77520 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 75 6e 63 ** Standard func
77530 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 tion tolower() i
77540 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 s implemented us
77550 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 55 ing the sqlite3U
77560 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a pperToLower[].**
77570 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 array. tolower(
77580 29 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 6f ) is used more o
77590 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 ften than touppe
775a0 72 28 29 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a r() by SQLite..*
775b0 2a 0a 2a 2a 20 42 69 74 20 30 78 34 30 20 69 73 *.** Bit 0x40 is
775c0 20 73 65 74 20 69 66 20 74 68 65 20 63 68 61 72 set if the char
775d0 61 63 74 65 72 20 6e 6f 6e 2d 61 6c 70 68 61 6e acter non-alphan
775e0 75 6d 65 72 69 63 20 61 6e 64 20 63 61 6e 20 62 umeric and can b
775f0 65 20 75 73 65 64 20 69 6e 20 61 6e 20 0a 2a 2a e used in an .**
77600 20 53 51 4c 69 74 65 20 69 64 65 6e 74 69 66 69 SQLite identifi
77610 65 72 2e 20 20 49 64 65 6e 74 69 66 69 65 72 73 er. Identifiers
77620 20 61 72 65 20 61 6c 70 68 61 6e 75 6d 65 72 69 are alphanumeri
77630 63 73 2c 20 22 5f 22 2c 20 22 24 22 2c 20 61 6e cs, "_", "$", an
77640 64 20 61 6e 79 0a 2a 2a 20 6e 6f 6e 2d 41 53 43 d any.** non-ASC
77650 49 49 20 55 54 46 20 63 68 61 72 61 63 74 65 72 II UTF character
77660 2e 20 48 65 6e 63 65 20 74 68 65 20 74 65 73 74 . Hence the test
77670 20 66 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20 for whether or
77680 6e 6f 74 20 61 20 63 68 61 72 61 63 74 65 72 20 not a character
77690 69 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e is.** part of an
776a0 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 20 30 identifier is 0
776b0 78 34 36 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 x46..**.** SQLit
776c0 65 27 73 20 76 65 72 73 69 6f 6e 73 20 61 72 65 e's versions are
776d0 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 identical to th
776e0 65 20 73 74 61 6e 64 61 72 64 20 76 65 72 73 69 e standard versi
776f0 6f 6e 73 20 61 73 73 75 6d 69 6e 67 20 61 0a 2a ons assuming a.*
77700 2a 20 6c 6f 63 61 6c 65 20 6f 66 20 22 43 22 2e * locale of "C".
77710 20 54 68 65 79 20 61 72 65 20 69 6d 70 6c 65 6d They are implem
77720 65 6e 74 65 64 20 61 73 20 6d 61 63 72 6f 73 20 ented as macros
77730 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a in sqliteInt.h..
77740 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
77750 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 _ASCII.SQLITE_PR
77760 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 IVATE const unsi
77770 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 gned char sqlite
77780 33 43 74 79 70 65 4d 61 70 5b 32 35 36 5d 20 3d 3CtypeMap[256] =
77790 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c {. 0x00, 0x00,
777a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
777b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
777c0 78 30 30 2c 20 20 2f 2a 20 30 30 2e 2e 30 37 20 x00, /* 00..07
777d0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
777e0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
777f0 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 1, 0x01, 0x01, 0
77800 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x01, 0x00, 0x00,
77810 20 20 2f 2a 20 30 38 2e 2e 30 66 20 20 20 20 2e /* 08..0f .
77820 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
77830 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77840 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77850 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
77860 20 31 30 2e 2e 31 37 20 20 20 20 2e 2e 2e 2e 2e 10..17 .....
77870 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 ... */. 0x00, 0
77880 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77890 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
778a0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 38 2e 0, 0x00, /* 18.
778b0 2e 31 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .1f ........
778c0 2a 2f 0a 20 20 30 78 30 31 2c 20 30 78 30 30 2c */. 0x01, 0x00,
778d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 34 0x00, 0x00, 0x4
778e0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
778f0 78 30 30 2c 20 20 2f 2a 20 32 30 2e 2e 32 37 20 x00, /* 20..27
77900 20 20 20 20 21 22 23 24 25 26 27 20 2a 2f 0a 20 !"#$%&' */.
77910 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77920 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77930 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77940 20 20 2f 2a 20 32 38 2e 2e 32 66 20 20 20 20 28 /* 28..2f (
77950 29 2a 2b 2c 2d 2e 2f 20 2a 2f 0a 20 20 30 78 30 )*+,-./ */. 0x0
77960 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 c, 0x0c, 0x0c, 0
77970 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c x0c, 0x0c, 0x0c,
77980 20 30 78 30 63 2c 20 30 78 30 63 2c 20 20 2f 2a 0x0c, 0x0c, /*
77990 20 33 30 2e 2e 33 37 20 20 20 20 30 31 32 33 34 30..37 01234
779a0 35 36 37 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 567 */. 0x0c, 0
779b0 78 30 63 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x0c, 0x00, 0x00,
779c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
779d0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 33 38 2e 0, 0x00, /* 38.
779e0 2e 33 66 20 20 20 20 38 39 3a 3b 3c 3d 3e 3f 20 .3f 89:;<=>?
779f0 2a 2f 0a 0a 20 20 30 78 30 30 2c 20 30 78 30 61 */.. 0x00, 0x0a
77a00 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 , 0x0a, 0x0a, 0x
77a10 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 0a, 0x0a, 0x0a,
77a20 30 78 30 32 2c 20 20 2f 2a 20 34 30 2e 2e 34 37 0x02, /* 40..47
77a30 20 20 20 20 40 41 42 43 44 45 46 47 20 2a 2f 0a @ABCDEFG */.
77a40 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 0x02, 0x02, 0x
77a50 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
77a60 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77a70 2c 20 20 2f 2a 20 34 38 2e 2e 34 66 20 20 20 20 , /* 48..4f
77a80 48 49 4a 4b 4c 4d 4e 4f 20 2a 2f 0a 20 20 30 78 HIJKLMNO */. 0x
77a90 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
77aa0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77ab0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f , 0x02, 0x02, /
77ac0 2a 20 35 30 2e 2e 35 37 20 20 20 20 50 51 52 53 * 50..57 PQRS
77ad0 54 55 56 57 20 2a 2f 0a 20 20 30 78 30 32 2c 20 TUVW */. 0x02,
77ae0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30 0x02, 0x02, 0x00
77af0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77b00 30 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 35 38 00, 0x40, /* 58
77b10 2e 2e 35 66 20 20 20 20 58 59 5a 5b 5c 5d 5e 5f ..5f XYZ[\]^_
77b20 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 32 61 */. 0x00, 0x2a
77b30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 , 0x2a, 0x2a, 0x
77b40 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 2a, 0x2a, 0x2a,
77b50 30 78 32 32 2c 20 20 2f 2a 20 36 30 2e 2e 36 37 0x22, /* 60..67
77b60 20 20 20 20 60 61 62 63 64 65 66 67 20 2a 2f 0a `abcdefg */.
77b70 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 0x22, 0x22, 0x
77b80 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
77b90 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77ba0 2c 20 20 2f 2a 20 36 38 2e 2e 36 66 20 20 20 20 , /* 68..6f
77bb0 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0a 20 20 30 78 hijklmno */. 0x
77bc0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
77bd0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77be0 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2f , 0x22, 0x22, /
77bf0 2a 20 37 30 2e 2e 37 37 20 20 20 20 70 71 72 73 * 70..77 pqrs
77c00 74 75 76 77 20 2a 2f 0a 20 20 30 78 32 32 2c 20 tuvw */. 0x22,
77c10 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 30 30 0x22, 0x22, 0x00
77c20 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77c30 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 37 38 00, 0x00, /* 78
77c40 2e 2e 37 66 20 20 20 20 78 79 7a 7b 7c 7d 7e 2e ..7f xyz{|}~.
77c50 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 30 78 34 */.. 0x40, 0x4
77c60 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77c70 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77c80 20 30 78 34 30 2c 20 20 2f 2a 20 38 30 2e 2e 38 0x40, /* 80..8
77c90 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
77ca0 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 . 0x40, 0x40, 0
77cb0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77cc0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77cd0 30 2c 20 20 2f 2a 20 38 38 2e 2e 38 66 20 20 20 0, /* 88..8f
77ce0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77cf0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77d00 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77d10 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 0, 0x40, 0x40,
77d20 2f 2a 20 39 30 2e 2e 39 37 20 20 20 20 2e 2e 2e /* 90..97 ...
77d30 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c ..... */. 0x40,
77d40 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77d50 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77d60 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 39 x40, 0x40, /* 9
77d70 38 2e 2e 39 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..9f .......
77d80 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 . */. 0x40, 0x4
77d90 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77da0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77db0 20 30 78 34 30 2c 20 20 2f 2a 20 61 30 2e 2e 61 0x40, /* a0..a
77dc0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
77dd0 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 . 0x40, 0x40, 0
77de0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77df0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77e00 30 2c 20 20 2f 2a 20 61 38 2e 2e 61 66 20 20 20 0, /* a8..af
77e10 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77e20 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77e30 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77e40 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 0, 0x40, 0x40,
77e50 2f 2a 20 62 30 2e 2e 62 37 20 20 20 20 2e 2e 2e /* b0..b7 ...
77e60 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c ..... */. 0x40,
77e70 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77e80 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77e90 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 62 x40, 0x40, /* b
77ea0 38 2e 2e 62 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..bf .......
77eb0 2e 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 30 78 . */.. 0x40, 0x
77ec0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77ed0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77ee0 2c 20 30 78 34 30 2c 20 20 2f 2a 20 63 30 2e 2e , 0x40, /* c0..
77ef0 63 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a c7 ........ *
77f00 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 /. 0x40, 0x40,
77f10 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77f20 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77f30 34 30 2c 20 20 2f 2a 20 63 38 2e 2e 63 66 20 20 40, /* c8..cf
77f40 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
77f50 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77f60 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77f70 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77f80 20 2f 2a 20 64 30 2e 2e 64 37 20 20 20 20 2e 2e /* d0..d7 ..
77f90 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 ...... */. 0x40
77fa0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77fb0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77fc0 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 0x40, 0x40, /*
77fd0 64 38 2e 2e 64 66 20 20 20 20 2e 2e 2e 2e 2e 2e d8..df ......
77fe0 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 .. */. 0x40, 0x
77ff0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
78000 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
78010 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 30 2e 2e , 0x40, /* e0..
78020 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a e7 ........ *
78030 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 /. 0x40, 0x40,
78040 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
78050 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
78060 34 30 2c 20 20 2f 2a 20 65 38 2e 2e 65 66 20 20 40, /* e8..ef
78070 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
78080 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
78090 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
780a0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
780b0 20 2f 2a 20 66 30 2e 2e 66 37 20 20 20 20 2e 2e /* f0..f7 ..
780c0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 ...... */. 0x40
780d0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
780e0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
780f0 30 78 34 30 2c 20 30 78 34 30 20 20 20 2f 2a 20 0x40, 0x40 /*
78100 66 38 2e 2e 66 66 20 20 20 20 2e 2e 2e 2e 2e 2e f8..ff ......
78110 2e 2e 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a .. */.};.#endif.
78120 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c .../*.** The fol
78130 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 74 6f 6e lowing singleton
78140 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 67 6c contains the gl
78150 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 obal configurati
78160 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 20 53 51 on for.** the SQ
78170 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f Lite library..*/
78180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78190 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
781a0 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 t Sqlite3Config
781b0 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 3d 20 sqlite3Config =
781c0 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 {. SQLITE_DEFA
781d0 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 2c 20 20 ULT_MEMSTATUS,
781e0 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a 2f 0a 20 /* bMemstat */.
781f0 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
78200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
78210 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f 0a 20 20 bCoreMutex */.
78220 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
78230 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f 2a 20 62 FE==1, /* b
78240 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a 20 20 20 FullMutex */.
78250 30 78 37 66 66 66 66 66 66 65 2c 20 20 20 20 20 0x7ffffffe,
78260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 /* mx
78270 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 31 30 30 Strlen */. 100
78280 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
78290 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4c 6f 6f /* szLoo
782a0 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 35 30 30 kaside */. 500
782b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
782c0 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c 6f 6f 6b /* nLook
782d0 61 73 69 64 65 20 2a 2f 0a 20 20 20 7b 30 2c 30 aside */. {0,0
782e0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 ,0,0,0,0,0,0},
782f0 20 20 20 20 20 20 20 2f 2a 20 6d 20 2a 2f 0a 20 /* m */.
78300 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0
78310 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 ,0,0}, /*
78320 6d 75 74 65 78 20 2a 2f 0a 20 20 20 7b 30 2c 30 mutex */. {0,0
78330 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 ,0,0,0,0,0,0,0,0
78340 2c 30 7d 2c 20 20 20 2f 2a 20 70 63 61 63 68 65 ,0}, /* pcache
78350 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c */. (void*)0,
78360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78370 20 20 2f 2a 20 70 48 65 61 70 20 2a 2f 0a 20 20 /* pHeap */.
78380 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e /* n
783a0 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20 30 2c Heap */. 0, 0,
783b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
783c0 20 20 20 20 20 20 2f 2a 20 6d 6e 48 65 61 70 2c /* mnHeap,
783d0 20 6d 78 48 65 61 70 20 2a 2f 0a 20 20 20 28 76 mxHeap */. (v
783e0 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 oid*)0,
783f0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 53 63 72 /* pScr
78400 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20 atch */. 0,
78410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78420 20 20 20 20 20 20 2f 2a 20 73 7a 53 63 72 61 74 /* szScrat
78430 63 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ch */. 0,
78440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78450 20 20 20 20 2f 2a 20 6e 53 63 72 61 74 63 68 20 /* nScratch
78460 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 */. (void*)0,
78470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78480 20 2f 2a 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 /* pPage */.
78490 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
784a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a /* sz
784b0 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Page */. 0,
784c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
784d0 20 20 20 20 20 20 2f 2a 20 6e 50 61 67 65 20 2a /* nPage *
784e0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
784f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78500 2f 2a 20 6d 78 50 61 72 73 65 72 53 74 61 63 6b /* mxParserStack
78510 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
78520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78530 20 20 2f 2a 20 73 68 61 72 65 64 43 61 63 68 65 /* sharedCache
78540 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20 2f 2a Enabled */. /*
78550 20 41 6c 6c 20 74 68 65 20 72 65 73 74 20 73 68 All the rest sh
78560 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 69 ould always be i
78570 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 nitialized to ze
78580 72 6f 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ro */. 0,
78590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
785a0 20 20 20 20 2f 2a 20 69 73 49 6e 69 74 20 2a 2f /* isInit */
785b0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
785c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
785d0 2a 20 69 6e 50 72 6f 67 72 65 73 73 20 2a 2f 0a * inProgress */.
785e0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
785f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78600 20 69 73 4d 75 74 65 78 49 6e 69 74 20 2a 2f 0a isMutexInit */.
78610 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
78620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78630 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 2a 2f isMallocInit */
78640 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
78650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
78660 2a 20 69 73 50 43 61 63 68 65 49 6e 69 74 20 2a * isPCacheInit *
78670 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
78680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78690 2f 2a 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f /* pInitMutex */
786a0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
786b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
786c0 2a 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 20 * nRefInitMutex
786d0 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 */.};.../*.** Ha
786e0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 67 6c 6f sh table for glo
786f0 62 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20 2d 20 bal functions -
78700 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6d 6d 6f 6e functions common
78710 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 61 74 61 62 to all.** datab
78720 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
78730 20 20 41 66 74 65 72 20 69 6e 69 74 69 61 6c 69 After initiali
78740 7a 61 74 69 6f 6e 2c 20 74 68 69 73 20 74 61 62 zation, this tab
78750 6c 65 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e le is.** read-on
78760 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
78770 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 IVATE SQLITE_WSD
78780 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c FuncDefHash sql
78790 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 ite3GlobalFuncti
787a0 6f 6e 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ons;../*.** The
787b0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 70 65 value of the "pe
787c0 6e 64 69 6e 67 22 20 62 79 74 65 20 6d 75 73 74 nding" byte must
787d0 20 62 65 20 30 78 34 30 30 30 30 30 30 30 20 28 be 0x40000000 (
787e0 31 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a 1 byte past the.
787f0 2a 2a 20 31 2d 67 69 62 61 62 79 74 65 20 62 6f ** 1-gibabyte bo
78800 75 6e 64 61 72 79 29 20 69 6e 20 61 20 63 6f 6d undary) in a com
78810 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 patible database
78820 2e 20 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 . SQLite never
78830 75 73 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 uses.** the data
78840 62 61 73 65 20 70 61 67 65 20 74 68 61 74 20 63 base page that c
78850 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 65 6e 64 ontains the pend
78860 69 6e 67 20 62 79 74 65 2e 20 20 49 74 20 6e 65 ing byte. It ne
78870 76 65 72 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 ver attempts.**
78880 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 to read or write
78890 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 65 that page. The
788a0 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 70 61 pending byte pa
788b0 67 65 20 69 73 20 73 65 74 20 61 73 73 69 67 6e ge is set assign
788c0 0a 2a 2a 20 66 6f 72 20 75 73 65 20 62 79 20 74 .** for use by t
788d0 68 65 20 56 46 53 20 6c 61 79 65 72 73 20 61 73 he VFS layers as
788e0 20 73 70 61 63 65 20 66 6f 72 20 6d 61 6e 61 67 space for manag
788f0 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a ing file locks..
78900 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 65 73 **.** During tes
78910 74 69 6e 67 2c 20 69 74 20 69 73 20 6f 66 74 65 ting, it is ofte
78920 6e 20 64 65 73 69 72 61 62 6c 65 20 74 6f 20 6d n desirable to m
78930 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 ove the pending
78940 62 79 74 65 20 74 6f 0a 2a 2a 20 61 20 64 69 66 byte to.** a dif
78950 66 65 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 ferent position
78960 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 in the file. Th
78970 69 73 20 61 6c 6c 6f 77 73 20 63 6f 64 65 20 74 is allows code t
78980 68 61 74 20 68 61 73 20 74 6f 0a 2a 2a 20 64 65 hat has to.** de
78990 61 6c 20 77 69 74 68 20 74 68 65 20 70 65 6e 64 al with the pend
789a0 69 6e 67 20 62 79 74 65 20 74 6f 20 72 75 6e 20 ing byte to run
789b0 6f 6e 20 66 69 6c 65 73 20 74 68 61 74 20 61 72 on files that ar
789c0 65 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 0a 2a e much smaller.*
789d0 2a 20 74 68 61 6e 20 31 20 47 69 42 2e 20 20 54 * than 1 GiB. T
789e0 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f he sqlite3_test_
789f0 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 control() interf
78a00 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 ace can be used
78a10 74 6f 0a 2a 2a 20 6d 6f 76 65 20 74 68 65 20 70 to.** move the p
78a20 65 6e 64 69 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a ending byte..**.
78a30 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a 20 20 43 ** IMPORTANT: C
78a40 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 hanging the pend
78a50 69 6e 67 20 62 79 74 65 20 74 6f 20 61 6e 79 20 ing byte to any
78a60 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e value other than
78a70 0a 2a 2a 20 30 78 34 30 30 30 30 30 30 30 20 72 .** 0x40000000 r
78a80 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 69 6e 63 esults in an inc
78a90 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 ompatible databa
78aa0 73 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 21 0a se file format!.
78ab0 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 ** Changing the
78ac0 70 65 6e 64 69 6e 67 20 62 79 74 65 20 64 75 72 pending byte dur
78ad0 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 72 65 ing operating re
78ae0 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e sults in undefin
78af0 65 64 0a 2a 2a 20 61 6e 64 20 64 69 6c 65 74 65 ed.** and dilete
78b00 72 69 6f 75 73 20 62 65 68 61 76 69 6f 72 2e 0a rious behavior..
78b10 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
78b20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 65 6e E int sqlite3Pen
78b30 64 69 6e 67 42 79 74 65 20 3d 20 30 78 34 30 30 dingByte = 0x400
78b40 30 30 30 30 30 3b 0a 0a 2f 2a 0a 2a 2a 20 50 72 00000;../*.** Pr
78b50 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63 6f operties of opco
78b60 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f des. The OPFLG_
78b70 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 INITIALIZER macr
78b80 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 o is.** created
78b90 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b by mkopcodeh.awk
78ba0 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 during compilat
78bb0 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f 62 ion. Data is ob
78bc0 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 tained.** from t
78bd0 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c he comments foll
78be0 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65 20 owing the "case
78bf0 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65 6d OP_xxxx:" statem
78c00 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 76 ents in.** the v
78c10 64 62 65 2e 63 20 66 69 6c 65 2e 20 20 0a 2a 2f dbe.c file. .*/
78c20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
78c30 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
78c40 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 har sqlite3Opcod
78c50 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 eProperty[] = OP
78c60 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b FLG_INITIALIZER;
78c70 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
78c80 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e * End of global.
78c90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
78ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78cc0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
78cd0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 61 * Begin file sta
78ce0 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tus.c **********
78cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78d10 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e /./*.** 2008 Jun
78d20 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 e 18.**.** The a
78d30 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
78d40 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
78d50 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
78d60 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
78d70 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
78d80 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
78d90 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
78da0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
78db0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
78dc0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
78dd0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
78de0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
78df0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
78e00 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
78e10 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
78e20 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
78e30 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78e80 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d ***.**.** This m
78e90 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 odule implements
78ea0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 the sqlite3_sta
78eb0 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 20 tus() interface
78ec0 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 and related.** f
78ed0 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2f unctionality..*/
78ee0 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 ../*.** Variable
78ef0 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 s in which to re
78f00 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e 66 6f cord status info
78f10 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 rmation..*/.type
78f20 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
78f30 65 33 53 74 61 74 54 79 70 65 20 73 71 6c 69 74 e3StatType sqlit
78f40 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 61 74 e3StatType;.stat
78f50 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 ic SQLITE_WSD st
78f60 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 ruct sqlite3Stat
78f70 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 Type {. int now
78f80 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 Value[9];
78f90 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 6c /* Current val
78fa0 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 61 ue */. int mxVa
78fb0 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 20 lue[9];
78fc0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75 /* Maximum valu
78fd0 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 74 e */.} sqlite3St
78fe0 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c at = { {0,}, {0,
78ff0 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 77 } };.../* The "w
79000 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69 sdStat" macro wi
79010 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 ll resolve to th
79020 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 e status informa
79030 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65 tion.** state ve
79040 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 ctor. If writab
79050 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
79060 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e s unsupported on
79070 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 the target,.**
79080 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 we have to locat
79090 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 e the state vect
790a0 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 or at run-time.
790b0 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d In the more com
790c0 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 mon.** case wher
790d0 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 e writable stati
790e0 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 c data is suppor
790f0 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e ted, wsdStat can
79100 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a refer directly.
79110 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 ** to the "sqlit
79120 65 33 53 74 61 74 22 20 73 74 61 74 65 20 76 65 e3Stat" state ve
79130 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 ctor declared ab
79140 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ove..*/.#ifdef S
79150 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 QLITE_OMIT_WSD.#
79160 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 define wsdStatI
79170 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 74 nit sqlite3Stat
79180 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 41 Type *x = &GLOBA
79190 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 L(sqlite3StatTyp
791a0 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a 23 e,sqlite3Stat).#
791b0 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20 define wsdStat
791c0 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 x[0].#else.# def
791d0 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 0a ine wsdStatInit.
791e0 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 # define wsdStat
791f0 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 6e sqlite3Stat.#en
79200 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
79210 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 n the current va
79220 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 lue of a status
79230 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 parameter..*/.SQ
79240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
79250 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 sqlite3StatusVa
79260 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 77 lue(int op){. w
79270 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 sdStatInit;. as
79280 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f sert( op>=0 && o
79290 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 p<ArraySize(wsdS
792a0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b tat.nowValue) );
792b0 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 61 . return wsdSta
792c0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a t.nowValue[op];.
792d0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 74 }../*.** Add N t
792e0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 o the value of a
792f0 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e 20 status record.
79300 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
79310 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 hat the.** calle
79320 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 69 r holds appropri
79330 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 ate locks..*/.SQ
79340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
79350 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 d sqlite3StatusA
79360 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 4e dd(int op, int N
79370 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 ){. wsdStatInit
79380 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d ;. assert( op>=
79390 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 0 && op<ArraySiz
793a0 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
793b0 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 ue) );. wsdStat
793c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d .nowValue[op] +=
793d0 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 N;. if( wsdSta
793e0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 t.nowValue[op]>w
793f0 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f sdStat.mxValue[o
79400 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 p] ){. wsdSta
79410 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 t.mxValue[op] =
79420 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
79430 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a [op];. }.}../*.
79440 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 ** Set the value
79450 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f 20 of a status to
79460 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 X..*/.SQLITE_PRI
79470 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
79480 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 6f 3StatusSet(int o
79490 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 64 p, int X){. wsd
794a0 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 StatInit;. asse
794b0 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c rt( op>=0 && op<
794c0 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 ArraySize(wsdSta
794d0 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 t.nowValue) );.
794e0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
794f0 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 28 e[op] = X;. if(
79500 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
79510 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 e[op]>wsdStat.mx
79520 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 Value[op] ){.
79530 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 wsdStat.mxValue
79540 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e [op] = wsdStat.n
79550 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d owValue[op];. }
79560 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 .}../*.** Query
79570 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 status informati
79580 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 on..**.** This i
79590 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 mplementation as
795a0 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 69 sumes that readi
795b0 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e ng or writing an
795c0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d 62 aligned.** 32-b
795d0 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 6e it integer is an
795e0 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f atomic operatio
795f0 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 75 n. If that assu
79600 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 72 mption is not tr
79610 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 ue,.** then this
79620 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 routine is not
79630 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 threadsafe..*/.S
79640 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
79650 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74 lite3_status(int
79660 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65 op, int *pCurre
79670 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61 nt, int *pHighwa
79680 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c ter, int resetFl
79690 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e ag){. wsdStatIn
796a0 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 7c it;. if( op<0 |
796b0 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 28 | op>=ArraySize(
796c0 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 wsdStat.nowValue
796d0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
796e0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
796f0 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20 3d }. *pCurrent =
79700 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 wsdStat.nowValu
79710 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 68 77 e[op];. *pHighw
79720 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e 6d ater = wsdStat.m
79730 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 69 66 xValue[op];. if
79740 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 ( resetFlag ){.
79750 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c wsdStat.mxVal
79760 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 ue[op] = wsdStat
79770 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 .nowValue[op];.
79780 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
79790 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
797a0 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 Query status inf
797b0 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 73 ormation for a s
797c0 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 63 ingle database c
797d0 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c onnection.*/.SQL
797e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
797f0 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 0a 20 te3_db_status(.
79800 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
79810 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
79820 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
79830 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 20 69 n whose status i
79840 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 20 69 s desired */. i
79850 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 nt op,
79860 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 76 /* Status v
79870 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 43 erb */. int *pC
79880 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f urrent, /
79890 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e 74 20 * Write current
798a0 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 20 20 value here */.
798b0 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c int *pHighwater,
798c0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68 /* Write h
798d0 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 68 igh-water mark h
798e0 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 ere */. int res
798f0 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 20 2f etFlag /
79900 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 61 74 * Reset high-wat
79910 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65 20 er mark if true
79920 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 */.){. switch(
79930 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 op ){. case S
79940 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c QLITE_DBSTATUS_L
79950 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20 7b OOKASIDE_USED: {
79960 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e 74 . *pCurrent
79970 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 = db->lookaside
79980 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70 48 .nOut;. *pH
79990 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e 6c ighwater = db->l
799a0 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b 0a ookaside.mxOut;.
799b0 20 20 20 20 20 20 69 66 28 20 72 65 73 65 74 46 if( resetF
799c0 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 64 lag ){. d
799d0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f b->lookaside.mxO
799e0 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 ut = db->lookasi
799f0 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d de.nOut;. }
79a00 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
79a10 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
79a20 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
79a30 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
79a40 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
79a50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
79a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
79a70 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 20 2a nd of status.c *
79a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
79ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
79ac0 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 2e 63 egin file date.c
79ad0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
79ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
79b00 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f 62 65 *.** 2003 Octobe
79b10 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 31.**.** The a
79b20 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
79b30 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
79b40 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
79b50 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
79b60 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
79b70 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
79b80 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
79b90 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
79ba0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
79bb0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
79bc0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
79bd0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
79be0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
79bf0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
79c00 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
79c10 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
79c20 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79c70 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
79c80 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
79c90 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
79ca0 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 61 6e mplement date an
79cb0 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 d time.** functi
79cc0 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 20 ons for SQLite.
79cd0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 .**.** There is
79ce0 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 only one export
79cf0 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 ed symbol in thi
79d00 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e s file - the fun
79d10 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 ction.** sqlite3
79d20 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 RegisterDateTime
79d30 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e Functions() foun
79d40 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 d at the bottom
79d50 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 of the file..**
79d60 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 All other code h
79d70 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a as file scope..*
79d80 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 *.** SQLite proc
79d90 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 esses all times
79da0 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c and dates as Jul
79db0 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e ian Day numbers.
79dc0 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 The.** dates a
79dd0 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f nd times are sto
79de0 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65 red as the numbe
79df0 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 r of days since
79e00 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e noon.** in Green
79e10 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 wich on November
79e20 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 24, 4714 B.C. a
79e30 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
79e40 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c Gregorian.** cal
79e50 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a endar system. .*
79e60 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 *.** 1970-01-01
79e70 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 00:00:00 is JD 2
79e80 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 440587.5.** 2000
79e90 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 -01-01 00:00:00
79ea0 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a is JD 2451544.5.
79eb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 **.** This imple
79ec0 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73 mention requires
79ed0 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78 70 years to be exp
79ee0 72 65 73 73 65 64 20 61 73 20 61 20 34 2d 64 69 ressed as a 4-di
79ef0 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 git number.** wh
79f00 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f ich means that o
79f10 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 65 65 nly dates betwee
79f20 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 n 0000-01-01 and
79f30 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 9999-12-31 can.
79f40 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 ** be represente
79f50 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a d, even though j
79f60 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
79f70 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 s allow a much w
79f80 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 ider.** range of
79f90 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 dates..**.** Th
79fa0 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 e Gregorian cale
79fb0 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 20 75 ndar system is u
79fc0 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 sed for all date
79fd0 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 s and times,.**
79fe0 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20 even those that
79ff0 70 72 65 64 61 74 65 20 74 68 65 20 47 72 65 67 predate the Greg
7a000 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 orian calendar.
7a010 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61 Historians usua
7a020 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a lly.** use the J
7a030 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 ulian calendar f
7a040 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 20 74 or dates prior t
7a050 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 o 1582-10-15 and
7a060 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 for some.** dat
7a070 65 73 20 61 66 74 65 72 77 61 72 64 73 2c 20 64 es afterwards, d
7a080 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 epending on loca
7a090 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74 le. Beware of t
7a0a0 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a his difference..
7a0b0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 **.** The conver
7a0c0 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 sion algorithms
7a0d0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 are implemented
7a0e0 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 based on descrip
7a0f0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 tions.** in the
7a100 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a following text:.
7a110 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 **.** Jean
7a120 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 Meeus.** As
7a130 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 tronomical Algor
7a140 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 ithms, 2nd Editi
7a150 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 on, 1998.**
7a160 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 ISBM 0-943396-6
7a170 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 1-1.** Will
7a180 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a mann-Bell, Inc.*
7a190 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c * Richmond,
7a1a0 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a Virginia (USA).
7a1b0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d */.#include <tim
7a1c0 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 e.h>..#ifndef SQ
7a1d0 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 LITE_OMIT_DATETI
7a1e0 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 ME_FUNCS../*.**
7a1f0 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f 77 On recent Window
7a200 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65 s platforms, the
7a210 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20 66 localtime_s() f
7a220 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69 6c unction is avail
7a230 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 able.** as part
7a240 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20 43 of the "Secure C
7a250 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65 6e RT". It is essen
7a260 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e tially equivalen
7a270 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 t to .** localti
7a280 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c 65 me_r() available
7a290 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 under most POSI
7a2a0 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 X platforms, exc
7a2b0 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a 2a ept that the .**
7a2c0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70 61 order of the pa
7a2d0 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 rameters is reve
7a2e0 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 rsed..**.** See
7a2f0 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 http://msdn.micr
7a300 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f osoft.com/en-us/
7a310 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 65 library/a442x3ye
7a320 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a (VS.80).aspx..**
7a330 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72 20 .** If the user
7a340 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74 65 has not indicate
7a350 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69 d to use localti
7a360 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74 me_r() or localt
7a370 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61 ime_s().** alrea
7a380 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e dy, check for an
7a390 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76 69 MSVC build envi
7a3a0 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72 6f ronment that pro
7a3b0 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 vides .** localt
7a3c0 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 ime_s()..*/.#if
7a3d0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f !defined(HAVE_LO
7a3e0 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21 64 CALTIME_R) && !d
7a3f0 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 efined(HAVE_LOCA
7a400 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20 LTIME_S) && \.
7a410 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f defined(_MSC_
7a420 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64 28 VER) && defined(
7a430 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44 45 _CRT_INSECURE_DE
7a440 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e 65 PRECATE).#define
7a450 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f HAVE_LOCALTIME_
7a460 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a S 1.#endif../*.*
7a470 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66 6f * A structure fo
7a480 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 r holding a sing
7a490 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 le date and time
7a4a0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
7a4b0 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 74 uct DateTime Dat
7a4c0 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 eTime;.struct Da
7a4d0 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 teTime {. sqlit
7a4e0 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a e3_int64 iJD; /*
7a4f0 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 The julian day
7a500 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36 34 number times 864
7a510 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 00000 */. int Y
7a520 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a , M, D; /*
7a530 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e Year, month, an
7a540 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 d day */. int h
7a550 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a , m; /*
7a560 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 Hour and minute
7a570 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 s */. int tz;
7a580 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d /* Tim
7a590 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 ezone offset in
7a5a0 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 minutes */. dou
7a5b0 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20 20 ble s;
7a5c0 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 /* Seconds */.
7a5d0 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 char validYMD;
7a5e0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 /* True (1) i
7a5f0 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 f Y,M,D are vali
7a600 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 d */. char vali
7a610 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 dHMS; /* Tru
7a620 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 e (1) if h,m,s a
7a630 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 re valid */. ch
7a640 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 ar validJD;
7a650 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 /* True (1) if
7a660 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a iJD is valid */.
7a670 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 char validTZ;
7a680 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 /* True (1)
7a690 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20 if tz is valid
7a6a0 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f */.};.../*.** Co
7a6b0 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f nvert zDate into
7a6c0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 one or more int
7a6d0 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e egers. Addition
7a6e0 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 al arguments.**
7a6f0 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f come in groups o
7a700 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a f 5 as follows:.
7a710 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 **.** N
7a720 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 number of di
7a730 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65 gits in the inte
7a740 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e ger.** min
7a750 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c minimum all
7a760 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 owed value of th
7a770 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 e integer.**
7a780 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d max maxim
7a790 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 um allowed value
7a7a0 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a of the integer.
7a7b0 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20 20 ** nextC
7a7c0 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72 first character
7a7d0 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65 67 after the integ
7a7e0 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c er.** pVal
7a7f0 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72 69 where to wri
7a800 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 te the integers
7a810 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e value..**.** Con
7a820 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 versions continu
7a830 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 e until one with
7a840 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 nextC==0 is enc
7a850 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 ountered..** The
7a860 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
7a870 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
7a880 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 successful conve
7a890 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 rsions..*/.stati
7a8a0 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 28 c int getDigits(
7a8b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 const char *zDat
7a8c0 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 e, ...){. va_li
7a8d0 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c st ap;. int val
7a8e0 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 ;. int N;. int
7a8f0 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b min;. int max;
7a900 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 . int nextC;.
7a910 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 int *pVal;. int
7a920 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 cnt = 0;. va_s
7a930 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b tart(ap, zDate);
7a940 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 . do{. N = v
7a950 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a a_arg(ap, int);.
7a960 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 min = va_arg
7a970 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d (ap, int);. m
7a980 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 ax = va_arg(ap,
7a990 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 int);. nextC
7a9a0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 = va_arg(ap, int
7a9b0 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61 );. pVal = va
7a9c0 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a _arg(ap, int*);.
7a9d0 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 val = 0;.
7a9e0 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 while( N-- ){.
7a9f0 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 if( !sqlite
7aa00 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 3Isdigit(*zDate)
7aa10 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
7aa20 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a end_getDigits;.
7aa30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61 }. va
7aa40 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 l = val*10 + *zD
7aa50 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 ate - '0';.
7aa60 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a zDate++;. }.
7aa70 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 if( val<min
7aa80 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e || val>max || (n
7aa90 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43 extC!=0 && nextC
7aaa0 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 !=*zDate) ){.
7aab0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 goto end_getD
7aac0 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 igits;. }.
7aad0 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 *pVal = val;.
7aae0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 zDate++;. c
7aaf0 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 nt++;. }while(
7ab00 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 nextC );.end_get
7ab10 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 Digits:. va_end
7ab20 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 (ap);. return c
7ab30 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 nt;.}../*.** Rea
7ab40 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 d text from z[]
7ab50 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f and convert into
7ab60 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
7ab70 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 t number. Retur
7ab80 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 n.** the number
7ab90 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 72 of digits conver
7aba0 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ted..*/.#define
7abb0 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 33 getValue sqlite3
7abc0 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 AtoF../*.** Pars
7abd0 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 e a timezone ext
7abe0 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e ension on the en
7abf0 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65 d of a date-time
7ac00 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69 ..** The extensi
7ac10 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 on is of the for
7ac20 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 m:.**.**
7ac30 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a (+/-)HH:MM.**.**
7ac40 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e Or the "zulu" n
7ac50 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 otation:.**.**
7ac60 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 Z.**.** If
7ac70 20 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75 the parse is su
7ac80 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 ccessful, write
7ac90 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
7aca0 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e nutes.** of chan
7acb0 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 ge in p->tz and
7acc0 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 return 0. If a
7acd0 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 parser error occ
7ace0 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e urs,.** return n
7acf0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 on-zero..**.** A
7ad00 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 missing specifi
7ad10 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 er is not consid
7ad20 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a ered an error..*
7ad30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 /.static int par
7ad40 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 seTimezone(const
7ad50 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 char *zDate, Da
7ad60 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e teTime *p){. in
7ad70 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 t sgn = 0;. int
7ad80 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 nHr, nMn;. int
7ad90 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c c;. while( sql
7ada0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 ite3Isspace(*zDa
7adb0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 te) ){ zDate++;
7adc0 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 }. p->tz = 0;.
7add0 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 c = *zDate;. i
7ade0 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 f( c=='-' ){.
7adf0 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c sgn = -1;. }el
7ae00 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b se if( c=='+' ){
7ae10 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 . sgn = +1;.
7ae20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a }else if( c=='Z
7ae30 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 ' || c=='z' ){.
7ae40 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
7ae50 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a goto zulu_time;.
7ae60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
7ae70 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 urn c!=0;. }.
7ae80 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 zDate++;. if( g
7ae90 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 etDigits(zDate,
7aea0 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 2, 0, 14, ':', &
7aeb0 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 nHr, 2, 0, 59, 0
7aec0 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 , &nMn)!=2 ){.
7aed0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
7aee0 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 zDate += 5;.
7aef0 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e p->tz = sgn*(nMn
7af00 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 + nHr*60);.zulu
7af10 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 _time:. while(
7af20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a sqlite3Isspace(*
7af30 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b zDate) ){ zDate+
7af40 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a +; }. return *z
7af50 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Date!=0;.}../*.*
7af60 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66 * Parse times of
7af70 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 the form HH:MM
7af80 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 or HH:MM:SS or H
7af90 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a H:MM:SS.FFFF..**
7afa0 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 The HH, MM, and
7afb0 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65 SS must each be
7afc0 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74 exactly 2 digit
7afd0 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 s. The.** fract
7afe0 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 ional seconds FF
7aff0 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 FF can be one or
7b000 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a more digits..**
7b010 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 .** Return 1 if
7b020 74 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69 there is a parsi
7b030 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f ng error and 0 o
7b040 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 n success..*/.st
7b050 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68 atic int parseHh
7b060 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20 MmSs(const char
7b070 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 *zDate, DateTime
7b080 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d *p){. int h, m
7b090 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 , s;. double ms
7b0a0 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 = 0.0;. if( ge
7b0b0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 tDigits(zDate, 2
7b0c0 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 , 0, 24, ':', &h
7b0d0 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 , 2, 0, 59, 0, &
7b0e0 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 m)!=2 ){. ret
7b0f0 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 urn 1;. }. zDa
7b100 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a te += 5;. if( *
7b110 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 zDate==':' ){.
7b120 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 zDate++;. i
7b130 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 f( getDigits(zDa
7b140 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c te, 2, 0, 59, 0,
7b150 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 &s)!=1 ){.
7b160 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
7b170 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b . zDate += 2;
7b180 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d . if( *zDate=
7b190 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 49 ='.' && sqlite3I
7b1a0 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 sdigit(zDate[1])
7b1b0 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 ){. double
7b1c0 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 rScale = 1.0;.
7b1d0 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 zDate++;.
7b1e0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
7b1f0 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 e3Isdigit(*zDate
7b200 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 ) ){. ms
7b210 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 = ms*10.0 + *zDa
7b220 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 te - '0';.
7b230 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 rScale *= 10.0
7b240 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b ;. zDate+
7b250 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 +;. }.
7b260 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 ms /= rScale;.
7b270 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
7b280 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 s = 0;. }. p
7b290 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
7b2a0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 p->validHMS = 1
7b2b0 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 ;. p->h = h;.
7b2c0 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 p->m = m;. p->s
7b2d0 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 = s + ms;. if(
7b2e0 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a parseTimezone(z
7b2f0 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72 Date, p) ) retur
7b300 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 n 1;. p->validT
7b310 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 Z = (p->tz!=0)?1
7b320 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a :0;. return 0;.
7b330 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
7b340 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 from YYYY-MM-DD
7b350 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c HH:MM:SS to jul
7b360 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77 ian day. We alw
7b370 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 ays assume.** th
7b380 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 at the YYYY-MM-D
7b390 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74 D is according t
7b3a0 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 o the Gregorian
7b3b0 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 calendar..**.**
7b3c0 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 Reference: Meeu
7b3d0 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 s page 61.*/.sta
7b3e0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
7b3f0 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b JD(DateTime *p){
7b400 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 . int Y, M, D,
7b410 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 A, B, X1, X2;..
7b420 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 if( p->validJD
7b430 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
7b440 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 p->validYMD ){.
7b450 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 Y = p->Y;.
7b460 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 M = p->M;. D
7b470 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 = p->D;. }else
7b480 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20 {. Y = 2000;
7b490 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 /* If no YMD sp
7b4a0 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20 ecified, assume
7b4b0 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 2000-Jan-01 */.
7b4c0 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 M = 1;. D
7b4d0 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d = 1;. }. if( M
7b4e0 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a <=2 ){. Y--;.
7b4f0 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d M += 12;. }
7b500 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 . A = Y/100;.
7b510 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 B = 2 - A + (A/4
7b520 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 2a );. X1 = 36525*
7b530 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 (Y+4716)/100;.
7b540 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 X2 = 306001*(M+1
7b550 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a )/10000;. p->iJ
7b560 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 D = (sqlite3_int
7b570 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 44 64)((X1 + X2 + D
7b580 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 20 + B - 1524.5 )
7b590 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70 * 86400000);. p
7b5a0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
7b5b0 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 if( p->validHMS
7b5c0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b ){. p->iJD +
7b5d0 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b = p->h*3600000 +
7b5e0 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 p->m*60000 + (s
7b5f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 2d qlite3_int64)(p-
7b600 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 66 >s*1000);. if
7b610 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a ( p->validTZ ){.
7b620 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 p->iJD -=
7b630 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 p->tz*60000;.
7b640 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d p->validYMD =
7b650 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 0;. p->val
7b660 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 idHMS = 0;.
7b670 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b p->validTZ = 0;
7b680 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
7b690 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 20 .** Parse dates
7b6a0 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a of the form.**.*
7b6b0 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 * YYYY-MM-DD
7b6c0 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a HH:MM:SS.FFF.**
7b6d0 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 YYYY-MM-DD
7b6e0 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 HH:MM:SS.**
7b6f0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d YYYY-MM-DD HH:MM
7b700 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d .** YYYY-MM-
7b710 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 DD.**.** Write t
7b720 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 he result into t
7b730 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75 he DateTime stru
7b740 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e cture and return
7b750 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 0.** on success
7b760 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 6e and 1 if the in
7b770 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e 6f put string is no
7b780 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a t a well-formed.
7b790 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 ** date..*/.stat
7b7a0 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79 ic int parseYyyy
7b7b0 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20 MmDd(const char
7b7c0 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 *zDate, DateTime
7b7d0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d *p){. int Y, M
7b7e0 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 , D, neg;.. if(
7b7f0 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 zDate[0]=='-' )
7b800 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 {. zDate++;.
7b810 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 neg = 1;. }e
7b820 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 lse{. neg = 0
7b830 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 44 ;. }. if( getD
7b840 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c igits(zDate,4,0,
7b850 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 9999,'-',&Y,2,1,
7b860 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 12,'-',&M,2,1,31
7b870 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 ,0,&D)!=3 ){.
7b880 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
7b890 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 zDate += 10;.
7b8a0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
7b8b0 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c space(*zDate) ||
7b8c0 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 'T'==*(u8*)zDat
7b8d0 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a e ){ zDate++; }.
7b8e0 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 if( parseHhMmS
7b8f0 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 s(zDate, p)==0 )
7b900 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 {. /* We got
7b910 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 the time */. }e
7b920 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d lse if( *zDate==
7b930 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 0 ){. p->vali
7b940 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 dHMS = 0;. }els
7b950 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b e{. return 1;
7b960 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a . }. p->validJ
7b970 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 D = 0;. p->vali
7b980 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 dYMD = 1;. p->Y
7b990 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b = neg ? -Y : Y;
7b9a0 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 . p->M = M;. p
7b9b0 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 ->D = D;. if( p
7b9c0 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 ->validTZ ){.
7b9d0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7b9e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
7b9f0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
7ba00 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72 time to the curr
7ba10 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65 ent time reporte
7ba20 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a d by the VFS.*/.
7ba30 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 44 static void setD
7ba40 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 ateTimeToCurrent
7ba50 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
7ba60 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 *context, DateT
7ba70 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c ime *p){. doubl
7ba80 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a e r;. sqlite3 *
7ba90 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
7baa0 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
7bab0 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 ontext);. sqlit
7bac0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
7bad0 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 db->pVfs, &r);.
7bae0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 p->iJD = (sqlit
7baf0 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 e3_int64)(r*8640
7bb00 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 0000.0 + 0.5);.
7bb10 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b p->validJD = 1;
7bb20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 .}../*.** Attemp
7bb30 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67 t to parse the g
7bb40 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f iven string into
7bb50 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 a Julian Day Nu
7bb60 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a mber. Return.**
7bb70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
7bb80 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rrors..**.** The
7bb90 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 following are a
7bba0 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 cceptable forms
7bbb0 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74 for the input st
7bbc0 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ring:.**.**
7bbd0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
7bbe0 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a M:SS.FFF +/-HH:
7bbf0 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e MM.** DDDD.
7bc00 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a DD .** now.
7bc10 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 **.** In the fir
7bc20 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d st form, the +/-
7bc30 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 HH:MM is always
7bc40 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 optional. The f
7bc50 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 ractional.** sec
7bc60 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 onds extension (
7bc70 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f the ".FFF") is o
7bc80 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 ptional. The se
7bc90 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a conds portion.**
7bca0 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20 (":SS.FFF") is
7bcb0 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 option. The yea
7bcc0 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62 r and date can b
7bcd0 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e e omitted as lon
7bce0 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 g.** as there is
7bcf0 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 a time string.
7bd00 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67 The time string
7bd10 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 can be omitted
7bd20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 as long.** as th
7bd30 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e ere is a year an
7bd40 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 d date..*/.stati
7bd50 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f c int parseDateO
7bd60 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 rTime(. sqlite3
7bd70 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7bd80 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 t, . const char
7bd90 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 65 *zDate, . Date
7bda0 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 Time *p.){. int
7bdb0 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f isRealNum; /
7bdc0 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 * Return from sq
7bdd0 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 29 2e lite3IsNumber().
7bde0 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 Not used */.
7bdf0 69 66 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44 if( parseYyyyMmD
7be00 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b d(zDate,p)==0 ){
7be10 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
7be20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 73 65 }else if( parse
7be30 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 HhMmSs(zDate, p)
7be40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
7be50 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 n 0;. }else if(
7be60 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
7be70 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 zDate,"now")==0)
7be80 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 6d {. setDateTim
7be90 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 eToCurrent(conte
7bea0 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75 xt, p);. retu
7beb0 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 rn 0;. }else if
7bec0 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 ( sqlite3IsNumbe
7bed0 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65 61 6c r(zDate, &isReal
7bee0 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Num, SQLITE_UTF8
7bef0 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 ) ){. double
7bf00 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28 r;. getValue(
7bf10 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 zDate, &r);.
7bf20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 p->iJD = (sqlite
7bf30 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 3_int64)(r*86400
7bf40 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 000.0 + 0.5);.
7bf50 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 p->validJD = 1
7bf60 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
7bf70 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
7bf80 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 }../*.** Compute
7bf90 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 the Year, Month
7bfa0 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 , and Day from t
7bfb0 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 he julian day nu
7bfc0 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mber..*/.static
7bfd0 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 void computeYMD(
7bfe0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
7bff0 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 int Z, A, B, C,
7c000 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 D, E, X1;. if(
7c010 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72 65 p->validYMD ) re
7c020 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e turn;. if( !p->
7c030 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 validJD ){. p
7c040 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 ->Y = 2000;.
7c050 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d p->M = 1;. p-
7c060 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b >D = 1;. }else{
7c070 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 28 28 . Z = (int)((
7c080 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 p->iJD + 4320000
7c090 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 0)/86400000);.
7c0a0 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d A = (int)((Z -
7c0b0 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 36 35 1867216.25)/365
7c0c0 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20 3d 20 24.25);. A =
7c0d0 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 Z + 1 + A - (A/4
7c0e0 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b 20 31 );. B = A + 1
7c0f0 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e 524;. C = (in
7c100 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 t)((B - 122.1)/3
7c110 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20 3d 20 65.25);. D =
7c120 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20 (36525*C)/100;.
7c130 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28 42 2d E = (int)((B-
7c140 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20 20 20 D)/30.6001);.
7c150 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30 2e 36 X1 = (int)(30.6
7c160 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44 001*E);. p->D
7c170 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 = B - D - X1;.
7c180 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f p->M = E<14 ?
7c190 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 E-1 : E-13;.
7c1a0 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f p->Y = p->M>2 ?
7c1b0 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 C - 4716 : C -
7c1c0 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 4715;. }. p->v
7c1d0 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a alidYMD = 1;.}..
7c1e0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
7c1f0 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 e Hour, Minute,
7c200 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d and Seconds from
7c210 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 the julian day
7c220 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
7c230 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d c void computeHM
7c240 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a S(DateTime *p){.
7c250 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 int s;. if( p
7c260 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72 65 74 ->validHMS ) ret
7c270 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 urn;. computeJD
7c280 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 (p);. s = (int)
7c290 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 ((p->iJD + 43200
7c2a0 30 30 30 29 20 25 20 38 36 34 30 30 30 30 30 29 000) % 86400000)
7c2b0 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 ;. p->s = s/100
7c2c0 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 0.0;. s = (int)
7c2d0 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 p->s;. p->s -=
7c2e0 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 s;. p->h = s/36
7c2f0 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 00;. s -= p->h*
7c300 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 3600;. p->m = s
7c310 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 /60;. p->s += s
7c320 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d - p->m*60;. p-
7c330 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d >validHMS = 1;.}
7c340 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 ../*.** Compute
7c350 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 both YMD and HMS
7c360 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7c370 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 computeYMD_HMS(D
7c380 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 ateTime *p){. c
7c390 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 omputeYMD(p);.
7c3a0 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d computeHMS(p);.}
7c3b0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
7c3c0 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e e YMD and HMS an
7c3d0 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 d the TZ.*/.stat
7c3e0 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 ic void clearYMD
7c3f0 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 _HMS_TZ(DateTime
7c400 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 *p){. p->valid
7c410 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 YMD = 0;. p->va
7c420 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d lidHMS = 0;. p-
7c430 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a >validTZ = 0;.}.
7c440 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7c450 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f OMIT_LOCALTIME./
7c460 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 *.** Compute the
7c470 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e 20 difference (in
7c480 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a milliseconds).**
7c490 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 between localti
7c4a0 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e me and UTC (a.k.
7c4b0 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 a. GMT).** for t
7c4c0 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70 20 he time value p
7c4d0 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 55 54 where p is in UT
7c4e0 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c C..*/.static sql
7c4f0 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c ite3_int64 local
7c500 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65 54 timeOffset(DateT
7c510 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 ime *p){. DateT
7c520 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 ime x, y;. time
7c530 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a _t t;. x = *p;.
7c540 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 computeYMD_HMS
7c550 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c (&x);. if( x.Y<
7c560 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 1971 || x.Y>=203
7c570 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 8 ){. x.Y = 2
7c580 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 000;. x.M = 1
7c590 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 ;. x.D = 1;.
7c5a0 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 x.h = 0;.
7c5b0 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 x.m = 0;. x.s
7c5c0 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 = 0.0;. } else
7c5d0 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20 28 {. int s = (
7c5e0 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b int)(x.s + 0.5);
7c5f0 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 . x.s = s;.
7c600 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 }. x.tz = 0;.
7c610 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 x.validJD = 0;.
7c620 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a computeJD(&x);.
7c630 20 20 74 20 3d 20 28 74 69 6d 65 5f 74 29 28 78 t = (time_t)(x
7c640 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 .iJD/1000 - 2108
7c650 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 6676*(i64)10000)
7c660 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 4c 4f ;.#ifdef HAVE_LO
7c670 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 CALTIME_R. {.
7c680 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 struct tm sLoc
7c690 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d al;. localtim
7c6a0 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 e_r(&t, &sLocal)
7c6b0 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 ;. y.Y = sLoc
7c6c0 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 al.tm_year + 190
7c6d0 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 0;. y.M = sLo
7c6e0 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a cal.tm_mon + 1;.
7c6f0 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c y.D = sLocal
7c700 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e .tm_mday;. y.
7c710 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f h = sLocal.tm_ho
7c720 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c ur;. y.m = sL
7c730 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 ocal.tm_min;.
7c740 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.s = sLocal.tm
7c750 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 _sec;. }.#elif
7c760 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 defined(HAVE_LOC
7c770 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 48 41 56 ALTIME_S) && HAV
7c780 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 0a 20 20 E_LOCALTIME_S.
7c790 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 {. struct tm
7c7a0 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 sLocal;. loca
7c7b0 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c ltime_s(&sLocal,
7c7c0 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 &t);. y.Y =
7c7d0 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b sLocal.tm_year +
7c7e0 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 1900;. y.M =
7c7f0 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b sLocal.tm_mon +
7c800 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 1;. y.D = sL
7c810 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 ocal.tm_mday;.
7c820 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.h = sLocal.t
7c830 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 m_hour;. y.m
7c840 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b = sLocal.tm_min;
7c850 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 . y.s = sLoca
7c860 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 l.tm_sec;. }.#e
7c870 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 lse. {. stru
7c880 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 ct tm *pTm;.
7c890 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
7c8a0 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ter(sqlite3Mutex
7c8b0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
7c8c0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
7c8d0 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f ));. pTm = lo
7c8e0 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 caltime(&t);.
7c8f0 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 y.Y = pTm->tm_y
7c900 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 ear + 1900;.
7c910 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f y.M = pTm->tm_mo
7c920 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d n + 1;. y.D =
7c930 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 pTm->tm_mday;.
7c940 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d y.h = pTm->tm
7c950 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d _hour;. y.m =
7c960 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 pTm->tm_min;.
7c970 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.s = pTm->tm_
7c980 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 sec;. sqlite3
7c990 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
7c9a0 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
7c9b0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
7c9c0 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d IC_MASTER));. }
7c9d0 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 .#endif. y.vali
7c9e0 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 dYMD = 1;. y.va
7c9f0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e lidHMS = 1;. y.
7ca00 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 validJD = 0;. y
7ca10 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 .validTZ = 0;.
7ca20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 computeJD(&y);.
7ca30 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 return y.iJD -
7ca40 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 x.iJD;.}.#endif
7ca50 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c /* SQLITE_OMIT_L
7ca60 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a OCALTIME */../*.
7ca70 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 ** Process a mod
7ca80 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d ifier to a date-
7ca90 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65 time stamp. The
7caa0 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a modifiers are.*
7cab0 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a * as follows:.**
7cac0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 .** NNN days
7cad0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 .** NNN hour
7cae0 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e s.** NNN min
7caf0 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e utes.** NNN.
7cb00 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 NNNN seconds.**
7cb10 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a NNN months.*
7cb20 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a * NNN years.
7cb30 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
7cb40 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 month.** sta
7cb50 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 rt of year.**
7cb60 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a start of week.
7cb70 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
7cb80 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 day.** weekd
7cb90 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 ay N.** unix
7cba0 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 epoch.** loc
7cbb0 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 altime.** ut
7cbc0 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 c.**.** Return 0
7cbd0 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 on success and
7cbe0 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 1 if there is an
7cbf0 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e y kind of error.
7cc00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
7cc10 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e arseModifier(con
7cc20 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 st char *zMod, D
7cc30 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
7cc40 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 nt rc = 1;. int
7cc50 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a n;. double r;.
7cc60 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b char *z, zBuf[
7cc70 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 30];. z = zBuf;
7cc80 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 . for(n=0; n<Ar
7cc90 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 raySize(zBuf)-1
7cca0 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 && zMod[n]; n++)
7ccb0 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 {. z[n] = (ch
7ccc0 61 72 29 73 71 6c 69 74 65 33 55 70 70 65 72 54 ar)sqlite3UpperT
7ccd0 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b oLower[(u8)zMod[
7cce0 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 n]];. }. z[n]
7ccf0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 7a = 0;. switch( z
7cd00 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 [0] ){.#ifndef S
7cd10 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c QLITE_OMIT_LOCAL
7cd20 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 27 6c TIME. case 'l
7cd30 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 ': {. /*
7cd40 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 localtime.
7cd50 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 **. ** Ass
7cd60 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e uming the curren
7cd70 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 73 20 t time value is
7cd80 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 UTC (a.k.a. GMT)
7cd90 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a 20 20 , shift it to.
7cda0 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 ** show loca
7cdb0 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f l time.. */
7cdc0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d . if( strcm
7cdd0 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 p(z, "localtime"
7cde0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7cdf0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7ce00 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
7ce10 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 localtimeOffset(
7ce20 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 p);. clea
7ce30 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a rYMD_HMS_TZ(p);.
7ce40 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a rc = 0;.
7ce50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
7ce60 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
7ce70 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 3a 20 f. case 'u':
7ce80 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 {. /*.
7ce90 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 68 ** unixepoch
7cea0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
7ceb0 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 75 72 ** Treat the cur
7cec0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 2d rent value of p-
7ced0 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 6d 62 >iJD as the numb
7cee0 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 er of. ** s
7cef0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 econds since 197
7cf00 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 0. Convert to a
7cf10 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 real julian day
7cf20 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a number.. *
7cf30 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 /. if( strc
7cf40 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 mp(z, "unixepoch
7cf50 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 ")==0 && p->vali
7cf60 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 dJD ){. p
7cf70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 ->iJD = (p->iJD
7cf80 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30 20 2b + 43200)/86400 +
7cf90 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 21086676*(i64)1
7cfa0 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 0000000;.
7cfb0 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
7cfc0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
7cfd0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 = 0;. }.#if
7cfe0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
7cff0 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 _LOCALTIME.
7d000 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 else if( strcmp
7d010 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b (z, "utc")==0 ){
7d020 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
7d030 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20 20 20 _int64 c1;.
7d040 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
7d050 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f . c1 = lo
7d060 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 caltimeOffset(p)
7d070 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 ;. p->iJD
7d080 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 -= c1;.
7d090 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
7d0a0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
7d0b0 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c JD += c1 - local
7d0c0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 timeOffset(p);.
7d0d0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7d0e0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
7d0f0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
7d100 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b . case 'w': {
7d110 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 . /*.
7d120 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 4e 0a ** weekday N.
7d130 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
7d140 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65 20 * Move the date
7d150 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 to the same time
7d160 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f 63 63 on the next occ
7d170 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 urrence of.
7d180 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 77 68 ** weekday N wh
7d190 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 ere 0==Sunday, 1
7d1a0 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f ==Monday, and so
7d1b0 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65 0a forth. If the.
7d1c0 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 69 73 ** date is
7d1d0 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 65 20 already on the
7d1e0 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 65 6b appropriate week
7d1f0 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e day, this is a n
7d200 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 o-op.. */.
7d210 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 if( strncmp
7d220 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 2c 20 (z, "weekday ",
7d230 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61 6c 75 8)==0 && getValu
7d240 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 e(&z[8],&r)>0.
7d250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
7d260 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 & (n=(int)r)==r
7d270 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 && n>=0 && r<7 )
7d280 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
7d290 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 3_int64 Z;.
7d2a0 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d computeYMD_HM
7d2b0 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d S(p);. p-
7d2c0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 >validTZ = 0;.
7d2d0 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 p->validJD
7d2e0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f = 0;. co
7d2f0 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 mputeJD(p);.
7d300 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 Z = ((p->iJD
7d310 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f 38 36 + 129600000)/86
7d320 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 20 20 400000) % 7;.
7d330 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a if( Z>n ) Z
7d340 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 20 70 -= 7;. p
7d350 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 ->iJD += (n - Z)
7d360 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 *86400000;.
7d370 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
7d380 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 TZ(p);. r
7d390 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 0;. }.
7d3a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7d3b0 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 }. case 's':
7d3c0 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 {. /*.
7d3d0 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f 66 20 ** start of
7d3e0 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 TTTTT. **.
7d3f0 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 ** Move the
7d400 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 73 20 date backwards
7d410 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 to the beginning
7d420 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
7d430 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 day,. ** or
7d440 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a month or year..
7d450 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
7d460 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 f( strncmp(z, "s
7d470 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 tart of ", 9)!=0
7d480 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
7d490 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f z += 9;. co
7d4a0 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 mputeYMD(p);.
7d4b0 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d p->validHMS =
7d4c0 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 1;. p->h =
7d4d0 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 p->m = 0;.
7d4e0 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 p->s = 0.0;.
7d4f0 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 p->validTZ =
7d500 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 0;. p->vali
7d510 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 dJD = 0;. i
7d520 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e f( strcmp(z,"mon
7d530 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 th")==0 ){.
7d540 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 p->D = 1;.
7d550 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7d560 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 }else if( str
7d570 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 cmp(z,"year")==0
7d580 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 ){. comp
7d590 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 uteYMD(p);.
7d5a0 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 p->M = 1;.
7d5b0 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 p->D = 1;.
7d5c0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7d5d0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 }else if( s
7d5e0 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d trcmp(z,"day")==
7d5f0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
7d600 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
7d610 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
7d620 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 case '+':.
7d630 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 case '-':.
7d640 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 63 61 case '0':. ca
7d650 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 73 65 se '1':. case
7d660 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '2':. case '
7d670 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 34 27 3':. case '4'
7d680 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 3a 0a :. case '5':.
7d690 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a 20 20 case '6':.
7d6a0 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 20 20 case '7':.
7d6b0 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 63 61 case '8':. ca
7d6c0 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 se '9': {.
7d6d0 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b double rRounder;
7d6e0 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 56 61 . n = getVa
7d6f0 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 lue(z, &r);.
7d700 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 assert( n>=1 )
7d710 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d ;. if( z[n]
7d720 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 ==':' ){.
7d730 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 6f /* A modifier o
7d740 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 f the form (+|-)
7d750 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 HH:MM:SS.FFF add
7d760 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 73 29 s (or subtracts)
7d770 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
7d780 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 specified number
7d790 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 of hours, minut
7d7a0 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 es, seconds, and
7d7b0 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f fractional seco
7d7c0 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 nds. ** t
7d7d0 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 68 65 o the time. The
7d7e0 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 20 6f ".FFF" may be o
7d7f0 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 3a 53 mitted. The ":S
7d800 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a 20 20 S.FFF" may be.
7d810 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 ** omitted
7d820 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
7d830 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
7d840 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 *z2 = z;.
7d850 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 DateTime tx;.
7d860 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
7d870 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 20 20 t64 day;.
7d880 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 if( !sqlite3Isd
7d890 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b igit(*z2) ) z2++
7d8a0 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 ;. memset
7d8b0 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 (&tx, 0, sizeof(
7d8c0 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 tx));. if
7d8d0 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 ( parseHhMmSs(z2
7d8e0 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a , &tx) ) break;.
7d8f0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a computeJ
7d900 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 D(&tx);.
7d910 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 tx.iJD -= 432000
7d920 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 00;. day
7d930 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 = tx.iJD/8640000
7d940 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 0;. tx.iJ
7d950 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 D -= day*8640000
7d960 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 0;. if( z
7d970 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a [0]=='-' ) tx.iJ
7d980 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 D = -tx.iJD;.
7d990 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 computeJD(p
7d9a0 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 );. clear
7d9b0 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 YMD_HMS_TZ(p);.
7d9c0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d p->iJD +=
7d9d0 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 tx.iJD;.
7d9e0 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 rc = 0;.
7d9f0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
7da00 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 z += n;.
7da10 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
7da20 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 e3Isspace(*z) )
7da30 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 z++;. n = s
7da40 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
7da50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31 );. if( n>1
7da60 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 0 || n<3 ) break
7da70 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d ;. if( z[n-
7da80 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 1]=='s' ){ z[n-1
7da90 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 ] = 0; n--; }.
7daa0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7dab0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a ;. rc = 0;.
7dac0 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d rRounder =
7dad0 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 r<0 ? -0.5 : +0
7dae0 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d .5;. if( n=
7daf0 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =3 && strcmp(z,"
7db00 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 day")==0 ){.
7db10 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
7db20 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
7db30 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 86400000.0 + rRo
7db40 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 under);. }e
7db50 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 lse if( n==4 &&
7db60 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 strcmp(z,"hour")
7db70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
7db80 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 ->iJD += (sqlite
7db90 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 3_int64)(r*(8640
7dba0 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 0000.0/24.0) + r
7dbb0 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 Rounder);.
7dbc0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 }else if( n==6 &
7dbd0 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 & strcmp(z,"minu
7dbe0 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 te")==0 ){.
7dbf0 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 p->iJD += (sq
7dc00 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 lite3_int64)(r*(
7dc10 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 86400000.0/(24.0
7dc20 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 *60.0)) + rRound
7dc30 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 er);. }else
7dc40 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 if( n==6 && str
7dc50 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d cmp(z,"second")=
7dc60 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d =0 ){. p-
7dc70 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 >iJD += (sqlite3
7dc80 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 _int64)(r*(86400
7dc90 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 000.0/(24.0*60.0
7dca0 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 *60.0)) + rRound
7dcb0 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 er);. }else
7dcc0 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 if( n==5 && str
7dcd0 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d cmp(z,"month")==
7dce0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 0 ){. int
7dcf0 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 63 x, y;. c
7dd00 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 omputeYMD_HMS(p)
7dd10 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b ;. p->M +
7dd20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 = (int)r;.
7dd30 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 x = p->M>0 ? (
7dd40 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d p->M-1)/12 : (p-
7dd50 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 >M-12)/12;.
7dd60 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 p->Y += x;.
7dd70 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a p->M -= x*
7dd80 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 12;. p->v
7dd90 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
7dda0 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7ddb0 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20 28 69 ;. y = (i
7ddc0 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 69 66 nt)r;. if
7ddd0 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 ( y!=r ){.
7dde0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
7ddf0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 qlite3_int64)((r
7de00 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 - y)*30.0*86400
7de10 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 000.0 + rRounder
7de20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
7de30 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d }else if( n==
7de40 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 4 && strcmp(z,"y
7de50 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ear")==0 ){.
7de60 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 int y = (int
7de70 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 )r;. comp
7de80 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 uteYMD_HMS(p);.
7de90 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 p->Y += y
7dea0 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c ;. p->val
7deb0 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 idJD = 0;.
7dec0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7ded0 20 20 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 if( y!=r
7dee0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
7def0 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 >iJD += (sqlite3
7df00 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a _int64)((r - y)*
7df10 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 365.0*86400000.0
7df20 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 + rRounder);.
7df30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
7df40 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
7df50 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 1;. }.
7df60 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
7df70 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 72 65 TZ(p);. bre
7df80 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
7df90 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 fault: {. b
7dfa0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
7dfb0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
7dfc0 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 /*.** Process ti
7dfd0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 me function argu
7dfe0 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 ments. argv[0]
7dff0 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73 is a date-time s
7e000 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d tamp..** argv[1]
7e010 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 and following a
7e020 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 re modifiers. P
7e030 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e arse them all an
7e040 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 d write.** the r
7e050 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e esulting time in
7e060 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 to the DateTime
7e070 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 52 65 structure p. Re
7e080 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 turn 0.** on suc
7e090 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 cess and 1 if th
7e0a0 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f ere are any erro
7e0b0 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 rs..**.** If the
7e0c0 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61 re are zero para
7e0d0 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 6e 20 meters (if even
7e0e0 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 argv[0] is undef
7e0f0 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 ined).** then as
7e100 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 20 76 sume a default v
7e110 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f alue of "now" fo
7e120 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 r argv[0]..*/.st
7e130 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 65 28 atic int isDate(
7e140 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7e150 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 xt *context, .
7e160 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c int argc, . sql
7e170 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7e180 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a v, . DateTime *
7e190 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 p.){. int i;.
7e1a0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
7e1b0 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 har *z;. int eT
7e1c0 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c ype;. memset(p,
7e1d0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0, sizeof(*p));
7e1e0 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 . if( argc==0 )
7e1f0 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 6d {. setDateTim
7e200 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 eToCurrent(conte
7e210 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 xt, p);. }else
7e220 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 71 6c if( (eType = sql
7e230 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
7e240 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 argv[0]))==SQLIT
7e250 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 E_FLOAT.
7e260 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 54 || eT
7e270 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 ype==SQLITE_INTE
7e280 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a GER ){. p->iJ
7e290 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 D = (sqlite3_int
7e2a0 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 64)(sqlite3_valu
7e2b0 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d e_double(argv[0]
7e2c0 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 )*86400000.0 + 0
7e2d0 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 .5);. p->vali
7e2e0 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 dJD = 1;. }else
7e2f0 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 {. z = sqlite
7e300 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
7e310 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 v[0]);. if( !
7e320 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72 z || parseDateOr
7e330 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 Time(context, (c
7e340 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 har*)z, p) ){.
7e350 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
7e360 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d }. }. for(i=
7e370 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 1; i<argc; i++){
7e380 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 73 71 . if( (z = sq
7e390 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
7e3a0 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c (argv[i]))==0 ||
7e3b0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 28 parseModifier((
7e3c0 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 char*)z, p) ){.
7e3d0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
7e3e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
7e3f0 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 n 0;.}.../*.** T
7e400 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
7e410 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 tines implement
7e420 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 74 65 the various date
7e430 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 74 69 and time functi
7e440 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 ons.** of SQLite
7e450 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a ..*/../*.** j
7e460 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 53 54 ulianday( TIMEST
7e470 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
7e480 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
7e490 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 n the julian day
7e4a0 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64 number of the d
7e4b0 61 74 65 20 73 70 65 63 69 66 69 65 64 20 69 6e ate specified in
7e4c0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a the arguments.*
7e4d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75 /.static void ju
7e4e0 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 liandayFunc(. s
7e4f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
7e500 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
7e510 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
7e520 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
7e530 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 DateTime x;. i
7e540 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 f( isDate(contex
7e550 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 t, argc, argv, &
7e560 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d x)==0 ){. com
7e570 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 puteJD(&x);.
7e580 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
7e590 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 ouble(context, x
7e5a0 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29 .iJD/86400000.0)
7e5b0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
7e5c0 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 4d 45 datetime( TIME
7e5d0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
7e5e0 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
7e5f0 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 urn YYYY-MM-DD H
7e600 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 H:MM:SS.*/.stati
7e610 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d 65 46 c void datetimeF
7e620 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
7e630 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
7e640 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
7e650 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
7e660 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d rgv.){. DateTim
7e670 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 e x;. if( isDat
7e680 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c e(context, argc,
7e690 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b argv, &x)==0 ){
7e6a0 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 . char zBuf[1
7e6b0 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 00];. compute
7e6c0 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 YMD_HMS(&x);.
7e6d0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7e6e0 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 f(sizeof(zBuf),
7e6f0 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64 zBuf, "%04d-%02d
7e700 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a -%02d %02d:%02d:
7e710 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20 %02d",.
7e720 20 20 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c x.Y,
7e730 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 x.M, x.D, x.h,
7e740 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 x.m, (int)(x.s))
7e750 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
7e760 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
7e770 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c t, zBuf, -1, SQL
7e780 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
7e790 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 }.}../*.**
7e7a0 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47 time( TIMESTRING
7e7b0 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 , MOD, MOD, ...)
7e7c0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 .**.** Return HH
7e7d0 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 :MM:SS.*/.static
7e7e0 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a void timeFunc(.
7e7f0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
7e800 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
7e810 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
7e820 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
7e830 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a {. DateTime x;.
7e840 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e if( isDate(con
7e850 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 text, argc, argv
7e860 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 , &x)==0 ){.
7e870 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a char zBuf[100];.
7e880 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 computeHMS(&
7e890 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f x);. sqlite3_
7e8a0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
7e8b0 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 zBuf), zBuf, "%0
7e8c0 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2d:%02d:%02d", x
7e8d0 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e .h, x.m, (int)x.
7e8e0 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f s);. sqlite3_
7e8f0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
7e900 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 ext, zBuf, -1, S
7e910 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
7e920 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
7e930 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 52 49 date( TIMESTRI
7e940 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e NG, MOD, MOD, ..
7e950 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .).**.** Return
7e960 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 YYYY-MM-DD.*/.st
7e970 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 46 75 atic void dateFu
7e980 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7e990 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7e9a0 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
7e9b0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
7e9c0 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 gv.){. DateTime
7e9d0 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 x;. if( isDate
7e9e0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 (context, argc,
7e9f0 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a argv, &x)==0 ){.
7ea00 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 char zBuf[10
7ea10 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 0];. computeY
7ea20 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 MD(&x);. sqli
7ea30 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
7ea40 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c eof(zBuf), zBuf,
7ea50 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64 "%04d-%02d-%02d
7ea60 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 ", x.Y, x.M, x.D
7ea70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 );. sqlite3_r
7ea80 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
7ea90 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 xt, zBuf, -1, SQ
7eaa0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
7eab0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 . }.}../*.**
7eac0 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 strftime( FORMA
7ead0 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d T, TIMESTRING, M
7eae0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a OD, MOD, ...).**
7eaf0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72 .** Return a str
7eb00 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 62 79 ing described by
7eb10 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 FORMAT. Conver
7eb20 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 sions as follows
7eb30 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 :.**.** %d da
7eb40 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 y of month.**
7eb50 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 %f ** fractiona
7eb60 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 l seconds SS.SS
7eb70 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20 S.** %H hour
7eb80 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 00-24.** %j d
7eb90 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33 ay of year 000-3
7eba0 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 66.** %J ** J
7ebb0 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
7ebc0 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 .** %m month
7ebd0 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 01-12.** %M m
7ebe0 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 inute 00-59.**
7ebf0 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e %s seconds sin
7ec00 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a ce 1970-01-01.**
7ec10 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30 %S seconds 0
7ec20 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 0-59.** %w da
7ec30 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73 y of week 0-6 s
7ec40 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 unday==0.** %W
7ec50 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30 week of year 0
7ec60 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 0-53.** %Y ye
7ec70 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 ar 0000-9999.**
7ec80 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 %% %.*/.stati
7ec90 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46 c void strftimeF
7eca0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
7ecb0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
7ecc0 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
7ecd0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
7ece0 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d rgv.){. DateTim
7ecf0 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 e x;. u64 n;.
7ed00 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 size_t i,j;. ch
7ed10 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 ar *z;. sqlite3
7ed20 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 *db;. const ch
7ed30 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 ar *zFmt = (cons
7ed40 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f t char*)sqlite3_
7ed50 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
7ed60 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 0]);. char zBuf
7ed70 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d [100];. if( zFm
7ed80 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65 28 63 t==0 || isDate(c
7ed90 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 ontext, argc-1,
7eda0 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20 72 65 argv+1, &x) ) re
7edb0 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c turn;. db = sql
7edc0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
7edd0 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b handle(context);
7ede0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b . for(i=0, n=1;
7edf0 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e zFmt[i]; i++, n
7ee00 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d ++){. if( zFm
7ee10 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 t[i]=='%' ){.
7ee20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b switch( zFmt[
7ee30 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 i+1] ){.
7ee40 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20 20 20 case 'd':.
7ee50 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20 20 20 case 'H':.
7ee60 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 case 'm':.
7ee70 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0a case 'M':.
7ee80 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27 case 'S'
7ee90 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
7eea0 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 2b W':. n+
7eeb0 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 +;. /*
7eec0 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 fall thru */.
7eed0 20 20 20 20 20 63 61 73 65 20 27 77 27 3a 0a 20 case 'w':.
7eee0 20 20 20 20 20 20 20 63 61 73 65 20 27 25 27 3a case '%':
7eef0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
7ef00 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7ef10 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 f':. n
7ef20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 += 8;.
7ef30 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7ef40 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 ase 'j':.
7ef50 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 n += 3;.
7ef60 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7ef70 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a 20 20 case 'Y':.
7ef80 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a n += 8;.
7ef90 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7efa0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 73 . case 's
7efb0 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 ':. case
7efc0 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 'J':. n
7efd0 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20 20 20 += 50;.
7efe0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7eff0 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 default:.
7f000 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 return; /*
7f010 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 ERROR. return a
7f020 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d NULL */. }
7f030 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 . i++;.
7f040 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 73 65 }. }. testcase
7f050 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 ( n==sizeof(zBuf
7f060 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 )-1 );. testcas
7f070 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 e( n==sizeof(zBu
7f080 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 f) );. testcase
7f090 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c ( n==(u64)db->aL
7f0a0 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
7f0b0 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 T_LENGTH]+1 );.
7f0c0 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 testcase( n==(u
7f0d0 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 64)db->aLimit[SQ
7f0e0 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
7f0f0 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 H] );. if( n<si
7f100 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 zeof(zBuf) ){.
7f110 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 z = zBuf;. }e
7f120 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34 29 64 lse if( n>(u64)d
7f130 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
7f140 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
7f150 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 {. sqlite3_re
7f160 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 sult_error_toobi
7f170 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 g(context);.
7f180 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b return;. }else{
7f190 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 . z = sqlite3
7f1a0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
7f1b0 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 (int)n);. if(
7f1c0 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 z==0 ){. s
7f1d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
7f1e0 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 ror_nomem(contex
7f1f0 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e t);. return
7f200 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f ;. }. }. co
7f210 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 mputeJD(&x);. c
7f220 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 omputeYMD_HMS(&x
7f230 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 );. for(i=j=0;
7f240 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 zFmt[i]; i++){.
7f250 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d if( zFmt[i]!=
7f260 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a '%' ){. z[j
7f270 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 ++] = zFmt[i];.
7f280 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
7f290 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69 74 63 i++;. switc
7f2a0 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 h( zFmt[i] ){.
7f2b0 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 20 case 'd':
7f2c0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7f2d0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
7f2e0 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.D); j+=2; br
7f2f0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
7f300 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 e 'f': {.
7f310 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e double s = x.
7f320 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 s;. if(
7f330 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 3d 20 s>59.999 ) s =
7f340 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20 20 20 59.999;.
7f350 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7f360 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 tf(7, &z[j],"%06
7f370 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 .3f", s);.
7f380 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 j += sqlite3
7f390 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b Strlen30(&z[j]);
7f3a0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
7f3b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
7f3c0 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 20 73 case 'H': s
7f3d0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7f3e0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 3, &z[j],"%02d",
7f3f0 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 x.h); j+=2; brea
7f400 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
7f410 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 'W': /* Fall thr
7f420 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 u */. cas
7f430 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 e 'j': {.
7f440 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 int nDay;
7f450 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
7f460 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 er of days since
7f470 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 61 72 1st day of year
7f480 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 44 61 */. Da
7f490 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 teTime y = x;.
7f4a0 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 64 4a y.validJ
7f4b0 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 D = 0;.
7f4c0 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 y.M = 1;.
7f4d0 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 y.D = 1;.
7f4e0 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
7f4f0 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (&y);.
7f500 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28 78 2e nDay = (int)((x.
7f510 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 iJD-y.iJD+432000
7f520 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 00)/86400000);.
7f530 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 46 6d if( zFm
7f540 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 t[i]=='W' ){.
7f550 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 64 3b int wd;
7f560 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 /* 0=Monday,
7f570 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 1=Tuesday, ... 6
7f580 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 =Sunday */.
7f590 20 20 20 20 20 20 20 77 64 20 3d 20 28 69 6e 74 wd = (int
7f5a0 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 )(((x.iJD+432000
7f5b0 30 30 29 2f 38 36 34 30 30 30 30 30 29 25 37 29 00)/86400000)%7)
7f5c0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 ;. sq
7f5d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7f5e0 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 , &z[j],"%02d",(
7f5f0 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 nDay+7-wd)/7);.
7f600 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 j +=
7f610 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 2;. }el
7f620 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
7f630 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7f640 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 (4, &z[j],"%03d"
7f650 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 ,nDay+1);.
7f660 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 j += 3;.
7f670 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
7f680 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
7f690 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 }. cas
7f6a0 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 e 'J': {.
7f6b0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
7f6c0 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 ntf(20, &z[j],"%
7f6d0 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 .16g",x.iJD/8640
7f6e0 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 0000.0);.
7f6f0 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 j+=sqlite3Str
7f700 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 len30(&z[j]);.
7f710 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7f720 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
7f730 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 case 'm': sqli
7f740 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 te3_snprintf(3,
7f750 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d &z[j],"%02d",x.M
7f760 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a ); j+=2; break;.
7f770 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 case 'M'
7f780 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
7f790 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 ntf(3, &z[j],"%0
7f7a0 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 2d",x.m); j+=2;
7f7b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7f7c0 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 ase 's': {.
7f7d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
7f7e0 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 rintf(30,&z[j],"
7f7f0 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20 20 20 %lld",.
7f800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f810 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 (i64)(x.iJD/10
7f820 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 28 69 00 - 21086676*(i
7f830 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20 20 20 64)10000));.
7f840 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 j += sqlit
7f850 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d e3Strlen30(&z[j]
7f860 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
7f870 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
7f880 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 20 case 'S':
7f890 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7f8a0 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 f(3,&z[j],"%02d"
7f8b0 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 ,(int)x.s); j+=2
7f8c0 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
7f8d0 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 case 'w': {.
7f8e0 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 z[j++] =
7f8f0 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 (char)(((x.iJD+1
7f900 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 30 29600000)/864000
7f910 30 30 29 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 00) % 7) + '0';.
7f920 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7f930 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7f940 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 case 'Y': {.
7f950 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
7f960 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a _snprintf(5,&z[j
7f970 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a ],"%04d",x.Y); j
7f980 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 +=sqlite3Strlen3
7f990 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 0(&z[j]);.
7f9a0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
7f9b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 65 66 }. def
7f9c0 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d ault: z[j++] =
7f9d0 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 '%'; break;.
7f9e0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
7f9f0 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c z[j] = 0;. sql
7fa00 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
7fa10 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c (context, z, -1,
7fa20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
7fa30 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f z==zBuf ?
7fa40 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
7fa50 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d T : SQLITE_DYNAM
7fa60 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 IC);.}../*.** cu
7fa70 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a rrent_time().**.
7fa80 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
7fa90 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d returns the sam
7faa0 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d 65 28 e value as time(
7fab0 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 'now')..*/.stati
7fac0 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 c void ctimeFunc
7fad0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7fae0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7faf0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 int NotUsed,. s
7fb00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e qlite3_value **N
7fb10 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 otUsed2.){. UNU
7fb20 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
7fb30 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
7fb40 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 63 6f );. timeFunc(co
7fb50 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a ntext, 0, 0);.}.
7fb60 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 ./*.** current_d
7fb70 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ate().**.** This
7fb80 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
7fb90 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 s the same value
7fba0 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e as date('now').
7fbb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7fbc0 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c cdateFunc(. sql
7fbd0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7fbe0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
7fbf0 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
7fc00 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
7fc10 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
7fc20 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
7fc30 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61 NotUsed2);. da
7fc40 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 teFunc(context,
7fc50 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0, 0);.}../*.**
7fc60 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d current_timestam
7fc70 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 p().**.** This f
7fc80 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
7fc90 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 the same value a
7fca0 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 s datetime('now'
7fcb0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
7fcc0 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 d ctimestampFunc
7fcd0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7fce0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7fcf0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 int NotUsed,. s
7fd00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e qlite3_value **N
7fd10 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 otUsed2.){. UNU
7fd20 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
7fd30 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
7fd40 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 75 6e );. datetimeFun
7fd50 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 c(context, 0, 0)
7fd60 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 ;.}.#endif /* !d
7fd70 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
7fd80 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 IT_DATETIME_FUNC
7fd90 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 S) */..#ifdef SQ
7fda0 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 LITE_OMIT_DATETI
7fdb0 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 ME_FUNCS./*.** I
7fdc0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 f the library is
7fdd0 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 compiled to omi
7fde0 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 t the full-scale
7fdf0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a date and time.*
7fe00 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 * handling (to g
7fe10 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e et a smaller bin
7fe20 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 ary), the follow
7fe30 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 ing minimal vers
7fe40 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 ion.** of the fu
7fe50 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f nctions current_
7fe60 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f time(), current_
7fe70 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 72 65 date() and curre
7fe80 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a nt_timestamp().*
7fe90 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 * are included i
7fea0 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 73 20 nstead. This is
7feb0 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d to support colum
7fec0 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 n declarations t
7fed0 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 hat.** include "
7fee0 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f DEFAULT CURRENT_
7fef0 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a TIME" etc..**.**
7ff00 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 This function u
7ff10 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72 ses the C-librar
7ff20 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 y functions time
7ff30 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 (), gmtime().**
7ff40 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20 and strftime().
7ff50 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e The format strin
7ff60 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72 g to pass to str
7ff70 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c ftime() is suppl
7ff80 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 ied.** as the us
7ff90 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20 er-data for the
7ffa0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
7ffb0 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74 tic void current
7ffc0 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 TimeFunc(. sqli
7ffd0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7ffe0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
7fff0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
80000 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 e **argv.){. ti
80010 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a me_t t;. char *
80020 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20 zFormat = (char
80030 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 *)sqlite3_user_d
80040 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 ata(context);.
80050 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 sqlite3 *db;. d
80060 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 ouble rT;. char
80070 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e zBuf[20];.. UN
80080 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
80090 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 rgc);. UNUSED_P
800a0 41 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0a ARAMETER(argv);.
800b0 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f . db = sqlite3_
800c0 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
800d0 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 e(context);. sq
800e0 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 lite3OsCurrentTi
800f0 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 me(db->pVfs, &rT
80100 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
80110 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
80120 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 POINT. t = 8640
80130 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30 35 38 0.0*(rT - 244058
80140 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 7.5) + 0.5;.#els
80150 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 e. /* without f
80160 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 loating point su
80170 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 pport, rT will h
80180 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 ave. ** already
80190 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e 61 6c lost fractional
801a0 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a day precision..
801b0 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 */. t = 86400
801c0 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35 38 37 * (rT - 2440587
801d0 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 ) - 43200;.#endi
801e0 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 47 4d f.#ifdef HAVE_GM
801f0 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 TIME_R. {. s
80200 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 truct tm sNow;.
80210 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 gmtime_r(&t,
80220 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 &sNow);. strf
80230 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a time(zBuf, 20, z
80240 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a Format, &sNow);.
80250 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 }.#else. {.
80260 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d struct tm *pTm
80270 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
80280 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
80290 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
802a0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
802b0 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 MASTER));. pT
802c0 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a m = gmtime(&t);.
802d0 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 strftime(zBu
802e0 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 f, 20, zFormat,
802f0 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pTm);. sqlite
80300 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 3_mutex_leave(sq
80310 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
80320 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
80330 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 TIC_MASTER));.
80340 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 }.#endif.. sqli
80350 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
80360 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d context, zBuf, -
80370 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 1, SQLITE_TRANSI
80380 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ENT);.}.#endif..
80390 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
803a0 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 ion registered a
803b0 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 ll of the above
803c0 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 C functions as S
803d0 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e QL.** functions.
803e0 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 This should be
803f0 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e the only routin
80400 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 e in this file w
80410 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 ith.** external
80420 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 linkage..*/.SQLI
80430 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
80440 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 sqlite3RegisterD
80450 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 ateTimeFunctions
80460 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
80470 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 SQLITE_WSD Func
80480 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 75 6e Def aDateTimeFun
80490 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 cs[] = {.#ifndef
804a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 SQLITE_OMIT_DAT
804b0 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 ETIME_FUNCS.
804c0 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 FUNCTION(juliand
804d0 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c 20 30 ay, -1, 0
804e0 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 , 0, juliandayFu
804f0 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 nc ),. FUNCTI
80500 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 20 20 ON(date,
80510 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 -1, 0, 0, d
80520 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a ateFunc ),.
80530 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d FUNCTION(tim
80540 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2d e, -
80550 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 1, 0, 0, timeFun
80560 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 c ),. FU
80570 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c NCTION(datetime,
80580 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 -1, 0,
80590 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e 63 20 0, datetimeFunc
805a0 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
805b0 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 20 20 (strftime,
805c0 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 -1, 0, 0, str
805d0 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 ftimeFunc ),.
805e0 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 FUNCTION(curre
805f0 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c nt_time, 0,
80600 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 0, 0, ctimeFunc
80610 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
80620 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d TION(current_tim
80630 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c estamp, 0, 0, 0,
80640 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 ctimestampFunc)
80650 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 ,. FUNCTION(c
80660 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 urrent_date,
80670 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 0, 0, 0, cdate
80680 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 Func ),.#els
80690 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 e. STR_FUNCTI
806a0 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c ON(current_time,
806b0 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 0, "%H:%M:
806c0 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c %S", 0,
806d0 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 currentTimeFunc
806e0 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 ),. STR_FUNCT
806f0 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 ION(current_time
80700 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d stamp, 0, "%Y-%m
80710 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 20 30 -%d", 0
80720 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e , currentTimeFun
80730 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 c),. STR_FUNC
80740 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 TION(current_dat
80750 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 2d 25 e, 0, "%Y-%
80760 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 m-%d %H:%M:%S",
80770 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 0, currentTimeFu
80780 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b nc),.#endif. };
80790 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 . int i;. Func
807a0 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d DefHash *pHash =
807b0 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 &GLOBAL(FuncDef
807c0 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f Hash, sqlite3Glo
807d0 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 balFunctions);.
807e0 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 FuncDef *aFunc
807f0 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f = (FuncDef*)&GLO
80800 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 44 61 BAL(FuncDef, aDa
80810 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 teTimeFuncs);..
80820 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 for(i=0; i<Arra
80830 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d 65 46 ySize(aDateTimeF
80840 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 uncs); i++){.
80850 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 sqlite3FuncDefI
80860 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61 46 nsert(pHash, &aF
80870 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a unc[i]);. }.}..
80880 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
80890 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a End of date.c **
808a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
808b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
808c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
808d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
808e0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 Begin file os.c
808f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
80920 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d /*.** 2005 Novem
80930 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 29.**.** The
80940 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
80950 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
80960 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
80970 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
80980 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
80990 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
809a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
809b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
809c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
809d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
809e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
809f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
80a00 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
80a10 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
80a20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
80a30 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
80a40 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
80aa0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
80ab0 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 ins OS interface
80ac0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
80ad0 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 mmon to all.** a
80ae0 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a 2f rchitectures..*/
80af0 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
80b00 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66 20 _OS_C_ 1.#undef
80b10 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a 2f _SQLITE_OS_C_../
80b20 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 *.** The default
80b30 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 33 5f SQLite sqlite3_
80b40 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 vfs implementati
80b50 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 ons do not alloc
80b60 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28 61 ate.** memory (a
80b70 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69 78 ctually, os_unix
80b80 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 .c allocates a s
80b90 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d mall amount of m
80ba0 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 emory.** from wi
80bb0 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c 20 thin OsOpen()),
80bc0 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 2d 70 but some third-p
80bd0 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 arty implementat
80be0 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f 20 ions may..** So
80bf0 77 65 20 74 65 73 74 20 74 68 65 20 65 66 66 65 we test the effe
80c00 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63 28 cts of a malloc(
80c10 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 74 68 ) failing and th
80c20 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 28 29 e sqlite3OsXXX()
80c30 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 .** function ret
80c40 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49 4f urning SQLITE_IO
80c50 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67 20 ERR_NOMEM using
80c60 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 the DO_OS_MALLOC
80c70 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a 0a _TEST macro..**.
80c80 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
80c90 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 functions are i
80ca0 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72 20 nstrumented for
80cb0 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 malloc() failure
80cc0 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a 2a .** testing:.**
80cd0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f .** sqlite3O
80ce0 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 73 sOpen().** s
80cf0 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 0a 2a qlite3OsRead().*
80d00 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 57 * sqlite3OsW
80d10 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 rite().** sq
80d20 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a 2a 2a lite3OsSync().**
80d30 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4c 6f sqlite3OsLo
80d40 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 20 64 ck().**.*/.#if d
80d50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
80d60 53 54 29 20 26 26 20 28 53 51 4c 49 54 45 5f 4f ST) && (SQLITE_O
80d70 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 23 64 65 66 S_WIN==0). #def
80d80 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 ine DO_OS_MALLOC
80d90 5f 54 45 53 54 28 78 29 20 69 66 20 28 21 78 20 _TEST(x) if (!x
80da0 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d 65 6d || !sqlite3IsMem
80db0 4a 6f 75 72 6e 61 6c 28 78 29 29 20 7b 20 20 20 Journal(x)) {
80dc0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 \. void *pT
80dd0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 stAlloc = sqlite
80de0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20 3Malloc(10);
80df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80e00 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 \. i
80e10 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 f (!pTstAlloc) r
80e20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
80e30 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20 RR_NOMEM;
80e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80e50 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 \. sqlite3_fr
80e60 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b 20 20 ee(pTstAlloc);
80e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80e90 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c \. }.#el
80ea0 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f se. #define DO_
80eb0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 OS_MALLOC_TEST(x
80ec0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
80ed0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
80ee0 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 utines are conve
80ef0 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 nience wrappers
80f00 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a around methods.*
80f10 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 * of the sqlite3
80f20 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 _file object. T
80f30 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 his is mostly ju
80f40 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 75 67 st syntactic sug
80f50 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 ar. All.** of th
80f60 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 is would be comp
80f70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 letely automatic
80f80 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 65 20 if SQLite were
80f90 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 coded using.** C
80fa0 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c ++ instead of pl
80fb0 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 ain old C..*/.SQ
80fc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
80fd0 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
80fe0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 sqlite3_file *pI
80ff0 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 d){. int rc = S
81000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
81010 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b pId->pMethods ){
81020 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 . rc = pId->p
81030 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 Methods->xClose(
81040 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 pId);. pId->p
81050 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d Methods = 0;. }
81060 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
81070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
81080 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 nt sqlite3OsRead
81090 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
810a0 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 d, void *pBuf, i
810b0 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 nt amt, i64 offs
810c0 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c et){. DO_OS_MAL
810d0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 LOC_TEST(id);.
810e0 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
810f0 6f 64 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 ods->xRead(id, p
81100 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 Buf, amt, offset
81110 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
81120 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
81130 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 sWrite(sqlite3_f
81140 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 ile *id, const v
81150 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 oid *pBuf, int a
81160 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b mt, i64 offset){
81170 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
81180 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
81190 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
811a0 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42 75 66 >xWrite(id, pBuf
811b0 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a , amt, offset);.
811c0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
811d0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 int sqlite3OsTr
811e0 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 uncate(sqlite3_f
811f0 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a ile *id, i64 siz
81200 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d e){. return id-
81210 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e >pMethods->xTrun
81220 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a cate(id, size);.
81230 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
81240 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79 int sqlite3OsSy
81250 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 nc(sqlite3_file
81260 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b *id, int flags){
81270 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
81280 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
81290 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
812a0 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73 >xSync(id, flags
812b0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
812c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
812d0 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 sFileSize(sqlite
812e0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 3_file *id, i64
812f0 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 *pSize){. DO_OS
81300 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 _MALLOC_TEST(id)
81310 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
81320 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 Methods->xFileSi
81330 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d ze(id, pSize);.}
81340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81350 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 int sqlite3OsLoc
81360 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
81370 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 id, int lockType
81380 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
81390 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
813a0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
813b0 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 s->xLock(id, loc
813c0 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 kType);.}.SQLITE
813d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
813e0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c ite3OsUnlock(sql
813f0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
81400 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 nt lockType){.
81410 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
81420 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c ods->xUnlock(id,
81430 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 lockType);.}.SQ
81440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
81450 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 sqlite3OsCheckR
81460 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
81470 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
81480 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 t *pResOut){. D
81490 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
814a0 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
814b0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 d->pMethods->xCh
814c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
814d0 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a id, pResOut);.}.
814e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
814f0 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 nt sqlite3OsFile
81500 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f Control(sqlite3_
81510 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 file *id, int op
81520 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 , void *pArg){.
81530 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
81540 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e 74 72 hods->xFileContr
81550 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 67 29 ol(id, op, pArg)
81560 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
81570 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
81580 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 SectorSize(sqlit
81590 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 e3_file *id){.
815a0 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69 7a int (*xSectorSiz
815b0 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a e)(sqlite3_file*
815c0 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 ) = id->pMethods
815d0 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20 ->xSectorSize;.
815e0 20 72 65 74 75 72 6e 20 28 78 53 65 63 74 6f 72 return (xSector
815f0 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 53 69 Size ? xSectorSi
81600 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 45 5f ze(id) : SQLITE_
81610 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 DEFAULT_SECTOR_S
81620 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 IZE);.}.SQLITE_P
81630 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
81640 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
81650 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
81660 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 3_file *id){. r
81670 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
81680 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61 72 61 ds->xDeviceChara
81690 63 74 65 72 69 73 74 69 63 73 28 69 64 29 3b 0a cteristics(id);.
816a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 }../*.** The nex
816b0 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 t group of routi
816c0 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 nes are convenie
816d0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f nce wrappers aro
816e0 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53 20 6d und the.** VFS m
816f0 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ethods..*/.SQLIT
81700 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81710 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20 20 73 lite3OsOpen(. s
81720 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
81730 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
81740 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74 *zPath, . sqlit
81750 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
81760 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 . int flags, .
81770 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75 74 0a int *pFlagsOut.
81780 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 ){. int rc;. D
81790 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
817a0 28 30 29 3b 0a 20 20 2f 2a 20 30 78 37 66 31 66 (0);. /* 0x7f1f
817b0 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 20 53 51 is a mask of SQ
817c0 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 LITE_OPEN_ flags
817d0 20 74 68 61 74 20 61 72 65 20 76 61 6c 69 64 20 that are valid
817e0 74 6f 20 62 65 20 70 61 73 73 65 64 0a 20 20 2a to be passed. *
817f0 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 65 20 * down into the
81800 56 46 53 20 6c 61 79 65 72 2e 20 20 53 6f 6d 65 VFS layer. Some
81810 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c SQLITE_OPEN_ fl
81820 61 67 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ags (for example
81830 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 ,. ** SQLITE_OP
81840 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 6f 72 20 EN_FULLMUTEX or
81850 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
81860 45 44 43 41 43 48 45 29 20 61 72 65 20 62 6c 6f EDCACHE) are blo
81870 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a cked before. **
81880 20 72 65 61 63 68 69 6e 67 20 74 68 65 20 56 46 reaching the VF
81890 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70 56 66 S. */. rc = pVf
818a0 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a s->xOpen(pVfs, z
818b0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 Path, pFile, fla
818c0 67 73 20 26 20 30 78 37 66 31 66 2c 20 70 46 6c gs & 0x7f1f, pFl
818d0 61 67 73 4f 75 74 29 3b 0a 20 20 61 73 73 65 72 agsOut);. asser
818e0 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
818f0 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 || pFile->pMeth
81900 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 ods==0 );. retu
81910 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
81920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
81930 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 te3OsDelete(sqli
81940 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63 te3_vfs *pVfs, c
81950 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
81960 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a , int dirSync){.
81970 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
81980 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 50 61 Delete(pVfs, zPa
81990 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a 7d 0a th, dirSync);.}.
819a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
819b0 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 nt sqlite3OsAcce
819c0 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 ss(. sqlite3_vf
819d0 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 s *pVfs, . cons
819e0 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a t char *zPath, .
819f0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 int flags, .
81a00 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a int *pResOut.){.
81a10 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
81a20 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75 72 6e EST(0);. return
81a30 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70 pVfs->xAccess(p
81a40 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 Vfs, zPath, flag
81a50 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 s, pResOut);.}.S
81a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
81a70 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 t sqlite3OsFullP
81a80 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 athname(. sqlit
81a90 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 e3_vfs *pVfs, .
81aa0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
81ab0 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68 th, . int nPath
81ac0 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50 Out, . char *zP
81ad0 61 74 68 4f 75 74 0a 29 7b 0a 20 20 7a 50 61 74 athOut.){. zPat
81ae0 68 4f 75 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 hOut[0] = 0;. r
81af0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 6c eturn pVfs->xFul
81b00 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 lPathname(pVfs,
81b10 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 2c zPath, nPathOut,
81b20 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 zPathOut);.}.#i
81b30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
81b40 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
81b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81b60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 void *sqlite3OsD
81b70 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
81b80 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 s *pVfs, const c
81b90 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20 72 har *zPath){. r
81ba0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c 4f eturn pVfs->xDlO
81bb0 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 29 pen(pVfs, zPath)
81bc0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
81bd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
81be0 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 sDlError(sqlite3
81bf0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
81c00 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 nByte, char *zBu
81c10 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e 78 fOut){. pVfs->x
81c20 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 42 DlError(pVfs, nB
81c30 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d yte, zBufOut);.}
81c40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81c50 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 void (*sqlite3Os
81c60 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
81c70 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
81c80 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 Hdle, const char
81c90 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a *zSym))(void){.
81ca0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
81cb0 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 64 6c DlSym(pVfs, pHdl
81cc0 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c 49 e, zSym);.}.SQLI
81cd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
81ce0 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 sqlite3OsDlClose
81cf0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
81d00 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
81d10 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 e){. pVfs->xDlC
81d20 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64 lose(pVfs, pHand
81d30 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a le);.}.#endif /*
81d40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
81d50 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 D_EXTENSION */.S
81d60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
81d70 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f t sqlite3OsRando
81d80 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 mness(sqlite3_vf
81d90 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 s *pVfs, int nBy
81da0 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 te, char *zBufOu
81db0 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 t){. return pVf
81dc0 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70 s->xRandomness(p
81dd0 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 Vfs, nByte, zBuf
81de0 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 Out);.}.SQLITE_P
81df0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
81e00 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 e3OsSleep(sqlite
81e10 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
81e20 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 nMicro){. retu
81e30 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 rn pVfs->xSleep(
81e40 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d pVfs, nMicro);.}
81e50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81e60 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 int sqlite3OsCur
81e70 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
81e80 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 _vfs *pVfs, doub
81e90 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 le *pTimeOut){.
81ea0 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 return pVfs->xC
81eb0 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c urrentTime(pVfs,
81ec0 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53 pTimeOut);.}..S
81ed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
81ee0 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d t sqlite3OsOpenM
81ef0 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 alloc(. sqlite3
81f00 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 _vfs *pVfs, . c
81f10 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
81f20 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c , . sqlite3_fil
81f30 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 e **ppFile, . i
81f40 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 nt flags,. int
81f50 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 *pOutFlags.){.
81f60 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
81f70 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 NOMEM;. sqlite3
81f80 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 _file *pFile;.
81f90 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 pFile = (sqlite3
81fa0 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d _file *)sqlite3M
81fb0 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 alloc(pVfs->szOs
81fc0 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 File);. if( pFi
81fd0 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 le ){. rc = s
81fe0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
81ff0 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c s, zFile, pFile,
82000 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 flags, pOutFlag
82010 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d s);. if( rc!=
82020 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
82030 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
82040 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 pFile);. }els
82050 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65 e{. *ppFile
82060 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a = pFile;. }.
82070 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
82080 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
82090 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
820a0 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33 loseFree(sqlite3
820b0 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 _file *pFile){.
820c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
820d0 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 _OK;. assert( p
820e0 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 File );. rc = s
820f0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 qlite3OsClose(pF
82100 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ile);. sqlite3_
82110 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 free(pFile);. r
82120 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
82130 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
82140 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 is a wrapper ar
82150 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70 65 63 ound the OS spec
82160 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ific implementat
82170 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 ion of.** sqlite
82180 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54 68 65 3_os_init(). The
82190 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 purpose of the
821a0 77 72 61 70 70 65 72 20 69 73 20 74 6f 20 70 72 wrapper is to pr
821b0 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61 62 69 ovide the.** abi
821c0 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 lity to simulate
821d0 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
821e0 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 68 e, so that the h
821f0 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a 2a andling of an.**
82200 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69 74 65 error in sqlite
82210 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79 20 74 3_os_init() by t
82220 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 73 20 he upper layers
82230 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e 0a 2a can be tested..*
82240 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
82250 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e int sqlite3OsIn
82260 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f 69 64 it(void){. void
82270 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 *p = sqlite3_ma
82280 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 28 20 lloc(10);. if(
82290 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 p==0 ) return SQ
822a0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 LITE_NOMEM;. sq
822b0 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
822c0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f return sqlite3_
822d0 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f 2a os_init();.}../*
822e0 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 .** The list of
822f0 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 56 all registered V
82300 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f FS implementatio
82310 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 ns..*/.static sq
82320 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 lite3_vfs * SQLI
82330 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 3d TE_WSD vfsList =
82340 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 0;.#define vfsL
82350 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 ist GLOBAL(sqlit
82360 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 e3_vfs *, vfsLis
82370 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 t)../*.** Locate
82380 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 a VFS by name.
82390 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 If no name is g
823a0 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 iven, simply ret
823b0 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 urn the.** first
823c0 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 VFS on the list
823d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
823e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c sqlite3_vfs *sql
823f0 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f ite3_vfs_find(co
82400 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 7b nst char *zVfs){
82410 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
82420 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 pVfs = 0;.#if SQ
82430 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
82440 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
82450 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 *mutex;.#endif.#
82460 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
82470 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e IT_AUTOINIT. in
82480 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 t rc = sqlite3_i
82490 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 nitialize();. i
824a0 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 30 f( rc ) return 0
824b0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c ;.#endif.#if SQL
824c0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 ITE_THREADSAFE.
824d0 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 mutex = sqlite3
824e0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
824f0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
82500 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 ASTER);.#endif.
82510 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
82520 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 nter(mutex);. f
82530 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69 73 or(pVfs = vfsLis
82540 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70 56 t; pVfs; pVfs=pV
82550 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 fs->pNext){.
82560 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 if( zVfs==0 ) br
82570 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74 72 eak;. if( str
82580 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e cmp(zVfs, pVfs->
82590 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 zName)==0 ) brea
825a0 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 k;. }. sqlite3
825b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
825c0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 ex);. return pV
825d0 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c fs;.}../*.** Unl
825e0 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20 74 ink a VFS from t
825f0 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a he linked list.*
82600 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 66 /.static void vf
82610 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f sUnlink(sqlite3_
82620 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 vfs *pVfs){. as
82630 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
82640 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 tex_held(sqlite3
82650 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
82660 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
82670 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66 28 ASTER)) );. if(
82680 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 pVfs==0 ){.
82690 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 /* No-op */. }e
826a0 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 3d lse if( vfsList=
826b0 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66 73 =pVfs ){. vfs
826c0 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 List = pVfs->pNe
826d0 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 xt;. }else if(
826e0 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 vfsList ){. s
826f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 qlite3_vfs *p =
82700 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 vfsList;. whi
82710 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 le( p->pNext &&
82720 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 29 p->pNext!=pVfs )
82730 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 {. p = p->p
82740 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
82750 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 if( p->pNext==pV
82760 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 fs ){. p->p
82770 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 Next = pVfs->pNe
82780 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a xt;. }. }.}.
82790 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 ./*.** Register
827a0 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 a VFS with the s
827b0 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 61 ystem. It is ha
827c0 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 74 rmless to regist
827d0 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 er the same.** V
827e0 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 FS multiple time
827f0 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 20 s. The new VFS
82800 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 61 becomes the defa
82810 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 ult if makeDflt
82820 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 is.** true..*/.S
82830 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
82840 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 lite3_vfs_regist
82850 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a er(sqlite3_vfs *
82860 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 pVfs, int makeDf
82870 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d lt){. sqlite3_m
82880 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b utex *mutex = 0;
82890 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
828a0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
828b0 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
828c0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 _initialize();.
828d0 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
828e0 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 rc;.#endif. mu
828f0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
82900 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
82910 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
82920 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d ER);. sqlite3_m
82930 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
82940 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 );. vfsUnlink(p
82950 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 Vfs);. if( make
82960 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d Dflt || vfsList=
82970 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e =0 ){. pVfs->
82980 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b pNext = vfsList;
82990 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 . vfsList = p
829a0 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 Vfs;. }else{.
829b0 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 pVfs->pNext =
829c0 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a vfsList->pNext;.
829d0 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 vfsList->pNe
829e0 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 xt = pVfs;. }.
829f0 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29 assert(vfsList)
82a00 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
82a10 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
82a20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
82a30 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 OK;.}../*.** Unr
82a40 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73 6f egister a VFS so
82a50 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 6c that it is no l
82a60 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c 65 onger accessible
82a70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
82a80 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f int sqlite3_vfs_
82a90 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 unregister(sqlit
82aa0 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 e3_vfs *pVfs){.#
82ab0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
82ac0 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d SAFE. sqlite3_m
82ad0 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 utex *mutex = sq
82ae0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
82af0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
82b00 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e TIC_MASTER);.#en
82b10 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 dif. sqlite3_mu
82b20 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 tex_enter(mutex)
82b30 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 ;. vfsUnlink(pV
82b40 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d fs);. sqlite3_m
82b50 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
82b60 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
82b70 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a TE_OK;.}../*****
82b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
82b90 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c **********
82ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82bc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
82bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
82be0 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a file fault.c ***
82bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82c10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
82c20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2008 Jan 22.**.*
82c30 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
82c40 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
82c50 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
82c60 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
82c70 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
82c80 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
82c90 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
82ca0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
82cb0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
82cc0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
82cd0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
82ce0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
82cf0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
82d00 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
82d10 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
82d20 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
82d30 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
82d40 0a 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 2a 2a ****************
82d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
82d90 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
82da0 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 70 70 ins code to supp
82db0 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 70 74 20 ort the concept
82dc0 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 of "benign" .**
82dd0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
82de0 28 77 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f (when the xMallo
82df0 63 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 c() or xRealloc(
82e00 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a ) method of the.
82e10 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d ** sqlite3_mem_m
82e20 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 ethods structure
82e30 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 fails to alloca
82e40 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 te a block of me
82e50 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 mory.** and retu
82e60 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d rns 0). .**.** M
82e70 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 ost malloc failu
82e80 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 res are non-beni
82e90 67 6e 2e 20 41 66 74 65 72 20 74 68 65 79 20 6f gn. After they o
82ea0 63 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 ccur, SQLite.**
82eb0 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 abandons the cur
82ec0 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 rent operation a
82ed0 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 nd returns an er
82ee0 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c ror code (usuall
82ef0 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 y.** SQLITE_NOME
82f00 4d 29 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 M) to the user.
82f10 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d However, sometim
82f20 65 73 20 61 20 66 61 75 6c 74 20 69 73 20 6e 6f es a fault is no
82f30 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a t necessarily.**
82f40 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d fatal. For exam
82f50 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 ple, if a malloc
82f60 20 66 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73 fails while res
82f70 69 7a 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 izing a hash tab
82f80 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 le, this .** is
82f90 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 completely recov
82fa0 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 erable simply by
82fb0 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75 not carrying ou
82fc0 74 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68 t the resize. Th
82fd0 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 e .** hash table
82fe0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 will continue t
82ff0 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 o function norma
83000 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f lly. So a mallo
83010 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 c failure .** du
83020 72 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c ring a hash tabl
83030 65 20 72 65 73 69 7a 65 20 69 73 20 61 20 62 65 e resize is a be
83040 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a nign fault..*/..
83050 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
83060 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
83070 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 T../*.** Global
83080 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 variables..*/.ty
83090 70 65 64 65 66 20 73 74 72 75 63 74 20 42 65 6e pedef struct Ben
830a0 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 ignMallocHooks B
830b0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
830c0 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f ;.static SQLITE_
830d0 57 53 44 20 73 74 72 75 63 74 20 42 65 6e 69 67 WSD struct Benig
830e0 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 nMallocHooks {.
830f0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 void (*xBenignB
83100 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 egin)(void);. v
83110 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 oid (*xBenignEnd
83120 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 )(void);.} sqlit
83130 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 e3Hooks = { 0, 0
83140 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 };../* The "wsd
83150 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c Hooks" macro wil
83160 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 l resolve to the
83170 20 61 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e appropriate Ben
83180 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a ignMallocHooks.*
83190 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 * structure. If
831a0 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 writable static
831b0 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f data is unsuppo
831c0 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 rted on the targ
831d0 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 et,.** we have t
831e0 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 o locate the sta
831f0 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e te vector at run
83200 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d -time. In the m
83210 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 ore common.** ca
83220 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c se where writabl
83230 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 e static data is
83240 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 48 supported, wsdH
83250 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 20 64 ooks can refer d
83260 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 irectly.** to th
83270 65 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 e "sqlite3Hooks"
83280 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 state vector de
83290 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f clared above..*/
832a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
832b0 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 MIT_WSD.# define
832c0 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a wsdHooksInit \.
832d0 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f BenignMallocHo
832e0 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c oks *x = &GLOBAL
832f0 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f (BenignMallocHoo
83300 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 ks,sqlite3Hooks)
83310 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f .# define wsdHoo
83320 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 ks x[0].#else.#
83330 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 define wsdHooksI
83340 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 nit.# define wsd
83350 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f Hooks sqlite3Hoo
83360 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a ks.#endif.../*.*
83370 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 * Register hooks
83380 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 to call when sq
83390 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
833a0 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 Malloc() and.**
833b0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
833c0 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 6c Malloc() are cal
833d0 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c led, respectivel
833e0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
833f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
83400 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 3BenignMallocHoo
83410 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 ks(. void (*xBe
83420 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 nignBegin)(void)
83430 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 ,. void (*xBeni
83440 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a gnEnd)(void).){.
83450 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a wsdHooksInit;.
83460 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 wsdHooks.xBeni
83470 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 gnBegin = xBenig
83480 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f nBegin;. wsdHoo
83490 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 ks.xBenignEnd =
834a0 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f xBenignEnd;.}../
834b0 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74 *.** This (sqlit
834c0 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
834d0 63 28 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62 c()) is called b
834e0 79 20 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f y SQLite code to
834f0 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 2a indicate that.*
83500 2a 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c * subsequent mal
83510 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 loc failures are
83520 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 benign. A call
83530 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e to sqlite3EndBen
83540 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 ignMalloc().** i
83550 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 73 75 ndicates that su
83560 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 bsequent malloc
83570 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e failures are non
83580 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 -benign..*/.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 67 69 6e 42 65 6e 69 sqlite3BeginBeni
835b0 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a gnMalloc(void){.
835c0 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a wsdHooksInit;.
835d0 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 if( wsdHooks.x
835e0 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 BenignBegin ){.
835f0 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e wsdHooks.xBen
83600 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a ignBegin();. }.
83610 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
83620 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 void sqlite3End
83630 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 BenignMalloc(voi
83640 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e d){. wsdHooksIn
83650 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f it;. if( wsdHoo
83660 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b ks.xBenignEnd ){
83670 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 . wsdHooks.xB
83680 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a enignEnd();. }.
83690 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 }..#endif /* #
836a0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
836b0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 IT_BUILTIN_TEST
836c0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
836d0 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 *** End of fault
836e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
836f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83710 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
83720 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d *** Begin file m
83730 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em0.c **********
83740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83760 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f **/./*.** 2008 O
83770 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 ctober 28.**.**
83780 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
83790 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
837a0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
837b0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
837c0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
837d0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
837e0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
837f0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
83800 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
83810 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
83820 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
83830 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
83840 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
83850 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
83860 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
83870 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
83880 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
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 2a 2a 2a 2a 2a 2a 2a 2a ****************
838c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
838d0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
838e0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
838f0 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 s a no-op memory
83900 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 allocation driv
83910 65 72 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e ers for use when
83920 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f .** SQLITE_ZERO_
83930 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 MALLOC is define
83940 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 d. The allocati
83950 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65 on drivers imple
83960 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 mented.** here a
83970 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51 4c lways fail. SQL
83980 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 ite will not ope
83990 72 61 74 65 20 77 69 74 68 20 74 68 65 73 65 20 rate with these
839a0 64 72 69 76 65 72 73 2e 20 20 54 68 65 73 65 0a drivers. These.
839b0 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 70 6c ** are merely pl
839c0 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 aceholders. Rea
839d0 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74 20 62 l drivers must b
839e0 65 20 73 75 62 73 74 69 74 75 74 65 64 20 75 73 e substituted us
839f0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 ing.** sqlite3_c
83a00 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 65 20 53 onfig() before S
83a10 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 QLite will opera
83a20 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 te..*/../*.** Th
83a30 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
83a40 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
83a50 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c or is the defaul
83a60 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 t. It is.** use
83a70 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 d when no other
83a80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
83a90 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 is specified us
83aa0 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 ing compile-time
83ab0 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 .** macros..*/.#
83ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 ifdef SQLITE_ZER
83ad0 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 O_MALLOC../*.**
83ae0 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f No-op versions o
83af0 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c f all memory all
83b00 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
83b10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
83b20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f *sqlite3MemMallo
83b30 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 c(int nByte){ re
83b40 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 turn 0; }.static
83b50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
83b60 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f Free(void *pPrio
83b70 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 r){ return; }.st
83b80 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
83b90 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 e3MemRealloc(voi
83ba0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
83bb0 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b Byte){ return 0;
83bc0 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 }.static int sq
83bd0 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 lite3MemSize(voi
83be0 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 d *pPrior){ retu
83bf0 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 rn 0; }.static i
83c00 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 nt sqlite3MemRou
83c10 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 ndup(int n){ ret
83c20 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 urn n; }.static
83c30 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e int sqlite3MemIn
83c40 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 it(void *NotUsed
83c50 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ){ return SQLITE
83c60 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f _OK; }.static vo
83c70 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 id sqlite3MemShu
83c80 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
83c90 73 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a sed){ return; }.
83ca0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
83cb0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ine is the only
83cc0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
83cd0 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e file with extern
83ce0 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a al linkage..**.*
83cf0 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c * Populate the l
83d00 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
83d10 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
83d20 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a ion pointers in.
83d30 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
83d40 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f Config.m with po
83d50 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f inters to the ro
83d60 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 utines in this f
83d70 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile..*/.SQLITE_P
83d80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
83d90 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
83da0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
83db0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
83dc0 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 em_methods defau
83dd0 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 ltMethods = {.
83de0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c sqlite3MemMal
83df0 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
83e00 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 3MemFree,. s
83e10 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
83e20 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
83e30 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 mSize,. sqli
83e40 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 te3MemRoundup,.
83e50 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e sqlite3MemIn
83e60 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 it,. sqlite3
83e70 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 MemShutdown,.
83e80 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 0. };. sqlit
83e90 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
83ea0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 _CONFIG_MALLOC,
83eb0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 &defaultMethods)
83ec0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
83ed0 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f QLITE_ZERO_MALLO
83ee0 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a C */../*********
83ef0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d ***** End of mem
83f00 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0.c ************
83f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83f30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
83f40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
83f50 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem1.c ********
83f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83f80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
83f90 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a August 14.**.**
83fa0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
83fb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
83fc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
83fd0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
83fe0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
83ff0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
84000 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
84010 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
84020 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
84030 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
84040 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
84050 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
84060 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
84070 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
84080 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
84090 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
840a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
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 2a 2a 2a 2a 2a 2a ****************
840e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
840f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
84100 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
84110 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d ns low-level mem
84120 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 ory allocation d
84130 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a rivers for when.
84140 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 ** SQLite will u
84150 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 se the standard
84160 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 C-library malloc
84170 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e /realloc/free in
84180 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 terface.** to ob
84190 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 tain the memory
841a0 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 it needs..**.**
841b0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
841c0 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ns implementatio
841d0 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 ns of the low-le
841e0 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 vel memory alloc
841f0 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 ation.** routine
84200 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 s specified in t
84210 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d he sqlite3_mem_m
84220 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a ethods object..*
84230 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 /../*.** This ve
84240 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d rsion of the mem
84250 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ory allocator is
84260 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 the default. I
84270 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 t is.** used whe
84280 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 n no other memor
84290 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 y allocator is s
842a0 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 pecified using c
842b0 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d ompile-time.** m
842c0 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 acros..*/.#ifdef
842d0 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d SQLITE_SYSTEM_M
842e0 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b ALLOC../*.** Lik
842f0 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 e malloc(), but
84300 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73 69 7a remember the siz
84310 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 e of the allocat
84320 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 ion.** so that w
84330 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 e can find it la
84340 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ter using sqlite
84350 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 3MemSize()..**.*
84360 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c * For this low-l
84370 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 evel routine, we
84380 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 are guaranteed
84390 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 that nByte>0 bec
843a0 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 ause.** cases of
843b0 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 nByte<=0 will b
843c0 65 20 69 6e 74 65 72 63 65 70 74 65 64 20 61 6e e intercepted an
843d0 64 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 d dealt with by
843e0 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 higher level.**
843f0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 routines..*/.sta
84400 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 tic void *sqlite
84410 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 3MemMalloc(int n
84420 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 Byte){. sqlite3
84430 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 _int64 *p;. ass
84440 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a ert( nByte>0 );.
84450 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 nByte = ROUND8
84460 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 6d (nByte);. p = m
84470 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38 20 29 alloc( nByte+8 )
84480 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
84490 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 p[0] = nByte;.
844a0 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 p++;. }. re
844b0 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a turn (void *)p;.
844c0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 }../*.** Like fr
844d0 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73 20 66 ee() but works f
844e0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f or allocations o
844f0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
84500 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a ite3MemMalloc().
84510 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d ** or sqlite3Mem
84520 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a Realloc()..**.**
84530 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 For this low-le
84540 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 vel routine, we
84550 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 already know tha
84560 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 t pPrior!=0 sinc
84570 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 e.** cases where
84580 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 pPrior==0 will
84590 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 63 65 have been intece
845a0 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 pted and dealt w
845b0 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 ith.** by higher
845c0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e -level routines.
845d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
845e0 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 sqlite3MemFree(v
845f0 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 oid *pPrior){.
84600 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
84610 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
84620 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 4*)pPrior;. ass
84630 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 ert( pPrior!=0 )
84640 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65 65 28 ;. p--;. free(
84650 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b p);.}../*.** Lik
84660 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65 e realloc(). Re
84670 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 size an allocati
84680 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 on previously ob
84690 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 tained from.** s
846a0 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 qlite3MemMalloc(
846b0 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 )..**.** For thi
846c0 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65 s low-level inte
846d0 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74 rface, we know t
846e0 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e 20 20 hat pPrior!=0.
846f0 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a 20 70 Cases where.** p
84700 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68 Prior==0 while h
84710 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65 ave been interce
84720 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c pted by higher-l
84730 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64 evel routine and
84740 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 .** redirected t
84750 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69 o xMalloc. Simi
84760 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74 larly, we know t
84770 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 hat nByte>0 beca
84780 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20 77 68 uses.** cases wh
84790 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c ere nByte<=0 wil
847a0 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 l have been inte
847b0 72 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 rcepted by highe
847c0 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 r-level.** routi
847d0 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65 63 74 nes and redirect
847e0 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a ed to xFree..*/.
847f0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c static void *sql
84800 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 ite3MemRealloc(v
84810 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 oid *pPrior, int
84820 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 nByte){. sqlit
84830 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 e3_int64 *p = (s
84840 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 qlite3_int64*)pP
84850 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 rior;. assert(
84860 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e 42 79 pPrior!=0 && nBy
84870 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 te>0 );. nByte
84880 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b = ROUND8(nByte);
84890 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f . p = (sqlite3_
848a0 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 int64*)pPrior;.
848b0 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 61 6c p--;. p = real
848c0 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38 20 29 loc(p, nByte+8 )
848d0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
848e0 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 p[0] = nByte;.
848f0 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 p++;. }. re
84900 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d turn (void*)p;.}
84910 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 ../*.** Report t
84920 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a he allocated siz
84930 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72 65 74 e of a prior ret
84940 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 urn from xMalloc
84950 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c 6c 6f ().** or xReallo
84960 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 c()..*/.static i
84970 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a nt sqlite3MemSiz
84980 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b e(void *pPrior){
84990 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
849a0 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69 6f *p;. if( pPrio
849b0 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b r==0 ) return 0;
849c0 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f . p = (sqlite3_
849d0 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 int64*)pPrior;.
849e0 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20 28 p--;. return (
849f0 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f 2a 0a int)p[0];.}../*.
84a00 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 ** Round up a re
84a10 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 quest size to th
84a20 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c e next valid all
84a30 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f ocation size..*/
84a40 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
84a50 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e te3MemRoundup(in
84a60 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 t n){. return R
84a70 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a OUND8(n);.}../*.
84a80 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
84a90 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 is module..*/.st
84aa0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
84ab0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f MemInit(void *No
84ac0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
84ad0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
84ae0 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ed);. return SQ
84af0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
84b00 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 * Deinitialize t
84b10 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 his module..*/.s
84b20 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 tatic void sqlit
84b30 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f e3MemShutdown(vo
84b40 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
84b50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
84b60 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
84b70 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 urn;.}../*.** Th
84b80 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
84b90 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 e only routine i
84ba0 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 n this file with
84bb0 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 external linkag
84bc0 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 e..**.** Populat
84bd0 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 e the low-level
84be0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
84bf0 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 n function point
84c00 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 ers in.** sqlite
84c10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3GlobalConfig.m
84c20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f with pointers to
84c30 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e the routines in
84c40 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 this file..*/.S
84c50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
84c60 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 id sqlite3MemSet
84c70 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 Default(void){.
84c80 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
84c90 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
84ca0 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 s defaultMethods
84cb0 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 = {. sqlite
84cc0 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 3MemMalloc,.
84cd0 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c sqlite3MemFree,
84ce0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
84cf0 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 Realloc,. sq
84d00 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 lite3MemSize,.
84d10 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 sqlite3MemRou
84d20 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 ndup,. sqlit
84d30 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 e3MemInit,.
84d40 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f sqlite3MemShutdo
84d50 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a wn,. 0. };.
84d60 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 sqlite3_config
84d70 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d (SQLITE_CONFIG_M
84d80 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d ALLOC, &defaultM
84d90 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 ethods);.}..#end
84da0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53 if /* SQLITE_SYS
84db0 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f TEM_MALLOC */../
84dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
84dd0 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a 2a nd of mem1.c ***
84de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
84e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
84e20 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e 63 egin file mem2.c
84e30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
84e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
84e60 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 *.** 2007 August
84e70 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
84e80 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
84e90 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
84ea0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
84eb0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
84ec0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
84ed0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
84ee0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
84ef0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
84f00 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
84f10 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
84f20 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
84f30 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
84f40 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
84f50 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
84f60 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
84f70 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
84f80 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84fd0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
84fe0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d le contains low-
84ff0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
85000 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 ocation drivers
85010 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 for when.** SQLi
85020 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 te will use the
85030 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 standard C-libra
85040 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f ry malloc/reallo
85050 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 c/free interface
85060 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 .** to obtain th
85070 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 e memory it need
85080 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67 20 6c s while adding l
85090 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 ots of additiona
850a0 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 69 l debugging.** i
850b0 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 65 61 nformation to ea
850c0 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e ch allocation in
850d0 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 20 64 order to help d
850e0 65 74 65 63 74 20 61 6e 64 20 66 69 78 20 6d 65 etect and fix me
850f0 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 61 6e mory.** leaks an
85100 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 65 d memory usage e
85110 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 rrors..**.** Thi
85120 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
85130 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
85140 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c of the low-level
85150 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
85160 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 on.** routines s
85170 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 pecified in the
85180 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
85190 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a ods object..*/..
851a0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
851b0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
851c0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 allocator is us
851d0 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a ed only if the.*
851e0 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 * SQLITE_MEMDEBU
851f0 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e G macro is defin
85200 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ed.*/.#ifdef SQL
85210 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a ITE_MEMDEBUG../*
85220 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 .** The backtrac
85230 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 e functionality
85240 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
85250 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a e with GLIBC.*/.
85260 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f #ifdef __GLIBC__
85270 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 . extern int ba
85280 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 cktrace(void**,i
85290 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f nt);. extern vo
852a0 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d id backtrace_sym
852b0 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e bols_fd(void*con
852c0 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 st*,int,int);.#e
852d0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 lse.# define bac
852e0 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 ktrace(A,B) 1.#
852f0 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 define backtrace
85300 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c _symbols_fd(A,B,
85310 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a C).#endif../*.**
85320 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c Each memory all
85330 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 ocation looks li
85340 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
85350 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85360 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85370 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85380 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85390 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 --------.** | T
853a0 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63 itle | backtrac
853b0 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 e pointers | Me
853c0 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c mBlockHdr | all
853d0 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 ocation | EndGu
853e0 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d ard |.** ------
853f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85400 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85410 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85420 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85430 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 --.**.** The app
85440 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 lication code se
85450 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 es only a pointe
85460 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 r to the allocat
85470 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a ion. We have.**
85480 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d to back up from
85490 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
854a0 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 pointer to find
854b0 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e the MemBlockHdr.
854c0 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 The.** MemBloc
854d0 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68 kHdr tells us th
854e0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c e size of the al
854f0 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 location and the
85500 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 number of.** ba
85510 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 cktrace pointers
85520 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f . There is also
85530 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 a guard word at
85540 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a the end of the.
85550 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a ** MemBlockHdr..
85560 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f */.struct MemBlo
85570 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53 ckHdr {. i64 iS
85580 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
85590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
855a0 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c Size of this al
855b0 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 location */. st
855c0 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
855d0 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b *pNext, *pPrev;
855e0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
855f0 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 of all unfreed
85600 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 memory */. char
85610 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20 nBacktrace;
85620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85630 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 /* Number of bac
85640 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20 ktraces on this
85650 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 alloc */. char
85660 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
85670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
85680 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b * Available back
85690 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 trace slots */.
856a0 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 short nTitle;
856b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
856c0 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 /* Bytes of
856d0 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 title; includes
856e0 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 '\0' */. int i
856f0 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 20 ForeGuard;
85700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
85710 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72 * Guard word for
85720 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f sanity */.};../
85730 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73 *.** Guard words
85740 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 .*/.#define FORE
85750 47 55 41 52 44 20 30 78 38 30 46 35 45 31 35 33 GUARD 0x80F5E153
85760 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 41 .#define REARGUA
85770 52 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f RD 0xE4676B53../
85780 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d *.** Number of m
85790 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 alloc size incre
857a0 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a ments to track..
857b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a */.#define NCSIZ
857c0 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 E 1000../*.** A
857d0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 ll of the static
857e0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
857f0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 by this module a
85800 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 re collected.**
85810 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 into a single st
85820 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d ructure named "m
85830 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f em". This is to
85840 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 keep the.** sta
85850 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 tic variables or
85860 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 ganized and to r
85870 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 educe namespace
85880 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 pollution.** whe
85890 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 n this module is
858a0 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f combined with o
858b0 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c ther in the amal
858c0 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 gamation..*/.sta
858d0 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 0a tic struct {. .
858e0 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 /*. ** Mutex
858f0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
85900 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 s to the memory
85910 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
85920 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c stem.. */. sql
85930 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
85940 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 x;.. /*. ** He
85950 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 ad and tail of a
85960 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
85970 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 all outstanding
85980 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f allocations. */
85990 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
859a0 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 ckHdr *pFirst;.
859b0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
859c0 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 Hdr *pLast;. .
859d0 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d /*. ** The num
859e0 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 ber of levels of
859f0 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73 61 backtrace to sa
85a00 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 ve in new alloca
85a10 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e tions.. */. in
85a20 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 t nBacktrace;.
85a30 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 void (*xBacktrac
85a40 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 e)(int, int, voi
85a50 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a d **);.. /*. *
85a60 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20 * Title text to
85a70 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 insert in front
85a80 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 of each block.
85a90 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b */. int nTitle;
85aa0 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
85ab0 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 of zTitle to sa
85ac0 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c ve. Includes '\
85ad0 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 0' and padding *
85ae0 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b /. char zTitle[
85af0 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 100]; /* The ti
85b00 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f tle text */.. /
85b10 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d * . ** sqlite3M
85b20 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 allocDisallow()
85b30 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 66 increments the f
85b40 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 ollowing counter
85b50 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 .. ** sqlite3Ma
85b60 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 llocAllow() decr
85b70 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a ements it.. */.
85b80 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 int disallow;
85b90 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 /* Do not allow
85ba0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
85bb0 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 n */.. /*. **
85bc0 47 61 74 68 65 72 20 73 74 61 74 69 73 74 69 63 Gather statistic
85bd0 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 6f s on the sizes o
85be0 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 f memory allocat
85bf0 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f ions.. ** nAllo
85c00 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 c[i] is the numb
85c10 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e er of allocation
85c20 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 attempts of i*8
85c30 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d . ** bytes. i=
85c40 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e =NCSIZE is the n
85c50 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
85c60 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72 ion attempts for
85c70 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 . ** sizes more
85c80 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 than NCSIZE*8 b
85c90 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 ytes.. */. int
85ca0 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b nAlloc[NCSIZE];
85cb0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
85cc0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
85cd0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 ions */. int nC
85ce0 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 urrent[NCSIZE];
85cf0 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 /* Current nu
85d00 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 mber of allocati
85d10 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 ons */. int mxC
85d20 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 urrent[NCSIZE];
85d30 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d /* Highwater m
85d40 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 ark for nCurrent
85d50 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a */..} mem;.../*
85d60 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 .** Adjust memor
85d70 79 20 75 73 61 67 65 20 73 74 61 74 69 73 74 69 y usage statisti
85d80 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 cs.*/.static voi
85d90 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 69 6e d adjustStats(in
85da0 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 t iSize, int inc
85db0 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 rement){. int i
85dc0 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a 65 29 = ROUND8(iSize)
85dd0 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 49 /8;. if( i>NCSI
85de0 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d 20 ZE-1 ){. i =
85df0 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a NCSIZE - 1;. }.
85e00 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e if( increment>
85e10 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c 0 ){. mem.nAl
85e20 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65 loc[i]++;. me
85e30 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b m.nCurrent[i]++;
85e40 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75 . if( mem.nCu
85e50 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43 rrent[i]>mem.mxC
85e60 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20 urrent[i] ){.
85e70 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 mem.mxCurrent
85e80 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65 [i] = mem.nCurre
85e90 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d nt[i];. }. }
85ea0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43 else{. mem.nC
85eb0 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20 urrent[i]--;.
85ec0 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75 assert( mem.nCu
85ed0 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20 rrent[i]>=0 );.
85ee0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 }.}../*.** Give
85ef0 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c n an allocation,
85f00 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f find the MemBlo
85f10 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 61 ckHdr for that a
85f20 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
85f30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
85f40 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20 ecks the guards
85f50 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66 at either end of
85f60 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
85f70 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 and.** if they a
85f80 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 20 re incorrect it
85f90 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74 asserts..*/.stat
85fa0 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f ic struct MemBlo
85fb0 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d 65 ckHdr *sqlite3Me
85fc0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76 6f msysGetHeader(vo
85fd0 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29 id *pAllocation)
85fe0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
85ff0 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74 ockHdr *p;. int
86000 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55 *pInt;. u8 *pU
86010 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 8;. int nReserv
86020 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 63 e;.. p = (struc
86030 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70 t MemBlockHdr*)p
86040 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d Allocation;. p-
86050 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e -;. assert( p->
86060 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e 74 iForeGuard==(int
86070 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 20 )FOREGUARD );.
86080 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 nReserve = ROUND
86090 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 70 8(p->iSize);. p
860a0 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c 6c Int = (int*)pAll
860b0 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20 3d ocation;. pU8 =
860c0 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 6f (u8*)pAllocatio
860d0 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e n;. assert( pIn
860e0 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f t[nReserve/sizeo
860f0 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 45 f(int)]==(int)RE
86100 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f 2a 20 ARGUARD );. /*
86110 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79 20 This checks any
86120 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20 62 of the "extra" b
86130 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 ytes allocated d
86140 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e 64 ue. ** to round
86150 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20 62 ing up to an 8 b
86160 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f 20 yte boundary to
86170 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 68 65 ensure . ** the
86180 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e 20 6f y haven't been o
86190 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a 2f verwritten.. */
861a0 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 65 72 . while( nReser
861b0 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a 65 20 ve-- > p->iSize
861c0 29 20 61 73 73 65 72 74 28 20 70 55 38 5b 6e 52 ) assert( pU8[nR
861d0 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 29 3b eserve]==0x65 );
861e0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
861f0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
86200 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
86210 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 currently alloc
86220 61 74 65 64 20 61 74 20 61 64 64 72 65 73 73 20 ated at address
86230 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
86240 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 sqlite3MemSize(
86250 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72 75 void *p){. stru
86260 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
86270 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20 29 pHdr;. if( !p )
86280 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
86290 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71 6c }. pHdr = sql
862a0 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 ite3MemsysGetHea
862b0 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72 6e der(p);. return
862c0 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d 0a pHdr->iSize;.}.
862d0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
862e0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c e the memory all
862f0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
86300 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
86310 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 sqlite3MemInit(
86320 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
86330 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
86340 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 ER(NotUsed);. a
86350 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28 73 ssert( (sizeof(s
86360 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
86370 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a 20 20 r)&7) == 0 );.
86380 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 if( !sqlite3Glob
86390 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 alConfig.bMemsta
863a0 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6d t ){. /* If m
863b0 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 73 20 emory status is
863c0 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 enabled, then th
863d0 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70 70 e malloc.c wrapp
863e0 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 0a er will already.
863f0 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65 20 ** hold the
86400 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 STATIC_MEM mutex
86410 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 69 6e when the routin
86420 65 73 20 68 65 72 65 20 61 72 65 20 69 6e 76 6f es here are invo
86430 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d 2e ked. */. mem.
86440 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
86450 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
86460 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
86470 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e M);. }. return
86480 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
86490 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a *.** Deinitializ
864a0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c e the memory all
864b0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
864c0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 m..*/.static voi
864d0 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 d sqlite3MemShut
864e0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 down(void *NotUs
864f0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
86500 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
86510 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 ;. mem.mutex =
86520 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 0;.}../*.** Roun
86530 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 d up a request s
86540 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ize to the next
86550 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e valid allocation
86560 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 size..*/.static
86570 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 int sqlite3MemR
86580 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 oundup(int n){.
86590 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e return ROUND8(n
865a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f );.}../*.** Allo
865b0 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 cate nByte bytes
865c0 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 of memory..*/.s
865d0 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 tatic void *sqli
865e0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 te3MemMalloc(int
865f0 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63 nByte){. struc
86600 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
86610 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 Hdr;. void **pB
86620 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 t;. char *z;.
86630 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 int *pInt;. voi
86640 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 d *p = 0;. int
86650 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 totalSize;. int
86660 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 6c nReserve;. sql
86670 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
86680 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 61 (mem.mutex);. a
86690 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c ssert( mem.disal
866a0 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 73 low==0 );. nRes
866b0 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 erve = ROUND8(nB
866c0 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 69 7a yte);. totalSiz
866d0 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 73 e = nReserve + s
866e0 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 73 izeof(*pHdr) + s
866f0 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 20 izeof(int) +.
86700 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e mem.
86710 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f nBacktrace*sizeo
86720 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e 6e f(void*) + mem.n
86730 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 6c Title;. p = mal
86740 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b 0a loc(totalSize);.
86750 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 7a if( p ){. z
86760 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 3d 20 = p;. pBt =
86770 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e (void**)&z[mem.n
86780 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 72 Title];. pHdr
86790 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c = (struct MemBl
867a0 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 6d ockHdr*)&pBt[mem
867b0 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 20 .nBacktrace];.
867c0 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d 20 pHdr->pNext =
867d0 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 0;. pHdr->pPr
867e0 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b 0a ev = mem.pLast;.
867f0 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 73 if( mem.pLas
86800 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70 t ){. mem.p
86810 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 48 Last->pNext = pH
86820 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 dr;. }else{.
86830 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 mem.pFirst
86840 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 20 = pHdr;. }.
86850 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 mem.pLast = pH
86860 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 46 dr;. pHdr->iF
86870 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 45 47 oreGuard = FOREG
86880 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 2d 3e UARD;. pHdr->
86890 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 nBacktraceSlots
868a0 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 = mem.nBacktrace
868b0 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 74 ;. pHdr->nTit
868c0 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b le = mem.nTitle;
868d0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 61 . if( mem.nBa
868e0 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 cktrace ){.
868f0 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 5d void *aAddr[40]
86900 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 ;. pHdr->nB
86910 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 6b 74 acktrace = backt
86920 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 6d 2e race(aAddr, mem.
86930 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 3b nBacktrace+1)-1;
86940 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 42 . memcpy(pB
86950 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 48 t, &aAddr[1], pH
86960 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a 73 dr->nBacktrace*s
86970 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a 20 izeof(void*));.
86980 20 20 20 20 20 61 73 73 65 72 74 28 70 42 74 5b assert(pBt[
86990 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 0]);. if( m
869a0 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29 7b em.xBacktrace ){
869b0 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42 61 . mem.xBa
869c0 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20 70 cktrace(nByte, p
869d0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d Hdr->nBacktrace-
869e0 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0a 20 1, &aAddr[1]);.
869f0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
86a00 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e 42 {. pHdr->nB
86a10 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a 20 20 acktrace = 0;.
86a20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e }. if( mem.
86a30 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20 20 nTitle ){.
86a40 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a 54 memcpy(z, mem.zT
86a50 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c 65 itle, mem.nTitle
86a60 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 48 64 );. }. pHd
86a70 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 74 65 r->iSize = nByte
86a80 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 61 74 ;. adjustStat
86a90 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a 20 20 s(nByte, +1);.
86aa0 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 26 pInt = (int*)&
86ab0 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 49 6e pHdr[1];. pIn
86ac0 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 6f t[nReserve/sizeo
86ad0 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 47 55 f(int)] = REARGU
86ae0 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 ARD;. memset(
86af0 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65 73 pInt, 0x65, nRes
86b00 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d 20 28 erve);. p = (
86b10 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d 0a void*)pInt;. }.
86b20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
86b30 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 leave(mem.mutex)
86b40 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d ;. return p; .}
86b50 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d ../*.** Free mem
86b60 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ory..*/.static v
86b70 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 oid sqlite3MemFr
86b80 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 ee(void *pPrior)
86b90 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
86ba0 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
86bb0 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 void **pBt;. ch
86bc0 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 ar *z;. assert(
86bd0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
86be0 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c 7c nfig.bMemstat ||
86bf0 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b mem.mutex!=0 );
86c00 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 65 . pHdr = sqlite
86c10 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 3MemsysGetHeader
86c20 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 74 20 (pPrior);. pBt
86c30 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a = (void**)pHdr;.
86c40 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e pBt -= pHdr->n
86c50 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a BacktraceSlots;.
86c60 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
86c70 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 enter(mem.mutex)
86c80 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 50 ;. if( pHdr->pP
86c90 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65 72 rev ){. asser
86ca0 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e t( pHdr->pPrev->
86cb0 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a 20 pNext==pHdr );.
86cc0 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e pHdr->pPrev->
86cd0 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70 4e pNext = pHdr->pN
86ce0 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ext;. }else{.
86cf0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 46 assert( mem.pF
86d00 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 irst==pHdr );.
86d10 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 mem.pFirst = p
86d20 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a Hdr->pNext;. }.
86d30 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65 78 if( pHdr->pNex
86d40 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 t ){. assert(
86d50 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 pHdr->pNext->pP
86d60 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 rev==pHdr );.
86d70 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 pHdr->pNext->pP
86d80 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 rev = pHdr->pPre
86d90 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 v;. }else{.
86da0 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61 73 assert( mem.pLas
86db0 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 6d t==pHdr );. m
86dc0 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 2d em.pLast = pHdr-
86dd0 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a 20 >pPrev;. }. z
86de0 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20 20 = (char*)pBt;.
86df0 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74 6c z -= pHdr->nTitl
86e00 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 74 73 e;. adjustStats
86e10 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 2d 31 (pHdr->iSize, -1
86e20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20 30 );. memset(z, 0
86e30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69 64 x2b, sizeof(void
86e40 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 *)*pHdr->nBacktr
86e50 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65 6f aceSlots + sizeo
86e60 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20 20 f(*pHdr) +.
86e70 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48 64 pHd
86e80 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65 6f r->iSize + sizeo
86e90 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e 6e f(int) + pHdr->n
86ea0 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28 7a Title);. free(z
86eb0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
86ec0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 ex_leave(mem.mut
86ed0 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ex); .}../*.**
86ee0 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 Change the size
86ef0 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d of an existing m
86f00 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
86f10 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 ..**.** For this
86f20 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c 65 debugging imple
86f30 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a 61 mentation, we *a
86f40 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63 6f lways* make a co
86f50 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 6c py of the.** all
86f60 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20 6e ocation into a n
86f70 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d 6f ew place in memo
86f80 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 ry. In this way
86f90 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69 67 , if the .** hig
86fa0 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20 69 her level code i
86fb0 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72 20 s using pointer
86fc0 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63 to the old alloc
86fd0 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a 2a ation, it is .**
86fe0 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c much more likel
86ff0 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20 77 y to break and w
87000 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65 20 e are much more
87010 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a 2a liking to find.*
87020 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a * the error..*/.
87030 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c static void *sql
87040 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 ite3MemRealloc(v
87050 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 oid *pPrior, int
87060 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 63 nByte){. struc
87070 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
87080 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a OldHdr;. void *
87090 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28 20 pNew;. assert(
870a0 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 mem.disallow==0
870b0 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 73 );. pOldHdr = s
870c0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
870d0 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 eader(pPrior);.
870e0 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d pNew = sqlite3M
870f0 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b emMalloc(nByte);
87100 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 . if( pNew ){.
87110 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 memcpy(pNew,
87120 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70 4f pPrior, nByte<pO
87130 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20 6e ldHdr->iSize ? n
87140 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d 3e Byte : pOldHdr->
87150 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20 iSize);. if(
87160 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e 69 nByte>pOldHdr->i
87170 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d 65 Size ){. me
87180 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 4e mset(&((char*)pN
87190 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 69 ew)[pOldHdr->iSi
871a0 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74 65 ze], 0x2b, nByte
871b0 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a - pOldHdr->iSiz
871c0 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 e);. }. sq
871d0 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50 72 lite3MemFree(pPr
871e0 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ior);. }. retu
871f0 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
87200 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c * Populate the l
87210 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
87220 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
87230 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a ion pointers in.
87240 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
87250 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f Config.m with po
87260 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f inters to the ro
87270 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 utines in this f
87280 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile..*/.SQLITE_P
87290 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
872a0 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
872b0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
872c0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
872d0 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 em_methods defau
872e0 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 ltMethods = {.
872f0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c sqlite3MemMal
87300 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
87310 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 3MemFree,. s
87320 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
87330 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
87340 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 mSize,. sqli
87350 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 te3MemRoundup,.
87360 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e sqlite3MemIn
87370 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 it,. sqlite3
87380 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 MemShutdown,.
87390 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 0. };. sqlit
873a0 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
873b0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 _CONFIG_MALLOC,
873c0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 &defaultMethods)
873d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
873e0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 61 63 he number of bac
873f0 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 6b 65 ktrace levels ke
87400 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c 6c 6f pt for each allo
87410 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 6c cation..** A val
87420 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 6e 73 ue of zero turns
87430 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 6e 67 off backtracing
87440 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 69 73 . The number is
87450 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 64 0a always rounded.
87460 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c 74 69 ** up to a multi
87470 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51 4c ple of 2..*/.SQL
87480 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
87490 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
874a0 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64 65 Backtrace(int de
874b0 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 70 74 pth){. if( dept
874c0 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20 30 h<0 ){ depth = 0
874d0 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 68 3e ; }. if( depth>
874e0 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 32 30 20 ){ depth = 20
874f0 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 28 64 ; }. depth = (d
87500 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a 20 20 epth+1)&0xfe;.
87510 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 3d mem.nBacktrace =
87520 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49 54 depth;.}..SQLIT
87530 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
87540 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42 61 qlite3MemdebugBa
87550 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 6b 28 cktraceCallback(
87560 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 void (*xBacktrac
87570 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 e)(int, int, voi
87580 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78 42 d **)){. mem.xB
87590 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 63 6b acktrace = xBack
875a0 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 trace;.}../*.**
875b0 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 73 74 Set the title st
875c0 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 71 75 ring for subsequ
875d0 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e ent allocations.
875e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
875f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
87600 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 65 28 emdebugSettitle(
87610 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 69 74 const char *zTit
87620 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 le){. unsigned
87630 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 int n = sqlite3S
87640 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29 20 trlen30(zTitle)
87650 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d + 1;. sqlite3_m
87660 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d utex_enter(mem.m
87670 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 3e 3d utex);. if( n>=
87680 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c sizeof(mem.zTitl
87690 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66 28 e) ) n = sizeof(
876a0 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a 20 mem.zTitle)-1;.
876b0 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 69 74 memcpy(mem.zTit
876c0 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 3b 0a le, zTitle, n);.
876d0 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d 20 mem.zTitle[n]
876e0 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74 6c = 0;. mem.nTitl
876f0 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 20 e = ROUND8(n);.
87700 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
87710 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b eave(mem.mutex);
87720 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
87730 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
87740 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 0a 20 emdebugSync(){.
87750 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b struct MemBlock
87760 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 6f 72 Hdr *pHdr;. for
87770 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 (pHdr=mem.pFirst
87780 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 ; pHdr; pHdr=pHd
87790 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 76 r->pNext){. v
877a0 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 6f 69 oid **pBt = (voi
877b0 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 70 42 d**)pHdr;. pB
877c0 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 6b t -= pHdr->nBack
877d0 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 20 traceSlots;.
877e0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 70 mem.xBacktrace(p
877f0 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 64 72 Hdr->iSize, pHdr
87800 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c 20 ->nBacktrace-1,
87810 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d 0a &pBt[1]);. }.}.
87820 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 ./*.** Open the
87830 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 file indicated a
87840 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f nd write a log o
87850 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 f all unfreed me
87860 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 mory .** allocat
87870 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c ions into that l
87880 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 og..*/.SQLITE_PR
87890 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
878a0 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 28 63 e3MemdebugDump(c
878b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
878c0 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f name){. FILE *o
878d0 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d ut;. struct Mem
878e0 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a BlockHdr *pHdr;.
878f0 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 void **pBt;.
87900 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d 20 66 int i;. out = f
87910 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 open(zFilename,
87920 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d "w");. if( out=
87930 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 =0 ){. fprint
87940 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e f(stderr, "** Un
87950 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d able to output m
87960 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70 emory debug outp
87970 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 ut log: %s **\n"
87980 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
87990 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 zFilename)
879a0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
879b0 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d }. for(pHdr=mem
879c0 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 .pFirst; pHdr; p
879d0 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 Hdr=pHdr->pNext)
879e0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 {. char *z =
879f0 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 20 20 (char*)pHdr;.
87a00 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 z -= pHdr->nBac
87a10 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 7a 65 ktraceSlots*size
87a20 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 64 72 of(void*) + pHdr
87a30 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 66 70 ->nTitle;. fp
87a40 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a 2a 2a rintf(out, "****
87a50 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 20 25 %lld bytes at %
87a60 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c 6e p from %s ****\n
87a70 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 ", .
87a80 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 70 48 pHdr->iSize, &pH
87a90 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 54 69 dr[1], pHdr->nTi
87aa0 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22 29 tle ? z : "???")
87ab0 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 2d 3e ;. if( pHdr->
87ac0 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 nBacktrace ){.
87ad0 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 29 3b fflush(out);
87ae0 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 76 6f . pBt = (vo
87af0 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 20 id**)pHdr;.
87b00 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 pBt -= pHdr->nB
87b10 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 acktraceSlots;.
87b20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 5f 73 backtrace_s
87b30 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c 20 70 ymbols_fd(pBt, p
87b40 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2c Hdr->nBacktrace,
87b50 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a 20 fileno(out));.
87b60 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 fprintf(out
87b70 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 , "\n");. }.
87b80 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 }. fprintf(out
87b90 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b 0a , "COUNTS:\n");.
87ba0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 for(i=0; i<NCS
87bb0 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 IZE-1; i++){.
87bc0 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b if( mem.nAlloc[
87bd0 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 i] ){. fpri
87be0 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64 ntf(out, " %5d
87bf0 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64 : %10d %10d %10d
87c00 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 \n", .
87c10 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c 6f i*8, mem.nAllo
87c20 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72 65 c[i], mem.nCurre
87c30 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 nt[i], mem.mxCur
87c40 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a rent[i]);. }.
87c50 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e 6e 41 }. if( mem.nA
87c60 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 20 29 lloc[NCSIZE-1] )
87c70 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
87c80 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 t, " %5d: %10d
87c90 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0a 20 %10d %10d\n",.
87ca0 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 53 49 NCSI
87cb0 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c 6c ZE*8-8, mem.nAll
87cc0 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20 20 oc[NCSIZE-1],.
87cd0 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e 6e mem.n
87ce0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 Current[NCSIZE-1
87cf0 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 ], mem.mxCurrent
87d00 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 20 7d [NCSIZE-1]);. }
87d10 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a . fclose(out);.
87d20 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
87d30 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 the number of ti
87d40 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 mes sqlite3MemMa
87d50 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 6e 20 lloc() has been
87d60 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 called..*/.SQLIT
87d70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
87d80 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61 6c lite3MemdebugMal
87d90 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 69 6e locCount(){. in
87da0 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 61 t i;. int nTota
87db0 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 l = 0;. for(i=0
87dc0 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b 29 ; i<NCSIZE; i++)
87dd0 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d 20 {. nTotal +=
87de0 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a 20 mem.nAlloc[i];.
87df0 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f 74 }. return nTot
87e00 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f al;.}...#endif /
87e10 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 * SQLITE_MEMDEBU
87e20 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a G */../*********
87e30 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d ***** End of mem
87e40 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2.c ************
87e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87e70 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
87e80 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
87e90 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem3.c ********
87ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87ec0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
87ed0 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a October 14.**.*
87ee0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
87ef0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
87f00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
87f10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
87f20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
87f30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
87f40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
87f50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
87f60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
87f70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
87f80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
87f90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
87fa0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
87fb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
87fc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
87fd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
87fe0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
87ff0 0a 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 2a 2a 2a 2a 2a 2a ****************
88020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
88030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
88040 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
88050 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
88060 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
88070 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f a memory.** allo
88080 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
88090 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 for use by SQLi
880a0 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 te. .**.** This
880b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
880c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
880d0 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 73 subsystem omits
880e0 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d all.** use of m
880f0 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 51 4c alloc(). The SQL
88100 69 74 65 20 75 73 65 72 20 73 75 70 70 6c 69 65 ite user supplie
88110 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d s a block of mem
88120 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 ory.** before ca
88130 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e lling sqlite3_in
88140 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 itialize() from
88150 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e which allocation
88160 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e s.** are made an
88170 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 d returned by th
88180 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 e xMalloc() and
88190 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 xRealloc() .** i
881a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 mplementations.
881b0 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 Once sqlite3_ini
881c0 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 tialize() has be
881d0 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 en called,.** th
881e0 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f e amount of memo
881f0 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 ry available to
88200 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 SQLite is fixed
88210 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 and cannot.** be
88220 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
88230 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
88240 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
88250 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
88260 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 is included.** i
88270 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 n the build only
88280 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c if SQLITE_ENABL
88290 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65 66 E_MEMSYS3 is def
882a0 69 6e 65 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ined..*/../*.**
882b0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
882c0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
882d0 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 69 ator is only bui
882e0 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 lt into the libr
882f0 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e ary.** SQLITE_EN
88300 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 ABLE_MEMSYS3 is
88310 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 6e defined. Definin
88320 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 6f g this symbol do
88330 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74 es not.** mean t
88340 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 hat the library
88350 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f 72 will use a memor
88360 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 6c y-pool by defaul
88370 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a 20 t, just that.**
88380 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e it is available.
88390 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c The mempool all
883a0 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 61 ocator is activa
883b0 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a ted by calling.*
883c0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 * sqlite3_config
883d0 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ()..*/.#ifdef SQ
883e0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
883f0 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d YS3../*.** Maxim
88400 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 um size (in Mem3
88410 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d Blocks) of a "sm
88420 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 all" chunk..*/.#
88430 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20 define MX_SMALL
88440 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 10.../*.** Numbe
88450 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61 r of freelist ha
88460 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66 sh slots.*/.#def
88470 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a ine N_HASH 61..
88480 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61 /*.** A memory a
88490 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20 llocation (also
884a0 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22 called a "chunk"
884b0 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77 ) consists of tw
884c0 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c o or .** more bl
884d0 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20 ocks where each
884e0 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73 block is 8 bytes
884f0 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62 . The first 8 b
88500 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68 ytes are .** a h
88510 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f eader that is no
88520 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 t returned to th
88530 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 e user..**.** A
88540 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20 chunk is two or
88550 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74 more blocks that
88560 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b is either check
88570 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65 ed out or.** fre
88580 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c e. The first bl
88590 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75 ock has format u
885a0 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a .hdr. u.hdr.siz
885b0 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74 e4x is 4 times t
885c0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 he.** size of th
885d0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 e allocation in
885e0 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c blocks if the al
885f0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
88600 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73 ..** The u.hdr.s
88610 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74 ize4x&1 bit is t
88620 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b rue if the chunk
88630 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20 is checked out
88640 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 and.** false if
88650 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 the chunk is on
88660 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54 the freelist. T
88670 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 he u.hdr.size4x&
88680 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2 bit.** is true
88690 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
886a0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
886b0 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20 d out and false
886c0 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f if the.** previo
886d0 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65 us chunk is free
886e0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65 . The u.hdr.pre
886f0 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74 vSize field is t
88700 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 he size of.** th
88710 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b e previous chunk
88720 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 in blocks if th
88730 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b e previous chunk
88740 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72 is on the.** fr
88750 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70 eelist. If the p
88760 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 revious chunk is
88770 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 checked out, th
88780 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76 en.** u.hdr.prev
88790 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74 Size can be part
887a0 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 of the data for
887b0 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20 that chunk and
887c0 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65 should.** not be
887d0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
887e0 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e ..**.** We often
887f0 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e identify a chun
88800 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69 k by its index i
88810 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20 n mem3.aPool[].
88820 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73 When.** this is
88830 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b done, the chunk
88840 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f index refers to
88850 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 the second bloc
88860 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e k of.** the chun
88870 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c k. In this way,
88880 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b the first chunk
88890 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
888a0 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 1..** A chunk i
888b0 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20 ndex of 0 means
888c0 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20 "no such chunk"
888d0 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76 and is the equiv
888e0 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55 alent.** of a NU
888f0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
88900 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f * The second blo
88910 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b ck of free chunk
88920 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d s is of the form
88930 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a u.list. The.**
88940 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d two fields form
88950 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64 a double-linked
88960 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20 list of chunks
88970 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73 of related sizes
88980 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f ..** Pointers to
88990 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 the head of the
889a0 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 64 list are stored
889b0 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c in mem3.aiSmall
889c0 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c [] .** for small
889d0 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65 er chunks and me
889e0 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 20 m3.aiHash[] for
889f0 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a 2a larger chunks..*
88a00 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 *.** The second
88a10 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e 6b block of a chunk
88a20 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 66 is user data if
88a30 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68 the chunk is ch
88a40 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 20 ecked .** out.
88a50 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 68 If a chunk is ch
88a60 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 75 ecked out, the u
88a70 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 74 ser data may ext
88a80 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 end into.** the
88a90 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 76 u.hdr.prevSize v
88aa0 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c alue of the foll
88ab0 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a owing chunk..*/.
88ac0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
88ad0 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f em3Block Mem3Blo
88ae0 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 42 ck;.struct Mem3B
88af0 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b lock {. union {
88b00 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
88b10 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65 u32 prevSize
88b20 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 ; /* Size of p
88b30 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e revious chunk in
88b40 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 Mem3Block eleme
88b50 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 nts */. u32
88b60 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20 size4x; /*
88b70 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63 4x the size of c
88b80 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20 urrent chunk in
88b90 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e Mem3Block elemen
88ba0 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 3b ts */. } hdr;
88bb0 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
88bc0 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 20 u32 next;
88bd0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
88be0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 mem3.aPool[] of
88bf0 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 next free chunk
88c00 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 */. u32 pre
88c10 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 v; /* Inde
88c20 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x in mem3.aPool[
88c30 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 ] of previous fr
88c40 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 ee chunk */.
88c50 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d } list;. } u;.}
88c60 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 ;../*.** All of
88c70 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 the static varia
88c80 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69 bles used by thi
88c90 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c s module are col
88ca0 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 lected.** into a
88cb0 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 single structur
88cc0 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e 20 e named "mem3".
88cd0 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 This is to keep
88ce0 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 the.** static v
88cf0 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a ariables organiz
88d00 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 ed and to reduce
88d10 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 namespace pollu
88d20 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 tion.** when thi
88d30 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 s module is comb
88d40 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 ined with other
88d50 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 in the amalgamat
88d60 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 ion..*/.static S
88d70 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
88d80 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 20 Mem3Global {.
88d90 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 /*. ** Memory a
88da0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c vailable for all
88db0 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69 ocation. nPool i
88dc0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 s the size of th
88dd0 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 6e e array. ** (in
88de0 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f 69 Mem3Blocks) poi
88df0 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f 6c nted to by aPool
88e00 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 20 less 2.. */.
88e10 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d u32 nPool;. Mem
88e20 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a 3Block *aPool;..
88e30 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 69 /*. ** True i
88e40 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74 f we are evaluat
88e50 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 ing an out-of-me
88e60 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 mory callback..
88e70 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d 42 */. int alarmB
88e80 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a usy;. . /*. *
88e90 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 * Mutex to contr
88ea0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
88eb0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
88ec0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 on subsystem..
88ed0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
88ee0 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 20 ex *mutex;. .
88ef0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e 69 /*. ** The mini
88f00 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 mum amount of fr
88f10 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 65 ee space that we
88f20 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a 2f have seen.. */
88f30 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 3b . u32 mnMaster;
88f40 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73 .. /*. ** iMas
88f50 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 78 ter is the index
88f60 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
88f70 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 20 hunk. Most new
88f80 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2a allocations. **
88f90 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 68 occur off of th
88fa0 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 73 is chunk. szMas
88fb0 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 20 ter is the size
88fc0 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a (in Mem3Blocks).
88fd0 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72 ** of the curr
88fe0 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d 61 ent master. iMa
88ff0 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 65 ster is 0 if the
89000 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 72 re is not master
89010 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 chunk.. ** The
89020 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 73 master chunk is
89030 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 74 not in either t
89040 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 61 he aiHash[] or a
89050 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20 iSmall[].. */.
89060 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 20 u32 iMaster;.
89070 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a 20 u32 szMaster;..
89080 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f /*. ** Array o
89090 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20 f lists of free
890a0 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67 blocks according
890b0 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 to the block si
890c0 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d 61 ze . ** for sma
890d0 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 20 ller chunks, or
890e0 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 6c a hash on the bl
890f0 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 72 ock size for lar
89100 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e ger. ** chunks.
89110 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 6d . */. u32 aiSm
89120 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b all[MX_SMALL-1];
89130 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 /* For sizes
89140 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 41 2 through MX_SMA
89150 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a 2f LL, inclusive */
89160 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e 5f . u32 aiHash[N_
89170 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a HASH]; /*
89180 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 4d For sizes MX_SM
89190 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 72 ALL+1 and larger
891a0 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 39 */.} mem3 = { 9
891b0 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 66 7535575 };..#def
891c0 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c 28 ine mem3 GLOBAL(
891d0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
891e0 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20 l, mem3)../*.**
891f0 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b Unlink the chunk
89200 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 at mem3.aPool[i
89210 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 ] from list it i
89220 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f s currently.** o
89230 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 n. *pRoot is th
89240 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 73 e list that i is
89250 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f a member of..*/
89260 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
89270 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 sys3UnlinkFromLi
89280 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
89290 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 78 Root){. u32 nex
892a0 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t = mem3.aPool[i
892b0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 ].u.list.next;.
892c0 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33 u32 prev = mem3
892d0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
892e0 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28 .prev;. assert(
892f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
89300 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
89310 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d 30 );. if( prev==0
89320 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 3d ){. *pRoot =
89330 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a next;. }else{.
89340 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 mem3.aPool[p
89350 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 rev].u.list.next
89360 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 = next;. }. i
89370 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 6d f( next ){. m
89380 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e em3.aPool[next].
89390 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72 u.list.prev = pr
893a0 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e 61 ev;. }. mem3.a
893b0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
893c0 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e ext = 0;. mem3.
893d0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
893e0 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a prev = 0;.}../*.
893f0 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 ** Unlink the ch
89400 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66 unk at index i f
89410 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 72 rom .** whatever
89420 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 list is current
89430 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a ly a member of..
89440 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
89450 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 32 emsys3Unlink(u32
89460 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c i){. u32 size,
89470 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28 hash;. assert(
89480 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
89490 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
894a0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
894b0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
894c0 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d hdr.size4x & 1)=
894d0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
894e0 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d i>=1 );. size =
894f0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
89500 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b .u.hdr.size4x/4;
89510 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d . assert( size=
89520 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 =mem3.aPool[i+si
89530 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
89540 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Size );. assert
89550 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 ( size>=2 );. i
89560 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d f( size <= MX_SM
89570 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 ALL ){. memsy
89580 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 s3UnlinkFromList
89590 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c (i, &mem3.aiSmal
895a0 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 l[size-2]);. }e
895b0 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 lse{. hash =
895c0 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20 size % N_HASH;.
895d0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
895e0 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d FromList(i, &mem
895f0 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 3.aiHash[hash]);
89600 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 . }.}../*.** Li
89610 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
89620 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f mem3.aPool[i] so
89630 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 that is on the
89640 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61 list rooted.** a
89650 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 t *pRoot..*/.sta
89660 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
89670 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32 LinkIntoList(u32
89680 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b i, u32 *pRoot){
89690 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
896a0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
896b0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d m3.mutex) );. m
896c0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
896d0 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f ist.next = *pRoo
896e0 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b t;. mem3.aPool[
896f0 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d i].u.list.prev =
89700 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f 74 0;. if( *pRoot
89710 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ){. mem3.aPo
89720 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73 ol[*pRoot].u.lis
89730 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a t.prev = i;. }.
89740 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a *pRoot = i;.}.
89750 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 ./*.** Link the
89760 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 chunk at index i
89770 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 65 into either the
89780 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
89790 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 74 small chunk list
897a0 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c 61 , or into the la
897b0 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74 rge chunk hash t
897c0 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 able..*/.static
897d0 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b void memsys3Link
897e0 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 (u32 i){. u32 s
897f0 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 ize, hash;. ass
89800 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
89810 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
89820 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
89830 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 i>=1 );. asser
89840 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
89850 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89860 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 69 & 1)==0 );. si
89870 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
89880 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
89890 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 x/4;. assert( s
898a0 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ize==mem3.aPool[
898b0 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e i+size-1].u.hdr.
898c0 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 prevSize );. as
898d0 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b sert( size>=2 );
898e0 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d . if( size <= M
898f0 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d X_SMALL ){. m
89900 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
89910 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d st(i, &mem3.aiSm
89920 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 all[size-2]);.
89930 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 }else{. hash
89940 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b = size % N_HASH;
89950 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
89960 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
89970 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 3.aiHash[hash]);
89980 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
89990 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 the STATIC_MEM
899a0 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 mutex is not alr
899b0 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 eady held, obtai
899c0 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 n it now. The mu
899d0 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 tex.** will alre
899e0 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 ady be held (obt
899f0 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e ained by code in
89a00 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a malloc.c) if.**
89a10 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
89a20 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 nfig.bMemStat is
89a30 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
89a40 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e 74 void memsys3Ent
89a50 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 er(void){. if(
89a60 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
89a70 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 fig.bMemstat==0
89a80 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d 30 && mem3.mutex==0
89a90 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 74 ){. mem3.mut
89aa0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
89ab0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
89ac0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
89ad0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
89ae0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e utex_enter(mem3.
89af0 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 mutex);.}.static
89b00 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61 void memsys3Lea
89b10 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 ve(void){. sqli
89b20 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
89b30 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a mem3.mutex);.}..
89b40 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 65 /*.** Called whe
89b50 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 n we are unable
89b60 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 6c to satisfy an al
89b70 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 74 location of nByt
89b80 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
89b90 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d id memsys3OutOfM
89ba0 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 29 emory(int nByte)
89bb0 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 6c {. if( !mem3.al
89bc0 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 6d armBusy ){. m
89bd0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 em3.alarmBusy =
89be0 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 1;. assert( s
89bf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
89c00 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
89c10 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
89c20 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 ex_leave(mem3.mu
89c30 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tex);. sqlite
89c40 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
89c50 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c (nByte);. sql
89c60 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
89c70 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem3.mutex);.
89c80 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 mem3.alarmBusy
89c90 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a = 0;. }.}.../*
89ca0 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 61 .** Chunk i is a
89cb0 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 74 free chunk that
89cc0 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b has been unlink
89cd0 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 20 ed. Adjust its
89ce0 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 74 .** size paramet
89cf0 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f 75 ers for check-ou
89d00 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 t and return a p
89d10 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a 2a ointer to the .*
89d20 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 6f * user portion o
89d30 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a f the chunk..*/.
89d40 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
89d50 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 32 sys3Checkout(u32
89d60 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 7b i, u32 nBlock){
89d70 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 65 . u32 x;. asse
89d80 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
89d90 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
89da0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
89db0 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 i>=1 );. assert
89dc0 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 ( mem3.aPool[i-1
89dd0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 ].u.hdr.size4x/4
89de0 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 ==nBlock );. as
89df0 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c sert( mem3.aPool
89e00 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
89e10 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 6c dr.prevSize==nBl
89e20 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 6d ock );. x = mem
89e30 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
89e40 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 6d dr.size4x;. mem
89e50 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
89e60 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f dr.size4x = nBlo
89e70 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29 ck*4 | 1 | (x&2)
89e80 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
89e90 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 +nBlock-1].u.hdr
89ea0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f .prevSize = nBlo
89eb0 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ck;. mem3.aPool
89ec0 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
89ed0 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a dr.size4x |= 2;.
89ee0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e 61 return &mem3.a
89ef0 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a Pool[i];.}../*.*
89f00 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 20 * Carve a piece
89f10 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 6f off of the end o
89f20 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 74 f the mem3.iMast
89f30 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 2a er free chunk..*
89f40 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
89f50 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c er to the new al
89f60 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 69 location. Or, i
89f70 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 f the master chu
89f80 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 nk.** is not lar
89f90 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 72 ge enough, retur
89fa0 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 n 0..*/.static v
89fb0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f 6d oid *memsys3From
89fc0 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f 63 Master(u32 nBloc
89fd0 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 k){. assert( sq
89fe0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
89ff0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
8a000 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 73 assert( mem3.s
8a010 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 zMaster>=nBlock
8a020 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e );. if( nBlock>
8a030 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 =mem3.szMaster-1
8a040 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 ){. /* Use t
8a050 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 he entire master
8a060 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20 */. void *p
8a070 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 = memsys3Checkou
8a080 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20 t(mem3.iMaster,
8a090 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a mem3.szMaster);.
8a0a0 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 mem3.iMaster
8a0b0 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 = 0;. mem3.s
8a0c0 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 zMaster = 0;.
8a0d0 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d mem3.mnMaster =
8a0e0 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 0;. return p
8a0f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
8a100 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 74 * Split the mast
8a110 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 72 er block. Retur
8a120 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a 20 n the tail. */.
8a130 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b 0a u32 newi, x;.
8a140 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 2e newi = mem3.
8a150 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e 73 iMaster + mem3.s
8a160 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 6b zMaster - nBlock
8a170 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 65 ;. assert( ne
8a180 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 wi > mem3.iMaste
8a190 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 2e r+1 );. mem3.
8a1a0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8a1b0 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
8a1c0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 -1].u.hdr.prevSi
8a1d0 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 ze = nBlock;.
8a1e0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
8a1f0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
8a200 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8a210 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 20 size4x |= 2;.
8a220 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 mem3.aPool[newi
8a230 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8a240 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b = nBlock*4 + 1;
8a250 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 . mem3.szMast
8a260 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 er -= nBlock;.
8a270 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 mem3.aPool[new
8a280 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 i-1].u.hdr.prevS
8a290 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 ize = mem3.szMas
8a2a0 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d ter;. x = mem
8a2b0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8a2c0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
8a2d0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d 65 ze4x & 2;. me
8a2e0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
8a2f0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
8a300 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d ize4x = mem3.szM
8a310 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 aster*4 | x;.
8a320 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 if( mem3.szMast
8a330 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 er < mem3.mnMast
8a340 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 er ){. mem3
8a350 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .mnMaster = mem3
8a360 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d .szMaster;. }
8a370 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 . return (voi
8a380 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e d*)&mem3.aPool[n
8a390 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ewi];. }.}../*.
8a3a0 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 ** *pRoot is the
8a3b0 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 20 head of a list
8a3c0 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 6f of free chunks o
8a3d0 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 0a f the same size.
8a3e0 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 20 ** or same size
8a3f0 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 20 hash. In other
8a400 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 73 words, *pRoot is
8a410 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 74 an entry in eit
8a420 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d her.** mem3.aiSm
8a430 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 69 all[] or mem3.ai
8a440 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 Hash[]. .**.**
8a450 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 61 This routine exa
8a460 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 65 mines all entrie
8a470 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 6c s on the given l
8a480 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a ist and tries.**
8a490 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 63 to coalesce eac
8a4a0 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 61 h entries with a
8a4b0 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 75 djacent free chu
8a4c0 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 nks. .**.** If
8a4d0 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20 it sees a chunk
8a4e0 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 that is larger t
8a4f0 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 han mem3.iMaster
8a500 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a , it replaces .*
8a510 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 * the current me
8a520 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20 m3.iMaster with
8a530 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63 the new larger c
8a540 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20 hunk. In order
8a550 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33 for.** this mem3
8a560 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65 .iMaster replace
8a570 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68 ment to work, th
8a580 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d e master chunk m
8a590 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64 ust be.** linked
8a5a0 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 into the hash t
8a5b0 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20 ables. That is
8a5c0 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73 not the normal s
8a5d0 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69 tate of.** affai
8a5e0 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 rs, of course.
8a5f0 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 The calling rout
8a600 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68 ine must link th
8a610 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e e master.** chun
8a620 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e k before invokin
8a630 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 g this routine,
8a640 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b then must unlink
8a650 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a the (possibly.*
8a660 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65 * changed) maste
8a670 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69 r chunk once thi
8a680 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69 s routine has fi
8a690 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 nished..*/.stati
8a6a0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65 c void memsys3Me
8a6b0 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b rge(u32 *pRoot){
8a6c0 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72 . u32 iNext, pr
8a6d0 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a ev, size, i, x;.
8a6e0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
8a6f0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
8a700 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 m3.mutex) );. f
8a710 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30 or(i=*pRoot; i>0
8a720 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20 ; i=iNext){.
8a730 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f iNext = mem3.aPo
8a740 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[i].u.list.nex
8a750 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 t;. size = me
8a760 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8a770 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 hdr.size4x;.
8a780 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 29 assert( (size&1)
8a790 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 ==0 );. if( (
8a7a0 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 20 size&2)==0 ){.
8a7b0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
8a7c0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f kFromList(i, pRo
8a7d0 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ot);. asser
8a7e0 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f t( i > mem3.aPoo
8a7f0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 l[i-1].u.hdr.pre
8a800 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 70 vSize );. p
8a810 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e 61 rev = i - mem3.a
8a820 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8a830 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 20 prevSize;.
8a840 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 20 if( prev==iNext
8a850 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 ){. iNext
8a860 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 = mem3.aPool[pr
8a870 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b ev].u.list.next;
8a880 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
8a890 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 65 emsys3Unlink(pre
8a8a0 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d v);. size =
8a8b0 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 72 i + size/4 - pr
8a8c0 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 ev;. x = me
8a8d0 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d m3.aPool[prev-1]
8a8e0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 .u.hdr.size4x &
8a8f0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 2;. mem3.aP
8a900 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64 ool[prev-1].u.hd
8a910 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 2a r.size4x = size*
8a920 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 4 | x;. mem
8a930 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a 3.aPool[prev+siz
8a940 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 e-1].u.hdr.prevS
8a950 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 ize = size;.
8a960 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 72 memsys3Link(pr
8a970 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 70 ev);. i = p
8a980 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a rev;. }else{.
8a990 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b size /= 4;
8a9a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
8a9b0 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ize>mem3.szMaste
8a9c0 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e r ){. mem3.
8a9d0 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 20 iMaster = i;.
8a9e0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
8a9f0 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 = size;. }.
8aa00 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
8aa10 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 rn a block of me
8aa20 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 mory of at least
8aa30 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e nBytes in size.
8aa40 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 .** Return NULL
8aa50 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a if unable..**.**
8aa60 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
8aa70 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
8aa80 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65 necessary mutexe
8aa90 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a s, if any, are.*
8aaa0 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62 * already held b
8aab0 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 y the caller. He
8aac0 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f nce "Unsafe"..*/
8aad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
8aae0 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys3MallocUnsaf
8aaf0 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 e(int nByte){.
8ab00 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 6c u32 i;. u32 nBl
8ab10 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 65 ock;. u32 toFre
8ab20 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
8ab30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
8ab40 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
8ab50 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
8ab60 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 (Mem3Block)==8 )
8ab70 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 31 ;. if( nByte<=1
8ab80 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 2 ){. nBlock
8ab90 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 2;. }else{.
8aba0 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 nBlock = (nByt
8abb0 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a 20 e + 11)/8;. }.
8abc0 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b 3e assert( nBlock>
8abd0 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 50 =2 );.. /* STEP
8abe0 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f 1:. ** Look fo
8abf0 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 r an entry of th
8ac00 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 69 e correct size i
8ac10 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d 61 n either the sma
8ac20 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61 ll. ** chunk ta
8ac30 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c 61 ble or in the la
8ac40 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74 rge chunk hash t
8ac50 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20 able. This is.
8ac60 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d ** successful m
8ac70 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20 ost of the time
8ac80 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f (about 9 times o
8ac90 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f 0a ut of 10).. */.
8aca0 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20 if( nBlock <=
8acb0 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 MX_SMALL ){.
8acc0 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c i = mem3.aiSmall
8acd0 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20 [nBlock-2];.
8ace0 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 if( i>0 ){.
8acf0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
8ad00 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
8ad10 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 aiSmall[nBlock-2
8ad20 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ]);. return
8ad30 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 memsys3Checkout
8ad40 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 (i, nBlock);.
8ad50 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
8ad60 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f 63 int hash = nBloc
8ad70 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 k % N_HASH;.
8ad80 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 73 for(i=mem3.aiHas
8ad90 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 3d h[hash]; i>0; i=
8ada0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
8adb0 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 list.next){.
8adc0 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c if( mem3.aPool
8add0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
8ade0 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 4x/4==nBlock ){.
8adf0 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 memsys3U
8ae00 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
8ae10 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 &mem3.aiHash[ha
8ae20 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 65 sh]);. re
8ae30 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 turn memsys3Chec
8ae40 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b kout(i, nBlock);
8ae50 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
8ae60 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a }.. /* STEP 2:
8ae70 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 74 . ** Try to sat
8ae80 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 isfy the allocat
8ae90 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 ion by carving a
8aea0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 piece off of th
8aeb0 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 e end. ** of th
8aec0 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 e master chunk.
8aed0 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 6c This step usual
8aee0 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 70 ly works if step
8aef0 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 1 fails.. */.
8af00 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 if( mem3.szMast
8af10 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 er>=nBlock ){.
8af20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 return memsys3
8af30 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63 FromMaster(nBloc
8af40 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53 k);. }... /* S
8af50 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f TEP 3: . ** Lo
8af60 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 op through the e
8af70 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f 6f ntire memory poo
8af80 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 6a l. Coalesce adj
8af90 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a 20 acent free. **
8afa0 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 75 chunks. Recompu
8afb0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 te the master ch
8afc0 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 65 unk as the large
8afd0 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 20 st free chunk..
8afe0 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 61 ** Then try aga
8aff0 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 in to satisfy th
8b000 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 e allocation by
8b010 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 carving a piece
8b020 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 off. ** of the
8b030 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 end of the maste
8b040 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 r chunk. This s
8b050 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 79 tep happens very
8b060 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65 . ** rarely (we
8b070 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 66 hope!). */. f
8b080 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b or(toFree=nBlock
8b090 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d *16; toFree<(mem
8b0a0 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46 3.nPool*16); toF
8b0b0 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 6d ree *= 2){. m
8b0c0 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 emsys3OutOfMemor
8b0d0 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 69 y(toFree);. i
8b0e0 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 f( mem3.iMaster
8b0f0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 ){. memsys3
8b100 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 Link(mem3.iMaste
8b110 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 r);. mem3.i
8b120 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
8b130 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 mem3.szMaster
8b140 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 = 0;. }. f
8b150 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 or(i=0; i<N_HASH
8b160 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 ; i++){. me
8b170 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 msys3Merge(&mem3
8b180 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 20 .aiHash[i]);.
8b190 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 }. for(i=0;
8b1a0 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b i<MX_SMALL-1; i+
8b1b0 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 +){. memsys
8b1c0 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 53 3Merge(&mem3.aiS
8b1d0 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a mall[i]);. }.
8b1e0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d if( mem3.szM
8b1f0 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
8b200 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d emsys3Unlink(mem
8b210 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 3.iMaster);.
8b220 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 if( mem3.szMas
8b230 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 ter>=nBlock ){.
8b240 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 return me
8b250 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 msys3FromMaster(
8b260 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d nBlock);. }
8b270 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
8b280 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
8b290 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68 above worked, th
8b2a0 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20 en we fail. */.
8b2b0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
8b2c0 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73 .** Free an outs
8b2d0 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 tanding memory a
8b2e0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
8b2f0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
8b300 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
8b310 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65 necessary mutexe
8b320 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a s, if any, are.*
8b330 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62 * already held b
8b340 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 y the caller. He
8b350 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f nce "Unsafe"..*/
8b360 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65 .void memsys3Fre
8b370 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f eUnsafe(void *pO
8b380 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b ld){. Mem3Block
8b390 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b *p = (Mem3Block
8b3a0 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b *)pOld;. int i;
8b3b0 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a . u32 size, x;.
8b3c0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8b3d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
8b3e0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
8b3f0 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f sert( p>mem3.aPo
8b400 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50 ol && p<&mem3.aP
8b410 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 ool[mem3.nPool]
8b420 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d );. i = p - mem
8b430 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 3.aPool;. asser
8b440 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
8b450 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8b460 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65 &1)==1 );. size
8b470 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
8b480 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
8b490 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73 4;. assert( i+s
8b4a0 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b ize<=mem3.nPool+
8b4b0 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 1 );. mem3.aPoo
8b4c0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8b4d0 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d e4x &= ~1;. mem
8b4e0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 3.aPool[i+size-1
8b4f0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
8b500 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e = size;. mem3.
8b510 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
8b520 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 u.hdr.size4x &=
8b530 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e ~2;. memsys3Lin
8b540 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 k(i);.. /* Try
8b550 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d 61 to expand the ma
8b560 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 6e ster using the n
8b570 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e 6b ewly freed chunk
8b580 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69 */. if( mem3.i
8b590 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68 Master ){. wh
8b5a0 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c ile( (mem3.aPool
8b5b0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
8b5c0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 .u.hdr.size4x&2)
8b5d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a ==0 ){. siz
8b5e0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d e = mem3.aPool[m
8b5f0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8b600 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 .hdr.prevSize;.
8b610 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 mem3.iMaste
8b620 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 r -= size;.
8b630 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b mem3.szMaster +
8b640 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 = size;. me
8b650 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 msys3Unlink(mem3
8b660 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 .iMaster);.
8b670 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
8b680 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
8b690 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 u.hdr.size4x & 2
8b6a0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f ;. mem3.aPo
8b6b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
8b6c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8b6d0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a = mem3.szMaster*
8b6e0 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 4 | x;. mem
8b6f0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8b700 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
8b710 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 er-1].u.hdr.prev
8b720 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 Size = mem3.szMa
8b730 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ster;. }.
8b740 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d x = mem3.aPool[m
8b750 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8b760 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b .hdr.size4x & 2;
8b770 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d . while( (mem
8b780 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8b790 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
8b7a0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8b7b0 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 4x&1)==0 ){.
8b7c0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 memsys3Unlink(
8b7d0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
8b7e0 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 3.szMaster);.
8b7f0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
8b800 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d += mem3.aPool[m
8b810 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8b820 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8b830 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20 dr.size4x/4;.
8b840 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
8b850 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
8b860 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d hdr.size4x = mem
8b870 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3.szMaster*4 | x
8b880 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f ;. mem3.aPo
8b890 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
8b8a0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
8b8b0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
8b8c0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
8b8d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
8b8e0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
8b8f0 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 ize of an outsta
8b900 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e nding allocation
8b910 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 , in bytes. The
8b920 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 .** size returne
8b930 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 d omits the 8-by
8b940 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 te header overhe
8b950 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a ad. This only.*
8b960 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e * works for chun
8b970 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 ks that are curr
8b980 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 ently checked ou
8b990 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
8b9a0 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69 memsys3Size(voi
8b9b0 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f d *p){. Mem3Blo
8b9c0 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 ck *pBlock;. if
8b9d0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
8b9e0 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 0;. pBlock = (M
8b9f0 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 em3Block*)p;. a
8ba00 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d ssert( (pBlock[-
8ba10 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
8ba20 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 1)!=0 );. retur
8ba30 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e n (pBlock[-1].u.
8ba40 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32 hdr.size4x&~3)*2
8ba50 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 - 4;.}../*.** R
8ba60 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 ound up a reques
8ba70 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 t size to the ne
8ba80 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 xt valid allocat
8ba90 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 ion size..*/.sta
8baa0 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 52 tic int memsys3R
8bab0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 oundup(int n){.
8bac0 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 if( n<=12 ){.
8bad0 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d return 12;. }
8bae0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
8baf0 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34 ((n+11)&~7) - 4
8bb00 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
8bb10 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f llocate nBytes o
8bb20 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 f memory..*/.sta
8bb30 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 tic void *memsys
8bb40 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 3Malloc(int nByt
8bb50 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 es){. sqlite3_i
8bb60 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 nt64 *p;. asser
8bb70 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 20 t( nBytes>0 );
8bb80 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f /* mallo
8bb90 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 20 c.c filters out
8bba0 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 20 0 byte requests
8bbb0 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 */. memsys3Ente
8bbc0 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 r();. p = memsy
8bbd0 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e s3MallocUnsafe(n
8bbe0 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73 Bytes);. memsys
8bbf0 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 3Leave();. retu
8bc00 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a rn (void*)p; .}.
8bc10 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
8bc20 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 ry..*/.void mems
8bc30 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 50 ys3Free(void *pP
8bc40 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 rior){. assert(
8bc50 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 6d pPrior );. mem
8bc60 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6d sys3Enter();. m
8bc70 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 emsys3FreeUnsafe
8bc80 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 (pPrior);. mems
8bc90 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f ys3Leave();.}../
8bca0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
8bcb0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
8bcc0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
8bcd0 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d ation.*/.void *m
8bce0 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 6f emsys3Realloc(vo
8bcf0 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 id *pPrior, int
8bd00 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e nBytes){. int n
8bd10 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a Old;. void *p;.
8bd20 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 if( pPrior==0
8bd30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 ){. return sq
8bd40 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 lite3_malloc(nBy
8bd50 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 tes);. }. if(
8bd60 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 nBytes<=0 ){.
8bd70 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 sqlite3_free(pP
8bd80 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72 rior);. retur
8bd90 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 n 0;. }. nOld
8bda0 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 50 = memsys3Size(pP
8bdb0 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 rior);. if( nBy
8bdc0 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 79 tes<=nOld && nBy
8bdd0 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b tes>=nOld-128 ){
8bde0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 . return pPri
8bdf0 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 or;. }. memsys
8be00 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 3Enter();. p =
8be10 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 memsys3MallocUns
8be20 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 afe(nBytes);. i
8be30 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 f( p ){. if(
8be40 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20 nOld<nBytes ){.
8be50 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 memcpy(p, p
8be60 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
8be70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d }else{. m
8be80 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c emcpy(p, pPrior,
8be90 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a nBytes);. }.
8bea0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 memsys3FreeU
8beb0 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 nsafe(pPrior);.
8bec0 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 }. memsys3Leav
8bed0 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b e();. return p;
8bee0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
8bef0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
8bf00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8bf10 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 64 memsys3Init(void
8bf20 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
8bf30 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
8bf40 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 21 otUsed);. if( !
8bf50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8bf60 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 20 fig.pHeap ){.
8bf70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
8bf80 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 RROR;. }.. /*
8bf90 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 Store a pointer
8bfa0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c to the memory bl
8bfb0 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74 ock in global st
8bfc0 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f ructure mem3. */
8bfd0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f . assert( sizeo
8bfe0 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 f(Mem3Block)==8
8bff0 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20 );. mem3.aPool
8c000 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73 = (Mem3Block *)s
8c010 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
8c020 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d 33 ig.pHeap;. mem3
8c030 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 65 .nPool = (sqlite
8c040 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 3GlobalConfig.nH
8c050 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 6d eap / sizeof(Mem
8c060 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a 20 3Block)) - 2;..
8c070 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
8c080 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e he master block.
8c090 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 */. mem3.szMas
8c0a0 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c ter = mem3.nPool
8c0b0 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 ;. mem3.mnMaste
8c0c0 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 r = mem3.szMaste
8c0d0 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 r;. mem3.iMaste
8c0e0 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 r = 1;. mem3.aP
8c0f0 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a ool[0].u.hdr.siz
8c100 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61 e4x = (mem3.szMa
8c110 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 20 ster<<2) + 2;.
8c120 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8c130 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 65 nPool].u.hdr.pre
8c140 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f vSize = mem3.nPo
8c150 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ol;. mem3.aPool
8c160 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 [mem3.nPool].u.h
8c170 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 0a dr.size4x = 1;..
8c180 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
8c190 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
8c1a0 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d nitialize this m
8c1b0 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 odule..*/.static
8c1c0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 75 void memsys3Shu
8c1d0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
8c1e0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
8c1f0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
8c200 29 3b 0a 20 20 6d 65 6d 33 2e 6d 75 74 65 78 20 );. mem3.mutex
8c210 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d = 0;. return;.}
8c220 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 ..../*.** Open t
8c230 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 he file indicate
8c240 64 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f d and write a lo
8c250 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 g of all unfreed
8c260 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f memory .** allo
8c270 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 cations into tha
8c280 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 t log..*/.SQLITE
8c290 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
8c2a0 6c 69 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 lite3Memsys3Dump
8c2b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
8c2c0 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 lename){.#ifdef
8c2d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 SQLITE_DEBUG. F
8c2e0 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 ILE *out;. u32
8c2f0 69 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 i, j;. u32 size
8c300 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d ;. if( zFilenam
8c310 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d e==0 || zFilenam
8c320 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f e[0]==0 ){. o
8c330 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d ut = stdout;. }
8c340 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 else{. out =
8c350 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c fopen(zFilename,
8c360 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f "w");. if( o
8c370 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 ut==0 ){. f
8c380 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
8c390 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 ** Unable to out
8c3a0 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 put memory debug
8c3b0 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 output log: %s
8c3c0 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 **\n",.
8c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 zFi
8c3e0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 lename);. r
8c3f0 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d eturn;. }. }
8c400 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 . memsys3Enter(
8c410 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
8c420 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a , "CHUNKS:\n");.
8c430 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 for(i=1; i<=me
8c440 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a m3.nPool; i+=siz
8c450 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d e/4){. size =
8c460 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
8c470 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 .u.hdr.size4x;.
8c480 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 if( size/4<=1
8c490 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
8c4a0 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 f(out, "%p size
8c4b0 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e error\n", &mem3.
8c4c0 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 aPool[i]);.
8c4d0 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 assert( 0 );.
8c4e0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
8c4f0 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 . if( (size&1
8c500 29 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f )==0 && mem3.aPo
8c510 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 ol[i+size/4-1].u
8c520 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 .hdr.prevSize!=s
8c530 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 ize/4 ){. f
8c540 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 printf(out, "%p
8c550 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e tail size does n
8c560 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 ot match\n", &me
8c570 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 m3.aPool[i]);.
8c580 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b assert( 0 );
8c590 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
8c5a0 20 20 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 }. if( ((me
8c5b0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f m3.aPool[i+size/
8c5c0 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 4-1].u.hdr.size4
8c5d0 78 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 x&2)>>1)!=(size&
8c5e0 31 29 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 1) ){. fpri
8c5f0 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 ntf(out, "%p tai
8c600 6c 20 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 l checkout bit i
8c610 73 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 s incorrect\n",
8c620 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b &mem3.aPool[i]);
8c630 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 . assert( 0
8c640 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
8c650 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
8c660 69 7a 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 ize&1 ){. f
8c670 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 printf(out, "%p
8c680 25 36 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 %6d bytes checke
8c690 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e d out\n", &mem3.
8c6a0 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f aPool[i], (size/
8c6b0 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 4)*8-8);. }el
8c6c0 73 65 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 se{. fprint
8c6d0 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 f(out, "%p %6d b
8c6e0 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 ytes free%s\n",
8c6f0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 &mem3.aPool[i],
8c700 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 (size/4)*8-8,.
8c710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8c720 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 i==mem3.iMaster
8c730 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 ? " **master**"
8c740 3a 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d : "");. }. }
8c750 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 . for(i=0; i<MX
8c760 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a _SMALL-1; i++){.
8c770 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 if( mem3.aiS
8c780 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e mall[i]==0 ) con
8c790 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e tinue;. fprin
8c7a0 74 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 tf(out, "small(%
8c7b0 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 2d):", i);. f
8c7c0 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d or(j = mem3.aiSm
8c7d0 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d all[i]; j>0; j=m
8c7e0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c em3.aPool[j].u.l
8c7f0 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 ist.next){.
8c800 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
8c810 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 %p(%d)", &mem3.a
8c820 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 Pool[j],.
8c830 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f (mem3.aPo
8c840 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[j-1].u.hdr.si
8c850 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 ze4x/4)*8-8);.
8c860 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 }. fprintf(
8c870 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d out, "\n"); . }
8c880 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f . for(i=0; i<N_
8c890 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 HASH; i++){.
8c8a0 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b if( mem3.aiHash[
8c8b0 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 i]==0 ) continue
8c8c0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
8c8d0 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c t, "hash(%2d):",
8c8e0 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d i);. for(j =
8c8f0 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b mem3.aiHash[i];
8c900 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f j>0; j=mem3.aPo
8c910 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[j].u.list.nex
8c920 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 t){. fprint
8c930 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 f(out, " %p(%d)"
8c940 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d , &mem3.aPool[j]
8c950 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
8c960 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d (mem3.aPool[j-1]
8c970 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 .u.hdr.size4x/4)
8c980 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 *8-8);. }.
8c990 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c fprintf(out, "\
8c9a0 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 n"); . }. fpri
8c9b0 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 ntf(out, "master
8c9c0 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 =%d\n", mem3.iMa
8c9d0 73 74 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 ster);. fprintf
8c9e0 28 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 (out, "nowUsed=%
8c9f0 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c d\n", mem3.nPool
8ca00 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 *8 - mem3.szMast
8ca10 65 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 er*8);. fprintf
8ca20 28 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 (out, "mxUsed=%d
8ca30 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a \n", mem3.nPool*
8ca40 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 8 - mem3.mnMaste
8ca50 72 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f r*8);. sqlite3_
8ca60 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 mutex_leave(mem3
8ca70 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f .mutex);. if( o
8ca80 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 ut==stdout ){.
8ca90 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 fflush(stdout)
8caa0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 ;. }else{. f
8cab0 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a close(out);. }.
8cac0 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 #else. UNUSED_P
8cad0 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 ARAMETER(zFilena
8cae0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f me);.#endif.}../
8caf0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
8cb00 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f e is the only ro
8cb10 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 utine in this fi
8cb20 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c le with external
8cb30 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a .** linkage..**
8cb40 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 .** Populate the
8cb50 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
8cb60 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
8cb70 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 ction pointers i
8cb80 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 n.** sqlite3Glob
8cb90 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 alConfig.m with
8cba0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 pointers to the
8cbb0 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 routines in this
8cbc0 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 file. The.** ar
8cbd0 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 guments specify
8cbe0 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d the block of mem
8cbf0 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a ory to manage..*
8cc00 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
8cc10 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 e is only called
8cc20 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 by sqlite3_conf
8cc30 69 67 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 ig(), and theref
8cc40 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 ore.** is not re
8cc50 71 75 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 quired to be thr
8cc60 65 61 64 73 61 66 65 20 28 69 74 20 69 73 20 6e eadsafe (it is n
8cc70 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ot)..*/.SQLITE_P
8cc80 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c RIVATE const sql
8cc90 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
8cca0 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d *sqlite3MemGetM
8ccb0 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 emsys3(void){.
8ccc0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
8ccd0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
8cce0 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 mempoolMethods
8ccf0 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 = {. memsys3
8cd00 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d Malloc,. mem
8cd10 73 79 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d sys3Free,. m
8cd20 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 emsys3Realloc,.
8cd30 20 20 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c memsys3Size,
8cd40 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 . memsys3Rou
8cd50 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 ndup,. memsy
8cd60 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d s3Init,. mem
8cd70 73 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 sys3Shutdown,.
8cd80 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 0. };. retu
8cd90 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f rn &mempoolMetho
8cda0 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a ds;.}..#endif /*
8cdb0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
8cdc0 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EMSYS3 */../****
8cdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
8cde0 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a f mem3.c *******
8cdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ce00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ce10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
8ce20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
8ce30 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a file mem5.c ***
8ce40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ce50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ce60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
8ce70 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 2007 October 14
8ce80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
8ce90 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
8cea0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
8ceb0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
8cec0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
8ced0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
8cee0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
8cef0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
8cf00 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
8cf10 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
8cf20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
8cf30 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
8cf40 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
8cf50 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
8cf60 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
8cf70 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
8cf80 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
8cf90 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
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 2a ****************
8cfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
8cfe0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
8cff0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
8d000 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
8d010 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a ment a memory.**
8d020 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
8d030 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 ystem for use by
8d040 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 SQLite. .**.**
8d050 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
8d060 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
8d070 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
8d080 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 omits all.** use
8d090 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 of malloc(). Th
8d0a0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 67 69 e application gi
8d0b0 76 65 73 20 53 51 4c 69 74 65 20 61 20 62 6c 6f ves SQLite a blo
8d0c0 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ck of memory.**
8d0d0 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 before calling s
8d0e0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
8d0f0 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 e() from which a
8d100 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 llocations.** ar
8d110 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 e made and retur
8d120 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c ned by the xMall
8d130 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f oc() and xReallo
8d140 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e c() .** implemen
8d150 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 tations. Once sq
8d160 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
8d170 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
8d180 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e ed,.** the amoun
8d190 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 t of memory avai
8d1a0 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 lable to SQLite
8d1b0 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e is fixed and can
8d1c0 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 not.** be change
8d1d0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 d..**.** This ve
8d1e0 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d rsion of the mem
8d1f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
8d200 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c ubsystem is incl
8d210 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 uded.** in the b
8d220 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c uild only if SQL
8d230 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
8d240 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a S5 is defined..*
8d250 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 6d 6f 72 79 *.** This memory
8d260 20 61 6c 6c 6f 63 61 74 6f 72 20 75 73 65 73 20 allocator uses
8d270 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c the following al
8d280 67 6f 72 69 74 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 gorithm:.**.**
8d290 20 31 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72 79 20 1. All memory
8d2a0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 73 69 7a 65 allocations size
8d2b0 73 20 61 72 65 20 72 6f 75 6e 64 65 64 20 75 70 s are rounded up
8d2c0 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 to a power of 2
8d2d0 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 49 66 ..**.** 2. If
8d2e0 20 74 77 6f 20 61 64 6a 61 63 65 6e 74 20 66 72 two adjacent fr
8d2f0 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20 74 68 ee blocks are th
8d300 65 20 68 61 6c 76 65 73 20 6f 66 20 61 20 6c 61 e halves of a la
8d310 72 67 65 72 20 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 rger block,.**
8d320 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 74 77 then the tw
8d330 6f 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 61 o blocks are coa
8d340 6c 65 73 65 64 20 69 6e 74 6f 20 74 68 65 20 73 lesed into the s
8d350 69 6e 67 6c 65 20 6c 61 72 67 65 72 20 62 6c 6f ingle larger blo
8d360 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 ck..**.** 3.
8d370 4e 65 77 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c New memory is al
8d380 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20 74 68 65 located from the
8d390 20 66 69 72 73 74 20 61 76 61 69 6c 61 62 6c 65 first available
8d3a0 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a free block..**.
8d3b0 2a 2a 20 54 68 69 73 20 61 6c 67 6f 72 69 74 68 ** This algorith
8d3c0 6d 20 69 73 20 64 65 73 63 72 69 62 65 64 20 69 m is described i
8d3d0 6e 3a 20 4a 2e 20 4d 2e 20 52 6f 62 73 6f 6e 2e n: J. M. Robson.
8d3e0 20 22 42 6f 75 6e 64 73 20 66 6f 72 20 53 6f 6d "Bounds for Som
8d3f0 65 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 43 e Functions.** C
8d400 6f 6e 63 65 72 6e 69 6e 67 20 44 79 6e 61 6d 69 oncerning Dynami
8d410 63 20 53 74 6f 72 61 67 65 20 41 6c 6c 6f 63 61 c Storage Alloca
8d420 74 69 6f 6e 22 2e 20 4a 6f 75 72 6e 61 6c 20 6f tion". Journal o
8d430 66 20 74 68 65 20 41 73 73 6f 63 69 61 74 69 6f f the Associatio
8d440 6e 20 66 6f 72 0a 2a 2a 20 43 6f 6d 70 75 74 69 n for.** Computi
8d450 6e 67 20 4d 61 63 68 69 6e 65 72 79 2c 20 56 6f ng Machinery, Vo
8d460 6c 75 6d 65 20 32 31 2c 20 4e 75 6d 62 65 72 20 lume 21, Number
8d470 38 2c 20 4a 75 6c 79 20 31 39 37 34 2c 20 70 61 8, July 1974, pa
8d480 67 65 73 20 34 39 31 2d 34 39 39 2e 0a 2a 2a 20 ges 491-499..**
8d490 0a 2a 2a 20 4c 65 74 20 6e 20 62 65 20 74 68 65 .** Let n be the
8d4a0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
8d4b0 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 gest allocation
8d4c0 64 69 76 69 64 65 64 20 62 79 20 74 68 65 20 6d divided by the m
8d4d0 69 6e 69 6d 75 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 inimum.** alloca
8d4e0 74 69 6f 6e 20 73 69 7a 65 20 28 61 66 74 65 72 tion size (after
8d4f0 20 72 6f 75 6e 64 69 6e 67 20 61 6c 6c 20 73 69 rounding all si
8d500 7a 65 73 20 75 70 20 74 6f 20 61 20 70 6f 77 65 zes up to a powe
8d510 72 20 6f 66 20 32 2e 29 20 20 4c 65 74 20 4d 0a r of 2.) Let M.
8d520 2a 2a 20 62 65 20 74 68 65 20 6d 61 78 69 6d 75 ** be the maximu
8d530 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f m amount of memo
8d540 72 79 20 65 76 65 72 20 6f 75 74 73 74 61 6e 64 ry ever outstand
8d550 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e ing at one time.
8d560 20 20 4c 65 74 0a 2a 2a 20 4e 20 62 65 20 74 68 Let.** N be th
8d570 65 20 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f e total amount o
8d580 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 f memory availab
8d590 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f le for allocatio
8d5a0 6e 2e 20 20 52 6f 62 73 6f 6e 0a 2a 2a 20 70 72 n. Robson.** pr
8d5b0 6f 76 65 64 20 74 68 61 74 20 74 68 69 73 20 6d oved that this m
8d5c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
8d5d0 77 69 6c 6c 20 6e 65 76 65 72 20 62 72 65 61 6b will never break
8d5e0 64 6f 77 6e 20 64 75 65 20 74 6f 20 0a 2a 2a 20 down due to .**
8d5f0 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 73 fragmentation as
8d600 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 66 6f 6c long as the fol
8d610 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e lowing constrain
8d620 74 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 t holds:.**.**
8d630 20 20 20 20 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b N >= M*(1 +
8d640 20 6c 6f 67 32 28 6e 29 2f 32 29 20 2d 20 6e 20 log2(n)/2) - n
8d650 2b 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 + 1.**.** The sq
8d660 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20 6c lite3_status() l
8d670 6f 67 69 63 20 74 72 61 63 6b 73 20 74 68 65 20 ogic tracks the
8d680 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 73 20 6f maximum values o
8d690 66 20 6e 20 61 6e 64 20 4d 20 73 6f 0a 2a 2a 20 f n and M so.**
8d6a0 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74 that an applicat
8d6b0 69 6f 6e 20 63 61 6e 2c 20 61 74 20 61 6e 79 20 ion can, at any
8d6c0 74 69 6d 65 2c 20 76 65 72 69 66 79 20 74 68 69 time, verify thi
8d6d0 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f s constraint..*/
8d6e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
8d6f0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
8d700 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
8d710 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a used only when .
8d720 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ** SQLITE_ENABLE
8d730 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 _MEMSYS5 is defi
8d740 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ned..*/.#ifdef S
8d750 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
8d760 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 SYS5../*.** A mi
8d770 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e nimum allocation
8d780 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
8d790 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
8d7a0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c structure..** L
8d7b0 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e arger allocation
8d7c0 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f s are an array o
8d7d0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
8d7e0 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 es where the.**
8d7f0 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 size of the arra
8d800 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 y is a power of
8d810 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 2..**.** The siz
8d820 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
8d830 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
8d840 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 66 of two. That f
8d850 61 63 74 20 69 73 0a 2a 2a 20 76 65 72 69 66 69 act is.** verifi
8d860 65 64 20 69 6e 20 6d 65 6d 73 79 73 35 49 6e 69 ed in memsys5Ini
8d870 74 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 t()..*/.typedef
8d880 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 struct Mem5Link
8d890 4d 65 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 Mem5Link;.struct
8d8a0 20 4d 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e Mem5Link {. in
8d8b0 74 20 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a t next; /*
8d8c0 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 Index of next f
8d8d0 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 ree chunk */. i
8d8e0 6e 74 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f nt prev; /
8d8f0 2a 20 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 * Index of previ
8d900 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a ous free chunk *
8d910 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 /.};../*.** Maxi
8d920 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 mum size of any
8d930 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 allocation is ((
8d940 31 3c 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 1<<LOGMAX)*mem5.
8d950 73 7a 41 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a szAtom). Since.*
8d960 2a 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 69 73 * mem5.szAtom is
8d970 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 always at least
8d980 20 38 20 61 6e 64 20 33 32 2d 62 69 74 20 69 6e 8 and 32-bit in
8d990 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 2c tegers are used,
8d9a0 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 61 63 .** it is not ac
8d9b0 74 75 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 tually possible
8d9c0 74 6f 20 72 65 61 63 68 20 74 68 69 73 20 6c 69 to reach this li
8d9d0 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 mit..*/.#define
8d9e0 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a LOGMAX 30../*.**
8d9f0 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f 72 20 Masks used for
8da00 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 6c 65 mem5.aCtrl[] ele
8da10 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e ments..*/.#defin
8da20 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 20 20 e CTRL_LOGSIZE
8da30 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67 32 20 0x1f /* Log2
8da40 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62 6c 6f Size of this blo
8da50 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 ck */.#define CT
8da60 52 4c 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 RL_FREE 0x20
8da70 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e /* True if n
8da80 6f 74 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a ot checked out *
8da90 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 /../*.** All of
8daa0 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 the static varia
8dab0 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69 bles used by thi
8dac0 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c s module are col
8dad0 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 lected.** into a
8dae0 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 single structur
8daf0 65 20 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 e named "mem5".
8db00 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 This is to keep
8db10 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 the.** static v
8db20 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a ariables organiz
8db30 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 ed and to reduce
8db40 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 namespace pollu
8db50 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 tion.** when thi
8db60 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 s module is comb
8db70 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 ined with other
8db80 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 in the amalgamat
8db90 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 ion..*/.static S
8dba0 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
8dbb0 20 4d 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 Mem5Global {.
8dbc0 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 /*. ** Memory a
8dbd0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c vailable for all
8dbe0 6f 63 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 ocation. */. i
8dbf0 6e 74 20 73 7a 41 74 6f 6d 3b 20 20 20 20 20 20 nt szAtom;
8dc00 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 /* Smallest poss
8dc10 69 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ible allocation
8dc20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e in bytes */. in
8dc30 74 20 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f t nBlock; /
8dc40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 7a 41 74 * Number of szAt
8dc50 6f 6d 20 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 om sized blocks
8dc60 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 in zPool */. u8
8dc70 20 2a 7a 50 6f 6f 6c 3b 20 20 20 20 20 20 20 2f *zPool; /
8dc80 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 * Memory availab
8dc90 6c 65 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 le to be allocat
8dca0 65 64 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 ed */. . /*.
8dcb0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 ** Mutex to cont
8dcc0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 rol access to th
8dcd0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
8dce0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 ion subsystem..
8dcf0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
8dd00 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f tex *mutex;.. /
8dd10 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e *. ** Performan
8dd20 63 65 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 ce statistics.
8dd30 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b */. u64 nAlloc;
8dd40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
8dd50 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c l number of call
8dd60 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 s to malloc */.
8dd70 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b u64 totalAlloc;
8dd80 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 /* Total of
8dd90 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c all malloc call
8dda0 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 s - includes int
8ddb0 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 ernal frag */.
8ddc0 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b u64 totalExcess;
8ddd0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 /* Total int
8dde0 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 ernal fragmentat
8ddf0 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 ion */. u32 cur
8de00 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 rentOut; /*
8de10 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 Current checkout
8de20 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 , including inte
8de30 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 rnal fragmentati
8de40 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 on */. u32 curr
8de50 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 entCount; /* C
8de60 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
8de70 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f distinct checko
8de80 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 uts */. u32 max
8de90 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Out; /*
8dea0 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 Maximum instanta
8deb0 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 neous currentOut
8dec0 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 */. u32 maxCou
8ded0 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 nt; /* Max
8dee0 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f imum instantaneo
8def0 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 us currentCount
8df00 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 */. u32 maxRequ
8df10 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 est; /* Larg
8df20 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 est allocation (
8df30 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 exclusive of int
8df40 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 ernal frag) */.
8df50 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 . /*. ** List
8df60 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 s of free blocks
8df70 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b 30 5d . aiFreelist[0]
8df80 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 66 72 is a list of fr
8df90 65 65 20 62 6c 6f 63 6b 73 20 6f 66 0a 20 20 2a ee blocks of. *
8dfa0 2a 20 73 69 7a 65 20 6d 65 6d 35 2e 73 7a 41 74 * size mem5.szAt
8dfb0 6f 6d 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b om. aiFreelist[
8dfc0 31 5d 20 68 6f 6c 64 73 20 62 6c 6f 63 6b 73 20 1] holds blocks
8dfd0 6f 66 20 73 69 7a 65 20 73 7a 41 74 6f 6d 2a 32 of size szAtom*2
8dfe0 2e 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 6f .. ** and so fo
8dff0 72 74 68 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 rth.. */. int
8e000 61 69 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 aiFreelist[LOGMA
8e010 58 2b 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a X+1];.. /*. **
8e020 20 53 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b Space for track
8e030 69 6e 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 ing which blocks
8e040 20 61 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 are checked out
8e050 20 61 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 and the size.
8e060 2a 2a 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b ** of each block
8e070 2e 20 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 . One byte per
8e080 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 block.. */. u8
8e090 20 2a 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 *aCtrl;..} mem5
8e0a0 20 3d 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a = { 0 };../*.**
8e0b0 20 41 63 63 65 73 73 20 74 68 65 20 73 74 61 74 Access the stat
8e0c0 69 63 20 76 61 72 69 61 62 6c 65 20 74 68 72 6f ic variable thro
8e0d0 75 67 68 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 ugh a macro for
8e0e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a SQLITE_OMIT_WSD.
8e0f0 2a 2f 0a 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 */.#define mem5
8e100 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 GLOBAL(struct Me
8e110 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a m5Global, mem5).
8e120 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 ./*.** Assuming
8e130 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73 20 64 69 mem5.zPool is di
8e140 76 69 64 65 64 20 75 70 20 69 6e 74 6f 20 61 6e vided up into an
8e150 20 61 72 72 61 79 20 6f 66 20 4d 65 6d 35 4c 69 array of Mem5Li
8e160 6e 6b 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 nk.** structures
8e170 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 , return a point
8e180 65 72 20 74 6f 20 74 68 65 20 69 64 78 2d 74 68 er to the idx-th
8e190 20 73 75 63 68 20 6c 69 6b 2e 0a 2a 2f 0a 23 64 such lik..*/.#d
8e1a0 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 efine MEM5LINK(i
8e1b0 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a dx) ((Mem5Link *
8e1c0 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 )(&mem5.zPool[(i
8e1d0 64 78 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d dx)*mem5.szAtom]
8e1e0 29 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b ))../*.** Unlink
8e1f0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 the chunk at me
8e200 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d m5.aPool[i] from
8e210 20 6c 69 73 74 20 69 74 20 69 73 20 63 75 72 72 list it is curr
8e220 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 ently.** on. It
8e230 20 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 should be found
8e240 20 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c on mem5.aiFreel
8e250 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a ist[iLogsize]..*
8e260 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8e270 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 msys5Unlink(int
8e280 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 i, int iLogsize)
8e290 7b 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 {. int next, pr
8e2a0 65 76 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e ev;. assert( i>
8e2b0 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c =0 && i<mem5.nBl
8e2c0 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ock );. assert(
8e2d0 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 iLogsize>=0 &&
8e2e0 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 iLogsize<=LOGMAX
8e2f0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d );. assert( (m
8e300 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 em5.aCtrl[i] & C
8e310 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c TRL_LOGSIZE)==iL
8e320 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 ogsize );.. nex
8e330 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d t = MEM5LINK(i)-
8e340 3e 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 >next;. prev =
8e350 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 MEM5LINK(i)->pre
8e360 76 3b 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 v;. if( prev<0
8e370 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 ){. mem5.aiFr
8e380 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d eelist[iLogsize]
8e390 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 = next;. }else
8e3a0 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 {. MEM5LINK(p
8e3b0 72 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 rev)->next = nex
8e3c0 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 t;. }. if( nex
8e3d0 74 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 t>=0 ){. MEM5
8e3e0 4c 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 LINK(next)->prev
8e3f0 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a = prev;. }.}..
8e400 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 /*.** Link the c
8e410 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f hunk at mem5.aPo
8e420 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 ol[i] so that is
8e430 20 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 on the iLogsize
8e440 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a .** free list..*
8e450 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8e460 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c msys5Link(int i,
8e470 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a int iLogsize){.
8e480 20 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 int x;. asser
8e490 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
8e4a0 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 _held(mem5.mutex
8e4b0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 ) );. assert( i
8e4c0 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 >=0 && i<mem5.nB
8e4d0 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 lock );. assert
8e4e0 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 ( iLogsize>=0 &&
8e4f0 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 iLogsize<=LOGMA
8e500 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 X );. assert( (
8e510 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 mem5.aCtrl[i] &
8e520 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 CTRL_LOGSIZE)==i
8e530 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 Logsize );.. x
8e540 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e = MEM5LINK(i)->n
8e550 65 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 ext = mem5.aiFre
8e560 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b elist[iLogsize];
8e570 0a 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e . MEM5LINK(i)->
8e580 70 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 prev = -1;. if(
8e590 20 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 x>=0 ){. ass
8e5a0 65 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f ert( x<mem5.nBlo
8e5b0 63 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 ck );. MEM5LI
8e5c0 4e 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b NK(x)->prev = i;
8e5d0 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 . }. mem5.aiFr
8e5e0 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d eelist[iLogsize]
8e5f0 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 = i;.}../*.** I
8e600 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d f the STATIC_MEM
8e610 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c mutex is not al
8e620 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 ready held, obta
8e630 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d in it now. The m
8e640 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 utex.** will alr
8e650 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 eady be held (ob
8e660 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 tained by code i
8e670 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a n malloc.c) if.*
8e680 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 * sqlite3GlobalC
8e690 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 onfig.bMemStat i
8e6a0 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 s true..*/.stati
8e6b0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e c void memsys5En
8e6c0 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c ter(void){. sql
8e6d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
8e6e0 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a (mem5.mutex);.}.
8e6f0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8e700 79 73 35 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a ys5Leave(void){.
8e710 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
8e720 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 74 65 78 leave(mem5.mutex
8e730 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
8e740 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 rn the size of a
8e750 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c n outstanding al
8e760 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 location, in byt
8e770 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 es. The.** size
8e780 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 returned omits
8e790 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 the 8-byte heade
8e7a0 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 r overhead. Thi
8e7b0 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 s only.** works
8e7c0 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 for chunks that
8e7d0 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 are currently ch
8e7e0 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 ecked out..*/.st
8e7f0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 atic int memsys5
8e800 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Size(void *p){.
8e810 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30 3b 0a int iSize = 0;.
8e820 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
8e830 6e 74 20 69 20 3d 20 28 28 75 38 20 2a 29 70 2d nt i = ((u8 *)p-
8e840 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 mem5.zPool)/mem5
8e850 2e 73 7a 41 74 6f 6d 3b 0a 20 20 20 20 61 73 73 .szAtom;. ass
8e860 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8e870 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8e880 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d 35 2e 73 iSize = mem5.s
8e890 7a 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 28 6d zAtom * (1 << (m
8e8a0 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43 54 52 em5.aCtrl[i]&CTR
8e8b0 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 20 7d L_LOGSIZE));. }
8e8c0 0a 20 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b . return iSize;
8e8d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 .}../*.** Find t
8e8e0 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f he first entry o
8e8f0 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 n the freelist i
8e900 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b Logsize. Unlink
8e910 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61 that.** entry a
8e920 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e nd return its in
8e930 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 dex. .*/.static
8e940 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e int memsys5Unlin
8e950 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73 kFirst(int iLogs
8e960 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ize){. int i;.
8e970 20 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20 int iFirst;..
8e980 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 assert( iLogsize
8e990 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c >=0 && iLogsize<
8e9a0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69 20 3d =LOGMAX );. i =
8e9b0 20 69 46 69 72 73 74 20 3d 20 6d 65 6d 35 2e 61 iFirst = mem5.a
8e9c0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
8e9d0 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 ze];. assert( i
8e9e0 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20 77 68 First>=0 );. wh
8e9f0 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 ile( i>0 ){.
8ea00 69 66 28 20 69 3c 69 46 69 72 73 74 20 29 20 69 if( i<iFirst ) i
8ea10 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 20 20 69 First = i;. i
8ea20 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e = MEM5LINK(i)->
8ea30 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 next;. }. mems
8ea40 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72 73 74 ys5Unlink(iFirst
8ea50 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 72 , iLogsize);. r
8ea60 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a 7d 0a eturn iFirst;.}.
8ea70 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
8ea80 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
8ea90 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74 of at least nByt
8eaa0 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 es in size..** R
8eab0 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e eturn NULL if un
8eac0 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 able. Return NU
8ead0 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d 3d 30 2e LL if nBytes==0.
8eae0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 .**.** The calle
8eaf0 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 r guarantees tha
8eb00 74 20 6e 42 79 74 65 20 70 6f 73 69 74 69 76 65 t nByte positive
8eb10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
8eb20 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 er has obtained
8eb30 61 20 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f a mutex prior to
8eb40 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 0a 2a invoking this.*
8eb50 2a 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 65 * routine so the
8eb60 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e 79 20 re is never any
8eb70 63 68 61 6e 63 65 20 74 68 61 74 20 74 77 6f 20 chance that two
8eb80 6f 72 20 6d 6f 72 65 0a 2a 2a 20 74 68 72 65 61 or more.** threa
8eb90 64 73 20 63 61 6e 20 62 65 20 69 6e 20 74 68 69 ds can be in thi
8eba0 73 20 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 s routine at the
8ebb0 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 same time..*/.s
8ebc0 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
8ebd0 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 ys5MallocUnsafe(
8ebe0 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e int nByte){. in
8ebf0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f t i; /
8ec00 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d * Index of a mem
8ec10 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 5.aPool[] slot *
8ec20 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 20 20 /. int iBin;
8ec30 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
8ec40 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 to mem5.aiFreeli
8ec50 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46 st[] */. int iF
8ec60 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 ullSz; /* Si
8ec70 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e ze of allocation
8ec80 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 70 rounded up to p
8ec90 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69 ower of 2 */. i
8eca0 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 nt iLogsize;
8ecb0 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c /* Log2 of iFull
8ecc0 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a Sz/POW2_MIN */..
8ecd0 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75 73 74 20 /* nByte must
8ece0 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 2a 2f be a positive */
8ecf0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
8ed00 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70 >0 );.. /* Keep
8ed10 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6d 61 track of the ma
8ed20 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e ximum allocation
8ed30 20 72 65 71 75 65 73 74 2e 20 20 45 76 65 6e 20 request. Even
8ed40 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 20 2a 2a unfulfilled. **
8ed50 20 72 65 71 75 65 73 74 73 20 61 72 65 20 63 6f requests are co
8ed60 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 28 unted */. if( (
8ed70 75 33 32 29 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d u32)nByte>mem5.m
8ed80 61 78 52 65 71 75 65 73 74 20 29 7b 0a 20 20 20 axRequest ){.
8ed90 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 mem5.maxRequest
8eda0 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 = nByte;. }..
8edb0 20 2f 2a 20 41 62 6f 72 74 20 69 66 20 74 68 65 /* Abort if the
8edc0 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 requested alloc
8edd0 61 74 69 6f 6e 20 73 69 7a 65 20 69 73 20 6c 61 ation size is la
8ede0 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 rger than the la
8edf0 72 67 65 73 74 0a 20 20 2a 2a 20 70 6f 77 65 72 rgest. ** power
8ee00 20 6f 66 20 74 77 6f 20 74 68 61 74 20 77 65 20 of two that we
8ee10 63 61 6e 20 72 65 70 72 65 73 65 6e 74 20 75 73 can represent us
8ee20 69 6e 67 20 33 32 2d 62 69 74 20 73 69 67 6e 65 ing 32-bit signe
8ee30 64 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f d integers.. */
8ee40 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3e 20 30 . if( nByte > 0
8ee50 78 34 30 30 30 30 30 30 30 20 29 7b 0a 20 20 20 x40000000 ){.
8ee60 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a return 0;. }..
8ee70 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42 79 74 65 /* Round nByte
8ee80 20 75 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 up to the next
8ee90 76 61 6c 69 64 20 70 6f 77 65 72 20 6f 66 20 74 valid power of t
8eea0 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 46 75 6c wo */. for(iFul
8eeb0 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2c lSz=mem5.szAtom,
8eec0 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 69 46 75 iLogsize=0; iFu
8eed0 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 46 75 6c llSz<nByte; iFul
8eee0 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 lSz *= 2, iLogsi
8eef0 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 ze++){}.. /* Ma
8ef00 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e 61 69 46 ke sure mem5.aiF
8ef10 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 reelist[iLogsize
8ef20 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 ] contains at le
8ef30 61 73 74 20 6f 6e 65 20 66 72 65 65 0a 20 20 2a ast one free. *
8ef40 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 * block. If not
8ef50 2c 20 74 68 65 6e 20 73 70 6c 69 74 20 61 20 62 , then split a b
8ef60 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e 65 78 74 lock of the next
8ef70 20 6c 61 72 67 65 72 20 70 6f 77 65 72 20 6f 66 larger power of
8ef80 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20 6f 72 64 . ** two in ord
8ef90 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e er to create a n
8efa0 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f 66 ew free block of
8efb0 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a 65 2e 0a size iLogsize..
8efc0 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42 69 6e 3d */. for(iBin=
8efd0 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 iLogsize; mem5.a
8efe0 69 46 72 65 65 6c 69 73 74 5b 69 42 69 6e 5d 3c iFreelist[iBin]<
8eff0 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 0 && iBin<=LOGMA
8f000 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 X; iBin++){}. i
8f010 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 f( iBin>LOGMAX )
8f020 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 20 3d return 0;. i =
8f030 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 memsys5UnlinkFi
8f040 72 73 74 28 69 42 69 6e 29 3b 0a 20 20 77 68 69 rst(iBin);. whi
8f050 6c 65 28 20 69 42 69 6e 3e 69 4c 6f 67 73 69 7a le( iBin>iLogsiz
8f060 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77 e ){. int new
8f070 53 69 7a 65 3b 0a 0a 20 20 20 20 69 42 69 6e 2d Size;.. iBin-
8f080 2d 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d -;. newSize =
8f090 20 31 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 20 20 1 << iBin;.
8f0a0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 mem5.aCtrl[i+new
8f0b0 53 69 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45 Size] = CTRL_FRE
8f0c0 45 20 7c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 E | iBin;. me
8f0d0 6d 73 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 msys5Link(i+newS
8f0e0 69 7a 65 2c 20 69 42 69 6e 29 3b 0a 20 20 7d 0a ize, iBin);. }.
8f0f0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 mem5.aCtrl[i]
8f100 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f = iLogsize;.. /
8f110 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f 63 61 74 * Update allocat
8f120 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73 or performance s
8f130 74 61 74 69 73 74 69 63 73 2e 20 2a 2f 0a 20 20 tatistics. */.
8f140 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 mem5.nAlloc++;.
8f150 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 mem5.totalAlloc
8f160 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d += iFullSz;. m
8f170 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 em5.totalExcess
8f180 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 += iFullSz - nBy
8f190 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 te;. mem5.curre
8f1a0 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d ntCount++;. mem
8f1b0 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2b 3d 20 5.currentOut +=
8f1c0 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6d iFullSz;. if( m
8f1d0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d em5.maxCount<mem
8f1e0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 29 5.currentCount )
8f1f0 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 3d mem5.maxCount =
8f200 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 mem5.currentCou
8f210 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d nt;. if( mem5.m
8f220 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 72 65 axOut<mem5.curre
8f230 6e 74 4f 75 74 20 29 20 6d 65 6d 35 2e 6d 61 78 ntOut ) mem5.max
8f240 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 Out = mem5.curre
8f250 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 ntOut;.. /* Ret
8f260 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
8f270 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d the allocated m
8f280 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75 emory. */. retu
8f290 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e rn (void*)&mem5.
8f2a0 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 zPool[i*mem5.szA
8f2b0 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 tom];.}../*.** F
8f2c0 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 ree an outstandi
8f2d0 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ng memory alloca
8f2e0 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
8f2f0 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 void memsys5Free
8f300 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c Unsafe(void *pOl
8f310 64 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 d){. u32 size,
8f320 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 6e 74 20 iLogsize;. int
8f330 69 42 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 iBlock;.. /* Se
8f340 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 t iBlock to the
8f350 69 6e 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f index of the blo
8f360 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ck pointed to by
8f370 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 pOld in . ** t
8f380 68 65 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 he array of mem5
8f390 2e 73 7a 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f .szAtom byte blo
8f3a0 63 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 cks pointed to b
8f3b0 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 y mem5.zPool..
8f3c0 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 */. iBlock = ((
8f3d0 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a u8 *)pOld-mem5.z
8f3e0 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f Pool)/mem5.szAto
8f3f0 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 m;.. /* Check t
8f400 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 hat the pointer
8f410 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 pOld points to a
8f420 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 valid, non-free
8f430 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 block. */. ass
8f440 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 ert( iBlock>=0 &
8f450 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 & iBlock<mem5.nB
8f460 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 lock );. assert
8f470 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 ( ((u8 *)pOld-me
8f480 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 m5.zPool)%mem5.s
8f490 7a 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 zAtom==0 );. as
8f4a0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 sert( (mem5.aCtr
8f4b0 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c l[iBlock] & CTRL
8f4c0 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 _FREE)==0 );..
8f4d0 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e iLogsize = mem5.
8f4e0 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 aCtrl[iBlock] &
8f4f0 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 CTRL_LOGSIZE;.
8f500 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 size = 1<<iLogsi
8f510 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 ze;. assert( iB
8f520 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 lock+size-1<(u32
8f530 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a )mem5.nBlock );.
8f540 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 . mem5.aCtrl[iB
8f550 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 lock] |= CTRL_FR
8f560 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c EE;. mem5.aCtrl
8f570 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 [iBlock+size-1]
8f580 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 |= CTRL_FREE;.
8f590 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 assert( mem5.cur
8f5a0 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 rentCount>0 );.
8f5b0 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 assert( mem5.cu
8f5c0 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a rrentOut>=(size*
8f5d0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a mem5.szAtom) );.
8f5e0 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f mem5.currentCo
8f5f0 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 unt--;. mem5.cu
8f600 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 rrentOut -= size
8f610 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 *mem5.szAtom;.
8f620 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 assert( mem5.cur
8f630 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d rentOut>0 || mem
8f640 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 5.currentCount==
8f650 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 0 );. assert( m
8f660 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
8f670 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 >0 || mem5.curre
8f680 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d ntOut==0 );.. m
8f690 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b em5.aCtrl[iBlock
8f6a0 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 ] = CTRL_FREE |
8f6b0 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c iLogsize;. whil
8f6c0 65 28 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69 e( ALWAYS(iLogsi
8f6d0 7a 65 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a 20 20 ze<LOGMAX) ){.
8f6e0 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0a 20 20 int iBuddy;.
8f6f0 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 if( (iBlock>>i
8f700 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29 7b 0a Logsize) & 1 ){.
8f710 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 iBuddy = i
8f720 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 Block - size;.
8f730 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
8f740 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2b Buddy = iBlock +
8f750 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20 size;. }.
8f760 20 61 73 73 65 72 74 28 20 69 42 75 64 64 79 3e assert( iBuddy>
8f770 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 69 =0 );. if( (i
8f780 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 Buddy+(1<<iLogsi
8f790 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b ze))>mem5.nBlock
8f7a0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 ) break;. if
8f7b0 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 ( mem5.aCtrl[iBu
8f7c0 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 52 45 45 ddy]!=(CTRL_FREE
8f7d0 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 29 20 62 | iLogsize) ) b
8f7e0 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 79 73 reak;. memsys
8f7f0 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 79 2c 20 5Unlink(iBuddy,
8f800 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 20 20 69 iLogsize);. i
8f810 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 Logsize++;. i
8f820 66 28 20 69 42 75 64 64 79 3c 69 42 6c 6f 63 6b f( iBuddy<iBlock
8f830 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 ){. mem5.a
8f840 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43 Ctrl[iBuddy] = C
8f850 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 TRL_FREE | iLogs
8f860 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e ize;. mem5.
8f870 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 aCtrl[iBlock] =
8f880 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20 0;. iBlock
8f890 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 20 7d 65 = iBuddy;. }e
8f8a0 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e lse{. mem5.
8f8b0 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 aCtrl[iBlock] =
8f8c0 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 CTRL_FREE | iLog
8f8d0 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 size;. mem5
8f8e0 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d .aCtrl[iBuddy] =
8f8f0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 0;. }. si
8f900 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d ze *= 2;. }. m
8f910 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 emsys5Link(iBloc
8f920 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a k, iLogsize);.}.
8f930 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
8f940 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 nBytes of memory
8f950 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8f960 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 69 *memsys5Malloc(i
8f970 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 nt nBytes){. sq
8f980 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d lite3_int64 *p =
8f990 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 0;. if( nBytes
8f9a0 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 >0 ){. memsys
8f9b0 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20 5Enter();. p
8f9c0 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 = memsys5MallocU
8f9d0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 nsafe(nBytes);.
8f9e0 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 memsys5Leave(
8f9f0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
8fa00 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a (void*)p; .}../*
8fa10 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e .** Free memory.
8fa20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 .**.** The outer
8fa30 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c layer memory al
8fa40 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 locator prevents
8fa50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 this routine fr
8fa60 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c om.** being call
8fa70 65 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d ed with pPrior==
8fa80 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 0..*/.static voi
8fa90 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f d memsys5Free(vo
8faa0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 id *pPrior){. a
8fab0 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 ssert( pPrior!=0
8fac0 20 29 3b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 );. memsys5Ent
8fad0 65 72 28 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 er();. memsys5F
8fae0 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 reeUnsafe(pPrior
8faf0 29 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 );. memsys5Leav
8fb00 65 28 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e(); .}../*.**
8fb10 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 Change the size
8fb20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d of an existing m
8fb30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8fb40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 ..**.** The oute
8fb50 72 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 r layer memory a
8fb60 6c 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 llocator prevent
8fb70 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 s this routine f
8fb80 72 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c rom.** being cal
8fb90 6c 65 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d led with pPrior=
8fba0 3d 30 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 =0. .**.** nByt
8fbb0 65 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 es is always a v
8fbc0 61 6c 75 65 20 6f 62 74 61 69 6e 65 64 20 66 72 alue obtained fr
8fbd0 6f 6d 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 om a prior call
8fbe0 74 6f 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 6f 75 to.** memsys5Rou
8fbf0 6e 64 28 29 2e 20 20 48 65 6e 63 65 20 6e 42 79 nd(). Hence nBy
8fc00 74 65 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 tes is always a
8fc10 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 70 6f 77 non-negative pow
8fc20 65 72 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 20 49 er.** of two. I
8fc30 66 20 6e 42 79 74 65 73 3d 3d 30 20 74 68 61 74 f nBytes==0 that
8fc40 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 6f means that an o
8fc50 76 65 72 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 versize allocati
8fc60 6f 6e 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 on.** (an alloca
8fc70 74 69 6f 6e 20 6c 61 72 67 65 72 20 74 68 61 6e tion larger than
8fc80 20 30 78 34 30 30 30 30 30 30 30 29 20 77 61 73 0x40000000) was
8fc90 20 72 65 71 75 65 73 74 65 64 20 61 6e 64 20 74 requested and t
8fca0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 his.** routine s
8fcb0 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 30 20 77 hould return 0 w
8fcc0 69 74 68 6f 75 74 20 66 72 65 65 69 6e 67 20 70 ithout freeing p
8fcd0 50 72 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 Prior..*/.static
8fce0 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65 void *memsys5Re
8fcf0 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 alloc(void *pPri
8fd00 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b or, int nBytes){
8fd10 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 . int nOld;. v
8fd20 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 oid *p;. assert
8fd30 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 ( pPrior!=0 );.
8fd40 20 61 73 73 65 72 74 28 20 28 6e 42 79 74 65 73 assert( (nBytes
8fd50 26 28 6e 42 79 74 65 73 2d 31 29 29 3d 3d 30 20 &(nBytes-1))==0
8fd60 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 );. assert( nBy
8fd70 74 65 73 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 tes>=0 );. if(
8fd80 6e 42 79 74 65 73 3d 3d 30 20 29 7b 0a 20 20 20 nBytes==0 ){.
8fd90 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
8fda0 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35 53 nOld = memsys5S
8fdb0 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 ize(pPrior);. i
8fdc0 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 f( nBytes<=nOld
8fdd0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 ){. return pP
8fde0 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 rior;. }. mems
8fdf0 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 ys5Enter();. p
8fe00 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 = memsys5MallocU
8fe10 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 nsafe(nBytes);.
8fe20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 if( p ){. me
8fe30 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 mcpy(p, pPrior,
8fe40 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d 73 79 nOld);. memsy
8fe50 73 35 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 s5FreeUnsafe(pPr
8fe60 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 ior);. }. mems
8fe70 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 ys5Leave();. re
8fe80 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
8fe90 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 Round up a requ
8fea0 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 est size to the
8feb0 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 next valid alloc
8fec0 61 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a ation size. If.
8fed0 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f ** the allocatio
8fee0 6e 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 n is too large t
8fef0 6f 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 o be handled by
8ff00 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 this allocation
8ff10 73 79 73 74 65 6d 2c 0a 2a 2a 20 72 65 74 75 72 system,.** retur
8ff20 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 61 n 0..**.** All a
8ff30 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 75 73 74 20 llocations must
8ff40 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 be a power of tw
8ff50 6f 20 61 6e 64 20 6d 75 73 74 20 62 65 20 65 78 o and must be ex
8ff60 70 72 65 73 73 65 64 20 62 79 20 61 0a 2a 2a 20 pressed by a.**
8ff70 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 32-bit signed in
8ff80 74 65 67 65 72 2e 20 20 48 65 6e 63 65 20 74 68 teger. Hence th
8ff90 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 e largest alloca
8ffa0 74 69 6f 6e 20 69 73 20 30 78 34 30 30 30 30 30 tion is 0x400000
8ffb0 30 30 0a 2a 2a 20 6f 72 20 31 30 37 33 37 34 31 00.** or 1073741
8ffc0 38 32 34 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 824 bytes..*/.st
8ffd0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 atic int memsys5
8ffe0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a Roundup(int n){.
8fff0 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 0a 20 int iFullSz;.
90000 20 69 66 28 20 6e 20 3e 20 30 78 34 30 30 30 30 if( n > 0x40000
90010 30 30 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 000 ) return 0;.
90020 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 for(iFullSz=me
90030 6d 35 2e 73 7a 41 74 6f 6d 3b 20 69 46 75 6c 6c m5.szAtom; iFull
90040 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d Sz<n; iFullSz *=
90050 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 2);. return iF
90060 75 6c 6c 53 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ullSz;.}../*.**
90070 52 65 74 75 72 6e 20 74 68 65 20 63 65 69 6c 69 Return the ceili
90080 6e 67 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 ng of the logari
90090 74 68 6d 20 62 61 73 65 20 32 20 6f 66 20 69 56 thm base 2 of iV
900a0 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d alue..**.** Exam
900b0 70 6c 65 73 3a 20 20 20 6d 65 6d 73 79 73 35 4c ples: memsys5L
900c0 6f 67 28 31 29 20 2d 3e 20 30 0a 2a 2a 20 20 20 og(1) -> 0.**
900d0 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 memsys
900e0 35 4c 6f 67 28 32 29 20 2d 3e 20 31 0a 2a 2a 20 5Log(2) -> 1.**
900f0 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 mems
90100 79 73 35 4c 6f 67 28 34 29 20 2d 3e 20 32 0a 2a ys5Log(4) -> 2.*
90110 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 * me
90120 6d 73 79 73 35 4c 6f 67 28 35 29 20 2d 3e 20 33 msys5Log(5) -> 3
90130 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
90140 6d 65 6d 73 79 73 35 4c 6f 67 28 38 29 20 2d 3e memsys5Log(8) ->
90150 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 3.**
90160 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 39 29 20 memsys5Log(9)
90170 2d 3e 20 34 0a 2a 2f 0a 73 74 61 74 69 63 20 69 -> 4.*/.static i
90180 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 28 69 6e nt memsys5Log(in
90190 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 t iValue){. int
901a0 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 69 4c 6f iLog;. for(iLo
901b0 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 29 3c 69 g=0; (1<<iLog)<i
901c0 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0a Value; iLog++);.
901d0 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 3b 0a 7d return iLog;.}
901e0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
901f0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
90200 6c 6f 63 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 locator..**.** T
90210 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e his routine is n
90220 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 ot threadsafe.
90230 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
90240 62 65 20 68 6f 6c 64 69 6e 67 20 61 20 6d 75 74 be holding a mut
90250 65 78 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 ex.** to prevent
90260 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 multiple thread
90270 73 20 66 72 6f 6d 20 65 6e 74 65 72 69 6e 67 20 s from entering
90280 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
90290 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
902a0 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 64 memsys5Init(void
902b0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 6e *NotUsed){. in
902c0 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
902d0 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
902e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b */. int nByte;
902f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
90300 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d er of bytes of m
90310 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 emory available
90320 74 6f 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 6f to this allocato
90330 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 79 74 65 r */. u8 *zByte
90340 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d ; /* Mem
90350 6f 72 79 20 75 73 61 62 6c 65 20 62 79 20 74 68 ory usable by th
90360 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a is allocator */.
90370 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 int nMinLog;
90380 20 20 20 20 20 2f 2a 20 4c 6f 67 20 62 61 73 65 /* Log base
90390 20 32 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 61 6c 2 of minimum al
903a0 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 6e location size in
903b0 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 bytes */. int
903c0 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f iOffset; /
903d0 2a 20 41 6e 20 6f 66 66 73 65 74 20 69 6e 74 6f * An offset into
903e0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f mem5.aCtrl[] */
903f0 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
90400 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a ETER(NotUsed);..
90410 20 20 2f 2a 20 46 6f 72 20 74 68 65 20 70 75 72 /* For the pur
90420 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f poses of this ro
90430 75 74 69 6e 65 2c 20 64 69 73 61 62 6c 65 20 74 utine, disable t
90440 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 6d 65 he mutex */. me
90450 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 0a 20 m5.mutex = 0;..
90460 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 /* The size of
90470 61 20 4d 65 6d 35 4c 69 6e 6b 20 6f 62 6a 65 63 a Mem5Link objec
90480 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 t must be a powe
90490 72 20 6f 66 20 74 77 6f 2e 20 20 56 65 72 69 66 r of two. Verif
904a0 79 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 69 73 y that. ** this
904b0 20 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 is case.. */.
904c0 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 assert( (sizeof
904d0 28 4d 65 6d 35 4c 69 6e 6b 29 26 28 73 69 7a 65 (Mem5Link)&(size
904e0 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 2d 31 29 29 of(Mem5Link)-1))
904f0 3d 3d 30 20 29 3b 0a 0a 20 20 6e 42 79 74 65 20 ==0 );.. nByte
90500 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
90510 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a 20 20 7a onfig.nHeap;. z
90520 42 79 74 65 20 3d 20 28 75 38 2a 29 73 71 6c 69 Byte = (u8*)sqli
90530 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
90540 70 48 65 61 70 3b 0a 20 20 61 73 73 65 72 74 28 pHeap;. assert(
90550 20 7a 42 79 74 65 21 3d 30 20 29 3b 20 20 2f 2a zByte!=0 ); /*
90560 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
90570 29 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 ) does not allow
90580 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 0a 20 otherwise */..
90590 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 nMinLog = memsy
905a0 73 35 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f s5Log(sqlite3Glo
905b0 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 balConfig.mnReq)
905c0 3b 0a 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 ;. mem5.szAtom
905d0 3d 20 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a = (1<<nMinLog);.
905e0 20 20 77 68 69 6c 65 28 20 28 69 6e 74 29 73 69 while( (int)si
905f0 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d zeof(Mem5Link)>m
90600 65 6d 35 2e 73 7a 41 74 6f 6d 20 29 7b 0a 20 20 em5.szAtom ){.
90610 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 mem5.szAtom =
90620 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 31 mem5.szAtom << 1
90630 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 35 2e 6e 42 ;. }.. mem5.nB
90640 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 20 2f 20 lock = (nByte /
90650 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2b 73 69 7a (mem5.szAtom+siz
90660 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 6d 65 6d eof(u8)));. mem
90670 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74 65 3b 5.zPool = zByte;
90680 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 20 3d 20 . mem5.aCtrl =
90690 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f (u8 *)&mem5.zPoo
906a0 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 l[mem5.nBlock*me
906b0 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 0a 20 20 66 m5.szAtom];.. f
906c0 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c 4f 47 or(ii=0; ii<=LOG
906d0 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 MAX; ii++){.
906e0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
906f0 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 ii] = -1;. }..
90700 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 iOffset = 0;.
90710 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b 20 69 for(ii=LOGMAX; i
90720 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 20 20 i>=0; ii--){.
90730 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28 31 int nAlloc = (1
90740 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 20 28 <<ii);. if( (
90750 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c iOffset+nAlloc)<
90760 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0a =mem5.nBlock ){.
90770 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c mem5.aCtrl
90780 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 20 7c [iOffset] = ii |
90790 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20 20 CTRL_FREE;.
907a0 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 4f memsys5Link(iO
907b0 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 20 20 ffset, ii);.
907c0 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e 41 6c iOffset += nAl
907d0 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 loc;. }. a
907e0 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 2b 6e ssert((iOffset+n
907f0 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 6c 6f Alloc)>mem5.nBlo
90800 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 ck);. }.. /* I
90810 66 20 61 20 6d 75 74 65 78 20 69 73 20 72 65 71 f a mutex is req
90820 75 69 72 65 64 20 66 6f 72 20 6e 6f 72 6d 61 6c uired for normal
90830 20 6f 70 65 72 61 74 69 6f 6e 2c 20 61 6c 6c 6f operation, allo
90840 63 61 74 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 cate one */. if
90850 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
90860 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d onfig.bMemstat==
90870 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 0 ){. mem5.mu
90880 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
90890 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
908a0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 UTEX_STATIC_MEM)
908b0 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
908c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
908d0 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
908e0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
908f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
90900 73 79 73 35 53 68 75 74 64 6f 77 6e 28 76 6f 69 sys5Shutdown(voi
90910 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
90920 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
90930 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 35 NotUsed);. mem5
90940 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 .mutex = 0;. re
90950 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 turn;.}..#ifdef
90960 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a SQLITE_TEST./*.*
90970 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 * Open the file
90980 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 indicated and wr
90990 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c ite a log of all
909a0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 unfreed memory
909b0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 .** allocations
909c0 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a into that log..*
909d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
909e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
909f0 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 sys5Dump(const c
90a00 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
90a10 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 . FILE *out;.
90a20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 69 int i, j, n;. i
90a30 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a 20 20 69 nt nMinLog;.. i
90a40 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 f( zFilename==0
90a50 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d || zFilename[0]=
90a60 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 =0 ){. out =
90a70 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b stdout;. }else{
90a80 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e . out = fopen
90a90 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 (zFilename, "w")
90aa0 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 ;. if( out==0
90ab0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
90ac0 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e f(stderr, "** Un
90ad0 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d able to output m
90ae0 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70 emory debug outp
90af0 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 ut log: %s **\n"
90b00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
90b10 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d zFilenam
90b20 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
90b30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 ;. }. }. me
90b40 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 msys5Enter();.
90b50 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 nMinLog = memsys
90b60 35 4c 6f 67 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5Log(mem5.szAtom
90b70 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
90b80 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b 6e 4d 69 =LOGMAX && i+nMi
90b90 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 nLog<32; i++){.
90ba0 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 for(n=0, j=me
90bb0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 5d m5.aiFreelist[i]
90bc0 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35 ; j>=0; j = MEM5
90bd0 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e LINK(j)->next, n
90be0 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 69 6e 74 ++){}. fprint
90bf0 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69 73 74 f(out, "freelist
90c00 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65 20 25 items of size %
90c10 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73 d: %d\n", mem5.s
90c20 7a 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0a zAtom << i, n);.
90c30 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 }. fprintf(ou
90c40 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 20 t, "mem5.nAlloc
90c50 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c = %llu\n",
90c60 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 mem5.nAlloc);.
90c70 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d fprintf(out, "m
90c80 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 20 em5.totalAlloc
90c90 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 = %llu\n", mem5
90ca0 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20 .totalAlloc);.
90cb0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 fprintf(out, "me
90cc0 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 20 m5.totalExcess
90cd0 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %llu\n", mem5.
90ce0 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a 20 20 totalExcess);.
90cf0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 fprintf(out, "me
90d00 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 20 20 m5.currentOut
90d10 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 = %u\n", mem5.cu
90d20 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66 70 72 rrentOut);. fpr
90d30 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
90d40 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d 20 25 currentCount = %
90d50 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 u\n", mem5.curre
90d60 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69 ntCount);. fpri
90d70 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d ntf(out, "mem5.m
90d80 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20 25 75 axOut = %u
90d90 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 \n", mem5.maxOut
90da0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 );. fprintf(out
90db0 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 , "mem5.maxCount
90dc0 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 = %u\n", me
90dd0 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a 20 20 m5.maxCount);.
90de0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 fprintf(out, "me
90df0 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 20 20 m5.maxRequest
90e00 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 = %u\n", mem5.ma
90e10 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d 65 6d xRequest);. mem
90e20 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 69 sys5Leave();. i
90e30 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 f( out==stdout )
90e40 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 {. fflush(std
90e50 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 out);. }else{.
90e60 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a fclose(out);.
90e70 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a }.}.#endif../*
90e80 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
90e90 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 is the only rou
90ea0 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c tine in this fil
90eb0 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 e with external
90ec0 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 20 49 74 20 .** linkage. It
90ed0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
90ee0 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 73 71 r to a static sq
90ef0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
90f00 73 0a 2a 2a 20 73 74 72 75 63 74 20 70 6f 70 75 s.** struct popu
90f10 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6d lated with the m
90f20 65 6d 73 79 73 35 20 6d 65 74 68 6f 64 73 2e 0a emsys5 methods..
90f30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
90f40 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f E const sqlite3_
90f50 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c mem_methods *sql
90f60 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 ite3MemGetMemsys
90f70 35 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 5(void){. stati
90f80 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
90f90 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d 73 mem_methods mems
90fa0 79 73 35 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 ys5Methods = {.
90fb0 20 20 20 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f memsys5Mallo
90fc0 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 46 c,. memsys5F
90fd0 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 ree,. memsys
90fe0 35 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 5Realloc,. m
90ff0 65 6d 73 79 73 35 53 69 7a 65 2c 0a 20 20 20 20 emsys5Size,.
91000 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 2c memsys5Roundup,
91010 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 49 6e 69 . memsys5Ini
91020 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 t,. memsys5S
91030 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a hutdown,. 0.
91040 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d };. return &m
91050 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 3b 0a 7d emsys5Methods;.}
91060 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
91070 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
91080 35 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 5 */../*********
91090 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d ***** End of mem
910a0 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 5.c ************
910b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
910e0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
910f0 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a mutex.c *******
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91120 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
91130 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a August 14.**.**
91140 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
91150 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
91160 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
91170 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
91180 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
91190 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
911a0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
911b0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
911c0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
911d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
911e0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
911f0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
91200 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
91210 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
91220 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
91230 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
91240 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
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 2a 2a 2a 2a 2a 2a ****************
91280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91290 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
912a0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
912b0 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 the C functions
912c0 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d that implement m
912d0 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 utexes..**.** Th
912e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
912f0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
91300 6d 6d 6f 6e 20 61 63 72 6f 73 73 20 61 6c 6c 20 mmon across all
91310 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
91320 74 69 6f 6e 73 2e 0a 2a 2f 0a 0a 23 69 66 20 64 tions..*/..#if d
91330 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
91340 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 BUG) && !defined
91350 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d (SQLITE_MUTEX_OM
91360 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 64 65 IT)./*.** For de
91370 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
91380 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 74 68 , record when th
91390 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
913a0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 m is initialized
913b0 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 69 61 .** and uninitia
913c0 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 77 65 lized so that we
913d0 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 69 66 can assert() if
913e0 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 74 74 there is an att
913f0 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 empt to.** alloc
91400 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c ate a mutex whil
91410 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 e the system is
91420 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a uninitialized..*
91430 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f /.static SQLITE_
91440 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49 WSD int mutexIsI
91450 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 nit = 0;.#endif
91460 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
91470 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
91480 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f ITE_MUTEX_OMIT./
91490 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
914a0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d the mutex system
914b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
914c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
914d0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
914e0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
914f0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c TE_OK;. if( sql
91500 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
91510 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 .bCoreMutex ){.
91520 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 if( !sqlite3G
91530 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
91540 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b x.xMutexAlloc ){
91550 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
91560 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 xMutexAlloc met
91570 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e hod has not been
91580 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75 set, then the u
91590 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 ser did not.
915a0 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d ** install a m
915b0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
915c0 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f ion via sqlite3_
915d0 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74 config() prior t
915e0 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 o . ** sqli
915f0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
91600 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54 being called. T
91610 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 his block copies
91620 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 20 pointers to.
91630 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c ** the defaul
91640 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e t implementation
91650 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 into the sqlite
91660 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 3GlobalConfig st
91670 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a ructure.. *
91680 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f /. sqlite3_
91690 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 mutex_methods *p
916a0 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65 From = sqlite3De
916b0 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 faultMutex();.
916c0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
916d0 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 3d x_methods *pTo =
916e0 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 &sqlite3GlobalC
916f0 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20 20 onfig.mutex;..
91700 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 memcpy(pTo,
91710 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28 pFrom, offsetof(
91720 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
91730 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c thods, xMutexAll
91740 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 oc));. memc
91750 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46 py(&pTo->xMutexF
91760 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75 ree, &pFrom->xMu
91770 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20 texFree,.
91780 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 54 sizeof(*pT
91790 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 73 71 o) - offsetof(sq
917a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
917b0 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 65 29 ods, xMutexFree)
917c0 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 78 4d );. pTo->xM
917d0 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 72 6f utexAlloc = pFro
917e0 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 3b 0a m->xMutexAlloc;.
917f0 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
91800 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
91810 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49 ig.mutex.xMutexI
91820 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64 nit();. }..#ifd
91830 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
91840 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 GLOBAL(int, mu
91850 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 3b 0a texIsInit) = 1;.
91860 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
91870 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 rc;.}../*.** Sh
91880 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78 utdown the mutex
91890 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61 system. This ca
918a0 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63 ll frees resourc
918b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a es allocated by.
918c0 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 ** sqlite3MutexI
918d0 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nit()..*/.SQLITE
918e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
918f0 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 ite3MutexEnd(voi
91900 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 d){. int rc = S
91910 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
91920 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91930 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91940 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 End ){. rc =
91950 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91960 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91970 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64 End();. }..#ifd
91980 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
91990 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 GLOBAL(int, mu
919a0 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 3b 0a texIsInit) = 0;.
919b0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
919c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
919d0 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 trieve a pointer
919e0 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 to a static mut
919f0 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 ex or allocate a
91a00 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 new dynamic one
91a10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
91a20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
91a30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
91a40 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e oc(int id){.#ifn
91a50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
91a60 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 AUTOINIT. if( s
91a70 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
91a80 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a e() ) return 0;.
91a90 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
91aa0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91ab0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91ac0 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 Alloc(id);.}..SQ
91ad0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
91ae0 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 ite3_mutex *sqli
91af0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e te3MutexAlloc(in
91b00 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 t id){. if( !sq
91b10 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
91b20 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
91b30 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
91b40 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c 4f 42 }. assert( GLOB
91b50 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 AL(int, mutexIsI
91b60 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e nit) );. return
91b70 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
91b80 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
91b90 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f xAlloc(id);.}../
91ba0 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 6e 61 *.** Free a dyna
91bb0 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 mic mutex..*/.SQ
91bc0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
91bd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 lite3_mutex_free
91be0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
91bf0 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 p){. if( p ){.
91c00 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
91c10 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91c20 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a texFree(p);. }.
91c30 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 }../*.** Obtain
91c40 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20 the mutex p. If
91c50 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 some other threa
91c60 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 74 68 d already has th
91c70 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a e mutex, block.*
91c80 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e 20 62 * until it can b
91c90 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53 e obtained..*/.S
91ca0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
91cb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
91cc0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
91cd0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
91ce0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
91cf0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91d00 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20 MutexEnter(p);.
91d10 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 }.}../*.** Obta
91d20 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 in the mutex p.
91d30 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
91d40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
91d50 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 Otherwise, if a
91d60 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 nother.** thread
91d70 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 holds the mutex
91d80 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62 and it cannot b
91d90 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75 e obtained, retu
91da0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a rn SQLITE_BUSY..
91db0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
91dc0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
91dd0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 try(sqlite3_mute
91de0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 x *p){. int rc
91df0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
91e00 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 74 75 f( p ){. retu
91e10 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
91e20 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91e30 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20 texTry(p);. }.
91e40 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
91e50 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
91e60 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
91e70 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
91e80 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70 72 utex that was pr
91e90 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65 eviously.** ente
91ea0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
91eb0 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 thread. The beh
91ec0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
91ed0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 ed if the mutex
91ee0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
91ef0 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 49 66 ntly entered. If
91f00 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
91f10 69 73 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 is passed as an
91f20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73 argument.** this
91f30 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
91f40 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f o-op..*/.SQLITE_
91f50 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
91f60 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
91f70 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
91f80 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 if( p ){. s
91f90 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
91fa0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c ig.mutex.xMutexL
91fb0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a eave(p);. }.}..
91fc0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f #ifndef NDEBUG./
91fd0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
91fe0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e _mutex_held() an
91ff0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
92000 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e notheld() routin
92010 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 e are.** intende
92020 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
92030 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
92040 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
92050 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
92060 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 mutex_held(sqlit
92070 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
92080 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 return p==0 || s
92090 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
920a0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48 ig.mutex.xMutexH
920b0 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 eld(p);.}.SQLITE
920c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
920d0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 _mutex_notheld(s
920e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
920f0 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 {. return p==0
92100 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c || sqlite3Global
92110 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
92120 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d texNotheld(p);.}
92130 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
92140 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f /* SQLITE_MUTEX_
92150 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a OMIT */../******
92160 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
92170 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mutex.c ********
92180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
921a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
921b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
921c0 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 ile mutex_noop.c
921d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
921e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
921f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
92200 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 008 October 07.*
92210 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
92220 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
92230 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
92240 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
92250 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
92260 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
92270 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
92280 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
92290 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
922a0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
922b0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
922c0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
922d0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
922e0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
922f0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
92300 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
92310 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
92320 0a 2a 2a 0a 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 2a 2a 2a ****************
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
92370 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
92380 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
92390 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
923a0 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a nt mutexes..**.*
923b0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * This implement
923c0 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 ation in this fi
923d0 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 le does not prov
923e0 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a ide any mutual.*
923f0 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 * exclusion and
92400 69 73 20 74 68 75 73 20 73 75 69 74 61 62 6c 65 is thus suitable
92410 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e for use only in
92420 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
92430 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
92440 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 68 72 in a single thr
92450 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e ead. The routin
92460 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 es defined.** he
92470 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c re are place-hol
92480 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 74 69 ders. Applicati
92490 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 74 75 ons can substitu
924a0 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 te working.** mu
924b0 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 74 20 tex routines at
924c0 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 start-time using
924d0 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 the.**.** s
924e0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 qlite3_config(SQ
924f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 LITE_CONFIG_MUTE
92500 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 X,...).**.** int
92510 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 erface..**.** If
92520 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 compiled with S
92530 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65 QLITE_DEBUG, the
92540 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 n additional log
92550 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 0a 2a ic is inserted.*
92560 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f * that does erro
92570 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 r checking on mu
92580 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 texes to make su
92590 72 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e re they are bein
925a0 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 g.** called corr
925b0 65 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 23 69 66 20 ectly..*/...#if
925c0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
925d0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 UTEX_NOOP) && !d
925e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
925f0 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 BUG)./*.** Stub
92600 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c routines for all
92610 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 2e 0a mutex methods..
92620 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
92630 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f 20 6d nes provide no m
92640 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 utual exclusion
92650 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e or error checkin
92660 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
92670 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 28 73 noopMutexHeld(s
92680 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
92690 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 { return 1; }.st
926a0 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 atic int noopMut
926b0 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 exNotheld(sqlite
926c0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 3_mutex *p){ ret
926d0 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 20 urn 1; }.static
926e0 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 int noopMutexIni
926f0 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 t(void){ return
92700 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 SQLITE_OK; }.sta
92710 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 tic int noopMute
92720 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 xEnd(void){ retu
92730 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
92740 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
92750 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 utex *noopMutexA
92760 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 72 65 lloc(int id){ re
92770 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 6d 75 turn (sqlite3_mu
92780 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 69 63 tex*)8; }.static
92790 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 46 void noopMutexF
927a0 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ree(sqlite3_mute
927b0 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d x *p){ return; }
927c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f .static void noo
927d0 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 pMutexEnter(sqli
927e0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 te3_mutex *p){ r
927f0 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 eturn; }.static
92800 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 int noopMutexTry
92810 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
92820 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 p){ return SQLIT
92830 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 E_OK; }.static v
92840 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 oid noopMutexLea
92850 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
92860 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a *p){ return; }.
92870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
92880 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
92890 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 thods *sqlite3De
928a0 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 faultMutex(void)
928b0 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
928c0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
928d0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 sMutex = {.
928e0 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c 0a 20 noopMutexInit,.
928f0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 2c noopMutexEnd,
92900 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c . noopMutexAl
92910 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 loc,. noopMut
92920 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 exFree,. noop
92930 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
92940 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a 20 20 noopMutexTry,.
92950 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 noopMutexLeave
92960 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 ,.. noopMutex
92970 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 Held,. noopMu
92980 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a texNotheld. };.
92990 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 . return &sMute
929a0 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 x;.}.#endif /* d
929b0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 efined(SQLITE_MU
929c0 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 TEX_NOOP) && !de
929d0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
929e0 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 UG) */..#if defi
929f0 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 ned(SQLITE_MUTEX
92a00 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 _NOOP) && define
92a10 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
92a20 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d /*.** In this im
92a30 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 72 plementation, er
92a40 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20 ror checking is
92a50 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 65 73 provided for tes
92a60 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 75 ting.** and debu
92a70 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 20 gging purposes.
92a80 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 74 69 The mutexes sti
92a90 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 ll do not provid
92aa0 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 e any.** mutual
92ab0 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f exclusion..*/../
92ac0 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f *.** The mutex o
92ad0 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 74 20 bject.*/.struct
92ae0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a sqlite3_mutex {.
92af0 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 2f 2a int id; /*
92b00 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 65 20 The mutex type
92b10 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 */. int cnt;
92b20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
92b30 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 61 20 tries without a
92b40 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 20 2a matching leave *
92b50 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
92b60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
92b70 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 ld() and sqlite3
92b80 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
92b90 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 routine are.**
92ba0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
92bb0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
92bc0 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
92bd0 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 static int debug
92be0 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 MutexHeld(sqlite
92bf0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 3_mutex *p){. r
92c00 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d eturn p==0 || p-
92c10 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 >cnt>0;.}.static
92c20 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 4e int debugMutexN
92c30 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
92c40 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
92c50 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e rn p==0 || p->cn
92c60 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 t==0;.}../*.** I
92c70 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 nitialize and de
92c80 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d initialize the m
92c90 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a utex subsystem..
92ca0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 */.static int de
92cb0 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 bugMutexInit(voi
92cc0 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
92cd0 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 E_OK; }.static i
92ce0 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64 nt debugMutexEnd
92cf0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 (void){ return S
92d00 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a QLITE_OK; }../*.
92d10 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
92d20 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 utex_alloc() rou
92d30 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 tine allocates a
92d40 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e new.** mutex an
92d50 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
92d60 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 ter to it. If i
92d70 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a t returns NULL.*
92d80 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 * that means tha
92d90 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 t a mutex could
92da0 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 not be allocated
92db0 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c . .*/.static sql
92dc0 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 62 75 ite3_mutex *debu
92dd0 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 gMutexAlloc(int
92de0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
92df0 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 74 61 lite3_mutex aSta
92e00 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 74 65 tic[6];. sqlite
92e10 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 3d 20 3_mutex *pNew =
92e20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64 20 0;. switch( id
92e30 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
92e40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 TE_MUTEX_FAST:.
92e50 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
92e60 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 UTEX_RECURSIVE:
92e70 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 {. pNew = s
92e80 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a qlite3Malloc(siz
92e90 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 eof(*pNew));.
92ea0 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 if( pNew ){.
92eb0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 pNew->id
92ec0 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 70 4e = id;. pN
92ed0 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 ew->cnt = 0;.
92ee0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
92ef0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
92f00 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ult: {. ass
92f10 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 20 29 ert( id-2 >= 0 )
92f20 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
92f30 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 69 7a id-2 < (int)(siz
92f40 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a eof(aStatic)/siz
92f50 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d 29 29 eof(aStatic[0]))
92f60 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d );. pNew =
92f70 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b &aStatic[id-2];
92f80 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 . pNew->id
92f90 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 65 61 = id;. brea
92fa0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 k;. }. }. r
92fb0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
92fc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
92fd0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
92fe0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 previously alloc
92ff0 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 ated mutex..*/.s
93000 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 tatic void debug
93010 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 MutexFree(sqlite
93020 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
93030 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 ssert( p->cnt==0
93040 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
93050 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
93060 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d X_FAST || p->id=
93070 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
93080 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 71 6c CURSIVE );. sql
93090 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a ite3_free(p);.}.
930a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
930b0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
930c0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
930d0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
930e0 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 s attempt.** to
930f0 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 enter a mutex.
93100 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 If another threa
93110 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 d is already wit
93120 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a hin the mutex,.*
93130 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
93140 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f enter() will blo
93150 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d ck and sqlite3_m
93160 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 utex_try() will
93170 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
93180 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 _BUSY. The sqli
93190 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
931a0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
931b0 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 s SQLITE_OK.** u
931c0 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 pon successful e
931d0 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 ntry. Mutexes c
931e0 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c reated using SQL
931f0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
93200 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e IVE can.** be en
93210 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 tered multiple t
93220 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 imes by the same
93230 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 thread. In suc
93240 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 h cases the,.**
93250 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 mutex must be ex
93260 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 ited an equal nu
93270 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 mber of times be
93280 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 fore another thr
93290 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 ead.** can enter
932a0 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 . If the same t
932b0 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 hread tries to e
932c0 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b nter any other k
932d0 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 ind of mutex.**
932e0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
932f0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
93300 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 undefined..*/.st
93310 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d atic void debugM
93320 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 utexEnter(sqlite
93330 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
93340 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
93350 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
93360 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 SIVE || debugMut
93370 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a exNotheld(p) );.
93380 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 p->cnt++;.}.st
93390 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 atic int debugMu
933a0 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
933b0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
933c0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
933d0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
933e0 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e E || debugMutexN
933f0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 otheld(p) );. p
93400 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72 ->cnt++;. retur
93410 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
93420 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
93430 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 3_mutex_leave()
93440 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 routine exits a
93450 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a mutex that was.*
93460 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 * previously ent
93470 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 ered by the same
93480 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 thread. The be
93490 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 havior.** is und
934a0 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 efined if the mu
934b0 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 tex is not curre
934c0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a ntly entered or.
934d0 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e ** is not curren
934e0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 tly allocated.
934f0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 SQLite will neve
93500 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a r do either..*/.
93510 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 static void debu
93520 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 gMutexLeave(sqli
93530 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
93540 20 61 73 73 65 72 74 28 20 64 65 62 75 67 4d 75 assert( debugMu
93550 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 texHeld(p) );.
93560 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 p->cnt--;. asse
93570 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 rt( p->id==SQLIT
93580 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
93590 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e E || debugMutexN
935a0 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a otheld(p) );.}..
935b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
935c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
935d0 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 hods *sqlite3Def
935e0 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b aultMutex(void){
935f0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
93600 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
93610 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 64 sMutex = {. d
93620 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c 0a 20 ebugMutexInit,.
93630 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64 debugMutexEnd
93640 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
93650 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 75 67 Alloc,. debug
93660 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 64 MutexFree,. d
93670 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 2c 0a ebugMutexEnter,.
93680 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 54 72 debugMutexTr
93690 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 y,. debugMute
936a0 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 65 62 xLeave,.. deb
936b0 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 ugMutexHeld,.
936c0 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
936d0 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 ld. };.. retur
936e0 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e n &sMutex;.}.#en
936f0 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 dif /* defined(S
93700 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 QLITE_MUTEX_NOOP
93710 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
93720 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f ITE_DEBUG) */../
93730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
93740 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 nd of mutex_noop
93750 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
93760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
93780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
93790 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f egin file mutex_
937a0 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os2.c **********
937b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
937c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
937d0 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 *.** 2007 August
937e0 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 28.**.** The au
937f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
93800 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
93810 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
93820 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
93830 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
93840 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
93850 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
93860 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
93870 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
93880 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
93890 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
938a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
938b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
938c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
938d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
938e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
938f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93940 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
93950 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
93960 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
93970 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 plement mutexes
93980 66 6f 72 20 4f 53 2f 32 0a 2a 2f 0a 0a 2f 2a 0a for OS/2.*/../*.
93990 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
939a0 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 his file is only
939b0 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f used if SQLITE_
939c0 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66 MUTEX_OS2 is def
939d0 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 ined..** See the
939e0 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f mutex.h file fo
939f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 r details..*/.#i
93a00 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 fdef SQLITE_MUTE
93a10 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a X_OS2../********
93a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f ************** O
93a30 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d S/2 Mutex Implem
93a40 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
93a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
93a60 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 **.** This imple
93a70 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 mentation of mut
93a80 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73 exes is built us
93a90 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49 ing the OS/2 API
93aa0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ..*/../*.** The
93ab0 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 mutex object.**
93ac0 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d Each recursive m
93ad0 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 utex is an insta
93ae0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
93af0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
93b00 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
93b10 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 3_mutex {. HMTX
93b20 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a mutex; /*
93b30 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 Mutex controlli
93b40 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 ng the lock */.
93b50 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20 int id;
93b60 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
93b70 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b */. int nRef;
93b80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
93b90 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
93ba0 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b */. TID owner;
93bb0 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 /* Thread
93bc0 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 holding this mu
93bd0 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 tex */.};..#defi
93be0 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 ne OS2_MUTEX_INI
93bf0 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 TIALIZER 0,0,0
93c00 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 ,0../*.** Initia
93c10 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 lize and deiniti
93c20 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 alize the mutex
93c30 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
93c40 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
93c50 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
93c60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
93c70 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d .static int os2M
93c80 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 utexEnd(void){ r
93c90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
93ca0 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 }../*.** The sq
93cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
93cc0 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f c() routine allo
93cd0 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d cates a new.** m
93ce0 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 utex and returns
93cf0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
93d00 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 . If it returns
93d10 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 NULL.** that me
93d20 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 ans that a mutex
93d30 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c could not be al
93d40 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c located. .** SQL
93d50 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 ite will unwind
93d60 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 its stack and re
93d70 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 turn an error.
93d80 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 The argument.**
93d90 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
93da0 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 _alloc() is one
93db0 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 of these integer
93dc0 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a constants:.**.*
93dd0 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
93de0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
93df0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
93e00 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 0.** <li> SQLIT
93e10 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
93e20 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 E 1.**
93e30 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
93e40 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
93e50 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 2.** <li>
93e60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
93e70 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 ATIC_MEM
93e80 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 3.** <li> SQLI
93e90 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
93ea0 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a PRNG 4.**
93eb0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
93ec0 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
93ed0 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
93ee0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
93ef0 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
93f00 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
93f10 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
93f20 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
93f30 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
93f40 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
93f50 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
93f60 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
93f70 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
93f80 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 sed..** The mute
93f90 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
93fa0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
93fb0 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
93fc0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
93fd0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
93fe0 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
93ff0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
94000 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
94010 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c ant to. But SQL
94020 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 ite will only re
94030 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 quest a recursiv
94040 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 e mutex in.** ca
94050 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 ses where it rea
94060 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 lly needs one.
94070 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
94080 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
94090 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
940a0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
940b0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
940c0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
940d0 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
940e0 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
940f0 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
94100 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
94110 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 X_FAST..**.** Th
94120 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
94130 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
94140 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
94150 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
94160 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
94170 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
94180 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 ting mutex. Thr
94190 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ee static mutexe
941a0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
941b0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
941c0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
941d0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
941e0 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
941f0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
94200 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
94210 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
94220 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
94230 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
94240 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
94250 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
94260 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
94270 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
94280 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
94290 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
942a0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
942b0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
942c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
942d0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
942e0 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 t if one of the
942f0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 dynamic mutex pa
94300 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 rameters (SQLITE
94310 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f _MUTEX_FAST.** o
94320 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 r SQLITE_MUTEX_R
94330 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 ECURSIVE) is use
94340 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d d then sqlite3_m
94350 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 utex_alloc().**
94360 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 returns a differ
94370 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 ent mutex on eve
94380 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f ry call. But fo
94390 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
943a0 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
943b0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
943c0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
943d0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
943e0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
943f0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
94400 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
94410 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 *os2MutexAlloc(i
94420 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c nt iType){. sql
94430 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 ite3_mutex *p =
94440 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 NULL;. switch(
94450 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
94460 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 e SQLITE_MUTEX_F
94470 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 AST:. case SQ
94480 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
94490 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 SIVE: {. p
944a0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
944b0 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 ero( sizeof(*p)
944c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 );. if( p )
944d0 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 {. p->id
944e0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 = iType;.
944f0 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75 if( DosCreateMu
94500 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d texSem( 0, &p->m
94510 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 utex, 0, FALSE )
94520 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
94530 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
94540 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20 3_free( p );.
94550 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b p = NULL;
94560 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
94570 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
94580 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
94590 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 t: {. stati
945a0 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 c volatile int i
945b0 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 sInit = 0;.
945c0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
945d0 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 mutex staticMute
945e0 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 xes[] = {.
945f0 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
94600 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
94610 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
94620 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
94630 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f ,. { OS2_
94640 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
94650 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 R, },. {
94660 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 OS2_MUTEX_INITIA
94670 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 LIZER, },.
94680 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
94690 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
946a0 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
946b0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
946c0 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 ,. };.
946d0 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b if ( !isInit ){
946e0 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20 . APIRET
946f0 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42 rc;. PTIB
94700 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50 ptib;. P
94710 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20 PIB ppib;.
94720 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 HMTX mutex;.
94730 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b char name[
94740 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 32];. Dos
94750 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 GetInfoBlocks( &
94760 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 ptib, &ppib );.
94770 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
94780 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 nprintf( sizeof(
94790 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c name), name, "\\
947a0 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 SEM32\\SQLITE%04
947b0 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 x",.
947c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 pp
947d0 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b ib->pib_ulpid );
947e0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
947f0 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 !isInit ){.
94800 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a mutex = 0;.
94810 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44 rc = D
94820 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d osCreateMutexSem
94830 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 ( name, &mutex,
94840 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 0, FALSE);.
94850 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e if( rc == N
94860 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
94870 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 unsigned
94880 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 int i;.
94890 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 20 29 if( !isInit )
948a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
948b0 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 for( i = 0; i <
948c0 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 sizeof(staticMut
948d0 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 exes)/sizeof(sta
948e0 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 ticMutexes[0]);
948f0 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 i++ ){.
94900 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65 DosCreate
94910 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74 MutexSem( 0, &st
94920 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d aticMutexes[i].m
94930 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 utex, 0, FALSE )
94940 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
94950 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
94960 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 isInit = 1;.
94970 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
94980 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75 DosCloseMu
94990 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 29 3b texSem( mutex );
949a0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
949b0 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 4f 52 if( rc == ERROR
949c0 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20 _DUPLICATE_NAME
949d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 ){. D
949e0 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a 20 20 osSleep( 1 );.
949f0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
94a00 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 retur
94a10 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d n p;. }
94a20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
94a30 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
94a40 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b iType-2 >= 0 );
94a50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
94a60 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 Type-2 < sizeof(
94a70 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 staticMutexes)/s
94a80 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
94a90 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 xes[0]) );.
94aa0 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 p = &staticMute
94ab0 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 xes[iType-2];.
94ac0 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 p->id = iTyp
94ad0 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a e;. break;.
94ae0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
94af0 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn p;.}.../*.**
94b00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 This routine dea
94b10 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 llocates a previ
94b20 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ously allocated
94b30 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65 mutex..** SQLite
94b40 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 is careful to d
94b50 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 20 eallocate every
94b60 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c mutex that it al
94b70 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 locates..*/.stat
94b80 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 ic void os2Mutex
94b90 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 Free(sqlite3_mut
94ba0 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d ex *p){. if( p=
94bb0 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 =0 ) return;. a
94bc0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
94bd0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
94be0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
94bf0 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 EX_FAST || p->id
94c00 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
94c10 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f ECURSIVE );. Do
94c20 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 sCloseMutexSem(
94c30 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73 71 p->mutex );. sq
94c40 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b lite3_free( p );
94c50 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
94c60 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 E_DEBUG./*.** Th
94c70 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
94c80 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
94c90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
94ca0 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
94cb0 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
94cc0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
94cd0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
94ce0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
94cf0 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 MutexHeld(sqlite
94d00 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 3_mutex *p){. T
94d10 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 ID tid;. PID pi
94d20 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 d;. ULONG ulCou
94d30 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b nt;. PTIB ptib;
94d40 0a 20 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a . if( p!=0 ) {.
94d50 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 DosQueryMute
94d60 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 xSem(p->mutex, &
94d70 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f pid, &tid, &ulCo
94d80 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b unt);. } else {
94d90 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 . DosGetInfoB
94da0 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c locks(&ptib, NUL
94db0 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 L);. tid = pt
94dc0 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 ib->tib_ptib2->t
94dd0 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 ib2_ultid;. }.
94de0 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
94df0 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 (p->nRef!=0 && p
94e00 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d ->owner==tid);.}
94e10 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d .static int os2M
94e20 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
94e30 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
94e40 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 TID tid;. PID
94e50 70 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 pid;. ULONG ulC
94e60 6f 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 ount;. PTIB pti
94e70 62 3b 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 b;. if( p!= 0 )
94e80 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d {. DosQueryM
94e90 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 utexSem(p->mutex
94ea0 2c 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 , &pid, &tid, &u
94eb0 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 lCount);. } els
94ec0 65 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e e {. DosGetIn
94ed0 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 foBlocks(&ptib,
94ee0 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d NULL);. tid =
94ef0 20 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 ptib->tib_ptib2
94f00 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 ->tib2_ultid;.
94f10 7d 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 }. return p==0
94f20 7c 7c 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c || p->nRef==0 ||
94f30 20 70 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a p->owner!=tid;.
94f40 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
94f50 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
94f60 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
94f70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
94f80 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
94f90 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
94fa0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
94fb0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
94fc0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
94fd0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
94fe0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
94ff0 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
95000 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
95010 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
95020 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
95030 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
95040 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
95050 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
95060 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
95070 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
95080 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
95090 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
950a0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
950b0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
950c0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
950d0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
950e0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
950f0 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
95100 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
95110 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
95120 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
95130 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
95140 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
95150 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
95160 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
95170 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
95180 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
95190 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
951a0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
951b0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
951c0 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 id os2MutexEnter
951d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
951e0 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
951f0 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 PID holder1;.
95200 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 ULONG holder2;.
95210 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
95220 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
95230 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
95240 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f X_RECURSIVE || o
95250 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 s2MutexNotheld(p
95260 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 73 ) );. DosReques
95270 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 tMutexSem(p->mut
95280 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 ex, SEM_INDEFINI
95290 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 51 TE_WAIT);. DosQ
952a0 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e ueryMutexSem(p->
952b0 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c mutex, &holder1,
952c0 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 &tid, &holder2)
952d0 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 ;. p->owner = t
952e0 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b id;. p->nRef++;
952f0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 .}.static int os
95300 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 2MutexTry(sqlite
95310 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
95320 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 64 nt rc;. TID tid
95330 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b ;. PID holder1;
95340 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 . ULONG holder2
95350 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
95360 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
95370 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
95380 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
95390 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d ECURSIVE || os2M
953a0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
953b0 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 65 ;. if( DosReque
953c0 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 stMutexSem(p->mu
953d0 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 41 tex, SEM_IMMEDIA
953e0 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e 4f TE_RETURN) == NO
953f0 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 6f _ERROR) {. Do
95400 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
95410 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 ->mutex, &holder
95420 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 1, &tid, &holder
95430 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 2);. p->owner
95440 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e 6e = tid;. p->n
95450 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
95460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 SQLITE_OK;. } e
95470 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 53 lse {. rc = S
95480 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a QLITE_BUSY;. }.
95490 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
954a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
954b0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
954c0 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
954d0 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
954e0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
954f0 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
95500 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
95510 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
95520 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
95530 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
95540 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
95550 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
95560 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
95570 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
95580 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
95590 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
955a0 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 MutexLeave(sqlit
955b0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
955c0 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
955d0 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
955e0 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
955f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
95600 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
95610 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 0 );. DosQueryM
95620 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 utexSem(p->mutex
95630 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 , &holder1, &tid
95640 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 61 , &holder2);. a
95650 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d ssert( p->owner=
95660 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 =tid );. p->nRe
95670 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
95680 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
95690 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
956a0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
956b0 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 53 DosReleaseMutexS
956c0 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a em(p->mutex);.}.
956d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
956e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
956f0 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 thods *sqlite3De
95700 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 faultMutex(void)
95710 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
95720 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
95730 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 sMutex = {.
95740 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 os2MutexInit,.
95750 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20 os2MutexEnd,.
95760 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 os2MutexAlloc
95770 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 46 72 ,. os2MutexFr
95780 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 ee,. os2Mutex
95790 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75 Enter,. os2Mu
957a0 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d texTry,. os2M
957b0 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 utexLeave,.#ifde
957c0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
957d0 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c os2MutexHeld,
957e0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74 . os2MutexNot
957f0 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b held.#endif. };
95800 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 .. return &sMut
95810 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ex;.}.#endif /*
95820 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
95830 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
95840 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 **** End of mute
95850 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_os2.c ********
95860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95880 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
95890 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
958a0 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a mutex_unix.c ***
958b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
958c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
958d0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
958e0 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 August 28.**.**
958f0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
95900 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
95910 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
95920 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
95930 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
95940 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
95950 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
95960 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
95970 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
95980 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
95990 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
959a0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
959b0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
959c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
959d0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
959e0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
959f0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
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 2a 2a 2a 2a 2a 2a 2a ****************
95a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95a40 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
95a50 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
95a60 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 he C functions t
95a70 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 hat implement mu
95a80 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 65 61 texes for pthrea
95a90 64 73 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ds.*/../*.** The
95aa0 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 code in this fi
95ab0 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 le is only used
95ac0 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c if we are compil
95ad0 69 6e 67 20 74 68 72 65 61 64 73 61 66 65 0a 2a ing threadsafe.*
95ae0 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 77 69 74 * under unix wit
95af0 68 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a h pthreads..**.*
95b00 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 * Note that this
95b10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
95b20 72 65 71 75 69 72 65 73 20 61 20 76 65 72 73 69 requires a versi
95b30 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 73 20 74 on of pthreads t
95b40 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 hat.** supports
95b50 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
95b60 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c s..*/.#ifdef SQL
95b70 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
95b80 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 DS..#include <pt
95b90 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a hread.h>.../*.**
95ba0 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 Each recursive
95bb0 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 mutex is an inst
95bc0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
95bd0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e owing structure.
95be0 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
95bf0 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 e3_mutex {. pth
95c00 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 read_mutex_t mut
95c10 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 ex; /* Mutex
95c20 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 controlling the
95c30 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 lock */. int i
95c40 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
95c50 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 /* Mutex t
95c60 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 ype */. int nRe
95c70 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
95c80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
95c90 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 entrances */.
95ca0 70 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b pthread_t owner;
95cb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
95cc0 72 65 61 64 20 74 68 61 74 20 69 73 20 77 69 74 read that is wit
95cd0 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78 20 2a hin this mutex *
95ce0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
95cf0 44 45 42 55 47 0a 20 20 69 6e 74 20 74 72 61 63 DEBUG. int trac
95d00 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
95d10 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 /* True to tr
95d20 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 ace changes */.#
95d30 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 endif.};.#ifdef
95d40 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 SQLITE_DEBUG.#de
95d50 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 fine SQLITE3_MUT
95d60 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b EX_INITIALIZER {
95d70 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 PTHREAD_MUTEX_I
95d80 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 NITIALIZER, 0, 0
95d90 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20 , (pthread_t)0,
95da0 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 0 }.#else.#defin
95db0 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f e SQLITE3_MUTEX_
95dc0 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 INITIALIZER { PT
95dd0 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 HREAD_MUTEX_INIT
95de0 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 IALIZER, 0, 0, (
95df0 70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 pthread_t)0 }.#e
95e00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
95e10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
95e20 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 ld() and sqlite3
95e30 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
95e40 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 routine are.**
95e50 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
95e60 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 only inside ass
95e70 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
95e80 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 . On some platf
95e90 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d orms,.** there m
95ea0 69 67 68 74 20 62 65 20 72 61 63 65 20 63 6f 6e ight be race con
95eb0 64 69 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e ditions that can
95ec0 20 63 61 75 73 65 20 74 68 65 73 65 20 72 6f 75 cause these rou
95ed0 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 tines to.** deli
95ee0 76 65 72 20 69 6e 63 6f 72 72 65 63 74 20 72 65 ver incorrect re
95ef0 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 72 74 69 sults. In parti
95f00 63 75 6c 61 72 2c 20 69 66 20 70 74 68 72 65 61 cular, if pthrea
95f10 64 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 d_equal() is.**
95f20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 not an atomic op
95f30 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 eration, then th
95f40 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 ese routines mig
95f50 68 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 ht delivery.** i
95f60 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 ncorrect results
95f70 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 . On most platf
95f80 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 5f 65 71 orms, pthread_eq
95f90 75 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 ual() is a .** c
95fa0 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f omparison of two
95fb0 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 69 73 integers and is
95fc0 20 74 68 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 therefore atomi
95fd0 63 2e 20 20 42 75 74 20 77 65 20 61 72 65 0a 2a c. But we are.*
95fe0 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 50 55 58 * told that HPUX
95ff0 20 69 73 20 6e 6f 74 20 73 75 63 68 20 61 20 70 is not such a p
96000 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c latform. If so,
96010 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 then these rout
96020 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 ines.** will not
96030 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 always work cor
96040 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a rectly on HPUX..
96050 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 **.** On those p
96060 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 65 20 70 latforms where p
96070 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 thread_equal() i
96080 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 s not atomic, SQ
96090 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 Lite.** should b
960a0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f e compiled witho
960b0 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 ut -DSQLITE_DEBU
960c0 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 G and with -DNDE
960d0 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 BUG to.** make s
960e0 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28 29 20 ure no assert()
960f0 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 statements are e
96100 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68 65 6e valuated and hen
96110 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 ce these.** rout
96120 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72 20 63 ines are never c
96130 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 alled..*/.#if !d
96140 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c efined(NDEBUG) |
96150 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
96160 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 69 _DEBUG).static i
96170 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 nt pthreadMutexH
96180 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
96190 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 x *p){. return
961a0 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 (p->nRef!=0 && p
961b0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e thread_equal(p->
961c0 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 owner, pthread_s
961d0 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 elf()));.}.stati
961e0 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 c int pthreadMut
961f0 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 exNotheld(sqlite
96200 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 3_mutex *p){. r
96210 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 eturn p->nRef==0
96220 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 71 75 61 || pthread_equa
96230 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 l(p->owner, pthr
96240 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a ead_self())==0;.
96250 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
96260 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 Initialize and d
96270 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
96280 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e mutex subsystem.
96290 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
962a0 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 28 threadMutexInit(
962b0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
962c0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
962d0 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 c int pthreadMut
962e0 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 exEnd(void){ ret
962f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
96300 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
96310 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
96320 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 ) routine alloca
96330 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 tes a new.** mut
96340 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 ex and returns a
96350 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 pointer to it.
96360 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e If it returns N
96370 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e ULL.** that mean
96380 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 s that a mutex c
96390 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f ould not be allo
963a0 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a cated. SQLite.*
963b0 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 * will unwind it
963c0 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 s stack and retu
963d0 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 rn an error. Th
963e0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f e argument.** to
963f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
96400 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 lloc() is one of
96410 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 these integer c
96420 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 onstants:.**.**
96430 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 <ul>.** <li> SQ
96440 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a LITE_MUTEX_FAST.
96450 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
96460 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a MUTEX_RECURSIVE.
96470 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
96480 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
96490 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c TER.** <li> SQL
964a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
964b0 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 _MEM.** <li> SQ
964c0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
964d0 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 C_MEM2.** <li>
964e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
964f0 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e TIC_PRNG.** <li>
96500 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
96510 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 TATIC_LRU.** <li
96520 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
96530 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c STATIC_LRU2.** <
96540 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 /ul>.**.** The f
96550 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e irst two constan
96560 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 ts cause sqlite3
96570 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 _mutex_alloc() t
96580 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 o create.** a ne
96590 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 w mutex. The ne
965a0 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 w mutex is recur
965b0 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 sive when SQLITE
965c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
965d0 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 .** is used but
965e0 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 not necessarily
965f0 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d so when SQLITE_M
96600 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 UTEX_FAST is use
96610 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 d..** The mutex
96620 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 implementation d
96630 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
96640 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 make a distincti
96650 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 on.** between SQ
96660 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
96670 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f SIVE and SQLITE_
96680 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 MUTEX_FAST if it
96690 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e does.** not wan
966a0 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 t to. But SQLit
966b0 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 e will only requ
966c0 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 est a recursive
966d0 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 mutex in.** case
966e0 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c s where it reall
966f0 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 y needs one. If
96700 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 a faster non-re
96710 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a cursive mutex.**
96720 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
96730 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 is available on
96740 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 the host platfor
96750 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 m, the mutex sub
96760 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 system.** might
96770 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 return such a mu
96780 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 tex in response
96790 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f to SQLITE_MUTEX_
967a0 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 FAST..**.** The
967b0 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 other allowed pa
967c0 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 rameters to sqli
967d0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
967e0 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a ) each return.**
967f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
96800 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 static preexisti
96810 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 ng mutex. Six s
96820 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 tatic mutexes ar
96830 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 e.** used by the
96840 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e current version
96850 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 of SQLite. Fut
96860 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ure versions of
96870 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 SQLite.** may ad
96880 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 d additional sta
96890 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 tic mutexes. St
968a0 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 atic mutexes are
968b0 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a for internal.**
968c0 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f use by SQLite o
968d0 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f nly. Applicatio
968e0 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 ns that use SQLi
968f0 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c te mutexes shoul
96900 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 d.** use only th
96910 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 e dynamic mutexe
96920 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 s returned by SQ
96930 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
96940 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 or.** SQLITE_MUT
96950 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a EX_RECURSIVE..**
96960 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 .** Note that if
96970 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 one of the dyna
96980 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 mic mutex parame
96990 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 ters (SQLITE_MUT
969a0 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 EX_FAST.** or SQ
969b0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
969c0 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 SIVE) is used th
969d0 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 en sqlite3_mutex
969e0 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 _alloc().** retu
969f0 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 rns a different
96a00 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 mutex on every c
96a10 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 all. But for th
96a20 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 e static .** mut
96a30 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 ex types, the sa
96a40 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 me mutex is retu
96a50 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 rned on every ca
96a60 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 ll that has.** t
96a70 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d he same type num
96a80 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ber..*/.static s
96a90 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 qlite3_mutex *pt
96aa0 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 hreadMutexAlloc(
96ab0 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 74 int iType){. st
96ac0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 atic sqlite3_mut
96ad0 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex staticMutexes
96ae0 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 [] = {. SQLIT
96af0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
96b00 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
96b10 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
96b20 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 ZER,. SQLITE3
96b30 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
96b40 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f ER,. SQLITE3_
96b50 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
96b60 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d R,. SQLITE3_M
96b70 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
96b80 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 ,. SQLITE3_MU
96b90 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a TEX_INITIALIZER.
96ba0 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d };. sqlite3_m
96bb0 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 utex *p;. switc
96bc0 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 h( iType ){.
96bd0 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 case SQLITE_MUTE
96be0 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 X_RECURSIVE: {.
96bf0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p = sqlite3
96c00 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
96c10 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 of(*p) );.
96c20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65 66 20 if( p ){.#ifdef
96c30 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e SQLITE_HOMEGROWN
96c40 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 _RECURSIVE_MUTEX
96c50 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 72 . /* If r
96c60 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
96c70 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 are not availab
96c80 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 61 76 65 le, we will have
96c90 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 to. ** b
96ca0 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 uild our own. S
96cb0 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 ee below. */.
96cc0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
96cd0 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 ex_init(&p->mute
96ce0 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 x, 0);.#else.
96cf0 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 65 /* Use a re
96d00 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 66 cursive mutex if
96d10 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 it is available
96d20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 */. pthr
96d30 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 ead_mutexattr_t
96d40 72 65 63 75 72 73 69 76 65 41 74 74 72 3b 0a 20 recursiveAttr;.
96d50 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96d60 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28 26 72 utexattr_init(&r
96d70 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 ecursiveAttr);.
96d80 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96d90 75 74 65 78 61 74 74 72 5f 73 65 74 74 79 70 65 utexattr_settype
96da0 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 2c (&recursiveAttr,
96db0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 PTHREAD_MUTEX_R
96dc0 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20 ECURSIVE);.
96dd0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 pthread_mutex
96de0 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c _init(&p->mutex,
96df0 20 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 &recursiveAttr)
96e00 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 ;. pthrea
96e10 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 65 73 74 d_mutexattr_dest
96e20 72 6f 79 28 26 72 65 63 75 72 73 69 76 65 41 74 roy(&recursiveAt
96e30 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 tr);.#endif.
96e40 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 p->id = iTyp
96e50 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 e;. }.
96e60 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
96e70 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
96e80 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 TEX_FAST: {.
96e90 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
96ea0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
96eb0 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 *p) );. if(
96ec0 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d p ){. p-
96ed0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 >id = iType;.
96ee0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
96ef0 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 ex_init(&p->mute
96f00 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 x, 0);. }.
96f10 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
96f20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
96f30 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
96f40 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 Type-2 >= 0 );.
96f50 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 assert( iTy
96f60 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69 7a 65 pe-2 < ArraySize
96f70 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 (staticMutexes)
96f80 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 );. p = &st
96f90 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 aticMutexes[iTyp
96fa0 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 e-2];. p->i
96fb0 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 d = iType;.
96fc0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
96fd0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
96fe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
96ff0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 tine deallocates
97000 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a a previously.**
97010 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 allocated mutex
97020 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 . SQLite is car
97030 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 eful to dealloca
97040 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 te every.** mute
97050 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 x that it alloca
97060 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tes..*/.static v
97070 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 oid pthreadMutex
97080 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 Free(sqlite3_mut
97090 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ex *p){. assert
970a0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
970b0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
970c0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 =SQLITE_MUTEX_FA
970d0 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c ST || p->id==SQL
970e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
970f0 49 56 45 20 29 3b 0a 20 20 70 74 68 72 65 61 64 IVE );. pthread
97100 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f 79 28 26 _mutex_destroy(&
97110 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c p->mutex);. sql
97120 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a ite3_free(p);.}.
97130 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
97140 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
97150 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
97160 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
97170 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 s attempt.** to
97180 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 enter a mutex.
97190 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 If another threa
971a0 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 d is already wit
971b0 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a hin the mutex,.*
971c0 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f * sqlite3_mutex_
971d0 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f enter() will blo
971e0 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d ck and sqlite3_m
971f0 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 utex_try() will
97200 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 return.** SQLITE
97210 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 _BUSY. The sqli
97220 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
97230 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
97240 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 s SQLITE_OK.** u
97250 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 pon successful e
97260 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 ntry. Mutexes c
97270 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c reated using SQL
97280 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
97290 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e IVE can.** be en
972a0 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 tered multiple t
972b0 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 imes by the same
972c0 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 thread. In suc
972d0 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 h cases the,.**
972e0 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 mutex must be ex
972f0 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 ited an equal nu
97300 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 mber of times be
97310 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 fore another thr
97320 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 ead.** can enter
97330 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 . If the same t
97340 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 hread tries to e
97350 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b nter any other k
97360 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 ind of mutex.**
97370 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 more than once,
97380 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 the behavior is
97390 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 undefined..*/.st
973a0 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 atic void pthrea
973b0 64 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 dMutexEnter(sqli
973c0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
973d0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
973e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
973f0 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 URSIVE || pthrea
97400 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 dMutexNotheld(p)
97410 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
97420 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 TE_HOMEGROWN_REC
97430 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f URSIVE_MUTEX. /
97440 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d * If recursive m
97450 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 utexes are not a
97460 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 vailable, then w
97470 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 e have to grow.
97480 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 ** our own. Th
97490 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
974a0 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 n assumes that p
974b0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 thread_equal().
974c0 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 ** is atomic -
974d0 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 that it cannot b
974e0 65 20 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 e deceived into
974f0 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 thinking self.
97500 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 ** and p->owner
97510 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e are equal if p->
97520 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 owner changes be
97530 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 tween two values
97540 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e . ** that are n
97550 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 ot equal to self
97560 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 while the compa
97570 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 rison is taking
97580 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 place.. ** This
97590 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
975a0 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 also assumes a c
975b0 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 oherent cache -
975c0 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 that . ** separ
975d0 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 ate processes ca
975e0 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 nnot read differ
975f0 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 ent values from
97600 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 the same. ** ad
97610 64 72 65 73 73 20 61 74 20 74 68 65 20 73 61 6d dress at the sam
97620 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 e time. If eith
97630 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 er of these two
97640 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 conditions. **
97650 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 are not met, the
97660 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 n the mutexes wi
97670 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 ll fail and prob
97680 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 lems will result
97690 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 .. */. {. p
976a0 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 thread_t self =
976b0 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a pthread_self();.
976c0 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e if( p->nRef>
976d0 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 0 && pthread_equ
976e0 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c al(p->owner, sel
976f0 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e f) ){. p->n
97700 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 Ref++;. }else
97710 7b 0a 20 20 20 20 20 20 70 74 68 72 65 61 64 5f {. pthread_
97720 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d mutex_lock(&p->m
97730 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 utex);. ass
97740 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
97750 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 );. p->owne
97760 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 r = self;.
97770 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 p->nRef = 1;.
97780 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f }. }.#else. /
97790 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d * Use the built-
977a0 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 in recursive mut
977b0 65 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 exes if they are
977c0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f available.. */
977d0 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 . pthread_mutex
977e0 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 _lock(&p->mutex)
977f0 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 ;. p->owner = p
97800 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 thread_self();.
97810 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 p->nRef++;.#end
97820 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
97830 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d E_DEBUG. if( p-
97840 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 >trace ){. pr
97850 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 intf("enter mute
97860 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e x %p (%d) with n
97870 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d Ref=%d\n", p, p-
97880 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 >trace, p->nRef)
97890 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 ;. }.#endif.}.s
978a0 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 tatic int pthrea
978b0 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 dMutexTry(sqlite
978c0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
978d0 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
978e0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
978f0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c UTEX_RECURSIVE |
97900 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f | pthreadMutexNo
97910 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 theld(p) );..#if
97920 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 def SQLITE_HOMEG
97930 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d ROWN_RECURSIVE_M
97940 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 UTEX. /* If rec
97950 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 ursive mutexes a
97960 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 re not available
97970 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65 20 74 , then we have t
97980 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 o grow. ** our
97990 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 own. This imple
979a0 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 mentation assume
979b0 73 20 74 68 61 74 20 70 74 68 72 65 61 64 5f 65 s that pthread_e
979c0 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 qual(). ** is a
979d0 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 74 20 tomic - that it
979e0 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 69 76 cannot be deceiv
979f0 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 ed into thinking
97a00 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 self. ** and p
97a10 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 75 61 ->owner are equa
97a20 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 l if p->owner ch
97a30 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20 74 77 anges between tw
97a40 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 o values. ** th
97a50 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c at are not equal
97a60 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 20 74 to self while t
97a70 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 he comparison is
97a80 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 taking place..
97a90 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 ** This impleme
97aa0 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 ntation also ass
97ab0 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e 74 20 umes a coherent
97ac0 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a 20 20 cache - that .
97ad0 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 ** separate proc
97ae0 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 esses cannot rea
97af0 64 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 d different valu
97b00 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 es from the same
97b10 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20 61 74 . ** address at
97b20 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 the same time.
97b30 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 If either of th
97b40 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f ese two conditio
97b50 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 ns. ** are not
97b60 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 met, then the mu
97b70 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 texes will fail
97b80 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c and problems wil
97b90 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 l result.. */.
97ba0 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74 {. pthread_t
97bb0 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f self = pthread_
97bc0 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 28 20 self();. if(
97bd0 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 74 68 p->nRef>0 && pth
97be0 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 read_equal(p->ow
97bf0 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 ner, self) ){.
97c00 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 p->nRef++;.
97c10 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
97c20 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 _OK;. }else i
97c30 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 f( pthread_mutex
97c40 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 _trylock(&p->mut
97c50 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ex)==0 ){.
97c60 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d assert( p->nRef=
97c70 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f =0 );. p->o
97c80 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 wner = self;.
97c90 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a p->nRef = 1;.
97ca0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
97cb0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b E_OK;. }else{
97cc0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
97cd0 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 TE_BUSY;. }.
97ce0 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 }.#else. /* Us
97cf0 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 e the built-in r
97d00 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
97d10 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76 61 if they are ava
97d20 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 ilable.. */. i
97d30 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 f( pthread_mutex
97d40 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 _trylock(&p->mut
97d50 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d ex)==0 ){. p-
97d60 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64 >owner = pthread
97d70 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e _self();. p->
97d80 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d nRef++;. rc =
97d90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
97da0 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 lse{. rc = SQ
97db0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 LITE_BUSY;. }.#
97dc0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
97dd0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 LITE_DEBUG. if(
97de0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
97df0 26 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 & p->trace ){.
97e00 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 printf("enter
97e10 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 mutex %p (%d) wi
97e20 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 th nRef=%d\n", p
97e30 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e , p->trace, p->n
97e40 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 Ref);. }.#endif
97e50 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
97e60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
97e70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
97e80 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
97e90 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
97ea0 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
97eb0 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
97ec0 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
97ed0 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e ehavior.** is un
97ee0 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d defined if the m
97ef0 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 utex is not curr
97f00 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 ently entered or
97f10 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
97f20 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ntly allocated.
97f30 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
97f40 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f er do either..*/
97f50 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 .static void pth
97f60 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 28 73 readMutexLeave(s
97f70 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
97f80 7b 0a 20 20 61 73 73 65 72 74 28 20 70 74 68 72 {. assert( pthr
97f90 65 61 64 4d 75 74 65 78 48 65 6c 64 28 70 29 20 eadMutexHeld(p)
97fa0 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a );. p->nRef--;.
97fb0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
97fc0 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 f==0 || p->id==S
97fd0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
97fe0 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 RSIVE );..#ifdef
97ff0 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 SQLITE_HOMEGROW
98000 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 N_RECURSIVE_MUTE
98010 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d X. if( p->nRef=
98020 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72 65 61 =0 ){. pthrea
98030 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 d_mutex_unlock(&
98040 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 p->mutex);. }.#
98050 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64 5f 6d else. pthread_m
98060 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e utex_unlock(&p->
98070 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a mutex);.#endif..
98080 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
98090 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 BUG. if( p->tra
980a0 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 ce ){. printf
980b0 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25 70 ("leave mutex %p
980c0 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d (%d) with nRef=
980d0 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 %d\n", p, p->tra
980e0 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 ce, p->nRef);.
980f0 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 }.#endif.}..SQLI
98100 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
98110 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
98120 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 *sqlite3Default
98130 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 Mutex(void){. s
98140 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 tatic sqlite3_mu
98150 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 tex_methods sMut
98160 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 ex = {. pthre
98170 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 adMutexInit,.
98180 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 64 pthreadMutexEnd
98190 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 ,. pthreadMut
981a0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 exAlloc,. pth
981b0 72 65 61 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 readMutexFree,.
981c0 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 pthreadMutexE
981d0 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 72 65 61 nter,. pthrea
981e0 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 70 dMutexTry,. p
981f0 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 threadMutexLeave
98200 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ,.#ifdef SQLITE_
98210 44 45 42 55 47 0a 20 20 20 20 70 74 68 72 65 61 DEBUG. pthrea
98220 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 dMutexHeld,.
98230 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 pthreadMutexNoth
98240 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c eld.#else. 0,
98250 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 . 0.#endif.
98260 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d };.. return &sM
98270 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 utex;.}..#endif
98280 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f /* SQLITE_MUTEX_
98290 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a PTHREAD */../***
982a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
982b0 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 of mutex_unix.c
982c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
982d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
982e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
982f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
98300 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 n file mutex_w32
98310 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
98320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
98340 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 * 2007 August 14
98350 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
98360 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
98370 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
98380 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
98390 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
983a0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
983b0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
983c0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
983d0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
983e0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
983f0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
98400 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
98410 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
98420 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
98430 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
98440 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
98450 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
98460 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
984a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
984b0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
984c0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
984d0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
984e0 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 ment mutexes for
984f0 20 77 69 6e 33 32 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a win32.*/../*.**
98500 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 The code in thi
98510 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 s file is only u
98520 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f sed if we are co
98530 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 mpiling multithr
98540 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 eaded.** on a wi
98550 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 n32 system..*/.#
98560 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 ifdef SQLITE_MUT
98570 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 EX_W32../*.** Ea
98580 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ch recursive mut
98590 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ex is an instanc
985a0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
985b0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ng structure..*/
985c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
985d0 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43 mutex {. CRITIC
985e0 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 AL_SECTION mutex
985f0 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f ; /* Mutex co
98600 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f ntrolling the lo
98610 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 ck */. int id;
98620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98630 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
98640 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
98650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
98660 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
98670 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57 terances */. DW
98680 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 ORD owner;
98690 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 /* Thre
986a0 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 ad holding this
986b0 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a mutex */.};../*.
986c0 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 ** Return true (
986d0 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 non-zero) if we
986e0 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 are running unde
986f0 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 r WinNT, Win2K,
98700 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e WinXP,.** or Win
98710 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 CE. Return fals
98720 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e e (zero) for Win
98730 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 95, Win98, or Wi
98740 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 nME..**.** Here
98750 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e is an interestin
98760 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 g observation:
98770 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e Win95, Win98, an
98780 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 d WinME lack.**
98790 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 the LockFileEx()
987a0 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 API. But we ca
987b0 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c n still statical
987c0 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 ly link against
987d0 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c that.** API as l
987e0 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 ong as we don't
987f0 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e call it win runn
98800 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e ing Win95/98/ME.
98810 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 A call to.** t
98820 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
98830 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
98840 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 if the host is
98850 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a Win95/98/ME or.*
98860 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f * WinNT/2K/XP so
98870 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e that we will kn
98880 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ow whether or no
98890 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 t we can safely
988a0 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b call.** the Lock
988b0 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2a FileEx() API..**
988c0 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 29 20 .** mutexIsNT()
988d0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 is only used for
988e0 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 72 69 the TryEnterCri
988f0 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 41 ticalSection() A
98900 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 63 PI call,.** whic
98910 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 h is only availa
98920 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 70 6c ble if your appl
98930 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f 6d 70 ication was comp
98940 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 5f 57 iled with .** _W
98950 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 69 6e IN32_WINNT defin
98960 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 3e 3d ed to a value >=
98970 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 65 6e 0x0400. Curren
98980 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a tly, the only.**
98990 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e 74 65 call to TryEnte
989a0 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e rCriticalSection
989b0 28 29 20 69 73 20 23 69 66 64 65 66 27 65 64 20 () is #ifdef'ed
989c0 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 20 0a out, so #ifdef .
989d0 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 20 77 ** this out as w
989e0 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 69 ell..*/.#if 0.#i
989f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
98a00 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78 E.# define mutex
98a10 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 IsNT() (1).#els
98a20 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d e. static int m
98a30 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a utexIsNT(void){.
98a40 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f static int o
98a50 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 sType = 0;. i
98a60 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a f( osType==0 ){.
98a70 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 OSVERSIONI
98a80 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 NFO sInfo;.
98a90 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 sInfo.dwOSVersi
98aa0 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a onInfoSize = siz
98ab0 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 eof(sInfo);.
98ac0 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 GetVersionEx(&
98ad0 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 sInfo);. os
98ae0 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 Type = sInfo.dwP
98af0 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 latformId==VER_P
98b00 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 LATFORM_WIN32_NT
98b10 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a ? 2 : 1;. }.
98b20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70 return osTyp
98b30 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 e==2;. }.#endif
98b40 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 /* SQLITE_OS_WI
98b50 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 NCE */.#endif..#
98b60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
98b70 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c UG./*.** The sql
98b80 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
98b90 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
98ba0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f tex_notheld() ro
98bb0 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 utine are.** int
98bc0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e ended for use on
98bd0 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ly inside assert
98be0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
98bf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
98c00 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 MutexHeld(sqlite
98c10 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 3_mutex *p){. r
98c20 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 eturn p->nRef!=0
98c30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 && p->owner==Ge
98c40 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 tCurrentThreadId
98c50 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 ();.}.static int
98c60 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
98c70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
98c80 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
98c90 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 nRef==0 || p->ow
98ca0 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e 74 54 ner!=GetCurrentT
98cb0 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e hreadId();.}.#en
98cc0 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 dif.../*.** Init
98cd0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
98ce0 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 tialize the mute
98cf0 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a x subsystem..*/.
98d00 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
98d10 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73 74 utex winMutex_st
98d20 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 3b 0a aticMutexes[6];.
98d30 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 static int winMu
98d40 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a tex_isInit = 0;.
98d50 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 49 6e /* As winMutexIn
98d60 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 74 65 it() and winMute
98d70 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c 6c 65 xEnd() are calle
98d80 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 d as part.** of
98d90 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 the sqlite3_init
98da0 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c 69 74 ialize and sqlit
98db0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a e3_shutdown().**
98dc0 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 68 65 processing, the
98dd0 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 20 6d "interlocked" m
98de0 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 6c 79 agic is probably
98df0 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 6c 79 not.** strictly
98e00 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 necessary..*/.s
98e10 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e 4d 75 tatic long winMu
98e20 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a 0a 73 tex_lock = 0;..s
98e30 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 tatic int winMut
98e40 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 exInit(void){ .
98e50 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 6f /* The first to
98e60 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 31 20 increment to 1
98e70 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 69 74 does actual init
98e80 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 ialization */.
98e90 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 if( InterlockedC
98ea0 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26 ompareExchange(&
98eb0 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 31 winMutex_lock, 1
98ec0 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 , 0)==0 ){. i
98ed0 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
98ee0 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 77 0; i<ArraySize(w
98ef0 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 inMutex_staticMu
98f00 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 texes); i++){.
98f10 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 InitializeCr
98f20 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77 iticalSection(&w
98f30 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 inMutex_staticMu
98f40 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b texes[i].mutex);
98f50 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e 4d 75 . }. winMu
98f60 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 3b 0a tex_isInit = 1;.
98f70 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
98f80 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 73 20 Someone else is
98f90 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f in the process o
98fa0 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 73 74 f initing the st
98fb0 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a 2f 0a atic mutexes */.
98fc0 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 6e 4d while( !winM
98fd0 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b 0a 20 utex_isInit ){.
98fe0 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 Sleep(1);.
98ff0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
99000 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
99010 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
99020 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 0a utexEnd(void){ .
99030 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 /* The first t
99040 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f 20 30 o decrement to 0
99050 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 68 75 does actual shu
99060 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 68 69 tdown . ** (whi
99070 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 ch should be the
99080 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 6f 77 last to shutdow
99090 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 6e 74 n.) */. if( Int
990a0 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 65 45 erlockedCompareE
990b0 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 74 65 xchange(&winMute
990c0 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d 3d 31 x_lock, 0, 1)==1
990d0 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 6e 4d ){. if( winM
990e0 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29 utex_isInit==1 )
990f0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
99100 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
99110 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74 ArraySize(winMut
99120 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
99130 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ); i++){.
99140 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 DeleteCriticalS
99150 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78 ection(&winMutex
99160 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 _staticMutexes[i
99170 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 ].mutex);.
99180 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 65 78 }. winMutex
99190 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 _isInit = 0;.
991a0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
991b0 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f SQLITE_OK; .}../
991c0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
991d0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
991e0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
991f0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
99200 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
99210 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
99220 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
99230 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
99240 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
99250 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
99260 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ed. SQLite.** w
99270 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
99280 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
99290 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 an error. The a
992a0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 rgument.** to sq
992b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
992c0 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 c() is one of th
992d0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
992e0 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c tants:.**.** <ul
992f0 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 >.** <li> SQLIT
99300 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
99310 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
99320 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
99330 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
99340 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
99350 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
99360 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
99370 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 M.** <li> SQLIT
99380 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
99390 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c EM2.** <li> SQL
993a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
993b0 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _PRNG.** <li> S
993c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
993d0 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 IC_LRU.** <li>
993e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
993f0 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c TIC_LRU2.** </ul
99400 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 >.**.** The firs
99410 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
99420 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
99430 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
99440 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
99450 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
99460 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
99470 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
99480 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
99490 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
994a0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
994b0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
994c0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a X_FAST is used..
994d0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 ** The mutex imp
994e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
994f0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
99500 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a e a distinction.
99510 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 ** between SQLIT
99520 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
99530 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 E and SQLITE_MUT
99540 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f EX_FAST if it do
99550 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 es.** not want t
99560 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 o. But SQLite w
99570 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
99580 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
99590 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
995a0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
995b0 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 eeds one. If a
995c0 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 faster non-recur
995d0 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d sive mutex.** im
995e0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
995f0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 available on the
99600 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 host platform,
99610 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
99620 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 tem.** might ret
99630 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 urn such a mutex
99640 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
99650 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
99660 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 T..**.** The oth
99670 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d er allowed param
99680 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 eters to sqlite3
99690 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 _mutex_alloc() e
996a0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ach return.** a
996b0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
996c0 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 tic preexisting
996d0 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 mutex. Six stat
996e0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a ic mutexes are.*
996f0 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 * used by the cu
99700 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 rrent version of
99710 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 SQLite. Future
99720 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
99730 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 ite.** may add a
99740 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
99750 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 mutexes. Stati
99760 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f c mutexes are fo
99770 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
99780 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 e by SQLite only
99790 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 . Applications
997a0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 that use SQLite
997b0 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a mutexes should.*
997c0 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 * use only the d
997d0 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 ynamic mutexes r
997e0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 eturned by SQLIT
997f0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a E_MUTEX_FAST or.
99800 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
99810 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a RECURSIVE..**.**
99820 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e Note that if on
99830 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 e of the dynamic
99840 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 mutex parameter
99850 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f s (SQLITE_MUTEX_
99860 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 FAST.** or SQLIT
99870 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
99880 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 E) is used then
99890 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
998a0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 loc().** returns
998b0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 a different mut
998c0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ex on every call
998d0 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 . But for the s
998e0 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 tatic .** mutex
998f0 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 types, the same
99900 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 mutex is returne
99910 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 d on every call
99920 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 that has.** the
99930 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 same type number
99940 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 ..*/.static sqli
99950 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d 75 te3_mutex *winMu
99960 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 texAlloc(int iTy
99970 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d pe){. sqlite3_m
99980 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69 74 utex *p;.. swit
99990 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 ch( iType ){.
999a0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 case SQLITE_MUT
999b0 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 EX_FAST:. cas
999c0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
999d0 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 ECURSIVE: {.
999e0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
999f0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
99a00 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 *p) );. if(
99a10 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 20 20 p ){ .
99a20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
99a30 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a Initializ
99a40 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e eCriticalSection
99a50 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 (&p->mutex);.
99a60 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
99a70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
99a80 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ult: {. ass
99a90 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 ert( winMutex_is
99aa0 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 Init==1 );.
99ab0 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
99ac0 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 >= 0 );. a
99ad0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c ssert( iType-2 <
99ae0 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 ArraySize(winMu
99af0 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 tex_staticMutexe
99b00 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 s) );. p =
99b10 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 &winMutex_static
99b20 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d Mutexes[iType-2]
99b30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 ;. p->id =
99b40 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 iType;. bre
99b50 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
99b60 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a return p;.}.../*
99b70 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
99b80 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 deallocates a p
99b90 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c reviously.** all
99ba0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53 ocated mutex. S
99bb0 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c QLite is careful
99bc0 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 to deallocate e
99bd0 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68 very.** mutex th
99be0 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
99bf0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
99c00 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c winMutexFree(sql
99c10 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
99c20 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 assert( p );.
99c30 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
99c40 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
99c50 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
99c60 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
99c70 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
99c80 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
99c90 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65 DeleteCriticalSe
99ca0 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 ction(&p->mutex)
99cb0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
99cc0 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 (p);.}../*.** Th
99cd0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
99ce0 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 enter() and sqli
99cf0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
99d00 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
99d10 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d .** to enter a m
99d20 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 utex. If anothe
99d30 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 r thread is alre
99d40 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d ady within the m
99d50 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 utex,.** sqlite3
99d60 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 _mutex_enter() w
99d70 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 ill block and sq
99d80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
99d90 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a ) will return.**
99da0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 SQLITE_BUSY. T
99db0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
99dc0 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 _try() interface
99dd0 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
99de0 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 OK.** upon succe
99df0 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 ssful entry. Mu
99e00 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 texes created us
99e10 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ing SQLITE_MUTEX
99e20 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a _RECURSIVE can.*
99e30 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c * be entered mul
99e40 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 tiple times by t
99e50 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 he same thread.
99e60 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 In such cases t
99e70 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 he,.** mutex mus
99e80 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 t be exited an e
99e90 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 qual number of t
99ea0 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 imes before anot
99eb0 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 her thread.** ca
99ec0 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 n enter. If the
99ed0 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 same thread tri
99ee0 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 es to enter any
99ef0 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 other kind of mu
99f00 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e tex.** more than
99f10 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 once, the behav
99f20 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ior is undefined
99f30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
99f40 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73 winMutexEnter(s
99f50 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
99f60 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 {. assert( p->i
99f70 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
99f80 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e RECURSIVE || win
99f90 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
99fa0 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63 );. EnterCritic
99fb0 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
99fc0 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 tex);. p->owner
99fd0 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 = GetCurrentThr
99fe0 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e eadId(); . p->n
99ff0 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 Ref++;.}.static
9a000 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28 int winMutexTry(
9a010 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
9a020 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
9a030 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73 LITE_BUSY;. ass
9a040 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
9a050 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
9a060 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f VE || winMutexNo
9a070 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a theld(p) );. /*
9a080 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 . ** The sqlite
9a090 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
9a0a0 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61 utine is very ra
9a0b0 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77 rely used, and w
9a0c0 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75 hen it. ** is u
9a0d0 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79 sed it is merely
9a0e0 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
9a0f0 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66 . So it is OK f
9a100 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a or it to always.
9a110 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a ** fail. . *
9a120 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e *. ** The TryEn
9a130 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
9a140 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 on() interface i
9a150 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
9a160 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20 on WinNT.. **
9a170 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73 And some windows
9a180 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c compilers compl
9a190 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74 ain if you try t
9a1a0 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74 o use it without
9a1b0 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e . ** first doin
9a1c0 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20 g some #defines
9a1d0 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c that prevent SQL
9a1e0 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e ite from buildin
9a1f0 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a g on Win98.. **
9a200 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e For that reason
9a210 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74 , we will omit t
9a220 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e his optimization
9a230 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20 for now. See.
9a240 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35 ** ticket #2685
9a250 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69 .. */.#if 0. i
9a260 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26 f( mutexIsNT() &
9a270 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 & TryEnterCritic
9a280 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 alSection(&p->mu
9a290 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f tex) ){. p->o
9a2a0 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e wner = GetCurren
9a2b0 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20 tThreadId();.
9a2c0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
9a2d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
9a2e0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 }.#else. UNUS
9a2f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b ED_PARAMETER(p);
9a300 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
9a310 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
9a320 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
9a330 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 leave() routine
9a340 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 exits a mutex th
9a350 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f at was.** previo
9a360 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 usly entered by
9a370 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
9a380 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a The behavior.*
9a390 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 * is undefined i
9a3a0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e f the mutex is n
9a3b0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ot currently ent
9a3c0 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f ered or.** is no
9a3d0 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f t currently allo
9a3e0 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 cated. SQLite w
9a3f0 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 ill never do eit
9a400 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 her..*/.static v
9a410 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 oid winMutexLeav
9a420 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
9a430 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
9a440 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61 73 ->nRef>0 );. as
9a450 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d sert( p->owner==
9a460 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
9a470 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 Id() );. p->nRe
9a480 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 f--;. assert( p
9a490 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
9a4a0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
9a4b0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
9a4c0 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 65 63 LeaveCriticalSec
9a4d0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b tion(&p->mutex);
9a4e0 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .}..SQLITE_PRIVA
9a4f0 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 TE sqlite3_mutex
9a500 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 _methods *sqlite
9a510 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 3DefaultMutex(vo
9a520 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
9a530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
9a540 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 ods sMutex = {.
9a550 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 2c winMutexInit,
9a560 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 64 . winMutexEnd
9a570 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c ,. winMutexAl
9a580 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 loc,. winMute
9a590 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d 75 xFree,. winMu
9a5a0 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 69 texEnter,. wi
9a5b0 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 77 nMutexTry,. w
9a5c0 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 inMutexLeave,.#i
9a5d0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
9a5e0 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 48 65 G. winMutexHe
9a5f0 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 ld,. winMutex
9a600 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 Notheld.#else.
9a610 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 0,. 0.#endi
9a620 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
9a630 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 &sMutex;.}.#end
9a640 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 if /* SQLITE_MUT
9a650 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a EX_W32 */../****
9a660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
9a670 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a f mutex_w32.c **
9a680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
9a6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
9a6c0 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a file malloc.c *
9a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
9a700 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
9a710 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
9a720 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
9a730 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
9a740 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
9a750 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
9a760 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
9a770 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
9a780 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
9a790 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
9a7a0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
9a7b0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
9a7c0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
9a7d0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
9a7e0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
9a7f0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
9a800 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
9a810 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
9a820 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
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 2a ****************
9a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a870 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 *.**.** Memory a
9a880 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 llocation functi
9a890 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 ons used through
9a8a0 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2f 0a 0a out sqlite..*/..
9a8b0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
9a8c0 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74 68 65 ne runs when the
9a8d0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
9a8e0 72 20 73 65 65 73 20 74 68 61 74 20 74 68 65 0a r sees that the.
9a8f0 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 ** total memory
9a900 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 allocation is ab
9a910 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20 74 68 out to exceed th
9a920 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c e soft heap.** l
9a930 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 imit..*/.static
9a940 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c 69 6d void softHeapLim
9a950 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f itEnforcer(. vo
9a960 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20 id *NotUsed, .
9a970 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e 6f sqlite3_int64 No
9a980 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20 61 6c tUsed2,. int al
9a990 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 locSize.){. UNU
9a9a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
9a9b0 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
9a9c0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c );. sqlite3_rel
9a9d0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f ease_memory(allo
9a9e0 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cSize);.}../*.**
9a9f0 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 68 65 Set the soft he
9aa00 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f ap-size limit fo
9aa10 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 50 r the library. P
9aa20 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f 72 assing a zero or
9aa30 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61 .** negative va
9aa40 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f lue indicates no
9aa50 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 limit..*/.SQLIT
9aa60 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
9aa70 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
9aa80 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c it(int n){. sql
9aa90 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d ite3_uint64 iLim
9aaa0 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67 it;. int overag
9aab0 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a e;. if( n<0 ){.
9aac0 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a iLimit = 0;.
9aad0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 }else{. iLi
9aae0 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 23 69 66 mit = n;. }.#if
9aaf0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
9ab00 5f 41 55 54 4f 49 4e 49 54 0a 20 20 73 71 6c 69 _AUTOINIT. sqli
9ab10 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
9ab20 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 ;.#endif. if( i
9ab30 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 Limit>0 ){. s
9ab40 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
9ab50 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 m(softHeapLimitE
9ab60 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d nforcer, 0, iLim
9ab70 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 it);. }else{.
9ab80 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 sqlite3MemoryA
9ab90 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20 larm(0, 0, 0);.
9aba0 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d 20 28 }. overage = (
9abb0 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 6d 65 6d int)(sqlite3_mem
9abc0 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 28 69 36 ory_used() - (i6
9abd0 34 29 6e 29 3b 0a 20 20 69 66 28 20 6f 76 65 72 4)n);. if( over
9abe0 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c age>0 ){. sql
9abf0 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
9ac00 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20 20 ory(overage);.
9ac10 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d }.}../*.** Attem
9ac20 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75 70 pt to release up
9ac30 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20 6e to n bytes of n
9ac40 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
9ac50 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a ory currently.**
9ac60 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65 2e held by SQLite.
9ac70 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e An example of n
9ac80 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
9ac90 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75 73 ory is memory us
9aca0 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20 64 ed to.** cache d
9acb0 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74 68 atabase pages th
9acc0 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 at are not curre
9acd0 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a ntly in use..*/.
9ace0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
9acf0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
9ad00 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23 69 emory(int n){.#i
9ad10 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
9ad20 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 LE_MEMORY_MANAGE
9ad30 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74 20 MENT. int nRet
9ad40 3d 20 30 3b 0a 20 20 6e 52 65 74 20 2b 3d 20 73 = 0;. nRet += s
9ad50 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 qlite3PcacheRele
9ad60 61 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 aseMemory(n-nRet
9ad70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 );. return nRet
9ad80 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 ;.#else. UNUSED
9ad90 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 _PARAMETER(n);.
9ada0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
9adb0 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a K;.#endif.}../*.
9adc0 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 ** State informa
9add0 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 tion local to th
9ade0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
9adf0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a ion subsystem..*
9ae00 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f /.static SQLITE_
9ae10 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 WSD struct Mem0G
9ae20 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d lobal {. /* Num
9ae30 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 ber of free page
9ae40 73 20 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e s for scratch an
9ae50 64 20 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d d page-cache mem
9ae60 6f 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 ory */. u32 nSc
9ae70 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 ratchFree;. u32
9ae80 20 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 nPageFree;.. s
9ae90 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
9aea0 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 tex; /*
9aeb0 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 Mutex to seriali
9aec0 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 ze access */..
9aed0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 /*. ** The alar
9aee0 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 m callback and i
9aef0 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 ts arguments. T
9af00 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f he mem0.mutex lo
9af10 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 ck will. ** be
9af20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 held while the c
9af30 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 allback is runni
9af40 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 ng. Recursive c
9af50 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 alls into. ** t
9af60 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 he memory subsys
9af70 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c tem are allowed,
9af80 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c but no new call
9af90 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 backs will be.
9afa0 2a 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a ** issued.. */.
9afb0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
9afc0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a alarmThreshold;.
9afd0 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 void (*alarmCa
9afe0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 llback)(void*, s
9aff0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 qlite3_int64,int
9b000 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d );. void *alarm
9b010 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 Arg;.. /*. **
9b020 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 Pointers to the
9b030 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c end of sqlite3Gl
9b040 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
9b050 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 6c tch and. ** sql
9b060 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b070 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c 6f 63 .pPage to a bloc
9b080 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 k of memory that
9b090 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 68 records. ** wh
9b0a0 69 63 68 20 70 61 67 65 73 20 61 72 65 20 61 76 ich pages are av
9b0b0 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 ailable.. */.
9b0c0 75 33 32 20 2a 61 53 63 72 61 74 63 68 46 72 65 u32 *aScratchFre
9b0d0 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 65 46 e;. u32 *aPageF
9b0e0 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20 ree;.} mem0 = {
9b0f0 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
9b100 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 , 0, 0 };..#defi
9b110 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 ne mem0 GLOBAL(s
9b120 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c truct Mem0Global
9b130 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 , mem0)../*.** I
9b140 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
9b150 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9b160 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 subsystem..*/.SQ
9b170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
9b180 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e sqlite3MallocIn
9b190 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 it(void){. if(
9b1a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b1b0 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 fig.m.xMalloc==0
9b1c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d ){. sqlite3M
9b1d0 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a emSetDefault();.
9b1e0 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 }. memset(&me
9b1f0 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 m0, 0, sizeof(me
9b200 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 m0));. if( sqli
9b210 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b220 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 bCoreMutex ){.
9b230 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 mem0.mutex = s
9b240 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
9b250 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
9b260 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 ATIC_MEM);. }.
9b270 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
9b280 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9b290 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 h && sqlite3Glob
9b2a0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
9b2b0 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 26 ch>=100. &&
9b2c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9b2d0 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 nfig.nScratch>=0
9b2e0 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
9b2f0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
9b300 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
9b310 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 = ROUNDDOWN8(sq
9b320 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b330 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a g.szScratch-4);.
9b340 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 mem0.aScratc
9b350 68 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28 hFree = (u32*)&(
9b360 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c (char*)sqlite3Gl
9b370 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
9b380 74 63 68 29 0a 20 20 20 20 20 20 20 20 20 20 20 tch).
9b390 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 [sqlite3G
9b3a0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 lobalConfig.szSc
9b3b0 72 61 74 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f ratch*sqlite3Glo
9b3c0 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 balConfig.nScrat
9b3d0 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 ch];. for(i=0
9b3e0 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 ; i<sqlite3Globa
9b3f0 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 lConfig.nScratch
9b400 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 ; i++){ mem0.aSc
9b410 72 61 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 ratchFree[i] = i
9b420 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 ; }. mem0.nSc
9b430 72 61 74 63 68 46 72 65 65 20 3d 20 73 71 6c 69 ratchFree = sqli
9b440 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b450 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 nScratch;. }els
9b460 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c e{. sqlite3Gl
9b470 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
9b480 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c tch = 0;. sql
9b490 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b4a0 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30 3b 0a .szScratch = 0;.
9b4b0 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
9b4c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 3GlobalConfig.pP
9b4d0 61 67 65 20 26 26 20 73 71 6c 69 74 65 33 47 6c age && sqlite3Gl
9b4e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9b4f0 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 26 20 e>=512. &&
9b500 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b510 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a fig.nPage>=1 ){.
9b520 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 int i;. i
9b530 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 nt overhead;.
9b540 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 int sz = ROUNDD
9b550 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 OWN8(sqlite3Glob
9b560 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 alConfig.szPage)
9b570 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 ;. int n = sq
9b580 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b590 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 g.nPage;. ove
9b5a0 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 rhead = (4*n + s
9b5b0 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 z - 1)/sz;. s
9b5c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9b5d0 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 ig.nPage -= over
9b5e0 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 head;. mem0.a
9b5f0 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a PageFree = (u32*
9b600 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 )&((char*)sqlite
9b610 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 3GlobalConfig.pP
9b620 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 age).
9b630 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 [sqlite3G
9b640 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 lobalConfig.szPa
9b650 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ge*sqlite3Global
9b660 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 Config.nPage];.
9b670 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 for(i=0; i<sq
9b680 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b690 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d g.nPage; i++){ m
9b6a0 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d em0.aPageFree[i]
9b6b0 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 = i; }. mem0
9b6c0 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c .nPageFree = sql
9b6d0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b6e0 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b .nPage;. }else{
9b6f0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9b700 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d alConfig.pPage =
9b710 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 0;. sqlite3G
9b720 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 lobalConfig.szPa
9b730 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ge = 0;. }. re
9b740 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
9b750 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 alConfig.m.xInit
9b760 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
9b770 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 nfig.m.pAppData)
9b780 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 ;.}../*.** Deini
9b790 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f tialize the memo
9b7a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
9b7b0 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 bsystem..*/.SQLI
9b7c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9b7d0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 sqlite3MallocEnd
9b7e0 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 (void){. if( sq
9b7f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b800 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b g.m.xShutdown ){
9b810 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9b820 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 alConfig.m.xShut
9b830 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 down(sqlite3Glob
9b840 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 alConfig.m.pAppD
9b850 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 ata);. }. mems
9b860 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a et(&mem0, 0, siz
9b870 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f eof(mem0));.}../
9b880 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
9b890 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
9b8a0 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b currently check
9b8b0 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 ed out..*/.SQLIT
9b8c0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
9b8d0 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f t64 sqlite3_memo
9b8e0 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 ry_used(void){.
9b8f0 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 int n, mx;. sq
9b900 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b lite3_int64 res;
9b910 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 . sqlite3_statu
9b920 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f s(SQLITE_STATUS_
9b930 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c MEMORY_USED, &n,
9b940 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 &mx, 0);. res
9b950 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 = (sqlite3_int64
9b960 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f )n; /* Work aro
9b970 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 und bug in Borla
9b980 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 nd C. Ticket #32
9b990 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 16 */. return r
9b9a0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 es;.}../*.** Ret
9b9b0 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 urn the maximum
9b9c0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
9b9d0 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62 that has ever b
9b9e0 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f een.** checked o
9b9f0 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 ut since either
9ba00 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
9ba10 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a this process.**
9ba20 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f or since the mo
9ba30 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e st recent reset.
9ba40 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 .*/.SQLITE_API s
9ba50 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c qlite3_int64 sql
9ba60 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
9ba70 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 water(int resetF
9ba80 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d lag){. int n, m
9ba90 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 x;. sqlite3_int
9baa0 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 64 res;. sqlite
9bab0 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 3_status(SQLITE_
9bac0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9bad0 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 ED, &n, &mx, res
9bae0 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d etFlag);. res =
9baf0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
9bb00 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f mx; /* Work aro
9bb10 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 und bug in Borla
9bb20 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 nd C. Ticket #32
9bb30 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 16 */. return r
9bb40 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 es;.}../*.** Cha
9bb50 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 nge the alarm ca
9bb60 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 llback.*/.SQLITE
9bb70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
9bb80 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
9bb90 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 . void(*xCallba
9bba0 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 ck)(void *pArg,
9bbb0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 sqlite3_int64 us
9bbc0 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 ed,int N),. voi
9bbd0 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 d *pArg,. sqlit
9bbe0 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 e3_int64 iThresh
9bbf0 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 old.){. sqlite3
9bc00 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9bc10 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 0.mutex);. mem0
9bc20 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d .alarmCallback =
9bc30 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 xCallback;. me
9bc40 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 m0.alarmArg = pA
9bc50 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d rg;. mem0.alarm
9bc60 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 Threshold = iThr
9bc70 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 eshold;. sqlite
9bc80 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9bc90 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 m0.mutex);. ret
9bca0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
9bcb0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
9bcc0 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
9bcd0 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 ./*.** Deprecate
9bce0 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 d external inter
9bcf0 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f face. Internal/
9bd00 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 core SQLite code
9bd10 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 .** should call
9bd20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 sqlite3MemoryAla
9bd30 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 rm..*/.SQLITE_AP
9bd40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 I int sqlite3_me
9bd50 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f mory_alarm(. vo
9bd60 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 id(*xCallback)(v
9bd70 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 oid *pArg, sqlit
9bd80 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e e3_int64 used,in
9bd90 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 t N),. void *pA
9bda0 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e rg,. sqlite3_in
9bdb0 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 t64 iThreshold.)
9bdc0 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
9bdd0 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 e3MemoryAlarm(xC
9bde0 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 allback, pArg, i
9bdf0 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 Threshold);.}.#e
9be00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 ndif../*.** Trig
9be10 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a ger the alarm .*
9be20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
9be30 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d lite3MallocAlarm
9be40 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 (int nByte){. v
9be50 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 oid (*xCallback)
9be60 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 (void*,sqlite3_i
9be70 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c nt64,int);. sql
9be80 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 ite3_int64 nowUs
9be90 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 ed;. void *pArg
9bea0 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 ;. if( mem0.ala
9beb0 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 rmCallback==0 )
9bec0 72 65 74 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 return;. xCallb
9bed0 61 63 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d ack = mem0.alarm
9bee0 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 Callback;. nowU
9bef0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 sed = sqlite3Sta
9bf00 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f tusValue(SQLITE_
9bf10 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9bf20 45 44 29 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 ED);. pArg = me
9bf30 6d 30 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d m0.alarmArg;. m
9bf40 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9bf50 6b 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 k = 0;. sqlite3
9bf60 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
9bf70 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 0.mutex);. xCal
9bf80 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 lback(pArg, nowU
9bf90 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 sed, nByte);. s
9bfa0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
9bfb0 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
9bfc0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c mem0.alarmCall
9bfd0 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b back = xCallback
9bfe0 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 ;. mem0.alarmAr
9bff0 67 20 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a g = pArg;.}../*.
9c000 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 ** Do a memory a
9c010 6c 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 llocation with s
9c020 74 61 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c tatistics and al
9c030 61 72 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 arms. Assume th
9c040 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 e.** lock is alr
9c050 65 61 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 eady held..*/.st
9c060 61 74 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 atic int mallocW
9c070 69 74 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 ithAlarm(int n,
9c080 76 6f 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e void **pp){. in
9c090 74 20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 t nFull;. void
9c0a0 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 *p;. assert( sq
9c0b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
9c0c0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a (mem0.mutex) );.
9c0d0 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 nFull = sqlite
9c0e0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9c0f0 78 52 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 xRoundup(n);. s
9c100 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
9c110 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 SQLITE_STATUS_MA
9c120 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 LLOC_SIZE, n);.
9c130 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 if( mem0.alarmC
9c140 61 6c 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 allback!=0 ){.
9c150 20 20 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 int nUsed = sq
9c160 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 lite3StatusValue
9c170 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9c180 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 EMORY_USED);.
9c190 20 69 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c if( nUsed+nFull
9c1a0 20 3e 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 >= mem0.alarmTh
9c1b0 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 reshold ){.
9c1c0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c sqlite3MallocAl
9c1d0 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 arm(nFull);.
9c1e0 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 }. }. p = sqli
9c1f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9c200 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 m.xMalloc(nFull)
9c210 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 ;. if( p==0 &&
9c220 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9c230 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ck ){. sqlite
9c240 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 3MallocAlarm(nFu
9c250 6c 6c 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c ll);. p = sql
9c260 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9c270 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c .m.xMalloc(nFull
9c280 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 );. }. if( p )
9c290 7b 0a 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 {. nFull = sq
9c2a0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
9c2b0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 p);. sqlite3S
9c2c0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9c2d0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9c2e0 45 44 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a ED, nFull);. }.
9c2f0 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 *pp = p;. ret
9c300 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a urn nFull;.}../*
9c310 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d .** Allocate mem
9c320 6f 72 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ory. This routi
9c330 6e 65 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 ne is like sqlit
9c340 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 e3_malloc() exce
9c350 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 pt that it.** as
9c360 73 75 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 sumes the memory
9c370 20 73 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 subsystem has a
9c380 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 lready been init
9c390 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ialized..*/.SQLI
9c3a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9c3b0 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 *sqlite3Malloc(i
9c3c0 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 nt n){. void *p
9c3d0 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 ;. if( n<=0 ||
9c3e0 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 20 29 7b n>=0x7fffff00 ){
9c3f0 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 . /* A memory
9c400 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 allocation of a
9c410 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
9c420 20 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 which is near t
9c430 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a he maximum. *
9c440 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 * signed integer
9c450 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75 value might cau
9c460 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 se an integer ov
9c470 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 erflow inside of
9c480 20 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c the. ** xMal
9c490 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65 loc(). Hence we
9c4a0 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d limit the maxim
9c4b0 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66 um size to 0x7ff
9c4c0 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20 fff00, giving.
9c4d0 20 20 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f ** 255 bytes o
9c4e0 66 20 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c f overhead. SQL
9c4f0 69 74 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 ite itself will
9c500 6e 65 76 65 72 20 75 73 65 20 61 6e 79 74 68 69 never use anythi
9c510 6e 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74 ng near. ** t
9c520 68 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65 his amount. The
9c530 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61 only way to rea
9c540 63 68 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 ch the limit is
9c550 77 69 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c with sqlite3_mal
9c560 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d loc() */. p =
9c570 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
9c580 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9c590 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
9c5a0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9c5b0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
9c5c0 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 ex);. mallocW
9c5d0 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b ithAlarm(n, &p);
9c5e0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
9c5f0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9c600 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 tex);. }else{.
9c610 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c p = sqlite3Gl
9c620 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 obalConfig.m.xMa
9c630 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 lloc(n);. }. r
9c640 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
9c650 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
9c660 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
9c670 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 ocation is for u
9c680 73 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 se by the applic
9c690 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 ation..** First
9c6a0 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 make sure the me
9c6b0 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 mory subsystem i
9c6c0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 s initialized, t
9c6d0 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c hen do the.** al
9c6e0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c location..*/.SQL
9c6f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
9c700 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 lite3_malloc(int
9c710 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c n){.#ifndef SQL
9c720 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
9c730 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f T. if( sqlite3_
9c740 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 initialize() ) r
9c750 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a eturn 0;.#endif.
9c760 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
9c770 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a Malloc(n);.}../*
9c780 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 .** Each thread
9c790 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 may only have a
9c7a0 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 single outstandi
9c7b0 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 ng allocation fr
9c7c0 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 om.** xScratchMa
9c7d0 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 lloc(). We veri
9c7e0 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 fy this constrai
9c7f0 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 nt in the single
9c800 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 -threaded.** cas
9c810 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 e by setting scr
9c820 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 atchAllocOut to
9c830 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 1 when an alloca
9c840 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 tion.** is outst
9c850 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 anding clearing
9c860 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f it when the allo
9c870 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e cation is freed.
9c880 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
9c890 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 HREADSAFE==0 &&
9c8a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
9c8b0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 .static int scra
9c8c0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b tchAllocOut = 0;
9c8d0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
9c8e0 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 Allocate memory
9c8f0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 that is to be us
9c900 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 ed and released
9c910 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 right away..** T
9c920 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 his routine is s
9c930 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 imilar to alloca
9c940 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 () in that it is
9c950 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a not intended.**
9c960 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 for situations
9c970 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 where the memory
9c980 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c might be held l
9c990 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a ong-term. This.
9c9a0 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e ** routine is in
9c9b0 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 tended to get me
9c9c0 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 mory to old larg
9c9d0 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 e transient data
9c9e0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 .** structures t
9c9f0 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f hat would not no
9ca00 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 rmally fit on th
9ca10 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a e stack of an.**
9ca20 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 embedded proces
9ca30 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 sor..*/.SQLITE_P
9ca40 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
9ca50 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f ite3ScratchMallo
9ca60 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 c(int n){. void
9ca70 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e *p;. assert( n
9ca80 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 >0 );..#if SQLIT
9ca90 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 E_THREADSAFE==0
9caa0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 && !defined(NDEB
9cab0 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 UG). /* Verify
9cac0 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 that no more tha
9cad0 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c n one scratch al
9cae0 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 location per thr
9caf0 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73 ead. ** is outs
9cb00 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 tanding at one t
9cb10 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f ime. (This is o
9cb20 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 nly checked in t
9cb30 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 he. ** single-t
9cb40 68 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e hreaded case sin
9cb50 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 ce checking in t
9cb60 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 he multi-threade
9cb70 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c d case. ** woul
9cb80 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 d be much more c
9cb90 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a omplicated.) */.
9cba0 20 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 assert( scratc
9cbb0 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a hAllocOut==0 );.
9cbc0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 #endif.. if( sq
9cbd0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9cbe0 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b g.szScratch<n ){
9cbf0 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 . goto scratc
9cc00 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 h_overflow;. }e
9cc10 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74 lse{ . sqlit
9cc20 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9cc30 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9cc40 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 if( mem0.nScratc
9cc50 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 hFree==0 ){.
9cc60 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9cc70 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
9cc80 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63 );. goto sc
9cc90 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a ratch_overflow;.
9cca0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9ccb0 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 int i;. i
9ccc0 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 = mem0.aScratchF
9ccd0 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 ree[--mem0.nScra
9cce0 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 tchFree];.
9ccf0 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 i *= sqlite3Glob
9cd00 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
9cd10 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ch;. sqlite
9cd20 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9cd30 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 E_STATUS_SCRATCH
9cd40 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 _USED, 1);.
9cd50 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 sqlite3StatusSe
9cd60 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t(SQLITE_STATUS_
9cd70 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 SCRATCH_SIZE, n)
9cd80 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
9cd90 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9cda0 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 .mutex);. p
9cdb0 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 = (void*)&((cha
9cdc0 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c r*)sqlite3Global
9cdd0 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 Config.pScratch)
9cde0 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 [i];. asser
9cdf0 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28 t( (((u8*)p - (
9ce00 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 u8*)0) & 7)==0 )
9ce10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20 ;. }. }.#if
9ce20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
9ce30 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 E==0 && !defined
9ce40 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 (NDEBUG). scrat
9ce50 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d chAllocOut = p!=
9ce60 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 0;.#endif.. ret
9ce70 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f urn p;..scratch_
9ce80 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 overflow:. if(
9ce90 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9cea0 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a fig.bMemstat ){.
9ceb0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9cec0 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 x_enter(mem0.mut
9ced0 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ex);. sqlite3
9cee0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
9cef0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
9cf00 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 SIZE, n);. n
9cf10 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 = mallocWithAlar
9cf20 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 m(n, &p);. if
9cf30 28 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 ( p ) sqlite3Sta
9cf40 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
9cf50 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 ATUS_SCRATCH_OVE
9cf60 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 RFLOW, n);. s
9cf70 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
9cf80 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
9cf90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d }else{. p =
9cfa0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9cfb0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e nfig.m.xMalloc(n
9cfc0 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 );. }.#if SQLIT
9cfd0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 E_THREADSAFE==0
9cfe0 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 && !defined(NDEB
9cff0 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c UG). scratchAll
9d000 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 ocOut = p!=0;.#e
9d010 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b ndif. return p;
9d020 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 .}.SQLITE_PR
9d030 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
9d040 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f e3ScratchFree(vo
9d050 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 id *p){. if( p
9d060 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 ){..#if SQLITE_T
9d070 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 HREADSAFE==0 &&
9d080 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
9d090 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 . /* Verify t
9d0a0 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e hat no more than
9d0b0 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c one scratch all
9d0c0 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 ocation per thre
9d0d0 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74 ad. ** is out
9d0e0 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 standing at one
9d0f0 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 time. (This is
9d100 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 only checked in
9d110 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c the. ** singl
9d120 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 e-threaded case
9d130 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 since checking i
9d140 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 n the multi-thre
9d150 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a aded case. **
9d160 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d would be much m
9d170 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e ore complicated.
9d180 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 ) */. assert(
9d190 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 scratchAllocOut
9d1a0 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74 ==1 );. scrat
9d1b0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a chAllocOut = 0;.
9d1c0 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
9d1d0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d1e0 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a fig.pScratch==0.
9d1f0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c || p<
9d200 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d210 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20 fig.pScratch.
9d220 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 || p>=(v
9d230 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74 oid*)mem0.aScrat
9d240 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 chFree ){.
9d250 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9d260 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
9d270 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
9d280 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d iSize = sqlite3M
9d290 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 allocSize(p);.
9d2a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
9d2b0 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
9d2c0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 utex);. s
9d2d0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
9d2e0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
9d2f0 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 RATCH_OVERFLOW,
9d300 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 -iSize);.
9d310 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9d320 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9d330 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 MEMORY_USED, -iS
9d340 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ize);. sq
9d350 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d360 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 g.m.xFree(p);.
9d370 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
9d380 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
9d390 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c utex);. }el
9d3a0 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
9d3b0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9d3c0 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 m.xFree(p);.
9d3d0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
9d3e0 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
9d3f0 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a i = (int)((u8*
9d400 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65 )p - (u8*)sqlite
9d410 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 3GlobalConfig.pS
9d420 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69 cratch);. i
9d430 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 /= sqlite3Globa
9d440 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9d450 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 h;. assert(
9d460 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 i>=0 && i<sqlit
9d470 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9d480 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 Scratch );.
9d490 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9d4a0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9d4b0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
9d4c0 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
9d4d0 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c e<(u32)sqlite3Gl
9d4e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 obalConfig.nScra
9d4f0 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d tch );. mem
9d500 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 0.aScratchFree[m
9d510 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 em0.nScratchFree
9d520 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 ++] = i;. s
9d530 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 qlite3StatusAdd(
9d540 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
9d550 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b RATCH_USED, -1);
9d560 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
9d570 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9d580 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 mutex);. }.
9d590 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 }.}../*.** TRUE
9d5a0 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 if p is a lookas
9d5b0 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ide memory alloc
9d5c0 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f ation from db.*/
9d5d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9d5e0 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 OMIT_LOOKASIDE.s
9d5f0 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b tatic int isLook
9d600 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 aside(sqlite3 *d
9d610 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 b, void *p){. r
9d620 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 26 eturn db && p &&
9d630 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 p>=db->lookasid
9d640 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62 e.pStart && p<db
9d650 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 ->lookaside.pEnd
9d660 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ;.}.#else.#defin
9d670 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c e isLookaside(A,
9d680 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a B) 0.#endif../*.
9d690 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 ** Return the si
9d6a0 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 ze of a memory a
9d6b0 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f llocation previo
9d6c0 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 usly obtained fr
9d6d0 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c om.** sqlite3Mal
9d6e0 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 loc() or sqlite3
9d6f0 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 _malloc()..*/.SQ
9d700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
9d710 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 sqlite3MallocSi
9d720 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 ze(void *p){. r
9d730 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
9d740 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a balConfig.m.xSiz
9d750 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 e(p);.}.SQLITE_P
9d760 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
9d770 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 e3DbMallocSize(s
9d780 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 qlite3 *db, void
9d790 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
9d7a0 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
9d7b0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
9d7c0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
9d7d0 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 isLookaside(db,
9d7e0 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e p) ){. return
9d7f0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 db->lookaside.s
9d800 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 z;. }else{.
9d810 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
9d820 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 obalConfig.m.xSi
9d830 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ze(p);. }.}../*
9d840 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 .** Free memory
9d850 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 previously obtai
9d860 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
9d870 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c Malloc()..*/.SQL
9d880 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
9d890 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a ite3_free(void *
9d8a0 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 p){. if( p==0 )
9d8b0 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 return;. if( s
9d8c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d8d0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 ig.bMemstat ){.
9d8e0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9d8f0 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9d900 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 x);. sqlite3S
9d910 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9d920 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9d930 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c ED, -sqlite3Mall
9d940 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 ocSize(p));.
9d950 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d960 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
9d970 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9d980 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9d990 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ex);. }else{.
9d9a0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9d9b0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
9d9c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 ;. }.}../*.** F
9d9d0 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 ree memory that
9d9e0 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 might be associa
9d9f0 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 ted with a parti
9da00 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a cular database.*
9da10 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f * connection..*/
9da20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9da30 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 void sqlite3DbFr
9da40 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ee(sqlite3 *db,
9da50 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
9da60 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c rt( db==0 || sql
9da70 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
9da80 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
9da90 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 if( isLookaside(
9daa0 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f db, p) ){. Lo
9dab0 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 okasideSlot *pBu
9dac0 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c f = (LookasideSl
9dad0 6f 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d ot*)p;. pBuf-
9dae0 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f >pNext = db->loo
9daf0 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 kaside.pFree;.
9db00 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
9db10 70 46 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 pFree = pBuf;.
9db20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
9db30 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b nOut--;. }else{
9db40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
9db50 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a e(p);. }.}../*.
9db60 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 ** Change the si
9db70 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e ze of an existin
9db80 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 g memory allocat
9db90 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ion.*/.SQLITE_PR
9dba0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
9dbb0 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 te3Realloc(void
9dbc0 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 *pOld, int nByte
9dbd0 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 s){. int nOld,
9dbe0 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e nNew;. void *pN
9dbf0 65 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d ew;. if( pOld==
9dc00 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
9dc10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 sqlite3Malloc(nB
9dc20 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ytes);. }. if(
9dc30 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 nBytes<=0 ){.
9dc40 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
9dc50 4f 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e Old);. return
9dc60 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 0;. }. if( nB
9dc70 79 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 ytes>=0x7fffff00
9dc80 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 ){. /* The 0
9dc90 78 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 x7ffff00 limit t
9dca0 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 erm is explained
9dcb0 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 in comments on
9dcc0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 sqlite3Malloc()
9dcd0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b */. return 0;
9dce0 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 . }. nOld = sq
9dcf0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
9dd00 70 4f 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 3d 20 pOld);. nNew =
9dd10 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9dd20 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e fig.m.xRoundup(n
9dd30 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 6e 4f Bytes);. if( nO
9dd40 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 ld==nNew ){.
9dd50 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 7d pNew = pOld;. }
9dd60 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 else if( sqlite3
9dd70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
9dd80 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c mstat ){. sql
9dd90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9dda0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9ddb0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 sqlite3StatusS
9ddc0 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 et(SQLITE_STATUS
9ddd0 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 _MALLOC_SIZE, nB
9dde0 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 73 ytes);. if( s
9ddf0 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 qlite3StatusValu
9de00 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e(SQLITE_STATUS_
9de10 4d 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 MEMORY_USED)+nNe
9de20 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 w-nOld >= .
9de30 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 mem0.alarmT
9de40 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 hreshold ){.
9de50 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 sqlite3MallocA
9de60 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b larm(nNew-nOld);
9de70 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 . }. pNew
9de80 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9de90 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 onfig.m.xRealloc
9dea0 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 (pOld, nNew);.
9deb0 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 if( pNew==0 &&
9dec0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 mem0.alarmCallb
9ded0 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ack ){. sql
9dee0 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 ite3MallocAlarm(
9def0 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 70 nBytes);. p
9df00 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f New = sqlite3Glo
9df10 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 balConfig.m.xRea
9df20 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 lloc(pOld, nNew)
9df30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
9df40 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 6e 4e pNew ){. nN
9df50 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ew = sqlite3Mall
9df60 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 ocSize(pNew);.
9df70 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
9df80 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
9df90 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 US_MEMORY_USED,
9dfa0 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 nNew-nOld);.
9dfb0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 }. sqlite3_mu
9dfc0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
9dfd0 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a utex);. }else{.
9dfe0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
9dff0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9e000 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 .xRealloc(pOld,
9e010 6e 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 72 65 74 nNew);. }. ret
9e020 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
9e030 2a 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e ** The public in
9e040 74 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 terface to sqlit
9e050 65 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 e3Realloc. Make
9e060 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d sure that the m
9e070 65 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 emory.** subsyst
9e080 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 em is initialize
9e090 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b d prior to invok
9e0a0 69 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f ing sqliteReallo
9e0b0 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 c..*/.SQLITE_API
9e0c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 void *sqlite3_r
9e0d0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c ealloc(void *pOl
9e0e0 64 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 d, int n){.#ifnd
9e0f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
9e100 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 UTOINIT. if( sq
9e110 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
9e120 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 () ) return 0;.#
9e130 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 endif. return s
9e140 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f qlite3Realloc(pO
9e150 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ld, n);.}.../*.*
9e160 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a * Allocate and z
9e170 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a ero memory..*/ .
9e180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9e190 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c oid *sqlite3Mall
9e1a0 6f 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 ocZero(int n){.
9e1b0 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 void *p = sqlit
9e1c0 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 e3Malloc(n);. i
9e1d0 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 f( p ){. mems
9e1e0 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d et(p, 0, n);. }
9e1f0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
9e200 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
9e210 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 nd zero memory.
9e220 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 If the allocati
9e230 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a on fails, make.*
9e240 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c * the mallocFail
9e250 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 ed flag in the c
9e260 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 onnection pointe
9e270 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
9e280 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
9e290 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 e3DbMallocZero(s
9e2a0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
9e2b0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 n){. void *p =
9e2c0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
9e2d0 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 aw(db, n);. if(
9e2e0 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 p ){. memset
9e2f0 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 (p, 0, n);. }.
9e300 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
9e310 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 .** Allocate and
9e320 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 zero memory. I
9e330 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
9e340 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 fails, make.**
9e350 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 the mallocFailed
9e360 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e flag in the con
9e370 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e nection pointer.
9e380 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 .**.** If db!=0
9e390 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 and db->mallocFa
9e3a0 69 6c 65 64 20 69 73 20 74 72 75 65 20 28 69 6e iled is true (in
9e3b0 64 69 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 dicating a prior
9e3c0 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 malloc.** failu
9e3d0 72 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 re on the same d
9e3e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
9e3f0 6f 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 on) then always
9e400 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e return 0..** Hen
9e410 63 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 ce for a particu
9e420 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
9e430 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 nection, once ma
9e440 6c 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 lloc starts.** f
9e450 61 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 ailing, it fails
9e460 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e consistently un
9e470 74 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 til mallocFailed
9e480 20 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 is reset..** Th
9e490 69 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 is is an importa
9e4a0 6e 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 nt assumption.
9e4b0 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 There are many p
9e4c0 6c 61 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 laces in the.**
9e4d0 63 6f 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 code that do thi
9e4e0 6e 67 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a ngs like this:.*
9e4f0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 *.** int
9e500 20 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 *a = (int*)sqli
9e510 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
9e520 62 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 b, 100);.**
9e530 20 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e int *b = (in
9e540 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c t*)sqlite3DbMall
9e550 6f 63 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a ocRaw(db, 200);.
9e560 2a 2a 20 20 20 20 20 20 20 20 20 69 66 28 20 62 ** if( b
9e570 20 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a ) a[10] = 9;.**
9e580 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 .** In other wor
9e590 64 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 ds, if a subsequ
9e5a0 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 ent malloc (ex:
9e5b0 22 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 "b") worked, it
9e5c0 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 is assumed.** th
9e5d0 61 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c at all prior mal
9e5e0 6c 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 locs (ex: "a") w
9e5f0 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 orked too..*/.SQ
9e600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9e610 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c d *sqlite3DbMall
9e620 6f 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 ocRaw(sqlite3 *d
9e630 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 b, int n){. voi
9e640 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 d *p;. assert(
9e650 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
9e660 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
9e670 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 6e 64 65 mutex) );.#ifnde
9e680 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
9e690 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 62 OKASIDE. if( db
9e6a0 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 ){. Lookasid
9e6b0 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20 eSlot *pBuf;.
9e6c0 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
9e6d0 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 ailed ){. r
9e6e0 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
9e6f0 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 if( db->looka
9e700 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26 side.bEnabled &&
9e710 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 n<=db->lookasid
9e720 65 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26 e.sz. &&
9e730 20 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f (pBuf = db->loo
9e740 6b 61 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30 kaside.pFree)!=0
9e750 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f ){. db->lo
9e760 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 okaside.pFree =
9e770 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pBuf->pNext;.
9e780 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 db->lookaside
9e790 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69 .nOut++;. i
9e7a0 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 f( db->lookaside
9e7b0 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 .nOut>db->lookas
9e7c0 69 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 ide.mxOut ){.
9e7d0 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
9e7e0 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c de.mxOut = db->l
9e7f0 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 ookaside.nOut;.
9e800 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
9e810 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b urn (void*)pBuf;
9e820 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 . }. }.#else
9e830 0a 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d . if( db && db-
9e840 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
9e850 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
9e860 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20 }.#endif. p =
9e870 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 sqlite3Malloc(n)
9e880 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20 64 62 ;. if( !p && db
9e890 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c ){. db->mall
9e8a0 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
9e8b0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
9e8c0 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 ./*.** Resize th
9e8d0 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 e block of memor
9e8e0 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 y pointed to by
9e8f0 70 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66 p to n bytes. If
9e900 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66 the.** resize f
9e910 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61 ails, set the ma
9e920 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
9e930 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f in the connectio
9e940 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c n object..*/.SQL
9e950 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9e960 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c *sqlite3DbReall
9e970 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 oc(sqlite3 *db,
9e980 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b void *p, int n){
9e990 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20 . void *pNew =
9e9a0 30 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 0;. assert( db!
9e9b0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
9e9c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
9e9d0 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
9e9e0 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
9e9f0 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 cFailed==0 ){.
9ea00 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 if( p==0 ){.
9ea10 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
9ea20 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
9ea30 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 , n);. }.
9ea40 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 if( isLookaside(
9ea50 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 20 db, p) ){.
9ea60 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 if( n<=db->looka
9ea70 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 side.sz ){.
9ea80 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 return p;.
9ea90 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 }. pNew
9eaa0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
9eab0 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 cRaw(db, n);.
9eac0 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 if( pNew ){.
9ead0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e memcpy(pN
9eae0 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 ew, p, db->looka
9eaf0 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 side.sz);.
9eb00 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
9eb10 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a db, p);. }.
9eb20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
9eb30 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f pNew = sqlite3_
9eb40 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 realloc(p, n);.
9eb50 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 if( !pNew )
9eb60 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 {. db->ma
9eb70 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
9eb80 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
9eb90 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
9eba0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 .}../*.** Attemp
9ebb0 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 t to reallocate
9ebc0 70 2e 20 20 49 66 20 74 68 65 20 72 65 61 6c 6c p. If the reall
9ebd0 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 ocation fails, t
9ebe0 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e hen free p.** an
9ebf0 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 d set the malloc
9ec00 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 Failed flag in t
9ec10 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
9ec20 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ection..*/.SQLIT
9ec30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
9ec40 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
9ec50 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a OrFree(sqlite3 *
9ec60 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 db, void *p, int
9ec70 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 n){. void *pNe
9ec80 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 w;. pNew = sqli
9ec90 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c te3DbRealloc(db,
9eca0 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 p, n);. if( !p
9ecb0 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 New ){. sqlit
9ecc0 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b e3DbFree(db, p);
9ecd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e . }. return pN
9ece0 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b ew;.}../*.** Mak
9ecf0 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20 73 74 e a copy of a st
9ed00 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f ring in memory o
9ed10 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
9ed20 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 iteMalloc(). The
9ed30 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 se .** functions
9ed40 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c call sqlite3Mal
9ed50 6c 6f 63 52 61 77 28 29 20 64 69 72 65 63 74 6c locRaw() directl
9ed60 79 20 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c y instead of sql
9ed70 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 iteMalloc(). Thi
9ed80 73 0a 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20 s.** is because
9ed90 77 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 when memory debu
9eda0 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 gging is turned
9edb0 6f 6e 2c 20 74 68 65 73 65 20 74 77 6f 20 66 75 on, these two fu
9edc0 6e 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 nctions are .**
9edd0 63 61 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f called via macro
9ede0 73 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 s that record th
9edf0 65 20 63 75 72 72 65 6e 74 20 66 69 6c 65 20 61 e current file a
9ee00 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 nd line number i
9ee10 6e 20 74 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 n the.** ThreadD
9ee20 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ata structure..*
9ee30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9ee40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 char *sqlite3Db
9ee50 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a StrDup(sqlite3 *
9ee60 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a db, const char *
9ee70 7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 z){. char *zNew
9ee80 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 ;. size_t n;.
9ee90 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 if( z==0 ){.
9eea0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
9eeb0 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
9eec0 6e 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20 61 73 n30(z) + 1;. as
9eed0 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 sert( (n&0x7ffff
9eee0 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 fff)==n );. zNe
9eef0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
9ef00 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29 locRaw(db, (int)
9ef10 6e 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 n);. if( zNew )
9ef20 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 {. memcpy(zNe
9ef30 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 w, z, n);. }.
9ef40 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 return zNew;.}.S
9ef50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
9ef60 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 ar *sqlite3DbStr
9ef70 4e 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 NDup(sqlite3 *db
9ef80 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
9ef90 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 int n){. char
9efa0 2a 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d *zNew;. if( z==
9efb0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
9efc0 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 0;. }. assert(
9efd0 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d (n&0x7fffffff)=
9efe0 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 =n );. zNew = s
9eff0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
9f000 77 28 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 w(db, n+1);. if
9f010 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 ( zNew ){. me
9f020 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 mcpy(zNew, z, n)
9f030 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 ;. zNew[n] =
9f040 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
9f050 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 zNew;.}../*.** C
9f060 72 65 61 74 65 20 61 20 73 74 72 69 6e 67 20 66 reate a string f
9f070 72 6f 6d 20 74 68 65 20 7a 46 72 6f 6d 61 74 20 rom the zFromat
9f080 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 argument and the
9f090 20 76 61 5f 6c 69 73 74 20 74 68 61 74 20 66 6f va_list that fo
9f0a0 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 llows..** Store
9f0b0 74 68 65 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 the string in me
9f0c0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
9f0d0 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
9f0e0 29 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a ) and make *pz.*
9f0f0 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 * point to that
9f100 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 string..*/.SQLIT
9f110 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
9f120 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
9f130 63 68 61 72 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 char **pz, sqlit
9f140 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
9f150 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e ar *zFormat, ...
9f160 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
9f170 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 . char *z;.. v
9f180 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
9f190 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 mat);. z = sqli
9f1a0 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 te3VMPrintf(db,
9f1b0 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
9f1c0 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 va_end(ap);. sq
9f1d0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
9f1e0 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b *pz);. *pz = z;
9f1f0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .}.../*.** This
9f200 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 function must be
9f210 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65 called before e
9f220 78 69 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66 xiting any API f
9f230 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a unction (i.e. .*
9f240 2a 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 * returning cont
9f250 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29 rol to the user)
9f260 20 74 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64 that has called
9f270 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 sqlite3_malloc
9f280 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 or.** sqlite3_re
9f290 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 alloc..**.** The
9f2a0 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 returned value
9f2b0 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f is normally a co
9f2c0 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 py of the second
9f2d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
9f2e0 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48 s.** function. H
9f2f0 6f 77 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c owever, if a mal
9f300 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 68 61 loc() failure ha
9f310 73 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 s occurred since
9f320 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a the previous.**
9f330 20 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49 invocation SQLI
9f340 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 TE_NOMEM is retu
9f350 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a rned instead. .*
9f360 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 *.** If the firs
9f370 74 20 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 t argument, db,
9f380 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 is not NULL and
9f390 61 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 a malloc() error
9f3a0 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 0a 2a has occurred,.*
9f3b0 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 * then the conne
9f3c0 63 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65 ction error-code
9f3d0 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 (the value retu
9f3e0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
9f3f0 65 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73 errcode()).** is
9f400 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e set to SQLITE_N
9f410 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f OMEM..*/.SQLITE_
9f420 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
9f430 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 te3ApiExit(sqlit
9f440 65 33 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b e3* db, int rc){
9f450 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20 . /* If the db
9f460 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55 handle is not NU
9f470 4c 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 LL, then we must
9f480 20 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63 hold the connec
9f490 74 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a tion handle. **
9f4a0 20 6d 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68 mutex here. Oth
9f4b0 65 72 77 69 73 65 20 74 68 65 20 72 65 61 64 20 erwise the read
9f4c0 28 61 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72 (and possible wr
9f4d0 69 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c ite) of db->mall
9f4e0 6f 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69 ocFailed . ** i
9f4f0 73 20 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20 s unsafe, as is
9f500 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
9f510 74 65 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f te3Error().. */
9f520 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 20 7c . assert( !db |
9f530 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
9f540 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
9f550 29 3b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 );. if( db && (
9f560 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9f570 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 || rc==SQLITE_I
9f580 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b 0a 20 OERR_NOMEM) ){.
9f590 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
9f5a0 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d db, SQLITE_NOMEM
9f5b0 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61 , 0);. db->ma
9f5c0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
9f5d0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
9f5e0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 NOMEM;. }. ret
9f5f0 75 72 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64 urn rc & (db ? d
9f600 62 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66 b->errMask : 0xf
9f610 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a f);.}../********
9f620 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 ****** End of ma
9f630 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a lloc.c *********
9f640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f660 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
9f670 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
9f680 65 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a e printf.c *****
9f690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f6b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 *****/./*.** The
9f6c0 20 22 70 72 69 6e 74 66 22 20 63 6f 64 65 20 74 "printf" code t
9f6d0 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61 74 65 hat follows date
9f6e0 73 20 66 72 6f 6d 20 74 68 65 20 31 39 38 30 27 s from the 1980'
9f6f0 73 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a 2a 20 s. It is in.**
9f700 74 68 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 the public domai
9f710 6e 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c n. The original
9f720 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 69 6e comments are in
9f730 63 6c 75 64 65 64 20 68 65 72 65 20 66 6f 72 0a cluded here for.
9f740 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e ** completeness.
9f750 20 20 54 68 65 79 20 61 72 65 20 76 65 72 79 20 They are very
9f760 6f 75 74 2d 6f 66 2d 64 61 74 65 20 62 75 74 20 out-of-date but
9f770 6d 69 67 68 74 20 62 65 20 75 73 65 66 75 6c 20 might be useful
9f780 61 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f 72 69 as.** an histori
9f790 63 61 6c 20 72 65 66 65 72 65 6e 63 65 2e 20 20 cal reference.
9f7a0 4d 6f 73 74 20 6f 66 20 74 68 65 20 22 65 6e 68 Most of the "enh
9f7b0 61 6e 63 65 6d 65 6e 74 73 22 20 68 61 76 65 20 ancements" have
9f7c0 62 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a 20 6f been backed.** o
9f7d0 75 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 ut so that the f
9f7e0 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 unctionality is
9f7f0 6e 6f 77 20 74 68 65 20 73 61 6d 65 20 61 73 20 now the same as
9f800 73 74 61 6e 64 61 72 64 20 70 72 69 6e 74 66 28 standard printf(
9f810 29 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a )..**.**********
9f820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f830 2a 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 2a 2a 2a 2a 2a ****************
9f860 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
9f870 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 wing modules is
9f880 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c an enhanced repl
9f890 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 acement for the
9f8a0 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 "printf" subrout
9f8b0 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e ines.** found in
9f8c0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 the standard C
9f8d0 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f library. The fo
9f8e0 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d llowing enhancem
9f8f0 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 ents are.** supp
9f900 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 orted:.**.**
9f910 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 + Additional
9f920 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 functions. The
9f930 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 standard set of
9f940 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f "printf" functio
9f950 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e ns.** in
9f960 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 cludes printf, f
9f970 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c printf, sprintf,
9f980 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e vprintf, vfprin
9f990 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 tf, and.**
9f9a0 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 vsprintf. Th
9f9b0 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 is module adds t
9f9c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
9f9d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 .** *
9f9e0 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 snprintf -- Wor
9f9f0 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c ks like sprintf,
9fa00 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 but has an extr
9fa10 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 a argument.**
9fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fa30 20 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 which is
9fa40 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
9fa50 62 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 buffer written t
9fa60 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 o..**.**
9fa70 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d * mprintf --
9fa80 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 Similar to spr
9fa90 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 intf. Writes ou
9faa0 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a tput to memory.*
9fab0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
9fac0 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 obtai
9fad0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e ned from malloc.
9fae0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9faf0 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 * xprintf --
9fb00 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e Calls a function
9fb10 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f to dispose of o
9fb20 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 utput..**.**
9fb30 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 * nprint
9fb40 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c f -- No output,
9fb50 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 but returns the
9fb60 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
9fb70 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 cters.**
9fb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9fb90 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 that would hav
9fba0 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 e been output by
9fbb0 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 printf..**.**
9fbc0 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d * A v-
9fbd0 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 version (ex: vs
9fbe0 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 nprintf) of ever
9fbf0 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c y function is al
9fc00 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 so.**
9fc10 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a supplied..**.
9fc20 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 ** + A few
9fc30 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 extensions to t
9fc40 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f he formatting no
9fc50 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f tation are suppo
9fc60 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 rted:.**.**
9fc70 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 * The "="
9fc80 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 flag (similar t
9fc90 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 o "-") causes th
9fca0 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a e output to be.*
9fcb0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 * b
9fcc0 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 e centered in th
9fcd0 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 e appropriately
9fce0 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a sized field..**.
9fcf0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
9fd00 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 The %b field out
9fd10 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 puts an integer
9fd20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 in binary notati
9fd30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 on..**.**
9fd40 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 * The %c fi
9fd50 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 eld now accepts
9fd60 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 a precision. Th
9fd70 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 e character outp
9fd80 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ut.**
9fd90 20 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 is repeated b
9fda0 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
9fdb0 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 times the precis
9fdc0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a ion specifies..*
9fdd0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9fde0 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 The %' field w
9fdf0 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 orks like %c, bu
9fe00 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 t takes as its c
9fe10 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 haracter the.**
9fe20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 nex
9fe30 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 t character of t
9fe40 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 he format string
9fe50 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 , instead of the
9fe60 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 next.**
9fe70 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 argument.
9fe80 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 For example, p
9fe90 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 rintf("%.78'-")
9fea0 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 prints 78 minus
9feb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9fec0 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 signs, the same
9fed0 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 as printf("%.7
9fee0 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 8c",'-')..**.**
9fef0 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d + When com
9ff00 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 piled using GCC
9ff10 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 on a SPARC, this
9ff20 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e version of prin
9ff30 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 tf is.**
9ff40 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 faster than the
9ff50 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 library printf
9ff60 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a for SUN OS 4.1..
9ff70 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c **.** + Al
9ff80 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 l functions are
9ff90 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e fully reentrant.
9ffa0 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f .**.*/../*.** Co
9ffb0 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 nversion types f
9ffc0 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 all into various
9ffd0 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 categories as d
9ffe0 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a efined by the.**
9fff0 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 following enume
a0000 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 ration..*/.#defi
a0010 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20 ne etRADIX
a0020 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 1 /* Integer ty
a0030 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f pes. %d, %x, %o
a0040 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a , and so forth *
a0050 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 /.#define etFLOA
a0060 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f T 2 /* Flo
a0070 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 ating point. %f
a0080 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 */.#define etEX
a0090 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 P 3 /* E
a00a0 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 xponentional not
a00b0 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 ation. %e and %E
a00c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 */.#define etGE
a00d0 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 NERIC 4 /* F
a00e0 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e loating or expon
a00f0 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e ential, dependin
a0100 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 g on exponent. %
a0110 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 g */.#define etS
a0120 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 IZE 5 /*
a0130 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 Return number of
a0140 20 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 characters proc
a0150 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e essed so far. %n
a0160 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 */.#define etST
a0170 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 RING 6 /* S
a0180 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 trings. %s */.#d
a0190 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e efine etDYNSTRIN
a01a0 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 G 7 /* Dynamic
a01b0 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 ally allocated s
a01c0 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 trings. %z */.#d
a01d0 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 efine etPERCENT
a01e0 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 8 /* Percent
a01f0 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 symbol. %% */.#
a0200 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 define etCHARX
a0210 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 9 /* Charac
a0220 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 ters. %c */./* T
a0230 68 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65 he rest are exte
a0240 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d nsions, not norm
a0250 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 ally found in pr
a0260 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e intf() */.#defin
a0270 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 e etSQLESCAPE 1
a0280 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 0 /* Strings wit
a0290 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 h '\'' doubled.
a02a0 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %q */.#define e
a02b0 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f tSQLESCAPE2 11 /
a02c0 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 * Strings with '
a02d0 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 \'' doubled and
a02e0 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a enclosed in '',.
a02f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0300 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 NULL p
a0310 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 ointers replaced
a0320 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 by SQL NULL. %
a0330 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 Q */.#define etT
a0340 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 OKEN 12 /*
a0350 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 a pointer to a T
a0360 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a oken structure *
a0370 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c /.#define etSRCL
a0380 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 IST 13 /* a p
a0390 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c ointer to a SrcL
a03a0 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 ist */.#define e
a03b0 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f tPOINTER 14 /
a03c0 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 * The %p convers
a03d0 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 ion */.#define e
a03e0 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f tSQLESCAPE3 15 /
a03f0 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 * %w -> Strings
a0400 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 with '\"' double
a0410 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f d */.#define etO
a0420 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 RDINAL 16 /*
a0430 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 %r -> 1st, 2nd,
a0440 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 3rd, 4th, etc.
a0450 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a English only */.
a0460 0a 23 64 65 66 69 6e 65 20 65 74 49 4e 56 41 4c .#define etINVAL
a0470 49 44 20 20 20 20 20 30 20 2f 2a 20 41 6e 79 20 ID 0 /* Any
a0480 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63 6f 6e unrecognized con
a0490 76 65 72 73 69 6f 6e 20 74 79 70 65 20 2a 2f 0a version type */.
a04a0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 ../*.** An "etBy
a04b0 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 te" is an 8-bit
a04c0 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a unsigned value..
a04d0 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 */.typedef unsig
a04e0 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b ned char etByte;
a04f0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 ../*.** Each bui
a0500 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 ltin conversion
a0510 63 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 character (ex: t
a0520 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 he 'd' in "%d")
a0530 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 is described.**
a0540 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
a0550 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
a0560 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 structure.*/.typ
a0570 65 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 edef struct et_i
a0580 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 nfo { /* Infor
a0590 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 mation about eac
a05a0 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a h format field *
a05b0 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 /. char fmttype
a05c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a05d0 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 The format field
a05e0 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a code letter */.
a05f0 20 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 etByte base;
a0600 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
a0610 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 e base for radix
a0620 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 conversion */.
a0630 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 etByte flags;
a0640 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
a0650 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 or more of FLAG
a0660 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f _ constants belo
a0670 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 w */. etByte ty
a0680 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pe;
a0690 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 /* Conversion pa
a06a0 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 radigm */. etBy
a06b0 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 te charset;
a06c0 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
a06d0 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 nto aDigits[] of
a06e0 20 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69 the digits stri
a06f0 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 ng */. etByte p
a0700 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 refix;
a0710 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
a0720 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 aPrefix[] of the
a0730 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a prefix string *
a0740 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a /.} et_info;../*
a0750 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
a0760 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 es for et_info.f
a0770 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lags.*/.#define
a0780 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 FLAG_SIGNED 1
a0790 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
a07a0 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 e value to conve
a07b0 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a rt is signed */.
a07c0 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 #define FLAG_INT
a07d0 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 ERN 2 /* Tr
a07e0 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e ue if for intern
a07f0 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 al use only */.#
a0800 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 define FLAG_STRI
a0810 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c NG 4 /* All
a0820 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 ow infinity prec
a0830 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a ision */.../*.**
a0840 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
a0850 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 able is searched
a0860 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 linearly, so it
a0870 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 is good to put
a0880 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 the.** most freq
a0890 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 uently used conv
a08a0 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 ersion types fir
a08b0 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f st..*/.static co
a08c0 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73 nst char aDigits
a08d0 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 [] = "0123456789
a08e0 41 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39 ABCDEF0123456789
a08f0 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 abcdef";.static
a0900 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 const char aPref
a0910 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 ix[] = "-x0\000X
a0920 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 0";.static const
a0930 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f et_info fmtinfo
a0940 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c [] = {. { 'd',
a0950 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 10, 1, etRADIX,
a0960 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 0, 0 },.
a0970 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 { 's', 0, 4,
a0980 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c etSTRING, 0,
a0990 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 0 },. { 'g',
a09a0 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 0, 1, etGENERI
a09b0 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 C, 30, 0 },.
a09c0 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 { 'z', 0, 4,
a09d0 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c etDYNSTRING, 0,
a09e0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 0 },. { 'q',
a09f0 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
a0a00 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 APE, 0, 0 },.
a0a10 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 { 'Q', 0, 4,
a0a20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c etSQLESCAPE2, 0,
a0a30 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 0 },. { 'w',
a0a40 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 0, 4, etSQLESC
a0a50 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 APE3, 0, 0 },.
a0a60 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 { 'c', 0, 0,
a0a70 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c etCHARX, 0,
a0a80 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 0 },. { 'o',
a0a90 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 8, 0, etRADIX,
a0aa0 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 0, 2 },.
a0ab0 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 { 'u', 10, 0,
a0ac0 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
a0ad0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 0 },. { 'x',
a0ae0 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 16, 0, etRADIX,
a0af0 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 16, 1 },.
a0b00 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 { 'X', 16, 0,
a0b10 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c etRADIX, 0,
a0b20 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 4 },.#ifndef S
a0b30 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
a0b40 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 ING_POINT. { '
a0b50 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f f', 0, 1, etFLO
a0b60 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d AT, 0, 0 }
a0b70 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 ,. { 'e', 0,
a0b80 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 1, etEXP,
a0b90 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 30, 0 },. { '
a0ba0 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 E', 0, 1, etEXP
a0bb0 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d , 14, 0 }
a0bc0 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 ,. { 'G', 0,
a0bd0 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 1, etGENERIC,
a0be0 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 14, 0 },.#endif
a0bf0 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 . { 'i', 10, 1
a0c00 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
a0c10 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 0, 0 },. { 'n
a0c20 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 ', 0, 0, etSIZE
a0c30 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c , 0, 0 },
a0c40 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 . { '%', 0, 0
a0c50 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 , etPERCENT,
a0c60 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 0, 0 },. { 'p
a0c70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e ', 16, 0, etPOIN
a0c80 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c TER, 0, 1 },
a0c90 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 ../* All the res
a0ca0 74 20 68 61 76 65 20 74 68 65 20 46 4c 41 47 5f t have the FLAG_
a0cb0 49 4e 54 45 52 4e 20 62 69 74 20 73 65 74 20 61 INTERN bit set a
a0cc0 6e 64 20 61 72 65 20 74 68 75 73 20 66 6f 72 20 nd are thus for
a0cd0 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 internal.** use
a0ce0 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 54 27 only */. { 'T'
a0cf0 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e , 0, 2, etTOKEN
a0d00 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a , 0, 0 },.
a0d10 20 20 7b 20 20 27 53 27 2c 20 20 30 2c 20 32 2c { 'S', 0, 2,
a0d20 20 65 74 53 52 43 4c 49 53 54 2c 20 20 20 20 30 etSRCLIST, 0
a0d30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 72 27 , 0 },. { 'r'
a0d40 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 44 49 4e , 10, 3, etORDIN
a0d50 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a AL, 0, 0 },.
a0d60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c };../*.** If SQL
a0d70 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
a0d80 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 66 69 6e G_POINT is defin
a0d90 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 ed, then none of
a0da0 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f the floating po
a0db0 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f int.** conversio
a0dc0 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f ns will work..*/
a0dd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a0de0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
a0df0 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 INT./*.** "*val"
a0e00 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63 is a double suc
a0e10 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 h that 0.1 <= *v
a0e20 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 al < 10.0.** Ret
a0e30 75 72 6e 20 74 68 65 20 61 73 63 69 69 20 63 6f urn the ascii co
a0e40 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61 64 69 de for the leadi
a0e50 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76 61 6c ng digit of *val
a0e60 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 , then.** multip
a0e70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31 30 2e ly "*val" by 10.
a0e80 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 0 to renormalize
a0e90 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a ..**.** Example:
a0ea0 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 .** input:
a0eb0 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 *val = 3.1415
a0ec0 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 9.** output:
a0ed0 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 *val = 1.415
a0ee0 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 9 function re
a0ef0 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a turn = '3'.**.**
a0f00 20 54 68 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e The counter *cn
a0f10 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 t is incremented
a0f20 20 65 61 63 68 20 74 69 6d 65 2e 20 20 41 66 74 each time. Aft
a0f30 65 72 20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 er counter excee
a0f40 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 ds.** 16 (the nu
a0f50 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 mber of signific
a0f60 61 6e 74 20 64 69 67 69 74 73 20 69 6e 20 61 20 ant digits in a
a0f70 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 20 27 30 64-bit float) '0
a0f80 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 ' is.** always r
a0f90 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
a0fa0 69 63 20 63 68 61 72 20 65 74 5f 67 65 74 64 69 ic char et_getdi
a0fb0 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 git(LONGDOUBLE_T
a0fc0 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 YPE *val, int *c
a0fd0 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 67 69 74 nt){. int digit
a0fe0 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 ;. LONGDOUBLE_T
a0ff0 59 50 45 20 64 3b 0a 20 20 69 66 28 20 28 2a 63 YPE d;. if( (*c
a1000 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65 nt)++ >= 16 ) re
a1010 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 69 67 69 turn '0';. digi
a1020 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 t = (int)*val;.
a1030 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 64 69 d = digit;. di
a1040 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 git += '0';. *v
a1050 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 29 2a al = (*val - d)*
a1060 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 10.0;. return (
a1070 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a 23 65 char)digit;.}.#e
a1080 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
a1090 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
a10a0 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 NT */../*.** App
a10b0 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68 61 72 end N space char
a10c0 61 63 74 65 72 73 20 74 6f 20 74 68 65 20 67 69 acters to the gi
a10d0 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66 66 65 ven string buffe
a10e0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
a10f0 64 20 61 70 70 65 6e 64 53 70 61 63 65 28 53 74 d appendSpace(St
a1100 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 rAccum *pAccum,
a1110 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 63 int N){. static
a1120 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 61 const char zSpa
a1130 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 20 ces[] = "
a1140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1150 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69 6c 65 ";. while
a1160 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66 ( N>=(int)sizeof
a1170 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a 20 (zSpaces)-1 ){.
a1180 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a1190 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a11a0 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 zSpaces, sizeof
a11b0 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 20 (zSpaces)-1);.
a11c0 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 N -= sizeof(zS
a11d0 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20 paces)-1;. }.
a11e0 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 if( N>0 ){. s
a11f0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
a1200 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 pend(pAccum, zSp
a1210 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a aces, N);. }.}.
a1220 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e ./*.** On machin
a1230 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c 20 es with a small
a1240 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20 stack size, you
a1250 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68 65 can redefine the
a1260 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 .** SQLITE_PRINT
a1270 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 20 _BUF_SIZE to be
a1280 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e 0a 2a less than 350..*
a1290 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
a12a0 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a _PRINT_BUF_SIZE.
a12b0 23 20 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c # if defined(SQL
a12c0 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 ITE_SMALL_STACK)
a12d0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 .# define SQLI
a12e0 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a TE_PRINT_BUF_SIZ
a12f0 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 E 50.# else.#
a1300 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 define SQLITE_PR
a1310 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 35 30 INT_BUF_SIZE 350
a1320 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
a1330 23 64 65 66 69 6e 65 20 65 74 42 55 46 53 49 5a #define etBUFSIZ
a1340 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 E SQLITE_PRINT_B
a1350 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65 UF_SIZE /* Size
a1360 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 of the output b
a1370 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 uffer */../*.**
a1380 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d The root program
a1390 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e . All variation
a13a0 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 65 s call this core
a13b0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a ..**.** INPUTS:.
a13c0 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68 69 73 ** func This
a13d0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
a13e0 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 a function taki
a13f0 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e ng three argumen
a1400 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ts.**
a1410 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 1. A pointer to
a1420 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d 65 anything. Same
a1430 20 61 73 20 74 68 65 20 22 61 72 67 22 20 70 61 as the "arg" pa
a1440 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20 rameter..**
a1450 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f 69 6e 2. A poin
a1460 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ter to the list
a1470 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f of characters to
a1480 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 be output.**
a1490 20 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 (Not
a14a0 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69 73 20 e, this list is
a14b0 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 NOT null termina
a14c0 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20 ted.).**
a14d0 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 65 3. An intege
a14e0 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 r number of char
a14f0 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 acters to be out
a1500 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 put..**
a1510 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 69 (Note: Thi
a1520 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 62 s number might b
a1530 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20 e zero.).**.**
a1540 20 61 72 67 20 20 20 20 54 68 69 73 20 69 73 20 arg This is
a1550 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 the pointer to a
a1560 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20 77 69 nything which wi
a1570 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61 73 20 ll be passed as
a1580 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
a1590 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
a15a0 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65 20 69 o "func". Use i
a15b0 74 20 66 6f 72 20 77 68 61 74 65 76 65 72 20 79 t for whatever y
a15c0 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20 ou like..**.**
a15d0 20 66 6d 74 20 20 20 20 54 68 69 73 20 69 73 20 fmt This is
a15e0 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e the format strin
a15f0 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75 73 75 g, as in the usu
a1600 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 al print..**.**
a1610 20 20 61 70 20 20 20 20 20 54 68 69 73 20 69 73 ap This is
a1620 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
a1630 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 list of argument
a1640 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a s. Same as in.*
a1650 2a 20 20 20 20 20 20 20 20 20 20 76 66 70 72 69 * vfpri
a1660 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54 nt..**.** OUTPUT
a1670 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 54 S:.** T
a1680 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
a1690 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d is the total num
a16a0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 ber of character
a16b0 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 s sent to.**
a16c0 20 20 20 20 20 20 74 68 65 20 66 75 6e 63 74 69 the functi
a16d0 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 74 75 on "func". Retu
a16e0 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f rns -1 on a erro
a16f0 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 r..**.** Note th
a1700 61 74 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 at the order in
a1710 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69 63 20 which automatic
a1720 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 64 65 variables are de
a1730 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20 clared below.**
a1740 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20 seems to make a
a1750 62 69 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 big difference i
a1760 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f n determining ho
a1770 77 20 66 61 73 74 20 74 68 69 73 20 62 65 61 73 w fast this beas
a1780 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a t.** will run..*
a1790 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
a17a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 void sqlite3VXP
a17b0 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75 rintf(. StrAccu
a17c0 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20 m *pAccum,
a17d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
a17e0 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74 ccumulate result
a17f0 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 s here */. int
a1800 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 useExtended,
a1810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a1820 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 * Allow extended
a1830 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a %-conversions *
a1840 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
a1850 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 fmt,
a1860 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 /* Format
a1870 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f string */. va_
a1880 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 20 20 list ap
a1890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a18a0 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a /* arguments */.
a18b0 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 ){. int c;
a18c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a18d0 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 /* Next characte
a18e0 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 r in the format
a18f0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 string */. char
a1900 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 *bufpt;
a1910 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
a1920 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 r to the convers
a1930 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 ion buffer */.
a1940 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 int precision;
a1950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 /* Pr
a1960 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 ecision of the c
a1970 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a urrent field */.
a1980 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 int length;
a1990 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a19a0 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 Length of the fi
a19b0 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 eld */. int idx
a19c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a19d0 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c /* A general
a19e0 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f purpose loop co
a19f0 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 unter */. int w
a1a00 69 64 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 idth;
a1a10 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f /* Width o
a1a20 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 f the current fi
a1a30 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 eld */. etByte
a1a40 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 flag_leftjustify
a1a50 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 ; /* True if "
a1a60 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 -" flag is prese
a1a70 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
a1a80 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20 lag_plussign;
a1a90 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b /* True if "+
a1aa0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
a1ab0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
a1ac0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20 ag_blanksign;
a1ad0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22 /* True if " "
a1ae0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a1af0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a1b00 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b g_alternateform;
a1b10 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23 22 20 /* True if "#"
a1b20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 flag is present
a1b30 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
a1b40 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20 _altform2;
a1b50 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22 20 66 /* True if "!" f
a1b60 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a lag is present *
a1b70 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
a1b80 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f zeropad; /
a1b90 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c 64 20 * True if field
a1ba0 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73 width constant s
a1bb0 74 61 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20 tarts with zero
a1bc0 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 */. etByte flag
a1bd0 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20 _long;
a1be0 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22 20 66 /* True if "l" f
a1bf0 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a lag is present *
a1c00 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
a1c10 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f longlong; /
a1c20 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 22 6c * True if the "l
a1c30 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 l" flag is prese
a1c40 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64 nt */. etByte d
a1c50 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 one;
a1c60 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69 /* Loop termi
a1c70 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20 nation flag */.
a1c80 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c sqlite_uint64 l
a1c90 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56 ongvalue; /* V
a1ca0 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72 alue for integer
a1cb0 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 types */. LONG
a1cc0 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c DOUBLE_TYPE real
a1cd0 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20 value; /* Value
a1ce0 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a for real types *
a1cf0 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 /. const et_inf
a1d00 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f o *infop; /
a1d10 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 * Pointer to the
a1d20 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 appropriate inf
a1d30 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 o structure */.
a1d40 20 63 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 char buf[etBUFS
a1d50 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 IZE]; /* C
a1d60 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 onversion buffer
a1d70 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69 */. char prefi
a1d80 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
a1d90 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61 72 61 /* Prefix chara
a1da0 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d cter. "+" or "-
a1db0 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27 " or " " or '\0'
a1dc0 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 . */. etByte xt
a1dd0 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ype = 0;
a1de0 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 /* Conversion
a1df0 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 paradigm */. ch
a1e00 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20 ar *zExtra;
a1e10 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
a1e20 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f a memory used fo
a1e30 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 63 6f r etTCLESCAPE co
a1e40 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 66 nversions */.#if
a1e50 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a1e60 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
a1e70 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 int exp, e2;
a1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a1e90 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c exponent of real
a1ea0 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 6f numbers */. do
a1eb0 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 20 uble rounder;
a1ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
a1ed0 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 6c for rounding fl
a1ee0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
a1ef0 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ues */. etByte
a1f00 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20 20 flag_dp;
a1f10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 /* True if d
a1f20 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 6f ecimal point sho
a1f30 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f 0a uld be shown */.
a1f40 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72 74 etByte flag_rt
a1f50 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 z; /*
a1f60 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e 67 True if trailing
a1f70 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 65 zeros should be
a1f80 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 74 removed */. et
a1f90 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 20 Byte flag_exp;
a1fa0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
a1fb0 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c 61 to force displa
a1fc0 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 6e y of the exponen
a1fd0 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b 20 t */. int nsd;
a1fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1ff0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
a2000 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
a2010 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 65 s returned */.#e
a2020 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 3d ndif.. length =
a2030 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 3b 0;. bufpt = 0;
a2040 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 6d . for(; (c=(*fm
a2050 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b 0a t))!=0; ++fmt){.
a2060 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 20 29 if( c!='%' )
a2070 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74 3b {. int amt;
a2080 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 28 . bufpt = (
a2090 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 20 char *)fmt;.
a20a0 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 20 amt = 1;.
a20b0 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b 66 while( (c=(*++f
a20c0 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 21 3d mt))!='%' && c!=
a20d0 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 20 0 ) amt++;.
a20e0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a20f0 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 Append(pAccum, b
a2100 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 20 ufpt, amt);.
a2110 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72 65 if( c==0 ) bre
a2120 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ak;. }. if
a2130 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d ( (c=(*++fmt))==
a2140 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
a2150 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
a2160 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 29 (pAccum, "%", 1)
a2170 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
a2180 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 }. /* Find
a2190 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73 20 out what flags
a21a0 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 are present */.
a21b0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a21c0 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73 ify = flag_pluss
a21d0 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b ign = flag_blank
a21e0 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61 sign = . fla
a21f0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 g_alternateform
a2200 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 = flag_altform2
a2210 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d = flag_zeropad =
a2220 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30 0;. done = 0
a2230 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 ;. do{.
a2240 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20 switch( c ){.
a2250 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 20 case '-':
a2260 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 flag_leftjustif
a2270 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65 61 6b y = 1; break
a2280 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
a2290 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 +': flag_pluss
a22a0 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 20 ign = 1;
a22b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
a22c0 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61 67 5f ase ' ': flag_
a22d0 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20 blanksign = 1;
a22e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a22f0 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 20 20 case '#':
a2300 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f flag_alternatefo
a2310 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b rm = 1; break;
a2320 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 21 . case '!
a2330 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 ': flag_altfor
a2340 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 m2 = 1; b
a2350 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
a2360 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a se '0': flag_z
a2370 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 20 eropad = 1;
a2380 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a2390 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 64 default: d
a23a0 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 20 20 one = 1;
a23b0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
a23c0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69 }. }whi
a23d0 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28 63 3d le( !done && (c=
a23e0 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a (*++fmt))!=0 );.
a23f0 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66 /* Get the f
a2400 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 20 ield width */.
a2410 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 width = 0;.
a2420 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 if( c=='*' ){.
a2430 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 61 5f width = va_
a2440 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 arg(ap,int);.
a2450 20 20 20 69 66 28 20 77 69 64 74 68 3c 30 20 29 if( width<0 )
a2460 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c {. flag_l
a2470 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a eftjustify = 1;.
a2480 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 width =
a2490 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a -width;. }.
a24a0 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
a24b0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
a24c0 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 while( c>='0'
a24d0 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 && c<='9' ){.
a24e0 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 77 69 width = wi
a24f0 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 dth*10 + c - '0'
a2500 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b ;. c = *+
a2510 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 +fmt;. }.
a2520 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69 64 74 }. if( widt
a2530 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 30 h > etBUFSIZE-10
a2540 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 ){. width
a2550 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a = etBUFSIZE-10;.
a2560 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 }. /* Get
a2570 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 2a the precision *
a2580 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2e 27 /. if( c=='.'
a2590 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 ){. precis
a25a0 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 ion = 0;. c
a25b0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a25c0 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 if( c=='*' ){.
a25d0 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e precision
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 20 20 69 66 28 20 70 );. if( p
a2600 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 recision<0 ) pre
a2610 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73 cision = -precis
a2620 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 3d ion;. c =
a2630 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d *++fmt;. }
a2640 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 68 else{. wh
a2650 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 ile( c>='0' && c
a2660 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 <='9' ){.
a2670 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 precision = p
a2680 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20 recision*10 + c
a2690 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 - '0';.
a26a0 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
a26b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
a26c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a26d0 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a precision = -1;.
a26e0 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 }. /* Get
a26f0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
a2700 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f type modifier */
a2710 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 . if( c=='l'
a2720 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f ){. flag_lo
a2730 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 20 ng = 1;. c
a2740 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 = *++fmt;.
a2750 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 if( c=='l' ){.
a2760 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c flag_longl
a2770 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ong = 1;.
a2780 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
a2790 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a27a0 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 flag_longlong
a27b0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
a27c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c }else{. fl
a27d0 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c ag_long = flag_l
a27e0 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 onglong = 0;.
a27f0 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 20 }. /* Fetch
a2800 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 66 the info entry f
a2810 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a or the field */.
a2820 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 infop = &fmt
a2830 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 74 79 info[0];. xty
a2840 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 44 3b 0a pe = etINVALID;.
a2850 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 for(idx=0; i
a2860 64 78 3c 41 72 72 61 79 53 69 7a 65 28 66 6d 74 dx<ArraySize(fmt
a2870 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b 0a 20 info); idx++){.
a2880 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d 74 69 if( c==fmti
a2890 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65 nfo[idx].fmttype
a28a0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 66 6f ){. info
a28b0 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78 p = &fmtinfo[idx
a28c0 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 ];. if( u
a28d0 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69 seExtended || (i
a28e0 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c nfop->flags & FL
a28f0 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b AG_INTERN)==0 ){
a2900 0a 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 . xtype
a2910 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a = infop->type;.
a2920 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
a2930 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b return;
a2940 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a2950 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a2960 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74 }. }. zExt
a2970 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 2f 2a ra = 0;... /*
a2980 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 Limit the preci
a2990 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 sion to prevent
a29a0 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b overflowing buf[
a29b0 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 ] during convers
a29c0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 ion */. if( p
a29d0 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 recision>etBUFSI
a29e0 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d ZE-40 && (infop-
a29f0 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 >flags & FLAG_ST
a2a00 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 RING)==0 ){.
a2a10 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 precision = et
a2a20 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 BUFSIZE-40;.
a2a30 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a }.. /*. **
a2a40 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 At this point,
a2a50 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e variables are in
a2a60 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c itialized as fol
a2a70 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 lows:. **.
a2a80 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 ** flag_alter
a2a90 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 nateform
a2aa0 20 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 TRUE if a '#'
a2ab0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 is present..
a2ac0 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 ** flag_altfor
a2ad0 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m2
a2ae0 20 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 TRUE if a '!' i
a2af0 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a s present.. *
a2b00 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 * flag_plussig
a2b10 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
a2b20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 TRUE if a '+' is
a2b30 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a present.. **
a2b40 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a2b50 69 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 ify T
a2b60 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 RUE if a '-' is
a2b70 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 present or if th
a2b80 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 e. **
a2b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2ba0 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 field wid
a2bb0 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e th was negative.
a2bc0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a . ** flag_z
a2bd0 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 eropad
a2be0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 TRUE if th
a2bf0 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 e width began wi
a2c00 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 th 0.. ** f
a2c10 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 lag_long
a2c20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 TRUE
a2c30 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c if the letter 'l
a2c40 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 ' (ell) prefixed
a2c50 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
a2c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2c70 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 the conver
a2c80 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a sion character..
a2c90 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f ** flag_lo
a2ca0 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 nglong
a2cb0 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 TRUE if the
a2cc0 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c letter 'll' (el
a2cd0 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a l ell) prefixed.
a2ce0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
a2cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2d00 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 the convers
a2d10 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 ion character..
a2d20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 ** flag_bla
a2d30 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 nksign
a2d40 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 TRUE if a '
a2d50 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
a2d60 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 ** width
a2d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2d80 20 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 The specified
a2d90 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 field width. T
a2da0 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 his is. **
a2db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2dc0 20 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 alwa
a2dd0 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e ys non-negative.
a2de0 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 Zero is the de
a2df0 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 fault.. **
a2e00 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 precision
a2e10 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
a2e20 73 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 specified precis
a2e30 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c ion. The defaul
a2e40 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 t. **
a2e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e60 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 is -1..
a2e70 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 ** xtype
a2e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2e90 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 The class of
a2ea0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a the conversion..
a2eb0 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 ** infop
a2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2ed0 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 Pointer to
a2ee0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
a2ef0 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 info struct..
a2f00 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 */. switch(
a2f10 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 xtype ){. c
a2f20 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 ase etPOINTER:.
a2f30 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 flag_long
a2f40 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 long = sizeof(ch
a2f50 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 ar*)==sizeof(i64
a2f60 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f );. flag_
a2f70 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 long = sizeof(ch
a2f80 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e ar*)==sizeof(lon
a2f90 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 g int);.
a2fa0 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 /* Fall through
a2fb0 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 into the next ca
a2fc0 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 se */. case
a2fd0 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 etORDINAL:.
a2fe0 20 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a case etRADIX:.
a2ff0 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
a3000 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f p->flags & FLAG_
a3010 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 SIGNED ){.
a3020 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 i64 v;.
a3030 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f if( flag_lo
a3040 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 nglong ){.
a3050 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 v = va_arg
a3060 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 (ap,i64);.
a3070 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c }else if( fl
a3080 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 ag_long ){.
a3090 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 v = va_ar
a30a0 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a g(ap,long int);.
a30b0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
a30c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d . v =
a30d0 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b va_arg(ap,int);
a30e0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a30f0 20 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 if( v<0 )
a3100 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f {. lo
a3110 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 ngvalue = -v;.
a3120 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 prefix
a3130 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 = '-';.
a3140 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
a3150 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d longvalue =
a3160 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 v;.
a3170 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 if( flag_plussig
a3180 6e 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 n ) prefi
a3190 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 x = '+';.
a31a0 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c else if( fl
a31b0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 ag_blanksign )
a31c0 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 prefix = ' ';.
a31d0 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 else
a31e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a31f0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b prefix = 0;
a3200 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a3210 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a3220 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c if( flag_l
a3230 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 onglong ){.
a3240 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 longvalue
a3250 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 = va_arg(ap,u64
a3260 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
a3270 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 se if( flag_long
a3280 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a3290 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 longvalue = va_a
a32a0 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c rg(ap,unsigned l
a32b0 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ong int);.
a32c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a32d0 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 longvalue
a32e0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 = va_arg(ap,uns
a32f0 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 igned int);.
a3300 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3310 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 prefix = 0;.
a3320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3330 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 if( longvalue==0
a3340 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 ) flag_alternat
a3350 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 eform = 0;.
a3360 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f if( flag_zero
a3370 70 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e pad && precision
a3380 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d <width-(prefix!=
a3390 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 0) ){.
a33a0 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 precision = widt
a33b0 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 h-(prefix!=0);.
a33c0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a33d0 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 bufpt = &buf[et
a33e0 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 BUFSIZE-1];.
a33f0 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 if( xtype==e
a3400 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 tORDINAL ){.
a3410 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
a3420 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d st char zOrd[] =
a3430 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 "thstndrd";.
a3440 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 int x = (
a3450 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 int)(longvalue %
a3460 20 31 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 10);.
a3470 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e if( x>=4 || (lon
a3480 67 76 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 gvalue/10)%10==1
a3490 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a34a0 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 x = 0;.
a34b0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 }. buf
a34c0 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 [etBUFSIZE-3] =
a34d0 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 zOrd[x*2];.
a34e0 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 buf[etBUFSI
a34f0 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 ZE-2] = zOrd[x*2
a3500 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 +1];. b
a3510 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 ufpt -= 2;.
a3520 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 }. {.
a3530 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 registe
a3540 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 r const char *cs
a3550 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 et; /* Use
a3560 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 registers for sp
a3570 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 eed */.
a3580 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 register int ba
a3590 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 se;. cs
a35a0 65 74 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e et = &aDigits[in
a35b0 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 fop->charset];.
a35c0 20 20 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 base =
a35d0 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 infop->base;.
a35e0 20 20 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 do{
a35f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3610 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 /* Convert
a3620 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 to ascii */.
a3630 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 *(--bufp
a3640 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 t) = cset[longva
a3650 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 lue%base];.
a3660 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 longvalue
a3670 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 = longvalue/bas
a3680 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 e;. }wh
a3690 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 ile( longvalue>0
a36a0 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 );. }.
a36b0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 length = (
a36c0 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 int)(&buf[etBUFS
a36d0 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 IZE-1]-bufpt);.
a36e0 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 for(idx=p
a36f0 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b recision-length;
a3700 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a idx>0; idx--){.
a3710 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 *(--bu
a3720 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 fpt) = '0';
a3730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3740 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 /* Zero
a3750 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d pad */. }
a3760 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 . if( pre
a3770 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 fix ) *(--bufpt)
a3780 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 = prefix;
a3790 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 /* Add
a37a0 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 sign */.
a37b0 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 if( flag_alterna
a37c0 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d teform && infop-
a37d0 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 >prefix ){
a37e0 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 /* Add "0" or "0
a37f0 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 x" */.
a3800 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b const char *pre;
a3810 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 . char
a3820 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 x;. pre
a3830 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f = &aPrefix[info
a3840 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 p->prefix];.
a3850 20 20 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 for(; (x=(
a3860 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b *pre))!=0; pre++
a3870 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 ) *(--bufpt) = x
a3880 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a3890 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e length = (in
a38a0 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a t)(&buf[etBUFSIZ
a38b0 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 E-1]-bufpt);.
a38c0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a38d0 20 20 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a case etFLOAT:.
a38e0 20 20 20 20 20 20 63 61 73 65 20 65 74 45 58 50 case etEXP
a38f0 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 47 :. case etG
a3900 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 ENERIC:.
a3910 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 realvalue = va_a
a3920 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 rg(ap,double);.#
a3930 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a3940 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
a3950 54 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 T. if( pr
a3960 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 ecision<0 ) prec
a3970 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 ision = 6;
a3980 20 20 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c /* Set defaul
a3990 74 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 t precision */.
a39a0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 if( preci
a39b0 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 sion>etBUFSIZE/2
a39c0 2d 31 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 -10 ) precision
a39d0 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 = etBUFSIZE/2-10
a39e0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 ;. if( re
a39f0 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 alvalue<0.0 ){.
a3a00 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c realval
a3a10 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b ue = -realvalue;
a3a20 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 . prefi
a3a30 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 x = '-';.
a3a40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a3a50 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 if( flag_pluss
a3a60 69 67 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 ign ) p
a3a70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 refix = '+';.
a3a80 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 else if(
a3a90 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 flag_blanksign )
a3aa0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 prefix = ' '
a3ab0 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 ;. else
a3ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3ad0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 prefix
a3ae0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 0;. }.
a3af0 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 if( xtype
a3b00 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 ==etGENERIC && p
a3b10 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 recision>0 ) pre
a3b20 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a cision--;.#if 0.
a3b30 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 /* Round
a3b40 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 ing works like B
a3b50 53 44 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 SD when the cons
a3b60 74 61 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 tant 0.4999 is u
a3b70 73 65 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a sed. Wierd! */.
a3b80 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d for(idx=
a3b90 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 precision, round
a3ba0 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 er=0.4999; idx>0
a3bb0 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 ; idx--, rounder
a3bc0 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 *=0.1);.#else.
a3bd0 20 20 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 /* It make
a3be0 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 s more sense to
a3bf0 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 use 0.5 */.
a3c00 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 for(idx=preci
a3c10 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e sion, rounder=0.
a3c20 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 5; idx>0; idx--,
a3c30 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d rounder*=0.1){}
a3c40 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
a3c50 69 66 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f if( xtype==etFLO
a3c60 41 54 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b AT ) realvalue +
a3c70 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 = rounder;.
a3c80 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 /* Normalize
a3c90 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 realvalue to wit
a3ca0 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 hin 10.0 > realv
a3cb0 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 alue >= 1.0 */.
a3cc0 20 20 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a exp = 0;.
a3cd0 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
a3ce0 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 te3IsNaN((double
a3cf0 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 )realvalue) ){.
a3d00 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d bufpt =
a3d10 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 "NaN";.
a3d20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 length = 3;.
a3d30 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a3d40 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3d50 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 if( realvalue>0
a3d60 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 .0 ){.
a3d70 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 while( realvalue
a3d80 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 >=1e32 && exp<=3
a3d90 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 50 ){ realvalue
a3da0 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 *= 1e-32; exp+=3
a3db0 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 2; }. w
a3dc0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e hile( realvalue>
a3dd0 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 =1e8 && exp<=350
a3de0 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d ){ realvalue *=
a3df0 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 1e-8; exp+=8; }
a3e00 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
a3e10 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e ( realvalue>=10.
a3e20 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 0 && exp<=350 ){
a3e30 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e realvalue *= 0.
a3e40 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 1; exp++; }.
a3e50 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 while( rea
a3e60 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 lvalue<1e-8 ){ r
a3e70 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b ealvalue *= 1e8;
a3e80 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 exp-=8; }.
a3e90 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c while( real
a3ea0 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 value<1.0 ){ rea
a3eb0 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 lvalue *= 10.0;
a3ec0 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 exp--; }.
a3ed0 20 20 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 if( exp>350 )
a3ee0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
a3ef0 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b ( prefix=='-' ){
a3f00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 . b
a3f10 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 ufpt = "-Inf";.
a3f20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }else
a3f30 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 if( prefix=='+'
a3f40 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a3f50 20 20 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 bufpt = "+Inf"
a3f60 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 ;. }e
a3f70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
a3f80 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 bufpt = "Inf"
a3f90 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
a3fa0 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 leng
a3fb0 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c th = sqlite3Strl
a3fc0 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 en30(bufpt);.
a3fd0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
a3fe0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a3ff0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 }. bu
a4000 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 fpt = buf;.
a4010 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a /*. **
a4020 20 49 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 If the field ty
a4030 70 65 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c pe is etGENERIC,
a4040 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f then convert to
a4050 20 65 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 either etEXP.
a4060 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c ** or etFL
a4070 4f 41 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 OAT, as appropri
a4080 61 74 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ate.. */.
a4090 20 20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 flag_exp
a40a0 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b = xtype==etEXP;
a40b0 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 . if( xty
a40c0 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 pe!=etFLOAT ){.
a40d0 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c realval
a40e0 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 ue += rounder;.
a40f0 20 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 if( rea
a4100 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 lvalue>=10.0 ){
a4110 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 realvalue *= 0.1
a4120 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 ; exp++; }.
a4130 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
a4140 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 xtype==etGENERI
a4150 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 C ){. f
a4160 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f lag_rtz = !flag_
a4170 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 alternateform;.
a4180 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 if( exp
a4190 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 <-4 || exp>preci
a41a0 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 sion ){.
a41b0 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 xtype = etEX
a41c0 50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c P;. }el
a41d0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a41e0 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 precision = prec
a41f0 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 ision - exp;.
a4200 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d xtype =
a4210 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 etFLOAT;.
a4220 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
a4230 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 lse{. f
a4240 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 lag_rtz = 0;.
a4250 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a4260 66 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 f( xtype==etEXP
a4270 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 ){. e2
a4280 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c = 0;. }el
a4290 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 se{. e2
a42a0 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 = exp;.
a42b0 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 }. nsd =
a42c0 30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 0;. flag_
a42d0 64 70 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e dp = (precision>
a42e0 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 0 ?1:0) | flag_a
a42f0 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 lternateform | f
a4300 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 lag_altform2;.
a4310 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 /* The sig
a4320 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 n in front of th
a4330 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
a4340 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 if( prefix )
a4350 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
a4360 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b fpt++) = prefix;
a4370 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a4380 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 /* Digits pri
a4390 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 or to the decima
a43a0 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 l point */.
a43b0 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 if( e2<0 ){.
a43c0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a43d0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 ++) = '0';.
a43e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a43f0 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b for(; e2>=0;
a4400 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 e2--){.
a4410 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a4420 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 et_getdigit(&re
a4430 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 alvalue,&nsd);.
a4440 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
a4450 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 }. /*
a4460 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e The decimal poin
a4470 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 t */. if(
a4480 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 flag_dp ){.
a4490 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a44a0 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 = '.';.
a44b0 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 }. /* "0"
a44c0 20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 digits after th
a44d0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
a44e0 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 but before the f
a44f0 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 irst. **
a4500 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
a4510 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 t of the number
a4520 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 */. for(e
a4530 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 2++; e2<0; preci
a4540 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 sion--, e2++){.
a4550 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
a4560 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a precision>0 );.
a4570 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 *(bufp
a4580 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 t++) = '0';.
a4590 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
a45a0 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 Significant dig
a45b0 69 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65 its after the de
a45c0 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 cimal point */.
a45d0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 while( (p
a45e0 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b recision--)>0 ){
a45f0 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
a4600 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 pt++) = et_getdi
a4610 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 git(&realvalue,&
a4620 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a nsd);. }.
a4630 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 /* Remov
a4640 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 e trailing zeros
a4650 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 and the "." if
a4660 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 no digits follow
a4670 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 the "." */.
a4680 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a if( flag_rtz
a4690 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 && flag_dp ){.
a46a0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a46b0 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 bufpt[-1]=='0' )
a46c0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b *(--bufpt) = 0;
a46d0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
a46e0 74 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a t( bufpt>buf );.
a46f0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 if( bu
a4700 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a fpt[-1]=='.' ){.
a4710 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
a4720 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b flag_altform2 ){
a4730 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a . *
a4740 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b (bufpt++) = '0';
a4750 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
a4760 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a4770 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 *(--bufpt) = 0
a4780 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
a4790 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a47a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
a47b0 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 Add the "eNNN"
a47c0 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 suffix */.
a47d0 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c if( flag_exp |
a47e0 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 | xtype==etEXP )
a47f0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 {. *(bu
a4800 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 fpt++) = aDigits
a4810 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d [infop->charset]
a4820 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a4830 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 exp<0 ){.
a4840 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a4850 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 = '-'; exp = -ex
a4860 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c p;. }el
a4870 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a4880 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 *(bufpt++) = '+'
a4890 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a48a0 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e if( exp>
a48b0 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 =100 ){.
a48c0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a48d0 20 28 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 (char)((exp/100
a48e0 29 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f )+'0'); /
a48f0 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f * 100's digit */
a4900 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70 . exp
a4910 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 %= 100;.
a4920 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a }. *
a4930 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 (bufpt++) = (cha
a4940 72 29 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 r)(exp/10+'0');
a4950 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
a4960 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 0's digit */.
a4970 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a4980 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 ) = (char)(exp%1
a4990 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 0+'0');
a49a0 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 /* 1's digit
a49b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 */. }.
a49c0 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 *bufpt = 0
a49d0 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ;.. /* Th
a49e0 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 e converted numb
a49f0 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 er is in buf[] a
a4a00 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 nd zero terminat
a4a10 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 ed. Output it..
a4a20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 ** Note t
a4a30 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 hat the number i
a4a40 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f s in the usual o
a4a50 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 rder, not revers
a4a60 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 ed as with.
a4a70 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f ** integer co
a4a80 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 nversions. */.
a4a90 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 length = (
a4aa0 69 6e 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b int)(bufpt-buf);
a4ab0 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d . bufpt =
a4ac0 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f buf;.. /
a4ad0 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 * Special case:
a4ae0 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 Add leading zer
a4af0 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a os if the flag_z
a4b00 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 eropad flag is.
a4b10 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e ** set an
a4b20 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 d we are not lef
a4b30 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 t justified */.
a4b40 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
a4b50 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 zeropad && !flag
a4b60 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 _leftjustify &&
a4b70 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b length < width){
a4b80 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
a4b90 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 ;. int
a4ba0 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c nPad = width - l
a4bb0 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 ength;.
a4bc0 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e for(i=width; i>
a4bd0 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 =nPad; i--){.
a4be0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 bufpt[i
a4bf0 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 ] = bufpt[i-nPad
a4c00 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 ];. }.
a4c10 20 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 i = pre
a4c20 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 fix!=0;.
a4c30 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 while( nPad--
a4c40 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 ) bufpt[i++] = '
a4c50 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 0';. le
a4c60 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 ngth = width;.
a4c70 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
a4c80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a4c90 20 20 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a case etSIZE:
a4ca0 0a 20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 . *(va_ar
a4cb0 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 g(ap,int*)) = pA
a4cc0 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 ccum->nChar;.
a4cd0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 length = wi
a4ce0 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dth = 0;.
a4cf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
a4d00 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 se etPERCENT:.
a4d10 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 buf[0] = '
a4d20 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 %';. bufp
a4d30 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 t = buf;.
a4d40 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 length = 1;.
a4d50 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a4d60 20 20 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a case etCHARX:.
a4d70 20 20 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 c = va_a
a4d80 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
a4d90 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 buf[0] = (ch
a4da0 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 ar)c;. if
a4db0 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 ( precision>=0 )
a4dc0 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 {. for(
a4dd0 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 idx=1; idx<preci
a4de0 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 sion; idx++) buf
a4df0 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b [idx] = (char)c;
a4e00 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 . lengt
a4e10 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 h = precision;.
a4e20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a4e30 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4e40 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 1;. }.
a4e50 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 bufpt = buf
a4e60 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a4e70 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 54 . case etST
a4e80 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 RING:. case
a4e90 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 etDYNSTRING:.
a4ea0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 bufpt = va
a4eb0 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a _arg(ap,char*);.
a4ec0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
a4ed0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 t==0 ){.
a4ee0 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 bufpt = "";.
a4ef0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
a4f00 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 xtype==etDYNSTRI
a4f10 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 NG ){.
a4f20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a zExtra = bufpt;.
a4f30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a4f40 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e if( precision>
a4f50 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
a4f60 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 for(length=0; le
a4f70 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 ngth<precision &
a4f80 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b & bufpt[length];
a4f90 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 length++){}.
a4fa0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a4fb0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 length = s
a4fc0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 qlite3Strlen30(b
a4fd0 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d ufpt);. }
a4fe0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a4ff0 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c case etSQL
a5000 45 53 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 ESCAPE:. ca
a5010 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a se etSQLESCAPE2:
a5020 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 . case etSQ
a5030 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 LESCAPE3: {.
a5040 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 2c int i, j, k,
a5050 20 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 n, isnull;.
a5060 20 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 int needQuot
a5070 65 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 e;. char
a5080 63 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ch;. char
a5090 20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 q = ((xtype==et
a50a0 53 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a SQLESCAPE3)?'"':
a50b0 27 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 '\''); /* Quot
a50c0 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 e character */.
a50d0 20 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 char *esc
a50e0 61 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c arg = va_arg(ap,
a50f0 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 char*);.
a5100 69 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d isnull = escarg=
a5110 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 =0;. if(
a5120 69 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 isnull ) escarg
a5130 3d 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 = (xtype==etSQLE
a5140 53 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 SCAPE2 ? "NULL"
a5150 3a 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 : "(NULL)");.
a5160 20 20 20 20 20 6b 20 3d 20 70 72 65 63 69 73 69 k = precisi
a5170 6f 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 on;. for(
a5180 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 i=n=0; (ch=escar
a5190 67 5b 69 5d 29 21 3d 30 20 26 26 20 6b 21 3d 30 g[i])!=0 && k!=0
a51a0 3b 20 69 2b 2b 2c 20 6b 2d 2d 29 7b 0a 20 20 20 ; i++, k--){.
a51b0 20 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 if( ch==q
a51c0 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 ) n++;.
a51d0 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 65 64 51 }. needQ
a51e0 75 6f 74 65 20 3d 20 21 69 73 6e 75 6c 6c 20 26 uote = !isnull &
a51f0 26 20 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 & xtype==etSQLES
a5200 43 41 50 45 32 3b 0a 20 20 20 20 20 20 20 20 6e CAPE2;. n
a5210 20 2b 3d 20 69 20 2b 20 31 20 2b 20 6e 65 65 64 += i + 1 + need
a5220 51 75 6f 74 65 2a 32 3b 0a 20 20 20 20 20 20 20 Quote*2;.
a5230 20 69 66 28 20 6e 3e 65 74 42 55 46 53 49 5a 45 if( n>etBUFSIZE
a5240 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 ){. bu
a5250 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 73 fpt = zExtra = s
a5260 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 qlite3Malloc( n
a5270 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
a5280 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 bufpt==0 ){.
a5290 20 20 20 20 20 20 20 20 20 70 41 63 63 75 6d 2d pAccum-
a52a0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
a52b0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 1;. r
a52c0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 20 eturn;.
a52d0 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
a52e0 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 {. bufp
a52f0 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 t = buf;.
a5300 20 7d 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 30 }. j = 0
a5310 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 ;. if( ne
a5320 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 5b edQuote ) bufpt[
a5330 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20 j++] = q;.
a5340 20 20 6b 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 k = i;.
a5350 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 for(i=0; i<k; i
a5360 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 ++){. b
a5370 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 20 3d ufpt[j++] = ch =
a5380 20 65 73 63 61 72 67 5b 69 5d 3b 0a 20 20 20 20 escarg[i];.
a5390 20 20 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 if( ch==q
a53a0 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 ) bufpt[j++] = c
a53b0 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 h;. }.
a53c0 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f if( needQuo
a53d0 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 te ) bufpt[j++]
a53e0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 = q;. buf
a53f0 70 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 pt[j] = 0;.
a5400 20 20 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 length = j;.
a5410 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 /* The pr
a5420 65 63 69 73 69 6f 6e 20 69 6e 20 25 71 20 61 6e ecision in %q an
a5430 64 20 25 51 20 6d 65 61 6e 73 20 68 6f 77 20 6d d %Q means how m
a5440 61 6e 79 20 69 6e 70 75 74 20 63 68 61 72 61 63 any input charac
a5450 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20 20 20 ters to.
a5460 2a 2a 20 63 6f 6e 73 75 6d 65 2c 20 6e 6f 74 20 ** consume, not
a5470 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
a5480 65 20 6f 75 74 70 75 74 2e 2e 2e 0a 20 20 20 20 e output....
a5490 20 20 20 20 2a 2a 20 69 66 28 20 70 72 65 63 69 ** if( preci
a54a0 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 69 sion>=0 && preci
a54b0 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65 sion<length ) le
a54c0 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e ngth = precision
a54d0 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 ; */. bre
a54e0 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
a54f0 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a 20 case etTOKEN:
a5500 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20 {. Token
a5510 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 67 *pToken = va_arg
a5520 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20 (ap, Token*);.
a5530 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e if( pToken
a5540 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
a5550 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a5560 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e end(pAccum, (con
a5570 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d st char*)pToken-
a5580 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a >z, pToken->n);.
a5590 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a55a0 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 length = width
a55b0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 = 0;. br
a55c0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
a55d0 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49 53 case etSRCLIS
a55e0 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63 T: {. Src
a55f0 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f List *pSrc = va_
a5600 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a arg(ap, SrcList*
a5610 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b );. int k
a5620 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e = va_arg(ap, in
a5630 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 t);. stru
a5640 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
a5650 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e *pItem = &pSrc->
a5660 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 a[k];. as
a5670 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c sert( k>=0 && k<
a5680 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 pSrc->nSrc );.
a5690 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d if( pItem-
a56a0 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20 >zDatabase ){.
a56b0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
a56c0 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a56d0 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 ccum, pItem->zDa
a56e0 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 20 tabase, -1);.
a56f0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
a5700 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 rAccumAppend(pAc
a5710 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 20 cum, ".", 1);.
a5720 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a5730 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
a5740 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 ppend(pAccum, pI
a5750 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b tem->zName, -1);
a5760 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 . length
a5770 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 = width = 0;.
a5780 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a5790 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c }. defaul
a57a0 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73 t: {. ass
a57b0 65 72 74 28 20 78 74 79 70 65 3d 3d 65 74 49 4e ert( xtype==etIN
a57c0 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 20 VALID );.
a57d0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
a57e0 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77 69 . }/* End swi
a57f0 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f 72 tch over the for
a5800 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 20 20 mat type */.
a5810 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 65 /*. ** The te
a5820 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65 72 xt of the conver
a5830 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64 20 sion is pointed
a5840 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61 6e to by "bufpt" an
a5850 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c 65 6e d is. ** "len
a5860 67 74 68 22 20 63 68 61 72 61 63 74 65 72 73 20 gth" characters
a5870 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 6c 64 long. The field
a5880 20 77 69 64 74 68 20 69 73 20 22 77 69 64 74 68 width is "width
a5890 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 74 68 ". Do. ** th
a58a0 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 2a 2f e output.. */
a58b0 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 5f 6c . if( !flag_l
a58c0 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 eftjustify ){.
a58d0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 register int
a58e0 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e nspace;. n
a58f0 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 space = width-le
a5900 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 ngth;. if(
a5910 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 nspace>0 ){.
a5920 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 appendSpace(
a5930 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b pAccum, nspace);
a5940 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
a5950 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30 20 if( length>0
a5960 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
a5970 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
a5980 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 6c 65 Accum, bufpt, le
a5990 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ngth);. }.
a59a0 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a 75 if( flag_leftju
a59b0 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72 stify ){. r
a59c0 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61 egister int nspa
a59d0 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65 ce;. nspace
a59e0 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b = width-length;
a59f0 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63 . if( nspac
a5a00 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 e>0 ){. a
a5a10 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 75 ppendSpace(pAccu
a5a20 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 20 m, nspace);.
a5a30 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
a5a40 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 20 20 ( zExtra ){.
a5a50 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
a5a60 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20 Extra);. }.
a5a70 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f 70 }/* End for loop
a5a80 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 over the format
a5a90 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 string */.} /*
a5aa0 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 End of function
a5ab0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 */../*.** Append
a5ac0 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 74 N bytes of text
a5ad0 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 53 from z to the S
a5ae0 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e 0a trAccum object..
a5af0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a5b00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
a5b10 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 rAccumAppend(Str
a5b20 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 Accum *p, const
a5b30 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29 7b char *z, int N){
a5b40 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 . assert( z!=0
a5b50 7c 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 || N==0 );. if(
a5b60 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d 3e p->tooBig | p->
a5b70 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
a5b80 20 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e testcase(p->
a5b90 74 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 65 73 tooBig);. tes
a5ba0 74 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f 63 46 tcase(p->mallocF
a5bb0 61 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 74 75 ailed);. retu
a5bc0 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3c rn;. }. if( N<
a5bd0 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 71 6c 0 ){. N = sql
a5be0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b ite3Strlen30(z);
a5bf0 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d 30 20 . }. if( N==0
a5c00 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d 30 29 20 29 || NEVER(z==0) )
a5c10 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 {. return;.
a5c20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68 61 72 }. if( p->nChar
a5c30 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 +N >= p->nAlloc
a5c40 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 ){. char *zNe
a5c50 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 75 w;. if( !p->u
a5c60 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 seMalloc ){.
a5c70 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b p->tooBig = 1;
a5c80 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e 6e 41 . N = p->nA
a5c90 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61 72 20 lloc - p->nChar
a5ca0 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 4e - 1;. if( N
a5cb0 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 <=0 ){. r
a5cc0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
a5cd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a5ce0 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e i64 szNew = p->n
a5cf0 43 68 61 72 3b 0a 20 20 20 20 20 20 73 7a 4e 65 Char;. szNe
a5d00 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20 20 w += N + 1;.
a5d10 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70 2d if( szNew > p-
a5d20 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 >mxAlloc ){.
a5d30 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a5d40 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 cumReset(p);.
a5d50 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d p->tooBig =
a5d60 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 1;. retu
a5d70 72 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b rn;. }else{
a5d80 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c . p->nAll
a5d90 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77 3b oc = (int)szNew;
a5da0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a . }. z
a5db0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
a5dc0 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 allocRaw(p->db,
a5dd0 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 p->nAlloc );.
a5de0 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 if( zNew ){.
a5df0 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e memcpy(zN
a5e00 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20 70 2d ew, p->zText, p-
a5e10 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20 20 20 >nChar);.
a5e20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a5e30 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 Reset(p);.
a5e40 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a 4e 65 p->zText = zNe
a5e50 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a w;. }else{.
a5e60 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f p->mallo
a5e70 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 cFailed = 1;.
a5e80 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a5e90 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 ccumReset(p);.
a5ea0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a5eb0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
a5ec0 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 54 65 memcpy(&p->zTe
a5ed0 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20 7a 2c xt[p->nChar], z,
a5ee0 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 72 20 N);. p->nChar
a5ef0 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 += N;.}../*.** F
a5f00 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 72 69 inish off a stri
a5f10 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 75 72 ng by making sur
a5f20 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 65 72 e it is zero-ter
a5f30 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65 74 75 minated..** Retu
a5f40 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
a5f50 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 the resulting st
a5f60 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61 20 ring. Return a
a5f70 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 NULL.** pointer
a5f80 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 if any kind of e
a5f90 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75 6e 74 rror was encount
a5fa0 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ered..*/.SQLITE_
a5fb0 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
a5fc0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
a5fd0 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a 70 29 ish(StrAccum *p)
a5fe0 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 {. if( p->zText
a5ff0 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65 78 74 ){. p->zText
a6000 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 3b 0a [p->nChar] = 0;.
a6010 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65 4d 61 if( p->useMa
a6020 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65 78 74 lloc && p->zText
a6030 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20 ==p->zBase ){.
a6040 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 73 p->zText = s
a6050 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
a6060 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43 68 61 w(p->db, p->nCha
a6070 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 r+1 );. if(
a6080 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 p->zText ){.
a6090 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e 7a memcpy(p->z
a60a0 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 2c 20 Text, p->zBase,
a60b0 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20 p->nChar+1);.
a60c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a60d0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 p->mallocFaile
a60e0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 d = 1;. }.
a60f0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
a6100 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a 0a 2f n p->zText;.}../
a6110 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20 53 74 *.** Reset an St
a6120 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e 20 20 rAccum string.
a6130 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61 6c 6c Reclaim all mall
a6140 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a oced memory..*/.
a6150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
a6160 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 oid sqlite3StrAc
a6170 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 cumReset(StrAccu
a6180 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e m *p){. if( p->
a6190 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73 65 20 zText!=p->zBase
a61a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
a61b0 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a Free(p->db, p->z
a61c0 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e Text);. }. p->
a61d0 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a zText = 0;.}../*
a61e0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
a61f0 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75 6c 61 string accumula
a6200 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 tor.*/.SQLITE_PR
a6210 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
a6220 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53 e3StrAccumInit(S
a6230 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 61 72 trAccum *p, char
a6240 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e 2c 20 *zBase, int n,
a6250 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e 7a 54 int mx){. p->zT
a6260 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 20 3d ext = p->zBase =
a6270 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64 62 20 zBase;. p->db
a6280 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61 72 20 = 0;. p->nChar
a6290 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c 6f 63 = 0;. p->nAlloc
a62a0 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41 6c 6c = n;. p->mxAll
a62b0 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e 75 73 oc = mx;. p->us
a62c0 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 70 eMalloc = 1;. p
a62d0 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a 20 20 ->tooBig = 0;.
a62e0 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 p->mallocFailed
a62f0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 = 0;.}../*.** Pr
a6300 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
a6310 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
a6320 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 liteMalloc(). U
a6330 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a se the internal.
a6340 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 ** %-conversion
a6350 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 extensions..*/.S
a6360 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
a6370 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 ar *sqlite3VMPri
a6380 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ntf(sqlite3 *db,
a6390 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
a63a0 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 rmat, va_list ap
a63b0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 ){. char *z;.
a63c0 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 char zBase[SQLIT
a63d0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 E_PRINT_BUF_SIZE
a63e0 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 ];. StrAccum ac
a63f0 63 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 c;. assert( db!
a6400 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 =0 );. sqlite3S
a6410 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 trAccumInit(&acc
a6420 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 , zBase, sizeof(
a6430 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 20 zBase),.
a6440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 db
a6450 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
a6460 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 29 3b 0a LIMIT_LENGTH]);.
a6470 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a 20 acc.db = db;.
a6480 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 sqlite3VXPrintf
a6490 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d 61 (&acc, 1, zForma
a64a0 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 t, ap);. z = sq
a64b0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
a64c0 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66 28 ish(&acc);. if(
a64d0 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 acc.mallocFaile
a64e0 64 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c d ){. db->mal
a64f0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
a6500 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d }. return z;.}
a6510 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e ../*.** Print in
a6520 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
a6530 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 ed from sqliteMa
a6540 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65 lloc(). Use the
a6550 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 internal.** %-c
a6560 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 onversion extens
a6570 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
a6580 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
a6590 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c lite3MPrintf(sql
a65a0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
a65b0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
a65c0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
a65d0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 p;. char *z;.
a65e0 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
a65f0 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c rmat);. z = sql
a6600 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c ite3VMPrintf(db,
a6610 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
a6620 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 va_end(ap);. r
a6630 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn z;.}../*.*
a6640 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d 50 * Like sqlite3MP
a6650 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 61 6c rintf(), but cal
a6660 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 l sqlite3DbFree(
a6670 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 20 ) on zStr after
a6680 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 68 formatting.** th
a6690 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 65 66 e string and bef
a66a0 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e 20 ore returnning.
a66b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
a66c0 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 intended to be
a66d0 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 used.** to modif
a66e0 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 74 y an existing st
a66f0 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d 70 ring. For examp
a6700 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 le:.**.**
a6710 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e x = sqlite3MPrin
a6720 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 66 69 tf(db, x, "prefi
a6730 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 78 29 x %s suffix", x)
a6740 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ;.**.*/.SQLITE_P
a6750 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
a6760 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c ite3MAppendf(sql
a6770 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a ite3 *db, char *
a6780 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 zStr, const char
a6790 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
a67a0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
a67b0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 char *z;. va_s
a67c0 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
a67d0 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a67e0 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f VMPrintf(db, zFo
a67f0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
a6800 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 end(ap);. sqlit
a6810 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 e3DbFree(db, zSt
a6820 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a r);. return z;.
a6830 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 }../*.** Print i
a6840 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 nto memory obtai
a6850 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
a6860 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 _malloc(). Omit
a6870 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a the internal.**
a6880 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 %-conversion ex
a6890 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c tensions..*/.SQL
a68a0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 ITE_API char *sq
a68b0 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 lite3_vmprintf(c
a68c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a68d0 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b at, va_list ap){
a68e0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 . char *z;. ch
a68f0 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f ar zBase[SQLITE_
a6900 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b PRINT_BUF_SIZE];
a6910 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b . StrAccum acc;
a6920 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a6930 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
a6940 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 if( sqlite3_init
a6950 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 ialize() ) retur
a6960 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 n 0;.#endif. sq
a6970 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 lite3StrAccumIni
a6980 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 t(&acc, zBase, s
a6990 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51 izeof(zBase), SQ
a69a0 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 LITE_MAX_LENGTH)
a69b0 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 ;. sqlite3VXPri
a69c0 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f ntf(&acc, 0, zFo
a69d0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d rmat, ap);. z =
a69e0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a69f0 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 Finish(&acc);.
a6a00 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a return z;.}../*.
a6a10 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 ** Print into me
a6a20 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
a6a30 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
a6a40 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 c()(). Omit the
a6a50 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 internal.** %-c
a6a60 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 onversion extens
a6a70 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
a6a80 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
a6a90 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 3_mprintf(const
a6aa0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
a6ab0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
a6ac0 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 69 p;. char *z;.#i
a6ad0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
a6ae0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 T_AUTOINIT. if(
a6af0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
a6b00 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 ize() ) return 0
a6b10 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 74 ;.#endif. va_st
a6b20 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
a6b30 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f ;. z = sqlite3_
a6b40 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74 vmprintf(zFormat
a6b50 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
a6b60 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b ap);. return z;
a6b70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 .}../*.** sqlite
a6b80 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72 3_snprintf() wor
a6b90 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66 ks like snprintf
a6ba0 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69 () except that i
a6bb0 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a t ignores the.**
a6bc0 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20 current locale
a6bd0 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20 settings. This
a6be0 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 is important for
a6bf0 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 65 20 SQLite because
a6c00 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62 we.** are not ab
a6c10 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20 le to use a ","
a6c20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 as the decimal p
a6c30 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66 oint in place of
a6c40 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69 "." as.** speci
a6c50 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63 fied by some loc
a6c60 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ales..*/.SQLITE_
a6c70 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
a6c80 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e 3_snprintf(int n
a6c90 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f , char *zBuf, co
a6ca0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
a6cb0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 t, ...){. char
a6cc0 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 *z;. va_list ap
a6cd0 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 ;. StrAccum acc
a6ce0 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b ;.. if( n<=0 ){
a6cf0 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66 . return zBuf
a6d00 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
a6d10 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 trAccumInit(&acc
a6d20 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a 20 , zBuf, n, 0);.
a6d30 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d acc.useMalloc =
a6d40 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 0;. va_start(a
a6d50 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 p,zFormat);. sq
a6d60 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 lite3VXPrintf(&a
a6d70 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 cc, 0, zFormat,
a6d80 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 ap);. va_end(ap
a6d90 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a6da0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
a6db0 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a acc);. return z
a6dc0 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 ;.}..#if defined
a6dd0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f (SQLITE_DEBUG)./
a6de0 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f *.** A version o
a6df0 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74 20 f printf() that
a6e00 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c 64 understands %lld
a6e10 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 . Used for debu
a6e20 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 72 gging..** The pr
a6e30 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e 74 intf() built int
a6e40 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 o some versions
a6e50 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 of windows does
a6e60 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 25 not understand %
a6e70 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 61 lld.** and segfa
a6e80 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76 65 ults if you give
a6e90 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20 it a long long
a6ea0 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 int..*/.SQLITE_P
a6eb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a6ec0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 te3DebugPrintf(c
a6ed0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a6ee0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
a6ef0 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 ist ap;. StrAcc
a6f00 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20 7a um acc;. char z
a6f10 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c 69 Buf[500];. sqli
a6f20 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
a6f30 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a 65 &acc, zBuf, size
a6f40 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 20 of(zBuf), 0);.
a6f50 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 acc.useMalloc =
a6f60 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 0;. va_start(ap
a6f70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c ,zFormat);. sql
a6f80 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 ite3VXPrintf(&ac
a6f90 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 c, 0, zFormat, a
a6fa0 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
a6fb0 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 ;. sqlite3StrAc
a6fc0 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b cumFinish(&acc);
a6fd0 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 . fprintf(stdou
a6fe0 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 t,"%s", zBuf);.
a6ff0 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b fflush(stdout);
a7000 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
a7010 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
a7020 52 41 43 45 0a 2f 2a 0a 2a 2a 20 76 61 72 69 61 RACE./*.** varia
a7030 62 6c 65 2d 61 72 67 75 6d 65 6e 74 20 77 72 61 ble-argument wra
a7040 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 pper around sqli
a7050 74 65 33 56 58 50 72 69 6e 74 66 28 29 2e 0a 2a te3VXPrintf()..*
a7060 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
a7070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 58 50 72 void sqlite3XPr
a7080 69 6e 74 66 28 53 74 72 41 63 63 75 6d 20 2a 70 intf(StrAccum *p
a7090 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
a70a0 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 ormat, ...){. v
a70b0 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f a_list ap;. va_
a70c0 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 start(ap,zFormat
a70d0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 );. sqlite3VXPr
a70e0 69 6e 74 66 28 70 2c 20 31 2c 20 7a 46 6f 72 6d intf(p, 1, zForm
a70f0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
a7100 64 28 61 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a d(ap);.}.#endif.
a7110 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a7120 20 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 End of printf.c
a7130 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
a7140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a7160 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a7170 20 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 Begin file rand
a7180 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a om.c ***********
a7190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a71a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a71b0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
a71c0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
a71d0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
a71e0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
a71f0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
a7200 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
a7210 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
a7220 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
a7230 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
a7240 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
a7250 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
a7260 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
a7270 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
a7280 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
a7290 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
a72a0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
a72b0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
a72c0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
a72d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7320 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
a7330 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
a7340 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 de to implement
a7350 61 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 a pseudo-random
a7360 6e 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 number.** genera
a7370 74 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 tor (PRNG) for S
a7380 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e QLite..**.** Ran
a7390 64 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 dom numbers are
a73a0 75 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 used by some of
a73b0 74 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 the database bac
a73c0 6b 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a kends in order.*
a73d0 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 * to generate ra
a73e0 6e 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 ndom integer key
a73f0 73 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 s for tables or
a7400 72 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 random filenames
a7410 2e 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 ..*/.../* All th
a7420 72 65 61 64 73 20 73 68 61 72 65 20 61 20 73 69 reads share a si
a7430 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 ngle random numb
a7440 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a er generator..**
a7450 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
a7460 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 is the current s
a7470 74 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 tate of the gene
a7480 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 rator..*/.static
a7490 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 SQLITE_WSD stru
a74a0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a74b0 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 pe {. unsigned
a74c0 63 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 char isInit;
a74d0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a74e0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
a74f0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
a7500 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 i, j;
a7510 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 /* State variab
a7520 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 les */. unsigne
a7530 64 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 d char s[256];
a7540 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 /* State
a7550 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 variables */.}
a7560 73 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a sqlite3Prng;../*
a7570 0a 2a 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 .** Get a single
a7580 20 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 8-bit random va
a7590 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 lue from the RC4
a75a0 20 50 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 PRNG. The Mute
a75b0 78 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c x.** must be hel
a75c0 64 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e d while executin
a75d0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a g this routine..
a75e0 2a 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 **.** Why not ju
a75f0 73 74 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 st use a library
a7600 20 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f random generato
a7610 72 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 r like lrand48()
a7620 20 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 for this?.** Be
a7630 63 61 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 cause the OP_New
a7640 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 Rowid opcode in
a7650 74 68 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 the VDBE depends
a7660 20 6f 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 on having a ver
a7670 79 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 y.** good source
a7680 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 of random numbe
a7690 72 73 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 rs. The lrand48
a76a0 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 () library funct
a76b0 69 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 ion may.** well
a76c0 62 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 be good enough.
a76d0 20 42 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 But maybe not.
a76e0 20 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 Or maybe lrand4
a76f0 38 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 8() has some.**
a7700 73 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 subtle problems
a7710 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 on some systems
a7720 74 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 that could cause
a7730 20 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 problems. It i
a7740 73 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f s hard.** to kno
a7750 77 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 w. To minimize
a7760 74 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 the risk of prob
a7770 6c 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 lems due to bad
a7780 6c 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 lrand48().** imp
a7790 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 lementations, SQ
a77a0 4c 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72 Lite uses this r
a77b0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
a77c0 65 72 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 erator based.**
a77d0 6f 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 on RC4, which we
a77e0 20 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 know works very
a77f0 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 well..**.** (La
a7800 74 65 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c ter): Actually,
a7810 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 OP_NewRowid doe
a7820 73 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 s not depend on
a7830 61 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 a good source of
a7840 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 .** randomness a
a7850 6e 79 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 ny more. But we
a7860 20 77 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 will leave this
a7870 20 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 code in all the
a7880 20 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 same..*/.static
a7890 20 75 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 u8 randomByte(v
a78a0 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 oid){. unsigned
a78b0 20 63 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 char t;... /*
a78c0 54 68 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 The "wsdPrng" ma
a78d0 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 cro will resolve
a78e0 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 to the pseudo-r
a78f0 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
a7900 65 72 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 erator. ** stat
a7910 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 e vector. If wr
a7920 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
a7930 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 ta is unsupporte
a7940 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c d on the target,
a7950 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f . ** we have to
a7960 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 locate the stat
a7970 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d e vector at run-
a7980 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f time. In the mo
a7990 72 65 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 re common. ** c
a79a0 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 ase where writab
a79b0 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 le static data i
a79c0 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 s supported, wsd
a79d0 50 72 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 Prng can refer d
a79e0 69 72 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 irectly. ** to
a79f0 74 68 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 the "sqlite3Prng
a7a00 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 " state vector d
a7a10 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 eclared above..
a7a20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
a7a30 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 E_OMIT_WSD. str
a7a40 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 uct sqlite3PrngT
a7a50 79 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c ype *p = &GLOBAL
a7a60 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 (struct sqlite3P
a7a70 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 rngType, sqlite3
a7a80 50 72 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 Prng);.# define
a7a90 77 73 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c wsdPrng p[0].#el
a7aa0 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 se.# define wsdP
a7ab0 72 6e 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a rng sqlite3Prng.
a7ac0 23 65 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e #endif... /* In
a7ad0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 itialize the sta
a7ae0 74 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d te of the random
a7af0 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f number generato
a7b00 72 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 r once,. ** the
a7b10 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 first time this
a7b20 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
a7b30 65 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 ed. The seed va
a7b40 6c 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f lue does. ** no
a7b50 74 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 t need to contai
a7b60 6e 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f n a lot of rando
a7b70 6d 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 mness since we a
a7b80 72 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 re not. ** tryi
a7b90 6e 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 ng to do secure
a7ba0 65 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e encryption or an
a7bb0 79 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 ything like that
a7bc0 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f .... **. ** No
a7bd0 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 thing in this fi
a7be0 6c 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 le or anywhere e
a7bf0 6c 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f lse in SQLite do
a7c00 65 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 es any kind of.
a7c10 20 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 ** encryption.
a7c20 20 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 The RC4 algorit
a7c30 68 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 hm is being used
a7c40 20 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 as a PRNG (pseu
a7c50 64 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e do-random. ** n
a7c60 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 umber generator)
a7c70 20 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 not as an encry
a7c80 70 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 ption device..
a7c90 2a 2f 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e */. if( !wsdPrn
a7ca0 67 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 g.isInit ){.
a7cb0 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 int i;. char
a7cc0 6b 5b 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 k[256];. wsdP
a7cd0 72 6e 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 rng.j = 0;. w
a7ce0 73 64 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 sdPrng.i = 0;.
a7cf0 20 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f sqlite3OsRando
a7d00 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 mness(sqlite3_vf
a7d10 73 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 s_find(0), 256,
a7d20 6b 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b k);. for(i=0;
a7d30 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 i<256; i++){.
a7d40 20 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d wsdPrng.s[i]
a7d50 20 3d 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a = (u8)i;. }.
a7d60 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 for(i=0; i<2
a7d70 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56; i++){.
a7d80 77 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 wsdPrng.j += wsd
a7d90 50 72 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d Prng.s[i] + k[i]
a7da0 3b 0a 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 ;. t = wsdP
a7db0 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d rng.s[wsdPrng.j]
a7dc0 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e ;. wsdPrng.
a7dd0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 s[wsdPrng.j] = w
a7de0 73 64 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 sdPrng.s[i];.
a7df0 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 wsdPrng.s[i]
a7e00 3d 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 = t;. }. w
a7e10 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 sdPrng.isInit =
a7e20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 1;. }.. /* Gen
a7e30 65 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e erate and return
a7e40 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 single random b
a7e50 79 74 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 yte. */. wsdPr
a7e60 6e 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 ng.i++;. t = ws
a7e70 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
a7e80 69 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 i];. wsdPrng.j
a7e90 2b 3d 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e += t;. wsdPrng.
a7ea0 73 5b 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 s[wsdPrng.i] = w
a7eb0 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 sdPrng.s[wsdPrng
a7ec0 2e 6a 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 .j];. wsdPrng.s
a7ed0 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b [wsdPrng.j] = t;
a7ee0 0a 20 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e . t += wsdPrng.
a7ef0 73 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 s[wsdPrng.i];.
a7f00 72 65 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 return wsdPrng.s
a7f10 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 [t];.}../*.** Re
a7f20 74 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 turn N random by
a7f30 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 tes..*/.SQLITE_A
a7f40 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
a7f50 72 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e randomness(int N
a7f60 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 , void *pBuf){.
a7f70 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
a7f80 7a 42 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 zBuf = pBuf;.#if
a7f90 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
a7fa0 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 FE. sqlite3_mut
a7fb0 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 ex *mutex = sqli
a7fc0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
a7fd0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
a7fe0 43 5f 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a C_PRNG);.#endif.
a7ff0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
a8000 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 enter(mutex);.
a8010 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 while( N-- ){.
a8020 20 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 *(zBuf++) = ra
a8030 6e 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a ndomByte();. }.
a8040 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
a8050 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a leave(mutex);.}.
a8060 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a8070 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
a8080 54 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 T./*.** For test
a8090 69 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 ing purposes, we
a80a0 20 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 sometimes want
a80b0 74 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20 to preserve the
a80c0 73 74 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 state of.** PRNG
a80d0 20 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 and restore the
a80e0 20 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 PRNG to its sav
a80f0 65 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 ed state at a la
a8100 74 65 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 ter time, or.**
a8110 74 6f 20 72 65 73 65 74 20 74 68 65 20 50 52 4e to reset the PRN
a8120 47 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c G to its initial
a8130 20 73 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 state. These r
a8140 6f 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 outines accompli
a8150 73 68 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b sh.** those task
a8160 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c s..**.** The sql
a8170 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f ite3_test_contro
a8180 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 l() interface ca
a8190 6c 6c 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e lls these routin
a81a0 65 73 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c es to.** control
a81b0 20 74 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 the PRNG..*/.st
a81c0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
a81d0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
a81e0 6e 67 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 ngType sqlite3Sa
a81f0 76 65 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f vedPrng;.SQLITE_
a8200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
a8210 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 ite3PrngSaveStat
a8220 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 e(void){. memcp
a8230 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 y(. &GLOBAL(s
a8240 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a8250 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 gType, sqlite3Sa
a8260 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 vedPrng),. &G
a8270 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c LOBAL(struct sql
a8280 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 ite3PrngType, sq
a8290 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 lite3Prng),.
a82a0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 sizeof(sqlite3Pr
a82b0 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 ng). );.}.SQLIT
a82c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a82d0 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 qlite3PrngRestor
a82e0 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 eState(void){.
a82f0 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f memcpy(. &GLO
a8300 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 BAL(struct sqlit
a8310 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 e3PrngType, sqli
a8320 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 te3Prng),. &G
a8330 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c LOBAL(struct sql
a8340 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 ite3PrngType, sq
a8350 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c lite3SavedPrng),
a8360 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 . sizeof(sqli
a8370 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a te3Prng). );.}.
a8380 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
a8390 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 oid sqlite3PrngR
a83a0 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b esetState(void){
a83b0 0a 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 . GLOBAL(struct
a83c0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 sqlite3PrngType
a83d0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 , sqlite3Prng).i
a83e0 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e sInit = 0;.}.#en
a83f0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
a8400 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 IT_BUILTIN_TEST
a8410 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
a8420 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f *** End of rando
a8430 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
a8440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8460 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
a8470 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 *** Begin file u
a8480 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tf.c ***********
a8490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a84a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a84b0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 **/./*.** 2004 A
a84c0 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 pril 13.**.** Th
a84d0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
a84e0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
a84f0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
a8500 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
a8510 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
a8520 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
a8530 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
a8540 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
a8550 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
a8560 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
a8570 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
a8580 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
a8590 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
a85a0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
a85b0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
a85c0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
a85d0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
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 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8620 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
a8630 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 ile contains rou
a8640 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 tines used to tr
a8650 61 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 anslate between
a8660 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 UTF-8, .** UTF-1
a8670 36 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 6, UTF-16BE, and
a8680 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a UTF-16LE..**.**
a8690 20 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a Notes on UTF-8:
a86a0 0a 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 .**.** Byte-0
a86b0 20 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 Byte-1 Byt
a86c0 65 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 e-2 Byte-3
a86d0 20 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 Value.** 0xxxx
a86e0 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 xxx
a86f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8700 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 00000000 000
a8710 30 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 00000 0xxxxxxx.*
a8720 2a 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 * 110yyyyy 10x
a8730 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 xxxxx
a8740 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 0000
a8750 30 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 0000 00000yyy yy
a8760 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a xxxxxx.** 1110z
a8770 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 zzz 10yyyyyy 1
a8780 30 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 0xxxxxx
a8790 20 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 00000000 zzz
a87a0 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a zyyyy yyxxxxxx.*
a87b0 2a 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 * 11110uuu 10u
a87c0 75 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 uzzzz 10yyyyyy
a87d0 20 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 10xxxxxx 000u
a87e0 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 uuuu zzzzyyyy yy
a87f0 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 xxxxxx.**.**.**
a8800 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a Notes on UTF-16:
a8810 20 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d (with wwww+1==
a8820 75 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 uuuuu).**.**
a8830 20 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 Word-0
a8840 20 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 Word-1
a8850 20 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 Value.**
a8860 20 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 110110ww wwzzzz
a8870 79 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 yy 110111yy yy
a8880 78 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 xxxxxx 000uuu
a8890 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 uu zzzzyyyy yyxx
a88a0 78 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 xxxx.** zzzzyyy
a88b0 79 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 y yyxxxxxx
a88c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a88d0 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 00000000 zzzzy
a88e0 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a yyy yyxxxxxx.**.
a88f0 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 **.** BOM or Byt
a8900 65 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a e Order Mark:.**
a8910 20 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 0xff 0xfe
a8920 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 little-endian u
a8930 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a tf-16 follows.**
a8940 20 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 0xfe 0xff
a8950 20 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d big-endian utf-
a8960 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 16 follows.**.*/
a8970 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a8980 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 Include vdbeInt
a8990 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
a89a0 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a of utf.c ******
a89b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a89c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
a89d0 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 Begin file vdbe
a89e0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
a89f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
a8a10 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 ./*.** 2003 Sept
a8a20 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 ember 6.**.** Th
a8a30 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
a8a40 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
a8a50 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
a8a60 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
a8a70 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
a8a80 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
a8a90 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
a8aa0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
a8ab0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
a8ac0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
a8ad0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
a8ae0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
a8af0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
a8b00 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
a8b10 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
a8b20 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
a8b30 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
a8b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8b80 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 ******.** This i
a8b90 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c s the header fil
a8ba0 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f e for informatio
a8bb0 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 n that is privat
a8bc0 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 e to the.** VDBE
a8bd0 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 . This informat
a8be0 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 ion used to all
a8bf0 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 be at the top of
a8c00 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 the single.** s
a8c10 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 ource code file
a8c20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 "vdbe.c". When
a8c30 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 that file became
a8c40 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a too big (over.*
a8c50 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e * 6000 lines lon
a8c60 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 g) it was split
a8c70 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 up into several
a8c80 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e smaller files an
a8c90 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 d.** this header
a8ca0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 information was
a8cb0 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a factored out..*
a8cc0 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 /.#ifndef _VDBEI
a8cd0 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 NT_H_.#define _V
a8ce0 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a DBEINT_H_../*.**
a8cf0 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 SQL is translat
a8d00 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e ed into a sequen
a8d10 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f ce of instructio
a8d20 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 ns to be.** exec
a8d30 75 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 uted by a virtua
a8d40 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 l machine. Each
a8d50 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
a8d60 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f an instance.** o
a8d70 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
a8d80 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 structure..*/.ty
a8d90 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
a8da0 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 eOp Op;../*.** B
a8db0 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f oolean values.*/
a8dc0 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 .typedef unsigne
a8dd0 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a d char Bool;../*
a8de0 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 .** A cursor is
a8df0 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 a pointer into a
a8e00 20 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 single BTree wi
a8e10 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 thin a database
a8e20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 file..** The cur
a8e30 73 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 sor can seek to
a8e40 61 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 a BTree entry wi
a8e50 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 th a particular
a8e60 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 key, or.** loop
a8e70 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 over all entries
a8e80 20 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 of the Btree.
a8e90 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 You can also ins
a8ea0 65 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a ert new BTree.**
a8eb0 20 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 entries or retr
a8ec0 69 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 ieve the key or
a8ed0 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e data from the en
a8ee0 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 try that the cur
a8ef0 73 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e sor.** is curren
a8f00 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e tly pointing to.
a8f10 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 .** .** Every cu
a8f20 72 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 rsor that the vi
a8f30 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 rtual machine ha
a8f40 73 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 s open is repres
a8f50 65 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 ented by an.** i
a8f60 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
a8f70 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
a8f80 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 re..**.** If the
a8f90 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 VdbeCursor.isTr
a8fa0 69 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 iggerRow flag is
a8fb0 20 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 set it means th
a8fc0 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 at this cursor i
a8fd0 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 s.** really a si
a8fe0 6e 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 ngle row that re
a8ff0 70 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 presents the NEW
a9000 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 or OLD pseudo-t
a9010 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 able of.** a row
a9020 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 trigger. The d
a9030 61 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 ata for the row
a9040 69 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 is stored in Vdb
a9050 65 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e eCursor.pData an
a9060 64 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 d.** the rowid i
a9070 73 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e s in VdbeCursor.
a9080 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 iKey..*/.struct
a9090 56 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 VdbeCursor {. B
a90a0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 tCursor *pCursor
a90b0 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 ; /* The curs
a90c0 6f 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 or structure of
a90d0 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 the backend */.
a90e0 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
a90f0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
a9100 6f 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 of cursor databa
a9110 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 se in db->aDb[]
a9120 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 (or -1) */. i64
a9130 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 lastRowid;
a9140 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 /* Last rowid
a9150 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 from a Next or
a9160 4e 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f NextIdx operatio
a9170 6e 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f n */. Bool zero
a9180 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ed; /*
a9190 54 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f True if zeroed o
a91a0 75 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 ut and ready for
a91b0 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c reuse */. Bool
a91c0 20 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 rowidIsValid;
a91d0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 /* True if las
a91e0 74 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 tRowid is valid
a91f0 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 */. Bool atFirs
a9200 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 t; /* Tr
a9210 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 ue if pointing t
a9220 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f o first entry */
a9230 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f . Bool useRando
a9240 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 mRowid; /* Gene
a9250 72 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 rate new record
a9260 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e numbers semi-ran
a9270 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 domly */. Bool
a9280 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 nullRow;
a9290 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e /* True if poin
a92a0 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 ting to a row wi
a92b0 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 th no data */.
a92c0 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 Bool deferredMov
a92d0 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 eto; /* A call
a92e0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d to sqlite3BtreeM
a92f0 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 oveto() is neede
a9300 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 d */. Bool isTa
a9310 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ble; /*
a9320 54 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 True if a table
a9330 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 requiring intege
a9340 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c r keys */. Bool
a9350 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 isIndex;
a9360 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 /* True if an
a9370 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 index containing
a9380 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 keys only - no
a9390 64 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f data */. i64 mo
a93a0 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 vetoTarget;
a93b0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 /* Argument to t
a93c0 68 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 he deferred sqli
a93d0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 te3BtreeMoveto()
a93e0 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 */. Btree *pBt
a93f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 ; /* S
a9400 65 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c eparate file hol
a9410 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 ding temporary t
a9420 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73 able */. int ps
a9430 65 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 eudoTableReg;
a9440 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
a9450 69 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 ing pseudotable
a9460 63 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65 content. */. Ke
a9470 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
a9480 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 /* Info abou
a9490 74 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 t index keys nee
a94a0 64 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 ded by index cur
a94b0 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 sors */. int nF
a94c0 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 ield;
a94d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 /* Number of fie
a94e0 6c 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 lds in the heade
a94f0 72 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f r */. i64 seqCo
a9500 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 unt; /*
a9510 53 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 Sequence counter
a9520 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 */. sqlite3_vt
a9530 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 ab_cursor *pVtab
a9540 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 Cursor; /* The
a9550 63 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 cursor for a vir
a9560 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 tual table */.
a9570 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
a9580 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 dule *pModule;
a9590 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 /* Module for
a95a0 20 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 cursor pVtabCur
a95b0 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 sor */.. /* Res
a95c0 75 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69 ult of last sqli
a95d0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 te3BtreeMoveto()
a95e0 20 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e done by an OP_N
a95f0 6f 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a otExists or . *
a9600 2a 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 * OP_IsUnique op
a9610 63 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72 code on this cur
a9620 73 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 sor. */. int se
a9630 65 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 ekResult;.. /*
a9640 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69 Cached informati
a9650 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61 on about the hea
a9660 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 der for the data
a9670 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65 record that the
a9680 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 . ** cursor is
a9690 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
a96a0 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c ng to. Only val
a96b0 69 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75 id if cacheStatu
a96c0 73 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56 s matches. ** V
a96d0 64 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56 dbe.cacheCtr. V
a96e0 64 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c dbe.cacheCtr wil
a96f0 6c 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20 l never take on
a9700 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a the value of. *
a9710 2a 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e * CACHE_STALE an
a9720 64 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63 d so setting cac
a9730 68 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53 heStatus=CACHE_S
a9740 54 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20 TALE guarantees
a9750 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61 that. ** the ca
a9760 63 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61 che is out of da
a9770 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52 te.. **. ** aR
a9780 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 ow might point t
a9790 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61 o (ephemeral) da
a97a0 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 ta for the curre
a97b0 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69 nt row, or it mi
a97c0 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c ght. ** be NULL
a97d0 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 .. */. u32 cac
a97e0 68 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f heStatus; /
a97f0 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64 * Cache is valid
a9800 20 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73 if this matches
a9810 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a Vdbe.cacheCtr *
a9820 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 /. int payloadS
a9830 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 ize; /* Tot
a9840 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
a9850 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 es in the record
a9860 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 */. u32 *aType
a9870 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
a9880 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 ype values for a
a9890 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 ll entries in th
a98a0 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 e record */. u3
a98b0 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 2 *aOffset;
a98c0 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66 /* Cached of
a98d0 66 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61 fsets to the sta
a98e0 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d rt of each colum
a98f0 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 ns data */. u8
a9900 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 *aRow;
a9910 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 /* Data for t
a9920 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 he current row,
a9930 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 if all on one pa
a9940 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 ge */.};.typedef
a9950 20 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 struct VdbeCurs
a9960 6f 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a or VdbeCursor;..
a9970 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62 /*.** When a sub
a9980 2d 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63 -program is exec
a9990 75 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d uted (OP_Program
a99a0 29 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f ), a structure o
a99b0 66 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69 f this type.** i
a99c0 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 s allocated to s
a99d0 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 tore the current
a99e0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72 value of the pr
a99f0 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61 ogram counter, a
a9a00 73 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65 s.** well as the
a9a10 20 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20 current memory
a9a20 63 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76 cell array and v
a9a30 61 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61 arious other fra
a9a40 6d 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76 me specific.** v
a9a50 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 alues stored in
a9a60 74 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e the Vdbe struct.
a9a70 20 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 When the sub-pr
a9a80 6f 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65 ogram is finishe
a9a90 64 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c d, .** these val
a9aa0 75 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62 ues are copied b
a9ab0 61 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20 ack to the Vdbe
a9ac0 66 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61 from the VdbeFra
a9ad0 6d 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a me structure,.**
a9ae0 20 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73 restoring the s
a9af0 74 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74 tate of the VM t
a9b00 6f 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f o as it was befo
a9b10 72 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 re the sub-progr
a9b20 61 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63 am.** began exec
a9b30 75 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61 uting..**.** Fra
a9b40 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 mes are stored i
a9b50 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
a9b60 68 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70 headed at Vdbe.p
a9b70 50 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61 Parent. Vdbe.pPa
a9b80 72 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 rent.** is the p
a9b90 61 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72 arent of the cur
a9ba0 72 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a rent frame, or z
a9bb0 65 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65 ero if the curre
a9bc0 6e 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74 nt frame.** is t
a9bd0 68 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f he main Vdbe pro
a9be0 67 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 gram..*/.typedef
a9bf0 20 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d struct VdbeFram
a9c00 65 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72 e VdbeFrame;.str
a9c10 75 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a uct VdbeFrame {.
a9c20 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 Vdbe *v;
a9c30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 /* VM
a9c40 74 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e this frame belon
a9c50 67 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70 gs to */. int p
a9c60 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
a9c70 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f /* Program Co
a9c80 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61 unter */. Op *a
a9c90 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
a9ca0 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e /* Program in
a9cb0 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 structions */.
a9cc0 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 int nOp;
a9cd0 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
a9ce0 6f 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a of aOp array */.
a9cf0 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 Mem *aMem;
a9d00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
a9d10 61 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c ay of memory cel
a9d20 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d ls */. int nMem
a9d30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a9d40 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
a9d50 72 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a ries in aMem */.
a9d60 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 VdbeCursor **a
a9d70 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65 pCsr; /* Ele
a9d80 6d 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72 ment of Vdbe cur
a9d90 73 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43 sors */. u16 nC
a9da0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 ursor;
a9db0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
a9dc0 6e 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20 ntries in apCsr
a9dd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e */. void *token
a9de0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a9df0 43 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72 Copy of SubProgr
a9e00 61 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e am.token */. in
a9e10 74 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 t nChildMem;
a9e20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a9e30 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 of memory cells
a9e40 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 for child frame
a9e50 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 */. int nChildC
a9e60 73 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sr; /*
a9e70 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 Number of cursor
a9e80 73 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d s for child fram
a9e90 65 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 e */. i64 lastR
a9ea0 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f owid; /
a9eb0 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f * Last insert ro
a9ec0 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 wid (sqlite3.las
a9ed0 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 tRowid) */. int
a9ee0 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 nChange;
a9ef0 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e /* Statemen
a9f00 74 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e t changes (Vdbe.
a9f10 6e 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f nChanges) */
a9f20 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 . VdbeFrame *pP
a9f30 61 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 arent; /* Pa
a9f40 72 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 rent of this fra
a9f50 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e me */.};..#defin
a9f60 65 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 e VdbeFrameMem(p
a9f70 29 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20 ) ((Mem *)&((u8
a9f80 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 *)p)[ROUND8(size
a9f90 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29 of(VdbeFrame))])
a9fa0 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 ../*.** A value
a9fb0 66 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 for VdbeCursor.c
a9fc0 61 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d acheValid that m
a9fd0 65 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 eans the cache i
a9fe0 73 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 s always invalid
a9ff0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 ..*/.#define CAC
aa000 48 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a HE_STALE 0../*.*
aa010 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 * Internally, th
aa020 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 e vdbe manipulat
aa030 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 es nearly all SQ
aa040 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a L values as Mem.
aa050 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 ** structures. E
aa060 61 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d ach Mem struct m
aa070 61 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c ay cache multipl
aa080 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e e representation
aa090 73 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e s (string,.** in
aa0a0 74 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 teger etc.) of t
aa0b0 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 he same value.
aa0c0 41 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 A value (and the
aa0d0 72 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 refore Mem struc
aa0e0 74 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 ture).** has the
aa0f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 following prope
aa100 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 rties:.**.** Eac
aa110 68 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 h value has a ma
aa120 6e 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 nifest type. The
aa130 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f manifest type o
aa140 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 f the value stor
aa150 65 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 ed.** in a Mem s
aa160 74 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 truct is returne
aa170 64 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 d by the MemType
aa180 28 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 (Mem*) macro. Th
aa190 65 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 e type is.** one
aa1a0 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c of SQLITE_NULL,
aa1b0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c SQLITE_INTEGER,
aa1c0 20 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 SQLITE_REAL, SQ
aa1d0 4c 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 LITE_TEXT or.**
aa1e0 53 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a SQLITE_BLOB..*/.
aa1f0 73 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 struct Mem {. u
aa200 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 nion {. i64 i
aa210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
aa220 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e * Integer value.
aa230 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 */. int nZer
aa240 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 o; /* U
aa250 73 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d sed when bit MEM
aa260 5f 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 _Zero is set in
aa270 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e flags */. Fun
aa280 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 cDef *pDef;
aa290 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 /* Used only wh
aa2a0 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 en flags==MEM_Ag
aa2b0 67 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 g */. RowSet
aa2c0 2a 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 *pRowSet; /*
aa2d0 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 Used only when f
aa2e0 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 lags==MEM_RowSet
aa2f0 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d */. VdbeFram
aa300 65 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 e *pFrame; /* U
aa310 73 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d sed when flags==
aa320 4d 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d MEM_Frame */. }
aa330 20 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 u;. double r;
aa340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 /* Rea
aa350 6c 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c l value */. sql
aa360 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
aa370 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 /* The associat
aa380 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ed database conn
aa390 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 ection */. char
aa3a0 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 *z;
aa3b0 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f /* String or BLO
aa3c0 42 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 B value */. int
aa3d0 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
aa3e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 /* Number of ch
aa3f0 61 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 aracters in stri
aa400 6e 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 ng value, exclud
aa410 69 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 ing '\0' */. u1
aa420 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 6 flags;
aa430 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e /* Some combin
aa440 61 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c ation of MEM_Nul
aa450 6c 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f l, MEM_Str, MEM_
aa460 44 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 Dyn, etc. */. u
aa470 38 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 8 type;
aa480 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c /* One of SQL
aa490 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 ITE_NULL, SQLITE
aa4a0 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e _TEXT, SQLITE_IN
aa4b0 54 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 TEGER, etc */.
aa4c0 75 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 u8 enc;
aa4d0 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 /* SQLITE_UT
aa4e0 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 F8, SQLITE_UTF16
aa4f0 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 BE, SQLITE_UTF16
aa500 4c 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 LE */. void (*x
aa510 44 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f Del)(void *); /
aa520 2a 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 * If not null, c
aa530 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
aa540 6e 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e n to delete Mem.
aa550 7a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 z */. char *zMa
aa560 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 lloc; /* Dy
aa570 6e 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c namic buffer all
aa580 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 ocated by sqlite
aa590 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 3_malloc() */.};
aa5a0 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 ../* One or more
aa5b0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
aa5c0 67 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 g flags are set
aa5d0 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 to indicate the
aa5e0 76 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 validOK.** repre
aa5f0 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 sentations of th
aa600 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
aa610 6e 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 n the Mem struct
aa620 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d ..**.** If the M
aa630 45 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 EM_Null flag is
aa640 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 set, then the va
aa650 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 lue is an SQL NU
aa660 4c 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 LL value..** No
aa670 6f 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 other flags may
aa680 62 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 be set in this c
aa690 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
aa6a0 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 e MEM_Str flag i
aa6b0 73 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a s set then Mem.z
aa6c0 20 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 points at a str
aa6d0 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
aa6e0 6f 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 on..** Usually t
aa6f0 68 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 his is encoded i
aa700 6e 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f n the same unico
aa710 64 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 de encoding as t
aa720 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 he main.** datab
aa730 61 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 ase (see below f
aa740 6f 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 or exceptions).
aa750 49 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 If the MEM_Term
aa760 66 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 flag is also.**
aa770 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 set, then the st
aa780 72 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d ring is nul term
aa790 69 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f inated. The MEM_
aa7a0 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c Int and MEM_Real
aa7b0 20 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 .** flags may c
aa7c0 6f 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 oexist with the
aa7d0 4d 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a MEM_Str flag..**
aa7e0 0a 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 .** Multiple of
aa7f0 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e these values can
aa800 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 appear in Mem.f
aa810 6c 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 lags. But only
aa820 6f 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 one.** at a time
aa830 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d can appear in M
aa840 65 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 em.type..*/.#def
aa850 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 ine MEM_Null
aa860 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 0x0001 /* Va
aa870 6c 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 lue is NULL */.#
aa880 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 define MEM_Str
aa890 20 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 0x0002 /*
aa8a0 20 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 Value is a stri
aa8b0 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 ng */.#define ME
aa8c0 4d 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 M_Int 0x00
aa8d0 30 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 04 /* Value is
aa8e0 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 an integer */.#
aa8f0 64 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 define MEM_Real
aa900 20 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 0x0008 /*
aa910 20 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c Value is a real
aa920 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 number */.#defi
aa930 6e 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 ne MEM_Blob
aa940 20 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 0x0010 /* Val
aa950 75 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a ue is a BLOB */.
aa960 23 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 #define MEM_RowS
aa970 65 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f et 0x0020 /
aa980 2a 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 * Value is a Row
aa990 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 Set object */.#d
aa9a0 65 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 efine MEM_Frame
aa9b0 20 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 0x0040 /*
aa9c0 56 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 Value is a VdbeF
aa9d0 72 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 rame object */.#
aa9e0 64 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d define MEM_TypeM
aa9f0 61 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a ask 0x00ff /*
aaa00 20 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 Mask of type bi
aaa10 74 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 ts */../* Whenev
aaa20 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 er Mem contains
aaa30 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f a valid string o
aaa40 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 r blob represent
aaa50 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a ation, one of.**
aaa60 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
aaa70 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 lags must be set
aaa80 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
aaa90 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d e memory managem
aaaa0 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f ent.** policy fo
aaab0 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 r Mem.z. The ME
aaac0 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c M_Term flag tell
aaad0 73 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 s us whether or
aaae0 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e not the.** strin
aaaf0 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 g is \000 or \u0
aab00 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 000 terminated.*
aab10 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 /.#define MEM_Te
aab20 72 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 rm 0x0200
aab30 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 /* String rep i
aab40 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 s nul terminated
aab50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aab60 44 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 Dyn 0x0400
aab70 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 /* Need to ca
aab80 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 ll sqliteFree()
aab90 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 on Mem.z */.#def
aaba0 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 ine MEM_Static
aabb0 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 0x0800 /* Me
aabc0 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 m.z points to a
aabd0 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f static string */
aabe0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 .#define MEM_Eph
aabf0 65 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 em 0x1000
aac00 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 /* Mem.z points
aac10 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 to an ephemeral
aac20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
aac30 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 e MEM_Agg
aac40 30 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 0x2000 /* Mem.
aac50 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 z points to an a
aac60 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 gg function cont
aac70 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ext */.#define M
aac80 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 EM_Zero 0x4
aac90 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 000 /* Mem.i c
aaca0 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 ontains count of
aacb0 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 0s appended to
aacc0 62 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 blob */..#ifdef
aacd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
aace0 42 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 BLOB. #undef ME
aacf0 4d 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 M_Zero. #define
aad00 20 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 MEM_Zero 0x0000
aad10 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
aad20 43 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 Clear any existi
aad30 6e 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 ng type flags fr
aad40 6f 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 om a Mem and rep
aad50 6c 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 lace them with f
aad60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 .*/.#define MemS
aad70 65 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 etTypeFlag(p, f)
aad80 20 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 \. ((p)->flag
aad90 73 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 s = ((p)->flags&
aada0 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d ~(MEM_TypeMask|M
aadb0 45 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f EM_Zero))|f).../
aadc0 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 * A VdbeFunc is
aadd0 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 just a FuncDef (
aade0 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 defined in sqlit
aadf0 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e eInt.h) that con
aae00 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f tains.** additio
aae10 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 nal information
aae20 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 about auxiliary
aae30 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e information boun
aae40 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a d to arguments.*
aae50 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f * of the functio
aae60 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 n. This is used
aae70 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
aae80 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 e sqlite3_get_au
aae90 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 xdata().** and s
aaea0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
aaeb0 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 ta() APIs. The
aaec0 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d "auxdata" is som
aaed0 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 e auxiliary data
aaee0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 .** that can be
aaef0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
aaf00 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d a constant argum
aaf10 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ent to a functio
aaf20 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f n. This.** allo
aaf30 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 ws functions suc
aaf40 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f h as "regexp" to
aaf50 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 compile their c
aaf60 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a onstant regular.
aaf70 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 ** expression ar
aaf80 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 gument once and
aaf90 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 reused the compi
aafa0 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c led code for mul
aafb0 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 tiple.** invocat
aafc0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ions..*/.struct
aafd0 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e VdbeFunc {. Fun
aafe0 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 cDef *pFunc;
aaff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
ab000 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 e definition of
ab010 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
ab020 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 int nAux;
ab030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab040 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
ab050 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 ries allocated f
ab060 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 or apAux[] */.
ab070 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b struct AuxData {
ab080 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b . void *pAux;
ab090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab0a0 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 /* Aux data f
ab0b0 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 or the i-th argu
ab0c0 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 ment */. void
ab0d0 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 (*xDelete)(void
ab0e0 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 *); /* Des
ab0f0 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 tructor for the
ab100 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 aux data */. }
ab110 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 apAux[1];
ab120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
ab130 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 ne slot for each
ab140 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
ab150 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.};../*.**
ab160 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 The "context" ar
ab170 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 gument for a ins
ab180 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f tallable functio
ab190 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f n. A pointer to
ab1a0 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
ab1b0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
ab1c0 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 e is the first a
ab1d0 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 rgument to the r
ab1e0 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 outines used.**
ab1f0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 implement the SQ
ab200 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a L functions..**.
ab210 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 ** There is a ty
ab220 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 pedef for this s
ab230 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 tructure in sqli
ab240 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f te.h. So all ro
ab250 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 utines,.** even
ab260 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 the public inter
ab270 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 face to SQLite,
ab280 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 can use a pointe
ab290 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 r to this struct
ab2a0 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 ure..** But this
ab2b0 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c file is the onl
ab2c0 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 y place where th
ab2d0 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 e internal detai
ab2e0 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 ls of this.** st
ab2f0 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 ructure are know
ab300 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 n..**.** This st
ab310 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e ructure is defin
ab320 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 ed inside of vdb
ab330 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 eInt.h because i
ab340 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 t uses substruct
ab350 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 ures.** (Mem) wh
ab360 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 ich are only def
ab370 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 ined there..*/.s
ab380 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f truct sqlite3_co
ab390 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 ntext {. FuncDe
ab3a0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
ab3b0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 /* Pointer to fu
ab3c0 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 nction informati
ab3d0 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 on. MUST BE FIR
ab3e0 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 ST */. VdbeFunc
ab3f0 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a *pVdbeFunc; /*
ab400 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 Auxilary data,
ab410 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 if created. */.
ab420 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 Mem s;
ab430 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
ab440 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 turn value is st
ab450 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d ored here */. M
ab460 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 em *pMem;
ab470 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
ab480 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 ell used to stor
ab490 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 e aggregate cont
ab4a0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 ext */. int isE
ab4b0 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f rror; /
ab4c0 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 * Error code ret
ab4d0 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e urned by the fun
ab4e0 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c ction. */. Coll
ab4f0 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 Seq *pColl;
ab500 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 /* Collating s
ab510 65 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f equence */.};../
ab520 2a 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 *.** A Set struc
ab530 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 ture is used for
ab540 20 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 quick testing t
ab550 6f 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 o see if a value
ab560 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 .** is part of a
ab570 20 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 small set. Set
ab580 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d s are used to im
ab590 70 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b plement code lik
ab5a0 65 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 e.** this:.**
ab5b0 20 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 x.y IN
ab5c0 28 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d ('hi','hoo','hum
ab5d0 27 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 ').*/.typedef st
ab5e0 72 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 ruct Set Set;.st
ab5f0 72 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 ruct Set {. Has
ab600 68 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 h hash;
ab610 20 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 /* A set is
ab620 6a 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c just a hash tabl
ab630 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
ab640 2a 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a *prev; /*
ab650 20 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 Previously acce
ab660 73 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e ssed hash elemen
ab670 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
ab680 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
ab690 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
ab6a0 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 . This structur
ab6b0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 e contains the c
ab6c0 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 omplete.** state
ab6d0 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 of the virtual
ab6e0 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 machine..**.** T
ab6f0 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 he "sqlite3_stmt
ab700 22 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e " structure poin
ab710 74 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75 ter that is retu
ab720 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
ab730 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 compile().** is
ab740 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 really a pointer
ab750 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 to an instance
ab760 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
ab770 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 e..**.** The Vdb
ab780 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 e.inVtabMethod v
ab790 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
ab7a0 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 o non-zero for t
ab7b0 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a he duration of.*
ab7c0 2a 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 * any virtual ta
ab7d0 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 ble method invoc
ab7e0 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 ations made by t
ab7f0 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e he vdbe program.
ab800 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f It is.** set to
ab810 20 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 2 for xDestroy
ab820 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 method calls and
ab830 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 1 for all other
ab840 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a methods. This.*
ab850 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 * variable is us
ab860 65 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f ed for two purpo
ab870 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 ses: to allow xD
ab880 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 estroy methods t
ab890 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 o execute.** "DR
ab8a0 4f 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d OP TABLE" statem
ab8b0 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 ents and to prev
ab8c0 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 ent some nasty s
ab8d0 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a ide effects of.*
ab8e0 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
ab8f0 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 when SQLite is
ab900 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 invoked recursiv
ab910 65 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c ely by a virtual
ab920 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f table .** metho
ab930 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 d function..*/.s
ab940 74 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 truct Vdbe {. s
ab950 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
ab960 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
ab970 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
ab980 6e 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 n that owns this
ab990 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
ab9a0 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65 Vdbe *pPrev,*pNe
ab9b0 78 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 xt; /* Linke
ab9c0 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 d list of VDBEs
ab9d0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 with the same Vd
ab9e0 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e be.db */. int n
ab9f0 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
aba00 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
aba10 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 instructions in
aba20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 the program */.
aba30 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 int nOpAlloc;
aba40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
aba50 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
aba60 63 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 cated for aOp[]
aba70 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 */. Op *aOp;
aba80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aba90 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
abaa0 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
abab0 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 e's program */.
abac0 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 int nLabel;
abad0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
abae0 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 er of labels use
abaf0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 d */. int nLabe
abb00 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f lAlloc; /
abb10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
abb20 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 s allocated in a
abb30 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 Label[] */. int
abb40 20 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 *aLabel;
abb50 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f /* Space to
abb60 20 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 hold the labels
abb70 20 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 */. Mem **apAr
abb80 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a g; /*
abb90 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 Arguments to cu
abba0 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e rrently executin
abbb0 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 g user function
abbc0 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 */. Mem *aColNa
abbd0 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 me; /*
abbe0 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 Column names to
abbf0 72 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 return */. Mem
abc00 2a 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 *pResultSet;
abc10 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
abc20 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 o an array of re
abc30 73 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e sults */. u16 n
abc40 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 ResColumn;
abc50 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
abc60 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 columns in one r
abc70 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ow of the result
abc80 20 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43 set */. u16 nC
abc90 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 ursor;
abca0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
abcb0 6c 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 lots in apCsr[]
abcc0 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
abcd0 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 **apCsr; /*
abce0 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 One element of t
abcf0 68 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 his array for ea
abd00 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a ch open cursor *
abd10 2f 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69 /. u8 errorActi
abd20 6f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 on; /* R
abd30 65 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74 ecovery action t
abd40 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 o do in case of
abd50 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 an error */. u8
abd60 20 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 okVar;
abd70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
abd80 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 azVar[] has bee
abd90 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f n initialized */
abda0 0a 20 20 79 6e 56 61 72 20 6e 56 61 72 3b 20 20 . ynVar nVar;
abdb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
abdc0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
abdd0 69 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d in aVar[] */. M
abde0 65 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 em *aVar;
abdf0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 /* Values
abe00 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 for the OP_Vari
abe10 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a able opcode. */.
abe20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 char **azVar;
abe30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
abe40 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a e of variables *
abe50 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 /. u32 magic;
abe60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
abe70 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 agic number for
abe80 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 sanity checking
abe90 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 */. int nMem;
abea0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
abeb0 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 Number of memory
abec0 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 locations curre
abed0 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a ntly allocated *
abee0 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 /. Mem *aMem;
abef0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
abf00 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 he memory locati
abf10 6f 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 ons */. u32 cac
abf20 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 heCtr;
abf30 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 /* VdbeCursor r
abf40 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 ow cache generat
abf50 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 ion counter */.
abf60 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 int pc;
abf70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
abf80 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 program counter
abf90 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
abfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
abfb0 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 Value to return
abfc0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d */. char *zErrM
abfd0 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sg; /*
abfe0 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 Error message wr
abff0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 itten here */.
ac000 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 u8 explain;
ac010 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
ac020 69 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 if EXPLAIN prese
ac030 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e nt on SQL comman
ac040 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 d */. u8 change
ac050 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f CntOn; /
ac060 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 * True to update
ac070 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
ac080 74 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 ter */. u8 expi
ac090 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 red;
ac0a0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
ac0b0 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 VM needs to be r
ac0c0 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 ecompiled */. u
ac0d0 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 8 minWriteFileFo
ac0e0 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 rmat; /* Minimu
ac0f0 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f m file format fo
ac100 72 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 r writable datab
ac110 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 ase files */. u
ac120 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 8 inVtabMethod;
ac130 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f /* See co
ac140 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a mments above */.
ac150 20 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 u8 usesStmtJou
ac160 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 rnal; /* Tru
ac170 65 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 e if uses a stat
ac180 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f ement journal */
ac190 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 . u8 readOnly;
ac1a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
ac1b0 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 ue for read-only
ac1c0 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 statements */.
ac1d0 20 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b u8 isPrepareV2;
ac1e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
ac1f0 20 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 if prepared wit
ac200 68 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a h prepare_v2() *
ac210 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b /. int nChange;
ac220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ac230 75 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e umber of db chan
ac240 67 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c ges made since l
ac250 61 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 ast reset */. i
ac260 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 nt btreeMask;
ac270 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 /* Bitmas
ac280 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 k of db->aDb[] e
ac290 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 ntries reference
ac2a0 64 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74 d */. i64 start
ac2b0 54 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f Time; /
ac2c0 2a 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72 * Time when quer
ac2d0 79 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64 y started - used
ac2e0 20 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a for profiling *
ac2f0 2f 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 /. BtreeMutexAr
ac300 72 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 ray aMutex; /* A
ac310 6e 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 n array of Btree
ac320 20 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e used here and n
ac330 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a eeding locks */.
ac340 20 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 int aCounter[2
ac350 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 ]; /* Cou
ac360 6e 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 nters used by sq
ac370 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 lite3_stmt_statu
ac380 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a s() */. char *z
ac390 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Sql;
ac3a0 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
ac3b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 SQL statement th
ac3c0 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 at generated thi
ac3d0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 s */. void *pFr
ac3e0 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ee; /
ac3f0 2a 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e * Free this when
ac400 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 deleting the vd
ac410 62 65 20 2a 2f 0a 20 20 69 36 34 20 6e 46 6b 43 be */. i64 nFkC
ac420 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 onstraint;
ac430 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6d 6d /* Number of imm
ac440 2e 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 . FK constraints
ac450 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 36 this VM */. i6
ac460 34 20 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 20 4 nStmtDefCons;
ac470 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ac480 6f 66 20 64 65 66 2e 20 63 6f 6e 73 74 72 61 69 of def. constrai
ac490 6e 74 73 20 77 68 65 6e 20 73 74 6d 74 20 73 74 nts when stmt st
ac4a0 61 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 arted */. int i
ac4b0 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 Statement;
ac4c0 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 /* Statement
ac4d0 6e 75 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 number (or 0 if
ac4e0 68 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 has not opened s
ac4f0 74 6d 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 tmt) */.#ifdef S
ac500 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 QLITE_DEBUG. FI
ac510 4c 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20 LE *trace;
ac520 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 /* Write a
ac530 6e 20 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63 n execution trac
ac540 65 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e e here, if not N
ac550 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ULL */.#endif.
ac560 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d VdbeFrame *pFram
ac570 65 3b 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e e; /* Paren
ac580 74 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 t frame */. int
ac590 20 6e 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 nFrame;
ac5a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
ac5b0 66 20 66 72 61 6d 65 73 20 69 6e 20 70 46 72 61 f frames in pFra
ac5c0 6d 65 20 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32 me list */. u32
ac5d0 20 65 78 70 6d 61 73 6b 3b 20 20 20 20 20 20 20 expmask;
ac5e0 20 20 20 20 20 2f 2a 20 42 69 6e 64 69 6e 67 20 /* Binding
ac5f0 74 6f 20 74 68 65 73 65 20 76 61 72 73 20 69 6e to these vars in
ac600 76 61 6c 69 64 61 74 65 73 20 56 4d 20 2a 2f 0a validates VM */.
ac610 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f };../*.** The fo
ac620 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f llowing are allo
ac630 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 wed values for V
ac640 64 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 dbe.magic.*/.#de
ac650 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f fine VDBE_MAGIC_
ac660 49 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 65 INIT 0x26bce
ac670 61 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 aa5 /* Buildi
ac680 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 ng a VDBE progra
ac690 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 m */.#define VDB
ac6a0 45 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 E_MAGIC_RUN
ac6b0 20 30 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 0xbdf20da3 /
ac6c0 2a 20 56 44 42 45 20 69 73 20 72 65 61 64 79 20 * VDBE is ready
ac6d0 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 to execute */.#d
ac6e0 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 efine VDBE_MAGIC
ac6f0 5f 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 _HALT 0x519c
ac700 32 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 2973 /* VDBE
ac710 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 has completed ex
ac720 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 ecution */.#defi
ac730 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 ne VDBE_MAGIC_DE
ac740 41 44 20 20 20 20 20 30 78 62 36 30 36 63 33 63 AD 0xb606c3c
ac750 38 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 8 /* The VDBE
ac760 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f has been deallo
ac770 63 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 cated */../*.**
ac780 46 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 Function prototy
ac790 70 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 pes.*/.SQLITE_PR
ac7a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ac7b0 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 e3VdbeFreeCursor
ac7c0 28 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 (Vdbe *, VdbeCur
ac7d0 73 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 sor*);.void sqli
ac7e0 74 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 teVdbePopStack(V
ac7f0 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 dbe*,int);.SQLIT
ac800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ac810 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
ac820 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 oveto(VdbeCursor
ac830 2a 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 *);.#if defined(
ac840 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c SQLITE_DEBUG) ||
ac850 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 defined(VDBE_PR
ac860 4f 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 OFILE).SQLITE_PR
ac870 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ac880 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 e3VdbePrintOp(FI
ac890 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a LE*, int, Op*);.
ac8a0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
ac8b0 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
ac8c0 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
ac8d0 65 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f en(u32);.SQLITE_
ac8e0 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 PRIVATE u32 sqli
ac8f0 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
ac900 65 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 e(Mem*, int);.SQ
ac910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
ac920 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
ac930 61 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 alPut(unsigned c
ac940 68 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c har*, int, Mem*,
ac950 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
ac960 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
ac970 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 3VdbeSerialGet(c
ac980 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
ac990 61 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b ar*, u32, Mem*);
ac9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac9b0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac9c0 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 DeleteAuxData(Vd
ac9d0 62 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a beFunc*, int);..
ac9e0 69 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 int sqlite2Btree
ac9f0 4b 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 KeyCompare(BtCur
aca00 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 sor *, const voi
aca10 64 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 d *, int, int, i
aca20 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt *);.SQLITE_PR
aca30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
aca40 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 3VdbeIdxKeyCompa
aca50 72 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 re(VdbeCursor*,U
aca60 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 npackedRecord*,i
aca70 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
aca80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
aca90 56 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c VdbeIdxRowid(sql
acaa0 69 74 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20 ite3*, BtCursor
acab0 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 *, i64 *);.SQLIT
acac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
acad0 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 lite3MemCompare(
acae0 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 const Mem*, cons
acaf0 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f t Mem*, const Co
acb00 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f llSeq*);.SQLITE_
acb10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acb20 74 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65 te3VdbeExec(Vdbe
acb30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
acb40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acb50 62 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 beList(Vdbe*);.S
acb60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
acb70 74 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c t sqlite3VdbeHal
acb80 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 t(Vdbe*);.SQLITE
acb90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acba0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
acbb0 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e coding(Mem *, in
acbc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
acbd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acbe0 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a beMemTooBig(Mem*
acbf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
acc00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
acc10 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 eMemCopy(Mem*, c
acc20 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 onst Mem*);.SQLI
acc30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
acc40 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 sqlite3VdbeMemSh
acc50 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 allowCopy(Mem*,
acc60 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 const Mem*, int)
acc70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
acc80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
acc90 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d eMemMove(Mem*, M
acca0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
accb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
accc0 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
accd0 61 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ate(Mem*);.SQLIT
acce0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
accf0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
acd00 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 tr(Mem*, const c
acd10 68 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 har*, int, u8, v
acd20 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
acd30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
acd40 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
acd50 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c emSetInt64(Mem*,
acd60 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 i64);.SQLITE_PR
acd70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
acd80 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 e3VdbeMemSetDoub
acd90 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29 le(Mem*, double)
acda0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
acdb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
acdc0 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a eMemSetNull(Mem*
acdd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
acde0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
acdf0 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 beMemSetZeroBlob
ace00 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 (Mem*,int);.SQLI
ace10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ace20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
ace30 74 52 6f 77 53 65 74 28 4d 65 6d 2a 29 3b 0a 53 tRowSet(Mem*);.S
ace40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ace50 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
ace60 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d 65 MakeWriteable(Me
ace70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
ace80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ace90 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 dbeMemStringify(
acea0 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 Mem*, int);.SQLI
aceb0 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 TE_PRIVATE i64 s
acec0 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
aced0 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 ue(Mem*);.SQLITE
acee0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
acef0 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
acf00 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c erify(Mem*);.SQL
acf10 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 ITE_PRIVATE doub
acf20 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 le sqlite3VdbeRe
acf30 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 alValue(Mem*);.S
acf40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
acf50 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e id sqlite3VdbeIn
acf60 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 tegerAffinity(Me
acf70 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
acf80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
acf90 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 dbeMemRealify(Me
acfa0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
acfb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
acfc0 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d dbeMemNumerify(M
acfd0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
acfe0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
acff0 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 VdbeMemFromBtree
ad000 28 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c 69 (BtCursor*,int,i
ad010 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53 51 nt,int,Mem*);.SQ
ad020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
ad030 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
ad040 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 3b Release(Mem *p);
ad050 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ad060 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ad070 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
ad080 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 al(Mem *p);.SQLI
ad090 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ad0a0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e qlite3VdbeMemFin
ad0b0 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63 alize(Mem*, Func
ad0c0 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Def*);.SQLITE_PR
ad0d0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
ad0e0 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e *sqlite3OpcodeN
ad0f0 61 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 ame(int);.SQLITE
ad100 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ad110 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
ad120 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e Mem *pMem, int n
ad130 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b , int preserve);
ad140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ad150 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
ad160 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 loseStatement(Vd
ad170 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 be *, int);.SQLI
ad180 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ad190 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 sqlite3VdbeFrame
ad1a0 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 Delete(VdbeFrame
ad1b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
ad1c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
ad1d0 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 beFrameRestore(V
ad1e0 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a 53 51 4c dbeFrame *);.SQL
ad1f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
ad200 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
ad210 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d toreType(Mem *pM
ad220 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 em);..#ifndef SQ
ad230 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
ad240 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 N_KEY.SQLITE_PRI
ad250 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ad260 56 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 VdbeCheckFk(Vdbe
ad270 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a *, int);.#else.
ad280 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
ad290 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 69 29 VdbeCheckFk(p,i)
ad2a0 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 0.#endif..#ifnd
ad2b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
ad2c0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
ad2d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ad2e0 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
ad2f0 41 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 ArrayEnter(Vdbe
ad300 2a 70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 *p);.#else.# def
ad310 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d ine sqlite3VdbeM
ad320 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 utexArrayEnter(p
ad330 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ).#endif..SQLITE
ad340 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ad350 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 ite3VdbeMemTrans
ad360 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a late(Mem*, u8);.
ad370 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
ad380 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 BUG.SQLITE_PRIVA
ad390 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
ad3a0 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 3VdbePrintSql(Vd
ad3b0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
ad3c0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
ad3d0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 te3VdbeMemPretty
ad3e0 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c Print(Mem *pMem,
ad3f0 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 char *zBuf);.#e
ad400 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
ad410 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ad420 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 dbeMemHandleBom(
ad430 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 Mem *pMem);..#if
ad440 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
ad450 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 _INCRBLOB.SQLITE
ad460 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
ad470 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
ad480 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a andBlob(Mem *);.
ad490 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
ad4a0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
ad4b0 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 pandBlob(x) SQLI
ad4c0 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 TE_OK.#endif..#e
ad4d0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
ad4e0 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f (_VDBEINT_H_) */
ad4f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
ad500 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 * End of vdbeInt
ad510 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
ad520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad540 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
ad550 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
ad560 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
ad570 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a n utf.c ********
ad580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad590 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
ad5a0 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f E_AMALGAMATION./
ad5b0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
ad5c0 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 ng constant valu
ad5d0 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 e is used by the
ad5e0 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 SQLITE_BIGENDIA
ad5f0 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f N and.** SQLITE_
ad600 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 LITTLEENDIAN mac
ad610 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ros..*/.SQLITE_P
ad620 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 RIVATE const int
ad630 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b sqlite3one = 1;
ad640 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
ad650 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a E_AMALGAMATION *
ad660 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f /../*.** This lo
ad670 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 okup table is us
ad680 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 ed to help decod
ad690 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 e the first byte
ad6a0 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 of.** a multi-b
ad6b0 79 74 65 20 55 54 46 38 20 63 68 61 72 61 63 74 yte UTF8 charact
ad6c0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f er..*/.static co
ad6d0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
ad6e0 72 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 r sqlite3Utf8Tra
ad6f0 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 ns1[] = {. 0x00
ad700 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
ad710 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 03, 0x04, 0x05,
ad720 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 0x06, 0x07,. 0x
ad730 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 08, 0x09, 0x0a,
ad740 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 0x0b, 0x0c, 0x0d
ad750 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 , 0x0e, 0x0f,.
ad760 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 0x10, 0x11, 0x12
ad770 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 , 0x13, 0x14, 0x
ad780 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 15, 0x16, 0x17,.
ad790 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 0x18, 0x19, 0x
ad7a0 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 1a, 0x1b, 0x1c,
ad7b0 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 0x1d, 0x1e, 0x1f
ad7c0 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 ,. 0x00, 0x01,
ad7d0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 0x02, 0x03, 0x04
ad7e0 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 , 0x05, 0x06, 0x
ad7f0 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 07,. 0x08, 0x09
ad800 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 , 0x0a, 0x0b, 0x
ad810 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 0c, 0x0d, 0x0e,
ad820 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 0x0f,. 0x00, 0x
ad830 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 01, 0x02, 0x03,
ad840 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 0x04, 0x05, 0x06
ad850 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 , 0x07,. 0x00,
ad860 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 0x01, 0x02, 0x03
ad870 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 , 0x00, 0x01, 0x
ad880 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 00, 0x00,.};...#
ad890 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 define WRITE_UTF
ad8a0 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 8(zOut, c) {
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8c0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c \. if( c<
ad8d0 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 0x00080 ){
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad900 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ad910 75 38 29 28 63 26 30 78 46 46 29 3b 20 20 20 20 u8)(c&0xFF);
ad920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad930 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
ad940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad970 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c \. else if( c<
ad980 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 0x00800 ){
ad990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ad9b0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b *zOut++ = 0xC0 +
ad9c0 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31 (u8)((c>>6)&0x1
ad9d0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ad9e0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad9f0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 = 0x80 + (u8)(c
ada00 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 & 0x3F);
ada10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ada20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }
ada30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada50 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 \. else if
ada60 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 ( c<0x10000 ){
ada70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ada90 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
adaa0 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 E0 + (u8)((c>>12
adab0 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 20 20 )&0x0F);
adac0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
adad0 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 ut++ = 0x80 + (u
adae0 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 8)((c>>6) & 0x3F
adaf0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
adb00 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
adb10 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 0x80 + (u8)(c &
adb20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 0x3F);
adb30 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c \. }el
adb40 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 se{
adb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb70 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
adb80 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63 = 0xF0 + (u8)((c
adb90 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20 >>18) & 0x07);
adba0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
adbb0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 *zOut++ = 0x80
adbc0 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26 + (u8)((c>>12) &
adbd0 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 0x3F);
adbe0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
adbf0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 + = 0x80 + (u8)(
adc00 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 (c>>6) & 0x3F);
adc10 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
adc20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 *zOut++ = 0x8
adc30 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 0 + (u8)(c & 0x3
adc40 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
adc50 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
adc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adc90 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 .}..#define WRIT
adca0 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 E_UTF16LE(zOut,
adcb0 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 c) {
adcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adcd0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
adce0 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 c<=0xFFFF ){
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 20 20 20 20 20 20 5c 0a \.
add20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
add30 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 8)(c&0x00FF);
add40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add60 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
add70 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 + = (u8)((c>>8)&
add80 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 0x00FF);
add90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adda0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 \. }e
addb0 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 lse{
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addf0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ade00 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 (u8)(((c>>10)&0x
ade10 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 003F) + (((c-0x1
ade20 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 0000)>>10)&0x00C
ade30 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 0)); \. *zOu
ade40 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 t++ = (u8)(0x00D
ade50 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 8 + (((c-0x10000
ade60 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 )>>18)&0x03));
ade70 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ade80 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
ade90 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 (c&0x00FF);
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 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
aded0 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 = (u8)(0x00DC +
adee0 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 ((c>>8)&0x03));
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 5c 0a 20 20 7d 20 20 20 \. }
adf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 5c 0a \.
adf50 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 }..#define WRITE
adf60 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 _UTF16BE(zOut, c
adf70 29 20 7b 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 20 5c 0a 20 20 69 66 28 20 63 \. if( c
adfa0 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 <=0xFFFF ){
adfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
adfe0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
adff0 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 )((c>>8)&0x00FF)
ae000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae020 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ae030 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 = (u8)(c&0x00FF
ae040 29 3b 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 20 5c 0a 20 20 7d 65 6c \. }el
ae070 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 se{
ae080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae0b0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ae0c0 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 u8)(0x00D8 + (((
ae0d0 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 c-0x10000)>>18)&
ae0e0 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 0x03));
ae0f0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ae100 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 ++ = (u8)(((c>>1
ae110 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 0)&0x003F) + (((
ae120 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 c-0x10000)>>10)&
ae130 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 0x00C0)); \.
ae140 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
ae150 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 0x00DC + ((c>>8)
ae160 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 &0x03));
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae180 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
ae190 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b (u8)(c&0x00FF);
ae1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 5c 0a 20 20 7d 20 20 20 20 \. }
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 5c 0a 7d \.}
ae210 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 ..#define READ_U
ae220 54 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d TF16LE(zIn, TERM
ae230 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 , c){
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae250 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 \. c =
ae260 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 (*zIn++);
ae270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2a0 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b \. c += ((*zIn+
ae2b0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 +)<<8);
ae2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae2e0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
ae2f0 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 c>=0xD800 && c<0
ae300 78 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b xE000 && TERM ){
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 20 20 20 20 20 20 20 20 20
ae330 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 \. int c2 = (
ae340 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 *zIn++);
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 20 20 20
ae370 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 \. c2
ae380 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 += ((*zIn++)<<8
ae390 29 3b 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 20 20 20 20 20 20 20 20 20 20 20 20 20
ae3c0 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 \. c = (c2&0x
ae3d0 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 03FF) + ((c&0x00
ae3e0 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 3F)<<10) + (((c&
ae3f0 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 0x03C0)+0x0040)<
ae400 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 <10); \. }
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 20 20 20 20 20 20 20 20 20 20
ae450 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 \.}..#define REA
ae460 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 54 D_UTF16BE(zIn, T
ae470 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20 ERM, c){
ae480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae490 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 \. c
ae4a0 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 = ((*zIn++)<<8)
ae4b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae4e0 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 \. c += (*zI
ae4f0 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
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 20 20
ae520 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 \. i
ae530 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 f( c>=0xD800 &&
ae540 63 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d c<0xE000 && TERM
ae550 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ae560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae570 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 \. int c2
ae580 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b = ((*zIn++)<<8);
ae590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5b0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ae5c0 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b c2 += (*zIn++);
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 20 20 20 20 20 20 20 20 20 20
ae600 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 \. c = (c2
ae610 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 &0x03FF) + ((c&0
ae620 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 x003F)<<10) + ((
ae630 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 (c&0x03C0)+0x004
ae640 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 0)<<10); \. }
ae650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 20 20 20 20 20 20 20
ae690 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 \.}../*.** Tr
ae6a0 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 anslate a single
ae6b0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
ae6c0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e . Return the un
ae6d0 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a icode value..**.
ae6e0 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c ** During transl
ae6f0 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 ation, assume th
ae700 61 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74 at the byte that
ae710 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a zTerm points.**
ae720 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a is a 0x00..**.*
ae730 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 * Write a pointe
ae740 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e r to the next un
ae750 72 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 read byte back i
ae760 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a nto *pzNext..**.
ae770 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 ** Notes On Inva
ae780 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a lid UTF-8:.**.**
ae790 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e * This routin
ae7a0 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 e never allows a
ae7b0 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 7-bit character
ae7c0 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 (0x00 through 0
ae7d0 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 x7f) to.** b
ae7e0 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d e encoded as a m
ae7f0 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 ulti-byte charac
ae800 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d ter. Any multi-
ae810 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 byte character t
ae820 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d hat.** attem
ae830 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 pts to encode a
ae840 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 value between 0x
ae850 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 00 and 0x7f is r
ae860 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 endered as 0xfff
ae870 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 d..**.** * Thi
ae880 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 s routine never
ae890 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 allows a UTF16 s
ae8a0 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 urrogate value t
ae8b0 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a o be encoded..**
ae8c0 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d If a multi-
ae8d0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61 byte character a
ae8e0 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 ttempts to encod
ae8f0 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 e a value betwee
ae900 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 n.** 0xd800
ae910 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 and 0xe000 then
ae920 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 it is rendered a
ae930 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 s 0xfffd..**.**
ae940 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 * Bytes in the
ae950 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 range of 0x80 t
ae960 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 hrough 0xbf whic
ae970 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 h occur as the f
ae980 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 irst.** byte
ae990 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20 of a character
ae9a0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 are interpreted
ae9b0 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 as single-byte c
ae9c0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 haracters.**
ae9d0 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 and rendered as
ae9e0 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e themselves even
ae9f0 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 though they are
aea00 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 technically.**
aea10 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 invalid char
aea20 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a acters..**.** *
aea30 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 This routine a
aea40 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 ccepts an infini
aea50 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 te number of dif
aea60 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f ferent UTF8 enco
aea70 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 dings.** for
aea80 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 unicode values
aea90 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 0x80 and greater
aeaa0 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 . It do not cha
aeab0 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a nge over-length.
aeac0 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 ** encodings
aead0 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f to 0xfffd as so
aeae0 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d me systems recom
aeaf0 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mend..*/.#define
aeb00 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 READ_UTF8(zIn,
aeb10 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 zTerm, c)
aeb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeb30 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 \. c = *(zI
aeb40 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
aeb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeb70 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 \. if( c>=0xc0
aeb80 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
aeb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
aebb0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
aebc0 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 tf8Trans1[c-0xc0
aebd0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ];
aebe0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
aebf0 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 while( zIn!=zTe
aec00 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 rm && (*zIn & 0x
aec10 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 c0)==0x80 ){
aec20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 \.
aec30 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 c = (c<<6) + (0x
aec40 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 3f & *(zIn++));
aec50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aec60 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 \. }
aec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aec80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeca0 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 \. if( c<0x
aecb0 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 80
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 20 20 20 20 20 5c \
aece0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 . || (c&0
aecf0 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 xFFFFF800)==0xD8
aed00 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
aed10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
aed20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 || (c&0xFF
aed30 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 FFFFFE)==0xFFFE
aed40 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 ){ c = 0xFFFD;
aed50 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 } \. }.S
aed60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
aed70 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 t sqlite3Utf8Rea
aed80 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 d(. const unsig
aed90 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20 ned char *zIn,
aeda0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
aedb0 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 te of UTF-8 char
aedc0 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 acter */. const
aedd0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
aede0 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 *pzNext /* Wr
aedf0 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70 ite first byte p
aee00 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 ast UTF-8 char h
aee10 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
aee20 63 3b 0a 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73 c;.. /* Same as
aee30 20 52 45 41 44 5f 55 54 46 38 28 29 20 61 62 6f READ_UTF8() abo
aee40 76 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74 ve but without t
aee50 68 65 20 7a 54 65 72 6d 20 70 61 72 61 6d 65 74 he zTerm paramet
aee60 65 72 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 er.. ** For thi
aee70 73 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 73 s routine, we as
aee80 73 75 6d 65 20 74 68 65 20 55 54 46 38 20 73 74 sume the UTF8 st
aee90 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 7a ring is always z
aeea0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a ero-terminated..
aeeb0 20 20 2a 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e */. c = *(zIn
aeec0 2b 2b 29 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 ++);. if( c>=0x
aeed0 63 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 c0 ){. c = sq
aeee0 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b lite3Utf8Trans1[
aeef0 63 2d 30 78 63 30 5d 3b 0a 20 20 20 20 77 68 69 c-0xc0];. whi
aef00 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 le( (*zIn & 0xc0
aef10 29 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20 )==0x80 ){.
aef20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 c = (c<<6) + (0
aef30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b x3f & *(zIn++));
aef40 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 . }. if( c
aef50 3c 30 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c <0x80. ||
aef60 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d (c&0xFFFFF800)=
aef70 3d 30 78 44 38 30 30 0a 20 20 20 20 20 20 20 20 =0xD800.
aef80 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 || (c&0xFFFFFFFE
aef90 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 )==0xFFFE ){ c
aefa0 3d 20 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a = 0xFFFD; }. }.
aefb0 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b *pzNext = zIn;
aefc0 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a . return c;.}..
aefd0 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 .../*.** If the
aefe0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 TRANSLATE_TRACE
aeff0 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 macro is defined
af000 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 , the value of e
af010 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 ach Mem is.** pr
af020 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20 inted on stderr
af030 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 on the way into
af040 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 and out of sqlit
af050 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
af060 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 te()..*/ ./* #de
af070 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 fine TRANSLATE_T
af080 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 RACE 1 */..#ifnd
af090 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
af0a0 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 TF16./*.** This
af0b0 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 routine transfor
af0c0 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ms the internal
af0d0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 text encoding us
af0e0 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a ed by pMem to.**
af0f0 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20 desiredEnc. It
af100 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 is an error if t
af110 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72 he string is alr
af120 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69 eady of the desi
af130 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c red.** encoding,
af140 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 or if *pMem doe
af150 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 s not contain a
af160 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f string value..*/
af170 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
af180 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
af190 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 emTranslate(Mem
af1a0 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65 *pMem, u8 desire
af1b0 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e dEnc){. int len
af1c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
af1d0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
af1e0 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74 length of output
af1f0 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73 string in bytes
af200 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
af210 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 har *zOut;
af220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
af230 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a utput buffer */.
af240 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
af250 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 *zIn;
af260 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 /* Input
af270 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 iterator */. u
af280 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 nsigned char *zT
af290 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 erm;
af2a0 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 /* End of i
af2b0 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e nput */. unsign
af2c0 65 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 ed char *z;
af2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
af2e0 2f 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74 /* Output iterat
af2f0 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 or */. unsigned
af300 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 int c;.. asser
af310 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
af320 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
af330 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
af340 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
af350 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d t( pMem->flags&M
af360 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65 EM_Str );. asse
af370 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 rt( pMem->enc!=d
af380 65 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 esiredEnc );. a
af390 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 ssert( pMem->enc
af3a0 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
af3b0 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a pMem->n>=0 );..
af3c0 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e #if defined(TRAN
af3d0 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 SLATE_TRACE) &&
af3e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
af3f0 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 EBUG). {. ch
af400 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 ar zBuf[100];.
af410 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
af420 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d PrettyPrint(pMem
af430 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 , zBuf);. fpr
af440 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e intf(stderr, "IN
af450 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 PUT: %s\n", zBu
af460 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a f);. }.#endif..
af470 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e /* If the tran
af480 73 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65 slation is betwe
af490 65 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 en UTF-16 little
af4a0 20 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c and big endian,
af4b0 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 then . ** all
af4c0 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 that is required
af4d0 20 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20 is to swap the
af4e0 62 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73 byte order. This
af4f0 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 case is handled
af500 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c . ** differentl
af510 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 y from the other
af520 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d s.. */. if( pM
af530 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f em->enc!=SQLITE_
af540 55 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45 UTF8 && desiredE
af550 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
af560 29 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a ){. u8 temp;.
af570 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 int rc;.
af580 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
af590 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
af5a0 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 (pMem);. if(
af5b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
af5c0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
af5d0 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
af5e0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
af5f0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
af600 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 }. zIn = (u
af610 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 8*)pMem->z;.
af620 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 zTerm = &zIn[pMe
af630 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 m->n&~1];. wh
af640 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 ile( zIn<zTerm )
af650 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a {. temp = *
af660 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 zIn;. *zIn
af670 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 = *(zIn+1);.
af680 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a zIn++;. *
af690 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 zIn++ = temp;.
af6a0 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e }. pMem->en
af6b0 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a c = desiredEnc;.
af6c0 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 goto transla
af6d0 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f te_out;. }.. /
af6e0 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 * Set len to the
af6f0 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
af700 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 of bytes require
af710 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 d in the output
af720 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 buffer. */. if(
af730 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
af740 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 ITE_UTF8 ){.
af750 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 /* When converti
af760 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 ng from UTF-16,
af770 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 the maximum grow
af780 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a th results from.
af790 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 ** translati
af7a0 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72 ng a 2-byte char
af7b0 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74 acter to a 4-byt
af7c0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 e UTF-8 characte
af7d0 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 r.. ** A sing
af7e0 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69 le byte is requi
af7f0 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70 red for the outp
af800 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a ut string. **
af810 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e nul-terminator.
af820 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d . */. pMem
af830 2d 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c ->n &= ~1;. l
af840 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 en = pMem->n * 2
af850 20 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 + 1;. }else{.
af860 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 /* When conve
af870 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 rting from UTF-8
af880 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d to UTF-16 the m
af890 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 aximum growth is
af8a0 20 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 caused. ** w
af8b0 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 46 hen a 1-byte UTF
af8c0 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73 20 -8 character is
af8d0 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 translated into
af8e0 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 0a a 2-byte UTF-16.
af8f0 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72 ** character
af900 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 20 . Two bytes are
af910 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 required in the
af920 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 66 6f output buffer fo
af930 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c r the. ** nul
af940 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 -terminator..
af950 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d */. len = pM
af960 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 em->n * 2 + 2;.
af970 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e }.. /* Set zIn
af980 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 to point at the
af990 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 6e start of the in
af9a0 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 7a put buffer and z
af9b0 54 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a Term to point 1.
af9c0 20 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 74 ** byte past t
af9d0 68 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a he end.. **. *
af9e0 2a 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 * Variable zOut
af9f0 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 is set to point
afa00 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 at the output bu
afa10 66 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 61 ffer, space obta
afa20 69 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 ined. ** from s
afa30 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e qlite3_malloc().
afa40 0a 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 . */. zIn = (u
afa50 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 8*)pMem->z;. zT
afa60 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d erm = &zIn[pMem-
afa70 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 >n];. zOut = sq
afa80 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
afa90 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b (pMem->db, len);
afaa0 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a . if( !zOut ){.
afab0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
afac0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a E_NOMEM;. }. z
afad0 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 = zOut;.. if(
afae0 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 pMem->enc==SQLIT
afaf0 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 66 E_UTF8 ){. if
afb00 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 ( desiredEnc==SQ
afb10 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a LITE_UTF16LE ){.
afb20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d /* UTF-8 -
afb30 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d > UTF-16 Little-
afb40 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 endian */.
afb50 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d while( zIn<zTerm
afb60 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 ){. /* c
afb70 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
afb80 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 ad(zIn, zTerm, (
afb90 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 const u8**)&zIn)
afba0 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 ; */. REA
afbb0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 D_UTF8(zIn, zTer
afbc0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 m, c);. W
afbd0 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 RITE_UTF16LE(z,
afbe0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
afbf0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
afc00 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d ert( desiredEnc=
afc10 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
afc20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d );. /* UTF-
afc30 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 8 -> UTF-16 Big-
afc40 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 endian */.
afc50 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d while( zIn<zTerm
afc60 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 ){. /* c
afc70 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
afc80 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 ad(zIn, zTerm, (
afc90 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 const u8**)&zIn)
afca0 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 ; */. REA
afcb0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 D_UTF8(zIn, zTer
afcc0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 m, c);. W
afcd0 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 RITE_UTF16BE(z,
afce0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
afcf0 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
afd00 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b (int)(z - zOut);
afd10 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 . *z++ = 0;.
afd20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
afd30 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d rt( desiredEnc==
afd40 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 SQLITE_UTF8 );.
afd50 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 if( pMem->enc
afd60 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 ==SQLITE_UTF16LE
afd70 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 ){. /* UTF
afd80 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 Little-endia
afd90 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 n -> UTF-8 */.
afda0 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a while( zIn<z
afdb0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 Term ){.
afdc0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e READ_UTF16LE(zIn
afdd0 2c 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b , zIn<zTerm, c);
afde0 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f . WRITE_
afdf0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 UTF8(z, c);.
afe00 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
afe10 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 /* UTF-16 B
afe20 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 ig-endian -> UTF
afe30 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c -8 */. whil
afe40 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a e( zIn<zTerm ){.
afe50 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
afe60 31 36 42 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 16BE(zIn, zIn<zT
afe70 65 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 erm, c); .
afe80 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 WRITE_UTF8(z,
afe90 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
afea0 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 }. pMem->n =
afeb0 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b (int)(z - zOut);
afec0 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 . }. *z = 0;.
afed0 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
afee0 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 n+(desiredEnc==S
afef0 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 29 QLITE_UTF8?1:2))
aff00 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69 <=len );.. sqli
aff10 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
aff20 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d e(pMem);. pMem-
aff30 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
aff40 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d Static|MEM_Dyn|M
aff50 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65 EM_Ephem);. pMe
aff60 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 m->enc = desired
aff70 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 Enc;. pMem->fla
aff80 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c gs |= (MEM_Term|
aff90 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d MEM_Dyn);. pMem
affa0 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75 ->z = (char*)zOu
affb0 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c t;. pMem->zMall
affc0 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74 oc = pMem->z;..t
affd0 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69 ranslate_out:.#i
affe0 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c f defined(TRANSL
afff0 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 ATE_TRACE) && de
b0000 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
b0010 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 UG). {. char
b0020 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 zBuf[100];.
b0030 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 sqlite3VdbeMemPr
b0040 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 ettyPrint(pMem,
b0050 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e zBuf);. fprin
b0060 74 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54 50 tf(stderr, "OUTP
b0070 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 UT: %s\n", zBuf)
b0080 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
b0090 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
b00a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
b00b0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f outine checks fo
b00c0 72 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d r a byte-order m
b00d0 61 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e ark at the begin
b00e0 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20 ning of the .**
b00f0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73 74 UTF-16 string st
b0100 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49 ored in *pMem. I
b0110 66 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e 74 f one is present
b0120 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64 20 , it is removed
b0130 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64 and.** the encod
b0140 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 61 ing of the Mem a
b0150 64 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72 6f djusted. This ro
b0160 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64 utine does not d
b0170 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77 o any.** byte-sw
b0180 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74 20 apping, it just
b0190 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70 sets Mem.enc app
b01a0 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a ropriately..**.*
b01b0 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e * The allocation
b01c0 20 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d 69 (static, dynami
b01d0 63 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f c etc.) and enco
b01e0 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 ding of the Mem
b01f0 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65 may be.** change
b0200 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
b0210 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
b0220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b0230 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 3VdbeMemHandleBo
b0240 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 m(Mem *pMem){.
b0250 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
b0260 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30 OK;. u8 bom = 0
b0270 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ;.. assert( pMe
b0280 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 69 66 28 m->n>=0 );. if(
b0290 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 pMem->n>1 ){.
b02a0 20 20 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a u8 b1 = *(u8 *
b02b0 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 )pMem->z;. u8
b02c0 20 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70 b2 = *(((u8 *)p
b02d0 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 Mem->z) + 1);.
b02e0 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26 if( b1==0xFE &
b02f0 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20 & b2==0xFF ){.
b0300 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 bom = SQLITE
b0310 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a _UTF16BE;. }.
b0320 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 46 if( b1==0xFF
b0330 20 26 26 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a && b2==0xFE ){.
b0340 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 bom = SQLI
b0350 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 TE_UTF16LE;.
b0360 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 62 }. }. . if( b
b0370 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 om ){. rc = s
b0380 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
b0390 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 eWriteable(pMem)
b03a0 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
b03b0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
b03c0 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 pMem->n -= 2;.
b03d0 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 memmove(pMe
b03e0 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 m->z, &pMem->z[2
b03f0 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 ], pMem->n);.
b0400 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d pMem->z[pMem-
b0410 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 >n] = '\0';.
b0420 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
b0430 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 n+1] = '\0';.
b0440 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
b0450 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 = MEM_Term;.
b0460 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f pMem->enc = bo
b0470 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 m;. }. }. r
b0480 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
b0490 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
b04a0 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a T_UTF16 */../*.*
b04b0 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20 * pZ is a UTF-8
b04c0 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 encoded unicode
b04d0 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65 string. If nByte
b04e0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 is less than ze
b04f0 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 ro,.** return th
b0500 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 e number of unic
b0510 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 ode characters i
b0520 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20 n pZ up to (but
b0530 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a not including).*
b0540 2a 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 * the first 0x00
b0550 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20 byte. If nByte
b0560 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e is not less than
b0570 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68 zero, return th
b0580 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 e.** number of u
b0590 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 nicode character
b05a0 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e s in the first n
b05b0 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75 Byte of pZ (or u
b05c0 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72 p to .** the fir
b05d0 73 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76 st 0x00, whichev
b05e0 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e er comes first).
b05f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b0600 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
b0610 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 f8CharLen(const
b0620 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e char *zIn, int n
b0630 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d Byte){. int r =
b0640 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 0;. const u8 *
b0650 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a z = (const u8*)z
b0660 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a In;. const u8 *
b0670 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79 zTerm;. if( nBy
b0680 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65 te>=0 ){. zTe
b0690 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a rm = &z[nByte];.
b06a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65 }else{. zTe
b06b0 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 rm = (const u8*)
b06c0 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 (-1);. }. asse
b06d0 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a rt( z<=zTerm );.
b06e0 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26 while( *z!=0 &
b06f0 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 & z<zTerm ){.
b0700 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 SQLITE_SKIP_UTF
b0710 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 8(z);. r++;.
b0720 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d }. return r;.}
b0730 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66 ../* This test f
b0740 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 unction is not c
b0750 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79 urrently used by
b0760 20 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74 the automated t
b0770 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48 est-suite. .** H
b0780 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20 ence it is only
b0790 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62 available in deb
b07a0 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 ug builds..*/.#i
b07b0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
b07c0 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65 _TEST) && define
b07d0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
b07e0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 /*.** Translate
b07f0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a UTF-8 to UTF-8..
b0800 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 20 74 **.** This has t
b0810 68 65 20 65 66 66 65 63 74 20 6f 66 20 6d 61 6b he effect of mak
b0820 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 74 68 ing sure that th
b0830 65 20 73 74 72 69 6e 67 20 69 73 20 77 65 6c 6c e string is well
b0840 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 -formed.** UTF-8
b0850 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 68 61 72 . Miscoded char
b0860 61 63 74 65 72 73 20 61 72 65 20 72 65 6d 6f 76 acters are remov
b0870 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 ed..**.** The tr
b0880 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e anslation is don
b0890 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63 e in-place (sinc
b08a0 65 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 62 e it is impossib
b08b0 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f le for the.** co
b08c0 72 72 65 63 74 20 55 54 46 2d 38 20 65 6e 63 6f rrect UTF-8 enco
b08d0 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65 ding to be longe
b08e0 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d r than a malform
b08f0 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f ed encoding)..*/
b0900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0910 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 int sqlite3Utf8T
b0920 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 o8(unsigned char
b0930 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e *zIn){. unsign
b0940 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 ed char *zOut =
b0950 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 zIn;. unsigned
b0960 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a char *zStart = z
b0970 49 6e 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20 In;. u32 c;..
b0980 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b while( zIn[0] ){
b0990 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 . c = sqlite3
b09a0 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 28 63 Utf8Read(zIn, (c
b09b0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b onst u8**)&zIn);
b09c0 0a 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 . if( c!=0xff
b09d0 66 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 fd ){. WRIT
b09e0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b E_UTF8(zOut, c);
b09f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f . }. }. *zO
b0a00 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ut = 0;. return
b0a10 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53 (int)(zOut - zS
b0a20 74 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a tart);.}.#endif.
b0a30 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
b0a40 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a OMIT_UTF16./*.**
b0a50 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 Convert a UTF-1
b0a60 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6 string in the
b0a70 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 native encoding
b0a80 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 into a UTF-8 str
b0a90 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 ing..** Memory t
b0aa0 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 o hold the UTF-8
b0ab0 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
b0ac0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
b0ad0 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 _malloc and must
b0ae0 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 .** be freed by
b0af0 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 the calling func
b0b00 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c tion..**.** NULL
b0b10 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
b0b20 74 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f there is an allo
b0b30 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f cation error..*/
b0b40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b0b50 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 char *sqlite3Utf
b0b60 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 16to8(sqlite3 *d
b0b70 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a b, const void *z
b0b80 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
b0b90 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 Mem m;. memset(
b0ba0 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 &m, 0, sizeof(m)
b0bb0 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a );. m.db = db;.
b0bc0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
b0bd0 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 SetStr(&m, z, nB
b0be0 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 yte, SQLITE_UTF1
b0bf0 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 6NATIVE, SQLITE_
b0c00 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 STATIC);. sqlit
b0c10 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
b0c20 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f ding(&m, SQLITE_
b0c30 55 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d UTF8);. if( db-
b0c40 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
b0c50 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
b0c60 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a MemRelease(&m);.
b0c70 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d m.z = 0;. }
b0c80 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c . assert( (m.fl
b0c90 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 ags & MEM_Term)!
b0ca0 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 =0 || db->malloc
b0cb0 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 Failed );. asse
b0cc0 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d rt( (m.flags & M
b0cd0 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62 EM_Str)!=0 || db
b0ce0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
b0cf0 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c ;. return (m.fl
b0d00 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d ags & MEM_Dyn)!=
b0d10 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 0 ? m.z : sqlite
b0d20 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e 3DbStrDup(db, m.
b0d30 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e z);.}../*.** Con
b0d40 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 vert a UTF-8 str
b0d50 69 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31 ing to the UTF-1
b0d60 36 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 6 encoding speci
b0d70 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
b0d80 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e r.** enc. A poin
b0d90 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 ter to the new s
b0da0 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 tring is returne
b0db0 64 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 d, and the value
b0dc0 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73 of *pnOut.** is
b0dd0 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 set to the leng
b0de0 74 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e th of the return
b0df0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 ed string in byt
b0e00 65 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f es. The call sho
b0e10 75 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74 uld.** arrange t
b0e20 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 o call sqlite3Db
b0e30 46 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65 Free() on the re
b0e40 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77 turned pointer w
b0e50 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20 hen it is.** no
b0e60 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e longer required.
b0e70 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c .** .** If a mal
b0e80 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 loc failure occu
b0e90 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 rs, NULL is retu
b0ea0 72 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e rned and the db.
b0eb0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20 mallocFailed.**
b0ec0 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66 flag set..*/.#if
b0ed0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
b0ee0 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 E_STAT2.SQLITE_P
b0ef0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
b0f00 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c ite3Utf8to16(sql
b0f10 69 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63 ite3 *db, u8 enc
b0f20 2c 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e , char *z, int n
b0f30 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 , int *pnOut){.
b0f40 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 Mem m;. memset
b0f50 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d (&m, 0, sizeof(m
b0f60 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b ));. m.db = db;
b0f70 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
b0f80 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e mSetStr(&m, z, n
b0f90 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
b0fa0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
b0fb0 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
b0fc0 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c MemTranslate(&m,
b0fd0 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73 enc) ){. ass
b0fe0 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ert( db->mallocF
b0ff0 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 ailed );. ret
b1000 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 0;. }. ass
b1010 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c ert( m.z==m.zMal
b1020 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20 loc );. *pnOut
b1030 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20 = m.n;. return
b1040 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f m.z;.}.#endif../
b1050 2a 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54 *.** zIn is a UT
b1060 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 F-16 encoded uni
b1070 63 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c code string at l
b1080 65 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 east nChar chara
b1090 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 cters long..** R
b10a0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
b10b0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 of bytes in the
b10c0 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 first nChar uni
b10d0 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a code characters.
b10e0 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 ** in pZ. nChar
b10f0 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 must be non-neg
b1100 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ative..*/.SQLITE
b1110 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b1120 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e ite3Utf16ByteLen
b1130 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e (const void *zIn
b1140 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 , int nChar){.
b1150 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 int c;. unsigne
b1160 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 d char const *z
b1170 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d = zIn;. int n =
b1180 20 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c 0;. . if( SQL
b1190 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d ITE_UTF16NATIVE=
b11a0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 =SQLITE_UTF16BE
b11b0 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c ){. while( n<
b11c0 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52 nChar ){. R
b11d0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 EAD_UTF16BE(z, 1
b11e0 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b , c);. n++;
b11f0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
b1200 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 while( n<nCh
b1210 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 ar ){. READ
b1220 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 _UTF16LE(z, 1, c
b1230 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 );. n++;.
b1240 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
b1250 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e (int)(z-(unsign
b1260 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 ed char const *)
b1270 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 zIn);.}..#if def
b1280 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
b1290 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 )./*.** This rou
b12a0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 tine is called f
b12b0 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 rom the TCL test
b12c0 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 function "trans
b12d0 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a late_selftest"..
b12e0 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 ** It checks tha
b12f0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 t the primitives
b1300 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 for serializing
b1310 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 and deserializi
b1320 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 ng.** characters
b1330 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e in each encodin
b1340 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f g are inverses o
b1350 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f f each other..*/
b1360 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b1370 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 void sqlite3UtfS
b1380 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 elfTest(void){.
b1390 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c unsigned int i,
b13a0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 t;. unsigned c
b13b0 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 har zBuf[20];.
b13c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b13d0 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 ;. int n;. uns
b13e0 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 igned int c;..
b13f0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b1400 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b1410 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
b1420 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b RITE_UTF8(z, i);
b1430 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a . n = (int)(z
b1440 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 -zBuf);. asse
b1450 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 rt( n>0 && n<=4
b1460 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b );. z[0] = 0;
b1470 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 . z = zBuf;.
b1480 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 c = sqlite3Ut
b1490 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74 f8Read(z, (const
b14a0 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 u8**)&z);. t
b14b0 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e = i;. if( i>
b14c0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 =0xD800 && i<=0x
b14d0 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 DFFF ) t = 0xFFF
b14e0 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 D;. if( (i&0x
b14f0 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 FFFFFFFE)==0xFFF
b1500 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a E ) t = 0xFFFD;.
b1510 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 assert( c==t
b1520 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
b1530 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 (z-zBuf)==n );.
b1540 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
b1550 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 0x00110000; i++)
b1560 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 {. if( i>=0xD
b1570 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 800 && i<0xE000
b1580 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
b1590 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 z = zBuf;. WR
b15a0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 ITE_UTF16LE(z, i
b15b0 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 );. n = (int)
b15c0 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 (z-zBuf);. as
b15d0 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d sert( n>0 && n<=
b15e0 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 4 );. z[0] =
b15f0 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0;. z = zBuf;
b1600 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c . READ_UTF16L
b1610 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 E(z, 1, c);.
b1620 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a assert( c==i );.
b1630 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a assert( (z-z
b1640 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 Buf)==n );. }.
b1650 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 for(i=0; i<0x00
b1660 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 110000; i++){.
b1670 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 if( i>=0xD800
b1680 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f && i<0xE000 ) co
b1690 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 ntinue;. z =
b16a0 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f zBuf;. WRITE_
b16b0 55 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 UTF16BE(z, i);.
b16c0 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a n = (int)(z-z
b16d0 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Buf);. assert
b16e0 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b ( n>0 && n<=4 );
b16f0 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 . z[0] = 0;.
b1700 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 z = zBuf;.
b1710 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c READ_UTF16BE(z,
b1720 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 1, c);. asse
b1730 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 rt( c==i );.
b1740 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 assert( (z-zBuf)
b1750 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e ==n );. }.}.#en
b1760 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 dif /* SQLITE_TE
b1770 53 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 ST */.#endif /*
b1780 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
b1790 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 6 */../*********
b17a0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66 ***** End of utf
b17b0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
b17c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b17d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b17e0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
b17f0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
b1800 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a util.c ********
b1810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1830 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
b1840 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
b1850 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
b1860 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
b1870 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
b1880 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
b1890 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
b18a0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
b18b0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
b18c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
b18d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
b18e0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
b18f0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
b1900 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
b1910 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
b1920 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
b1930 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
b1940 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
b1950 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
b1960 2a 2a 0a 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 2a 2a 2a 2a 2a 2a ****************
b1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b19a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
b19b0 55 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e Utility function
b19c0 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75 s used throughou
b19d0 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 t sqlite..**.**
b19e0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
b19f0 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 ns functions for
b1a00 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f allocating memo
b1a10 72 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a ry, comparing.**
b1a20 20 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 74 strings, and st
b1a30 75 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a uff like that..*
b1a40 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 *.*/.#ifdef SQLI
b1a50 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 20 TE_HAVE_ISNAN.#
b1a60 69 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e include <math.h>
b1a70 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
b1a80 6f 75 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f outine needed to
b1a90 20 73 75 70 70 6f 72 74 20 74 68 65 20 74 65 73 support the tes
b1aa0 74 63 61 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a tcase() macro..*
b1ab0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
b1ac0 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 COVERAGE_TEST.SQ
b1ad0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b1ae0 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 d sqlite3Coverag
b1af0 65 28 69 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 e(int x){. stat
b1b00 69 63 20 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 ic int dummy = 0
b1b10 3b 0a 20 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a ;. dummy += x;.
b1b20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
b1b30 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 Return true if t
b1b40 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e he floating poin
b1b50 74 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61 t value is Not a
b1b60 20 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a Number (NaN)..*
b1b70 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74 *.** Use the mat
b1b80 68 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28 h library isnan(
b1b90 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f ) function if co
b1ba0 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 mpiled with SQLI
b1bb0 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a TE_HAVE_ISNAN..*
b1bc0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20 * Otherwise, we
b1bd0 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70 have our own imp
b1be0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 lementation that
b1bf0 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 works on most s
b1c00 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ystems..*/.SQLIT
b1c10 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b1c20 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c lite3IsNaN(doubl
b1c30 65 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 e x){. int rc;
b1c40 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 /* The value r
b1c50 65 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65 eturn */.#if !de
b1c60 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 fined(SQLITE_HAV
b1c70 45 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 E_ISNAN). /*.
b1c80 2a 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20 ** Systems that
b1c90 73 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61 support the isna
b1ca0 6e 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 n() library func
b1cb0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62 tion should prob
b1cc0 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 ably. ** make u
b1cd0 73 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70 se of it by comp
b1ce0 69 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c iling with -DSQL
b1cf0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20 ITE_HAVE_ISNAN.
b1d00 20 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a But we have. *
b1d10 2a 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e * found that man
b1d20 79 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74 y systems do not
b1d30 20 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20 have a working
b1d40 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e isnan() function
b1d50 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d so. ** this im
b1d60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
b1d70 70 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61 provided as an a
b1d80 6c 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a lternative.. **
b1d90 0a 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74 . ** This NaN t
b1da0 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 est sometimes fa
b1db0 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ils if compiled
b1dc0 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61 on GCC with -ffa
b1dd0 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e st-math.. ** On
b1de0 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
b1df0 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61 the use of -ffa
b1e00 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 st-math comes wi
b1e10 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 th the following
b1e20 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 . ** warning:.
b1e30 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 **. ** Th
b1e40 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 is option [-ffas
b1e50 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e t-math] should n
b1e60 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f ever be turned o
b1e70 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 n by any. **
b1e80 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e -O option sin
b1e90 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 ce it can result
b1ea0 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 in incorrect ou
b1eb0 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d tput for program
b1ec0 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 s. ** whic
b1ed0 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 h depend on an e
b1ee0 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 xact implementat
b1ef0 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 ion of IEEE or I
b1f00 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 SO . ** ru
b1f10 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f les/specificatio
b1f20 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 ns for math func
b1f30 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
b1f40 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69 Under MSVC, thi
b1f50 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66 s NaN test may f
b1f60 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 ail if compiled
b1f70 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d with a floating-
b1f80 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63 . ** point prec
b1f90 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72 ision mode other
b1fa0 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73 than /fp:precis
b1fb0 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44 e. From the MSD
b1fc0 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 N . ** document
b1fd0 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a ation:. **. **
b1fe0 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c The compil
b1ff0 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65 er [with /fp:pre
b2000 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65 cise] will prope
b2010 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61 rly handle compa
b2020 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20 risons . **
b2030 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e involving NaN.
b2040 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20 For example, x
b2050 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74 != x evaluates t
b2060 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e o true if x is N
b2070 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e aN . ** ..
b2080 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f .. */.#ifdef __
b2090 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 FAST_MATH__.# er
b20a0 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 ror SQLite will
b20b0 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 not work correct
b20c0 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61 ly with the -ffa
b20d0 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f st-math option o
b20e0 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20 f GCC..#endif.
b20f0 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 volatile double
b2100 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c y = x;. volatil
b2110 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a e double z = y;.
b2120 20 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 rc = (y!=z);.#
b2130 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69 else /* if defi
b2140 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f ned(SQLITE_HAVE_
b2150 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d ISNAN) */. rc =
b2160 20 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69 isnan(x);.#endi
b2170 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45 f /* SQLITE_HAVE
b2180 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74 _ISNAN */. test
b2190 63 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65 case( rc );. re
b21a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
b21b0 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 * Compute a stri
b21c0 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69 ng length that i
b21d0 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61 s limited to wha
b21e0 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 t can be stored
b21f0 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62 in.** lower 30 b
b2200 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20 its of a 32-bit
b2210 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a signed integer..
b2220 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
b2230 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65 returned will ne
b2240 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e ver be negative.
b2250 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76 Nor will it ev
b2260 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a er be greater.**
b2270 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c than the actual
b2280 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 length of the s
b2290 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79 tring. For very
b22a0 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67 long strings (g
b22b0 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31 reater.** than 1
b22c0 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72 GiB) the value r
b22d0 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65 eturned might be
b22e0 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74 less than the t
b22f0 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 rue string lengt
b2300 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 h..*/.SQLITE_PRI
b2310 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b2320 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 Strlen30(const c
b2330 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 har *z){. const
b2340 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 char *z2 = z;.
b2350 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 if( z==0 ) retu
b2360 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a rn 0;. while( *
b2370 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 z2 ){ z2++; }.
b2380 72 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66 return 0x3ffffff
b2390 66 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a f & (int)(z2 - z
b23a0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
b23b0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
b23c0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 error code and e
b23d0 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 rror string for
b23e0 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 the sqlite.** ha
b23f0 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65 ndle "db". The e
b2400 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 rror code is set
b2410 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a to "err_code"..
b2420 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e **.** If it is n
b2430 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 ot NULL, string
b2440 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65 zFormat specifie
b2450 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 s the format of
b2460 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 the.** error str
b2470 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65 ing in the style
b2480 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66 of the printf f
b2490 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f unctions: The fo
b24a0 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 llowing.** forma
b24b0 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 t characters are
b24c0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 allowed:.**.**
b24d0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 %s Ins
b24e0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 ert a string.**
b24f0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 %z A s
b2500 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c tring that shoul
b2510 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 d be freed after
b2520 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 use.** %d
b2530 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 Insert an i
b2540 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 nteger.** %
b2550 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 T Insert a
b2560 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 token.** %S
b2570 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 Insert the
b2580 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
b2590 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a f a SrcList.**.*
b25a0 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e * zFormat and an
b25b0 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 y string tokens
b25c0 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 that follow it a
b25d0 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 re assumed to be
b25e0 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 .** encoded in U
b25f0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 TF-8..**.** To c
b2600 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65 lear the most re
b2610 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73 cent error for s
b2620 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62 qlite handle "db
b2630 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a ", sqlite3Error.
b2640 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c ** should be cal
b2650 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64 led with err_cod
b2660 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f e set to SQLITE_
b2670 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 OK and zFormat s
b2680 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a et.** to NULL..*
b2690 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b26a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 void sqlite3Err
b26b0 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 or(sqlite3 *db,
b26c0 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f int err_code, co
b26d0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
b26e0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 t, ...){. if( d
b26f0 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c b && (db->pErr |
b2700 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 | (db->pErr = sq
b2710 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 lite3ValueNew(db
b2720 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 ))!=0) ){. db
b2730 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f ->errCode = err_
b2740 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 code;. if( zF
b2750 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63 ormat ){. c
b2760 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 har *z;. va
b2770 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20 _list ap;.
b2780 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
b2790 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d rmat);. z =
b27a0 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 sqlite3VMPrintf
b27b0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 (db, zFormat, ap
b27c0 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28 );. va_end(
b27d0 61 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ap);. sqlit
b27e0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 e3ValueSetStr(db
b27f0 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 ->pErr, -1, z, S
b2800 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
b2810 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 TE_DYNAMIC);.
b2820 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
b2830 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 lite3ValueSetStr
b2840 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c (db->pErr, 0, 0,
b2850 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
b2860 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
b2870 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
b2880 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 Add an error me
b2890 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d ssage to pParse-
b28a0 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 >zErrMsg and inc
b28b0 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e rement pParse->n
b28c0 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c Err..** The foll
b28d0 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 owing formatting
b28e0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
b28f0 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 allowed:.**.**
b2900 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 %s Inse
b2910 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 rt a string.**
b2920 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 %z A st
b2930 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 ring that should
b2940 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20 be freed after
b2950 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 use.** %d
b2960 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e Insert an in
b2970 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 teger.** %T
b2980 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 Insert a t
b2990 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 oken.** %S
b29a0 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 Insert the
b29b0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 first element of
b29c0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a a SrcList.**.**
b29d0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
b29e0 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f hould be used to
b29f0 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f report any erro
b2a00 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 r that occurs wh
b2a10 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e ilst.** compilin
b2a20 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 g an SQL stateme
b2a30 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 nt (i.e. within
b2a40 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 sqlite3_prepare(
b2a50 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 )). The.** last
b2a60 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 thing the sqlite
b2a70 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63 3_prepare() func
b2a80 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 tion does is cop
b2a90 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 y the error.** s
b2aa0 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 75 tored by this fu
b2ab0 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 nction into the
b2ac0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
b2ad0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 72 using sqlite3Err
b2ae0 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f or()..** Functio
b2af0 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 n sqlite3Error()
b2b00 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 should be used
b2b10 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 during statement
b2b20 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 execution.** (s
b2b30 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 74 qlite3_step() et
b2b40 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 c.)..*/.SQLITE_P
b2b50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b2b60 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 te3ErrorMsg(Pars
b2b70 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 e *pParse, const
b2b80 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
b2b90 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
b2ba0 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ap;. sqlite3 *d
b2bb0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
b2bc0 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b pParse->nErr++
b2bd0 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
b2be0 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 e(db, pParse->zE
b2bf0 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 rrMsg);. va_sta
b2c00 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
b2c10 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d . pParse->zErrM
b2c20 73 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 sg = sqlite3VMPr
b2c30 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
b2c40 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
b2c50 61 70 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 ap);. pParse->r
b2c60 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
b2c70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 ;.}../*.** Clear
b2c80 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
b2c90 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66 ge in pParse, if
b2ca0 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 any.*/.SQLITE_P
b2cb0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
b2cc0 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 te3ErrorClear(Pa
b2cd0 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 rse *pParse){.
b2ce0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 sqlite3DbFree(pP
b2cf0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 arse->db, pParse
b2d00 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 ->zErrMsg);. pP
b2d10 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
b2d20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 0;. pParse->nEr
b2d30 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r = 0;.}../*.**
b2d40 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 Convert an SQL-s
b2d50 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 tyle quoted stri
b2d60 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c ng into a normal
b2d70 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 string by remov
b2d80 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 ing.** the quote
b2d90 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 characters. Th
b2da0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
b2db0 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 done in-place.
b2dc0 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 If the.** input
b2dd0 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 does not begin w
b2de0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 ith a quote char
b2df0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 acter, then this
b2e00 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 routine.** is a
b2e10 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 no-op..**.** Th
b2e20 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d e input string m
b2e30 75 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d ust be zero-term
b2e40 69 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a inated. A new z
b2e50 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a ero-terminator.*
b2e60 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 * is added to th
b2e70 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e e dequoted strin
b2e80 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 g..**.** The ret
b2e90 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20 urn value is -1
b2ea0 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 if no dequoting
b2eb0 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65 occurs or the le
b2ec0 6e 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 ngth of the.** d
b2ed0 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20 equoted string,
b2ee0 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 exclusive of the
b2ef0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 zero terminator
b2f00 2c 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64 , if dequoting d
b2f10 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a oes.** occur..**
b2f20 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a .** 2002-Feb-14:
b2f30 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
b2f40 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d extended to rem
b2f50 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74 ove MS-Access st
b2f60 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20 yle.** brackets
b2f70 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e from around iden
b2f80 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61 tifers. For exa
b2f90 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 mple: "[a-b-c]"
b2fa0 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 becomes.** "a-b
b2fb0 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 -c"..*/.SQLITE_P
b2fc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b2fd0 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a e3Dequote(char *
b2fe0 7a 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65 z){. char quote
b2ff0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 ;. int i, j;.
b3000 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
b3010 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20 n -1;. quote =
b3020 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 z[0];. switch(
b3030 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 quote ){. cas
b3040 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a e '\'': break;.
b3050 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 case '"':
b3060 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
b3070 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 '`': break;
b3080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b3090 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 For MySQL compat
b30a0 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 ibility */. c
b30b0 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 ase '[': quote
b30c0 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 = ']'; break;
b30d0 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 /* For MS SqlSe
b30e0 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 rver compatibili
b30f0 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c ty */. defaul
b3100 74 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b t: return -1;
b3110 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 . }. for(i=1,
b3120 6a 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d j=0; ALWAYS(z[i]
b3130 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 ); i++){. if(
b3140 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a z[i]==quote ){.
b3150 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d if( z[i+1]
b3160 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 ==quote ){.
b3170 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 z[j++] = quot
b3180 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a e;. i++;.
b3190 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
b31a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
b31b0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
b31c0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b z[j++] = z[
b31d0 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 i];. }. }.
b31e0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 z[j] = 0;. retu
b31f0 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 rn j;.}../* Conv
b3200 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e enient short-han
b3210 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70 d */.#define Upp
b3220 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65 erToLower sqlite
b3230 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 3UpperToLower../
b3240 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d *.** Some system
b3250 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29 s have stricmp()
b3260 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73 . Others have s
b3270 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65 trcasecmp(). Be
b3280 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69 cause.** there i
b3290 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79 s no consistency
b32a0 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65 , we will define
b32b0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c our own..*/.SQL
b32c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b32d0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 sqlite3StrICmp(c
b32e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 onst char *zLeft
b32f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 , const char *zR
b3300 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 ight){. registe
b3310 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
b3320 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
b3330 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
b3340 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
b3350 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
b3360 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 ght;. while( *a
b3370 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f !=0 && UpperToLo
b3380 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f wer[*a]==UpperTo
b3390 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b Lower[*b]){ a++;
b33a0 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e b++; }. return
b33b0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
b33c0 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 ] - UpperToLower
b33d0 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 [*b];.}.SQLITE_A
b33e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
b33f0 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 trnicmp(const ch
b3400 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 ar *zLeft, const
b3410 20 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 char *zRight, i
b3420 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 nt N){. registe
b3430 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 r unsigned char
b3440 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 *a, *b;. a = (u
b3450 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a nsigned char *)z
b3460 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 Left;. b = (uns
b3470 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 igned char *)zRi
b3480 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d ght;. while( N-
b3490 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 - > 0 && *a!=0 &
b34a0 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a & UpperToLower[*
b34b0 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 a]==UpperToLower
b34c0 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b [*b]){ a++; b++;
b34d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 }. return N<0
b34e0 3f 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 ? 0 : UpperToLow
b34f0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f er[*a] - UpperTo
b3500 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a Lower[*b];.}../*
b3510 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
b3520 69 66 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e if z is a pure n
b3530 75 6d 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 umeric string.
b3540 52 65 74 75 72 6e 20 46 41 4c 53 45 20 61 6e 64 Return FALSE and
b3550 20 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e leave.** *realn
b3560 75 6d 20 75 6e 63 68 61 6e 67 65 64 20 69 66 20 um unchanged if
b3570 74 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 the string conta
b3580 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 ins any characte
b3590 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 0a 2a r which is not.*
b35a0 2a 20 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 * part of a numb
b35b0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 er..**.** If the
b35c0 20 73 74 72 69 6e 67 20 69 73 20 70 75 72 65 20 string is pure
b35d0 6e 75 6d 65 72 69 63 2c 20 73 65 74 20 2a 72 65 numeric, set *re
b35e0 61 6c 6e 75 6d 20 74 6f 20 54 52 55 45 20 69 66 alnum to TRUE if
b35f0 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 63 the string.** c
b3600 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20 ontains the '.'
b3610 63 68 61 72 61 63 74 65 72 20 6f 72 20 61 6e 20 character or an
b3620 22 45 2b 30 30 30 22 20 73 74 79 6c 65 20 65 78 "E+000" style ex
b3630 70 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 73 75 66 ponentiation suf
b3640 66 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 fix..** Otherwis
b3650 65 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 e set *realnum t
b3660 6f 20 46 41 4c 53 45 2e 20 20 4e 6f 74 65 20 74 o FALSE. Note t
b3670 68 61 74 20 6a 75 73 74 20 62 65 63 61 75 65 20 hat just becaue
b3680 2a 72 65 61 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66 *realnum is.** f
b3690 61 6c 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 alse does not me
b36a0 61 6e 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 an that the numb
b36b0 65 72 20 63 61 6e 20 62 65 20 73 75 63 63 65 73 er can be succes
b36c0 73 66 75 6c 6c 79 20 63 6f 6e 76 65 72 74 65 64 sfully converted
b36d0 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65 into.** an inte
b36e0 67 65 72 20 2d 20 69 74 20 6d 69 67 68 74 20 62 ger - it might b
b36f0 65 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a e too big..**.**
b3700 20 41 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 An empty string
b3710 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e is considered n
b3720 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 on-numeric..*/.S
b3730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b3740 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 t sqlite3IsNumbe
b3750 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c r(const char *z,
b3760 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 int *realnum, u
b3770 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 8 enc){. int in
b3780 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 cr = (enc==SQLIT
b3790 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 E_UTF8?1:2);. i
b37a0 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 f( enc==SQLITE_U
b37b0 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 TF16BE ) z++;.
b37c0 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a if( *z=='-' || *
b37d0 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e z=='+' ) z += in
b37e0 63 72 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 cr;. if( !sqlit
b37f0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b e3Isdigit(*z) ){
b3800 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b3810 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a }. z += incr;.
b3820 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a *realnum = 0;.
b3830 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
b3840 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a Isdigit(*z) ){ z
b3850 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 += incr; }. if
b3860 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 ( *z=='.' ){.
b3870 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 z += incr;.
b3880 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 if( !sqlite3Isdi
b3890 67 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e git(*z) ) return
b38a0 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 0;. while( s
b38b0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b38c0 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 ) ){ z += incr;
b38d0 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d }. *realnum =
b38e0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 1;. }. if( *z
b38f0 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 =='e' || *z=='E'
b3900 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 ){. z += inc
b3910 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 r;. if( *z=='
b3920 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 +' || *z=='-' )
b3930 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 z += incr;. i
b3940 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 f( !sqlite3Isdig
b3950 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 it(*z) ) return
b3960 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 0;. while( sq
b3970 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
b3980 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d ){ z += incr; }
b3990 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 . *realnum =
b39a0 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
b39b0 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 *z==0;.}../*.**
b39c0 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69 The string z[] i
b39d0 73 20 61 6e 20 41 53 43 49 49 20 72 65 70 72 65 s an ASCII repre
b39e0 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 72 sentation of a r
b39f0 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 eal number..** C
b3a00 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 69 onvert this stri
b3a10 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a ng to a double..
b3a20 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
b3a30 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 ne assumes that
b3a40 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 20 z[] really is a
b3a50 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 49 valid number. I
b3a60 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 f it.** is not,
b3a70 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e the result is un
b3a80 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 defined..**.** T
b3a90 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
b3aa0 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 sed instead of t
b3ab0 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 he library atof(
b3ac0 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 75 ) function becau
b3ad0 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 se.** the librar
b3ae0 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 77 y atof() might w
b3af0 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 61 ant to use "," a
b3b00 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f s the decimal po
b3b10 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f int instead.** o
b3b20 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 20 f "." depending
b3b30 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 73 on how locale is
b3b40 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 20 set. But that
b3b50 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 would cause prob
b3b60 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e lems.** for SQL.
b3b70 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e So this routin
b3b80 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 2e e always uses ".
b3b90 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 " regardless of
b3ba0 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 locale..*/.SQLIT
b3bb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b3bc0 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 lite3AtoF(const
b3bd0 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 char *z, double
b3be0 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e 64 *pResult){.#ifnd
b3bf0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
b3c00 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
b3c10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 67 const char *zBeg
b3c20 69 6e 20 3d 20 7a 3b 0a 20 20 2f 2a 20 73 69 67 in = z;. /* sig
b3c30 6e 20 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 20 n * significand
b3c40 2a 20 28 31 30 20 5e 20 28 65 73 69 67 6e 20 2a * (10 ^ (esign *
b3c50 20 65 78 70 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20 exponent)) */.
b3c60 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 20 20 int sign = 1;
b3c70 20 2f 2a 20 73 69 67 6e 20 6f 66 20 73 69 67 6e /* sign of sign
b3c80 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 36 34 ificand */. i64
b3c90 20 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 s = 0; /*
b3ca0 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 significand */.
b3cb0 20 69 6e 74 20 64 20 3d 20 30 3b 20 20 20 20 20 int d = 0;
b3cc0 20 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e /* adjust expon
b3cd0 65 6e 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67 ent for shifting
b3ce0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a decimal point *
b3cf0 2f 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 /. int esign =
b3d00 31 3b 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65 1; /* sign of e
b3d10 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 xponent */. int
b3d20 20 65 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 e = 0; /*
b3d30 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f exponent */. do
b3d40 75 62 6c 65 20 72 65 73 75 6c 74 3b 0a 20 20 69 uble result;. i
b3d50 6e 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b 0a nt nDigits = 0;.
b3d60 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 . /* skip leadi
b3d70 6e 67 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77 ng spaces */. w
b3d80 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
b3d90 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a pace(*z) ) z++;.
b3da0 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 /* get sign of
b3db0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a significand */.
b3dc0 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b if( *z=='-' ){
b3dd0 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a . sign = -1;.
b3de0 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 z++;. }else
b3df0 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a if( *z=='+' ){.
b3e00 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f z++;. }. /
b3e10 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a * skip leading z
b3e20 65 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 eroes */. while
b3e30 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 2b ( z[0]=='0' ) z+
b3e40 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 +, nDigits++;..
b3e50 20 2f 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67 /* copy max sig
b3e60 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 nificant digits
b3e70 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a to significand *
b3e80 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 /. while( sqlit
b3e90 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 e3Isdigit(*z) &&
b3ea0 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 s<((LARGEST_INT
b3eb0 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 64-9)/10) ){.
b3ec0 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20 s = s*10 + (*z
b3ed0 2d 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c - '0');. z++,
b3ee0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a nDigits++;. }.
b3ef0 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 /* skip non-si
b3f00 67 6e 69 66 69 63 61 6e 74 20 73 69 67 6e 69 66 gnificant signif
b3f10 69 63 61 6e 64 20 64 69 67 69 74 73 0a 20 20 2a icand digits. *
b3f20 2a 20 28 69 6e 63 72 65 61 73 65 20 65 78 70 6f * (increase expo
b3f30 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 nent by d to shi
b3f40 66 74 20 64 65 63 69 6d 61 6c 20 6c 65 66 74 29 ft decimal left)
b3f50 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c */. while( sql
b3f60 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 ite3Isdigit(*z)
b3f70 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b ) z++, nDigits++
b3f80 2c 20 64 2b 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20 , d++;.. /* if
b3f90 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73 decimal point is
b3fa0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 present */. if
b3fb0 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 ( *z=='.' ){.
b3fc0 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 63 6f 70 z++;. /* cop
b3fd0 79 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66 y digits from af
b3fe0 74 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73 ter decimal to s
b3ff0 69 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 20 2a ignificand. *
b4000 2a 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f * (decrease expo
b4010 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 nent by d to shi
b4020 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74 ft decimal right
b4030 29 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 ) */. while(
b4040 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b4050 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53 z) && s<((LARGES
b4060 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29 T_INT64-9)/10) )
b4070 7b 0a 20 20 20 20 20 20 73 20 3d 20 73 2a 31 30 {. s = s*10
b4080 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 + (*z - '0');.
b4090 20 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 z++, nDigit
b40a0 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a s++, d--;. }.
b40b0 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d /* skip non-
b40c0 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 significant digi
b40d0 74 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 ts */. while(
b40e0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
b40f0 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 *z) ) z++, nDigi
b4100 74 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ts++;. }.. /*
b4110 69 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 70 if exponent is p
b4120 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 resent */. if(
b4130 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 *z=='e' || *z=='
b4140 45 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 E' ){. z++;.
b4150 20 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f /* get sign o
b4160 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 f exponent */.
b4170 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b if( *z=='-' ){
b4180 0a 20 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d . esign = -
b4190 31 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 1;. z++;.
b41a0 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d }else if( *z==
b41b0 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b '+' ){. z++
b41c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 ;. }. /* c
b41d0 6f 70 79 20 64 69 67 69 74 73 20 74 6f 20 65 78 opy digits to ex
b41e0 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 77 68 ponent */. wh
b41f0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
b4200 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 git(*z) ){.
b4210 20 65 20 3d 20 65 2a 31 30 20 2b 20 28 2a 7a 20 e = e*10 + (*z
b4220 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b - '0');. z+
b4230 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 +;. }. }..
b4240 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 /* adjust expone
b4250 6e 74 20 62 79 20 64 2c 20 61 6e 64 20 75 70 64 nt by d, and upd
b4260 61 74 65 20 73 69 67 6e 20 2a 2f 0a 20 20 65 20 ate sign */. e
b4270 3d 20 28 65 2a 65 73 69 67 6e 29 20 2b 20 64 3b = (e*esign) + d;
b4280 0a 20 20 69 66 28 20 65 3c 30 20 29 20 7b 0a 20 . if( e<0 ) {.
b4290 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 esign = -1;.
b42a0 20 20 20 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20 e *= -1;. }
b42b0 65 6c 73 65 20 7b 0a 20 20 20 20 65 73 69 67 6e else {. esign
b42c0 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 1;. }.. /*
b42d0 69 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64 if 0 significand
b42e0 20 2a 2f 0a 20 20 69 66 28 20 21 73 20 29 20 7b */. if( !s ) {
b42f0 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 49 . /* In the I
b4300 45 45 45 20 37 35 34 20 73 74 61 6e 64 61 72 64 EEE 754 standard
b4310 2c 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65 64 , zero is signed
b4320 2e 0a 20 20 20 20 2a 2a 20 41 64 64 20 74 68 65 .. ** Add the
b4330 20 73 69 67 6e 20 69 66 20 77 65 27 76 65 20 73 sign if we've s
b4340 65 65 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 een at least one
b4350 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 72 65 digit */. re
b4360 73 75 6c 74 20 3d 20 28 73 69 67 6e 3c 30 20 26 sult = (sign<0 &
b4370 26 20 6e 44 69 67 69 74 73 29 20 3f 20 2d 28 64 & nDigits) ? -(d
b4380 6f 75 62 6c 65 29 30 20 3a 20 28 64 6f 75 62 6c ouble)0 : (doubl
b4390 65 29 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a e)0;. } else {.
b43a0 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 /* attempt t
b43b0 6f 20 72 65 64 75 63 65 20 65 78 70 6f 6e 65 6e o reduce exponen
b43c0 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 73 69 t */. if( esi
b43d0 67 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 77 68 gn>0 ){. wh
b43e0 69 6c 65 28 20 73 3c 28 4c 41 52 47 45 53 54 5f ile( s<(LARGEST_
b43f0 49 4e 54 36 34 2f 31 30 29 20 26 26 20 65 3e 30 INT64/10) && e>0
b4400 20 29 20 65 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20 ) e--,s*=10;.
b4410 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 }else{. w
b4420 68 69 6c 65 28 20 21 28 73 25 31 30 29 20 26 26 hile( !(s%10) &&
b4430 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30 e>0 ) e--,s/=10
b4440 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
b4450 61 64 6a 75 73 74 20 74 68 65 20 73 69 67 6e 20 adjust the sign
b4460 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a of significand *
b4470 2f 0a 20 20 20 20 73 20 3d 20 73 69 67 6e 3c 30 /. s = sign<0
b4480 20 3f 20 2d 73 20 3a 20 73 3b 0a 0a 20 20 20 20 ? -s : s;..
b4490 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20 /* if exponent,
b44a0 73 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e scale significan
b44b0 64 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 d as appropriate
b44c0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 . ** and stor
b44d0 65 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0a e in result. */.
b44e0 20 20 20 20 69 66 28 20 65 20 29 7b 0a 20 20 20 if( e ){.
b44f0 20 20 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 20 double scale
b4500 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20 = 1.0;. /*
b4510 61 74 74 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c attempt to handl
b4520 65 20 65 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c e extremely smal
b4530 6c 2f 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20 l/large numbers
b4540 62 65 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 better */.
b4550 69 66 28 20 65 3e 33 30 37 20 26 26 20 65 3c 33 if( e>307 && e<3
b4560 34 32 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 42 ){. wh
b4570 69 6c 65 28 20 65 25 33 30 38 20 29 20 7b 20 73 ile( e%308 ) { s
b4580 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 cale *= 1.0e+1;
b4590 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 e -= 1; }.
b45a0 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b if( esign<0 ){
b45b0 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c . resul
b45c0 74 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 t = s / scale;.
b45d0 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 result
b45e0 2f 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 /= 1.0e+308;.
b45f0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
b4600 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 result = s
b4610 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 * scale;.
b4620 20 20 20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e result *= 1.
b4630 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 0e+308;.
b4640 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
b4650 20 20 20 20 20 20 20 2f 2a 20 31 2e 30 65 2b 32 /* 1.0e+2
b4660 32 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 2 is the largest
b4670 20 70 6f 77 65 72 20 6f 66 20 31 30 20 74 68 61 power of 10 tha
b4680 6e 20 63 61 6e 20 62 65 20 0a 20 20 20 20 20 20 n can be .
b4690 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 ** represented
b46a0 20 65 78 61 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 exactly. */.
b46b0 20 20 20 20 20 77 68 69 6c 65 28 20 65 25 32 32 while( e%22
b46c0 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e ) { scale *= 1.
b46d0 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 0e+1; e -= 1; }.
b46e0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 while( e
b46f0 3e 30 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 >0 ) { scale *=
b4700 31 2e 30 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32 1.0e+22; e -= 22
b4710 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
b4720 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 esign<0 ){.
b4730 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 result = s
b4740 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 / scale;.
b4750 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
b4760 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 result = s * s
b4770 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a cale;. }.
b4780 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c }. } el
b4790 73 65 20 7b 0a 20 20 20 20 20 20 72 65 73 75 6c se {. resul
b47a0 74 20 3d 20 28 64 6f 75 62 6c 65 29 73 3b 0a 20 t = (double)s;.
b47b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73 }. }.. /* s
b47c0 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
b47d0 2a 2f 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 */. *pResult =
b47e0 72 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65 result;.. /* re
b47f0 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 turn number of c
b4800 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20 2a haracters used *
b4810 2f 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 /. return (int)
b4820 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65 (z - zBegin);.#e
b4830 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 6c lse. return sql
b4840 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 ite3Atoi64(z, pR
b4850 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f esult);.#endif /
b4860 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c * SQLITE_OMIT_FL
b4870 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a OATING_POINT */.
b4880 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 }../*.** Compare
b4890 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65 the 19-characte
b48a0 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 r string zNum ag
b48b0 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72 ainst the text r
b48c0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a epresentation.**
b48d0 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 value 2^63: 92
b48e0 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 2337203685477580
b48f0 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74 8. Return negat
b4900 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f ive, zero, or po
b4910 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 sitive.** if zNu
b4920 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 m is less than,
b4930 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 equal to, or gre
b4940 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 ater than the st
b4950 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 ring..**.** Unli
b4960 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 ke memcmp() this
b4970 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 72 routine is guar
b4980 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 6e anteed to return
b4990 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a the difference.
b49a0 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 73 ** in the values
b49b0 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 67 of the last dig
b49c0 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 it if the only d
b49d0 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e 20 ifference is in
b49e0 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 the.** last digi
b49f0 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d t. So, for exam
b4a00 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 ple,.**.**
b4a10 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39 compare2pow63("9
b4a20 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
b4a30 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 00").**.** will
b4a40 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 return -8..*/.st
b4a50 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 atic int compare
b4a60 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 61 2pow63(const cha
b4a70 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 r *zNum){. int
b4a80 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 c;. c = memcmp(
b4a90 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 36 zNum,"9223372036
b4aa0 38 35 34 37 37 35 38 30 22 2c 31 38 29 2a 31 30 85477580",18)*10
b4ab0 3b 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a ;. if( c==0 ){.
b4ac0 20 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d c = zNum[18]
b4ad0 20 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 - '8';. }. re
b4ae0 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn c;.}.../*.*
b4af0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 * Return TRUE if
b4b00 20 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 zNum is a 64-bi
b4b10 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
b4b20 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 and write.** th
b4b30 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 e value of the i
b4b40 6e 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 nteger into *pNu
b4b50 6d 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e m. If zNum is n
b4b60 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a ot an integer.**
b4b70 20 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 or is an intege
b4b80 72 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 r that is too la
b4b90 72 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 rge to be expres
b4ba0 73 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 sed with 64 bits
b4bb0 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e ,.** then return
b4bc0 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 false..**.** Wh
b4bd0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
b4be0 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 was originally w
b4bf0 72 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 ritten it dealt
b4c00 77 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d with only.** 32-
b4c10 62 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 bit numbers. At
b4c20 20 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 that time, it w
b4c30 61 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 as much faster t
b4c40 68 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 han the.** atoi(
b4c50 29 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e ) library routin
b4c60 65 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e e in RedHat 7.2.
b4c70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b4c80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 TE int sqlite3At
b4c90 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 oi64(const char
b4ca0 2a 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d *zNum, i64 *pNum
b4cb0 29 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a ){. i64 v = 0;.
b4cc0 20 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 int neg;. int
b4cd0 20 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 i, c;. const c
b4ce0 68 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 har *zStart;. w
b4cf0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 hile( sqlite3Iss
b4d00 70 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e pace(*zNum) ) zN
b4d10 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 um++;. if( *zNu
b4d20 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 m=='-' ){. ne
b4d30 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b g = 1;. zNum+
b4d40 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a +;. }else if( *
b4d50 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 zNum=='+' ){.
b4d60 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e neg = 0;. zN
b4d70 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 um++;. }else{.
b4d80 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a neg = 0;. }.
b4d90 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b zStart = zNum;
b4da0 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 . while( zNum[0
b4db0 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b ]=='0' ){ zNum++
b4dc0 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 ; } /* Skip over
b4dd0 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 leading zeros.
b4de0 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a Ticket #2454 */.
b4df0 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e for(i=0; (c=zN
b4e00 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 um[i])>='0' && c
b4e10 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 <='9'; i++){.
b4e20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 v = v*10 + c -
b4e30 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d '0';. }. *pNum
b4e40 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b = neg ? -v : v;
b4e50 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 . if( c!=0 || (
b4e60 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d i==0 && zStart==
b4e70 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b zNum) || i>19 ){
b4e80 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 . /* zNum is
b4e90 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e empty or contain
b4ea0 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 s non-numeric te
b4eb0 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a xt or is longer.
b4ec0 20 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 ** than 19 d
b4ed0 69 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 igits (thus guar
b4ee0 61 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 anting that it i
b4ef0 73 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a s too large) */.
b4f00 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b4f10 7d 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 }else if( i<19 )
b4f20 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 {. /* Less th
b4f30 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f an 19 digits, so
b4f40 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 we know that it
b4f50 20 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 fits in 64 bits
b4f60 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 */. return 1
b4f70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
b4f80 2a 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 * 19-digit numbe
b4f90 72 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 rs must be no la
b4fa0 72 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 rger than 922337
b4fb0 32 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 2036854775807 if
b4fc0 20 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a positive. **
b4fd0 20 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 or 922337203685
b4fe0 34 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 4775808 if negat
b4ff0 69 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 ive. Note that
b5000 39 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 9223372036854665
b5010 38 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 808. ** is 2^
b5020 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 63. */. retur
b5030 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 n compare2pow63(
b5040 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d zNum)<neg;. }.}
b5050 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 ../*.** The stri
b5060 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e ng zNum represen
b5070 74 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69 ts an unsigned i
b5080 6e 74 65 67 65 72 2e 20 20 54 68 65 20 7a 4e 75 nteger. The zNu
b5090 6d 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 m string.** cons
b50a0 69 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72 20 6d ists of one or m
b50b0 6f 72 65 20 64 69 67 69 74 20 63 68 61 72 61 63 ore digit charac
b50c0 74 65 72 73 20 61 6e 64 20 69 73 20 74 65 72 6d ters and is term
b50d0 69 6e 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 7a inated by.** a z
b50e0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 20 20 ero character.
b50f0 41 6e 79 20 73 74 72 61 79 20 63 68 61 72 61 63 Any stray charac
b5100 74 65 72 73 20 69 6e 20 7a 4e 75 6d 20 72 65 73 ters in zNum res
b5110 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 ult in undefined
b5120 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a .** behavior..**
b5130 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 73 69 67 .** If the unsig
b5140 6e 65 64 20 69 6e 74 65 67 65 72 20 74 68 61 74 ned integer that
b5150 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 zNum represents
b5160 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a will fit in a.*
b5170 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 * 64-bit signed
b5180 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 integer, return
b5190 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65 TRUE. Otherwise
b51a0 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a return FALSE..*
b51b0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 67 46 *.** If the negF
b51c0 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 lag parameter is
b51d0 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e true, that mean
b51e0 73 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 61 6c s that zNum real
b51f0 6c 79 20 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a ly represents.**
b5200 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 a negative numb
b5210 65 72 2e 20 20 28 54 68 65 20 6c 65 61 64 69 6e er. (The leadin
b5220 67 20 22 2d 22 20 69 73 20 6f 6d 69 74 74 65 64 g "-" is omitted
b5230 20 66 72 6f 6d 20 7a 4e 75 6d 2e 29 20 20 54 68 from zNum.) Th
b5240 69 73 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 is.** parameter
b5250 69 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74 is needed to det
b5260 65 72 6d 69 6e 65 20 61 20 62 6f 75 6e 64 61 72 ermine a boundar
b5270 79 20 63 61 73 65 2e 20 20 41 20 73 74 72 69 6e y case. A strin
b5280 67 0a 2a 2a 20 6f 66 20 22 39 32 32 33 33 37 33 g.** of "9223373
b5290 30 33 36 38 35 34 37 37 35 38 30 38 22 20 72 65 036854775808" re
b52a0 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 6e turns false if n
b52b0 65 67 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20 egFlag is false
b52c0 6f 72 20 74 72 75 65 0a 2a 2a 20 69 66 20 6e 65 or true.** if ne
b52d0 67 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a gFlag is true..*
b52e0 2a 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 7a 65 72 *.** Leading zer
b52f0 6f 73 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a os are ignored..
b5300 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b5310 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 E int sqlite3Fit
b5320 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 sIn64Bits(const
b5330 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 char *zNum, int
b5340 6e 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 negFlag){. int
b5350 69 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 i;. int neg = 0
b5360 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 75 ;.. assert( zNu
b5370 6d 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 m[0]>='0' && zNu
b5380 6d 5b 30 5d 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 m[0]<='9' ); /*
b5390 7a 4e 75 6d 20 69 73 20 61 6e 20 75 6e 73 69 67 zNum is an unsig
b53a0 6e 65 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 ned number */..
b53b0 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 6e if( negFlag ) n
b53c0 65 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68 eg = 1-neg;. wh
b53d0 69 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 ile( *zNum=='0'
b53e0 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 ){. zNum++;
b53f0 20 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 /* Skip leading
b5400 20 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 zeros. Ticket
b5410 23 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 #2454 */. }. f
b5420 6f 72 28 69 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b or(i=0; zNum[i];
b5430 20 69 2b 2b 29 7b 20 61 73 73 65 72 74 28 20 7a i++){ assert( z
b5440 4e 75 6d 5b 69 5d 3e 3d 27 30 27 20 26 26 20 7a Num[i]>='0' && z
b5450 4e 75 6d 5b 69 5d 3c 3d 27 39 27 20 29 3b 20 7d Num[i]<='9' ); }
b5460 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 . if( i<19 ){.
b5470 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 /* Guaranteed
b5480 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 to fit if less
b5490 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a than 19 digits *
b54a0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a /. return 1;.
b54b0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 }else if( i>19
b54c0 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 ){. /* Guara
b54d0 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 nteed to be too
b54e0 62 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 big if greater t
b54f0 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f han 19 digits */
b5500 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b5510 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 }else{. /* C
b5520 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 ompare against 2
b5530 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 ^63. */. retu
b5540 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 rn compare2pow63
b5550 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a (zNum)<neg;. }.
b5560 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d }../*.** If zNum
b5570 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 represents an i
b5580 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c nteger that will
b5590 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c fit in 32-bits,
b55a0 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 then set.** *pV
b55b0 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 alue to that int
b55c0 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eger and return
b55d0 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 true. Otherwise
b55e0 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a return false..*
b55f0 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d *.** Any non-num
b5600 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 eric characters
b5610 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a that following z
b5620 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e Num are ignored.
b5630 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 .** This is diff
b5640 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 erent from sqlit
b5650 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 e3Atoi64() which
b5660 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a requires the.**
b5670 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f input number to
b5680 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
b5690 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
b56a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b56b0 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 e3GetInt32(const
b56c0 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 char *zNum, int
b56d0 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c *pValue){. sql
b56e0 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b ite_int64 v = 0;
b56f0 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 . int i, c;. i
b5700 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 nt neg = 0;. if
b5710 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 ( zNum[0]=='-' )
b5720 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 {. neg = 1;.
b5730 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c zNum++;. }el
b5740 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d se if( zNum[0]==
b5750 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b '+' ){. zNum+
b5760 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 +;. }. while(
b5770 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a zNum[0]=='0' ) z
b5780 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 Num++;. for(i=0
b5790 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a ; i<11 && (c = z
b57a0 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 Num[i] - '0')>=0
b57b0 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a && c<=9; i++){.
b57c0 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 v = v*10 + c
b57d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
b57e0 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 longest decimal
b57f0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
b5800 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 f a 32 bit integ
b5810 65 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a er is 10 digits:
b5820 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
b5830 20 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 123456789
b5840 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 0. ** 2^31
b5850 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 -> 2147483648.
b5860 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b */. if( i>10 ){
b5870 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b5880 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 }. if( v-neg>2
b5890 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 147483647 ){.
b58a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
b58b0 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 if( neg ){.
b58c0 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 v = -v;. }. *p
b58d0 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a Value = (int)v;.
b58e0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f return 1;.}../
b58f0 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c *.** The variabl
b5900 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
b5910 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 encoding is as
b5920 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b follows:.**.** K
b5930 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 EY:.** A
b5940 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 = 0xxxxxxx 7
b5950 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e bits of data an
b5960 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a d one flag bit.*
b5970 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 * B = 1x
b5980 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 xxxxxx 7 bits
b5990 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 of data and one
b59a0 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 flag bit.**
b59b0 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 C = xxxxxxx
b59c0 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 x 8 bits of d
b59d0 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 ata.**.** 7 bit
b59e0 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 s - A.** 14 bits
b59f0 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 - BA.** 21 bits
b5a00 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 - BBA.** 28 bit
b5a10 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 s - BBBA.** 35 b
b5a20 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 its - BBBBA.** 4
b5a30 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2 bits - BBBBBA.
b5a40 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 ** 49 bits - BBB
b5a50 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 BBBA.** 56 bits
b5a60 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 - BBBBBBBA.** 64
b5a70 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 bits - BBBBBBBB
b5a80 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 C.*/../*.** Writ
b5a90 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 e a 64-bit varia
b5aa0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
b5ab0 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 er to memory sta
b5ac0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a rting at p[0]..*
b5ad0 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 * The length of
b5ae0 64 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 data write will
b5af0 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 be between 1 and
b5b00 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 9 bytes. The n
b5b10 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 umber.** of byte
b5b20 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 s written is ret
b5b30 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 urned..**.** A v
b5b40 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
b5b50 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 nteger consists
b5b60 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 of the lower 7 b
b5b70 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 its of each byte
b5b80 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 .** for all byte
b5b90 73 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 s that have the
b5ba0 38 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 8th bit set and
b5bb0 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 one byte with th
b5bc0 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 e 8th.** bit cle
b5bd0 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 ar. Except, if
b5be0 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 we get to the 9t
b5bf0 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 h byte, it store
b5c00 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 s the full.** 8
b5c10 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 bits and is the
b5c20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 last byte..*/.SQ
b5c30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b5c40 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
b5c50 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t(unsigned char
b5c60 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e *p, u64 v){. in
b5c70 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 t i, j, n;. u8
b5c80 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 buf[10];. if( v
b5c90 20 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 & (((u64)0xff00
b5ca0 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 0000)<<32) ){.
b5cb0 20 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a p[8] = (u8)v;.
b5cc0 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 v >>= 8;.
b5cd0 20 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 for(i=7; i>=0;
b5ce0 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d i--){. p[i]
b5cf0 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37 = (u8)((v & 0x7
b5d00 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 f) | 0x80);.
b5d10 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d v >>= 7;. }
b5d20 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20 . return 9;.
b5d30 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a } . n = 0;.
b5d40 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b do{. buf[n+
b5d50 2b 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 +] = (u8)((v & 0
b5d60 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 x7f) | 0x80);.
b5d70 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 v >>= 7;. }wh
b5d80 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 ile( v!=0 );. b
b5d90 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 uf[0] &= 0x7f;.
b5da0 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b assert( n<=9 );
b5db0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d . for(i=0, j=n-
b5dc0 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 1; j>=0; j--, i+
b5dd0 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 +){. p[i] = b
b5de0 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 uf[j];. }. ret
b5df0 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn n;.}../*.**
b5e00 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
b5e10 61 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e a faster version
b5e20 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 of sqlite3PutVa
b5e30 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 rint() that only
b5e40 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 .** works for 32
b5e50 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e -bit positive in
b5e60 74 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 tegers and which
b5e70 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f is optimized fo
b5e80 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 r.** the common
b5e90 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e case of small in
b5ea0 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f tegers. A MACRO
b5eb0 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 version, putVar
b5ec0 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f int32,.** is pro
b5ed0 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 vided which inli
b5ee0 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 nes the single-b
b5ef0 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 yte case. All c
b5f00 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a ode should use.*
b5f10 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 * the MACRO vers
b5f20 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 ion as this func
b5f30 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 tion assumes the
b5f40 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 single-byte cas
b5f50 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 e has.** already
b5f60 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a been handled..*
b5f70 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b5f80 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 int sqlite3PutV
b5f90 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 arint32(unsigned
b5fa0 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 char *p, u32 v)
b5fb0 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 {.#ifndef putVar
b5fc0 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 int32. if( (v &
b5fd0 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 ~0x7f)==0 ){.
b5fe0 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 p[0] = v;.
b5ff0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 return 1;. }.#e
b6000 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 ndif. if( (v &
b6010 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 ~0x3fff)==0 ){.
b6020 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28 p[0] = (u8)((
b6030 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20 v>>7) | 0x80);.
b6040 20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 p[1] = (u8)(v
b6050 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65 & 0x7f);. re
b6060 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65 turn 2;. }. re
b6070 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56 turn sqlite3PutV
b6080 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a arint(p, v);.}..
b6090 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d /*.** Read a 64-
b60a0 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e bit variable-len
b60b0 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d gth integer from
b60c0 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 memory starting
b60d0 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 at p[0]..** Ret
b60e0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
b60f0 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 f bytes read. T
b6100 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
b6110 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c ed in *v..*/.SQL
b6120 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
b6130 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
b6140 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
b6150 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b har *p, u64 *v){
b6160 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 . u32 a,b,s;..
b6170 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a = *p;. /* a:
b6180 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p0 (unmasked) *
b6190 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 /. if (!(a&0x80
b61a0 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 )). {. *v =
b61b0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b a;. return 1;
b61c0 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 . }.. p++;. b
b61d0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 = *p;. /* b: p
b61e0 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 1 (unmasked) */.
b61f0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
b6200 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 . {. a &= 0x
b6210 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 7f;. a = a<<7
b6220 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b6230 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
b6240 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 turn 2;. }.. p
b6250 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
b6260 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
b6270 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 a: p0<<14 | p2
b6280 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b6290 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b62a0 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 {. a &= (0x7
b62b0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b62c0 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 b &= 0x7f;.
b62d0 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b62e0 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d a |= b;. *v =
b62f0 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 a;. return 3
b6300 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 ;. }.. /* CSE1
b6310 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 from below */.
b6320 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 a &= (0x7f<<14)
b6330 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a |(0x7f);. p++;.
b6340 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 b = b<<14;. b
b6350 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 |= *p;. /* b:
b6360 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d p1<<14 | p3 (unm
b6370 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b6380 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(b&0x80)). {.
b6390 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 b &= (0x7f<<1
b63a0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 4)|(0x7f);. /
b63b0 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 * moved CSE1 up
b63c0 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 */. /* a &= (
b63d0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b63e0 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c ; */. a = a<<
b63f0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b6400 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 *v = a;. r
b6410 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 eturn 4;. }..
b6420 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 /* a: p0<<14 | p
b6430 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2 (masked) */.
b6440 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
b6450 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
b6460 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20 /* 1:save off
b6470 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 p0<<21 | p1<<14
b6480 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 | p2<<7 | p3 (ma
b6490 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f sked) */. /* mo
b64a0 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 ved CSE1 up */.
b64b0 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c /* a &= (0x7f<<
b64c0 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 14)|(0x7f); */.
b64d0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 b &= (0x7f<<14)
b64e0 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61 |(0x7f);. s = a
b64f0 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 ;. /* s: p0<<14
b6500 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a | p2 (masked) *
b6510 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 /.. p++;. a =
b6520 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 a<<14;. a |= *p
b6530 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 ;. /* a: p0<<28
b6540 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 | p2<<14 | p4 (
b6550 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b6560 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
b6570 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 {. /* we can
b6580 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65 skip these cause
b6590 20 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65 they were (effe
b65a0 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 ctively) done ab
b65b0 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 ove in calc'ing
b65c0 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d s */. /* a &=
b65d0 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 (0x7f<<28)|(0x7
b65e0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a f<<14)|(0x7f); *
b65f0 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 /. /* b &= (0
b6600 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6610 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 */. b = b<<7
b6620 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b6630 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 s = s>>18;.
b6640 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c *v = ((u64)s)<<
b6650 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 32 | a;. retu
b6660 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 5;. }.. /*
b6670 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 2:save off p0<<2
b6680 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 1 | p1<<14 | p2<
b6690 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 <7 | p3 (masked)
b66a0 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a */. s = s<<7;.
b66b0 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 s |= b;. /* s
b66c0 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 : p0<<21 | p1<<1
b66d0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 4 | p2<<7 | p3 (
b66e0 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b masked) */.. p+
b66f0 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a +;. b = b<<14;.
b6700 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 b |= *p;. /*
b6710 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c b: p1<<28 | p3<<
b6720 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 14 | p5 (unmaske
b6730 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
b6740 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 0x80)). {. /
b6750 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 * we can skip th
b6760 69 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20 is cause it was
b6770 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f (effectively) do
b6780 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 ne above in calc
b6790 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 'ing s */. /*
b67a0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 b &= (0x7f<<28)
b67b0 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 |(0x7f<<14)|(0x7
b67c0 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 f); */. a &=
b67d0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b67e0 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b );. a = a<<7;
b67f0 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b6800 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 s = s>>18;.
b6810 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
b6820 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
b6830 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b n 6;. }.. p++;
b6840 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 . a = a<<14;.
b6850 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
b6860 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34 p2<<28 | p4<<14
b6870 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29 | p6 (unmasked)
b6880 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 */. if (!(a&0x
b6890 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 80)). {. a &
b68a0 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 = (0x1f<<28)|(0x
b68b0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b68c0 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c b &= (0x7f<<
b68d0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
b68e0 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 b = b<<7;. a
b68f0 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e |= b;. s = s>
b6900 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 >11;. *v = ((
b6910 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a u64)s)<<32 | a;.
b6920 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 return 7;.
b6930 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f }.. /* CSE2 fro
b6940 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 m below */. a &
b6950 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b6960 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 7f);. p++;. b
b6970 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 = b<<14;. b |=
b6980 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c *p;. /* b: p3<<
b6990 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37 28 | p5<<14 | p7
b69a0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b69b0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a if (!(b&0x80)).
b69c0 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 {. b &= (0x
b69d0 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 1f<<28)|(0x7f<<1
b69e0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 4)|(0x7f);. /
b69f0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 * moved CSE2 up
b6a00 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 */. /* a &= (
b6a10 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b6a20 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c ; */. a = a<<
b6a30 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 7;. a |= b;.
b6a40 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 s = s>>4;.
b6a50 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c *v = ((u64)s)<<
b6a60 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 32 | a;. retu
b6a70 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b rn 8;. }.. p++
b6a80 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 ;. a = a<<15;.
b6a90 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 a |= *p;. /* a
b6aa0 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 : p4<<29 | p6<<1
b6ab0 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64 5 | p8 (unmasked
b6ac0 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 ) */.. /* moved
b6ad0 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a CSE2 up */. /*
b6ae0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 a &= (0x7f<<29)
b6af0 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 |(0x7f<<15)|(0xf
b6b00 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 f); */. b &= (0
b6b10 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6b20 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 . b = b<<8;. a
b6b30 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c |= b;.. s = s<
b6b40 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b <4;. b = p[-4];
b6b50 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 . b &= 0x7f;.
b6b60 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d b = b>>3;. s |=
b6b70 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36 b;.. *v = ((u6
b6b80 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 4)s)<<32 | a;..
b6b90 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a return 9;.}../*
b6ba0 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 .** Read a 32-bi
b6bb0 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 t variable-lengt
b6bc0 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d h integer from m
b6bd0 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 emory starting a
b6be0 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 t p[0]..** Retur
b6bf0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
b6c00 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 bytes read. The
b6c10 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 value is stored
b6c20 20 69 6e 20 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 in *v..**.** If
b6c30 20 74 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72 the varint stor
b6c40 65 64 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61 ed in p[0] is la
b6c50 72 67 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69 rger than can fi
b6c60 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e t in a 32-bit un
b6c70 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 signed.** intege
b6c80 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74 r, then set *v t
b6c90 6f 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a o 0xffffffff..**
b6ca0 0a 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 .** A MACRO vers
b6cb0 69 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 ion, getVarint32
b6cc0 2c 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 , is provided wh
b6cd0 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 ich inlines the
b6ce0 0a 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 .** single-byte
b6cf0 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 case. All code
b6d00 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d should use the M
b6d10 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 ACRO version as
b6d20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f .** this functio
b6d30 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 n assumes the si
b6d40 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 ngle-byte case h
b6d50 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
b6d60 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 handled..*/.SQLI
b6d70 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 TE_PRIVATE u8 sq
b6d80 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 lite3GetVarint32
b6d90 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 (const unsigned
b6da0 63 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 char *p, u32 *v)
b6db0 7b 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 {. u32 a,b;..
b6dc0 2f 2a 20 54 68 65 20 31 2d 62 79 74 65 20 63 61 /* The 1-byte ca
b6dd0 73 65 2e 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e se. Overwhelmin
b6de0 67 6c 79 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d gly the most com
b6df0 6d 6f 6e 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e mon. Handled in
b6e00 6c 69 6e 65 0a 20 20 2a 2a 20 62 79 20 74 68 65 line. ** by the
b6e10 20 67 65 74 56 61 72 69 6e 33 32 28 29 20 6d 61 getVarin32() ma
b6e20 63 72 6f 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b cro */. a = *p;
b6e30 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d . /* a: p0 (unm
b6e40 61 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 asked) */.#ifnde
b6e50 66 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 20 f getVarint32.
b6e60 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b6e70 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
b6e80 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 between 0 and 1
b6e90 32 37 20 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61 27 */. *v = a
b6ea0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
b6eb0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
b6ec0 20 54 68 65 20 32 2d 62 79 74 65 20 63 61 73 65 The 2-byte case
b6ed0 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d */. p++;. b =
b6ee0 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 *p;. /* b: p1
b6ef0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b6f00 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
b6f10 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
b6f20 20 62 65 74 77 65 65 6e 20 31 32 38 20 61 6e 64 between 128 and
b6f30 20 31 36 33 38 33 20 2a 2f 0a 20 20 20 20 61 20 16383 */. a
b6f40 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d &= 0x7f;. a =
b6f50 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 a<<7;. *v =
b6f60 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 a | b;. retur
b6f70 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 n 2;. }.. /* T
b6f80 68 65 20 33 2d 62 79 74 65 20 63 61 73 65 20 2a he 3-byte case *
b6f90 2f 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 /. p++;. a = a
b6fa0 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<14;. a |= *p;
b6fb0 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 . /* a: p0<<14
b6fc0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p2 (unmasked)
b6fd0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
b6fe0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 0)). {. /* V
b6ff0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 36 alues between 16
b7000 33 38 34 20 61 6e 64 20 32 30 39 37 31 35 31 20 384 and 2097151
b7010 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 */. a &= (0x7
b7020 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b7030 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 b &= 0x7f;.
b7040 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b7050 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
b7060 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 return 3;. }..
b7070 20 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61 72 /* A 32-bit var
b7080 69 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 int is used to s
b7090 74 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d tore size inform
b70a0 61 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e ation in btrees.
b70b0 0a 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72 . ** Objects ar
b70c0 65 20 72 61 72 65 6c 79 20 6c 61 72 67 65 72 20 e rarely larger
b70d0 74 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20 than 2MiB limit
b70e0 6f 66 20 61 20 33 2d 62 79 74 65 20 76 61 72 69 of a 3-byte vari
b70f0 6e 74 2e 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74 nt.. ** A 3-byt
b7100 65 20 76 61 72 69 6e 74 20 69 73 20 73 75 66 66 e varint is suff
b7110 69 63 69 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d icient, for exam
b7120 70 6c 65 2c 20 74 6f 20 72 65 63 6f 72 64 20 74 ple, to record t
b7130 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 he size. ** of
b7140 61 20 31 30 34 38 35 36 39 2d 62 79 74 65 20 42 a 1048569-byte B
b7150 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20 LOB or string..
b7160 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 **. ** We only
b7170 20 75 6e 72 6f 6c 6c 20 74 68 65 20 66 69 72 73 unroll the firs
b7180 74 20 31 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d t 1-, 2-, and 3-
b7190 20 62 79 74 65 20 63 61 73 65 73 2e 20 20 54 68 byte cases. Th
b71a0 65 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 e very. ** rare
b71b0 20 6c 61 72 67 65 72 20 63 61 73 65 73 20 63 61 larger cases ca
b71c0 6e 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 n be handled by
b71d0 74 68 65 20 73 6c 6f 77 65 72 20 36 34 2d 62 69 the slower 64-bi
b71e0 74 20 76 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f t varint. ** ro
b71f0 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 utine.. */.#if
b7200 31 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 1. {. u64 v6
b7210 34 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 4;. u8 n;..
b7220 20 20 70 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 p -= 2;. n
b7230 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 = sqlite3GetVari
b7240 6e 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 nt(p, &v64);.
b7250 20 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26 20 assert( n>3 &&
b7260 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 69 66 28 20 n<=9 );. if(
b7270 28 76 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 (v64 & SQLITE_MA
b7280 58 5f 55 33 32 29 21 3d 76 36 34 20 29 7b 0a 20 X_U32)!=v64 ){.
b7290 20 20 20 20 20 2a 76 20 3d 20 30 78 66 66 66 66 *v = 0xffff
b72a0 66 66 66 66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ffff;. }else{
b72b0 0a 20 20 20 20 20 20 2a 76 20 3d 20 28 75 33 32 . *v = (u32
b72c0 29 76 36 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 )v64;. }.
b72d0 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 return n;. }..#
b72e0 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 20 66 6f else. /* For fo
b72f0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 28 6b 65 llowing code (ke
b7300 70 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 pt for historica
b7310 6c 20 72 65 63 6f 72 64 20 6f 6e 6c 79 29 20 73 l record only) s
b7320 68 6f 77 73 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 hows an. ** unr
b7330 6f 6c 6c 69 6e 67 20 66 6f 72 20 74 68 65 20 33 olling for the 3
b7340 2d 20 61 6e 64 20 34 2d 62 79 74 65 20 76 61 72 - and 4-byte var
b7350 69 6e 74 20 63 61 73 65 73 2e 20 20 54 68 69 73 int cases. This
b7360 20 63 6f 64 65 20 69 73 0a 20 20 2a 2a 20 73 6c code is. ** sl
b7370 69 67 68 74 6c 79 20 66 61 73 74 65 72 2c 20 62 ightly faster, b
b7380 75 74 20 69 74 20 69 73 20 61 6c 73 6f 20 6c 61 ut it is also la
b7390 72 67 65 72 20 61 6e 64 20 6d 75 63 68 20 68 61 rger and much ha
b73a0 72 64 65 72 20 74 6f 20 74 65 73 74 2e 0a 20 20 rder to test..
b73b0 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 */. p++;. b =
b73c0 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 b<<14;. b |= *p
b73d0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 ;. /* b: p1<<14
b73e0 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 | p3 (unmasked)
b73f0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 */. if (!(b&0x
b7400 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 80)). {. /*
b7410 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 32 Values between 2
b7420 30 39 37 31 35 32 20 61 6e 64 20 32 36 38 34 33 097152 and 26843
b7430 35 34 35 35 20 2a 2f 0a 20 20 20 20 62 20 26 3d 5455 */. b &=
b7440 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b7450 66 29 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 f);. a &= (0x
b7460 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b7470 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 a = a<<7;.
b7480 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 *v = a | b;.
b7490 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a return 4;. }.
b74a0 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
b74b0 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <14;. a |= *p;.
b74c0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c /* a: p0<<28 |
b74d0 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e p2<<14 | p4 (un
b74e0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b74f0 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(a&0x80)). {.
b7500 20 20 20 20 2f 2a 20 57 61 6c 75 65 73 20 20 62 /* Walues b
b7510 65 74 77 65 65 6e 20 32 36 38 34 33 35 34 35 36 etween 268435456
b7520 20 61 6e 64 20 33 34 33 35 39 37 33 38 33 36 37 and 34359738367
b7530 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 */. a &= (0x
b7540 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 1f<<28)|(0x7f<<1
b7550 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b7560 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 &= (0x1f<<28)|(
b7570 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b7580 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a ;. b = b<<7;.
b7590 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a *v = a | b;.
b75a0 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 return 5;.
b75b0 7d 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f }.. /* We can o
b75c0 6e 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 70 nly reach this p
b75d0 6f 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e oint when readin
b75e0 67 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 g a corrupt data
b75f0 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 base. ** file.
b7600 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 In that case we
b7610 20 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20 are not in any
b7620 68 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 20 hurry. Use the
b7630 28 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a (relatively. **
b7640 20 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70 slow) general-p
b7650 75 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65 urpose sqlite3Ge
b7660 74 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e tVarint() routin
b7670 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 e to extract the
b7680 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a . ** value. */.
b7690 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b {. u64 v64;
b76a0 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 . u8 n;..
b76b0 70 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20 p -= 4;. n =
b76c0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
b76d0 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 (p, &v64);. a
b76e0 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c ssert( n>5 && n<
b76f0 3d 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 =9 );. *v = (
b7700 75 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 u32)v64;. ret
b7710 75 72 6e 20 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 urn n;. }.#endi
b7720 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 f.}../*.** Retur
b7730 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
b7740 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 bytes that will
b7750 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f be needed to sto
b7760 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 re the given.**
b7770 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 64-bit integer..
b7780 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b7790 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 E int sqlite3Var
b77a0 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20 intLen(u64 v){.
b77b0 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f int i = 0;. do
b77c0 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 {. i++;. v
b77d0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 >>= 7;. }while
b77e0 28 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59 53 ( v!=0 && ALWAYS
b77f0 28 69 3c 39 29 20 29 3b 0a 20 20 72 65 74 75 72 (i<9) );. retur
b7800 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 n i;.}.../*.** R
b7810 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66 ead or write a f
b7820 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 our-byte big-end
b7830 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ian integer valu
b7840 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
b7850 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
b7860 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 Get4byte(const u
b7870 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 8 *p){. return
b7880 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b (p[0]<<24) | (p[
b7890 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c 1]<<16) | (p[2]<
b78a0 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 <8) | p[3];.}.SQ
b78b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b78c0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 d sqlite3Put4byt
b78d0 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 e(unsigned char
b78e0 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b *p, u32 v){. p[
b78f0 30 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 34 29 0] = (u8)(v>>24)
b7900 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 ;. p[1] = (u8)(
b7910 76 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d 20 3d v>>16);. p[2] =
b7920 20 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 20 70 (u8)(v>>8);. p
b7930 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a [3] = (u8)v;.}..
b7940 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
b7950 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
b7960 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 LITERAL) || defi
b7970 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 ned(SQLITE_HAS_C
b7980 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e ODEC)./*.** Tran
b7990 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62 slate a single b
b79a0 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20 yte of Hex into
b79b0 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 an integer..** T
b79c0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 his routine only
b79d0 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65 61 6c works if h real
b79e0 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 68 65 ly is a valid he
b79f0 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 xadecimal.** cha
b7a00 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e racter: 0..9a..
b7a10 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20 fA..F.*/.static
b7a20 75 38 20 68 65 78 54 6f 49 6e 74 28 69 6e 74 20 u8 hexToInt(int
b7a30 68 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 68 h){. assert( (h
b7a40 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 29 >='0' && h<='9')
b7a50 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 20 || (h>='a' &&
b7a60 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d h<='f') || (h>=
b7a70 27 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 29 'A' && h<='F') )
b7a80 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
b7a90 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28 ASCII. h += 9*(
b7aa0 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 1&(h>>6));.#endi
b7ab0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
b7ac0 45 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a EBCDIC. h += 9*
b7ad0 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e (1&~(h>>4));.#en
b7ae0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 38 dif. return (u8
b7af0 29 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65 )(h & 0xf);.}.#e
b7b00 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
b7b10 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
b7b20 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f L || SQLITE_HAS_
b7b30 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 CODEC */..#if !d
b7b40 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
b7b50 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 IT_BLOB_LITERAL)
b7b60 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
b7b70 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a TE_HAS_CODEC)./*
b7b80 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 42 4c .** Convert a BL
b7b90 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 OB literal of th
b7ba0 65 20 66 6f 72 6d 20 22 78 27 68 68 68 68 68 68 e form "x'hhhhhh
b7bb0 27 22 20 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 '" into its bina
b7bc0 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 ry.** value. Re
b7bd0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
b7be0 6f 20 69 74 73 20 62 69 6e 61 72 79 20 76 61 6c o its binary val
b7bf0 75 65 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f ue. Space to ho
b7c00 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 ld the.** binary
b7c10 20 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20 value has been
b7c20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
b7c30 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 lloc and must be
b7c40 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 freed by.** the
b7c50 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 calling routine
b7c60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b7c70 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
b7c80 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 3HexToBlob(sqlit
b7c90 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
b7ca0 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 ar *z, int n){.
b7cb0 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 char *zBlob;.
b7cc0 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 int i;.. zBlob
b7cd0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
b7ce0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
b7cf0 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d n/2 + 1);. n--
b7d00 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b ;. if( zBlob ){
b7d10 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
b7d20 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 n; i+=2){.
b7d30 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 zBlob[i/2] = (he
b7d40 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 xToInt(z[i])<<4)
b7d50 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b | hexToInt(z[i+
b7d60 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 1]);. }. z
b7d70 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 Blob[i/2] = 0;.
b7d80 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f }. return zBlo
b7d90 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 b;.}.#endif /* !
b7da0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
b7db0 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 _LITERAL || SQLI
b7dc0 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a TE_HAS_CODEC */.
b7dd0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
b7de0 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 he sqlite.magic
b7df0 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 from SQLITE_MAGI
b7e00 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 C_OPEN to SQLITE
b7e10 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 _MAGIC_BUSY..**
b7e20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
b7e30 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 (non-zero) if th
b7e40 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 e magic was not
b7e50 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b7e60 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 N.** when this r
b7e70 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
b7e80 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
b7e90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 tine is called w
b7ea0 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 hen entering an
b7eb0 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65 SQLite API. The
b7ec0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 SQLITE_MAGIC_OP
b7ed0 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 EN.** value indi
b7ee0 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64 cates that the d
b7ef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
b7f00 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 on passed into t
b7f10 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 he API is.** ope
b7f20 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 n and is not bei
b7f30 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68 ng used by anoth
b7f40 65 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63 er thread. By c
b7f50 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 hanging the valu
b7f60 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d e.** to SQLITE_M
b7f70 41 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64 AGIC_BUSY we ind
b7f80 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63 icate that the c
b7f90 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 onnection is in
b7fa0 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 use..** sqlite3S
b7fb0 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 afetyOff() below
b7fc0 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 will change the
b7fd0 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 value back to S
b7fe0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b7ff0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49 .** when the API
b8000 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 exits. .**.** T
b8010 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
b8020 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65 attempt to dete
b8030 63 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64 ct if two thread
b8040 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d s use the.** sam
b8050 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 e sqlite* pointe
b8060 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 r at the same ti
b8070 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 me. There is a
b8080 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 race .** conditi
b8090 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73 on so it is poss
b80a0 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72 ible that the er
b80b0 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63 ror is not detec
b80c0 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 ted..** But usua
b80d0 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 lly the problem
b80e0 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 will be seen. T
b80f0 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 he result will b
b8100 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 e an.** error wh
b8110 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 ich can be used
b8120 74 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70 to debug the app
b8130 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 lication that is
b8140 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65 .** using SQLite
b8150 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a incorrectly..**
b8160 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a .** Ticket #202:
b8170 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 If db->magic i
b8180 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 s not a valid op
b8190 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 en value, take c
b81a0 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f are not.** to mo
b81b0 64 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75 dify the db stru
b81c0 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 cture at all. I
b81d0 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 t could be that
b81e0 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a db is a stale.**
b81f0 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 pointer. In ot
b8200 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f her words, it co
b8210 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72 uld be that ther
b8220 65 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69 e has been a pri
b8230 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 or.** call to sq
b8240 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 lite3_close(db)
b8250 61 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20 and db has been
b8260 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e deallocated. An
b8270 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 d we do.** not w
b8280 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74 ant to write int
b8290 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 o deallocated me
b82a0 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 mory..*/.#ifdef
b82b0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
b82c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b82d0 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
b82e0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
b82f0 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 if( db->magic==S
b8300 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b8310 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 ){. db->magi
b8320 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 c = SQLITE_MAGIC
b8330 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 73 65 72 _BUSY;. asser
b8340 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
b8350 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
b8360 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 );. return 0
b8370 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 ;. }else if( db
b8380 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
b8390 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 MAGIC_BUSY ){.
b83a0 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 db->magic = SQ
b83b0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 LITE_MAGIC_ERROR
b83c0 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 ;. db->u1.isI
b83d0 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a nterrupted = 1;.
b83e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
b83f0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
b8400 43 68 61 6e 67 65 20 74 68 65 20 6d 61 67 69 63 Change the magic
b8410 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 from SQLITE_MAG
b8420 49 43 5f 42 55 53 59 20 74 6f 20 53 51 4c 49 54 IC_BUSY to SQLIT
b8430 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a E_MAGIC_OPEN..**
b8440 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
b8450 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 (non-zero) if t
b8460 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 he magic was not
b8470 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
b8480 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 SY.** when this
b8490 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
b84a0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c d..*/.#ifdef SQL
b84b0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
b84c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b84d0 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 ite3SafetyOff(sq
b84e0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 lite3 *db){. if
b84f0 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c ( db->magic==SQL
b8500 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 ITE_MAGIC_BUSY )
b8510 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 {. db->magic
b8520 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f = SQLITE_MAGIC_O
b8530 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 PEN;. assert(
b8540 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
b8550 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
b8560 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
b8570 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d }else{. db-
b8580 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f >magic = SQLITE_
b8590 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 MAGIC_ERROR;.
b85a0 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
b85b0 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 upted = 1;. r
b85c0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 eturn 1;. }.}.#
b85d0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 endif../*.** Che
b85e0 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ck to make sure
b85f0 77 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20 we have a valid
b8600 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 db pointer. Thi
b8610 73 20 74 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a s test is not.**
b8620 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 foolproof but i
b8630 74 20 64 6f 65 73 20 70 72 6f 76 69 64 65 20 73 t does provide s
b8640 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 20 70 ome measure of p
b8650 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 rotection agains
b8660 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 t.** misuse of t
b8670 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 75 63 he interface suc
b8680 68 20 61 73 20 70 61 73 73 69 6e 67 20 69 6e 20 h as passing in
b8690 64 62 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 db pointers that
b86a0 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 are.** NULL or
b86b0 77 68 69 63 68 20 68 61 76 65 20 62 65 65 6e 20 which have been
b86c0 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 previously close
b86d0 64 2e 20 20 49 66 20 74 68 69 73 20 72 6f 75 74 d. If this rout
b86e0 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 ine returns.** 1
b86f0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 it means that t
b8700 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 he db pointer is
b8710 20 76 61 6c 69 64 20 61 6e 64 20 30 20 69 66 20 valid and 0 if
b8720 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 it should not be
b8730 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64 .** dereferenced
b8740 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e for any reason.
b8750 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 The calling fu
b8760 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e nction should in
b8770 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d voke.** SQLITE_M
b8780 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c ISUSE immediatel
b8790 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 y..**.** sqlite3
b87a0 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 SafetyCheckOk()
b87b0 72 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68 requires that th
b87c0 65 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20 e db pointer be
b87d0 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 valid for.** use
b87e0 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 . sqlite3Safety
b87f0 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 CheckSickOrOk()
b8800 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e allows a db poin
b8810 74 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20 ter that failed
b8820 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 to.** open prope
b8830 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 rly and is not f
b8840 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 it for general u
b8850 73 65 20 62 75 74 20 77 68 69 63 68 20 63 61 6e se but which can
b8860 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 be.** used as a
b8870 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 n argument to sq
b8880 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 6f lite3_errmsg() o
b8890 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 r sqlite3_close(
b88a0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
b88b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b88c0 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 SafetyCheckOk(sq
b88d0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 lite3 *db){. u3
b88e0 32 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 2 magic;. if( d
b88f0 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b b==0 ) return 0;
b8900 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d . magic = db->m
b8910 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 agic;. if( magi
b8920 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f c!=SQLITE_MAGIC_
b8930 4f 50 45 4e 20 0a 23 69 66 64 65 66 20 53 51 4c OPEN .#ifdef SQL
b8940 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 26 ITE_DEBUG. &
b8950 26 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f & magic!=SQLITE_
b8960 4d 41 47 49 43 5f 42 55 53 59 0a 23 65 6e 64 69 MAGIC_BUSY.#endi
b8970 66 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 f. ){. retur
b8980 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 n 0;. }else{.
b8990 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
b89a0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
b89b0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
b89c0 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 tyCheckSickOrOk(
b89d0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
b89e0 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 u32 magic;. mag
b89f0 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a ic = db->magic;.
b8a00 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c if( magic!=SQL
b8a10 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 ITE_MAGIC_SICK &
b8a20 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 &. magic!=S
b8a30 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b8a40 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 &&. magic!
b8a50 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 =SQLITE_MAGIC_BU
b8a60 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 SY ) return 0;.
b8a70 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
b8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
b8a90 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a d of util.c ****
b8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b8ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
b8ae0 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 63 20 gin file hash.c
b8af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
b8b20 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
b8b30 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 22.**.** The
b8b40 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
b8b50 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
b8b60 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
b8b70 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
b8b80 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
b8b90 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
b8ba0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
b8bb0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
b8bc0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
b8bd0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
b8be0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
b8bf0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
b8c00 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
b8c10 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
b8c20 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
b8c30 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
b8c40 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8c90 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ****.** This is
b8ca0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
b8cb0 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61 on of generic ha
b8cc0 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 sh-tables.** use
b8cd0 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a d in SQLite..*/.
b8ce0 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 ./* Turn bulk me
b8cf0 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 mory into a hash
b8d00 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 table object by
b8d10 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
b8d20 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 e.** fields of t
b8d30 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72 he Hash structur
b8d40 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 e..**.** "pNew"
b8d50 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
b8d60 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 the hash table t
b8d70 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 hat is to be ini
b8d80 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c tialized..*/.SQL
b8d90 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b8da0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 sqlite3HashInit
b8db0 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20 (Hash *pNew){.
b8dc0 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 assert( pNew!=0
b8dd0 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 );. pNew->first
b8de0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f = 0;. pNew->co
b8df0 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d unt = 0;. pNew-
b8e00 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 >htsize = 0;. p
b8e10 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a New->ht = 0;.}..
b8e20 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e /* Remove all en
b8e30 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 tries from a has
b8e40 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 h table. Reclai
b8e50 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a m all memory..**
b8e60 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 Call this routi
b8e70 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 ne to delete a h
b8e80 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 ash table or to
b8e90 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 reset a hash tab
b8ea0 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 le.** to the emp
b8eb0 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c ty state..*/.SQL
b8ec0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b8ed0 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 sqlite3HashClea
b8ee0 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 r(Hash *pH){. H
b8ef0 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 ashElem *elem;
b8f00 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f /* For lo
b8f10 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 oping over all e
b8f20 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74 lements of the t
b8f30 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 able */.. asser
b8f40 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c t( pH!=0 );. el
b8f50 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a em = pH->first;.
b8f60 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b pH->first = 0;
b8f70 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
b8f80 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 pH->ht);. pH->h
b8f90 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 t = 0;. pH->hts
b8fa0 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 ize = 0;. while
b8fb0 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 ( elem ){. Ha
b8fc0 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 shElem *next_ele
b8fd0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a m = elem->next;.
b8fe0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
b8ff0 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d (elem);. elem
b9000 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 = next_elem;.
b9010 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 }. pH->count =
b9020 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 0;.}../*.** The
b9030 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e hashing function
b9040 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 ..*/.static unsi
b9050 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 68 gned int strHash
b9060 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 (const char *z,
b9070 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 int nKey){. int
b9080 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 h = 0;. assert
b9090 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77 ( nKey>=0 );. w
b90a0 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 hile( nKey > 0
b90b0 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 ){. h = (h<<3
b90c0 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55 ) ^ h ^ sqlite3U
b90d0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 pperToLower[(uns
b90e0 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d igned char)*z++]
b90f0 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 ;. nKey--;.
b9100 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a }. return h;.}.
b9110 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65 ../* Link pNew e
b9120 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 lement into the
b9130 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 hash table pH.
b9140 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 65 If pEntry!=0 the
b9150 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 n also.** insert
b9160 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 70 pNew into the p
b9170 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b 65 Entry hash bucke
b9180 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
b9190 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 d insertElement(
b91a0 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 . Hash *pH,
b91b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
b91c0 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 complete hash t
b91d0 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 able */. struct
b91e0 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 20 _ht *pEntry,
b91f0 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e /* The entry in
b9200 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73 to which pNew is
b9210 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 48 inserted */. H
b9220 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 ashElem *pNew
b9230 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 /* The ele
b9240 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 ment to be inser
b9250 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 ted */.){. Hash
b9260 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 Elem *pHead;
b9270 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d /* First elem
b9280 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70 ent already in p
b9290 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 70 Entry */. if( p
b92a0 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 65 Entry ){. pHe
b92b0 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 ad = pEntry->cou
b92c0 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 nt ? pEntry->cha
b92d0 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74 in : 0;. pEnt
b92e0 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 ry->count++;.
b92f0 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d pEntry->chain =
b9300 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a pNew;. }else{.
b9310 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a 20 pHead = 0;.
b9320 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 29 }. if( pHead )
b9330 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 {. pNew->next
b9340 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e = pHead;. pN
b9350 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 ew->prev = pHead
b9360 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20 ->prev;. if(
b9370 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 pHead->prev ){ p
b9380 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 Head->prev->next
b9390 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 = pNew; }. e
b93a0 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 lse
b93b0 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e { pH->first = pN
b93c0 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d ew; }. pHead-
b93d0 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 >prev = pNew;.
b93e0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d }else{. pNew-
b93f0 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 >next = pH->firs
b9400 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66 t;. if( pH->f
b9410 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 irst ){ pH->firs
b9420 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 t->prev = pNew;
b9430 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 }. pNew->prev
b9440 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 = 0;. pH->fi
b9450 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a rst = pNew;. }.
b9460 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 }.../* Resize th
b9470 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 e hash table so
b9480 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 that it cantains
b9490 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b "new_size" buck
b94a0 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 ets..**.** The h
b94b0 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 ash table might
b94c0 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 69 fail to resize i
b94d0 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 f sqlite3_malloc
b94e0 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 () fails or.** i
b94f0 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 69 f the new size i
b9500 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 s the same as th
b9510 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a e prior size..**
b9520 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
b9530 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 72 the resize occur
b9540 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e s and false if n
b9550 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ot..*/.static in
b9560 74 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70 t rehash(Hash *p
b9570 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 H, unsigned int
b9580 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 new_size){. str
b9590 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b uct _ht *new_ht;
b95a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
b95b0 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c he new hash tabl
b95c0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
b95d0 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 *elem, *next_ele
b95e0 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f m; /* For loo
b95f0 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 ping over existi
b9600 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a ng elements */..
b9610 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f #if SQLITE_MALLO
b9620 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 C_SOFT_LIMIT>0.
b9630 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 if( new_size*si
b9640 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 zeof(struct _ht)
b9650 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 >SQLITE_MALLOC_S
b9660 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 OFT_LIMIT ){.
b9670 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 new_size = SQLI
b9680 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c TE_MALLOC_SOFT_L
b9690 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 IMIT/sizeof(stru
b96a0 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 ct _ht);. }. i
b96b0 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d f( new_size==pH-
b96c0 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e >htsize ) return
b96d0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0;.#endif.. /*
b96e0 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74 The inability t
b96f0 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 o allocates spac
b9700 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 68 e for a larger h
b9710 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 2a ash table is. *
b9720 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 * a performance
b9730 68 69 74 20 62 75 74 20 69 74 20 69 73 20 6e 6f hit but it is no
b9740 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e t a fatal error.
b9750 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20 So mark the.
b9760 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 ** allocation as
b9770 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a a benign.. */.
b9780 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
b9790 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
b97a0 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74 new_ht = (struct
b97b0 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 _ht *)sqlite3Ma
b97c0 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 lloc( new_size*s
b97d0 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 izeof(struct _ht
b97e0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e ) );. sqlite3En
b97f0 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
b9800 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d .. if( new_ht==
b9810 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
b9820 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d sqlite3_free(pH-
b9830 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d >ht);. pH->ht =
b9840 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 new_ht;. pH->h
b9850 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 tsize = new_size
b9860 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
b9870 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a Size(new_ht)/siz
b9880 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b eof(struct _ht);
b9890 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74 . memset(new_ht
b98a0 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69 , 0, new_size*si
b98b0 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 zeof(struct _ht)
b98c0 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 );. for(elem=pH
b98d0 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 ->first, pH->fir
b98e0 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d st=0; elem; elem
b98f0 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 = next_elem){.
b9900 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
b9910 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d h = strHash(elem
b9920 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b ->pKey, elem->nK
b9930 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a ey) % new_size;.
b9940 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 next_elem =
b9950 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 elem->next;.
b9960 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 insertElement(pH
b9970 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c , &new_ht[h], el
b9980 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 em);. }. retur
b9990 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 n 1;.}../* This
b99a0 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e function (for in
b99b0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 ternal use only)
b99c0 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d locates an elem
b99d0 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 ent in an.** has
b99e0 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 h table that mat
b99f0 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b ches the given k
b9a00 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f ey. The hash fo
b9a10 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a r this key has.*
b9a20 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 * already been c
b9a30 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 omputed and is p
b9a40 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68 assed as the 4th
b9a50 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 parameter..*/.s
b9a60 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a tatic HashElem *
b9a70 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e findElementGiven
b9a80 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 Hash(. const Ha
b9a90 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 sh *pH, /* T
b9aa0 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 he pH to be sear
b9ab0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ched */. const
b9ac0 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a char *pKey, /*
b9ad0 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 The key we are
b9ae0 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f searching for */
b9af0 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 . int nKey,
b9b00 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
b9b10 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e in key (not coun
b9b20 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e ting zero termin
b9b30 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67 ator) */. unsig
b9b40 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f ned int h /
b9b50 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 * The hash for t
b9b60 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 his key. */.){.
b9b70 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b HashElem *elem;
b9b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b9b90 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 /* Used to loop
b9ba0 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 thru the element
b9bb0 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 list */. int c
b9bc0 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 ount;
b9bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
b9be0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
b9bf0 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a left to test */.
b9c00 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b . if( pH->ht ){
b9c10 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 . struct _ht
b9c20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 *pEntry = &pH->h
b9c30 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d t[h];. elem =
b9c40 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a pEntry->chain;.
b9c50 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 count = pEnt
b9c60 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c ry->count;. }el
b9c70 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 se{. elem = p
b9c80 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f H->first;. co
b9c90 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b unt = pH->count;
b9ca0 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f . }. while( co
b9cb0 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 unt-- && ALWAYS(
b9cc0 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28 elem) ){. if(
b9cd0 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 elem->nKey==nKe
b9ce0 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e y && sqlite3StrN
b9cf0 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c ICmp(elem->pKey,
b9d00 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b pKey,nKey)==0 ){
b9d10 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 . return e
b9d20 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 lem;. }. e
b9d30 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 lem = elem->next
b9d40 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 ;. }. return 0
b9d50 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 ;.}../* Remove a
b9d60 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 single entry fr
b9d70 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c om the hash tabl
b9d80 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 e given a pointe
b9d90 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 r to that.** ele
b9da0 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 ment and a hash
b9db0 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 on the element's
b9dc0 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 key..*/.static
b9dd0 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 void removeEleme
b9de0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 ntGivenHash(. H
b9df0 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 ash *pH,
b9e00 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 /* The pH conta
b9e10 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a ining "elem" */.
b9e20 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d HashElem* elem
b9e30 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 , /* The eleme
b9e40 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 nt to be removed
b9e50 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a from the pH */.
b9e60 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
b9e70 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 /* Hash valu
b9e80 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e e for the elemen
b9e90 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 t */.){. struct
b9ea0 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 _ht *pEntry;.
b9eb0 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 if( elem->prev )
b9ec0 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 {. elem->prev
b9ed0 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e ->next = elem->n
b9ee0 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 ext; . }else{.
b9ef0 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 pH->first = e
b9f00 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 lem->next;. }.
b9f10 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 if( elem->next
b9f20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 ){. elem->nex
b9f30 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e t->prev = elem->
b9f40 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 prev;. }. if(
b9f50 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45 pH->ht ){. pE
b9f60 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 ntry = &pH->ht[h
b9f70 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72 ];. if( pEntr
b9f80 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 y->chain==elem )
b9f90 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e {. pEntry->
b9fa0 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 chain = elem->ne
b9fb0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 xt;. }. pE
b9fc0 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 ntry->count--;.
b9fd0 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 assert( pEntr
b9fe0 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 y->count>=0 );.
b9ff0 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
ba000 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d e( elem );. pH-
ba010 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 >count--;. if(
ba020 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a pH->count<=0 ){.
ba030 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e assert( pH->
ba040 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 first==0 );.
ba050 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e assert( pH->coun
ba060 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 t==0 );. sqli
ba070 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 te3HashClear(pH)
ba080 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 ;. }.}../* Atte
ba090 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e mpt to locate an
ba0a0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
ba0b0 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 hash table pH wi
ba0c0 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 th a key.** that
ba0d0 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b matches pKey,nK
ba0e0 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 ey. Return the
ba0f0 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c data for this el
ba100 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a ement if it is.*
ba110 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c * found, or NULL
ba120 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
ba130 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 match..*/.SQLITE
ba140 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
ba150 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 qlite3HashFind(c
ba160 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 onst Hash *pH, c
ba170 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c onst char *pKey,
ba180 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 int nKey){. Ha
ba190 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 shElem *elem;
ba1a0 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 /* The element
ba1b0 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 that matches key
ba1c0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
ba1d0 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 nt h; /* A ha
ba1e0 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 sh on key */..
ba1f0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b assert( pH!=0 );
ba200 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 . assert( pKey!
ba210 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
ba220 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 nKey>=0 );. if(
ba230 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68 pH->ht ){. h
ba240 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c = strHash(pKey,
ba250 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 nKey) % pH->hts
ba260 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
ba270 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 h = 0;. }. e
ba280 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e lem = findElemen
ba290 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70 tGivenHash(pH, p
ba2a0 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 Key, nKey, h);.
ba2b0 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 return elem ? e
ba2c0 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d lem->data : 0;.}
ba2d0 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 ../* Insert an e
ba2e0 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 lement into the
ba2f0 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 hash table pH.
ba300 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c The key is pKey,
ba310 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 nKey.** and the
ba320 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a data is "data"..
ba330 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d **.** If no elem
ba340 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20 ent exists with
ba350 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 a matching key,
ba360 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c then a new.** el
ba370 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 ement is created
ba380 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 and NULL is ret
ba390 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
ba3a0 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 another element
ba3b0 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77 already exists w
ba3c0 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 ith the same key
ba3d0 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 , then the.** ne
ba3e0 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 w data replaces
ba3f0 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 the old data and
ba400 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 the old data is
ba410 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 returned..** Th
ba420 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 e key is not cop
ba430 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 ied in this inst
ba440 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c ance. If a mall
ba450 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a oc fails, then.*
ba460 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 * the new data i
ba470 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
ba480 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 he hash table is
ba490 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a unchanged..**.*
ba4a0 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20 * If the "data"
ba4b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 parameter to thi
ba4c0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 s function is NU
ba4d0 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 LL, then the.**
ba4e0 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f element correspo
ba4f0 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 nding to "key" i
ba500 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 s removed from t
ba510 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a he hash table..*
ba520 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
ba530 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 void *sqlite3Ha
ba540 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70 shInsert(Hash *p
ba550 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 H, const char *p
ba560 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 Key, int nKey, v
ba570 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e oid *data){. un
ba580 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 signed int h;
ba590 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 /* the hash
ba5a0 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c of the key modul
ba5b0 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a o hash table siz
ba5c0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
ba5d0 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 *elem; /*
ba5e0 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 Used to loop thr
ba5f0 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 u the element li
ba600 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d st */. HashElem
ba610 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a *new_elem; /*
ba620 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 New element add
ba630 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a ed to the pH */.
ba640 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 . assert( pH!=0
ba650 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b );. assert( pK
ba660 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ey!=0 );. asser
ba670 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 t( nKey>=0 );.
ba680 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 if( pH->htsize )
ba690 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 {. h = strHas
ba6a0 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 h(pKey, nKey) %
ba6b0 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 pH->htsize;. }e
ba6c0 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a lse{. h = 0;.
ba6d0 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e }. elem = fin
ba6e0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 dElementGivenHas
ba6f0 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 h(pH,pKey,nKey,h
ba700 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b );. if( elem ){
ba710 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 . void *old_d
ba720 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 ata = elem->data
ba730 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d ;. if( data==
ba740 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76 0 ){. remov
ba750 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 eElementGivenHas
ba760 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 h(pH,elem,h);.
ba770 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 }else{. e
ba780 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 lem->data = data
ba790 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b ;. elem->pK
ba7a0 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 ey = pKey;.
ba7b0 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c assert(nKey==el
ba7c0 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d em->nKey);. }
ba7d0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f . return old_
ba7e0 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 data;. }. if(
ba7f0 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e data==0 ) return
ba800 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 0;. new_elem =
ba810 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69 (HashElem*)sqli
ba820 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f te3Malloc( sizeo
ba830 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 f(HashElem) );.
ba840 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 if( new_elem==0
ba850 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a ) return data;.
ba860 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 new_elem->pKey
ba870 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 = pKey;. new_e
ba880 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 lem->nKey = nKey
ba890 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 ;. new_elem->da
ba8a0 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d ta = data;. pH-
ba8b0 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 >count++;. if(
ba8c0 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26 pH->count>=10 &&
ba8d0 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 pH->count > 2*p
ba8e0 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 H->htsize ){.
ba8f0 20 69 66 28 20 72 65 68 61 73 68 28 70 48 2c 20 if( rehash(pH,
ba900 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b 0a pH->count*2) ){.
ba910 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 48 assert( pH
ba920 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 ->htsize>0 );.
ba930 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 h = strHash(
ba940 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 pKey, nKey) % pH
ba950 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a ->htsize;. }.
ba960 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 }. if( pH->ht
ba970 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c ){. insertEl
ba980 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 ement(pH, &pH->h
ba990 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b t[h], new_elem);
ba9a0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
ba9b0 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 sertElement(pH,
ba9c0 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 0, new_elem);.
ba9d0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
ba9e0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
ba9f0 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a End of hash.c *
baa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
baa30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
baa40 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f Begin file opco
baa50 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.c **********
baa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
baa80 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c ./* Automaticall
baa90 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f y generated. Do
baaa0 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 not edit */./*
baab0 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 See the mkopcode
baac0 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 c.awk script for
baad0 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 details. */.#if
baae0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
baaf0 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c _OMIT_EXPLAIN) |
bab00 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 | !defined(NDEBU
bab10 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 G) || defined(VD
bab20 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 BE_PROFILE) || d
bab30 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
bab40 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 BUG).SQLITE_PRIV
bab50 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
bab60 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
bab70 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 e(int i){. stati
bab80 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f c const char *co
bab90 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b nst azName[] = {
baba0 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 "?",. /*
babb0 31 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 1 */ "Goto",.
babc0 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f 73 /* 2 */ "Gos
babd0 75 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33 ub",. /* 3
babe0 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 */ "Return",.
babf0 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 59 69 /* 4 */ "Yi
bac00 65 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 eld",. /*
bac10 35 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 5 */ "HaltIfNull
bac20 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a ",. /* 6 *
bac30 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f / "Halt",. /
bac40 2a 20 20 20 37 20 2a 2f 20 22 49 6e 74 65 67 65 * 7 */ "Intege
bac50 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 r",. /* 8
bac60 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 */ "Int64",.
bac70 20 2f 2a 20 20 20 39 20 2a 2f 20 22 53 74 72 69 /* 9 */ "Stri
bac80 6e 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 ng",. /* 10
bac90 20 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 */ "Null",.
baca0 20 2f 2a 20 20 31 31 20 2a 2f 20 22 42 6c 6f 62 /* 11 */ "Blob
bacb0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a ",. /* 12 *
bacc0 2f 20 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 / "Variable",.
bacd0 20 20 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4d 6f /* 13 */ "Mo
bace0 76 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 ve",. /* 14
bacf0 20 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 */ "Copy",.
bad00 20 2f 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70 /* 15 */ "SCop
bad10 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 y",. /* 16
bad20 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a */ "ResultRow",.
bad30 20 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 /* 17 */ "
bad40 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f CollSeq",. /
bad50 2a 20 20 31 38 20 2a 2f 20 22 46 75 6e 63 74 69 * 18 */ "Functi
bad60 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 on",. /* 19
bad70 20 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 */ "Not",.
bad80 2f 2a 20 20 32 30 20 2a 2f 20 22 41 64 64 49 6d /* 20 */ "AddIm
bad90 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 m",. /* 21
bada0 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a */ "MustBeInt",.
badb0 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 /* 22 */ "
badc0 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 RealAffinity",.
badd0 20 20 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 50 /* 23 */ "P
bade0 65 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20 ermutation",.
badf0 20 20 2f 2a 20 20 32 34 20 2a 2f 20 22 43 6f 6d /* 24 */ "Com
bae00 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 pare",. /*
bae10 32 35 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 25 */ "Jump",.
bae20 20 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 49 66 /* 26 */ "If
bae30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a ",. /* 27 *
bae40 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 / "IfNot",.
bae50 2f 2a 20 20 32 38 20 2a 2f 20 22 43 6f 6c 75 6d /* 28 */ "Colum
bae60 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 n",. /* 29
bae70 2a 2f 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 */ "Affinity",.
bae80 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 4d /* 30 */ "M
bae90 61 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 20 20 akeRecord",.
baea0 20 2f 2a 20 20 33 31 20 2a 2f 20 22 43 6f 75 6e /* 31 */ "Coun
baeb0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 t",. /* 32
baec0 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a */ "Savepoint",.
baed0 20 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 /* 33 */ "
baee0 41 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 AutoCommit",.
baef0 20 20 2f 2a 20 20 33 34 20 2a 2f 20 22 54 72 61 /* 34 */ "Tra
baf00 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 nsaction",.
baf10 2f 2a 20 20 33 35 20 2a 2f 20 22 52 65 61 64 43 /* 35 */ "ReadC
baf20 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 ookie",. /*
baf30 20 33 36 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 36 */ "SetCooki
baf40 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 37 20 e",. /* 37
baf50 2a 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 */ "VerifyCookie
baf60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a ",. /* 38 *
baf70 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20 / "OpenRead",.
baf80 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 4f 70 /* 39 */ "Op
baf90 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f enWrite",. /
bafa0 2a 20 20 34 30 20 2a 2f 20 22 4f 70 65 6e 45 70 * 40 */ "OpenEp
bafb0 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f hemeral",. /
bafc0 2a 20 20 34 31 20 2a 2f 20 22 4f 70 65 6e 50 73 * 41 */ "OpenPs
bafd0 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 eudo",. /*
bafe0 34 32 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 42 */ "Close",.
baff0 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 53 /* 43 */ "S
bb000 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 eekLt",. /*
bb010 20 34 34 20 2a 2f 20 22 53 65 65 6b 4c 65 22 2c 44 */ "SeekLe",
bb020 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20 . /* 45 */
bb030 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f "SeekGe",. /
bb040 2a 20 20 34 36 20 2a 2f 20 22 53 65 65 6b 47 74 * 46 */ "SeekGt
bb050 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37 20 2a ",. /* 47 *
bb060 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f / "Seek",. /
bb070 2a 20 20 34 38 20 2a 2f 20 22 4e 6f 74 46 6f 75 * 48 */ "NotFou
bb080 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 39 nd",. /* 49
bb090 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 20 20 */ "Found",.
bb0a0 20 20 2f 2a 20 20 35 30 20 2a 2f 20 22 49 73 55 /* 50 */ "IsU
bb0b0 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 nique",. /*
bb0c0 20 35 31 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 51 */ "NotExist
bb0d0 73 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32 20 s",. /* 52
bb0e0 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 0a 20 */ "Sequence",.
bb0f0 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 4e /* 53 */ "N
bb100 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f ewRowid",. /
bb110 2a 20 20 35 34 20 2a 2f 20 22 49 6e 73 65 72 74 * 54 */ "Insert
bb120 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a ",. /* 55 *
bb130 2f 20 22 49 6e 73 65 72 74 49 6e 74 22 2c 0a 20 / "InsertInt",.
bb140 20 20 20 20 2f 2a 20 20 35 36 20 2a 2f 20 22 44 /* 56 */ "D
bb150 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 elete",. /*
bb160 20 35 37 20 2a 2f 20 22 52 65 73 65 74 43 6f 75 57 */ "ResetCou
bb170 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 nt",. /* 58
bb180 20 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 */ "RowKey",.
bb190 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 52 6f /* 59 */ "Ro
bb1a0 77 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 wData",. /*
bb1b0 20 36 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 60 */ "Rowid",.
bb1c0 20 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 /* 61 */ "
bb1d0 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f NullRow",. /
bb1e0 2a 20 20 36 32 20 2a 2f 20 22 4c 61 73 74 22 2c * 62 */ "Last",
bb1f0 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 . /* 63 */
bb200 22 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 "Sort",. /*
bb210 20 36 34 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 64 */ "Rewind",
bb220 0a 20 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 . /* 65 */
bb230 22 50 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 "Prev",. /*
bb240 20 36 36 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 66 */ "Next",.
bb250 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 49 /* 67 */ "I
bb260 64 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 dxInsert",.
bb270 2f 2a 20 20 36 38 20 2a 2f 20 22 4f 72 22 2c 0a /* 68 */ "Or",.
bb280 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 /* 69 */ "
bb290 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 And",. /* 7
bb2a0 30 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22 0 */ "IdxDelete"
bb2b0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f ,. /* 71 */
bb2c0 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 "IdxRowid",.
bb2d0 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 49 64 78 /* 72 */ "Idx
bb2e0 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 LT",. /* 73
bb2f0 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 */ "IsNull",.
bb300 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f /* 74 */ "No
bb310 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 tNull",. /*
bb320 20 37 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 75 */ "Ne",.
bb330 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22 /* 76 */ "Eq"
bb340 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f ,. /* 77 */
bb350 20 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Gt",. /*
bb360 37 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 78 */ "Le",.
bb370 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 2c /* 79 */ "Lt",
bb380 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 . /* 80 */
bb390 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 "Ge",. /* 8
bb3a0 31 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 1 */ "IdxGE",.
bb3b0 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 42 69 /* 82 */ "Bi
bb3c0 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 tAnd",. /*
bb3d0 38 33 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 83 */ "BitOr",.
bb3e0 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22 53 /* 84 */ "S
bb3f0 68 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 hiftLeft",.
bb400 2f 2a 20 20 38 35 20 2a 2f 20 22 53 68 69 66 74 /* 85 */ "Shift
bb410 52 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 Right",. /*
bb420 20 38 36 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 86 */ "Add",.
bb430 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 53 75 /* 87 */ "Su
bb440 62 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a btract",. /*
bb450 20 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 88 */ "Multipl
bb460 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 y",. /* 89
bb470 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 */ "Divide",.
bb480 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 6d /* 90 */ "Rem
bb490 61 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a ainder",. /*
bb4a0 20 20 39 31 20 2a 2f 20 22 43 6f 6e 63 61 74 22 91 */ "Concat"
bb4b0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f ,. /* 92 */
bb4c0 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 "Destroy",.
bb4d0 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e /* 93 */ "BitN
bb4e0 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 ot",. /* 94
bb4f0 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 */ "String8",.
bb500 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 /* 95 */ "C
bb510 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lear",. /*
bb520 39 36 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 96 */ "CreateInd
bb530 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 ex",. /* 97
bb540 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65 */ "CreateTable
bb550 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a ",. /* 98 *
bb560 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61 22 2c / "ParseSchema",
bb570 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 . /* 99 */
bb580 22 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a "LoadAnalysis",.
bb590 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 /* 100 */ "
bb5a0 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 DropTable",.
bb5b0 20 2f 2a 20 31 30 31 20 2a 2f 20 22 44 72 6f 70 /* 101 */ "Drop
bb5c0 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 Index",. /*
bb5d0 31 30 32 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 102 */ "DropTrig
bb5e0 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 ger",. /* 10
bb5f0 33 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 3 */ "IntegrityC
bb600 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 k",. /* 104
bb610 2a 2f 20 22 52 6f 77 53 65 74 41 64 64 22 2c 0a */ "RowSetAdd",.
bb620 20 20 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 /* 105 */ "
bb630 52 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 20 20 RowSetRead",.
bb640 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 52 6f 77 /* 106 */ "Row
bb650 53 65 74 54 65 73 74 22 2c 0a 20 20 20 20 20 2f SetTest",. /
bb660 2a 20 31 30 37 20 2a 2f 20 22 50 72 6f 67 72 61 * 107 */ "Progra
bb670 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 m",. /* 108
bb680 2a 2f 20 22 50 61 72 61 6d 22 2c 0a 20 20 20 20 */ "Param",.
bb690 20 2f 2a 20 31 30 39 20 2a 2f 20 22 46 6b 43 6f /* 109 */ "FkCo
bb6a0 75 6e 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 unter",. /*
bb6b0 31 31 30 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 110 */ "FkIfZero
bb6c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a ",. /* 111 *
bb6d0 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 / "MemMax",.
bb6e0 20 2f 2a 20 31 31 32 20 2a 2f 20 22 49 66 50 6f /* 112 */ "IfPo
bb6f0 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 33 20 s",. /* 113
bb700 2a 2f 20 22 49 66 4e 65 67 22 2c 0a 20 20 20 20 */ "IfNeg",.
bb710 20 2f 2a 20 31 31 34 20 2a 2f 20 22 49 66 5a 65 /* 114 */ "IfZe
bb720 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 ro",. /* 115
bb730 20 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 20 */ "AggStep",.
bb740 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 41 /* 116 */ "A
bb750 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f ggFinal",. /
bb760 2a 20 31 31 37 20 2a 2f 20 22 56 61 63 75 75 6d * 117 */ "Vacuum
bb770 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 38 20 2a ",. /* 118 *
bb780 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a / "IncrVacuum",.
bb790 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 /* 119 */ "
bb7a0 45 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f 2a Expire",. /*
bb7b0 20 31 32 30 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 120 */ "TableLo
bb7c0 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 31 ck",. /* 121
bb7d0 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 */ "VBegin",.
bb7e0 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 56 43 /* 122 */ "VC
bb7f0 72 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 reate",. /*
bb800 31 32 33 20 2a 2f 20 22 56 44 65 73 74 72 6f 79 123 */ "VDestroy
bb810 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a ",. /* 124 *
bb820 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 / "VOpen",.
bb830 2f 2a 20 31 32 35 20 2a 2f 20 22 56 46 69 6c 74 /* 125 */ "VFilt
bb840 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 er",. /* 126
bb850 20 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 */ "VColumn",.
bb860 20 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 /* 127 */ "V
bb870 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 Next",. /* 1
bb880 32 38 20 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 28 */ "VRename",
bb890 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 . /* 129 */
bb8a0 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 "VUpdate",.
bb8b0 2f 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c 22 /* 130 */ "Real"
bb8c0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f ,. /* 131 */
bb8d0 20 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 "Pagecount",.
bb8e0 20 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 54 72 /* 132 */ "Tr
bb8f0 61 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 ace",. /* 13
bb900 33 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 3 */ "Noop",.
bb910 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 45 78 70 /* 134 */ "Exp
bb920 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 lain",. /* 1
bb930 33 35 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 35 */ "NotUsed_1
bb940 33 35 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36 35",. /* 136
bb950 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 36 */ "NotUsed_136
bb960 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 37 20 2a ",. /* 137 *
bb970 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22 2c / "NotUsed_137",
bb980 0a 20 20 20 20 20 2f 2a 20 31 33 38 20 2a 2f 20 . /* 138 */
bb990 22 4e 6f 74 55 73 65 64 5f 31 33 38 22 2c 0a 20 "NotUsed_138",.
bb9a0 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 4e /* 139 */ "N
bb9b0 6f 74 55 73 65 64 5f 31 33 39 22 2c 0a 20 20 20 otUsed_139",.
bb9c0 20 20 2f 2a 20 31 34 30 20 2a 2f 20 22 4e 6f 74 /* 140 */ "Not
bb9d0 55 73 65 64 5f 31 34 30 22 2c 0a 20 20 20 20 20 Used_140",.
bb9e0 2f 2a 20 31 34 31 20 2a 2f 20 22 54 6f 54 65 78 /* 141 */ "ToTex
bb9f0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 20 t",. /* 142
bba00 2a 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 */ "ToBlob",.
bba10 20 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 4e /* 143 */ "ToN
bba20 75 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a umeric",. /*
bba30 20 31 34 34 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 144 */ "ToInt",
bba40 0a 20 20 20 20 20 2f 2a 20 31 34 35 20 2a 2f 20 . /* 145 */
bba50 22 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 "ToReal",. };.
bba60 20 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 return azName[i
bba70 5d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a ];.}.#endif../**
bba80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
bba90 20 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a of opcodes.c **
bbaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
bbad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
bbae0 69 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 in file os_os2.c
bbaf0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
bbb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
bbb20 2a 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a ** 2006 Feb 14.*
bbb30 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
bbb40 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
bbb50 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
bbb60 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
bbb70 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
bbb80 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
bbb90 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
bbba0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
bbbb0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
bbbc0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
bbbd0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
bbbe0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
bbbf0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
bbc00 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
bbc10 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
bbc20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
bbc30 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
bbc40 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc90 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
bbca0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
bbcb0 20 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 that is specifi
bbcc0 63 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a c to OS/2..*/...
bbcd0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 #if SQLITE_OS_OS
bbce0 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 2../*.** A Note
bbcf0 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c About Memory All
bbd00 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 ocation:.**.** T
bbd10 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20 his driver uses
bbd20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 malloc()/free()
bbd30 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 directly rather
bbd40 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 than going throu
bbd50 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 gh.** the SQLite
bbd60 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 -wrappers sqlite
bbd70 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 3_malloc()/sqlit
bbd80 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 e3_free(). Thos
bbd90 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 e wrappers.** ar
bbda0 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 e designed for u
bbdb0 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 se on embedded s
bbdc0 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d ystems where mem
bbdd0 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e ory is scarce an
bbde0 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c d.** malloc fail
bbdf0 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 ures happen freq
bbe00 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f uently. OS/2 do
bbe10 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 es not typically
bbe20 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 run on.** embed
bbe30 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 ded systems, and
bbe40 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 when it does th
bbe50 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 e developers nor
bbe60 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 mally have bigge
bbe70 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f r.** problems to
bbe80 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 worry about tha
bbe90 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 n running out of
bbea0 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 memory. So the
bbeb0 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 re is not.** a c
bbec0 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 ompelling need t
bbed0 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 o use the wrappe
bbee0 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 rs..**.** But th
bbef0 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 ere is a good re
bbf00 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 ason to not use
bbf10 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 the wrappers. I
bbf20 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 f we use the.**
bbf30 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 wrappers then we
bbf40 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 will get simula
bbf50 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 ted malloc() fai
bbf60 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 lures within thi
bbf70 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e s.** driver. An
bbf80 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c d that causes al
bbf90 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c l kinds of probl
bbfa0 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 ems for our test
bbfb0 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 s. We.** could
bbfc0 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 enhance SQLite t
bbfd0 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 o deal with simu
bbfe0 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 lated malloc fai
bbff0 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 lures within.**
bc000 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 the OS driver, b
bc010 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 ut the code to d
bc020 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 eal with those f
bc030 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 ailure would not
bc040 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 .** be exercised
bc050 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 on Linux (which
bc060 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
bc070 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 o malloc() in th
bc080 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 e driver).** and
bc090 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 so we would hav
bc0a0 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 e difficulty wri
bc0b0 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 ting coverage te
bc0c0 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 sts for that.**
bc0d0 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f code. Better to
bc0e0 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 leave the code
bc0f0 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a out, we think..*
bc100 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f *.** The point o
bc110 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f f this discussio
bc120 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a n is as follows:
bc130 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 When creating
bc140 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 a new.** OS laye
bc150 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 r for an embedde
bc160 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 d system, if you
bc170 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 use this file a
bc180 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a s an example,.**
bc190 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f avoid the use o
bc1a0 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 f malloc()/free(
bc1b0 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e ). Those routin
bc1c0 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 es work ok on OS
bc1d0 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 /2.** desktops b
bc1e0 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 ut not so well i
bc1f0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
bc200 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 ms..*/../*.** Ma
bc210 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 cros used to det
bc220 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
bc230 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 r not to use thr
bc240 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 eads..*/.#if def
bc250 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 ined(SQLITE_THRE
bc260 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 54 ADSAFE) && SQLIT
bc270 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 E_THREADSAFE.# d
bc280 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 efine SQLITE_OS2
bc290 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 _THREADS 1.#endi
bc2a0 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 f../*.** Include
bc2b0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
bc2c0 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a mmon to all os_*
bc2d0 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a .c files.*/./***
bc2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
bc2f0 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 ude os_common.h
bc300 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
bc310 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a os_os2.c ******
bc320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
bc330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
bc340 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e n file os_common
bc350 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
bc380 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a * 2004 May 22.**
bc390 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
bc3a0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
bc3b0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
bc3c0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
bc3d0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
bc3e0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
bc3f0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
bc400 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
bc410 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
bc420 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
bc430 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
bc440 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
bc450 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
bc460 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
bc470 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
bc480 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
bc490 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
bc4a0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
bc4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc4f0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
bc500 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f e contains macro
bc510 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 s and a little b
bc520 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 it of code that
bc530 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 is common to.**
bc540 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 all of the platf
bc550 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c orm-specific fil
bc560 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 es (os_*.c) and
bc570 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 is #included int
bc580 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 o those.** files
bc590 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ..**.** This fil
bc5a0 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 e should be #inc
bc5b0 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f luded by the os_
bc5c0 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 *.c files only.
bc5d0 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 It is not a.**
bc5e0 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 general purpose
bc5f0 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a header file..*/.
bc600 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d #ifndef _OS_COMM
bc610 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f ON_H_.#define _O
bc620 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a S_COMMON_H_../*.
bc630 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 ** At least two
bc640 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 bugs have slippe
bc650 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 d in because we
bc660 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f changed the MEMO
bc670 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 RY_DEBUG.** macr
bc680 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 o to SQLITE_DEBU
bc690 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 G and some older
bc6a0 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 makefiles have
bc6b0 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 not yet made the
bc6c0 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 .** switch. The
bc6d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 following code
bc6e0 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 should catch thi
bc6f0 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d s problem at com
bc700 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 pile-time..*/.#i
bc710 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 fdef MEMORY_DEBU
bc720 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d G.# error "The M
bc730 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 EMORY_DEBUG macr
bc740 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 o is obsolete.
bc750 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 Use SQLITE_DEBUG
bc760 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 instead.".#endi
bc770 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
bc780 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
bc790 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
bc7a0 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 3OSTrace = 0;.#d
bc7b0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 efine OSTRACE1(X
bc7c0 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 ) if( sq
bc7d0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
bc7e0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
bc7f0 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 f(X).#define OST
bc800 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 RACE2(X,Y)
bc810 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
bc820 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
bc830 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 ugPrintf(X,Y).#d
bc840 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 efine OSTRACE3(X
bc850 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 ,Y,Z) if( sq
bc860 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
bc870 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
bc880 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 f(X,Y,Z).#define
bc890 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c OSTRACE4(X,Y,Z,
bc8a0 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 A) if( sqlite3
bc8b0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
bc8c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
bc8d0 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,Z,A).#define OS
bc8e0 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE5(X,Y,Z,A,B
bc8f0 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 ) if( sqlite3OST
bc900 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 race ) sqlite3De
bc910 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
bc920 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 A,B).#define OST
bc930 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE6(X,Y,Z,A,B,
bc940 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 C) \. if(sqli
bc950 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 te3OSTrace) sqli
bc960 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
bc970 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 ,Y,Z,A,B,C).#def
bc980 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 ine OSTRACE7(X,Y
bc990 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 ,Z,A,B,C,D) \.
bc9a0 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 if(sqlite3OSTr
bc9b0 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 ace) sqlite3Debu
bc9c0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c gPrintf(X,Y,Z,A,
bc9d0 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 B,C,D).#else.#de
bc9e0 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 fine OSTRACE1(X)
bc9f0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bca00 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 2(X,Y).#define O
bca10 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 STRACE3(X,Y,Z).#
bca20 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
bca30 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 X,Y,Z,A).#define
bca40 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c OSTRACE5(X,Y,Z,
bca50 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 A,B).#define OST
bca60 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE6(X,Y,Z,A,B,
bca70 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 C).#define OSTRA
bca80 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c CE7(X,Y,Z,A,B,C,
bca90 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a D).#endif../*.**
bcaa0 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 Macros for perf
bcab0 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e ormance tracing.
bcac0 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 Normally turne
bcad0 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 d off. Only wor
bcae0 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 ks.** on i486 ha
bcaf0 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 rdware..*/.#ifde
bcb00 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d f SQLITE_PERFORM
bcb10 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a ANCE_TRACE../* .
bcb20 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 ** hwtime.h cont
bcb30 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 ains inline asse
bcb40 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 mbler code for i
bcb50 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 mplementing .**
bcb60 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 high-performance
bcb70 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 timing routines
bcb80 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..*/./**********
bcb90 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 **** Include hwt
bcba0 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 ime.h in the mid
bcbb0 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e dle of os_common
bcbc0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
bcbd0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
bcbe0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
bcbf0 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a hwtime.h *******
bcc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcc20 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
bcc30 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 May 27.**.** The
bcc40 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
bcc50 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
bcc60 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
bcc70 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
bcc80 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
bcc90 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
bcca0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
bccb0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
bccc0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
bccd0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
bcce0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
bccf0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
bcd00 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
bcd10 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
bcd20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
bcd30 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
bcd40 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
bcd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
bcda0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
bcdb0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 ins inline asm c
bcdc0 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 ode for retrievi
bcdd0 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d ng "high-perform
bcde0 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 ance".** counter
bcdf0 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 s for x86 class
bce00 43 50 55 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 CPUs..*/.#ifndef
bce10 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 _HWTIME_H_.#def
bce20 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a ine _HWTIME_H_..
bce30 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
bce40 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 ing routine only
bce50 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 works on pentiu
bce60 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 m-class (or newe
bce70 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a r) processors..*
bce80 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 * It uses the RD
bce90 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 TSC opcode to re
bcea0 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 ad the cycle cou
bceb0 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 nt value out of
bcec0 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 the.** processor
bced0 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 and returns tha
bcee0 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 t value. This c
bcef0 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 an be used for h
bcf00 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 igh-res.** profi
bcf10 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 ling..*/.#if (de
bcf20 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
bcf30 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f || defined(_MSC_
bcf40 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 VER)) && \.
bcf50 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 (defined(i386)
bcf60 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 || defined(__i38
bcf70 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 6__) || defined(
bcf80 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 _M_IX86)).. #if
bcf90 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
bcfa0 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f _).. __inline__
bcfb0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
bcfc0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
bcfd0 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 d){. unsigne
bcfe0 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 d int lo, hi;.
bcff0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
bd000 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
bd010 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d : "=a" (lo), "=
bd020 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 d" (hi));. r
bd030 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 eturn (sqlite_ui
bd040 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 nt64)hi << 32 |
bd050 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 lo;. }.. #elif
bd060 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
bd070 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 R).. __declspec
bd080 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 (naked) __inline
bd090 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f sqlite_uint64 _
bd0a0 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 _cdecl sqlite3Hw
bd0b0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
bd0c0 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 __asm {.
bd0d0 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 rdtsc. r
bd0e0 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 et ; retur
bd0f0 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 n value at EDX:E
bd100 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 AX. }. }..
bd110 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 #endif..#elif (
bd120 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
bd130 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) && defined(__x
bd140 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 86_64__)).. __i
bd150 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
bd160 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
bd170 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
bd180 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 unsigned long v
bd190 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f al;. __asm_
bd1a0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
bd1b0 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 "rdtsc" : "=A" (
bd1c0 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 val));. ret
bd1d0 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 urn val;. }. .#
bd1e0 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
bd1f0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
bd200 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 ed(__ppc__))..
bd210 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
bd220 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
bd230 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
bd240 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
bd250 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 g long retval;.
bd260 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
bd270 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f ng junk;. _
bd280 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
bd290 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 e__ ("\n\.
bd2a0 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 1: mftb
bd2b0 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 u %1\n\.
bd2c0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
bd2d0 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 %L0\n\.
bd2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
bd2f0 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 bu %0\n\.
bd300 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 cmp
bd310 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 w %0,%1\n\.
bd320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd330 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 bne 1b".
bd340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 :
bd350 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 "=r" (retval), "
bd360 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 =r" (junk));.
bd370 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c return retval
bd380 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 ;. }..#else..
bd390 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c #error Need impl
bd3a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
bd3b0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
bd3c0 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e r your platform.
bd3d0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 .. /*. ** To c
bd3e0 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 ompile without i
bd3f0 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 mplementing sqli
bd400 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
bd410 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 your platform,.
bd420 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f ** you can remo
bd430 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 ve the above #er
bd440 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 ror and use the
bd450 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 following. ** s
bd460 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 tub function. Y
bd470 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d ou will lose tim
bd480 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 ing support for
bd490 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 many. ** of the
bd4a0 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 debugging and t
bd4b0 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 esting utilities
bd4c0 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 , but it should
bd4d0 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f at. ** least co
bd4e0 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 mpile and run..
bd4f0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
bd500 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 TE sqlite_uint
bd510 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
bd520 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 (void){ return (
bd530 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
bd540 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 ); }..#endif..#e
bd550 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
bd560 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a (_HWTIME_H_) */.
bd570 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bd580 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 End of hwtime.h
bd590 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
bd5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bd5c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bd5d0 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
bd5e0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
bd5f0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
bd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bd610 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ..static sqlite_
bd620 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a uint64 g_start;.
bd630 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
bd640 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a nt64 g_elapsed;.
bd650 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 #define TIMER_ST
bd660 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 ART g_star
bd670 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 t=sqlite3Hwtime(
bd680 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ).#define TIMER_
bd690 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c END g_el
bd6a0 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 apsed=sqlite3Hwt
bd6b0 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 ime()-g_start.#d
bd6c0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
bd6d0 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 SED g_elapse
bd6e0 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 d.#else.#define
bd6f0 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 TIMER_START.#def
bd700 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 ine TIMER_END.#d
bd710 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
bd720 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 SED ((sqlite
bd730 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 _uint64)0).#endi
bd740 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 f../*.** If we c
bd750 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 ompile with the
bd760 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 SQLITE_TEST macr
bd770 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 o set, then the
bd780 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a following block.
bd790 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 ** of code will
bd7a0 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c give us the abil
bd7b0 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 ity to simulate
bd7c0 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 a disk I/O error
bd7d0 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 . This.** is us
bd7e0 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 ed for testing t
bd7f0 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 he I/O recovery
bd800 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 logic..*/.#ifdef
bd810 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
bd820 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
bd830 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
bd840 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
bd850 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
bd860 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a of I/O Errors *
bd870 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
bd880 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bd890 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 r_hardhit = 0;
bd8a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
bd8b0 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 of non-benign er
bd8c0 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
bd8d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
bd8e0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
bd8f0 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 = 0; /* C
bd900 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 ount down to fir
bd910 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
bd920 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
bd930 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bd940 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 persist = 0;
bd950 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 /* True if I
bd960 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 /O errors persis
bd970 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 t */.SQLITE_API
bd980 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
bd990 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b rror_benign = 0;
bd9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
bd9b0 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 if errors are b
bd9c0 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f enign */.SQLITE_
bd9d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bd9e0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
bd9f0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
bda00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 int sqlite3_dis
bda10 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 kfull = 0;.#defi
bda20 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
bda30 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 orBenign(X) sqli
bda40 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
bda50 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 ign=(X).#define
bda60 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
bda70 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 CODE) \. if( (
bda80 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bda90 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 _persist && sqli
bdaa0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
bdab0 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 ) \. || sq
bdac0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
bdad0 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 ending-- == 1 )
bdae0 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
bdaf0 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 { local_ioerr()
bdb00 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 ; CODE; }.static
bdb10 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 void local_ioer
bdb20 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 r(){. IOTRACE((
bdb30 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 "IOERR\n"));. s
bdb40 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bdb50 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 hit++;. if( !sq
bdb60 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
bdb70 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f enign ) sqlite3_
bdb80 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
bdb90 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 ++;.}.#define Si
bdba0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
bdbb0 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 ror(CODE) \. i
bdbc0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
bdbd0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c ull_pending ){ \
bdbe0 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 . if( sqlite
bdbf0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
bdc00 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 ng == 1 ){ \.
bdc10 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 local_ioerr(
bdc20 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 ); \. sqli
bdc30 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 te3_diskfull = 1
bdc40 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
bdc50 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
bdc60 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f = 1; \. CO
bdc70 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 DE; \. }else
bdc80 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 { \. sqlit
bdc90 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
bdca0 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 ing--; \. }
bdcb0 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 \. }.#else.#de
bdcc0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
bdcd0 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 rrorBenign(X).#d
bdce0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
bdcf0 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 Error(A).#define
bdd00 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
bdd10 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 lError(A).#endif
bdd20 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 ../*.** When tes
bdd30 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 ting, keep a cou
bdd40 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
bdd50 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a of open files..
bdd60 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
bdd70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
bdd80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
bdd90 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 n_file_count = 0
bdda0 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f ;.#define OpenCo
bddb0 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 unter(X) sqlite
bddc0 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
bddd0 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 t+=(X).#else.#de
bdde0 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
bddf0 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 (X).#endif..#end
bde00 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
bde10 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f OS_COMMON_H_) */
bde20 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
bde30 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d * End of os_comm
bde40 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
bde50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bde60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bde70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
bde80 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
bde90 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
bdea0 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a n os_os2.c *****
bdeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdec0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 /../*.** The os2
bded0 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 File structure i
bdee0 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 s subclass of sq
bdef0 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 lite3_file speci
bdf00 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 fic for the OS/2
bdf10 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 .** protability
bdf20 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 layer..*/.typede
bdf30 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 f struct os2File
bdf40 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 os2File;.struct
bdf50 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e os2File {. con
bdf60 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
bdf70 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 thods *pMethod;
bdf80 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 /* Always the f
bdf90 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 irst entry */.
bdfa0 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 HFILE h;
bdfb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e /* Han
bdfc0 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e dle for accessin
bdfd0 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 g the file */.
bdfe0 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b char* pathToDel;
bdff0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
be000 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c e of file to del
be010 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 ete on close, NU
be020 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 LL if not */. u
be030 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 nsigned char loc
be040 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 ktype; /* Type
be050 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 of lock current
be060 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 ly held on this
be070 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 file */.};..#def
be080 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ine LOCK_TIMEOUT
be090 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 10L /* the defa
be0a0 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 ult locking time
be0b0 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a out */../*******
be0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be100 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ******.** The ne
be110 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 xt group of rout
be120 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 ines implement t
be130 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 he I/O methods s
be140 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 pecified.** by t
be150 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
be160 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
be170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
be1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
be1c0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
be1d0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
be1e0 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 os2Close( sqlit
be1f0 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 e3_file *id ){.
be200 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
be210 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 ERROR;. os2File
be220 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 *pFile;. if( i
be230 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f d && (pFile = (o
be240 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 s2File*)id) != 0
be250 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 ){. OSTRACE2
be260 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 ( "CLOSE %d\n",
be270 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 pFile->h );.
be280 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 rc = DosClose( p
be290 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 File->h );. p
be2a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
be2b0 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 NO_LOCK;. if
be2c0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
be2d0 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 el != NULL ){.
be2e0 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 rc = DosForc
be2f0 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 eDelete( (PSZ)pF
be300 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
be310 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 ;. free( pF
be320 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
be330 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 ;. pFile->p
be340 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
be350 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 . }. id =
be360 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 0;. OpenCount
be370 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 er( -1 );. }..
be380 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
be390 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
be3a0 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
be3b0 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 R;.}../*.** Read
be3c0 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c data from a fil
be3d0 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e e into a buffer.
be3e0 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
be3f0 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 OK if all.** byt
be400 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 es were read suc
be410 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 cessfully and SQ
be420 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e LITE_IOERR if an
be430 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
be440 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rong..*/.static
be450 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 int os2Read(. s
be460 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
be470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be480 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 * File to read f
be490 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 rom */. void *p
be4a0 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
be4b0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
be4c0 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 e content into t
be4d0 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 his buffer */.
be4e0 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
be4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be500 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
be510 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 es to read */.
be520 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
be530 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 fset
be540 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
be550 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
be560 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 */.){. ULONG fi
be570 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b leLocation = 0L;
be580 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 . ULONG got;.
be590 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d os2File *pFile =
be5a0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 (os2File*)id;.
be5b0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
be5c0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
be5d0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
be5e0 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b TE_IOERR_READ );
be5f0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 . OSTRACE3( "RE
be600 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 AD %d lock=%d\n"
be610 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
be620 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
be630 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 if( DosSetFileP
be640 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 tr(pFile->h, off
be650 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c set, FILE_BEGIN,
be660 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 &fileLocation)
be670 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
be680 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
be690 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 _IOERR;. }. if
be6a0 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 ( DosRead( pFile
be6b0 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 ->h, pBuf, amt,
be6c0 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 &got ) != NO_ERR
be6d0 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
be6e0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
be6f0 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f AD;. }. if( go
be700 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 t == (ULONG)amt
be710 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ). return SQL
be720 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b ITE_OK;. else {
be730 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 . /* Unread p
be740 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 ortions of the i
be750 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 nput buffer must
be760 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 be zero-filled
be770 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 */. memset(&(
be780 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 (char*)pBuf)[got
be790 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a ], 0, amt-got);.
be7a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
be7b0 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 E_IOERR_SHORT_RE
be7c0 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a AD;. }.}../*.**
be7d0 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d Write data from
be7e0 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 a buffer into a
be7f0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 file. Return S
be800 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
be810 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f ess.** or some o
be820 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 ther error code
be830 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 on failure..*/.s
be840 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 tatic int os2Wri
be850 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 te(. sqlite3_fi
be860 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 le *id,
be870 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f /* File to
be880 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 write into */.
be890 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 const void *pBu
be8a0 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
be8b0 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f /* The bytes to
be8c0 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 be written */.
be8d0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 int amt,
be8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be8f0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
be900 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
be910 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
be920 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 offset
be930 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
be940 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 the file to beg
be950 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f in writing at */
be960 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 .){. ULONG file
be970 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 Location = 0L;.
be980 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
be990 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 ERROR;. ULONG w
be9a0 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 rote;. os2File
be9b0 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
be9c0 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 e*)id;. assert(
be9d0 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 id!=0 );. Simu
be9e0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
be9f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
bea00 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 _WRITE );. Simu
bea10 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
bea20 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
bea30 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 _FULL );. OSTRA
bea40 43 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c CE3( "WRITE %d l
bea50 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
bea60 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
bea70 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f type );. if( Do
bea80 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c sSetFilePtr(pFil
bea90 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 e->h, offset, FI
beaa0 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c LE_BEGIN, &fileL
beab0 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 ocation) != NO_E
beac0 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 RROR ){. retu
bead0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
beae0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 . }. assert( a
beaf0 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 mt>0 );. while(
beb00 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 amt > 0 &&.
beb10 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 ( rc = DosW
beb20 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 rite( pFile->h,
beb30 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 (PVOID)pBuf, amt
beb40 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 , &wrote ) ) ==
beb50 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 NO_ERROR &&.
beb60 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 wrote > 0.
beb70 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 ){. amt -= w
beb80 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d rote;. pBuf =
beb90 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
beba0 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 wrote];. }.. r
bebb0 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f eturn ( rc != NO
bebc0 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 _ERROR || amt >
bebd0 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 (int)wrote ) ? S
bebe0 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c QLITE_FULL : SQL
bebf0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
bec00 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 Truncate an ope
bec10 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 n file to a spec
bec20 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 ified size.*/.st
bec30 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e atic int os2Trun
bec40 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 cate( sqlite3_fi
bec50 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 le *id, i64 nByt
bec60 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 e ){. APIRET rc
bec70 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f = NO_ERROR;. o
bec80 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
bec90 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
beca0 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 OSTRACE3( "TRUNC
becb0 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 ATE %d %lld\n",
becc0 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 pFile->h, nByte
becd0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
bece0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
becf0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
bed00 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 TE );. rc = Dos
bed10 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 SetFileSize( pFi
bed20 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a le->h, nByte );.
bed30 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e return rc == N
bed40 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 O_ERROR ? SQLITE
bed50 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
bed60 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a RR_TRUNCATE;.}..
bed70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
bed80 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 ST./*.** Count t
bed90 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c he number of ful
beda0 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 lsyncs and norma
bedb0 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 l syncs. This i
bedc0 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a s used to test.*
bedd0 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 * that syncs and
bede0 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f fullsyncs are o
bedf0 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 ccuring at the r
bee00 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 ight times..*/.S
bee10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bee20 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 lite3_sync_count
bee30 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
bee40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c int sqlite3_ful
bee50 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b lsync_count = 0;
bee60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
bee70 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 ake sure all wri
bee80 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 tes to a particu
bee90 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d lar file are com
beea0 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a mitted to disk..
beeb0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
beec0 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 2Sync( sqlite3_f
beed0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
beee0 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 gs ){. os2File
beef0 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
bef00 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 e*)id;. OSTRACE
bef10 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3( "SYNC %d lock
bef20 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
bef30 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
bef40 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 e );.#ifdef SQLI
bef50 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c TE_TEST. if( fl
bef60 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e ags & SQLITE_SYN
bef70 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c C_FULL){. sql
bef80 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
bef90 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c unt++;. }. sql
befa0 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b ite3_sync_count+
befb0 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 +;.#endif. /* I
befc0 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 f we compiled wi
befd0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f th the SQLITE_NO
befe0 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e _SYNC flag, then
beff0 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 syncing is a.
bf000 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 ** no-op. */.#i
bf010 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 fdef SQLITE_NO_S
bf020 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 YNC. UNUSED_PAR
bf030 41 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 AMETER(pFile);.
bf040 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
bf050 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 K;.#else. retur
bf060 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 n DosResetBuffer
bf070 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 ( pFile->h ) ==
bf080 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 NO_ERROR ? SQLIT
bf090 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f E_OK : SQLITE_IO
bf0a0 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f ERR;.#endif.}../
bf0b0 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 *.** Determine t
bf0c0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
bf0d0 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 of a file in byt
bf0e0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 es.*/.static int
bf0f0 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 os2FileSize( sq
bf100 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
bf110 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
bf120 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 Size ){. APIRET
bf130 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
bf140 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 FILESTATUS3 fs
bf150 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d ts3FileInfo;. m
bf160 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 emset(&fsts3File
bf170 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Info, 0, sizeof(
bf180 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b fsts3FileInfo));
bf190 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
bf1a0 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
bf1b0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
bf1c0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 LITE_IOERR_FSTAT
bf1d0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 );. rc = DosQu
bf1e0 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f eryFileInfo( ((o
bf1f0 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 s2File*)id)->h,
bf200 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 FIL_STANDARD, &f
bf210 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 sts3FileInfo, si
bf220 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 zeof(FILESTATUS3
bf230 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d ) );. if( rc ==
bf240 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
bf250 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 *pSize = fsts3F
bf260 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a ileInfo.cbFile;.
bf270 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
bf280 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
bf290 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
bf2a0 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
bf2b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 }.}../*.** Acqui
bf2c0 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b re a reader lock
bf2d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bf2e0 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 getReadLock( os2
bf2f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 File *pFile ){.
bf300 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
bf310 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
bf320 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 UnlockArea;. A
bf330 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d PIRET res;. mem
bf340 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 set(&LockArea, 0
bf350 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 , sizeof(LockAre
bf360 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 a));. memset(&U
bf370 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 nlockArea, 0, si
bf380 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 zeof(UnlockArea)
bf390 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f );. LockArea.lO
bf3a0 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 ffset = SHARED_F
bf3b0 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 IRST;. LockArea
bf3c0 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 .lRange = SHARED
bf3d0 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 _SIZE;. UnlockA
bf3e0 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
bf3f0 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c ;. UnlockArea.l
bf400 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 Range = 0L;. re
bf410 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
bf420 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
bf430 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
bf440 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
bf450 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 OUT, 1L );. OST
bf460 52 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c RACE3( "GETREADL
bf470 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 OCK %d res=%d\n"
bf480 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
bf490 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b );. return res;
bf4a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 .}../*.** Undo a
bf4b0 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 readlock.*/.sta
bf4c0 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 tic int unlockRe
bf4d0 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 adLock( os2File
bf4e0 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 *id ){. FILELOC
bf4f0 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 K LockArea,.
bf500 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 UnlockA
bf510 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 rea;. APIRET re
bf520 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 s;. memset(&Loc
bf530 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
bf540 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d (LockArea));. m
bf550 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 emset(&UnlockAre
bf560 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c a, 0, sizeof(Unl
bf570 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 ockArea));. Loc
bf580 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
bf590 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 0L;. LockArea.l
bf5a0 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e Range = 0L;. Un
bf5b0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
bf5c0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
bf5d0 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 . UnlockArea.lR
bf5e0 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 ange = SHARED_SI
bf5f0 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 ZE;. res = DosS
bf600 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d etFileLocks( id-
bf610 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
bf620 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b &LockArea, LOCK
bf630 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a _TIMEOUT, 1L );.
bf640 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c OSTRACE3( "UNL
bf650 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c OCK-READLOCK fil
bf660 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d e handle=%d res=
bf670 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 %d?\n", id->h, r
bf680 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 es );. return r
bf690 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 es;.}../*.** Loc
bf6a0 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
bf6b0 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
bf6c0 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
bf6d0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
bf6e0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
bf6f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
bf700 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
bf710 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
bf720 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
bf730 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
bf740 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
bf750 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
bf760 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
bf770 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
bf780 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
bf790 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
bf7a0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
bf7b0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
bf7c0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
bf7d0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
bf7e0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
bf7f0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
bf800 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
bf810 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
bf820 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
bf830 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
bf840 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
bf850 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
bf860 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
bf870 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
bf880 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
bf890 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
bf8a0 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
bf8b0 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
bf8c0 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
bf8d0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
bf8e0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
bf8f0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
bf900 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
bf910 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
bf920 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
bf930 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
bf940 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
bf950 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
bf960 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
bf970 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 lock. The os2U
bf980 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a nlock() routine.
bf990 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f ** erases all lo
bf9a0 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 cks at once and
bf9b0 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 returns us immed
bf9c0 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e iately to lockin
bf9d0 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 g level 0..** It
bf9e0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
bf9f0 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f to lower the lo
bfa00 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 cking level one
bfa10 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 step at a time.
bfa20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 You.** must go
bfa30 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b straight to lock
bfa40 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a ing level 0..*/.
bfa50 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f static int os2Lo
bfa60 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
bfa70 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
bfa80 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pe ){. int rc =
bfa90 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
bfaa0 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
bfab0 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 from subroutine
bfac0 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 s */. APIRET re
bfad0 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 s = NO_ERROR;
bfae0 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e /* Result of an
bfaf0 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 OS/2 lock call
bfb00 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b */. int newLock
bfb10 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 type; /* S
bfb20 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 et pFile->lockty
bfb30 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 pe to this value
bfb40 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
bfb50 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 */. int gotPend
bfb60 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 ingLock = 0;/* T
bfb70 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 rue if we acquir
bfb80 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ed a PENDING loc
bfb90 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 k this time */.
bfba0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
bfbb0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
bfbc0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f UnlockArea;. o
bfbd0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
bfbe0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
bfbf0 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
bfc00 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
bfc10 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 Area));. memset
bfc20 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c (&UnlockArea, 0,
bfc30 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 sizeof(UnlockAr
bfc40 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ea));. assert(
bfc50 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 pFile!=0 );. OS
bfc60 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 TRACE4( "LOCK %d
bfc70 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 %d was %d\n", p
bfc80 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
bfc90 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 e, pFile->lockty
bfca0 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 pe );.. /* If t
bfcb0 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
bfcc0 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 a lock of this t
bfcd0 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 ype or more rest
bfce0 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 rictive on the.
bfcf0 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 ** os2File, do
bfd00 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 nothing. Don't u
bfd10 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a se the end_lock:
bfd20 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 exit path, as.
bfd30 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ** sqlite3_mute
bfd40 78 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 x_enter() hasn't
bfd50 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 been called yet
bfd60 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
bfd70 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f le->locktype>=lo
bfd80 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 cktype ){. OS
bfd90 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
bfda0 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 %d ok (already
bfdb0 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d held)\n", pFile-
bfdc0 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a >h, locktype );.
bfdd0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
bfde0 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
bfdf0 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f Make sure the lo
bfe00 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 cking sequence i
bfe10 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 s correct. */.
bfe20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
bfe30 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
bfe40 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 K || locktype==S
bfe50 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
bfe60 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
bfe70 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 !=PENDING_LOCK )
bfe80 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
bfe90 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c type!=RESERVED_L
bfea0 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f OCK || pFile->lo
bfeb0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
bfec0 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 OCK );.. /* Loc
bfed0 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f k the PENDING_LO
bfee0 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 CK byte if we ne
bfef0 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 ed to acquire a
bff00 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a PENDING lock or.
bff10 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f ** a SHARED lo
bff20 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 ck. If we are a
bff30 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 cquiring a SHARE
bff40 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 D lock, the acqu
bff50 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 isition of. **
bff60 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
bff70 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 byte is tempora
bff80 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f ry.. */. newLo
bff90 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e cktype = pFile->
bffa0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 locktype;. if(
bffb0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
bffc0 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c =NO_LOCK. |
bffd0 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
bffe0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
bfff0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
c0000 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 RESERVED_LOCK).
c0010 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 ){. LockArea
c0020 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
c0030 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 NG_BYTE;. Loc
c0040 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
c0050 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
c0060 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
c0070 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c0080 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 Range = 0L;..
c0090 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 /* wait longer
c00a0 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 than LOCK_TIMEOU
c00b0 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 T here not to ha
c00c0 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 ve to try multip
c00d0 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 le times */.
c00e0 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
c00f0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
c0100 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
c0110 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 ockArea, 100L, 0
c0120 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 L );. if( res
c0130 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c0140 20 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 gotPending
c0150 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 Lock = 1;.
c0160 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
c0170 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 %d pending lock
c0180 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 boolean set. re
c0190 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
c01a0 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a h, res );. }.
c01b0 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
c01c0 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a e a shared lock.
c01d0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
c01e0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
c01f0 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 && res == NO_ER
c0200 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ROR ){. asser
c0210 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
c0220 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
c0230 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 res = getRead
c0240 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
c0250 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 if( res == NO_E
c0260 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 RROR ){. ne
c0270 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 wLocktype = SHAR
c0280 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 ED_LOCK;. }.
c0290 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
c02a0 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 CK %d acquire sh
c02b0 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 ared lock. res=%
c02c0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c02d0 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a res );. }.. /*
c02e0 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 Acquire a RESER
c02f0 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 VED lock. */.
c0300 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 if( locktype==RE
c0310 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 SERVED_LOCK && r
c0320 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
c0330 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
c0340 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
c0350 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
c0360 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c0370 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
c0380 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
c0390 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
c03a0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c03b0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
c03c0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c03d0 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 0L;. res =
c03e0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c03f0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c0400 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c0410 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c0420 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 , 0L );. if(
c0430 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
c0440 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
c0450 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f type = RESERVED_
c0460 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 LOCK;. }.
c0470 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
c0480 25 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 %d acquire reser
c0490 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 ved lock. res=%d
c04a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
c04b0 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 es );. }.. /*
c04c0 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e Acquire a PENDIN
c04d0 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 G lock. */. if
c04e0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c ( locktype==EXCL
c04f0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 USIVE_LOCK && re
c0500 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
c0510 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 . newLocktype
c0520 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b = PENDING_LOCK;
c0530 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c . gotPendingL
c0540 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 ock = 0;. OST
c0550 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 RACE2( "LOCK %d
c0560 61 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 acquire pending
c0570 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f lock. pending lo
c0580 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 ck boolean unset
c0590 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 .\n", pFile->h )
c05a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
c05b0 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ire an EXCLUSIVE
c05c0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
c05d0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
c05e0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
c05f0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c0600 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
c0610 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 e->locktype>=SHA
c0620 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
c0630 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 res = unlockRead
c0640 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
c0650 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 OSTRACE2( "unre
c0660 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 adlock = %d\n",
c0670 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 res );. LockA
c0680 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 rea.lOffset = SH
c0690 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
c06a0 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
c06b0 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
c06c0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
c06d0 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
c06e0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
c06f0 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 e = 0L;. res
c0700 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
c0710 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
c0720 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
c0730 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
c0740 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 T, 0L );. if(
c0750 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 res == NO_ERROR
c0760 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
c0770 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 ktype = EXCLUSIV
c0780 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 E_LOCK;. }els
c0790 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 e{. OSTRACE
c07a0 32 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 2( "OS/2 error-c
c07b0 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 ode = %d\n", res
c07c0 20 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 );. getRea
c07d0 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 dLock(pFile);.
c07e0 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 }. OSTRACE3
c07f0 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 ( "LOCK %d acqui
c0800 72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 re exclusive loc
c0810 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 k. res=%d\n", p
c0820 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
c0830 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 }.. /* If we
c0840 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 are holding a PE
c0850 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 NDING lock that
c0860 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 ought to be rele
c0870 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 ased, then. **
c0880 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a release it now..
c0890 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 */. if( gotPe
c08a0 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 ndingLock && loc
c08b0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
c08c0 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b CK ){. int r;
c08d0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
c08e0 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
c08f0 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
c0900 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
c0910 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 Area.lOffset = P
c0920 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
c0930 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
c0940 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d ge = 1L;. r =
c0950 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c0960 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c0970 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c0980 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c0990 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
c09a0 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 ACE3( "LOCK %d u
c09b0 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 nlocking pending
c09c0 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 /is shared. r=%d
c09d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
c09e0 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 );. }.. /* Up
c09f0 64 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f date the state o
c0a00 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 f the lock has h
c0a10 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 eld in the file
c0a20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a descriptor then.
c0a30 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 ** return the
c0a40 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 appropriate resu
c0a50 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 lt code.. */.
c0a60 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
c0a70 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ROR ){. rc =
c0a80 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
c0a90 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 se{. OSTRACE4
c0aa0 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 ( "LOCK FAILED %
c0ab0 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 d trying for %d
c0ac0 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 but got %d\n", p
c0ad0 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
c0ae0 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c locktype,
c0af0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a newLocktype );.
c0b00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
c0b10 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c BUSY;. }. pFil
c0b20 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 e->locktype = ne
c0b30 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 wLocktype;. OST
c0b40 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 RACE3( "LOCK %d
c0b50 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 now %d\n", pFile
c0b60 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
c0b70 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e type );. return
c0b80 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
c0b90 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
c0ba0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
c0bb0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
c0bc0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
c0bd0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
c0be0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
c0bf0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
c0c00 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
c0c10 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d , return.** non-
c0c20 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 zero, otherwise
c0c30 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zero..*/.static
c0c40 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 int os2CheckRese
c0c50 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 rvedLock( sqlite
c0c60 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
c0c70 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 *pOut ){. int r
c0c80 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 = 0;. os2File
c0c90 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
c0ca0 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 e*)id;. assert(
c0cb0 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 pFile!=0 );. i
c0cc0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
c0cd0 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe>=RESERVED_LOC
c0ce0 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a K ){. r = 1;.
c0cf0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 OSTRACE3( "T
c0d00 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
c0d10 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 d (local)\n", pF
c0d20 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d ile->h, r );. }
c0d30 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f else{. FILELO
c0d40 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 CK LockArea,.
c0d50 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f Unlo
c0d60 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 ckArea;. APIR
c0d70 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
c0d80 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f ;. memset(&Lo
c0d90 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f ckArea, 0, sizeo
c0da0 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 f(LockArea));.
c0db0 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b memset(&Unlock
c0dc0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
c0dd0 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 UnlockArea));.
c0de0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c0df0 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
c0e00 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
c0e10 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
c0e20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c0e30 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
c0e40 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c0e50 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 = 0L;. rc =
c0e60 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c0e70 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c0e80 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c0e90 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c0ea0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c0eb0 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f CE3( "TEST WR-LO
c0ec0 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 CK %d lock reser
c0ed0 76 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e ved byte rc=%d\n
c0ee0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 ", pFile->h, rc
c0ef0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d );. if( rc ==
c0f00 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c0f10 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 APIRET rcu =
c0f20 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 NO_ERROR; /* ret
c0f30 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c urn code for unl
c0f40 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 ocking */.
c0f50 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
c0f60 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 = 0L;. Loc
c0f70 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c0f80 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 L;. UnlockA
c0f90 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 rea.lOffset = RE
c0fa0 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 SERVED_BYTE;.
c0fb0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
c0fc0 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 ange = 1L;.
c0fd0 20 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c rcu = DosSetFil
c0fe0 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
c0ff0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
c1000 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
c1010 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
c1020 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 OSTRACE3( "T
c1030 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 EST WR-LOCK %d u
c1040 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 nlock reserved b
c1050 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 yte r=%d\n", pFi
c1060 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 le->h, rcu );.
c1070 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 }. r = !(rc
c1080 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 == NO_ERROR);.
c1090 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
c10a0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
c10b0 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 (remote)\n", pF
c10c0 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d ile->h, r );. }
c10d0 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 . *pOut = r;.
c10e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c10f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 ;.}../*.** Lower
c1100 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
c1110 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 el on file descr
c1120 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b iptor id to lock
c1130 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
c1140 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
c1150 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
c1160 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
c1170 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
c1180 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
c1190 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
c11a0 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
c11b0 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
c11c0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
c11d0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
c11e0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
c11f0 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 * It is not poss
c1200 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f ible for this ro
c1210 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 utine to fail if
c1220 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
c1230 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f ment.** is NO_LO
c1240 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f CK. If the seco
c1250 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 nd argument is S
c1260 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 HARED_LOCK then
c1270 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
c1280 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c might return SQL
c1290 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 ITE_IOERR;.*/.st
c12a0 61 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f atic int os2Unlo
c12b0 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
c12c0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
c12d0 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 pe ){. int type
c12e0 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
c12f0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
c1300 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d d;. APIRET rc =
c1310 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 SQLITE_OK;. AP
c1320 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 IRET res = NO_ER
c1330 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 ROR;. FILELOCK
c1340 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
c1350 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
c1360 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 a;. memset(&Loc
c1370 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
c1380 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d (LockArea));. m
c1390 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 emset(&UnlockAre
c13a0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c a, 0, sizeof(Unl
c13b0 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 ockArea));. ass
c13c0 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b ert( pFile!=0 );
c13d0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
c13e0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
c13f0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 );. OSTRACE4(
c1400 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 "UNLOCK %d to %d
c1410 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c was %d\n", pFil
c1420 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 e->h, locktype,
c1430 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
c1440 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c );. type = pFil
c1450 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 e->locktype;. i
c1460 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 f( type>=EXCLUSI
c1470 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c VE_LOCK ){. L
c1480 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c1490 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c14a0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c14b0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c14c0 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
c14d0 46 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 FIRST;. Unloc
c14e0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 kArea.lRange = S
c14f0 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 HARED_SIZE;.
c1500 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
c1510 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
c1520 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
c1530 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 ockArea, LOCK_TI
c1540 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 MEOUT, 0L );.
c1550 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f OSTRACE3( "UNLO
c1560 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 CK %d exclusive
c1570 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 lock res=%d\n",
c1580 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c1590 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 . if( locktyp
c15a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
c15b0 26 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 & getReadLock(pF
c15c0 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ile) != NO_ERROR
c15d0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
c15e0 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 s should never h
c15f0 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c appen. We shoul
c1600 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 d always be able
c1610 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 to. ** rea
c1620 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
c1630 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 lock */. OS
c1640 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 TRACE3( "UNLOCK
c1650 25 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 %d to %d getRead
c1660 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 Lock() failed\n"
c1670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
c1680 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 type );. rc
c1690 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
c16a0 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 UNLOCK;. }.
c16b0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 }. if( type>=RE
c16c0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
c16d0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c16e0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
c16f0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c1700 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
c1710 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 ea.lOffset = RES
c1720 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
c1730 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
c1740 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 e = 1L;. res
c1750 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
c1760 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
c1770 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
c1780 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
c1790 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 T, 0L );. OST
c17a0 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
c17b0 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 d reserved res=%
c17c0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c17d0 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 res );. }. if(
c17e0 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
c17f0 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 CK && type>=SHAR
c1800 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
c1810 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c es = unlockReadL
c1820 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
c1830 4f 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 OSTRACE5( "UNLOC
c1840 4b 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 K %d is %d want
c1850 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 %d res=%d\n", pF
c1860 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f ile->h, type, lo
c1870 63 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 cktype, res );.
c1880 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 }. if( type>=P
c1890 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 ENDING_LOCK ){.
c18a0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c18b0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
c18c0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c18d0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
c18e0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e ea.lOffset = PEN
c18f0 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 DING_BYTE;. U
c1900 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c1910 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 1L;. res =
c1920 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c1930 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c1940 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c1950 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c1960 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
c1970 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c1980 20 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c pending res=%d\
c1990 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
c19a0 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 s );. }. pFile
c19b0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
c19c0 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 ktype;. OSTRACE
c19d0 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 3( "UNLOCK %d no
c19e0 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e w %d\n", pFile->
c19f0 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
c1a00 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pe );. return r
c1a10 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 c;.}../*.** Cont
c1a20 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 rol and query of
c1a30 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 the open file h
c1a40 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 andle..*/.static
c1a50 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 int os2FileCont
c1a60 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 rol(sqlite3_file
c1a70 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f *id, int op, vo
c1a80 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 id *pArg){. swi
c1a90 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
c1aa0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
c1ab0 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 _LOCKSTATE: {.
c1ac0 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
c1ad0 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 = ((os2File*)id)
c1ae0 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 ->locktype;.
c1af0 20 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e OSTRACE3( "FCN
c1b00 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 TL_LOCKSTATE %d
c1b10 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 lock=%d\n", ((os
c1b20 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 2File*)id)->h, (
c1b30 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c (os2File*)id)->l
c1b40 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 ocktype );.
c1b50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c1b60 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 K;. }. }. r
c1b70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
c1b80 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OR;.}../*.** Ret
c1b90 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 urn the sector s
c1ba0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
c1bb0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 the underlying b
c1bc0 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a lock device for.
c1bd0 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ** the specified
c1be0 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
c1bf0 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 lmost always 512
c1c00 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 bytes, but may
c1c10 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 be.** larger for
c1c20 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a some devices..*
c1c30 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 *.** SQLite code
c1c40 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 assumes this fu
c1c50 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 nction cannot fa
c1c60 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 il. It also assu
c1c70 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 mes that.** if t
c1c80 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 wo files are cre
c1c90 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ated in the same
c1ca0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 file-system dir
c1cb0 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 ectory (i.e..**
c1cc0 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 a database and i
c1cd0 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ts journal file)
c1ce0 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 that the sector
c1cf0 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 size will be th
c1d00 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f e.** same for bo
c1d10 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
c1d20 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 t os2SectorSize(
c1d30 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c1d40 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 ){. return SQLI
c1d50 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
c1d60 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a R_SIZE;.}../*.**
c1d70 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 Return a vector
c1d80 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 of device chara
c1d90 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 cteristics..*/.s
c1da0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 tatic int os2Dev
c1db0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
c1dc0 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
c1dd0 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 *id){. return 0
c1de0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 ;.}.../*.** Char
c1df0 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 acter set conver
c1e00 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 sion objects use
c1e10 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 d by conversion
c1e20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 routines..*/.sta
c1e30 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 tic UconvObject
c1e40 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f ucUtf8 = NULL; /
c1e50 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 * convert betwee
c1e60 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d n UTF-8 and UCS-
c1e70 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 2 */.static Ucon
c1e80 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 vObject uclCp =
c1e90 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 NULL; /* conver
c1ea0 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 t between local
c1eb0 63 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 codepage and UCS
c1ec0 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c -2 */../*.** Hel
c1ed0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 per function to
c1ee0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 initialize the c
c1ef0 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 onversion object
c1f00 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 s from and to UT
c1f10 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 F-8..*/.static v
c1f20 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a oid initUconvObj
c1f30 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 ects( void ){.
c1f40 69 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f if( UniCreateUco
c1f50 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c nvObject( UTF_8,
c1f60 20 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c &ucUtf8 ) != UL
c1f70 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 S_SUCCESS ).
c1f80 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 ucUtf8 = NULL;.
c1f90 20 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 if ( UniCreateU
c1fa0 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 convObject( (Uni
c1fb0 43 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 Char *)L"@path=y
c1fc0 65 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d es", &uclCp ) !=
c1fd0 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 ULS_SUCCESS ).
c1fe0 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b uclCp = NULL;
c1ff0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 .}../*.** Helper
c2000 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 function to fre
c2010 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e e the conversion
c2020 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e objects from an
c2030 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 d to UTF-8..*/.s
c2040 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 tatic void freeU
c2050 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 convObjects( voi
c2060 64 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 d ){. if ( ucUt
c2070 66 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 f8 ). UniFree
c2080 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 UconvObject( ucU
c2090 74 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 tf8 );. if ( uc
c20a0 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 lCp ). UniFre
c20b0 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 eUconvObject( uc
c20c0 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 lCp );. ucUtf8
c20d0 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 = NULL;. uclCp
c20e0 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a = NULL;.}../*.**
c20f0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e Helper function
c2100 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d to convert UTF-
c2110 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 8 filenames to l
c2120 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 ocal OS/2 codepa
c2130 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 ge..** The two-s
c2140 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 tep process: fir
c2150 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 st convert the i
c2160 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 ncoming UTF-8 st
c2170 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 ring.** into UCS
c2180 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d -2 and then from
c2190 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 UCS-2 to the cu
c21a0 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a rrent codepage..
c21b0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
c21c0 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 char pointer has
c21d0 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f to be freed..*/
c21e0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f .static char *co
c21f0 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
c2200 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 p( const char *i
c2210 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 n ){. UniChar t
c2220 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 empPath[CCHMAXPA
c2230 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 TH];. char *out
c2240 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f = (char *)callo
c2250 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 c( CCHMAXPATH, 1
c2260 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 );.. if( !out
c2270 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c ). return NUL
c2280 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 L;.. if( !ucUtf
c2290 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 8 || !uclCp ).
c22a0 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 initUconvObjec
c22b0 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 ts();.. /* dete
c22c0 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 rmine string for
c22d0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c22e0 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 of UTF-8 which i
c22f0 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 s CP1208 */. if
c2300 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 ( UniStrToUcs( u
c2310 63 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c cUtf8, tempPath,
c2320 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 (char *)in, CCH
c2330 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 MAXPATH ) != ULS
c2340 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 _SUCCESS ). r
c2350 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 eturn out; /* if
c2360 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c conversion fail
c2370 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d s, return the em
c2380 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 pty string */..
c2390 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 /* conversion f
c23a0 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 or current codep
c23b0 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 age which can be
c23c0 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 used for paths
c23d0 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 */. UniStrFromU
c23e0 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 cs( uclCp, out,
c23f0 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 tempPath, CCHMAX
c2400 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 PATH );.. retur
c2410 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n out;.}../*.**
c2420 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c2430 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e to convert filen
c2440 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 ames from local
c2450 63 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d codepage to UTF-
c2460 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 8..** The two-st
c2470 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 ep process: firs
c2480 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e t convert the in
c2490 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d coming codepage-
c24a0 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 specific.** stri
c24b0 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e ng into UCS-2 an
c24c0 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d d then from UCS-
c24d0 32 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 2 to the codepag
c24e0 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 e of UTF-8..** T
c24f0 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 he returned char
c2500 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 pointer has to
c2510 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 be freed..**.**
c2520 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
c2530 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 non-static to b
c2540 65 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 e able to use th
c2550 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e is in shell.c an
c2560 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 d.** similar app
c2570 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 lications that t
c2580 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 ake command line
c2590 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 arguments..*/.c
c25a0 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 har *convertCpPa
c25b0 74 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 thToUtf8( const
c25c0 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e char *in ){. Un
c25d0 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 iChar tempPath[C
c25e0 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 CHMAXPATH];. ch
c25f0 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 ar *out = (char
c2600 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 *)calloc( CCHMAX
c2610 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 PATH, 1 );.. if
c2620 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 ( !out ). ret
c2630 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 urn NULL;.. if(
c2640 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c !ucUtf8 || !ucl
c2650 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f Cp ). initUco
c2660 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 nvObjects();..
c2670 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f /* conversion fo
c2680 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 r current codepa
c2690 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ge which can be
c26a0 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a used for paths *
c26b0 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f /. if( UniStrTo
c26c0 55 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 Ucs( uclCp, temp
c26d0 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e Path, (char *)in
c26e0 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 , CCHMAXPATH ) !
c26f0 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a = ULS_SUCCESS ).
c2700 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 return out;
c2710 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e /* if conversion
c2720 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 fails, return t
c2730 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 he empty string
c2740 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 */.. /* determi
c2750 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 ne string for th
c2760 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 e conversion of
c2770 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 UTF-8 which is C
c2780 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 P1208 */. UniSt
c2790 72 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 rFromUcs( ucUtf8
c27a0 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c , out, tempPath,
c27b0 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a CCHMAXPATH );..
c27c0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a return out;.}.
c27d0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 ./*.** This vect
c27e0 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 or defines all t
c27f0 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 he methods that
c2800 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 can operate on a
c2810 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c n.** sqlite3_fil
c2820 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 e for os2..*/.st
c2830 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
c2840 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 e3_io_methods os
c2850 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 2IoMethod = {.
c2860 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
c2870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
c2880 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c rsion */. os2Cl
c2890 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a ose,. os2Read,.
c28a0 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 os2Write,. os
c28b0 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 2Truncate,. os2
c28c0 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 Sync,. os2FileS
c28d0 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a ize,. os2Lock,.
c28e0 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f os2Unlock,. o
c28f0 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c s2CheckReservedL
c2900 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f ock,. os2FileCo
c2910 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 ntrol,. os2Sect
c2920 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 orSize,. os2Dev
c2930 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
c2940 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a cs.};../********
c2950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2990 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 ***.** Here ends
c29a0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
c29b0 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 that form the s
c29c0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
c29d0 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 s object..**.**
c29e0 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f The next block o
c29f0 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 f code implement
c2a00 73 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 s the VFS method
c2a10 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s..*************
c2a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c2a60 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
c2a70 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
c2a80 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a name in zBuf. z
c2a90 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 Buf must be big
c2aa0 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c enough to.** hol
c2ab0 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 d at pVfs->mxPat
c2ac0 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 hname characters
c2ad0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c2ae0 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 getTempname(int
c2af0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
c2b00 20 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e ){. static con
c2b10 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
c2b20 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 zChars[] =.
c2b30 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f "abcdefghijklmno
c2b40 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 pqrstuvwxyz".
c2b50 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e "ABCDEFGHIJKLMN
c2b60 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 OPQRSTUVWXYZ".
c2b70 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a "0123456789";.
c2b80 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 int i, j;. ch
c2b90 61 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b ar zTempPathBuf[
c2ba0 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 3];. PSZ zTempP
c2bb0 61 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d ath = (PSZ)&zTem
c2bc0 70 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 pPathBuf;. if(
c2bd0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
c2be0 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 ectory ){. zT
c2bf0 65 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 empPath = sqlite
c2c00 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
c2c10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
c2c20 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 f( DosScanEnv( (
c2c30 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 PSZ)"TEMP", &zTe
c2c40 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 mpPath ) ){.
c2c50 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 if( DosScanEnv
c2c60 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a ( (PSZ)"TMP", &z
c2c70 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 TempPath ) ){.
c2c80 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 if( DosSca
c2c90 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 nEnv( (PSZ)"TMPD
c2ca0 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 IR", &zTempPath
c2cb0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
c2cc0 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d ULONG ulDriveNum
c2cd0 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 = 0, ulDriveMap
c2ce0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
c2cf0 20 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 DosQueryCurrent
c2d00 44 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 Disk( &ulDriveNu
c2d10 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 m, &ulDriveMap )
c2d20 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 ;. spr
c2d30 69 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 intf( (char*)zTe
c2d40 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 mpPath, "%c:", (
c2d50 63 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 char)( 'A' + ulD
c2d60 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b riveNum - 1 ) );
c2d70 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
c2d80 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f }. }. }. /
c2d90 2a 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 * Strip off a tr
c2da0 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f ailing slashes o
c2db0 72 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f r backslashes, o
c2dc0 74 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c therwise we woul
c2dd0 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c d get *. * mul
c2de0 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 tiple (back)slas
c2df0 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 hes which causes
c2e00 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 DosOpen() to fa
c2e10 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 il.
c2e20 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 *. * Trailing
c2e30 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 spaces are not
c2e40 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e allowed, either.
c2e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2e60 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */.
c2e70 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c j = sqlite3Strl
c2e80 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b en30(zTempPath);
c2e90 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 . while( j > 0
c2ea0 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a && ( zTempPath[j
c2eb0 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a -1] == '\\' || z
c2ec0 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d TempPath[j-1] ==
c2ed0 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 '/'.
c2ee0 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d || zTem
c2ef0 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 pPath[j-1] == '
c2f00 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a ' ) ){. j--;.
c2f10 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b }. zTempPath[
c2f20 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 j] = '\0';. if(
c2f30 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 !sqlite3_temp_d
c2f40 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
c2f50 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 char *zTempPathU
c2f60 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 TF = convertCpPa
c2f70 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 thToUtf8( zTempP
c2f80 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ath );. sqlit
c2f90 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 e3_snprintf( nBu
c2fa0 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 f-30, zBuf,.
c2fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2fc0 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
c2fd0 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
c2fe0 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b zTempPathUTF );
c2ff0 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 . free( zTemp
c3000 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c PathUTF );. }el
c3010 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f se{. sqlite3_
c3020 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 snprintf( nBuf-3
c3030 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 0, zBuf,.
c3040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
c3050 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 %s\\"SQLITE_TEMP
c3060 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 _FILE_PREFIX, zT
c3070 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 empPath );. }.
c3080 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c j = sqlite3Strl
c3090 65 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 en30( zBuf );.
c30a0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
c30b0 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d ss( 20, &zBuf[j]
c30c0 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 );. for( i = 0
c30d0 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a ; i < 20; i++, j
c30e0 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a ++ ){. zBuf[j
c30f0 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 ] = (char)zChars
c3100 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 [ ((unsigned cha
c3110 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 r)zBuf[j])%(size
c3120 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b of(zChars)-1) ];
c3130 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d . }. zBuf[j] =
c3140 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 0;. OSTRACE2(
c3150 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 "TEMP FILENAME:
c3160 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 %s\n", zBuf );.
c3170 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c3180 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 K;.}.../*.** Tur
c3190 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 n a relative pat
c31a0 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c hname into a ful
c31b0 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 l pathname. Wri
c31c0 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 te the full.** p
c31d0 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 athname into zFu
c31e0 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 ll[]. zFull[] w
c31f0 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 ill be at least
c3200 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
c3210 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a .** bytes in siz
c3220 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
c3230 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 os2FullPathname
c3240 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
c3250 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 *pVfs,
c3260 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 /* Pointer to vf
c3270 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f s object */. co
c3280 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 nst char *zRelat
c3290 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 ive, /* Pos
c32a0 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 sibly relative i
c32b0 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 nput path */. i
c32c0 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 nt nFull,
c32d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
c32e0 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
c32f0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
c3300 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 char *zFull
c3310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c3320 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a Output buffer *
c3330 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 /.){. char *zRe
c3340 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 lativeCp = conve
c3350 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 rtUtf8PathToCp(
c3360 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 zRelative );. c
c3370 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d har zFullCp[CCHM
c3380 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a AXPATH] = "\0";.
c3390 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 char *zFullUTF
c33a0 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
c33b0 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f DosQueryPathInfo
c33c0 28 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 ( zRelativeCp, F
c33d0 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 IL_QUERYFULLNAME
c33e0 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 , zFullCp,.
c33f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3400 20 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 CCHMA
c3410 58 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 XPATH );. free(
c3420 20 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a zRelativeCp );.
c3430 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e zFullUTF = con
c3440 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 vertCpPathToUtf8
c3450 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 ( zFullCp );. s
c3460 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
c3470 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a nFull, zFull, z
c3480 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 FullUTF );. fre
c3490 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 e( zFullUTF );.
c34a0 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
c34b0 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
c34c0 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
c34d0 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 R;.}.../*.** Ope
c34e0 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 n a file..*/.sta
c34f0 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 tic int os2Open(
c3500 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c3510 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
c3520 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a /* Not used */.
c3530 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
c3540 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ame,
c3550 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
c3560 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ile */. sqlite3
c3570 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 _file *id,
c3580 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
c3590 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 the SQLite file
c35a0 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 handle here */.
c35b0 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
c35c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c35d0 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 * Open mode flag
c35e0 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 s */. int *pOut
c35f0 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
c3600 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 /* Status r
c3610 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 eturn flags */.)
c3620 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 {. HFILE h;. U
c3630 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 LONG ulFileAttri
c3640 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d bute = FILE_NORM
c3650 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 AL;. ULONG ulOp
c3660 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 enFlags = 0;. U
c3670 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 LONG ulOpenMode
c3680 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a = 0;. os2File *
c3690 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
c36a0 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 *)id;. APIRET r
c36b0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c36c0 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a ULONG ulAction;.
c36d0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b char *zNameCp;
c36e0 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 . char zTmpname
c36f0 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 [CCHMAXPATH+1];
c3700 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 /* Buffer to
c3710 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d hold name of tem
c3720 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 p file */.. /*
c3730 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
c3740 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 gument to this f
c3750 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c unction is NULL,
c3760 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a generate a . *
c3770 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 * temporary file
c3780 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 name to use .
c3790 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 */. if( !zName
c37a0 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 ){. int rc =
c37b0 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d getTempname(CCHM
c37c0 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 AXPATH+1, zTmpna
c37d0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 me);. if( rc!
c37e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
c37f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
c3800 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d }. zName =
c3810 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a zTmpname;. }..
c3820 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 . memset( pFile
c3830 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 , 0, sizeof(*pFi
c3840 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 le) );.. OSTRAC
c3850 45 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 E2( "OPEN want %
c3860 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a d\n", flags );..
c3870 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 if( flags & SQ
c3880 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
c3890 49 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 ITE ){. ulOpe
c38a0 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 nMode |= OPEN_AC
c38b0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a CESS_READWRITE;.
c38c0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c38d0 50 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e PEN read/write\n
c38e0 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 " );. }else{.
c38f0 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
c3900 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 OPEN_ACCESS_READ
c3910 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 ONLY;. OSTRAC
c3920 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f E1( "OPEN read o
c3930 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 nly\n" );. }..
c3940 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
c3950 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
c3960 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 ){. ulOpenFla
c3970 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f gs |= OPEN_ACTIO
c3980 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 N_OPEN_IF_EXISTS
c3990 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 | OPEN_ACTION_C
c39a0 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 REATE_IF_NEW;.
c39b0 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c39c0 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 N open new/creat
c39d0 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b e\n" );. }else{
c39e0 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 . ulOpenFlags
c39f0 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f |= OPEN_ACTION_
c3a00 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c OPEN_IF_EXISTS |
c3a10 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 OPEN_ACTION_FAI
c3a20 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 L_IF_NEW;. OS
c3a30 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 TRACE1( "OPEN op
c3a40 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 en existing\n" )
c3a50 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 ;. }.. if( fla
c3a60 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
c3a70 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 _MAIN_DB ){.
c3a80 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 ulOpenMode |= OP
c3a90 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e EN_SHARE_DENYNON
c3aa0 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 E;. OSTRACE1(
c3ab0 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 "OPEN share rea
c3ac0 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 d/write\n" );.
c3ad0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 }else{. ulOpe
c3ae0 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 nMode |= OPEN_SH
c3af0 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 ARE_DENYWRITE;.
c3b00 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c3b10 45 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e EN share read on
c3b20 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 ly\n" );. }..
c3b30 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
c3b40 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
c3b50 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 CLOSE ){. cha
c3b60 72 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 r pathUtf8[CCHMA
c3b70 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e XPATH];.#ifdef N
c3b80 44 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 DEBUG /* when de
c3b90 62 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 bugging we want
c3ba0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 to make sure it
c3bb0 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 is deleted */.
c3bc0 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 ulFileAttribut
c3bd0 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b e = FILE_HIDDEN;
c3be0 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 .#endif. os2F
c3bf0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 ullPathname( pVf
c3c00 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 s, zName, CCHMAX
c3c10 50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 PATH, pathUtf8 )
c3c20 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 ;. pFile->pat
c3c30 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 hToDel = convert
c3c40 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 Utf8PathToCp( pa
c3c50 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 thUtf8 );. OS
c3c60 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 TRACE1( "OPEN hi
c3c70 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 dden/delete on c
c3c80 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 lose file attrib
c3c90 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c utes\n" );. }el
c3ca0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 se{. pFile->p
c3cb0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
c3cc0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
c3cd0 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 OPEN normal file
c3ce0 20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b attribute\n" );
c3cf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 . }.. /* alway
c3d00 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d s open in random
c3d10 20 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 access mode for
c3d20 20 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 possibly better
c3d30 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 speed */. ulOp
c3d40 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 enMode |= OPEN_F
c3d50 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 LAGS_RANDOM;. u
c3d60 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
c3d70 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f N_FLAGS_FAIL_ON_
c3d80 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d ERROR;. ulOpenM
c3d90 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 ode |= OPEN_FLAG
c3da0 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 S_NOINHERIT;..
c3db0 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 zNameCp = conver
c3dc0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
c3dd0 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 Name );. rc = D
c3de0 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 osOpen( (PSZ)zNa
c3df0 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 meCp,.
c3e00 20 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 &h,.
c3e10 20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 &ulAct
c3e20 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ion,.
c3e30 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 0L,.
c3e40 20 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 ulFileA
c3e50 74 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 ttribute,.
c3e60 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e ulOpen
c3e70 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 Flags,.
c3e80 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 ulOpenMod
c3e90 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
c3ea0 20 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 (PEAOP2)NULL
c3eb0 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 );. free( zName
c3ec0 43 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 Cp );. if( rc !
c3ed0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c3ee0 20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 OSTRACE7( "OPE
c3ef0 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 N Invalid handle
c3f00 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 rc=%d: zName=%s
c3f10 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c , ulAction=%#lx,
c3f20 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c ulAttr=%#lx, ul
c3f30 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f Flags=%#lx, ulMo
c3f40 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 de=%#lx\n",.
c3f50 20 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e rc, zN
c3f60 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 ame, ulAction, u
c3f70 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 lFileAttribute,
c3f80 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f ulOpenFlags, ulO
c3f90 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 penMode );. i
c3fa0 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f f( pFile->pathTo
c3fb0 44 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 Del ). free
c3fc0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
c3fd0 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d el );. pFile-
c3fe0 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c >pathToDel = NUL
c3ff0 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 L;. if( flags
c4000 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
c4010 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
c4020 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 OSTRACE2( "OPE
c4030 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e N %d Invalid han
c4040 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 dle\n", ((flags
c4050 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 | SQLITE_OPEN_RE
c4060 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 ADONLY) & ~SQLIT
c4070 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
c4080 29 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ) );. retur
c4090 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c n os2Open( pVfs,
c40a0 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 zName, id,.
c40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c40c0 20 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 ((flags | SQLI
c40d0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
c40e0 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e ) & ~SQLITE_OPEN
c40f0 5f 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 _READWRITE),.
c4100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4110 20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a pOutFlags );.
c4120 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
c4130 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
c4140 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 ANTOPEN;. }.
c4150 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c }.. if( pOutFl
c4160 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 ags ){. *pOut
c4170 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 Flags = flags &
c4180 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c4190 57 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f WRITE ? SQLITE_O
c41a0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 PEN_READWRITE :
c41b0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c41c0 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 ONLY;. }.. pFi
c41d0 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f le->pMethod = &o
c41e0 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 s2IoMethod;. pF
c41f0 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 ile->h = h;. Op
c4200 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 enCounter(+1);.
c4210 20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e OSTRACE3( "OPEN
c4220 20 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 %d pOutFlags=%d
c4230 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
c4240 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 OutFlags );. re
c4250 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c4260 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
c4270 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a the named file..
c4280 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c4290 32 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 2Delete(. sqlit
c42a0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
c42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c42c0 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e /* Not used on
c42d0 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 os2 */. const
c42e0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
c42f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4300 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
c4310 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
c4320 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 int syncDir
c4330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c4340 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
c4350 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a ed on os2 */.){.
c4360 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
c4370 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a _ERROR;. char *
c4380 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f zFilenameCp = co
c4390 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
c43a0 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a p( zFilename );.
c43b0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
c43c0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
c43d0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b _IOERR_DELETE );
c43e0 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 . rc = DosDelet
c43f0 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d e( (PSZ)zFilenam
c4400 65 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a eCp );. free( z
c4410 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 FilenameCp );.
c4420 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 OSTRACE2( "DELET
c4430 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 E \"%s\"\n", zFi
c4440 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 lename );. retu
c4450 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f rn rc == NO_ERRO
c4460 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 R ? SQLITE_OK :
c4470 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
c4480 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ETE;.}../*.** Ch
c4490 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 eck the existanc
c44a0 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 e and status of
c44b0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
c44c0 63 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 c int os2Access(
c44d0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c44e0 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 pVfs, /*
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 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
c4510 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f *zFilename, /
c4520 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
c4530 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 o check */. int
c4540 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
c4550 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f /* Type o
c4560 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f f test to make o
c4570 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 n this file */.
c4580 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 int *pOut
c4590 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
c45a0 69 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 ite results here
c45b0 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 */.){. FILESTA
c45c0 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 TUS3 fsts3Config
c45d0 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 Info;. APIRET r
c45e0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c45f0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 char *zFilenameC
c4600 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 p = convertUtf8P
c4610 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 athToCp( zFilena
c4620 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 me );.. memset(
c4630 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 &fsts3ConfigInf
c4640 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 o, 0, sizeof(fst
c4650 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b s3ConfigInfo) );
c4660 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 . rc = DosQuery
c4670 50 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a PathInfo( (PSZ)z
c4680 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f FilenameCp, FIL_
c4690 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 STANDARD,.
c46a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c46b0 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 &fsts3ConfigI
c46c0 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 nfo, sizeof(FILE
c46d0 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 STATUS3) );. fr
c46e0 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 ee( zFilenameCp
c46f0 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 );. OSTRACE4( "
c4700 41 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 ACCESS fsts3Conf
c4710 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d igInfo.attrFile=
c4720 25 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 %d flags=%d rc=%
c4730 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
c4740 20 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 fsts3ConfigInf
c4750 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 o.attrFile, flag
c4760 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 s, rc );. switc
c4770 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 h( flags ){.
c4780 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
c4790 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 SS_READ:. cas
c47a0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
c47b0 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 EXISTS:. rc
c47c0 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 = (rc == NO_ERR
c47d0 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 OR);. OSTRA
c47e0 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 CE3( "ACCESS %s
c47f0 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 access of read a
c4800 6e 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 nd exists rc=%d
c4810 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 \n", zFilename,
c4820 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 rc );. brea
c4830 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
c4840 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
c4850 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 ITE:. rc =
c4860 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 (rc == NO_ERROR)
c4870 20 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 && ( (fsts3Conf
c4880 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 igInfo.attrFile
c4890 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 & FILE_READONLY)
c48a0 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f == 0 );. O
c48b0 53 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 STRACE3( "ACCESS
c48c0 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 %s access of re
c48d0 61 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c ad/write rc=%d\
c48e0 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 n", zFilename, r
c48f0 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b c );. break
c4900 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
c4910 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 assert( !"I
c4920 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 nvalid flags arg
c4930 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 ument" );. }.
c4940 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 *pOut = rc;. re
c4950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c4960 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }...#ifndef SQLI
c4970 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
c4980 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 ENSION./*.** Int
c4990 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
c49a0 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
c49b0 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
c49c0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
c49d0 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
c49e0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
c49f0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
c4a00 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a library..*/./*.*
c4a10 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 * Interfaces for
c4a20 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 opening a share
c4a30 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 d library, findi
c4a40 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a ng entry points.
c4a50 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 ** within the sh
c4a60 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e ared library, an
c4a70 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 d closing the sh
c4a80 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f ared library..*/
c4a90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 .static void *os
c4aa0 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 2DlOpen(sqlite3_
c4ab0 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
c4ac0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
c4ad0 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 ){. UCHAR loadE
c4ae0 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 rr[256];. HMODU
c4af0 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 LE hmod;. APIRE
c4b00 54 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 T rc;. char *zF
c4b10 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 ilenameCp = conv
c4b20 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c4b30 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 zFilename);. rc
c4b40 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 = DosLoadModule
c4b50 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 ((PSZ)loadErr, s
c4b60 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 izeof(loadErr),
c4b70 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d zFilenameCp, &hm
c4b80 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c od);. free(zFil
c4b90 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 enameCp);. retu
c4ba0 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f rn rc != NO_ERRO
c4bb0 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 R ? 0 : (void*)h
c4bc0 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e mod;.}./*.** A n
c4bd0 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 o-op since the e
c4be0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
c4bf0 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 urned on the Dos
c4c00 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e LoadModule call.
c4c10 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 .** os2Dlopen re
c4c20 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f turns zero if Do
c4c30 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e sLoadModule is n
c4c40 6f 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a ot successful..*
c4c50 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
c4c60 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2DlError(sqlite3
c4c70 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
c4c80 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
c4c90 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a Out){./* no-op *
c4ca0 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 /.}.static void
c4cb0 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 *os2DlSym(sqlite
c4cc0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
c4cd0 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 d *pHandle, cons
c4ce0 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 t char *zSymbol)
c4cf0 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 {. PFN pfn;. A
c4d00 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d PIRET rc;. rc =
c4d10 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 DosQueryProcAdd
c4d20 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 r((HMODULE)pHand
c4d30 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c le, 0L, zSymbol,
c4d40 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 &pfn);. if( rc
c4d50 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
c4d60 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 /* if the sy
c4d70 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 mbol itself was
c4d80 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 not found, searc
c4d90 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 h again for the
c4da0 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 same. * symb
c4db0 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 ol with an extra
c4dc0 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 underscore, tha
c4dd0 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 t might be neede
c4de0 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 d depending.
c4df0 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e * on the callin
c4e00 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a g convention */.
c4e10 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f char _zSymbo
c4e20 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 l[256] = "_";.
c4e30 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 strncat(_zSymb
c4e40 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 ol, zSymbol, 255
c4e50 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 );. rc = DosQ
c4e60 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d ueryProcAddr((HM
c4e70 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 ODULE)pHandle, 0
c4e80 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 L, _zSymbol, &pf
c4e90 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
c4ea0 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc != NO_ERROR
c4eb0 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e ? 0 : (void*)pfn
c4ec0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
c4ed0 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 os2DlClose(sqlit
c4ee0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
c4ef0 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
c4f00 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 DosFreeModule((H
c4f10 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b MODULE)pHandle);
c4f20 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 .}.#else /* if S
c4f30 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
c4f40 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 EXTENSION is def
c4f50 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 ined: */. #defi
c4f60 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 ne os2DlOpen 0.
c4f70 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 #define os2DlEr
c4f80 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 ror 0. #define
c4f90 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 os2DlSym 0. #de
c4fa0 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 fine os2DlClose
c4fb0 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
c4fc0 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 Write up to nBu
c4fd0 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f f bytes of rando
c4fe0 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e mness into zBuf.
c4ff0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c5000 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c s2Randomness(sql
c5010 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c5020 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
c5030 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 zBuf ){. int n
c5040 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 = 0;.#if defined
c5050 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 (SQLITE_TEST).
c5060 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 n = nBuf;. mems
c5070 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 et(zBuf, 0, nBuf
c5080 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 );.#else. int s
c5090 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a izeofULong = siz
c50a0 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 eof(ULONG);. if
c50b0 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 ( (int)sizeof(DA
c50c0 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 TETIME) <= nBuf
c50d0 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 - n ){. DATET
c50e0 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 IME x;. DosGe
c50f0 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 tDateTime(&x);.
c5100 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
c5110 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 n], &x, sizeof(x
c5120 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
c5130 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 eof(x);. }.. i
c5140 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c f( sizeofULong <
c5150 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
c5160 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 PPIB ppib;.
c5170 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b DosGetInfoBlock
c5180 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a s(NULL, &ppib);.
c5190 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
c51a0 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f [n], &ppib->pib_
c51b0 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f ulpid, sizeofULo
c51c0 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 ng);. n += si
c51d0 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a zeofULong;. }..
c51e0 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c51f0 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c5200 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a . PTIB ptib;.
c5210 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
c5220 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c ocks(&ptib, NULL
c5230 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
c5240 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 Buf[n], &ptib->t
c5250 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 ib_ptib2->tib2_u
c5260 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e ltid, sizeofULon
c5270 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a g);. n += siz
c5280 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 eofULong;. }..
c5290 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 /* if we still
c52a0 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 haven't filled t
c52b0 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 he buffer yet th
c52c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c e following will
c52d0 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 */. /* grab ev
c52e0 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e erything once in
c52f0 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 stead of making
c5300 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f several calls fo
c5310 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 r a single item
c5320 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 */. if( sizeofU
c5330 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c5340 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c ){. ULONG ul
c5350 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d SysInfo[QSV_MAX]
c5360 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 ;. DosQuerySy
c5370 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 sInfo(1L, QSV_MA
c5380 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 X, ulSysInfo, si
c5390 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f zeofULong * QSV_
c53a0 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 MAX);.. memcp
c53b0 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
c53c0 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f ysInfo[QSV_MS_CO
c53d0 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 UNT - 1], sizeof
c53e0 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d ULong);. n +=
c53f0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 sizeofULong;..
c5400 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f if( sizeofULo
c5410 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
c5420 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 {. memcpy(&
c5430 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 zBuf[n], &ulSysI
c5440 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e nfo[QSV_TIMER_IN
c5450 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a TERVAL - 1], siz
c5460 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
c5470 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
c5480 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 g;. }. if(
c5490 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
c54a0 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
c54b0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c54c0 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ], &ulSysInfo[QS
c54d0 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c V_TIME_LOW - 1],
c54e0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 sizeofULong);.
c54f0 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 n += sizeof
c5500 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 ULong;. }.
c5510 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c5520 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c5530 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
c5540 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
c5550 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 o[QSV_TIME_HIGH
c5560 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e - 1], sizeofULon
c5570 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 g);. n += s
c5580 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 izeofULong;.
c5590 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 }. if( sizeof
c55a0 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
c55b0 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 n ){. memcp
c55c0 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
c55d0 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 ysInfo[QSV_TOTAV
c55e0 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a AILMEM - 1], siz
c55f0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
c5600 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
c5610 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e g;. }. }.#en
c5620 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b dif.. return n;
c5630 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 .}../*.** Sleep
c5640 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 for a little whi
c5650 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 le. Return the
c5660 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 amount of time s
c5670 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 lept..** The arg
c5680 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d ument is the num
c5690 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
c56a0 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 nds we want to s
c56b0 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 leep..** The ret
c56c0 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 urn value is the
c56d0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
c56e0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
c56f0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 actually.** req
c5700 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 uested from the
c5710 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 underlying opera
c5720 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e ting system, a n
c5730 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d umber which.** m
c5740 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 ight be greater
c5750 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
c5760 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 the argument, b
c5770 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 ut not less.** t
c5780 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 han the argument
c5790 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c57a0 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 os2Sleep( sqlite
c57b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
c57c0 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 microsec ){. D
c57d0 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 osSleep( (micros
c57e0 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 ec/1000) );. re
c57f0 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d turn microsec;.}
c5800 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
c5810 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
c5820 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
c5830 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f zero value, beco
c5840 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a mes the result.*
c5850 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 * returned from
c5860 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
c5870 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 Time(). This is
c5880 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
c5890 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c g..*/.#ifdef SQL
c58a0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
c58b0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c58c0 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 current_time = 0
c58d0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
c58e0 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 Find the current
c58f0 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 time (in Univer
c5900 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 sal Coordinated
c5910 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 Time). Write th
c5920 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d e.** current tim
c5930 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 e and date as a
c5940 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 Julian Day numbe
c5950 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e r into *prNow an
c5960 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 d.** return 0.
c5970 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
c5980 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 time and date ca
c5990 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
c59a0 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 /.int os2Current
c59b0 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 Time( sqlite3_vf
c59c0 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
c59d0 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 *prNow ){. doub
c59e0 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 le now;. SHORT
c59f0 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 minute; /* needs
c5a00 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 to be able to c
c5a10 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 ope with negativ
c5a20 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 e timezone offse
c5a30 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 t */. USHORT se
c5a40 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 cond, hour,.
c5a50 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c day, month,
c5a60 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d year;. DATETIM
c5a70 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 E dt;. DosGetDa
c5a80 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 teTime( &dt );.
c5a90 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 second = (USHOR
c5aa0 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 T)dt.seconds;.
c5ab0 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 minute = (SHORT)
c5ac0 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e dt.minutes + dt.
c5ad0 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 timezone;. hour
c5ae0 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f = (USHORT)dt.ho
c5af0 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 urs;. day = (US
c5b00 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d HORT)dt.day;. m
c5b10 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 onth = (USHORT)d
c5b20 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 t.month;. year
c5b30 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 = (USHORT)dt.yea
c5b40 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 r;.. /* Calcula
c5b50 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a tions from http:
c5b60 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c //www.astro.keel
c5b70 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 e.ac.uk/~rno/Ast
c5b80 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a ronomy/hjd.html.
c5b90 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e http://www.
c5ba0 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 astro.keele.ac.u
c5bb0 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 k/~rno/Astronomy
c5bc0 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 /hjd-0.1.c */.
c5bd0 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 /* Calculate the
c5be0 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a Julian days */.
c5bf0 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 now = day - 32
c5c00 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 076 +. 1461*(
c5c10 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d year + 4800 + (m
c5c20 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 onth - 14)/12)/4
c5c30 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 +. 367*(mont
c5c40 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d h - 2 - (month -
c5c50 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 14)/12*12)/12 -
c5c60 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 . 3*((year +
c5c70 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 4900 + (month -
c5c80 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 14)/12)/100)/4;.
c5c90 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 . /* Add the fr
c5ca0 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 actional hours,
c5cb0 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 mins and seconds
c5cc0 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f */. now += (ho
c5cd0 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b ur + 12.0)/24.0;
c5ce0 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 . now += minute
c5cf0 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b /1440.0;. now +
c5d00 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 = second/86400.0
c5d10 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 ;. *prNow = now
c5d20 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
c5d30 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
c5d40 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
c5d50 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
c5d60 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
c5d70 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 time/86400.0 + 2
c5d80 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 440587.5;. }.#e
c5d90 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b ndif. return 0;
c5da0 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .}..static int o
c5db0 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 s2GetLastError(s
c5dc0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
c5dd0 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
c5de0 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 *zBuf){. retur
c5df0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e n 0;.}../*.** In
c5e00 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
c5e10 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 nitialize the op
c5e20 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
c5e30 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c nterface..*/.SQL
c5e40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c5e50 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
c5e60 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
c5e70 74 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d te3_vfs os2Vfs =
c5e80 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 {. 1,
c5e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
c5ea0 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a rsion */. siz
c5eb0 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 eof(os2File),
c5ec0 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 /* szOsFile */.
c5ed0 20 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 CCHMAXPATH,
c5ee0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
c5ef0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
c5f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c5f10 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f pNext */. "o
c5f20 73 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 s2",
c5f30 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 /* zName */.
c5f40 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
c5f50 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
c5f60 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c */.. os2Open,
c5f70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f /* xO
c5f80 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 pen */. os2De
c5f90 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a lete, /*
c5fa0 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 xDelete */.
c5fb0 6f 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 os2Access,
c5fc0 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f /* xAccess */
c5fd0 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 . os2FullPath
c5fe0 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c name, /* xFull
c5ff0 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 Pathname */.
c6000 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 os2DlOpen,
c6010 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f /* xDlOpen */
c6020 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c . os2DlError,
c6030 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
c6040 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c ror */. os2Dl
c6050 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Sym, /*
c6060 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f xDlSym */. o
c6070 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 s2DlClose,
c6080 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f /* xDlClose */
c6090 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 . os2Randomne
c60a0 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 ss, /* xRand
c60b0 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 omness */. os
c60c0 32 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 2Sleep,
c60d0 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 /* xSleep */.
c60e0 20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 os2CurrentTime
c60f0 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 , /* xCurrent
c6100 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 Time */. os2G
c6110 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f etLastError /
c6120 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 * xGetLastError
c6130 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 */. };. sqlite
c6140 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 3_vfs_register(&
c6150 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e os2Vfs, 1);. in
c6160 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 itUconvObjects()
c6170 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c6180 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 E_OK;.}.SQLITE_A
c6190 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
c61a0 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 s_end(void){. f
c61b0 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 reeUconvObjects(
c61c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
c61d0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 TE_OK;.}..#endif
c61e0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 /* SQLITE_OS_OS
c61f0 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2 */../*********
c6200 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
c6210 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os2.c **********
c6220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6240 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
c6250 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
c6260 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a os_unix.c *****
c6270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6290 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
c62a0 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 May 22.**.** Th
c62b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
c62c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
c62d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
c62e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
c62f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
c6300 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
c6310 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
c6320 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
c6330 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
c6340 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
c6350 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
c6360 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
c6370 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
c6380 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
c6390 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
c63a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
c63b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
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 2a 2a 2a 2a 2a 2a ****************
c63f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
c6410 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
c6420 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 ains the VFS imp
c6430 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 lementation for
c6440 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 unix-like operat
c6450 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 ing systems.** i
c6460 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 nclude Linux, Ma
c6470 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c cOSX, *BSD, QNX,
c6480 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 VxWorks, AIX, H
c6490 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e PUX, and others.
c64a0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
c64b0 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 actually severa
c64c0 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 l different VFS
c64d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
c64e0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a in this file..**
c64f0 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 The differences
c6500 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 are in the way
c6510 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e that file lockin
c6520 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 g is done. The
c6530 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 default.** imple
c6540 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 mentation uses P
c6550 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f osix Advisory Lo
c6560 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 cks. Alternativ
c6570 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
c6580 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 s.** use flock()
c6590 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 , dot-files, var
c65a0 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 ious proprietary
c65b0 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 locking schemas
c65c0 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 , or simply.** s
c65d0 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 kip locking all
c65e0 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 together..**.**
c65f0 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 This source file
c6600 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e is organized in
c6610 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 to divisions whe
c6620 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 re the logic for
c6630 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 various.** subf
c6640 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 unctions is cont
c6650 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 ained within the
c6660 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 appropriate div
c6670 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a ision. PLEASE.*
c6680 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 * KEEP THE STRUC
c6690 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c TURE OF THIS FIL
c66a0 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 E INTACT. New c
c66b0 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c ode should be pl
c66c0 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 aced.** in the c
c66d0 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 orrect division
c66e0 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c and should be cl
c66f0 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a early labeled..*
c6700 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 *.** The layout
c6710 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 of divisions is
c6720 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
c6730 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 * * General-p
c6740 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 urpose declarati
c6750 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 ons and utility
c6760 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 functions..**
c6770 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 * Unique file I
c6780 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 D logic used by
c6790 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 VxWorks..** *
c67a0 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 Various locking
c67b0 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 primitive imple
c67c0 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 mentations (all
c67d0 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 except proxy loc
c67e0 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b king):.** +
c67f0 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 for Posix Advis
c6800 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 ory Locks.**
c6810 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f + for no-op lo
c6820 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f cks.** + fo
c6830 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 r dot-file locks
c6840 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 .** + for f
c6850 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a lock() locking.*
c6860 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d * + for nam
c6870 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
c6880 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 ks (VxWorks only
c6890 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 ).** + for
c68a0 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c AFP filesystem l
c68b0 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c ocks (MacOSX onl
c68c0 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 y).** * sqlit
c68d0 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 e3_file methods
c68e0 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 not associated w
c68f0 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 ith locking..**
c6900 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 * Definitions
c6910 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d of sqlite3_io_m
c6920 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 ethods objects f
c6930 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a or all locking.*
c6940 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 * methods p
c6950 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e lus "finder" fun
c6960 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 ctions for each
c6970 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a locking method..
c6980 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f ** * sqlite3_
c6990 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 vfs method imple
c69a0 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 mentations..**
c69b0 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d * Locking prim
c69c0 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 itives for the p
c69d0 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e roxy uber-lockin
c69e0 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 g-method. (MacOS
c69f0 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 X only).** *
c6a00 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 Definitions of s
c6a10 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
c6a20 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 ts for all locki
c6a30 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 ng methods.**
c6a40 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e plus implemen
c6a50 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 tations of sqlit
c6a60 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 e3_os_init() and
c6a70 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
c6a80 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 )..*/.#if SQLITE
c6a90 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 _OS_UNIX
c6aa0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 /* This fi
c6ab0 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e le is used on un
c6ac0 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a ix only */../*.*
c6ad0 2a 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69 * There are vari
c6ae0 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 ous methods for
c6af0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 file locking use
c6b00 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 d for concurrenc
c6b10 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a y.** control:.**
c6b20 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c .** 1. POSIX l
c6b30 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 ocking (the defa
c6b40 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f ult),.** 2. No
c6b50 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 locking,.** 3
c6b60 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 . Dot-file locki
c6b70 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 ng,.** 4. floc
c6b80 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 k() locking,.**
c6b90 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 5. AFP locking
c6ba0 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 (OSX only),.**
c6bb0 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 6. Named POSIX
c6bc0 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 semaphores (VXW
c6bd0 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 orks only),.**
c6be0 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 7. proxy lockin
c6bf0 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a g. (OSX only).**
c6c00 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c .** Styles 4, 5,
c6c10 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 and 7 are only
c6c20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c available of SQL
c6c30 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c6c40 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 NG_STYLE.** is d
c6c50 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 efined to 1. Th
c6c60 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f e SQLITE_ENABLE_
c6c70 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c LOCKING_STYLE al
c6c80 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d so enables autom
c6c90 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f atic.** selectio
c6ca0 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 n of the appropr
c6cb0 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 iate locking sty
c6cc0 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 le based on the
c6cd0 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 filesystem.** wh
c6ce0 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ere the database
c6cf0 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a is located. .*
c6d00 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
c6d10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
c6d20 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 KING_STYLE).# i
c6d30 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
c6d40 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 E__).# define
c6d50 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c6d60 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 OCKING_STYLE 1.#
c6d70 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 else.# defi
c6d80 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ne SQLITE_ENABLE
c6d90 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 _LOCKING_STYLE 0
c6da0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 .# endif.#endif
c6db0 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 ../*.** Define t
c6dc0 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 he OS_VXWORKS pr
c6dd0 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 e-processor macr
c6de0 6f 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 o to 1 if buildi
c6df0 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b ng on .** vxwork
c6e00 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 s, or 0 otherwis
c6e10 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 e..*/.#ifndef OS
c6e20 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 _VXWORKS.# if d
c6e30 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 efined(__RTP__)
c6e40 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f || defined(_WRS_
c6e50 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 KERNEL).# def
c6e60 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 ine OS_VXWORKS 1
c6e70 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 .# else.# de
c6e80 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 fine OS_VXWORKS
c6e90 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 0.# endif.#endi
c6ea0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 f../*.** These #
c6eb0 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 defines should e
c6ec0 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 nable >2GB file
c6ed0 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 support on Posix
c6ee0 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 if the.** under
c6ef0 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 lying operating
c6f00 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 system supports
c6f10 69 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c it. If the OS l
c6f20 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 acks.** large fi
c6f30 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 le support, thes
c6f40 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f e should be no-o
c6f50 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 ps..**.** Large
c6f60 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e file support can
c6f70 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 be disabled usi
c6f80 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f ng the -DSQLITE_
c6f90 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 DISABLE_LFS swit
c6fa0 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d ch.** on the com
c6fb0 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 piler command li
c6fc0 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 ne. This is nec
c6fd0 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 essary if you ar
c6fe0 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f e compiling.** o
c6ff0 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 n a recent machi
c7000 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 ne (ex: RedHat 7
c7010 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 .2) but you want
c7020 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f your code to wo
c7030 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 rk.** on an olde
c7040 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 r machine (ex: R
c7050 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 edHat 6.0). If
c7060 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 you compile on R
c7070 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 edHat 7.2.** wit
c7080 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e hout this option
c7090 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e , LFS is enable.
c70a0 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e But LFS does n
c70b0 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 ot exist in the
c70c0 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 kernel.** in Red
c70d0 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 Hat 6.0, so the
c70e0 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e code won't work.
c70f0 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 Hence, for max
c7100 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 imum binary.** p
c7110 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 ortability you s
c7120 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a hould omit LFS..
c7130 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f **.** The previo
c7140 75 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73 us paragraph was
c7150 20 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 written in 2005
c7160 2e 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61 . (This paragra
c7170 70 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a ph is written.**
c7180 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 on 2008-11-28.)
c7190 20 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c These days, all
c71a0 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 Linux kernels s
c71b0 75 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c upport large fil
c71c0 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 es, so.** you sh
c71d0 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 ould probably le
c71e0 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e ave LFS enabled.
c71f0 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 But some embed
c7200 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 ded platforms mi
c7210 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 ght.** lack LFS
c7220 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
c7230 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 e SQLITE_DISABLE
c7240 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 _LFS macro might
c7250 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c still be useful
c7260 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
c7270 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a ITE_DISABLE_LFS.
c7280 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f # define _LARGE_
c7290 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 FILE 1.# i
c72a0 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 fndef _FILE_OFFS
c72b0 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 ET_BITS.# defi
c72c0 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f ne _FILE_OFFSET_
c72d0 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a BITS 64.# endif.
c72e0 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 # define _LARGEF
c72f0 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e ILE_SOURCE 1.#en
c7300 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 dif../*.** stand
c7310 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 ard include file
c7320 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c s..*/.#include <
c7330 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e sys/types.h>.#in
c7340 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e clude <sys/stat.
c7350 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e h>.#include <fcn
c7360 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c tl.h>.#include <
c7370 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 unistd.h>.#inclu
c7380 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a de <sys/time.h>.
c7390 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e #include <errno.
c73a0 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 h>..#if SQLITE_E
c73b0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
c73c0 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 YLE.# include <s
c73d0 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 ys/ioctl.h>.# if
c73e0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 OS_VXWORKS.# i
c73f0 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 nclude <semaphor
c7400 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 e.h>.# include
c7410 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 <limits.h>.# els
c7420 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 e.# include <sy
c7430 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 s/file.h>.# inc
c7440 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e lude <sys/param.
c7450 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 h>.# include <s
c7460 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e ys/mount.h>.# en
c7470 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 dif.#endif /* SQ
c7480 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c7490 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a ING_STYLE */../*
c74a0 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f .** If we are to
c74b0 20 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c be thread-safe,
c74c0 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 include the pth
c74d0 72 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 reads header and
c74e0 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 define.** the S
c74f0 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 QLITE_UNIX_THREA
c7500 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 DS macro..*/.#if
c7510 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
c7520 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 FE.# define SQLI
c7530 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 TE_UNIX_THREADS
c7540 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 1.#endif../*.**
c7550 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 Default permissi
c7560 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e ons when creatin
c7570 67 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a g a new file.*/.
c7580 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
c7590 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d EFAULT_FILE_PERM
c75a0 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 ISSIONS.# define
c75b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
c75c0 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 FILE_PERMISSIONS
c75d0 20 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0644.#endif../*
c75e0 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 . ** Default per
c75f0 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 missions when cr
c7600 65 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 eating auto prox
c7610 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 y dir. */.#ifnde
c7620 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 f SQLITE_DEFAULT
c7630 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
c7640 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 SIONS.# define S
c7650 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 QLITE_DEFAULT_PR
c7660 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f OXYDIR_PERMISSIO
c7670 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a NS 0755.#endif..
c7680 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 /*.** Maximum su
c7690 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e pported path-len
c76a0 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 gth..*/.#define
c76b0 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 MAX_PATHNAME 512
c76c0 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 ../*.** Only set
c76d0 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 the lastErrno i
c76e0 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 f the error code
c76f0 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 is a real error
c7700 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e and not .** a n
c7710 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 ormal expected r
c7720 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 eturn code of SQ
c7730 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c LITE_BUSY or SQL
c7740 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e ITE_OK.*/.#defin
c7750 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 e IS_LOCK_ERROR(
c7760 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 x) ((x != SQLIT
c7770 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 E_OK) && (x != S
c7780 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f QLITE_BUSY)).../
c7790 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 *.** Sometimes,
c77a0 61 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e after a file han
c77b0 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 dle is closed by
c77c0 20 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c SQLite, the fil
c77d0 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 e descriptor.**
c77e0 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 cannot be closed
c77f0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e immediately. In
c7800 20 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e these cases, in
c7810 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 stances of the f
c7820 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
c7830 63 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74 cture are used t
c7840 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 o store the file
c7850 20 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c descriptor whil
c7860 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e e waiting for an
c7870 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 .** opportunity
c7880 74 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 to either close
c7890 6f 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a or reuse it..*/.
c78a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 typedef struct U
c78b0 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 nixUnusedFd Unix
c78c0 55 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 UnusedFd;.struct
c78d0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a UnixUnusedFd {.
c78e0 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 int fd;
c78f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
c7900 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
c7910 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 o close */. int
c7920 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
c7930 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
c7940 74 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 this file descri
c7950 70 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 ptor was opened
c7960 77 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e with */. UnixUn
c7970 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 usedFd *pNext;
c7980 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 /* Next unus
c7990 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
c79a0 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 or on same file
c79b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
c79c0 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
c79d0 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 ure is subclass
c79e0 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 of sqlite3_file
c79f0 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 specific to the
c7a00 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c unix.** VFS impl
c7a10 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a ementations..*/.
c7a20 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 typedef struct u
c7a30 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 nixFile unixFile
c7a40 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c ;.struct unixFil
c7a50 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f e {. sqlite3_io
c7a60 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a _methods const *
c7a70 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 pMethod; /* Alw
c7a80 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e ays the first en
c7a90 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 try */. struct
c7aa0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
c7ab0 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 en; /* Inf
c7ac0 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e o about all open
c7ad0 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e fd's on this in
c7ae0 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ode */. struct
c7af0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c unixLockInfo *pL
c7b00 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 ock; /* Inf
c7b10 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e o about locks on
c7b20 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 this inode */.
c7b30 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 int h;
c7b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7b50 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
c7b60 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e scriptor */. in
c7b70 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 t dirfd;
c7b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c7b90 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
c7ba0 72 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 r for the direct
c7bb0 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ory */. unsigne
c7bc0 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b d char locktype;
c7bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
c7be0 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 type of lock he
c7bf0 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f ld on this fd */
c7c00 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f . int lastErrno
c7c10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c7c20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 /* The unix
c7c30 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c errno from the l
c7c40 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f ast I/O error */
c7c50 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 . void *locking
c7c60 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 Context;
c7c70 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 /* Locking s
c7c80 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 tyle specific st
c7c90 61 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 ate */. UnixUnu
c7ca0 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 sedFd *pUnused;
c7cb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
c7cc0 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 -allocated UnixU
c7cd0 6e 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 nusedFd */. int
c7ce0 20 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 fileFlags;
c7cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c7d00 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c Miscellanous fl
c7d10 61 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ags */.#if SQLIT
c7d20 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c7d30 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 _STYLE. int ope
c7d40 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 nFlags;
c7d50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
c7d60 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 flags specified
c7d70 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 at open() */.#e
c7d80 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
c7d90 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
c7da0 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
c7db0 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 . pthread_t tid
c7dc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c7dd0 20 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 /* The threa
c7de0 64 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 d that "owns" th
c7df0 69 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 is unixFile */.#
c7e00 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 endif.#if OS_VXW
c7e10 4f 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c ORKS. int isDel
c7e20 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ete;
c7e30 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 /* Delet
c7e40 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 e on close if tr
c7e50 75 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 ue */. struct v
c7e60 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 xworksFileId *pI
c7e70 64 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 d; /* Uniq
c7e80 75 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 ue file ID */.#e
c7e90 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 ndif.#ifndef NDE
c7ea0 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 BUG. /* The nex
c7eb0 74 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 t group of varia
c7ec0 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f bles are used to
c7ed0 20 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f track whether o
c7ee0 72 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 r not the. ** t
c7ef0 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
c7f00 65 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 er in bytes 24-2
c7f10 37 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 7 of database fi
c7f20 6c 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a les are updated.
c7f30 20 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e ** whenever an
c7f40 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 y part of the da
c7f50 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 tabase changes.
c7f60 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
c7f70 75 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 ult will. ** oc
c7f80 63 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 cur if a file is
c7f90 20 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 updated without
c7fa0 20 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 also updating t
c7fb0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 he transaction.
c7fc0 20 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 ** counter. Th
c7fd0 69 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 is test is made
c7fe0 74 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f to avoid new pro
c7ff0 62 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f blems similar to
c8000 20 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 the. ** one de
c8010 73 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 scribed by ticke
c8020 74 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 t #3584. . */.
c8030 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 unsigned char t
c8040 72 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 ransCntrChng;
c8050 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74 /* True if the t
c8060 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
c8070 65 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 er changed */.
c8080 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 unsigned char db
c8090 55 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f Update; /
c80a0 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 * True if any pa
c80b0 72 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 rt of database f
c80c0 69 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 ile changed */.
c80d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
c80e0 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 nNormalWrite;
c80f0 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 /* True if in a
c8100 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 normal write ope
c8110 72 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 ration */.#endif
c8120 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c8130 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 EST. /* In test
c8140 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 mode, increase
c8150 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 the size of this
c8160 20 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 structure a bit
c8170 20 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 so that . ** i
c8180 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e t is larger than
c8190 20 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 the struct Cras
c81a0 68 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e hFile defined in
c81b0 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 test6.c.. */.
c81c0 20 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 char aPadding[3
c81d0 32 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2];.#endif.};../
c81e0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
c81f0 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 ng macros define
c8200 20 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c bits in unixFil
c8210 65 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 e.fileFlags.*/.#
c8220 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 define SQLITE_WH
c8230 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 OLE_FILE_LOCKING
c8240 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 0x0001 /* Us
c8250 65 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 e whole-file loc
c8260 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 king */../*.** I
c8270 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 nclude code that
c8280 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
c8290 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a l os_*.c files.*
c82a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c82b0 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d * Include os_com
c82c0 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 mon.h in the mid
c82d0 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 dle of os_unix.c
c82e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c82f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c8300 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
c8310 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8340 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
c8350 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
c8360 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
c8370 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
c8380 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
c8390 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
c83a0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
c83b0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
c83c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
c83d0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
c83e0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
c83f0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
c8400 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
c8410 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
c8420 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
c8430 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
c8440 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
c8450 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
c8460 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
c8470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c84a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c84b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
c84c0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
c84d0 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 macros and a li
c84e0 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 ttle bit of code
c84f0 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
c8500 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 to.** all of the
c8510 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 platform-specif
c8520 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 ic files (os_*.c
c8530 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 ) and is #includ
c8540 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a ed into those.**
c8550 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 files..**.** Th
c8560 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 is file should b
c8570 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 e #included by t
c8580 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 he os_*.c files
c8590 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 only. It is not
c85a0 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 a.** general pu
c85b0 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c rpose header fil
c85c0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f e..*/.#ifndef _O
c85d0 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
c85e0 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
c85f0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
c8600 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
c8610 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
c8620 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
c8630 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
c8640 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
c8650 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
c8660 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
c8670 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
c8680 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
c8690 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
c86a0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
c86b0 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
c86c0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
c86d0 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
c86e0 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
c86f0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
c8700 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
c8710 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
c8720 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
c8730 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
c8740 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
c8750 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
c8760 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d sqlite3OSTrace =
c8770 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 0;.#define OSTR
c8780 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 ACE1(X)
c8790 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
c87a0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
c87b0 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 gPrintf(X).#defi
c87c0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
c87d0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
c87e0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c87f0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c8800 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
c8810 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 ACE3(X,Y,Z)
c8820 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
c8830 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
c8840 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 gPrintf(X,Y,Z).#
c8850 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
c8860 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 X,Y,Z,A) if( s
c8870 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
c8880 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c8890 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 tf(X,Y,Z,A).#def
c88a0 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
c88b0 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 ,Z,A,B) if( sqli
c88c0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
c88d0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
c88e0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
c88f0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
c8900 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 Z,A,B,C) \. i
c8910 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
c8920 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
c8930 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
c8940 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c8950 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
c8960 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
c8970 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
c8980 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
c8990 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c Y,Z,A,B,C,D).#el
c89a0 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 se.#define OSTRA
c89b0 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f CE1(X).#define O
c89c0 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 STRACE2(X,Y).#de
c89d0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
c89e0 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
c89f0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 RACE4(X,Y,Z,A).#
c8a00 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
c8a10 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
c8a20 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
c8a30 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
c8a40 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
c8a50 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a A,B,C,D).#endif.
c8a60 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
c8a70 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 r performance tr
c8a80 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 acing. Normally
c8a90 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e turned off. On
c8aa0 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 ly works.** on i
c8ab0 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 486 hardware..*/
c8ac0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 .#ifdef SQLITE_P
c8ad0 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 ERFORMANCE_TRACE
c8ae0 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
c8af0 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
c8b00 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
c8b10 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
c8b20 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
c8b30 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
c8b40 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
c8b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
c8b60 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
c8b70 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
c8b80 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c8b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
c8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
c8bb0 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
c8bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
c8bf0 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
c8c00 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
c8c10 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
c8c20 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
c8c30 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
c8c40 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
c8c50 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
c8c60 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
c8c70 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
c8c80 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
c8c90 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
c8ca0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
c8cb0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
c8cc0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
c8cd0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
c8ce0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
c8cf0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
c8d00 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
c8d10 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
c8d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8d60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
c8d70 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
c8d80 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
c8d90 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
c8da0 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
c8db0 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
c8dc0 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23 class CPUs..*/.#
c8dd0 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 ifndef _HWTIME_H
c8de0 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d _.#define _HWTIM
c8df0 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 E_H_../*.** The
c8e00 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
c8e10 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 e only works on
c8e20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f pentium-class (o
c8e30 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 r newer) process
c8e40 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 ors..** It uses
c8e50 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 the RDTSC opcode
c8e60 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 to read the cyc
c8e70 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f le count value o
c8e80 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f ut of the.** pro
c8e90 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 cessor and retur
c8ea0 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 ns that value.
c8eb0 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 This can be used
c8ec0 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a for high-res.**
c8ed0 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 profiling..*/.#
c8ee0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
c8ef0 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 UC__) || defined
c8f00 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c (_MSC_VER)) && \
c8f10 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 . (defined(
c8f20 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 i386) || defined
c8f30 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 (__i386__) || de
c8f40 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a fined(_M_IX86)).
c8f50 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f . #if defined(_
c8f60 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e _GNUC__).. __in
c8f70 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
c8f80 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
c8f90 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 me(void){. u
c8fa0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 nsigned int lo,
c8fb0 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f hi;. __asm__
c8fc0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
c8fd0 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c rdtsc" : "=a" (l
c8fe0 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a o), "=d" (hi));.
c8ff0 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c return (sql
c9000 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c ite_uint64)hi <<
c9010 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 32 | lo;. }..
c9020 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f #elif defined(_
c9030 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 MSC_VER).. __de
c9040 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f clspec(naked) __
c9050 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 inline sqlite_ui
c9060 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c nt64 __cdecl sql
c9070 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
c9080 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 {. __asm {.
c9090 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 rdtsc.
c90a0 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b ret ;
c90b0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 return value at
c90c0 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a EDX:EAX. }.
c90d0 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 }.. #endif..#
c90e0 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
c90f0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
c9100 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a ed(__x86_64__)).
c9110 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
c9120 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
c9130 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
c9140 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
c9150 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 long val;.
c9160 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 __asm__ __volati
c9170 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 le__ ("rdtsc" :
c9180 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 "=A" (val));.
c9190 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 return val;.
c91a0 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 }. .#elif (defi
c91b0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
c91c0 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f defined(__ppc__
c91d0 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
c91e0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
c91f0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
c9200 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
c9210 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 ed long long ret
c9220 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 val;. unsig
c9230 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 ned long junk;.
c9240 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
c9250 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a olatile__ ("\n\.
c9260 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 1:
c9270 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a mftbu %1\n\.
c9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c9290 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c mftb %L0\n\
c92a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c92b0 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c mftbu %0\n\
c92c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c92d0 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 cmpw %0,%1
c92e0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
c92f0 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 bne 1b
c9300 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
c9310 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 : "=r" (retv
c9320 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 al), "=r" (junk)
c9330 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
c9340 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c retval;. }..#el
c9350 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 se.. #error Nee
c9360 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e d implementation
c9370 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d of sqlite3Hwtim
c9380 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 e() for your pla
c9390 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a tform... /*. *
c93a0 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 * To compile wit
c93b0 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e hout implementin
c93c0 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 g sqlite3Hwtime(
c93d0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
c93e0 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 orm,. ** you ca
c93f0 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f n remove the abo
c9400 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 ve #error and us
c9410 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a e the following.
c9420 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 ** stub functi
c9430 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f on. You will lo
c9440 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 se timing suppor
c9450 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 t for many. **
c9460 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 of the debugging
c9470 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 and testing uti
c9480 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 lities, but it s
c9490 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 hould at. ** le
c94a0 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 ast compile and
c94b0 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 run.. */.SQLITE
c94c0 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 _PRIVATE sqlit
c94d0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
c94e0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 Hwtime(void){ re
c94f0 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 turn ((sqlite_ui
c9500 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 nt64)0); }..#end
c9510 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 if..#endif /* !d
c9520 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 efined(_HWTIME_H
c9530 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
c9540 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 ****** End of hw
c9550 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a time.h *********
c9560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9580 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
c9590 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
c95a0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
c95b0 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e off in os_common
c95c0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
c95d0 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 *****/..static s
c95e0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 qlite_uint64 g_s
c95f0 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c tart;.static sql
c9600 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 ite_uint64 g_ela
c9610 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 psed;.#define TI
c9620 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 MER_START
c9630 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 g_start=sqlite3H
c9640 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 wtime().#define
c9650 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 TIMER_END
c9660 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 g_elapsed=sqli
c9670 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 te3Hwtime()-g_st
c9680 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 art.#define TIME
c9690 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f R_ELAPSED g_
c96a0 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 elapsed.#else.#d
c96b0 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 efine TIMER_STAR
c96c0 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f T.#define TIMER_
c96d0 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 END.#define TIME
c96e0 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 R_ELAPSED ((
c96f0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 sqlite_uint64)0)
c9700 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
c9710 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 f we compile wit
c9720 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 h the SQLITE_TES
c9730 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 T macro set, the
c9740 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
c9750 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 block.** of code
c9760 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 will give us th
c9770 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d e ability to sim
c9780 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f ulate a disk I/O
c9790 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a error. This.**
c97a0 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 is used for tes
c97b0 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 ting the I/O rec
c97c0 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a overy logic..*/.
c97d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
c97e0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
c97f0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
c9800 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 or_hit = 0;
c9810 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
c9820 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 number of I/O Er
c9830 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
c9840 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
c9850 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 o_error_hardhit
c9860 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e = 0; /* N
c9870 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e umber of non-ben
c9880 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 ign errors */.SQ
c9890 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c98a0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
c98b0 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 nding = 0;
c98c0 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 /* Count down
c98d0 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 to first I/O err
c98e0 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 or */.SQLITE_API
c98f0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
c9900 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 error_persist =
c9910 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 0; /* Tru
c9920 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 e if I/O errors
c9930 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 persist */.SQLIT
c9940 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c9950 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
c9960 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f n = 0; /
c9970 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 * True if errors
c9980 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 are benign */.S
c9990 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
c99a0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
c99b0 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 ending = 0;.SQLI
c99c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c99d0 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b e3_diskfull = 0;
c99e0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
c99f0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 eIOErrorBenign(X
c9a00 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 ) sqlite3_io_err
c9a10 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 or_benign=(X).#d
c9a20 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
c9a30 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 Error(CODE) \.
c9a40 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f if( (sqlite3_io
c9a50 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 _error_persist &
c9a60 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 & sqlite3_io_err
c9a70 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 or_hit) \.
c9a80 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 || sqlite3_io_e
c9a90 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d rror_pending-- =
c9aa0 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 = 1 ) \.
c9ab0 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 { local_i
c9ac0 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a oerr(); CODE; }.
c9ad0 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 static void loca
c9ae0 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 l_ioerr(){. IOT
c9af0 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 RACE(("IOERR\n")
c9b00 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f );. sqlite3_io_
c9b10 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 error_hit++;. i
c9b20 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 f( !sqlite3_io_e
c9b30 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 rror_benign ) sq
c9b40 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
c9b50 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 ardhit++;.}.#def
c9b60 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b ine SimulateDisk
c9b70 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 fullError(CODE)
c9b80 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 \. if( sqlite3
c9b90 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
c9ba0 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 g ){ \. if(
c9bb0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
c9bc0 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b _pending == 1 ){
c9bd0 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f \. local_
c9be0 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 ioerr(); \.
c9bf0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 sqlite3_diskfu
c9c00 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 ll = 1; \.
c9c10 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c9c20 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 r_hit = 1; \.
c9c30 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 CODE; \.
c9c40 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 }else{ \.
c9c50 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
c9c60 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 l_pending--; \.
c9c70 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c } \. }.#el
c9c80 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c se.#define Simul
c9c90 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
c9ca0 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (X).#define Simu
c9cb0 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 lateIOError(A).#
c9cc0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 define SimulateD
c9cd0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a iskfullError(A).
c9ce0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 #endif../*.** Wh
c9cf0 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 en testing, keep
c9d00 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 a count of the
c9d10 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 number of open f
c9d20 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 iles..*/.#ifdef
c9d30 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
c9d40 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c9d50 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 e3_open_file_cou
c9d60 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 nt = 0;.#define
c9d70 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 OpenCounter(X)
c9d80 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c sqlite3_open_fil
c9d90 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c e_count+=(X).#el
c9da0 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 se.#define OpenC
c9db0 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 ounter(X).#endif
c9dc0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
c9dd0 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f ined(_OS_COMMON_
c9de0 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
c9df0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
c9e00 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a s_common.h *****
c9e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9e30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
c9e40 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
c9e50 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
c9e60 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e off in os_unix.
c9e70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
c9e80 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 ******/../*.** D
c9e90 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 efine various ma
c9ea0 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 cros that are mi
c9eb0 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 ssing from some
c9ec0 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e systems..*/.#ifn
c9ed0 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a def O_LARGEFILE.
c9ee0 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 # define O_LARGE
c9ef0 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 FILE 0.#endif.#i
c9f00 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 fdef SQLITE_DISA
c9f10 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 BLE_LFS.# undef
c9f20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 O_LARGEFILE.# de
c9f30 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 fine O_LARGEFILE
c9f40 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 0.#endif.#ifnde
c9f50 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 f O_NOFOLLOW.# d
c9f60 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 efine O_NOFOLLOW
c9f70 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 0.#endif.#ifnde
c9f80 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 f O_BINARY.# def
c9f90 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 ine O_BINARY 0.#
c9fa0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
c9fb0 20 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 DJGPP compiler
c9fc0 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b environment look
c9fd0 73 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e s mostly like Un
c9fe0 69 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 ix, but it.** la
c9ff0 63 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 cks the fcntl()
ca000 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f system call. So
ca010 20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 redefine fcntl(
ca020 29 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e ) to be somethin
ca030 67 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 g.** that always
ca040 20 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 succeeds. This
ca050 20 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b means that lock
ca060 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 ing does not occ
ca070 75 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 ur under.** DJGP
ca080 50 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 4f P. But it is DO
ca090 53 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 S - what did you
ca0a0 20 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 expect?.*/.#ifd
ca0b0 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 ef __DJGPP__.# d
ca0c0 65 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c efine fcntl(A,B,
ca0d0 43 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a C) 0.#endif../*.
ca0e0 2a 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 20 ** The threadid
ca0f0 6d 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 macro resolves t
ca100 6f 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 o the thread-id
ca110 6f 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 or to 0. Used f
ca120 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e or.** testing an
ca130 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 d debugging only
ca140 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
ca150 54 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 THREADSAFE.#defi
ca160 6e 65 20 74 68 72 65 61 64 69 64 20 70 74 68 72 ne threadid pthr
ca170 65 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 ead_self().#else
ca180 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 .#define threadi
ca190 64 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a d 0.#endif.../*.
ca1a0 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 ** Helper functi
ca1b0 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e ons to obtain an
ca1c0 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 d relinquish the
ca1d0 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 global mutex. T
ca1e0 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 he.** global mut
ca1f0 65 78 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 ex is used to pr
ca200 6f 74 65 63 74 20 74 68 65 20 75 6e 69 78 4f 70 otect the unixOp
ca210 65 6e 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 enCnt, unixLockI
ca220 6e 66 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 nfo and.** vxwor
ca230 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 ksFileId objects
ca240 20 75 73 65 64 20 62 79 20 74 68 69 73 20 66 69 used by this fi
ca250 6c 65 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 le, all of which
ca260 20 6d 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 may be .** shar
ca270 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 ed by multiple t
ca280 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 hreads..**.** Fu
ca290 6e 63 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 nction unixMutex
ca2a0 48 65 6c 64 28 29 20 69 73 20 75 73 65 64 20 74 Held() is used t
ca2b0 6f 20 61 73 73 65 72 74 28 29 20 74 68 61 74 20 o assert() that
ca2c0 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 the global mutex
ca2d0 20 0a 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 .** is held whe
ca2e0 6e 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 n required. This
ca2f0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c function is onl
ca300 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f y used as part o
ca310 66 20 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 f assert() .** s
ca320 74 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a tatements. e.g..
ca330 2a 2a 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 **.** unixEnte
ca340 72 4d 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 rMutex().**
ca350 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 assert( unixMute
ca360 78 48 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 xHeld() );.**
ca370 75 6e 69 78 45 6e 74 65 72 4c 65 61 76 65 28 29 unixEnterLeave()
ca380 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
ca390 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 unixEnterMutex(v
ca3a0 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f oid){. sqlite3_
ca3b0 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 mutex_enter(sqli
ca3c0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
ca3d0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
ca3e0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 C_MASTER));.}.st
ca3f0 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 atic void unixLe
ca400 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a aveMutex(void){.
ca410 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
ca420 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 leave(sqlite3Mut
ca430 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
ca440 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
ca450 45 52 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 ER));.}.#ifdef S
ca460 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 QLITE_DEBUG.stat
ca470 69 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 ic int unixMutex
ca480 48 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 Held(void) {. r
ca490 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 eturn sqlite3_mu
ca4a0 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 tex_held(sqlite3
ca4b0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
ca4c0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
ca4d0 41 53 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 ASTER));.}.#endi
ca4e0 66 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 f...#ifdef SQLIT
ca4f0 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 E_DEBUG./*.** He
ca500 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f lper function fo
ca510 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 r printing out t
ca520 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e race information
ca530 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a from debugging.
ca540 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 ** binaries. Thi
ca550 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 s returns the st
ca560 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 ring represetati
ca570 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 on of the suppli
ca580 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f ed.** integer lo
ca590 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 ck-type..*/.stat
ca5a0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c ic const char *l
ca5b0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 ocktypeName(int
ca5c0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 locktype){. swi
ca5d0 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b tch( locktype ){
ca5e0 0a 20 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 . case NO_LOC
ca5f0 4b 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 K: return "NONE"
ca600 3b 0a 20 20 20 20 63 61 73 65 20 53 48 41 52 45 ;. case SHARE
ca610 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 D_LOCK: return "
ca620 53 48 41 52 45 44 22 3b 0a 20 20 20 20 63 61 73 SHARED";. cas
ca630 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a e RESERVED_LOCK:
ca640 20 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 return "RESERVE
ca650 44 22 3b 0a 20 20 20 20 63 61 73 65 20 50 45 4e D";. case PEN
ca660 44 49 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 DING_LOCK: retur
ca670 6e 20 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 n "PENDING";.
ca680 20 63 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f case EXCLUSIVE_
ca690 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 LOCK: return "EX
ca6a0 43 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 CLUSIVE";. }.
ca6b0 72 65 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a return "ERROR";.
ca6c0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 }.#endif..#ifdef
ca6d0 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 SQLITE_LOCK_TRA
ca6e0 43 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f CE./*.** Print o
ca6f0 75 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ut information a
ca700 62 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 bout all locking
ca710 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a operations..**.
ca720 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
ca730 69 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 is used for trou
ca740 62 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b bleshooting lock
ca750 73 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 s on multithread
ca760 65 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e ed.** platforms.
ca770 20 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 Enable by comp
ca780 69 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d iling with the -
ca790 44 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 DSQLITE_LOCK_TRA
ca7a0 43 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 CE.** command-li
ca7b0 6e 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 ne option on the
ca7c0 20 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 compiler. This
ca7d0 20 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c code is normall
ca7e0 79 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e y.** turned off.
ca7f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c .*/.static int l
ca800 6f 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c ockTrace(int fd,
ca810 20 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 int op, struct
ca820 66 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 flock *p){. cha
ca830 72 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 r *zOpName, *zTy
ca840 70 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 pe;. int s;. i
ca850 6e 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 nt savedErrno;.
ca860 20 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b if( op==F_GETLK
ca870 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 ){. zOpName
ca880 3d 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c = "GETLK";. }el
ca890 73 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 se if( op==F_SET
ca8a0 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d LK ){. zOpNam
ca8b0 65 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d e = "SETLK";. }
ca8c0 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 else{. s = fc
ca8d0 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a ntl(fd, op, p);.
ca8e0 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 sqlite3Debug
ca8f0 50 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e Printf("fcntl un
ca900 6b 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e known %d %d %d\n
ca910 22 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 ", fd, op, s);.
ca920 20 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d return s;. }
ca930 0a 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 . if( p->l_type
ca940 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 ==F_RDLCK ){.
ca950 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 zType = "RDLCK"
ca960 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d ;. }else if( p-
ca970 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b >l_type==F_WRLCK
ca980 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 ){. zType =
ca990 22 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 "WRLCK";. }else
ca9a0 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d if( p->l_type==
ca9b0 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a F_UNLCK ){. z
ca9c0 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a Type = "UNLCK";.
ca9d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
ca9e0 65 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 ert( 0 );. }.
ca9f0 61 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 assert( p->l_whe
caa00 6e 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b nce==SEEK_SET );
caa10 0a 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c . s = fcntl(fd,
caa20 20 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 op, p);. saved
caa30 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
caa40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
caa50 6e 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 ntf("fcntl %d %d
caa60 20 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 %s %s %d %d %d
caa70 25 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 %d\n",. thre
caa80 61 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d adid, fd, zOpNam
caa90 65 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 e, zType, (int)p
caaa0 2d 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 ->l_start, (int)
caab0 70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 p->l_len,. (
caac0 69 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 int)p->l_pid, s)
caad0 3b 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 ;. if( s==(-1)
caae0 26 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 && op==F_SETLK &
caaf0 26 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f & (p->l_type==F_
cab00 52 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 RDLCK || p->l_ty
cab10 70 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a pe==F_WRLCK) ){.
cab20 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b struct flock
cab30 20 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 l2;. l2 = *p
cab40 3b 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 ;. fcntl(fd,
cab50 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 F_GETLK, &l2);.
cab60 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 if( l2.l_type
cab70 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 ==F_RDLCK ){.
cab80 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 zType = "RDLC
cab90 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 K";. }else if
caba0 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 ( l2.l_type==F_W
cabb0 52 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 RLCK ){. zT
cabc0 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 ype = "WRLCK";.
cabd0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e }else if( l2.
cabe0 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 l_type==F_UNLCK
cabf0 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d ){. zType =
cac00 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 "UNLCK";. }e
cac10 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
cac20 74 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 t( 0 );. }.
cac30 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 sqlite3DebugPr
cac40 69 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c intf("fcntl-fail
cac50 75 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 ure-reason: %s %
cac60 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 d %d %d\n",.
cac70 20 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c zType, (int)l
cac80 32 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 2.l_start, (int)
cac90 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c l2.l_len, (int)l
caca0 32 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 2.l_pid);. }.
cacb0 65 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 errno = savedErr
cacc0 6e 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a no;. return s;.
cacd0 7d 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 }.#define fcntl
cace0 6c 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 lockTrace.#endif
cacf0 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f /* SQLITE_LOCK_
cad00 54 52 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a TRACE */..../*.*
cad10 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 * This routine t
cad20 72 61 6e 73 6c 61 74 65 73 20 61 20 73 74 61 6e ranslates a stan
cad30 64 61 72 64 20 50 4f 53 49 58 20 65 72 72 6e 6f dard POSIX errno
cad40 20 63 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 code into somet
cad50 68 69 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 hing.** useful t
cad60 6f 20 74 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 o the clients of
cad70 20 74 68 65 20 73 71 6c 69 74 65 33 20 66 75 6e the sqlite3 fun
cad80 63 74 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 ctions. Specifi
cad90 63 61 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 cally, it is.**
cada0 69 6e 74 65 6e 64 65 64 20 74 6f 20 74 72 61 6e intended to tran
cadb0 73 6c 61 74 65 20 61 20 76 61 72 69 65 74 79 20 slate a variety
cadc0 6f 66 20 22 74 72 79 20 61 67 61 69 6e 22 20 65 of "try again" e
cadd0 72 72 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 rrors into SQLIT
cade0 45 5f 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 E_BUSY.** and a
cadf0 76 61 72 69 65 74 79 20 6f 66 20 22 70 6c 65 61 variety of "plea
cae00 73 65 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c se close the fil
cae10 65 20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 e descriptor NOW
cae20 22 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a " errors into .*
cae30 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a * SQLITE_IOERR.*
cae40 2a 20 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 * .** Errors dur
cae50 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ing initializati
cae60 6f 6e 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 on of locks, or
cae70 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 file system supp
cae80 6f 72 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a ort for locks,.*
cae90 2a 20 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 * should handle
caea0 45 4e 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c ENOLCK, ENOTSUP,
caeb0 20 45 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 61 EOPNOTSUPP sepa
caec0 72 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 rately..*/.stati
caed0 63 20 69 6e 74 20 73 71 6c 69 74 65 45 72 72 6f c int sqliteErro
caee0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
caef0 69 6e 74 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 int posixError,
caf00 69 6e 74 20 73 71 6c 69 74 65 49 4f 45 72 72 29 int sqliteIOErr)
caf10 20 7b 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73 {. switch (pos
caf20 69 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 ixError) {. cas
caf30 65 20 30 3a 20 0a 20 20 20 20 72 65 74 75 72 6e e 0: . return
caf40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
caf50 0a 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0a . case EAGAIN:.
caf60 20 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54 case ETIMEDOUT
caf70 3a 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a 0a :. case EBUSY:.
caf80 20 20 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 case EINTR:.
caf90 63 61 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 case ENOLCK: .
cafa0 20 20 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 /* random NFS
cafb0 20 72 65 74 72 79 20 65 72 72 6f 72 2c 20 75 6e retry error, un
cafc0 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 less during file
cafd0 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 system support
cafe0 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 . * introspe
caff0 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 ction, in which
cb000 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e it actually mean
cb010 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a s what it says *
cb020 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c /. return SQL
cb030 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 ITE_BUSY;. .
cb040 20 63 61 73 65 20 45 41 43 43 45 53 3a 20 0a 20 case EACCES: .
cb050 20 20 20 2f 2a 20 45 41 43 43 45 53 20 69 73 20 /* EACCES is
cb060 6c 69 6b 65 20 45 41 47 41 49 4e 20 64 75 72 69 like EAGAIN duri
cb070 6e 67 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 ng locking opera
cb080 74 69 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 tions, but not a
cb090 6e 79 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a ny other time*/.
cb0a0 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 49 if( (sqliteI
cb0b0 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 OErr == SQLITE_I
cb0c0 4f 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 OERR_LOCK) || ..
cb0d0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 (sqliteIOErr ==
cb0e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
cb0f0 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 OCK) || ..(sqlit
cb100 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 eIOErr == SQLITE
cb110 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c _IOERR_RDLOCK) |
cb120 7c 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 |..(sqliteIOErr
cb130 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f == SQLITE_IOERR_
cb140 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
cb150 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 K) ){. retu
cb160 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a rn SQLITE_BUSY;.
cb170 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 }. /* els
cb180 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a e fall through *
cb190 2f 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a 20 /. case EPERM:
cb1a0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
cb1b0 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 TE_PERM;. .
cb1c0 63 61 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 case EDEADLK:.
cb1d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
cb1e0 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 IOERR_BLOCKED;.
cb1f0 20 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 .#if EOPNOTSU
cb200 50 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 PP!=ENOTSUP. ca
cb210 73 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a se EOPNOTSUPP: .
cb220 20 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 /* something
cb230 20 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20 61 went terribly a
cb240 77 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 wry, unless duri
cb250 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 ng file system s
cb260 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 upport . * i
cb270 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e ntrospection, in
cb280 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c which it actual
cb290 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 ly means what it
cb2a0 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a says */.#endif.
cb2b0 23 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 #ifdef ENOTSUP.
cb2c0 20 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a case ENOTSUP: .
cb2d0 20 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 /* invalid f
cb2e0 64 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 d, unless during
cb2f0 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 file system sup
cb300 70 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74 69 port introspecti
cb310 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 20 on, in which .
cb320 20 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 * it actually
cb330 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 means what it s
cb340 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ays */.#endif.
cb350 63 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 case EIO:. case
cb360 20 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20 45 EBADF:. case E
cb370 49 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e INVAL:. case EN
cb380 4f 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 OTCONN:. case E
cb390 4e 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45 4e NODEV:. case EN
cb3a0 58 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 XIO:. case ENOE
cb3b0 4e 54 3a 0a 20 20 63 61 73 65 20 45 53 54 41 4c NT:. case ESTAL
cb3c0 45 3a 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53 E:. case ENOSYS
cb3d0 3a 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73 :. /* these s
cb3e0 68 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20 hould force the
cb3f0 63 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 client to close
cb400 74 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63 the file and rec
cb410 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 onnect */. .
cb420 20 64 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 default: . r
cb430 65 74 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 eturn sqliteIOEr
cb440 72 3b 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a r;. }.}..../***
cb450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
cb4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
cb4b0 65 67 69 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 egin Unique File
cb4c0 20 49 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 ID Utility Used
cb4d0 20 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a By VxWorks ****
cb4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
cb4f0 2a 20 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f * On most versio
cb500 6e 73 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 ns of unix, we c
cb510 61 6e 20 67 65 74 20 61 20 75 6e 69 71 75 65 20 an get a unique
cb520 49 44 20 66 6f 72 20 61 20 66 69 6c 65 20 62 79 ID for a file by
cb530 20 63 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a concatenating.*
cb540 2a 20 74 68 65 20 64 65 76 69 63 65 20 6e 75 6d * the device num
cb550 62 65 72 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 ber and the inod
cb560 65 20 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 74 e number. But t
cb570 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 his does not wor
cb580 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a k on VxWorks..**
cb590 20 4f 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 On VxWorks, a u
cb5a0 6e 69 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75 nique file id mu
cb5b0 73 74 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74 st be based on t
cb5c0 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c he canonical fil
cb5d0 65 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 ename..**.** A p
cb5e0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
cb5f0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
cb600 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
cb610 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 can be used as
cb620 61 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 a.** unique file
cb630 20 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 ID in VxWorks.
cb640 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f Each instance o
cb650 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
cb660 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 contains.** a c
cb670 6f 70 79 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e opy of the canon
cb680 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 ical filename.
cb690 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 There is also a
cb6a0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e reference count.
cb6b0 20 20 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 .** The struct
cb6c0 75 72 65 20 69 73 20 72 65 63 6c 61 69 6d 65 64 ure is reclaimed
cb6d0 20 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 when the number
cb6e0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
cb6f0 69 74 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a it drops to.** z
cb700 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ero..**.** There
cb710 20 61 72 65 20 6e 65 76 65 72 20 76 65 72 79 20 are never very
cb720 6d 61 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 many files open
cb730 61 74 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 at one time and
cb740 6c 6f 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a lookups are not.
cb750 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 ** a performance
cb760 2d 63 72 69 74 69 63 61 6c 20 70 61 74 68 2c 20 -critical path,
cb770 73 6f 20 69 74 20 69 73 20 73 75 66 66 69 63 69 so it is suffici
cb780 65 6e 74 20 74 6f 20 70 75 74 20 74 68 65 73 65 ent to put these
cb790 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 6f .** structures o
cb7a0 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e n a linked list.
cb7b0 0a 2a 2f 0a 73 74 72 75 63 74 20 76 78 77 6f 72 .*/.struct vxwor
cb7c0 6b 73 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 ksFileId {. str
cb7d0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
cb7e0 64 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 d *pNext; /* Ne
cb7f0 78 74 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 xt in a list of
cb800 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e them all */. in
cb810 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
cb820 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
cb830 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e umber of referen
cb840 63 65 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 ces to this one
cb850 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 */. int nName;
cb860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb870 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 /* Length of
cb880 74 68 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 the zCanonicalNa
cb890 6d 65 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 me[] string */.
cb8a0 20 63 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 char *zCanonica
cb8b0 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f lName; /
cb8c0 2a 20 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 * Canonical file
cb8d0 6e 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 name */.};..#if
cb8e0 4f 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a OS_VXWORKS./* .*
cb8f0 2a 20 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c * All unique fil
cb900 65 6e 61 6d 65 73 20 61 72 65 20 68 65 6c 64 20 enames are held
cb910 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 on a linked list
cb920 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73 0a headed by this.
cb930 2a 2a 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a ** variable:.*/.
cb940 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78 static struct vx
cb950 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 worksFileId *vxw
cb960 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 orksFileList = 0
cb970 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 ;../*.** Simplif
cb980 79 20 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 y a filename int
cb990 6f 20 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 o its canonical
cb9a0 66 6f 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e form.** by makin
cb9b0 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 g the following
cb9c0 63 68 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 changes:.**.**
cb9d0 2a 20 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 * removing any t
cb9e0 72 61 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c railing and dupl
cb9f0 69 63 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f icate /.** * co
cba00 6e 76 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a nvert /./ into j
cba10 75 73 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 ust /.** * conv
cba20 65 72 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 ert /A/../ where
cba30 20 41 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 A is any simple
cba40 20 6e 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 name into just
cba50 2f 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 /.**.** Changes
cba60 61 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 are made in-plac
cba70 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e e. Return the n
cba80 65 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a ew name length..
cba90 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e **.** The origin
cbaa0 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 al filename is i
cbab0 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 n z[0..n-1]. Re
cbac0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
cbad0 6f 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 of.** characters
cbae0 20 69 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 69 in the simplifi
cbaf0 65 64 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 ed name..*/.stat
cbb00 69 63 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 ic int vxworksSi
cbb10 6d 70 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 20 mplifyName(char
cbb20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e *z, int n){. in
cbb30 74 20 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 t i, j;. while(
cbb40 20 6e 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d n>1 && z[n-1]==
cbb50 27 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 '/' ){ n--; }.
cbb60 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 for(i=j=0; i<n;
cbb70 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b i++){. if( z[
cbb80 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 i]=='/' ){.
cbb90 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 if( z[i+1]=='/'
cbba0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
cbbb0 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 if( z[i+1]=='
cbbc0 2e 27 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a .' && i+2<n && z
cbbd0 5b 69 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 [i+2]=='/' ){.
cbbe0 20 20 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 i += 1;.
cbbf0 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a continue;.
cbc00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
cbc10 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 ( z[i+1]=='.' &&
cbc20 20 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d i+3<n && z[i+2]
cbc30 3d 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d =='.' && z[i+3]=
cbc40 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='/' ){.
cbc50 77 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b while( j>0 && z[
cbc60 6a 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d j-1]!='/' ){ j--
cbc70 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 ; }. if(
cbc80 6a 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 j>0 ){ j--; }.
cbc90 20 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 i += 2;.
cbca0 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a continue;.
cbcb0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
cbcc0 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b z[j++] = z[i];
cbcd0 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b . }. z[j] = 0;
cbce0 0a 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a . return j;.}..
cbcf0 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 /*.** Find a uni
cbd00 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 que file ID for
cbd10 74 68 65 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 the given absolu
cbd20 74 65 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 te pathname. Re
cbd30 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 turn.** a pointe
cbd40 72 20 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 r to the vxworks
cbd50 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 FileId object.
cbd60 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 This pointer is
cbd70 74 68 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 the unique.** fi
cbd80 6c 65 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 le ID..**.** The
cbd90 20 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20 74 nRef field of t
cbda0 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 he vxworksFileId
cbdb0 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72 65 object is incre
cbdc0 6d 65 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a mented before.**
cbdd0 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 72 the object is r
cbde0 65 74 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 eturned. A new
cbdf0 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 vxworksFileId ob
cbe00 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 0a ject is created.
cbe10 2a 2a 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 ** and added to
cbe20 74 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 the global list
cbe30 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a if necessary..**
cbe40 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 .** If a memory
cbe50 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
cbe60 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 occurs, return
cbe70 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 NULL..*/.static
cbe80 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cbe90 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e leId *vxworksFin
cbea0 64 46 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 dFileId(const ch
cbeb0 61 72 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d ar *zAbsoluteNam
cbec0 65 29 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 e){. struct vxw
cbed0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 orksFileId *pNew
cbee0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65 61 ; /* sea
cbef0 72 63 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 rch key and new
cbf00 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 file ID */. str
cbf10 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
cbf20 64 20 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 20 d *pCandidate;
cbf30 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 /* For looping
cbf40 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20 66 69 over existing fi
cbf50 6c 65 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 le IDs */. int
cbf60 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
cbf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cbf80 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 /* Length of zAb
cbf90 73 6f 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 6e soluteName strin
cbfa0 67 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 g */.. assert(
cbfb0 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d zAbsoluteName[0]
cbfc0 3d 3d 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 =='/' );. n = (
cbfd0 69 6e 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f int)strlen(zAbso
cbfe0 6c 75 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 luteName);. pNe
cbff0 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c w = sqlite3_mall
cc000 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 oc( sizeof(*pNew
cc010 29 20 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 ) + (n+1) );. i
cc020 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 f( pNew==0 ) ret
cc030 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a urn 0;. pNew->z
cc040 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 CanonicalName =
cc050 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b (char*)&pNew[1];
cc060 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e . memcpy(pNew->
cc070 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 zCanonicalName,
cc080 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e zAbsoluteName, n
cc090 2b 31 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 +1);. n = vxwor
cc0a0 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 ksSimplifyName(p
cc0b0 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e New->zCanonicalN
cc0c0 61 6d 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 ame, n);.. /* S
cc0d0 65 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78 69 earch for an exi
cc0e0 73 74 69 6e 67 20 65 6e 74 72 79 20 74 68 61 74 sting entry that
cc0f0 20 6d 61 74 63 68 69 6e 67 20 74 68 65 20 63 61 matching the ca
cc100 6e 6f 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 nonical name..
cc110 2a 2a 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 ** If found, inc
cc120 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 rement the refer
cc130 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 ence count and r
cc140 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
cc150 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 73 to. ** the exis
cc160 74 69 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 ting file ID..
cc170 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 */. unixEnterMu
cc180 74 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 tex();. for(pCa
cc190 6e 64 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 ndidate=vxworksF
cc1a0 69 6c 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 ileList; pCandid
cc1b0 61 74 65 3b 20 70 43 61 6e 64 69 64 61 74 65 3d ate; pCandidate=
cc1c0 70 43 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 pCandidate->pNex
cc1d0 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 61 6e t){. if( pCan
cc1e0 64 69 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e didate->nName==n
cc1f0 20 0a 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 . && memcmp
cc200 28 70 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 (pCandidate->zCa
cc210 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 nonicalName, pNe
cc220 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d w->zCanonicalNam
cc230 65 2c 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a e, n)==0. ){.
cc240 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
cc250 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 ree(pNew);.
cc260 20 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 pCandidate->nR
cc270 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 ef++;. uni
cc280 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
cc290 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 61 return pCa
cc2a0 6e 64 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 ndidate;. }.
cc2b0 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 }.. /* No matc
cc2c0 68 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 h was found. We
cc2d0 20 77 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 will make a new
cc2e0 20 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e file ID */. pN
cc2f0 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 ew->nRef = 1;.
cc300 70 4e 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b pNew->nName = n;
cc310 0a 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d . pNew->pNext =
cc320 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 vxworksFileList
cc330 3b 0a 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c ;. vxworksFileL
cc340 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e ist = pNew;. un
cc350 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
cc360 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
cc370 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e ../*.** Decremen
cc380 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 t the reference
cc390 63 6f 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 count on a vxwor
cc3a0 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e ksFileId object.
cc3b0 20 20 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 Free.** the ob
cc3c0 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 72 65 ject when the re
cc3d0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 ference count re
cc3e0 61 63 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 aches zero..*/.s
cc3f0 74 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f 72 tatic void vxwor
cc400 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 ksReleaseFileId(
cc410 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cc420 6c 65 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e leId *pId){. un
cc430 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
cc440 20 20 61 73 73 65 72 74 28 20 70 49 64 2d 3e 6e assert( pId->n
cc450 52 65 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e Ref>0 );. pId->
cc460 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 nRef--;. if( pI
cc470 64 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 d->nRef==0 ){.
cc480 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
cc490 46 69 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 FileId **pp;.
cc4a0 20 66 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 for(pp=&vxworks
cc4b0 46 69 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 FileList; *pp &&
cc4c0 20 2a 70 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 *pp!=pId; pp =
cc4d0 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 &((*pp)->pNext))
cc4e0 7b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a {}. assert( *
cc4f0 70 70 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a pp==pId );. *
cc500 70 70 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b pp = pId->pNext;
cc510 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
cc520 65 28 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e e(pId);. }. un
cc530 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
cc540 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 }.#endif /* OS_V
cc550 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a XWORKS */./*****
cc560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
cc570 66 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 f Unique File ID
cc580 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 Utility Used By
cc590 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a VxWorks *******
cc5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
cc5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc5f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a ********/.../***
cc600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
cc650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc660 2a 2a 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 ******* Posix Ad
cc670 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a visory Locking *
cc680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
cc6a0 2a 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 * POSIX advisory
cc6b0 20 6c 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 locks are broke
cc6c0 6e 20 62 79 20 64 65 73 69 67 6e 2e 20 20 41 4e n by design. AN
cc6d0 53 49 20 53 54 44 20 31 30 30 33 2e 31 20 28 31 SI STD 1003.1 (1
cc6e0 39 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 996).** section
cc6f0 36 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 6.5.2.2 lines 48
cc700 33 20 74 68 72 6f 75 67 68 20 34 39 30 20 73 70 3 through 490 sp
cc710 65 63 69 66 79 20 74 68 61 74 20 77 68 65 6e 20 ecify that when
cc720 61 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 a process.** set
cc730 73 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c 6f s or clears a lo
cc740 63 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74 69 ck, that operati
cc750 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 on overrides any
cc760 20 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 prior locks set
cc770 0a 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65 20 .** by the same
cc780 70 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f 65 process. It doe
cc790 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 s not explicitly
cc7a0 20 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68 69 say so, but thi
cc7b0 73 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 s implies.** tha
cc7c0 74 20 69 74 20 6f 76 65 72 72 69 64 65 73 20 6c t it overrides l
cc7d0 6f 63 6b 73 20 73 65 74 20 62 79 20 74 68 65 20 ocks set by the
cc7e0 73 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73 69 same process usi
cc7f0 6e 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a ng a different.*
cc800 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f * file descripto
cc810 72 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68 69 r. Consider thi
cc820 73 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a s test case:.**.
cc830 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 31 ** int fd1
cc840 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 = open("./file1
cc850 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 ", O_RDWR|O_CREA
cc860 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 T, 0644);.**
cc870 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 int fd2 = ope
cc880 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 n("./file2", O_R
cc890 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 DWR|O_CREAT, 064
cc8a0 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 4);.**.** Suppos
cc8b0 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f e ./file1 and ./
cc8c0 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79 file2 are really
cc8d0 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28 the same file (
cc8e0 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 because.** one i
cc8f0 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62 s a hard or symb
cc900 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 olic link to the
cc910 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 20 other) then if
cc920 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 you set.** an ex
cc930 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 clusive lock on
cc940 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f fd1, then try to
cc950 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76 get an exclusiv
cc960 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 e lock.** on fd2
cc970 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 , it works. I w
cc980 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 74 ould have expect
cc990 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f ed the second lo
cc9a0 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 ck to.** fail si
cc9b0 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61 6c nce there was al
cc9c0 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 ready a lock on
cc9d0 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20 the file due to
cc9e0 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 fd1..** But not
cc9f0 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20 so. Since both
cca00 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 locks came from
cca10 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 the same process
cca20 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 , the.** second
cca30 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66 69 overrides the fi
cca40 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 rst, even though
cca50 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69 they were on di
cca60 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 fferent.** file
cca70 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e descriptors open
cca80 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 ed on different
cca90 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a file names..**.*
ccaa0 2a 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 * This means tha
ccab0 74 20 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 t we cannot use
ccac0 50 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 POSIX locks to s
ccad0 79 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 ynchronize file
ccae0 61 63 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 access.** among
ccaf0 63 6f 6d 70 65 74 69 6e 67 20 74 68 72 65 61 64 competing thread
ccb00 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 70 72 s of the same pr
ccb10 6f 63 65 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f ocess. POSIX lo
ccb20 63 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 cks will work fi
ccb30 6e 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f ne.** to synchro
ccb40 6e 69 7a 65 20 61 63 63 65 73 73 20 66 6f 72 20 nize access for
ccb50 74 68 72 65 61 64 73 20 69 6e 20 73 65 70 61 72 threads in separ
ccb60 61 74 65 20 70 72 6f 63 65 73 73 65 73 2c 20 62 ate processes, b
ccb70 75 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 ut not.** thread
ccb80 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d s within the sam
ccb90 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a e process..**.**
ccba0 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 To work around
ccbb0 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c the problem, SQL
ccbc0 69 74 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 ite has to manag
ccbd0 65 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 e file locks int
ccbe0 65 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 ernally.** on it
ccbf0 73 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 s own. Whenever
ccc00 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 a new database
ccc10 69 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 is opened, we ha
ccc20 76 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a ve to find the.*
ccc30 2a 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65 * specific inode
ccc40 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
ccc50 20 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 file (the inode
ccc60 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
ccc70 79 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 y the.** st_dev
ccc80 61 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 and st_ino field
ccc90 73 20 6f 66 20 74 68 65 20 73 74 61 74 20 73 74 s of the stat st
ccca0 72 75 63 74 75 72 65 20 74 68 61 74 20 66 73 74 ructure that fst
cccb0 61 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a at() fills in).*
cccc0 2a 20 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 * and check for
cccd0 6c 6f 63 6b 73 20 61 6c 72 65 61 64 79 20 65 78 locks already ex
ccce0 69 73 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 isting on that i
cccf0 6e 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b node. When lock
ccd00 73 20 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 s are.** created
ccd10 20 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 or removed, we
ccd20 68 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 have to look at
ccd30 6f 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c our own internal
ccd40 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a record of the.*
ccd50 2a 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 * locks to see i
ccd60 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 f another thread
ccd70 20 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 has previously
ccd80 73 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 set a lock on th
ccd90 61 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 at same.** inode
ccda0 2e 0a 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 ..**.** (Aside:
ccdb0 54 68 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 The use of inode
ccdc0 20 6e 75 6d 62 65 72 73 20 61 73 20 75 6e 69 71 numbers as uniq
ccdd0 75 65 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 ue IDs does not
ccde0 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e work on VxWorks.
ccdf0 0a 2a 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c .** For VxWorks,
cce00 20 77 65 20 68 61 76 65 20 74 6f 20 75 73 65 20 we have to use
cce10 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 the alternative
cce20 75 6e 69 71 75 65 20 49 44 20 73 79 73 74 65 6d unique ID system
cce30 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e based on.** can
cce40 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 onical filename
cce50 61 6e 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 and implemented
cce60 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 in the previous
cce70 64 69 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a division.).**.**
cce80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c The sqlite3_fil
cce90 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 e structure for
ccea0 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 POSIX is no long
cceb0 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 er just an integ
ccec0 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 er file.** descr
cced0 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f iptor. It is no
ccee0 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 w a structure th
ccef0 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 at holds the int
ccf00 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 eger file.** des
ccf10 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f criptor and a po
ccf20 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 inter to a struc
ccf30 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
ccf40 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c bes the internal
ccf50 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 .** locks on the
ccf60 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 corresponding i
ccf70 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 node. There is
ccf80 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 one locking stru
ccf90 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f cture.** per ino
ccfa0 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 de, so if the sa
ccfb0 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e me inode is open
ccfc0 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 ed twice, both u
ccfd0 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 nixFile structur
ccfe0 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 es.** point to t
ccff0 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 he same locking
cd000 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
cd010 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 locking structur
cd020 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 e keeps.** a ref
cd030 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f erence count (so
cd040 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 we will know wh
cd050 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29 en to delete it)
cd060 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 and a "cnt".**
cd070 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 field that tells
cd080 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c us its internal
cd090 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63 lock status. c
cd0a0 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a nt==0 means the.
cd0b0 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 ** file is unloc
cd0c0 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 ked. cnt==-1 me
cd0d0 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 73 ans the file has
cd0e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
cd0f0 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 ck..** cnt>0 mea
cd100 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e 74 ns there are cnt
cd110 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e shared locks on
cd120 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
cd130 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
cd140 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 lock or unlock a
cd150 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 63 file first chec
cd160 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a ks the locking.*
cd170 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 * structure. Th
cd180 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d e fcntl() system
cd190 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e call is only in
cd1a0 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a voked to set a .
cd1b0 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 ** POSIX lock if
cd1c0 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f the internal lo
cd1d0 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 61 ck structure tra
cd1e0 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e nsitions between
cd1f0 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 .** a locked and
cd200 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 an unlocked sta
cd210 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 te..**.** But wa
cd220 69 74 3a 20 20 74 68 65 72 65 20 61 72 65 20 79 it: there are y
cd230 65 74 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 et more problems
cd240 20 77 69 74 68 20 50 4f 53 49 58 20 61 64 76 69 with POSIX advi
cd250 73 6f 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a sory locks..**.*
cd260 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 * If you close a
cd270 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
cd280 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 that points to
cd290 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 a file that has
cd2a0 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f locks,.** all lo
cd2b0 63 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 cks on that file
cd2c0 20 74 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20 that are owned
cd2d0 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 by the current p
cd2e0 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 rocess are.** re
cd2f0 6c 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b leased. To work
cd300 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f around this pro
cd310 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 blem, each unixF
cd320 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f ile structure co
cd330 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e ntains.** a poin
cd340 74 65 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 ter to an unixOp
cd350 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e enCnt structure.
cd360 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 75 There is one u
cd370 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 nixOpenCnt struc
cd380 74 75 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e ture.** per open
cd390 20 69 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 inode, which me
cd3a0 61 6e 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c ans that multipl
cd3b0 65 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 e unixFile can p
cd3c0 6f 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 oint to a single
cd3d0 0a 2a 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e .** unixOpenCnt.
cd3e0 20 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 When an attemp
cd3f0 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f t is made to clo
cd400 73 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 se an unixFile,
cd410 69 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 if there are.**
cd420 6f 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20 6f other unixFile o
cd430 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 pen on the same
cd440 69 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20 68 inode that are h
cd450 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 olding locks, th
cd460 65 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f e call.** to clo
cd470 73 65 28 29 20 74 68 65 20 66 69 6c 65 20 64 65 se() the file de
cd480 73 63 72 69 70 74 6f 72 20 69 73 20 64 65 66 65 scriptor is defe
cd490 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f rred until all o
cd4a0 66 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 f the locks clea
cd4b0 72 2e 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 r..** The unixOp
cd4c0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 enCnt structure
cd4d0 6b 65 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 keeps a list of
cd4e0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
cd4f0 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a that need to.**
cd500 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 be closed and t
cd510 68 61 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b hat list is walk
cd520 65 64 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 ed (and cleared)
cd530 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c when the last l
cd540 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a ock.** clears..*
cd550 2a 0a 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 *.** Yet another
cd560 20 70 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 problem: Linux
cd570 54 68 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 Threads do not p
cd580 6c 61 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f lay well with po
cd590 73 69 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a six locks..**.**
cd5a0 20 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 Many older vers
cd5b0 69 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 ions of linux us
cd5c0 65 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 e the LinuxThrea
cd5d0 64 73 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 ds library which
cd5e0 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 is.** not posix
cd5f0 20 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 compliant. Und
cd600 65 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c er LinuxThreads,
cd610 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 a lock created
cd620 62 79 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 by thread.** A c
cd630 61 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 annot be modifie
cd640 64 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 d or overridden
cd650 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 by a different t
cd660 68 72 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 hread B..** Only
cd670 20 74 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f thread A can mo
cd680 64 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 dify the lock.
cd690 4c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 Locking behavior
cd6a0 20 69 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 is correct.** i
cd6b0 66 20 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e f the appliation
cd6c0 20 75 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 uses the newer
cd6d0 4e 61 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 Native Posix Thr
cd6e0 65 61 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 ead Library (NPT
cd6f0 4c 29 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d L).** on linux -
cd700 20 77 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 with NPTL a loc
cd710 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 72 k created by thr
cd720 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 ead A can overri
cd730 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 de locks.** in t
cd740 68 72 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 hread B. But th
cd750 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f ere is no way to
cd760 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 know at compile
cd770 2d 74 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 -time which.** t
cd780 68 72 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 hreading library
cd790 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 is being used.
cd7a0 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 So there is no
cd7b0 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a way to know at.*
cd7c0 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 * compile-time w
cd7d0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
cd7e0 72 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 read A can overr
cd7f0 69 64 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 ide locks on thr
cd800 65 61 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 ead B..** We hav
cd810 65 20 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 e to do a run-ti
cd820 6d 65 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 me check to disc
cd830 6f 76 65 72 20 74 68 65 20 62 65 68 61 76 69 6f over the behavio
cd840 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 r of the.** curr
cd850 65 6e 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a ent process..**.
cd860 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 ** On systems wh
cd870 65 72 65 20 74 68 72 65 61 64 20 41 20 69 73 20 ere thread A is
cd880 75 6e 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 unable to modify
cd890 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 62 locks created b
cd8a0 79 0a 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 77 y.** thread B, w
cd8b0 65 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20 74 e have to keep t
cd8c0 72 61 63 6b 20 6f 66 20 77 68 69 63 68 20 74 68 rack of which th
cd8d0 72 65 61 64 20 63 72 65 61 74 65 64 20 65 61 63 read created eac
cd8e0 68 0a 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 h.** lock. Henc
cd8f0 65 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 78 e there is an ex
cd900 74 72 61 20 66 69 65 6c 64 20 69 6e 20 74 68 65 tra field in the
cd910 20 6b 65 79 20 74 6f 20 74 68 65 20 75 6e 69 78 key to the unix
cd920 4c 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 LockInfo.** stru
cd930 63 74 75 72 65 20 74 6f 20 72 65 63 6f 72 64 20 cture to record
cd940 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e this information
cd950 2e 20 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 . And on those
cd960 73 79 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 systems it.** is
cd970 20 69 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 illegal to begi
cd980 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 n a transaction
cd990 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e in one thread an
cd9a0 64 20 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 d finish it.** i
cd9b0 6e 20 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 n another. For
cd9c0 74 68 69 73 20 6c 61 74 74 65 72 20 72 65 73 74 this latter rest
cd9d0 72 69 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 riction, there i
cd9e0 73 20 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 s no work-around
cd9f0 2e 0a 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 6d ..** It is a lim
cda00 69 74 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 itation of Linux
cda10 54 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a Threads..*/../*.
cda20 2a 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b 20 ** Set or check
cda30 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 the unixFile.tid
cda40 20 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66 69 field. This fi
cda50 65 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e 20 eld is set when
cda60 61 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 an unixFile.** i
cda70 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 s first opened.
cda80 20 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 All subsequent
cda90 75 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69 78 uses of the unix
cdaa0 46 69 6c 65 20 76 65 72 69 66 79 20 74 68 61 74 File verify that
cdab0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 the.** same thr
cdac0 65 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e 67 ead is operating
cdad0 20 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c 65 on the unixFile
cdae0 2e 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e . Some operatin
cdaf0 67 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 g systems do.**
cdb00 6e 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 not allow locks
cdb10 74 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e to be overridden
cdb20 20 62 79 20 6f 74 68 65 72 20 74 68 72 65 61 64 by other thread
cdb30 73 20 61 6e 64 20 74 68 61 74 20 72 65 73 74 72 s and that restr
cdb40 69 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 iction.** means
cdb50 74 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64 61 that sqlite3* da
cdb60 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 63 tabase handles c
cdb70 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 annot be moved f
cdb80 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a rom one thread.*
cdb90 2a 20 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 * to another whi
cdba0 6c 65 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c le locks are hel
cdbb0 64 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e d..**.** Version
cdbc0 20 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 3.3.1 (2006-01-
cdbd0 31 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 15): unixFile c
cdbe0 61 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d an be moved from
cdbf0 20 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a one thread to.*
cdc00 2a 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e * another as lon
cdc10 67 20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e g as we are runn
cdc20 69 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 ing on a system
cdc30 74 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 68 that supports th
cdc40 72 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 reads.** overrid
cdc50 69 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 20 ing each others
cdc60 6c 6f 63 6b 73 20 28 77 68 69 63 68 20 69 73 20 locks (which is
cdc70 6e 6f 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d now the most com
cdc80 6d 6f 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a mon behavior).**
cdc90 20 6f 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 or if no locks
cdca0 61 72 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 are held. But t
cdcb0 68 65 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 he unixFile.pLoc
cdcc0 6b 20 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f k field needs to
cdcd0 20 62 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 be.** recompute
cdce0 64 20 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 d because its ke
cdcf0 79 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 y includes the t
cdd00 68 72 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 hread-id. See t
cdd10 68 65 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f he .** transferO
cdd20 77 6e 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 wnership() funct
cdd30 69 6f 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 ion below for ad
cdd40 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
cdd50 74 69 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 tion.*/.#if SQLI
cdd60 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
cdd70 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
cdd80 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54 __).# define SET
cdd90 5f 54 48 52 45 41 44 49 44 28 58 29 20 20 20 28 _THREADID(X) (
cdda0 58 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61 X)->tid = pthrea
cddb0 64 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e d_self().# defin
cddc0 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 e CHECK_THREADID
cddd0 28 58 29 20 28 74 68 72 65 61 64 73 4f 76 65 72 (X) (threadsOver
cdde0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f rideEachOthersLo
cddf0 63 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 cks==0 && \.
cde00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cde10 20 20 20 20 20 20 20 20 21 70 74 68 72 65 61 64 !pthread
cde20 5f 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c _equal((X)->tid,
cde30 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 pthread_self())
cde40 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
cde50 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 SET_THREADID(X)
cde60 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f .# define CHECK_
cde70 54 48 52 45 41 44 49 44 28 58 29 20 30 0a 23 65 THREADID(X) 0.#e
cde80 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 ndif../*.** An i
cde90 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
cdea0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
cdeb0 72 65 20 73 65 72 76 65 73 20 61 73 20 74 68 65 re serves as the
cdec0 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 key used.** to
cded0 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 75 locate a particu
cdee0 6c 61 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 lar unixOpenCnt
cdef0 73 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 structure given
cdf00 69 74 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 its inode. This
cdf10 0a 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 .** is the same
cdf20 61 73 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b as the unixLockK
cdf30 65 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74 ey except that t
cdf40 68 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20 he thread ID is
cdf50 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 omitted..*/.stru
cdf60 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a ct unixFileId {.
cdf70 20 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20 20 dev_t dev;
cdf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
cdf90 20 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a Device number *
cdfa0 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 /.#if OS_VXWORKS
cdfb0 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b . struct vxwork
cdfc0 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f sFileId *pId; /
cdfd0 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 * Unique file ID
cdfe0 20 66 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f for vxworks. */
cdff0 0a 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 .#else. ino_t i
ce000 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
ce010 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 /* Inode nu
ce020 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d mber */.#endif.}
ce030 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 ;../*.** An inst
ce040 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
ce050 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
ce060 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 serves as the ke
ce070 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 y used.** to loc
ce080 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 ate a particular
ce090 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 unixLockInfo st
ce0a0 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 ructure given it
ce0b0 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 s inode..**.** I
ce0c0 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 f threads cannot
ce0d0 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f override each o
ce0e0 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e thers locks (Lin
ce0f0 75 78 54 68 72 65 61 64 73 29 2c 20 74 68 65 6e uxThreads), then
ce100 20 77 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 we.** set the u
ce110 6e 69 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 nixLockKey.tid f
ce120 69 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65 ield to the thre
ce130 61 64 20 49 44 2e 20 20 49 66 20 74 68 72 65 61 ad ID. If threa
ce140 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a ds can override.
ce150 2a 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c ** each others l
ce160 6f 63 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 20 ocks (Posix and
ce170 4e 50 54 4c 29 20 74 68 65 6e 20 74 69 64 20 69 NPTL) then tid i
ce180 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 s always set to
ce190 7a 65 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 zero..** tid is
ce1a0 6f 6d 69 74 74 65 64 20 69 66 20 77 65 20 63 6f omitted if we co
ce1b0 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68 mpile without th
ce1c0 72 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74 20 reading support
ce1d0 6f 72 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f or on an OS.** o
ce1e0 74 68 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e ther than linux.
ce1f0 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c .*/.struct unixL
ce200 6f 63 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 ockKey {. struc
ce210 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 64 t unixFileId fid
ce220 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 ; /* Unique ide
ce230 6e 74 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 ntifier for the
ce240 66 69 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 file */.#if SQLI
ce250 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
ce260 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
ce270 5f 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20 __). pthread_t
ce280 74 69 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 tid; /* Thread
ce290 49 44 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 ID of lock owner
ce2a0 2e 20 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 . Zero if not us
ce2b0 69 6e 67 20 4c 69 6e 75 78 54 68 72 65 61 64 73 ing LinuxThreads
ce2c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
ce2d0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
ce2e0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
ce2f0 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 g structure is a
ce300 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 llocated for eac
ce310 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e h open.** inode.
ce320 20 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 Or, on LinuxTh
ce330 72 65 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 reads, there is
ce340 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 one of these str
ce350 75 63 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 uctures for.** e
ce360 61 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 ach inode opened
ce370 20 62 79 20 65 61 63 68 20 74 68 72 65 61 64 2e by each thread.
ce380 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 .**.** A single
ce390 69 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d inode can have m
ce3a0 75 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 ultiple file des
ce3b0 63 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 criptors, so eac
ce3c0 68 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 h unixFile.** st
ce3d0 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
ce3e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
ce3f0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
ce400 73 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 s object and thi
ce410 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 s.** object keep
ce420 73 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 s a count of the
ce430 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 number of unixF
ce440 69 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 ile pointing to
ce450 69 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e it..*/.struct un
ce460 69 78 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 ixLockInfo {. s
ce470 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 truct unixLockKe
ce480 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f y lockKey; /
ce490 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 * The lookup key
ce4a0 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 */. int cnt;
ce4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce4c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ce4d0 6f 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 of SHARED locks
ce4e0 68 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f held */. int lo
ce4f0 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 cktype;
ce500 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
ce510 20 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c of SHARED_LOCK,
ce520 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 RESERVED_LOCK e
ce530 74 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 tc. */. int nRe
ce540 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
ce550 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
ce560 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 er of pointers t
ce570 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 o this structure
ce580 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
ce590 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 xLockInfo *pNext
ce5a0 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ; /* List of
ce5b0 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 all unixLockInf
ce5c0 6f 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 o objects */. s
ce5d0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
ce5e0 66 6f 20 2a 70 50 72 65 76 3b 20 20 20 20 20 2f fo *pPrev; /
ce5f0 2a 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 * .... doubly
ce600 20 6c 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f linked */.};../
ce610 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
ce620 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
ce630 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 g structure is a
ce640 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 llocated for eac
ce650 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e h open.** inode.
ce660 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 This structure
ce670 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 keeps track of
ce680 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f the number of lo
ce690 63 6b 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 cks on that.** i
ce6a0 6e 6f 64 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 node. If a clos
ce6b0 65 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 e is attempted a
ce6c0 67 61 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 gainst an inode
ce6d0 74 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a that is holding.
ce6e0 2a 2a 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c ** locks, the cl
ce6f0 6f 73 65 20 69 73 20 64 65 66 65 72 72 65 64 20 ose is deferred
ce700 75 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 until all locks
ce710 63 6c 65 61 72 20 62 79 20 61 64 64 69 6e 67 20 clear by adding
ce720 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 the.** file desc
ce730 72 69 70 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f riptor to be clo
ce740 73 65 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69 sed to the pendi
ce750 6e 67 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 ng list..**.** T
ce760 4f 44 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 ODO: Consider c
ce770 68 61 6e 67 69 6e 67 20 74 68 69 73 20 73 6f 20 hanging this so
ce780 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e that there is on
ce790 6c 79 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 ly a single file
ce7a0 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 66 .** descriptor f
ce7b0 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 66 69 6c or each open fil
ce7c0 65 2c 20 65 76 65 6e 20 77 68 65 6e 20 69 74 20 e, even when it
ce7d0 69 73 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 is opened multip
ce7e0 6c 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 le times..** The
ce7f0 20 63 6c 6f 73 65 28 29 20 73 79 73 74 65 6d 20 close() system
ce800 63 61 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 call would only
ce810 6f 63 63 75 72 20 77 68 65 6e 20 74 68 65 20 6c occur when the l
ce820 61 73 74 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ast database.**
ce830 75 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 63 using the file c
ce840 6c 6f 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 loses..*/.struct
ce850 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 unixOpenCnt {.
ce860 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 struct unixFile
ce870 49 64 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 Id fileId; /*
ce880 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a The lookup key *
ce890 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 /. int nRef;
ce8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce8b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
ce8c0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 nters to this st
ce8d0 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 ructure */. int
ce8e0 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 nLock;
ce8f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
ce900 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e er of outstandin
ce910 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 g locks */. Uni
ce920 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 xUnusedFd *pUnus
ce930 65 64 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 ed; /* Unus
ce940 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
ce950 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a ors to close */.
ce960 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
ce970 20 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 sem_t *pSem;
ce980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce990 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 /* Named POSIX
ce9a0 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 semaphore */.
ce9b0 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 char aSemName[MA
ce9c0 58 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 X_PATHNAME+2];
ce9d0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 /* Name of that
ce9e0 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 semaphore */.#e
ce9f0 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e ndif. struct un
cea00 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 ixOpenCnt *pNext
cea10 2c 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c , *pPrev; /* L
cea20 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f ist of all unixO
cea30 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a penCnt objects *
cea40 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 /.};../*.** List
cea50 73 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 s of all unixLoc
cea60 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 kInfo and unixOp
cea70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 enCnt objects.
cea80 54 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 65 These used to be
cea90 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e hash.** tables.
ceaa0 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 But the number
ceab0 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 of objects is r
ceac0 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 arely more than
cead0 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e a dozen and.** n
ceae0 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66 ever exceeds a f
ceaf0 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e ew thousand. An
ceb00 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 d lookup is not
ceb10 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a on a critical.**
ceb20 20 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c path so a simpl
ceb30 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 e linked list wi
ceb40 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 ll suffice..*/.s
ceb50 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 tatic struct uni
ceb60 78 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c xLockInfo *lockL
ceb70 69 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 ist = 0;.static
ceb80 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
ceb90 6e 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 nt *openList = 0
ceba0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 ;../*.** This va
cebb0 72 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 riable remembers
cebc0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
cebd0 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 threads can over
cebe0 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
cebf0 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a .** locks..**.**
cec00 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 0: No. Thr
cec10 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 eads cannot over
cec20 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
cec30 20 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 locks. (LinuxT
cec40 68 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a hreads).** 1:
cec50 20 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 20 Yes. Threads
cec60 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 can override eac
cec70 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 h others locks.
cec80 20 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a (Posix & NLPT).
cec90 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e ** -1: We don
ceca0 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 't know yet..**.
cecb0 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 ** On some syste
cecc0 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 ms, we know at c
cecd0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 ompile-time if t
cece0 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 hreads can overr
cecf0 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 ide each.** othe
ced00 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 rs locks. On th
ced10 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 ose systems, the
ced20 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f SQLITE_THREAD_O
ced30 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 VERRIDE_LOCK mac
ced40 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 ro.** will be se
ced50 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e t appropriately.
ced60 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65 On other syste
ced70 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63 ms, we have to c
ced80 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 heck at.** runti
ced90 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 me. On these la
ceda0 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51 tter systems, SQ
cedb0 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 LTIE_THREAD_OVER
cedc0 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 RIDE_LOCK is.**
cedd0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a undefined..**.**
cede0 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e This variable n
cedf0 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 ormally has file
cee00 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 scope only. Bu
cee10 74 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 t during testing
cee20 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 , we make.** it
cee30 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 a global so that
cee40 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 63 the test code c
cee50 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 61 an change its va
cee60 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 lue in order to
cee70 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 verify.** that t
cee80 68 65 20 72 69 67 68 74 20 73 74 75 66 66 20 68 he right stuff h
cee90 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72 appens in either
ceea0 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 case..*/.#if SQ
ceeb0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
ceec0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e && defined(__lin
ceed0 75 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 ux__).# ifndef
ceee0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 SQLITE_THREAD_OV
ceef0 45 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 ERRIDE_LOCK.#
cef00 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 define SQLITE_T
cef10 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
cef20 4f 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a OCK -1.# endif.
cef30 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f # ifdef SQLITE_
cef40 54 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73 TEST.int threads
cef50 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cef60 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 rsLocks = SQLITE
cef70 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
cef80 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 _LOCK;.# else.s
cef90 74 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 64 tatic int thread
cefa0 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
cefb0 65 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 ersLocks = SQLIT
cefc0 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
cefd0 45 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 E_LOCK;.# endif
cefe0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
ceff0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f his structure ho
cf000 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 lds information
cf010 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 passed into indi
cf020 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 vidual test.** t
cf030 68 72 65 61 64 73 20 62 79 20 74 68 65 20 74 65 hreads by the te
cf040 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 stThreadLockingB
cf050 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e ehavior() routin
cf060 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 e..*/.struct thr
cf070 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20 eadTestData {.
cf080 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 int fd;
cf090 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 /* File t
cf0a0 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 o be locked */.
cf0b0 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
cf0c0 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c ck; /* The l
cf0d0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ocking operation
cf0e0 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 */. int result
cf0f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
cf100 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f Result of the lo
cf110 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 cking operation
cf120 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 */.};..#if SQLIT
cf130 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cf140 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cf150 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 _)./*.** This fu
cf160 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 nction is used a
cf170 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 s the main routi
cf180 6e 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 20 ne for a thread
cf190 6c 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 launched by.** t
cf1a0 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
cf1b0 42 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74 Behavior(). It t
cf1c0 65 73 74 73 20 77 68 65 74 68 65 72 20 74 68 65 ests whether the
cf1d0 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 shared-lock obt
cf1e0 61 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 ained.** by the
cf1f0 6d 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74 main thread in t
cf200 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
cf210 42 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c Behavior() confl
cf220 69 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 icts with a.** h
cf230 79 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74 ypothetical writ
cf240 65 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 e-lock obtained
cf250 62 79 20 74 68 69 73 20 74 68 72 65 61 64 20 6f by this thread o
cf260 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e n the same file.
cf270 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 .**.** The write
cf280 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 -lock is not act
cf290 75 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20 ually acquired,
cf2a0 61 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 as this is not p
cf2b0 6f 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 ossible if .** t
cf2c0 68 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 he file is open
cf2d0 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 in read-only mod
cf2e0 65 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 33 e (see ticket #3
cf2f0 34 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 472)..*/ .static
cf300 20 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 void *threadLoc
cf310 6b 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 kingTest(void *p
cf320 41 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 Arg){. struct t
cf330 68 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 hreadTestData *p
cf340 44 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 Data = (struct t
cf350 68 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 hreadTestData*)p
cf360 41 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 Arg;. pData->re
cf370 73 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 sult = fcntl(pDa
cf380 74 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c ta->fd, F_GETLK,
cf390 20 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a &pData->lock);.
cf3a0 20 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d return pArg;.}
cf3b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
cf3c0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cf3d0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cf3e0 5f 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 _) */...#if SQLI
cf3f0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
cf400 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
cf410 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 __)./*.** This p
cf420 72 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 rocedure attempt
cf430 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 s to determine w
cf440 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
cf450 72 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 reads.** can ove
cf460 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 rride each other
cf470 73 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 s locks then set
cf480 73 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 s the .** thread
cf490 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
cf4a0 65 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c ersLocks variabl
cf4b0 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e e appropriately.
cf4c0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
cf4d0 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e testThreadLockin
cf4e0 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 gBehavior(int fd
cf4f0 5f 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 _orig){. int fd
cf500 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 ;. int rc;. st
cf510 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 ruct threadTestD
cf520 61 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20 ata d;. struct
cf530 66 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 flock l;. pthre
cf540 61 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 ad_t t;.. fd =
cf550 64 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 dup(fd_orig);.
cf560 69 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 if( fd<0 ) retur
cf570 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 n;. memset(&l,
cf580 30 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 0, sizeof(l));.
cf590 20 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 l.l_type = F_RD
cf5a0 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d LCK;. l.l_len =
cf5b0 20 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 1;. l.l_start
cf5c0 3d 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 = 0;. l.l_whenc
cf5d0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
cf5e0 72 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 rc = fcntl(fd_or
cf5f0 69 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 ig, F_SETLK, &l)
cf600 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 ;. if( rc!=0 )
cf610 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 return;. memset
cf620 28 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 (&d, 0, sizeof(d
cf630 29 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b ));. d.fd = fd;
cf640 0a 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 . d.lock = l;.
cf650 20 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d d.lock.l_type =
cf660 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 F_WRLCK;. if(
cf670 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 pthread_create(&
cf680 74 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b t, 0, threadLock
cf690 69 6e 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 ingTest, &d)==0
cf6a0 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6a ){. pthread_j
cf6b0 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 oin(t, 0);. }.
cf6c0 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 close(fd);. if
cf6d0 28 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 ( d.result!=0 )
cf6e0 72 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 return;. thread
cf6f0 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
cf700 65 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f ersLocks = (d.lo
cf710 63 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c ck.l_type==F_UNL
cf720 43 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a CK);.}.#endif /*
cf730 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
cf740 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
cf750 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a linux__) */../*.
cf760 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 ** Release a uni
cf770 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 xLockInfo struct
cf780 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 ure previously a
cf790 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 llocated by find
cf7a0 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a LockInfo()..**.*
cf7b0 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 * The mutex ente
cf7c0 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e red using the un
cf7d0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 ixEnterMutex() f
cf7e0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 unction must be
cf7f0 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 held.** when thi
cf800 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
cf810 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lled..*/.static
cf820 76 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b void releaseLock
cf830 49 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69 78 Info(struct unix
cf840 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 LockInfo *pLock)
cf850 7b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 {. assert( unix
cf860 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 MutexHeld() );.
cf870 20 69 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 if( pLock ){.
cf880 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b pLock->nRef--;
cf890 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e . if( pLock->
cf8a0 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 nRef==0 ){.
cf8b0 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 if( pLock->pPre
cf8c0 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 v ){. ass
cf8d0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 ert( pLock->pPre
cf8e0 76 2d 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 v->pNext==pLock
cf8f0 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b );. pLock
cf900 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d ->pPrev->pNext =
cf910 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pLock->pNext;.
cf920 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
cf930 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b assert( lock
cf940 4c 69 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 List==pLock );.
cf950 20 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 lockList
cf960 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a = pLock->pNext;.
cf970 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
cf980 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 ( pLock->pNext )
cf990 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
cf9a0 28 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e ( pLock->pNext->
cf9b0 70 50 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a pPrev==pLock );.
cf9c0 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 pLock->p
cf9d0 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c Next->pPrev = pL
cf9e0 6f 63 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 ock->pPrev;.
cf9f0 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
cfa00 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 3_free(pLock);.
cfa10 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
cfa20 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 * Release a unix
cfa30 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 OpenCnt structur
cfa40 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c e previously all
cfa50 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f ocated by findLo
cfa60 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 ckInfo()..**.**
cfa70 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 The mutex entere
cfa80 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 d using the unix
cfa90 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e EnterMutex() fun
cfaa0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 ction must be he
cfab0 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 ld.** when this
cfac0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
cfad0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
cfae0 69 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e id releaseOpenCn
cfaf0 74 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 t(struct unixOpe
cfb00 6e 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 nCnt *pOpen){.
cfb10 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 assert( unixMute
cfb20 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 xHeld() );. if(
cfb30 20 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f pOpen ){. pO
cfb40 70 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 pen->nRef--;.
cfb50 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 if( pOpen->nRef
cfb60 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
cfb70 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b pOpen->pPrev ){
cfb80 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
cfb90 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 pOpen->pPrev->p
cfba0 4e 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 Next==pOpen );.
cfbb0 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 pOpen->pP
cfbc0 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 rev->pNext = pOp
cfbd0 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 en->pNext;.
cfbe0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
cfbf0 61 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 assert( openList
cfc00 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 ==pOpen );.
cfc10 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f openList = pO
cfc20 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 pen->pNext;.
cfc30 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f }. if( pO
cfc40 70 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 pen->pNext ){.
cfc50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
cfc60 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 pen->pNext->pPre
cfc70 76 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 v==pOpen );.
cfc80 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 pOpen->pNext
cfc90 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d ->pPrev = pOpen-
cfca0 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a >pPrev;. }.
cfcb0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
cfcc0 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 DSAFE && defined
cfcd0 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 20 (__linux__).
cfce0 20 20 61 73 73 65 72 74 28 20 21 70 4f 70 65 6e assert( !pOpen
cfcf0 2d 3e 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 72 ->pUnused || thr
cfd00 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
cfd10 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 OthersLocks==0 )
cfd20 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 ;.#endif..
cfd30 2f 2a 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 6e /* If pOpen->pUn
cfd40 75 73 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c 6c used is not null
cfd50 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6e , then memory an
cfd60 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f d file-descripto
cfd70 72 73 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 rs. ** are
cfd80 6c 65 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a 2a leaked.. **
cfd90 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 77 . ** This w
cfda0 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 ill only happen
cfdb0 69 66 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 74 if, under Linuxt
cfdc0 68 72 65 61 64 73 2c 20 74 68 65 20 75 73 65 72 hreads, the user
cfdd0 20 68 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 20 has opened.
cfde0 20 20 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 ** a transacti
cfdf0 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 on in one thread
cfe00 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 , then attempts
cfe10 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 to close the dat
cfe20 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 abase. ** h
cfe30 61 6e 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 68 andle from anoth
cfe40 65 72 20 74 68 72 65 61 64 20 28 77 69 74 68 6f er thread (witho
cfe50 75 74 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 69 ut first unlocki
cfe60 6e 67 20 74 68 65 20 64 62 20 66 69 6c 65 29 2e ng the db file).
cfe70 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 . ** This i
cfe80 73 20 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f 0a s a misuse. */.
cfe90 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
cfea0 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d ee(pOpen);. }
cfeb0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 . }.}../*.** Gi
cfec0 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 ven a file descr
cfed0 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e iptor, locate un
cfee0 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 ixLockInfo and u
cfef0 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 nixOpenCnt struc
cff00 74 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 tures that.** de
cff10 73 63 72 69 62 65 73 20 74 68 61 74 20 66 69 6c scribes that fil
cff20 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 e descriptor. C
cff30 72 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 reate new ones i
cff40 66 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 f necessary. Th
cff50 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 e.** return valu
cff60 65 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e es might be unin
cff70 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 itialized if an
cff80 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a error occurs..**
cff90 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e .** The mutex en
cffa0 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 tered using the
cffb0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
cffc0 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 function must b
cffd0 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 e held.** when t
cffe0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
cfff0 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 called..**.** Re
d0000 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 turn an appropri
d0010 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a ate error code..
d0020 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
d0030 6e 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e ndLockInfo(. un
d0040 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 ixFile *pFile,
d0050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d0060 55 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 Unix file with f
d0070 69 6c 65 20 64 65 73 63 20 75 73 65 64 20 69 6e ile desc used in
d0080 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 the key */. st
d0090 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
d00a0 6f 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 o **ppLock, /*
d00b0 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c Return the unixL
d00c0 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ockInfo structur
d00d0 65 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 e here */. stru
d00e0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
d00f0 2a 70 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 *ppOpen /* Re
d0100 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 turn the unixOpe
d0110 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 nCnt structure h
d0120 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
d0130 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
d0140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 /* Sy
d0150 73 74 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e stem call return
d0160 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 code */. int f
d0170 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
d0180 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
d0190 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d01a0 20 66 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 for pFile */.
d01b0 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b struct unixLockK
d01c0 65 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f ey lockKey; /
d01d0 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 * Lookup key for
d01e0 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 the unixLockInf
d01f0 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 o structure */.
d0200 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 struct unixFile
d0210 49 64 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 Id fileId;
d0220 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f /* Lookup key fo
d0230 72 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e r the unixOpenCn
d0240 74 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 t struct */. st
d0250 72 75 63 74 20 73 74 61 74 20 73 74 61 74 62 75 ruct stat statbu
d0260 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 f; /*
d0270 4c 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 Low-level file i
d0280 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 nformation */.
d0290 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 struct unixLockI
d02a0 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f nfo *pLock = 0;/
d02b0 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 * Candidate unix
d02c0 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 LockInfo object
d02d0 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 */. struct unix
d02e0 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 OpenCnt *pOpen;
d02f0 20 20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 /* Candidate
d0300 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a unixOpenCnt obj
d0310 65 63 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ect */.. assert
d0320 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 ( unixMutexHeld(
d0330 29 20 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c ) );.. /* Get l
d0340 6f 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 ow-level informa
d0350 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 tion about the f
d0360 69 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 ile that we can
d0370 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 used to. ** cre
d0380 61 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d ate a unique nam
d0390 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a e for the file..
d03a0 20 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c */. fd = pFil
d03b0 65 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 e->h;. rc = fst
d03c0 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 at(fd, &statbuf)
d03d0 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b ;. if( rc!=0 ){
d03e0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
d03f0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 Errno = errno;.#
d0400 69 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a ifdef EOVERFLOW.
d0410 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c if( pFile->l
d0420 61 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 astErrno==EOVERF
d0430 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c LOW ) return SQL
d0440 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 ITE_NOLFS;.#endi
d0450 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c f. return SQL
d0460 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a ITE_IOERR;. }..
d0470 23 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f #ifdef __APPLE__
d0480 0a 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e . /* On OS X on
d0490 20 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 an msdos filesy
d04a0 73 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 stem, the inode
d04b0 6e 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 number is report
d04c0 65 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 ed. ** incorrec
d04d0 74 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a tly for zero-siz
d04e0 65 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 e files. See ti
d04f0 63 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 cket #3260. To
d0500 77 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 work. ** around
d0510 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 this problem (w
d0520 65 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 e consider it a
d0530 62 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 bug in OS X, not
d0540 20 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 SQLite). ** we
d0550 20 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 always increase
d0560 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 the file size t
d0570 6f 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 o 1 by writing a
d0580 20 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a single byte. *
d0590 2a 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 * prior to acces
d05a0 73 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e sing the inode n
d05b0 75 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 umber. The one
d05c0 62 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a byte written is.
d05d0 20 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 ** an ASCII 'S
d05e0 27 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 ' character whic
d05f0 68 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 h also happens t
d0600 6f 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 o be the first b
d0610 79 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 yte. ** in the
d0620 68 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 header of every
d0630 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e SQLite database.
d0640 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 In this way, i
d0650 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 f there. ** is
d0660 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e a race condition
d0670 20 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 such that anoth
d0680 65 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c er thread has al
d0690 72 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a ready populated.
d06a0 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 ** the first p
d06b0 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 age of the datab
d06c0 61 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 ase, no damage i
d06d0 73 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 s done.. */. i
d06e0 66 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 f( statbuf.st_si
d06f0 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 ze==0 ){. rc
d0700 3d 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c = write(fd, "S",
d0710 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 1);. if( rc!
d0720 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =1 ){. retu
d0730 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
d0740 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
d0750 66 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 fstat(fd, &statb
d0760 75 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 uf);. if( rc!
d0770 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c =0 ){. pFil
d0780 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
d0790 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 rrno;. retu
d07a0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
d07b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
d07c0 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 f.. memset(&loc
d07d0 6b 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 kKey, 0, sizeof(
d07e0 6c 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 lockKey));. loc
d07f0 6b 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 kKey.fid.dev = s
d0800 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 tatbuf.st_dev;.#
d0810 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
d0820 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 lockKey.fid.pId
d0830 3d 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 = pFile->pId;.#e
d0840 6c 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 lse. lockKey.fi
d0850 64 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e d.ino = statbuf.
d0860 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 st_ino;.#endif.#
d0870 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
d0880 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
d0890 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 __linux__). if(
d08a0 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 threadsOverride
d08b0 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c EachOthersLocks<
d08c0 30 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 0 ){. testThr
d08d0 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
d08e0 6f 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f or(fd);. }. lo
d08f0 63 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 ckKey.tid = thre
d0900 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
d0910 74 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a thersLocks ? 0 :
d0920 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b pthread_self();
d0930 0a 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 .#endif. fileId
d0940 20 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a = lockKey.fid;.
d0950 20 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 if( ppLock!=0
d0960 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c ){. pLock = l
d0970 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 ockList;. whi
d0980 6c 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d le( pLock && mem
d0990 63 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 cmp(&lockKey, &p
d09a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 Lock->lockKey, s
d09b0 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 izeof(lockKey))
d09c0 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d ){. pLock =
d09d0 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pLock->pNext;.
d09e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f }. if( pLo
d09f0 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 ck==0 ){. p
d0a00 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d Lock = sqlite3_m
d0a10 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 alloc( sizeof(*p
d0a20 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 Lock) );. i
d0a30 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 f( pLock==0 ){.
d0a40 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
d0a50 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
d0a60 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 goto exit_find
d0a70 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 lockinfo;.
d0a80 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 }. memcpy(&
d0a90 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 26 pLock->lockKey,&
d0aa0 6c 6f 63 6b 4b 65 79 2c 73 69 7a 65 6f 66 28 6c lockKey,sizeof(l
d0ab0 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 20 20 20 20 ockKey));.
d0ac0 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b pLock->nRef = 1;
d0ad0 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e . pLock->cn
d0ae0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f t = 0;. pLo
d0af0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 30 ck->locktype = 0
d0b00 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 ;. pLock->p
d0b10 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b Next = lockList;
d0b20 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 . pLock->pP
d0b30 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 rev = 0;. i
d0b40 66 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f f( lockList ) lo
d0b50 63 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 ckList->pPrev =
d0b60 70 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 6c 6f 63 pLock;. loc
d0b70 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 kList = pLock;.
d0b80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d0b90 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 pLock->nRef++;.
d0ba0 20 20 20 7d 0a 20 20 20 20 2a 70 70 4c 6f 63 6b }. *ppLock
d0bb0 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 = pLock;. }.
d0bc0 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b if( ppOpen!=0 ){
d0bd0 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 65 . pOpen = ope
d0be0 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 nList;. while
d0bf0 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 6d ( pOpen && memcm
d0c00 70 28 26 66 69 6c 65 49 64 2c 20 26 70 4f 70 65 p(&fileId, &pOpe
d0c10 6e 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f n->fileId, sizeo
d0c20 66 28 66 69 6c 65 49 64 29 29 20 29 7b 0a 20 20 f(fileId)) ){.
d0c30 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 4f 70 65 pOpen = pOpe
d0c40 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a n->pNext;. }.
d0c50 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 if( pOpen==0
d0c60 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 ){. pOpen
d0c70 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
d0c80 28 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 ( sizeof(*pOpen)
d0c90 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f );. if( pO
d0ca0 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pen==0 ){.
d0cb0 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 releaseLockInf
d0cc0 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 o(pLock);.
d0cd0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
d0ce0 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 MEM;. got
d0cf0 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 o exit_findlocki
d0d00 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 nfo;. }.
d0d10 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 65 6e 2c memset(pOpen,
d0d20 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 0, sizeof(*pOpe
d0d30 6e 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e n));. pOpen
d0d40 2d 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 49 ->fileId = fileI
d0d50 64 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e d;. pOpen->
d0d60 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 nRef = 1;.
d0d70 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f pOpen->pNext = o
d0d80 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 69 penList;. i
d0d90 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f 70 f( openList ) op
d0da0 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 enList->pPrev =
d0db0 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70 65 pOpen;. ope
d0dc0 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a 20 nList = pOpen;.
d0dd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d0de0 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 pOpen->nRef++;.
d0df0 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65 6e }. *ppOpen
d0e00 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65 = pOpen;. }..e
d0e10 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f xit_findlockinfo
d0e20 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d :. return rc;.}
d0e30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 ../*.** If we ar
d0e40 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 e currently in a
d0e50 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
d0e60 64 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 d than the threa
d0e70 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e d that the.** un
d0e80 69 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 ixFile argument
d0e90 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e belongs to, then
d0ea0 20 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 transfer owners
d0eb0 68 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 hip of the unixF
d0ec0 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 ile.** over to t
d0ed0 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 he current threa
d0ee0 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 d..**.** A unixF
d0ef0 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 ile is only owne
d0f00 64 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e d by a thread on
d0f10 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 75 73 systems that us
d0f20 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a e LinuxThreads..
d0f30 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70 20 **.** Ownership
d0f40 74 72 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c 79 transfer is only
d0f50 20 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 20 allowed if the
d0f60 75 6e 69 78 46 69 6c 65 20 69 73 20 63 75 72 72 unixFile is curr
d0f70 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a ently unlocked..
d0f80 2a 2a 20 49 66 20 74 68 65 20 75 6e 69 78 46 69 ** If the unixFi
d0f90 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e 64 le is locked and
d0fa0 20 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 69 73 an ownership is
d0fb0 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65 74 wrong, then ret
d0fc0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 urn.** SQLITE_MI
d0fd0 53 55 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f 4b SUSE. SQLITE_OK
d0fe0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
d0ff0 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 everything works
d1000 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
d1010 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
d1020 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
d1030 0a 73 74 61 74 69 63 20 69 6e 74 20 74 72 61 6e .static int tran
d1040 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75 6e sferOwnership(un
d1050 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a ixFile *pFile){.
d1060 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 int rc;. pthr
d1070 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 ead_t hSelf;. i
d1080 66 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 f( threadsOverri
d1090 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b deEachOthersLock
d10a0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 s ){. /* Owne
d10b0 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 73 20 rship transfers
d10c0 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 not needed on th
d10d0 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 is system */.
d10e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d10f0 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d K;. }. hSelf =
d1100 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b pthread_self();
d1110 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 65 . if( pthread_e
d1120 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c qual(pFile->tid,
d1130 20 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f hSelf) ){. /
d1140 2a 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20 69 * We are still i
d1150 6e 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 n the same threa
d1160 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 d */. OSTRACE
d1170 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 1("No-transfer,
d1180 73 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29 3b same thread\n");
d1190 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d11a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
d11b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d11c0 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 !=NO_LOCK ){.
d11d0 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 /* We cannot ch
d11e0 61 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20 77 ange ownership w
d11f0 68 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c 64 hile we are hold
d1200 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 ing a lock! */.
d1210 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d1220 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f _MISUSE;. }. O
d1230 53 54 52 41 43 45 34 28 22 54 72 61 6e 73 66 65 STRACE4("Transfe
d1240 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 25 r ownership of %
d1250 64 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c d from %d to %d\
d1260 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
d1270 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d pFile->h, pFile-
d1280 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 >tid, hSelf);.
d1290 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 pFile->tid = hSe
d12a0 6c 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d lf;. if (pFile-
d12b0 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 >pLock != NULL)
d12c0 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 {. releaseLoc
d12d0 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f kInfo(pFile->pLo
d12e0 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 ck);. rc = fi
d12f0 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 ndLockInfo(pFile
d1300 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c , &pFile->pLock,
d1310 20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 0);. OSTRACE
d1320 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 73 5("LOCK %d is
d1330 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c 6e now %s(%s,%d)\n
d1340 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 ", pFile->h,.
d1350 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
d1360 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b Name(pFile->lock
d1370 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 20 type),.
d1380 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 locktypeName(p
d1390 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 File->pLock->loc
d13a0 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e 70 ktype), pFile->p
d13b0 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 20 Lock->cnt);.
d13c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 return rc;. } e
d13d0 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e lse {. return
d13e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
d13f0 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20 6e }.#else /* if n
d1400 6f 74 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ot SQLITE_THREAD
d1410 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f 6e 20 SAFE */. /* On
d1420 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
d1430 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 69 builds, ownershi
d1440 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 20 p transfer is a
d1450 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e no-op */.# defin
d1460 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 e transferOwners
d1470 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f 4b hip(X) SQLITE_OK
d1480 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
d1490 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a E_THREADSAFE */.
d14a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
d14b0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
d14c0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
d14d0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
d14e0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
d14f0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
d1500 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
d1510 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
d1520 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 ck is held, set
d1530 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 *pResOut.** to a
d1540 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 non-zero value
d1550 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f otherwise *pResO
d1560 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 ut is set to zer
d1570 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 o. The return v
d1580 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 alue.** is set t
d1590 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 o SQLITE_OK unle
d15a0 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 ss an I/O error
d15b0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f occurs during lo
d15c0 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a ck checking..*/.
d15d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 static int unixC
d15e0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
d15f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d1600 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 d, int *pResOut)
d1610 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
d1620 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 ITE_OK;. int re
d1630 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e served = 0;. un
d1640 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d1650 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a (unixFile*)id;..
d1660 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
d1670 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
d1680 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
d1690 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 20 RVEDLOCK; );..
d16a0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d16b0 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
d16c0 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 x(); /* Because
d16d0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 pFile->pLock is
d16e0 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 shared across th
d16f0 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 reads */.. /* C
d1700 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 heck if a thread
d1710 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 in this process
d1720 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f holds such a lo
d1730 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c ck */. if( pFil
d1740 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 e->pLock->lockty
d1750 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 pe>SHARED_LOCK )
d1760 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d {. reserved =
d1770 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 1;. }.. /* Ot
d1780 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 herwise see if s
d1790 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ome other proces
d17a0 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f s holds it.. */
d17b0 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50 50 .#ifndef __DJGPP
d17c0 5f 5f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 __. if( !reserv
d17d0 65 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 ed ){. struct
d17e0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 flock lock;.
d17f0 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d lock.l_whence =
d1800 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c SEEK_SET;. l
d1810 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 ock.l_start = RE
d1820 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 SERVED_BYTE;.
d1830 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b lock.l_len = 1;
d1840 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
d1850 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 = F_WRLCK;.
d1860 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 if (-1 == fcntl(
d1870 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c pFile->h, F_GETL
d1880 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 K, &lock)) {.
d1890 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
d18a0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 errno;. rc
d18b0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d18c0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d18d0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d18e0 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
d18f0 43 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 CK);. pFile
d1900 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d1910 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 rrno;. } else
d1920 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 if( lock.l_type
d1930 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 !=F_UNLCK ){.
d1940 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b reserved = 1;
d1950 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
d1960 66 0a 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 f. . unixLeave
d1970 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 Mutex();. OSTRA
d1980 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE4("TEST WR-LOC
d1990 4b 20 25 64 20 25 64 20 25 64 20 28 75 6e 69 78 K %d %d %d (unix
d19a0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
d19b0 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a rc, reserved);..
d19c0 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 *pResOut = res
d19d0 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 erved;. return
d19e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 rc;.}../*.** Per
d19f0 66 6f 72 6d 20 61 20 66 69 6c 65 20 6c 6f 63 6b form a file lock
d1a00 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e ing operation on
d1a10 20 61 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 a range of byte
d1a20 73 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2a 20 s in a file..**
d1a30 54 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 The "op" paramet
d1a40 65 72 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 er should be one
d1a50 20 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 46 5f 57 of F_RDLCK, F_W
d1a60 52 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e 4c 43 4b RLCK, or F_UNLCK
d1a70 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e ..** Return 0 on
d1a80 20 73 75 63 63 65 73 73 20 6f 72 20 2d 31 20 66 success or -1 f
d1a90 6f 72 20 66 61 69 6c 75 72 65 2e 20 20 4f 6e 20 or failure. On
d1aa0 66 61 69 6c 75 72 65 2c 20 77 72 69 74 65 20 74 failure, write t
d1ab0 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 he error.** code
d1ac0 20 69 6e 74 6f 20 2a 70 45 72 72 63 6f 64 65 2e into *pErrcode.
d1ad0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 .**.** If the SQ
d1ae0 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
d1af0 4c 4f 43 4b 49 4e 47 20 62 69 74 20 69 73 20 63 LOCKING bit is c
d1b00 6c 65 61 72 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 lear, then only
d1b10 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 61 6e 67 lock.** the rang
d1b20 65 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 74 68 e of bytes on th
d1b30 65 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 62 e locking page b
d1b40 65 74 77 65 65 6e 20 53 48 41 52 45 44 5f 46 49 etween SHARED_FI
d1b50 52 53 54 20 61 6e 64 0a 2a 2a 20 53 48 41 52 45 RST and.** SHARE
d1b60 44 5f 53 49 5a 45 2e 20 20 49 66 20 53 51 4c 49 D_SIZE. If SQLI
d1b70 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f TE_WHOLE_FILE_LO
d1b80 43 4b 49 4e 47 20 69 73 20 73 65 74 2c 20 74 68 CKING is set, th
d1b90 65 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a 2a 20 62 en lock all.** b
d1ba0 79 74 65 73 20 66 72 6f 6d 20 30 20 75 70 20 74 ytes from 0 up t
d1bb0 6f 20 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 o but not includ
d1bc0 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ing PENDING_BYTE
d1bd0 2c 20 61 6e 64 20 61 6c 6c 20 62 79 74 65 73 0a , and all bytes.
d1be0 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 53 ** that follow S
d1bf0 48 41 52 45 44 5f 46 49 52 53 54 2e 0a 2a 2a 0a HARED_FIRST..**.
d1c00 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 ** In other word
d1c10 73 2c 20 6f 66 20 53 51 4c 49 54 45 5f 57 48 4f s, of SQLITE_WHO
d1c20 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 LE_FILE_LOCKING
d1c30 69 66 20 66 61 6c 73 65 20 28 74 68 65 20 68 69 if false (the hi
d1c40 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 65 66 61 storical.** defa
d1c50 75 6c 74 20 63 61 73 65 29 20 74 68 65 6e 20 6f ult case) then o
d1c60 6e 6c 79 20 6c 6f 63 6b 20 61 20 73 6d 61 6c 6c nly lock a small
d1c70 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 range of bytes
d1c80 66 72 6f 6d 20 53 48 41 52 45 44 5f 46 49 52 53 from SHARED_FIRS
d1c90 54 0a 2a 2a 20 74 68 72 6f 75 67 68 20 53 48 41 T.** through SHA
d1ca0 52 45 44 5f 46 49 52 53 54 2b 53 48 41 52 45 44 RED_FIRST+SHARED
d1cb0 5f 53 49 5a 45 2d 31 2e 20 20 42 75 74 20 69 66 _SIZE-1. But if
d1cc0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
d1cd0 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 0a 2a 2a LE_LOCKING is.**
d1ce0 20 74 72 75 65 20 74 68 65 6e 20 6c 6f 63 6b 20 true then lock
d1cf0 65 76 65 72 79 20 62 79 74 65 20 69 6e 20 74 68 every byte in th
d1d00 65 20 66 69 6c 65 20 65 78 63 65 70 74 20 66 6f e file except fo
d1d10 72 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 61 r PENDING_BYTE a
d1d20 6e 64 0a 2a 2a 20 52 45 53 45 52 56 45 44 5f 42 nd.** RESERVED_B
d1d30 59 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 YTE..**.** SQLIT
d1d40 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d1d50 4b 49 4e 47 3d 74 72 75 65 20 6f 76 65 72 6c 61 KING=true overla
d1d60 70 73 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f ps SQLITE_WHOLE_
d1d70 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 66 61 6c FILE_LOCKING=fal
d1d80 73 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 74 68 65 se.** and so the
d1d90 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 locking schemes
d1da0 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e are compatible.
d1db0 20 20 4f 6e 65 20 74 79 70 65 20 6f 66 20 6c 6f One type of lo
d1dc0 63 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 66 65 63 ck will.** effec
d1dd0 74 69 76 65 6c 79 20 65 78 63 6c 75 64 65 20 74 tively exclude t
d1de0 68 65 20 6f 74 68 65 72 20 74 79 70 65 2e 20 20 he other type.
d1df0 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 The reason for u
d1e00 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 sing the.** SQLI
d1e10 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f TE_WHOLE_FILE_LO
d1e20 43 4b 49 4e 47 3d 74 72 75 65 20 69 73 20 74 68 CKING=true is th
d1e30 61 74 20 62 79 20 69 6e 64 69 63 61 74 69 6e 67 at by indicating
d1e40 20 74 68 65 20 66 75 6c 6c 20 72 61 6e 67 65 0a the full range.
d1e50 2a 2a 20 6f 66 20 62 79 74 65 73 20 74 6f 20 62 ** of bytes to b
d1e60 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 e read or writte
d1e70 6e 2c 20 77 65 20 67 69 76 65 20 68 69 6e 74 73 n, we give hints
d1e80 20 74 6f 20 4e 46 53 20 74 6f 20 68 65 6c 70 20 to NFS to help
d1e90 69 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 6e 20 63 it.** maintain c
d1ea0 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 2e 20 ache coherency.
d1eb0 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 On the other ha
d1ec0 6e 64 2c 20 77 68 6f 6c 65 20 66 69 6c 65 20 6c nd, whole file l
d1ed0 6f 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 73 6c 6f ocking.** is slo
d1ee0 77 65 72 2c 20 73 6f 20 77 65 20 64 6f 6e 27 74 wer, so we don't
d1ef0 20 77 61 6e 74 20 74 6f 20 75 73 65 20 69 74 20 want to use it
d1f00 65 78 63 65 70 74 20 66 6f 72 20 4e 46 53 2e 0a except for NFS..
d1f10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61 */.static int ra
d1f20 6e 67 65 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 ngeLock(unixFile
d1f30 20 2a 70 46 69 6c 65 2c 20 69 6e 74 20 6f 70 2c *pFile, int op,
d1f40 20 69 6e 74 20 2a 70 45 72 72 63 6f 64 65 29 7b int *pErrcode){
d1f50 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 . struct flock
d1f60 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 3b 0a lock;. int rc;.
d1f70 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
d1f80 6f 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 op;. lock.l_sta
d1f90 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 rt = SHARED_FIRS
d1fa0 54 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e T;. lock.l_when
d1fb0 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
d1fc0 20 69 66 28 20 28 70 46 69 6c 65 2d 3e 66 69 6c if( (pFile->fil
d1fd0 65 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f eFlags & SQLITE_
d1fe0 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 WHOLE_FILE_LOCKI
d1ff0 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6c 6f NG)==0 ){. lo
d2000 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 ck.l_len = SHARE
d2010 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 63 20 3d D_SIZE;. rc =
d2020 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d2030 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d2040 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 ;. *pErrcode
d2050 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 6c 73 65 = errno;. }else
d2060 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e {. lock.l_len
d2070 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 = 0;. rc = f
d2080 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 cntl(pFile->h, F
d2090 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a _SETLK, &lock);.
d20a0 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 *pErrcode =
d20b0 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 4e errno;. if( N
d20c0 45 56 45 52 28 6f 70 3d 3d 46 5f 55 4e 4c 43 4b EVER(op==F_UNLCK
d20d0 29 20 7c 7c 20 72 63 21 3d 28 2d 31 29 20 29 7b ) || rc!=(-1) ){
d20e0 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 . lock.l_st
d20f0 61 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c art = 0;. l
d2100 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 45 4e 44 ock.l_len = PEND
d2110 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 ING_BYTE;.
d2120 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 rc = fcntl(pFile
d2130 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
d2140 6f 63 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ock);. if(
d2150 41 4c 57 41 59 53 28 6f 70 21 3d 46 5f 55 4e 4c ALWAYS(op!=F_UNL
d2160 43 4b 29 20 26 26 20 72 63 3d 3d 28 2d 31 29 20 CK) && rc==(-1)
d2170 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72 ){. *pErr
d2180 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 code = errno;.
d2190 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 lock.l_typ
d21a0 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 e = F_UNLCK;.
d21b0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 lock.l_star
d21c0 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
d21d0 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c ;. lock.l
d21e0 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 _len = 0;.
d21f0 20 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 fcntl(pFile->h
d2200 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
d2210 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
d2220 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
d2230 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 ;.}../*.** Lock
d2240 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 the file with th
d2250 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 e lock specified
d2260 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f by parameter lo
d2270 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 cktype - one.**
d2280 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
d2290 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 :.**.** (1)
d22a0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SHARED_LOCK.**
d22b0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f (2) RESERVED_
d22c0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 LOCK.** (3)
d22d0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 PENDING_LOCK.**
d22e0 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 (4) EXCLUSIV
d22f0 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d E_LOCK.**.** Som
d2300 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 etimes when requ
d2310 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 esting one lock
d2320 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 state, additiona
d2330 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a l lock states.**
d2340 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e are inserted in
d2350 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c between. The l
d2360 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 ocking might fai
d2370 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 l on one of the
d2380 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 later.** transit
d2390 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 ions leaving the
d23a0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 lock state diff
d23b0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 erent from what
d23c0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a it started but.*
d23d0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 * still short of
d23e0 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 its goal. The
d23f0 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 following chart
d2400 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 shows the allowe
d2410 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 d.** transitions
d2420 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 and the inserte
d2430 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 d intermediate s
d2440 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 tates:.**.**
d2450 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 UNLOCKED -> SHAR
d2460 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
d2470 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 -> RESERVED.**
d2480 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e SHARED -> (PEN
d2490 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
d24a0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 VE.** RESERVE
d24b0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
d24c0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
d24d0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 PENDING -> EXC
d24e0 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 LUSIVE.**.** Thi
d24f0 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
d2500 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
d2510 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 ock. Use the sq
d2520 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a lite3OsUnlock().
d2530 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f ** routine to lo
d2540 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 wer a locking le
d2550 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 vel..*/.static i
d2560 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 nt unixLock(sqli
d2570 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
d2580 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f t locktype){. /
d2590 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
d25a0 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d describes the im
d25b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
d25c0 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b the various lock
d25d0 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 s and. ** lock
d25e0 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 transitions in t
d25f0 65 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 erms of the POSI
d2600 58 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 X advisory share
d2610 64 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a d and exclusive.
d2620 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 ** lock primit
d2630 69 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 ives (called rea
d2640 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 d-locks and writ
d2650 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 e-locks below, t
d2660 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e o avoid. ** con
d2670 66 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 fusion with SQLi
d2680 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 te lock names).
d2690 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 The algorithms a
d26a0 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 re complicated.
d26b0 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 ** slightly in
d26c0 6f 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 order to be comp
d26d0 61 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 atible with wind
d26e0 6f 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 ows systems simu
d26f0 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 ltaneously. **
d2700 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 accessing the sa
d2710 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 me database file
d2720 2c 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 , in case that i
d2730 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e s ever required.
d2740 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f . **. ** Symbo
d2750 6c 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 ls defined in os
d2760 2e 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 .h indentify the
d2770 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 'pending byte'
d2780 61 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 and the 'reserve
d2790 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 d. ** byte', ea
d27a0 63 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 ch single bytes
d27b0 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 at well known of
d27c0 66 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 fsets, and the '
d27d0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
d27e0 20 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 range', a range
d27f0 20 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 of 510 bytes at
d2800 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 a well known of
d2810 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 fset.. **. **
d2820 54 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 To obtain a SHAR
d2830 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d ED lock, a read-
d2840 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
d2850 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 on the 'pending
d2860 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 . ** byte'. If
d2870 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 this is success
d2880 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 ful, a random by
d2890 74 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 te from the 'sha
d28a0 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 red byte. ** ra
d28b0 6e 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 nge' is read-loc
d28c0 6b 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b ked and the lock
d28d0 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 on the 'pending
d28e0 20 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e byte' released.
d28f0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f . **. ** A pro
d2900 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 cess may only ob
d2910 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 tain a RESERVED
d2920 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 lock after it ha
d2930 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e s a SHARED lock.
d2940 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 . ** A RESERVED
d2950 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 lock is impleme
d2960 6e 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 nted by grabbing
d2970 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e a write-lock on
d2980 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 the. ** 'reser
d2990 76 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a ved byte'. . **
d29a0 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 . ** A process
d29b0 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 may only obtain
d29c0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 a PENDING lock a
d29d0 66 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 fter it has obta
d29e0 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 ined a. ** SHAR
d29f0 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 ED lock. A PENDI
d2a00 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 NG lock is imple
d2a10 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e mented by obtain
d2a20 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
d2a30 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 . ** on the 'pe
d2a40 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 nding byte'. Thi
d2a50 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e s ensures that n
d2a60 6f 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 o new SHARED loc
d2a70 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f ks can be. ** o
d2a80 62 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 btained, but exi
d2a90 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 sting SHARED loc
d2aa0 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 ks are allowed t
d2ab0 6f 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f o persist. A pro
d2ac0 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e cess. ** does n
d2ad0 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 ot have to obtai
d2ae0 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 n a RESERVED loc
d2af0 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 k on the way to
d2b00 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a a PENDING lock..
d2b10 20 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 ** This proper
d2b20 74 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 ty is used by th
d2b30 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 e algorithm for
d2b40 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a rolling back a j
d2b50 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a ournal file. **
d2b60 20 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a after a crash..
d2b70 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 **. ** An EXC
d2b80 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 LUSIVE lock, obt
d2b90 61 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 ained after a PE
d2ba0 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 NDING lock is he
d2bb0 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c ld, is. ** impl
d2bc0 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 emented by obtai
d2bd0 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 ning a write-loc
d2be0 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 k on the entire
d2bf0 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 'shared byte. *
d2c00 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 * range'. Since
d2c10 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 all other locks
d2c20 72 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c require a read-l
d2c30 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ock on one of th
d2c40 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 e bytes. ** wit
d2c50 68 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 hin this range,
d2c60 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 this ensures tha
d2c70 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 t no other locks
d2c80 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 are held on the
d2c90 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 . ** database.
d2ca0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 . **. ** The r
d2cb0 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 eason a single b
d2cc0 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 yte cannot be us
d2cd0 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 ed instead of th
d2ce0 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 e 'shared byte.
d2cf0 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 ** range' is th
d2d00 61 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 at some versions
d2d10 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e of windows do n
d2d20 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d ot support read-
d2d30 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c locks. By. ** l
d2d40 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 ocking a random
d2d50 62 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 byte from a rang
d2d60 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 e, concurrent SH
d2d70 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 ARED locks may e
d2d80 78 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 xist. ** even i
d2d90 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 f the locking pr
d2da0 69 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 imitive used is
d2db0 61 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c always a write-l
d2dc0 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 ock.. */. int
d2dd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d2de0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d2df0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d2e00 64 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 d;. struct unix
d2e10 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 LockInfo *pLock
d2e20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a = pFile->pLock;.
d2e30 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
d2e40 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 20 3d 20 30 ock;. int s = 0
d2e50 3b 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 0a ;. int tErrno;.
d2e60 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
d2e70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 );. OSTRACE7("
d2e80 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 LOCK %d %s wa
d2e90 73 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d s %s(%s,%d) pid=
d2ea0 25 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 %d (unix)\n", pF
d2eb0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f ile->h,. lo
d2ec0 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 cktypeName(lockt
d2ed0 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 ype), locktypeNa
d2ee0 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 me(pFile->lockty
d2ef0 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 pe),. lockt
d2f00 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c ypeName(pLock->l
d2f10 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d ocktype), pLock-
d2f20 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 29 29 >cnt , getpid())
d2f30 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
d2f40 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
d2f50 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
d2f60 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
d2f70 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
d2f80 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 unixFile, do not
d2f90 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
d2fa0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 the end_lock: ex
d2fb0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
d2fc0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
d2fd0 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 ) hasn't been ca
d2fe0 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 lled yet.. */.
d2ff0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d3000 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 type>=locktype )
d3010 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 {. OSTRACE3("
d3020 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b LOCK %d %s ok
d3030 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 20 (already held)
d3040 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65 (unix)\n", pFile
d3050 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
d3060 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
d3070 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 cktype));. re
d3080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d3090 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
d30a0 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ure the locking
d30b0 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 sequence is corr
d30c0 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 20 57 ect.. ** (1) W
d30d0 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 6f e never move fro
d30e0 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 6e m unlocked to an
d30f0 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 68 ything higher th
d3100 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 0a an shared lock..
d3110 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 65 ** (2) SQLite
d3120 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 6c never explicitl
d3130 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65 6e y requests a pen
d3140 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 dig lock.. **
d3150 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f 63 (3) A shared loc
d3160 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 k is always held
d3170 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 20 when a reserve
d3180 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 lock is requeste
d3190 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
d31a0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
d31b0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f e!=NO_LOCK || lo
d31c0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d31d0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
d31e0 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 locktype!=PENDI
d31f0 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 NG_LOCK );. ass
d3200 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 ert( locktype!=R
d3210 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 ESERVED_LOCK ||
d3220 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
d3230 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d3240 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 . /* This mutex
d3250 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 is needed becau
d3260 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 se pFile->pLock
d3270 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 is shared across
d3280 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 threads. */.
d3290 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
d32a0 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 ;.. /* Make sur
d32b0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 e the current th
d32c0 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 read owns the pF
d32d0 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d ile.. */. rc =
d32e0 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 transferOwnersh
d32f0 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 ip(pFile);. if(
d3300 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
d3310 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d {. unixLeaveM
d3320 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 utex();. retu
d3330 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f rn rc;. }. pLo
d3340 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ck = pFile->pLoc
d3350 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 k;.. /* If some
d3360 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 thread using th
d3370 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 is PID has a loc
d3380 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e k via a differen
d3390 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a t unixFile*. **
d33a0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 handle that pre
d33b0 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 cludes the reque
d33c0 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 sted lock, retur
d33d0 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 n BUSY.. */. i
d33e0 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 f( (pFile->lockt
d33f0 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ype!=pLock->lock
d3400 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 type && .
d3410 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 (pLock->lockt
d3420 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype>=PENDING_LOC
d3430 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 K || locktype>SH
d3440 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b ARED_LOCK)). ){
d3450 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d3460 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 _BUSY;. goto
d3470 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 end_lock;. }..
d3480 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 /* If a SHARED
d3490 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 lock is requeste
d34a0 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 d, and some thre
d34b0 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 ad using this PI
d34c0 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 D already. ** h
d34d0 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 as a SHARED or R
d34e0 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 ESERVED lock, th
d34f0 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 en increment ref
d3500 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e erence counts an
d3510 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 d. ** return SQ
d3520 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 LITE_OK.. */.
d3530 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
d3540 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 ARED_LOCK && .
d3550 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b (pLock->lock
d3560 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d3570 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b K || pLock->lock
d3580 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
d3590 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 OCK) ){. asse
d35a0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 rt( locktype==SH
d35b0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ARED_LOCK );.
d35c0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
d35d0 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 locktype==0 );.
d35e0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
d35f0 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 ->cnt>0 );. p
d3600 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d3610 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d3620 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a pLock->cnt++;.
d3630 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e pFile->pOpen
d3640 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 ->nLock++;. g
d3650 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
d3660 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 }... /* A PENDI
d3670 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 NG lock is neede
d3680 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 d before acquiri
d3690 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ng a SHARED lock
d36a0 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a and before. **
d36b0 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 acquiring an EX
d36c0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 CLUSIVE lock. F
d36d0 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f or the SHARED lo
d36e0 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 ck, the PENDING
d36f0 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c will. ** be rel
d3700 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f eased.. */. lo
d3710 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 ck.l_len = 1L;.
d3720 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d lock.l_whence =
d3730 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66 28 SEEK_SET;. if(
d3740 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
d3750 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c D_LOCK . ||
d3760 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c (locktype==EXCL
d3770 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 USIVE_LOCK && pF
d3780 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 ile->locktype<PE
d3790 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b NDING_LOCK). ){
d37a0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
d37b0 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 = (locktype==SH
d37c0 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43 ARED_LOCK?F_RDLC
d37d0 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20 K:F_WRLCK);.
d37e0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 lock.l_start = P
d37f0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
d3800 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 s = fcntl(pFile
d3810 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
d3820 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d ock);. if( s=
d3830 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 74 =(-1) ){. t
d3840 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d3850 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d3860 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d3870 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d3880 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
d3890 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
d38a0 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
d38b0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d38c0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d38d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
d38e0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 goto end_lock;.
d38f0 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 }. }... /*
d3900 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 If control gets
d3910 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 to this point, t
d3920 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 hen actually go
d3930 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 ahead and make.
d3940 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ** operating sy
d3950 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 stem calls for t
d3960 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 he specified loc
d3970 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f k.. */. if( lo
d3980 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d3990 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 OCK ){. asser
d39a0 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 t( pLock->cnt==0
d39b0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
d39c0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d pLock->locktype=
d39d0 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f =0 );.. /* No
d39e0 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c w get the read-l
d39f0 6f 63 6b 20 2a 2f 0a 20 20 20 20 73 20 3d 20 72 ock */. s = r
d3a00 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 angeLock(pFile,
d3a10 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e 6f F_RDLCK, &tErrno
d3a20 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 );.. /* Drop
d3a30 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 the temporary PE
d3a40 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 NDING lock */.
d3a50 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d lock.l_start =
d3a60 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 PENDING_BYTE;.
d3a70 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 lock.l_len =
d3a80 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 1L;. lock.l_t
d3a90 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 ype = F_UNLCK;.
d3aa0 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69 if( fcntl(pFi
d3ab0 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
d3ac0 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 &lock)!=0 ){.
d3ad0 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20 29 if( s != -1 )
d3ae0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 {. /* Thi
d3af0 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 s could happen w
d3b00 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f ith a network mo
d3b10 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 unt */. t
d3b20 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a Errno = errno; .
d3b30 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
d3b40 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d3b50 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d3b60 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
d3b70 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 CK); . if
d3b80 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d3b90 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc) ){.
d3ba0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d3bb0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d3bc0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f }. go
d3bd0 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 to end_lock;.
d3be0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
d3bf0 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 f( s==(-1) ){.
d3c00 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d3c10 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d3c20 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d3c30 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 E_IOERR_LOCK);.
d3c40 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d3c50 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d3c60 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d3c70 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d3c80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
d3c90 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d se{. pFile-
d3ca0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 >locktype = SHAR
d3cb0 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 ED_LOCK;. p
d3cc0 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f File->pOpen->nLo
d3cd0 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 ck++;. pLoc
d3ce0 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 k->cnt = 1;.
d3cf0 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f }. }else if( lo
d3d00 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
d3d10 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d E_LOCK && pLock-
d3d20 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a >cnt>1 ){. /*
d3d30 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20 66 We are trying f
d3d40 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 or an exclusive
d3d50 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 lock but another
d3d60 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 0a thread in this.
d3d70 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 ** same proc
d3d80 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c ess is still hol
d3d90 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f ding a shared lo
d3da0 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ck. */. rc =
d3db0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
d3dc0 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
d3dd0 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 request was for
d3de0 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 a RESERVED or E
d3df0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
d3e00 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 It is. ** ass
d3e10 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20 umed that there
d3e20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 is a SHARED or g
d3e30 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 reater lock on t
d3e40 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 he file. ** a
d3e50 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 lready.. */.
d3e60 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 46 assert( 0!=pF
d3e70 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
d3e80 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 . lock.l_type
d3e90 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 = F_WRLCK;.
d3ea0 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 switch( locktype
d3eb0 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 ){. case R
d3ec0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 ESERVED_LOCK:.
d3ed0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
d3ee0 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 rt = RESERVED_BY
d3ef0 54 45 3b 0a 20 20 20 20 20 20 20 20 73 20 3d 20 TE;. s =
d3f00 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d3f10 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b F_SETLK, &lock);
d3f20 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20 . tErrno
d3f30 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
d3f40 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
d3f50 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 se EXCLUSIVE_LOC
d3f60 4b 3a 0a 20 20 20 20 20 20 20 20 73 20 3d 20 72 K:. s = r
d3f70 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 angeLock(pFile,
d3f80 46 5f 57 52 4c 43 4b 2c 20 26 74 45 72 72 6e 6f F_WRLCK, &tErrno
d3f90 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
d3fa0 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a ;. default:
d3fb0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
d3fc0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0);. }. if
d3fd0 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 ( s==(-1) ){.
d3fe0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
d3ff0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d4000 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d4010 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 _IOERR_LOCK);.
d4020 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d4030 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
d4040 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d4050 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d4060 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
d4070 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }. ..#ifndef ND
d4080 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 75 70 EBUG. /* Set up
d4090 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
d40a0 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 20 -counter change
d40b0 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 20 77 checking flags w
d40c0 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 69 74 hen. ** transit
d40d0 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53 48 ioning from a SH
d40e0 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 52 56 ARED to a RESERV
d40f0 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63 68 ED lock. The ch
d4100 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 53 ange. ** from S
d4110 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56 45 HARED to RESERVE
d4120 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67 69 D marks the begi
d4130 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d 61 nning of a norma
d4140 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70 65 l. ** write ope
d4150 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68 6f ration (not a ho
d4160 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t journal rollba
d4170 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ck).. */. if(
d4180 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 rc==SQLITE_OK.
d4190 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 && pFile->lockt
d41a0 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
d41b0 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d . && locktype=
d41c0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 20 =RESERVED_LOCK.
d41d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 74 ){. pFile->t
d41e0 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 30 ransCntrChng = 0
d41f0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 55 ;. pFile->dbU
d4200 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 70 pdate = 0;. p
d4210 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 File->inNormalWr
d4220 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e ite = 1;. }.#en
d4230 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 3d 3d dif... if( rc==
d4240 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
d4250 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d4260 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 = locktype;.
d4270 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d4280 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d = locktype;. }
d4290 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 else if( locktyp
d42a0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
d42b0 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
d42c0 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 locktype = PENDI
d42d0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f NG_LOCK;. pLo
d42e0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 ck->locktype = P
d42f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d ENDING_LOCK;. }
d4300 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e ..end_lock:. un
d4310 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
d4320 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b OSTRACE4("LOCK
d4330 20 20 20 20 25 64 20 25 73 20 25 73 20 28 75 6e %d %s %s (un
d4340 69 78 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ix)\n", pFile->h
d4350 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c , locktypeName(l
d4360 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 ocktype), .
d4370 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f rc==SQLITE_OK ?
d4380 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 "ok" : "failed"
d4390 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
d43a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
d43b0 6c 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ll file descript
d43c0 6f 72 73 20 61 63 63 75 6d 75 61 74 65 64 20 69 ors accumuated i
d43d0 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e n the unixOpenCn
d43e0 74 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e t->pUnused list.
d43f0 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 63 68 20 .** If all such
d4400 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
d4410 20 61 72 65 20 63 6c 6f 73 65 64 20 77 69 74 68 are closed with
d4420 6f 75 74 20 65 72 72 6f 72 2c 20 74 68 65 20 6c out error, the l
d4430 69 73 74 20 69 73 0a 2a 2a 20 63 6c 65 61 72 65 ist is.** cleare
d4440 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 d and SQLITE_OK
d4450 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
d4460 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e Otherwise, if an
d4470 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
d4480 68 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 hen successfully
d4490 20 63 6c 6f 73 65 64 20 66 69 6c 65 20 64 65 73 closed file des
d44a0 63 72 69 70 74 6f 72 0a 2a 2a 20 65 6e 74 72 69 criptor.** entri
d44b0 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66 es are removed f
d44c0 72 6f 6d 20 74 68 65 20 6c 69 73 74 2c 20 61 6e rom the list, an
d44d0 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 d SQLITE_IOERR_C
d44e0 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 20 0a LOSE returned. .
d44f0 2a 2a 20 6e 6f 74 20 64 65 6c 65 74 65 64 20 61 ** not deleted a
d4500 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f nd SQLITE_IOERR_
d4510 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 0a CLOSE returned..
d4520 2a 2f 20 0a 73 74 61 74 69 63 20 69 6e 74 20 63 */ .static int c
d4530 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 losePendingFds(u
d4540 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b nixFile *pFile){
d4550 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d4560 54 45 5f 4f 4b 3b 0a 20 20 73 74 72 75 63 74 20 TE_OK;. struct
d4570 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
d4580 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 en = pFile->pOpe
d4590 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 n;. UnixUnusedF
d45a0 64 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 d *pError = 0;.
d45b0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 UnixUnusedFd *p
d45c0 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 ;. UnixUnusedFd
d45d0 20 2a 70 4e 65 78 74 3b 0a 20 20 66 6f 72 28 70 *pNext;. for(p
d45e0 3d 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b =pOpen->pUnused;
d45f0 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 p; p=pNext){.
d4600 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 pNext = p->pNe
d4610 78 74 3b 0a 20 20 20 20 69 66 28 20 63 6c 6f 73 xt;. if( clos
d4620 65 28 70 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 e(p->fd) ){.
d4630 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d4640 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d4650 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
d4660 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 ERR_CLOSE;.
d4670 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 45 72 72 p->pNext = pErr
d4680 6f 72 3b 0a 20 20 20 20 20 20 70 45 72 72 6f 72 or;. pError
d4690 20 3d 20 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = p;. }else{
d46a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
d46b0 72 65 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 ree(p);. }.
d46c0 7d 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 }. pOpen->pUnus
d46d0 65 64 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 72 ed = pError;. r
d46e0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d46f0 2a 2a 20 41 64 64 20 74 68 65 20 66 69 6c 65 20 ** Add the file
d4700 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 descriptor used
d4710 62 79 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70 by file handle p
d4720 46 69 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72 File to the corr
d4730 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 70 55 6e esponding.** pUn
d4740 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 used list..*/.st
d4750 61 74 69 63 20 76 6f 69 64 20 73 65 74 50 65 6e atic void setPen
d4760 64 69 6e 67 46 64 28 75 6e 69 78 46 69 6c 65 20 dingFd(unixFile
d4770 2a 70 46 69 6c 65 29 7b 0a 20 20 73 74 72 75 63 *pFile){. struc
d4780 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 t unixOpenCnt *p
d4790 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f Open = pFile->pO
d47a0 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 pen;. UnixUnuse
d47b0 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e dFd *p = pFile->
d47c0 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e pUnused;. p->pN
d47d0 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 55 6e ext = pOpen->pUn
d47e0 75 73 65 64 3b 0a 20 20 70 4f 70 65 6e 2d 3e 70 used;. pOpen->p
d47f0 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 20 70 46 Unused = p;. pF
d4800 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 70 ile->h = -1;. p
d4810 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20 File->pUnused =
d4820 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 0;.}../*.** Lowe
d4830 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 r the locking le
d4840 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 vel on file desc
d4850 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 riptor pFile to
d4860 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 locktype. lockt
d4870 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ype.** must be e
d4880 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 ither NO_LOCK or
d4890 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a SHARED_LOCK..**
d48a0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 .** If the locki
d48b0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 ng level of the
d48c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
d48d0 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 is already at or
d48e0 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 below.** the re
d48f0 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 quested locking
d4900 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 level, this rout
d4910 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ine is a no-op..
d4920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
d4930 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 ixUnlock(sqlite3
d4940 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
d4950 6f 63 6b 74 79 70 65 29 7b 0a 20 20 75 6e 69 78 ocktype){. unix
d4960 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d4970 6e 69 78 46 69 6c 65 2a 29 69 64 3b 20 2f 2a 20 nixFile*)id; /*
d4980 54 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f The open file */
d4990 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f . struct unixLo
d49a0 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 ckInfo *pLock;
d49b0 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65 /* Structure
d49c0 20 64 65 73 63 72 69 62 69 6e 67 20 63 75 72 72 describing curr
d49d0 65 6e 74 20 6c 6f 63 6b 20 73 74 61 74 65 20 2a ent lock state *
d49e0 2f 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b /. struct flock
d49f0 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 lock;
d4a00 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
d4a10 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 ion passed into
d4a20 66 63 6e 74 6c 28 29 20 2a 2f 0a 20 20 69 6e 74 fcntl() */. int
d4a30 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d4a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
d4a50 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f Return code fro
d4a60 6d 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 m this interface
d4a70 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 */. int h;
d4a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d4a90 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e /* The un
d4aa0 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 derlying file de
d4ab0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e scriptor */. in
d4ac0 74 20 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 t tErrno;
d4ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d4ae0 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f * Error code fro
d4af0 6d 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 65 72 m system call er
d4b00 72 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 rors */.. asser
d4b10 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
d4b20 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 TRACE7("UNLOCK
d4b30 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c %d %d was %d(%d,
d4b40 25 64 29 20 70 69 64 3d 25 64 20 28 75 6e 69 78 %d) pid=%d (unix
d4b50 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
d4b60 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 locktype,.
d4b70 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c pFile->locktype,
d4b80 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c pFile->pLock->l
d4b90 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
d4ba0 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 pLock->cnt, getp
d4bb0 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 id());.. assert
d4bc0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
d4bd0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 ED_LOCK );. if(
d4be0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d4bf0 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 <=locktype ){.
d4c00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d4c10 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 OK;. }. if( CH
d4c20 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 ECK_THREADID(pFi
d4c30 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 le) ){. retur
d4c40 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
d4c50 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 . }. unixEnter
d4c60 4d 75 74 65 78 28 29 3b 0a 20 20 68 20 3d 20 70 Mutex();. h = p
d4c70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b File->h;. pLock
d4c80 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b = pFile->pLock;
d4c90 0a 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b . assert( pLock
d4ca0 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a 20 20 69 66 ->cnt!=0 );. if
d4cb0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
d4cc0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b e>SHARED_LOCK ){
d4cd0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f . assert( pLo
d4ce0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 ck->locktype==pF
d4cf0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
d4d00 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 . SimulateIOE
d4d10 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 rrorBenign(1);.
d4d20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 SimulateIOErr
d4d30 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 or( h=(-1) ).
d4d40 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4d50 42 65 6e 69 67 6e 28 30 29 3b 0a 0a 23 69 66 6e Benign(0);..#ifn
d4d60 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f def NDEBUG. /
d4d70 2a 20 57 68 65 6e 20 72 65 64 75 63 69 6e 67 20 * When reducing
d4d80 61 20 6c 6f 63 6b 20 73 75 63 68 20 74 68 61 74 a lock such that
d4d90 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 other processes
d4da0 20 63 61 6e 20 73 74 61 72 74 0a 20 20 20 20 2a can start. *
d4db0 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 * reading the da
d4dc0 74 61 62 61 73 65 20 66 69 6c 65 20 61 67 61 69 tabase file agai
d4dd0 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 n, make sure tha
d4de0 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 t the. ** tra
d4df0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
d4e00 20 77 61 73 20 75 70 64 61 74 65 64 20 69 66 20 was updated if
d4e10 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 any part of the
d4e20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 database. **
d4e30 66 69 6c 65 20 63 68 61 6e 67 65 64 2e 20 20 49 file changed. I
d4e40 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
d4e50 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6e 6f 74 n counter is not
d4e60 20 75 70 64 61 74 65 64 2c 0a 20 20 20 20 2a 2a updated,. **
d4e70 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
d4e80 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 66 ns to the same f
d4e90 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74 20 72 65 ile might not re
d4ea0 61 6c 69 7a 65 20 74 68 61 74 0a 20 20 20 20 2a alize that. *
d4eb0 2a 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 * the file has c
d4ec0 68 61 6e 67 65 64 20 61 6e 64 20 68 65 6e 63 65 hanged and hence
d4ed0 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 might not know
d4ee0 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 0a 20 to flush their.
d4ef0 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 20 54 68 ** cache. Th
d4f00 65 20 75 73 65 20 6f 66 20 61 20 73 74 61 6c 65 e use of a stale
d4f10 20 63 61 63 68 65 20 63 61 6e 20 6c 65 61 64 20 cache can lead
d4f20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 to database corr
d4f30 75 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 uption.. */.
d4f40 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
d4f50 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3d ->inNormalWrite=
d4f60 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 =0. || p
d4f70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 3d 3d File->dbUpdate==
d4f80 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 46 0. || pF
d4f90 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 ile->transCntrCh
d4fa0 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20 70 46 69 ng==1 );. pFi
d4fb0 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 le->inNormalWrit
d4fc0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a e = 0;.#endif...
d4fd0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 if( locktype
d4fe0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
d4ff0 0a 20 20 20 20 20 20 69 66 28 20 72 61 6e 67 65 . if( range
d5000 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44 Lock(pFile, F_RD
d5010 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 3d 3d 28 LCK, &tErrno)==(
d5020 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 -1) ){. r
d5030 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d5040 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d5050 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d5060 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 RR_RDLOCK);.
d5070 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d5080 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
d5090 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d50a0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d50b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
d50c0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c goto end_unl
d50d0 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ock;. }.
d50e0 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 }. lock.l_ty
d50f0 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 pe = F_UNLCK;.
d5100 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 lock.l_whence
d5110 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 = SEEK_SET;.
d5120 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 lock.l_start = P
d5130 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
d5140 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c lock.l_len = 2L
d5150 3b 20 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 ; assert( PENDI
d5160 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 NG_BYTE+1==RESER
d5170 56 45 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 VED_BYTE );.
d5180 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 if( fcntl(h, F_S
d5190 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d ETLK, &lock)!=(-
d51a0 31 29 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 1) ){. pLoc
d51b0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 k->locktype = SH
d51c0 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d ARED_LOCK;. }
d51d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 74 45 72 72 else{. tErr
d51e0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d51f0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d5200 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d5210 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d5220 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
d5230 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d5240 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d5250 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d5260 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d5270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 . }. g
d5280 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a oto end_unlock;.
d5290 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
d52a0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
d52b0 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 K ){. struct
d52c0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
d52d0 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 en;.. /* Decr
d52e0 65 6d 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 ement the shared
d52f0 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 lock counter.
d5300 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b Release the lock
d5310 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a using an. **
d5320 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 OS call only wh
d5330 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 en all threads i
d5340 6e 20 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 n this same proc
d5350 65 73 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 ess have release
d5360 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 d. ** the loc
d5370 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c k.. */. pL
d5380 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 ock->cnt--;.
d5390 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d if( pLock->cnt==
d53a0 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 0 ){. lock.
d53b0 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b l_type = F_UNLCK
d53c0 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 ;. lock.l_w
d53d0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
d53e0 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 ;. lock.l_s
d53f0 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 tart = lock.l_le
d5400 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 53 69 n = 0L;. Si
d5410 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
d5420 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20 53 69 ign(1);. Si
d5430 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 mulateIOError( h
d5440 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 20 53 69 =(-1) ). Si
d5450 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
d5460 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 69 66 ign(0);. if
d5470 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 ( fcntl(h, F_SET
d5480 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 LK, &lock)!=(-1)
d5490 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 ){. pLoc
d54a0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f k->locktype = NO
d54b0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c _LOCK;. }el
d54c0 73 65 7b 0a 20 20 20 20 20 20 20 20 74 45 72 72 se{. tErr
d54d0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
d54e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d54f0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d5500 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d5510 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d5520 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f . if( IS_
d5530 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d5540 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c {. pFil
d5550 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d5560 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d Errno;. }
d5570 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e . pLock->
d5580 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
d5590 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c CK;. pFil
d55a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f e->locktype = NO
d55b0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 _LOCK;. }.
d55c0 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 }.. /* Dec
d55d0 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 rement the count
d55e0 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 of locks agains
d55f0 74 20 74 68 69 73 20 73 61 6d 65 20 66 69 6c 65 t this same file
d5600 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 20 20 20 . When the.
d5610 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 ** count reaches
d5620 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 zero, close any
d5630 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 other file desc
d5640 72 69 70 74 6f 72 73 20 77 68 6f 73 65 20 63 6c riptors whose cl
d5650 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 ose. ** was d
d5660 65 66 65 72 72 65 64 20 62 65 63 61 75 73 65 20 eferred because
d5670 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c of outstanding l
d5680 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ocks.. */.
d5690 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e pOpen = pFile->
d56a0 70 4f 70 65 6e 3b 0a 20 20 20 20 70 4f 70 65 6e pOpen;. pOpen
d56b0 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 ->nLock--;. a
d56c0 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c ssert( pOpen->nL
d56d0 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 ock>=0 );. if
d56e0 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d ( pOpen->nLock==
d56f0 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 0 ){. int r
d5700 63 32 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e c2 = closePendin
d5710 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 gFds(pFile);.
d5720 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
d5730 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
d5740 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 rc = rc2;.
d5750 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e }. }. }...en
d5760 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 d_unlock:. unix
d5770 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
d5780 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
d5790 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 K ) pFile->lockt
d57a0 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d57b0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
d57c0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
d57d0 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 65 ion performs the
d57e0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 22 63 parts of the "c
d57f0 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 65 72 61 lose file" opera
d5800 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 tion .** common
d5810 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 to all locking s
d5820 63 68 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65 chemes. It close
d5830 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 s the directory
d5840 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 and file.** hand
d5850 6c 65 73 2c 20 69 66 20 74 68 65 79 20 61 72 65 les, if they are
d5860 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 73 valid, and sets
d5870 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 all fields of t
d5880 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 he unixFile.** s
d5890 74 72 75 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a tructure to 0..*
d58a0 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a *.** It is *not*
d58b0 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 68 6f necessary to ho
d58c0 6c 64 20 74 68 65 20 6d 75 74 65 78 20 77 68 65 ld the mutex whe
d58d0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
d58e0 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 s called,.** eve
d58f0 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41 n on VxWorks. A
d5900 20 6d 75 74 65 78 20 77 69 6c 6c 20 62 65 20 61 mutex will be a
d5910 63 71 75 69 72 65 64 20 6f 6e 20 56 78 57 6f 72 cquired on VxWor
d5920 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20 76 78 77 ks by the.** vxw
d5930 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 orksReleaseFileI
d5940 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a d() routine..*/.
d5950 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 static int close
d5960 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 74 65 33 UnixFile(sqlite3
d5970 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e _file *id){. un
d5980 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d5990 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
d59a0 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 if( pFile ){.
d59b0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 if( pFile->dir
d59c0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 fd>=0 ){. i
d59d0 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 nt err = close(p
d59e0 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 0a 20 20 File->dirfd);.
d59f0 20 20 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 if( err ){.
d5a00 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d5a10 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
d5a20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
d5a30 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 SQLITE_IOERR_DIR
d5a40 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 65 _CLOSE;. }e
d5a50 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 69 lse{. pFi
d5a60 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 3b 0a 20 20 le->dirfd=-1;.
d5a70 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d5a80 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 if( pFile->h>=0
d5a90 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 ){. int err
d5aa0 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e = close(pFile->
d5ab0 68 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 h);. if( er
d5ac0 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 r ){. pFi
d5ad0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d5ae0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 errno;. r
d5af0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d5b00 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 RR_CLOSE;.
d5b10 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4f 53 5f 56 }. }.#if OS_V
d5b20 58 57 4f 52 4b 53 0a 20 20 20 20 69 66 28 20 70 XWORKS. if( p
d5b30 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 File->pId ){.
d5b40 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 69 73 if( pFile->is
d5b50 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 Delete ){.
d5b60 20 20 75 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e unlink(pFile->
d5b70 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e pId->zCanonicalN
d5b80 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ame);. }.
d5b90 20 20 20 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 vxworksRelea
d5ba0 73 65 46 69 6c 65 49 64 28 70 46 69 6c 65 2d 3e seFileId(pFile->
d5bb0 70 49 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c pId);. pFil
d5bc0 65 2d 3e 70 49 64 20 3d 20 30 3b 0a 20 20 20 20 e->pId = 0;.
d5bd0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 }.#endif. OST
d5be0 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25 RACE2("CLOSE %
d5bf0 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 -3d\n", pFile->h
d5c00 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 );. OpenCount
d5c10 65 72 28 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 er(-1);. sqli
d5c20 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e te3_free(pFile->
d5c30 70 55 6e 75 73 65 64 29 3b 0a 20 20 20 20 6d 65 pUnused);. me
d5c40 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 mset(pFile, 0, s
d5c50 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 izeof(unixFile))
d5c60 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
d5c70 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
d5c80 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e ** Close a file.
d5c90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
d5ca0 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 nixClose(sqlite3
d5cb0 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e _file *id){. in
d5cc0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d5cd0 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 ;. if( id ){.
d5ce0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d5cf0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 e = (unixFile *)
d5d00 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 6e 6c 6f id;. unixUnlo
d5d10 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b ck(id, NO_LOCK);
d5d20 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 . unixEnterMu
d5d30 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 tex();. if( p
d5d40 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 File->pOpen && p
d5d50 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f File->pOpen->nLo
d5d60 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 ck ){. /* I
d5d70 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 f there are outs
d5d80 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 tanding locks, d
d5d90 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 o not actually c
d5da0 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 lose the file ju
d5db0 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 st. ** yet
d5dc0 62 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 because that wou
d5dd0 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c ld clear those l
d5de0 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 ocks. Instead,
d5df0 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 add the file.
d5e00 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 ** descriptor
d5e10 20 74 6f 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 to pOpen->pUnus
d5e20 65 64 20 6c 69 73 74 2e 20 20 49 74 20 77 69 6c ed list. It wil
d5e30 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c l be automatical
d5e40 6c 79 20 63 6c 6f 73 65 64 20 0a 20 20 20 20 20 ly closed .
d5e50 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 ** when the las
d5e60 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 t lock is cleare
d5e70 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
d5e80 20 20 73 65 74 50 65 6e 64 69 6e 67 46 64 28 70 setPendingFd(p
d5e90 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 File);. }.
d5ea0 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f releaseLockInfo
d5eb0 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a (pFile->pLock);.
d5ec0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 releaseOpenC
d5ed0 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 nt(pFile->pOpen)
d5ee0 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 ;. rc = close
d5ef0 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 UnixFile(id);.
d5f00 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
d5f10 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ();. }. return
d5f20 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a rc;.}../*******
d5f30 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 ******* End of t
d5f40 68 65 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72 he posix advisor
d5f50 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 y lock implement
d5f60 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
d5f70 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d5f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5fc0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a ******/../******
d5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6010 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
d6020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6030 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f ******* No-op Lo
d6040 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a cking **********
d6050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6060 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f ********.**.** O
d6070 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 6f f the various lo
d6080 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 cking implementa
d6090 74 69 6f 6e 73 20 61 76 61 69 6c 61 62 6c 65 2c tions available,
d60a0 20 74 68 69 73 20 69 73 20 62 79 20 66 61 72 20 this is by far
d60b0 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 73 74 3a the.** simplest:
d60c0 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 69 67 6e locking is ign
d60d0 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74 65 6d 70 ored. No attemp
d60e0 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6c 6f 63 t is made to loc
d60f0 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a k the database.*
d6100 2a 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69 * file for readi
d6110 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a ng or writing..*
d6120 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63 6b 69 6e *.** This lockin
d6130 67 20 6d 6f 64 65 20 69 73 20 61 70 70 72 6f 70 g mode is approp
d6140 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e riate for use on
d6150 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 read-only datab
d6160 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20 64 61 74 ases.** (ex: dat
d6170 61 62 61 73 65 73 20 74 68 61 74 20 61 72 65 20 abases that are
d6180 62 75 72 6e 65 64 20 69 6e 74 6f 20 43 44 2d 52 burned into CD-R
d6190 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e OM, for example.
d61a0 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 ) It can.** als
d61b0 6f 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 o be used if the
d61c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 65 6d 70 application emp
d61d0 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74 65 72 6e loys some extern
d61e0 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20 74 6f 0a al mechanism to.
d61f0 2a 2a 20 70 72 65 76 65 6e 74 20 73 69 6d 75 6c ** prevent simul
d6200 74 61 6e 65 6f 75 73 20 61 63 63 65 73 73 20 6f taneous access o
d6210 66 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 f the same datab
d6220 61 73 65 20 62 79 20 74 77 6f 20 6f 72 20 6d 6f ase by two or mo
d6230 72 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 re.** database c
d6240 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 onnections. But
d6250 20 74 68 65 72 65 20 69 73 20 61 20 73 65 72 69 there is a seri
d6260 6f 75 73 20 72 69 73 6b 20 6f 66 20 64 61 74 61 ous risk of data
d6270 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 base.** corrupti
d6280 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f 63 6b 69 on if this locki
d6290 6e 67 20 6d 6f 64 65 20 69 73 20 75 73 65 64 20 ng mode is used
d62a0 69 6e 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 in situations wh
d62b0 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 ere multiple.**
d62c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
d62d0 69 6f 6e 73 20 61 72 65 20 61 63 63 65 73 73 69 ions are accessi
d62e0 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ng the same data
d62f0 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 base file at the
d6300 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65 20 61 6e same.** time an
d6310 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 d one or more of
d6320 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f those connectio
d6330 6e 73 20 61 72 65 20 77 72 69 74 69 6e 67 2e 0a ns are writing..
d6340 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e */..static int n
d6350 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 olockCheckReserv
d6360 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
d6370 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e ile *NotUsed, in
d6380 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 55 t *pResOut){. U
d6390 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
d63a0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a 70 52 65 NotUsed);. *pRe
d63b0 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 sOut = 0;. retu
d63c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
d63d0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 static int noloc
d63e0 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 kLock(sqlite3_fi
d63f0 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 le *NotUsed, int
d6400 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e NotUsed2){. UN
d6410 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 USED_PARAMETER2(
d6420 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 NotUsed, NotUsed
d6430 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 2);. return SQL
d6440 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 ITE_OK;.}.static
d6450 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 int nolockUnloc
d6460 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d6470 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 NotUsed, int Not
d6480 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 Used2){. UNUSED
d6490 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
d64a0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
d64b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d64c0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f OK;.}../*.** Clo
d64d0 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a se the file..*/.
d64e0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 static int noloc
d64f0 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 kClose(sqlite3_f
d6500 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 72 65 74 ile *id) {. ret
d6510 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c urn closeUnixFil
d6520 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a e(id);.}../*****
d6530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
d6540 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d 6f 70 20 nd of the no-op
d6550 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d6560 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ion ************
d6570 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
d6580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a ********/../****
d65d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d65f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d6620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6630 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f 74 2d 66 **** Begin dot-f
d6640 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a ile Locking ****
d6650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
d6670 20 54 68 65 20 64 6f 74 66 69 6c 65 20 6c 6f 63 The dotfile loc
d6680 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 king implementat
d6690 69 6f 6e 20 75 73 65 73 20 74 68 65 20 65 78 69 ion uses the exi
d66a0 73 74 61 6e 63 65 20 6f 66 20 73 65 70 61 72 61 stance of separa
d66b0 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 te lock.** files
d66c0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e in order to con
d66d0 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 trol access to t
d66e0 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
d66f0 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 is works on just
d6700 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65 72 79 20 .** about every
d6710 66 69 6c 65 73 79 73 74 65 6d 20 69 6d 61 67 69 filesystem imagi
d6720 6e 61 62 6c 65 2e 20 20 42 75 74 20 74 68 65 72 nable. But ther
d6730 65 20 61 72 65 20 73 65 72 69 6f 75 73 20 64 6f e are serious do
d6740 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 wnsides:.**.**
d6750 20 20 28 31 29 20 20 54 68 65 72 65 20 69 73 20 (1) There is
d6760 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79 zero concurrency
d6770 2e 20 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64 . A single read
d6780 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 er blocks all ot
d6790 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 her.** c
d67a0 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 onnections from
d67b0 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
d67c0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ng the database.
d67d0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 41 .**.** (2) A
d67e0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 72 n application cr
d67f0 61 73 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73 ash or power los
d6800 73 20 63 61 6e 20 6c 65 61 76 65 20 73 74 61 6c s can leave stal
d6810 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 e lock files.**
d6820 20 20 20 20 20 20 20 20 73 69 74 74 69 6e 67 20 sitting
d6830 61 72 6f 75 6e 64 20 74 68 61 74 20 6e 65 65 64 around that need
d6840 20 74 6f 20 62 65 20 63 6c 65 61 72 65 64 20 6d to be cleared m
d6850 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e anually..**.** N
d6860 65 76 65 72 74 68 65 6c 65 73 73 2c 20 61 20 64 evertheless, a d
d6870 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20 61 70 70 otlock is an app
d6880 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 ropriate locking
d6890 20 6d 6f 64 65 20 66 6f 72 20 75 73 65 20 69 66 mode for use if
d68a0 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 no.** other loc
d68b0 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 69 73 king strategy is
d68c0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a available..**.*
d68d0 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e * Dotfile lockin
d68e0 67 20 77 6f 72 6b 73 20 62 79 20 63 72 65 61 74 g works by creat
d68f0 69 6e 67 20 61 20 66 69 6c 65 20 69 6e 20 74 68 ing a file in th
d6900 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 e same directory
d6910 20 61 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 as the.** datab
d6920 61 73 65 20 61 6e 64 20 77 69 74 68 20 74 68 65 ase and with the
d6930 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 same name but w
d6940 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22 20 65 78 ith a ".lock" ex
d6950 74 65 6e 73 69 6f 6e 20 61 64 64 65 64 2e 0a 2a tension added..*
d6960 2a 20 54 68 65 20 65 78 69 73 74 61 6e 63 65 20 * The existance
d6970 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c 65 20 69 of a lock file i
d6980 6d 70 6c 69 65 73 20 61 6e 20 45 58 43 4c 55 53 mplies an EXCLUS
d6990 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f IVE lock. All o
d69a0 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 ther lock.** typ
d69b0 65 73 20 28 53 48 41 52 45 44 2c 20 52 45 53 45 es (SHARED, RESE
d69c0 52 56 45 44 2c 20 50 45 4e 44 49 4e 47 29 20 61 RVED, PENDING) a
d69d0 72 65 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 45 re mapped into E
d69e0 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a XCLUSIVE..*/../*
d69f0 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 73 75 66 .** The file suf
d6a00 66 69 78 20 61 64 64 65 64 20 74 6f 20 74 68 65 fix added to the
d6a10 20 64 61 74 61 20 62 61 73 65 20 66 69 6c 65 6e data base filen
d6a20 61 6d 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ame in order to
d6a30 63 72 65 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f create the.** lo
d6a40 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 ck file..*/.#def
d6a50 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 ine DOTLOCK_SUFF
d6a60 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a IX ".lock"../*.*
d6a70 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
d6a80 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 hecks if there i
d6a90 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
d6aa0 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 k held on the sp
d6ab0 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 ecified.** file
d6ac0 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
d6ad0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 ther process. If
d6ae0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 such a lock is
d6af0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f held, set *pResO
d6b00 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a ut.** to a non-z
d6b10 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 ero value otherw
d6b20 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 ise *pResOut is
d6b30 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 set to zero. Th
d6b40 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
d6b50 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
d6b60 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 TE_OK unless an
d6b70 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 I/O error occurs
d6b80 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 during lock che
d6b90 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 cking..**.** In
d6ba0 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c dotfile locking,
d6bb0 20 65 69 74 68 65 72 20 61 20 6c 6f 63 6b 20 65 either a lock e
d6bc0 78 69 73 74 73 20 6f 72 20 69 74 20 64 6f 65 73 xists or it does
d6bd0 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 74 68 69 not. So in thi
d6be0 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f s.** variation o
d6bf0 66 20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c f CheckReservedL
d6c00 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f 75 74 20 ock(), *pResOut
d6c10 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 is set to true i
d6c20 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 f any lock.** is
d6c30 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c held on the fil
d6c40 65 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74 e and false if t
d6c50 68 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 he file is unloc
d6c60 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ked..*/.static i
d6c70 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 nt dotlockCheckR
d6c80 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 eservedLock(sqli
d6c90 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
d6ca0 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 t *pResOut) {.
d6cb0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
d6cc0 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 OK;. int reserv
d6cd0 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 ed = 0;. unixFi
d6ce0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d6cf0 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 xFile*)id;.. Si
d6d00 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
d6d10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d6d20 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
d6d30 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 LOCK; );. . as
d6d40 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a sert( pFile );..
d6d50 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 /* Check if a
d6d60 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 thread in this p
d6d70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 rocess holds suc
d6d80 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 h a lock */. if
d6d90 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
d6da0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b e>SHARED_LOCK ){
d6db0 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 . /* Either t
d6dc0 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f his connection o
d6dd0 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e r some other con
d6de0 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 nection in the s
d6df0 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 20 20 20 ame process.
d6e00 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 ** holds a lock
d6e10 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 4e 6f on the file. No
d6e20 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66 need to check f
d6e30 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 72 urther. */. r
d6e40 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d eserved = 1;. }
d6e50 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
d6e60 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 69 66 lock is held if
d6e70 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 and only if the
d6e80 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 73 74 73 lockfile exists
d6e90 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 */. const ch
d6ea0 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 ar *zLockFile =
d6eb0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 46 69 (const char*)pFi
d6ec0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
d6ed0 78 74 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 xt;. reserved
d6ee0 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f 63 6b 46 = access(zLockF
d6ef0 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a ile, 0)==0;. }.
d6f00 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 OSTRACE4("TEST
d6f10 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 WR-LOCK %d %d %
d6f20 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e 22 2c 20 d (dotlock)\n",
d6f30 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 pFile->h, rc, re
d6f40 73 65 72 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 served);. *pRes
d6f50 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a Out = reserved;.
d6f60 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
d6f70 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 /*.** Lock the f
d6f80 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 ile with the loc
d6f90 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 k specified by p
d6fa0 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 arameter locktyp
d6fb0 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 e - one.** of th
d6fc0 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
d6fd0 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 ** (1) SHARE
d6fe0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 D_LOCK.** (2
d6ff0 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ) RESERVED_LOCK.
d7000 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 ** (3) PENDI
d7010 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 NG_LOCK.** (
d7020 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4) EXCLUSIVE_LOC
d7030 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 K.**.** Sometime
d7040 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e s when requestin
d7050 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 g one lock state
d7060 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 , additional loc
d7070 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 k states.** are
d7080 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 inserted in betw
d7090 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e een. The lockin
d70a0 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 g might fail on
d70b0 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 one of the later
d70c0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d70d0 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b leaving the lock
d70e0 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 state different
d70f0 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 from what it st
d7100 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 arted but.** sti
d7110 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 ll short of its
d7120 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f goal. The follo
d7130 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 wing chart shows
d7140 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 the allowed.**
d7150 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 transitions and
d7160 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 the inserted int
d7170 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 ermediate states
d7180 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 :.**.** UNLOC
d7190 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a KED -> SHARED.**
d71a0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 SHARED -> RE
d71b0 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 SERVED.** SHA
d71c0 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 RED -> (PENDING)
d71d0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
d71e0 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 RESERVED ->
d71f0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
d7200 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e LUSIVE.** PEN
d7210 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 DING -> EXCLUSIV
d7220 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 E.**.** This rou
d7230 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 tine will only i
d7240 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 ncrease a lock.
d7250 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 Use the sqlite3
d7260 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f OsUnlock().** ro
d7270 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 utine to lower a
d7280 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a locking level..
d7290 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 **.** With dotfi
d72a0 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 le locking, we r
d72b0 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f eally only suppo
d72c0 72 74 20 73 74 61 74 65 20 28 34 29 3a 20 45 58 rt state (4): EX
d72d0 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 CLUSIVE..** But
d72e0 77 65 20 74 72 61 63 6b 20 74 68 65 20 6f 74 68 we track the oth
d72f0 65 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c er locking level
d7300 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f s internally..*/
d7310 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
d7320 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f ockLock(sqlite3_
d7330 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
d7340 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 cktype) {. unix
d7350 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d7360 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 nixFile*)id;. i
d7370 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a nt fd;. char *z
d7380 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 LockFile = (char
d7390 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
d73a0 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 gContext;. int
d73b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d73c0 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 .. /* If we hav
d73d0 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e e any lock, then
d73e0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 the lock file a
d73f0 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 20 lready exists.
d7400 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 2a 2a All we have. **
d7410 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 73 74 to do is adjust
d7420 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 our internal re
d7430 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b cord of the lock
d7440 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 level.. */. i
d7450 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d7460 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a pe > NO_LOCK ){.
d7470 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d7480 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d7490 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a #if !OS_VXWORKS.
d74a0 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 /* Always up
d74b0 64 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61 date the timesta
d74c0 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 mp on the old fi
d74d0 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 le */. utimes
d74e0 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c (zLockFile, NULL
d74f0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 );.#endif. re
d7500 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d7510 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 }. . /* grab
d7520 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
d7530 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 ck */. fd = ope
d7540 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 n(zLockFile,O_RD
d7550 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 ONLY|O_CREAT|O_E
d7560 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 XCL,0600);. if(
d7570 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 fd<0 ){. /*
d7580 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 failed to open/c
d7590 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 reate the file,
d75a0 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 someone else may
d75b0 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 have stolen the
d75c0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 lock */. int
d75d0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d75e0 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 54 20 . if( EEXIST
d75f0 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 == tErrno ){.
d7600 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
d7610 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 USY;. } else
d7620 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
d7630 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d7640 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d7650 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
d7660 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f );. if( IS_
d7670 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d7680 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d7690 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d76a0 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rno;. }.
d76b0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
d76c0 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f ;. } . if( clo
d76d0 73 65 28 66 64 29 20 29 7b 0a 20 20 20 20 70 46 se(fd) ){. pF
d76e0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d76f0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d errno;. rc =
d7700 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c SQLITE_IOERR_CL
d7710 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a OSE;. }. . /*
d7720 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 got it, set the
d7730 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e type and return
d7740 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e ok */. pFile->
d7750 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
d7760 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ype;. return rc
d7770 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 ;.}../*.** Lower
d7780 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
d7790 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 el on file descr
d77a0 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c iptor pFile to l
d77b0 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 ocktype. lockty
d77c0 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 pe.** must be ei
d77d0 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 ther NO_LOCK or
d77e0 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a SHARED_LOCK..**.
d77f0 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e ** If the lockin
d7800 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 g level of the f
d7810 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 ile descriptor i
d7820 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 s already at or
d7830 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 below.** the req
d7840 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c uested locking l
d7850 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 evel, this routi
d7860 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
d7870 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f *.** When the lo
d7880 63 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 cking level reac
d7890 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c hes NO_LOCK, del
d78a0 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c ete the lock fil
d78b0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
d78c0 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 dotlockUnlock(s
d78d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d78e0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
d78f0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d7900 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d7910 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 id;. char *zLoc
d7920 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 kFile = (char *)
d7930 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d7940 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 ntext;.. assert
d7950 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 ( pFile );. OST
d7960 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 RACE5("UNLOCK %
d7970 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d d %d was %d pid=
d7980 25 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e 22 2c %d (dotlock)\n",
d7990 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
d79a0 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e ype,.. pFile->
d79b0 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 locktype, getpid
d79c0 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c ());. assert( l
d79d0 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f ocktype<=SHARED_
d79e0 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 LOCK );. . /*
d79f0 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c no-op if possibl
d7a00 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 e */. if( pFile
d7a10 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b ->locktype==lock
d7a20 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 type ){. retu
d7a30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d7a40 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 }.. /* To downg
d7a50 72 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 rade to shared,
d7a60 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 simply update ou
d7a70 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f r internal notio
d7a80 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f n of the. ** lo
d7a90 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 ck state. No ne
d7aa0 65 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 ed to mess with
d7ab0 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b the file on disk
d7ac0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 .. */. if( loc
d7ad0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d7ae0 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK ){. pFile-
d7af0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 >locktype = SHAR
d7b00 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 ED_LOCK;. ret
d7b10 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
d7b20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 }. . /* To fu
d7b30 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 lly unlock the d
d7b40 61 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 atabase, delete
d7b50 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f the lock file */
d7b60 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d7b70 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a ype==NO_LOCK );.
d7b80 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f if( unlink(zLo
d7b90 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 ckFile) ){. i
d7ba0 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 69 nt rc = 0;. i
d7bb0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d7bc0 6f 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e o;. if( ENOEN
d7bd0 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 T != tErrno ){.
d7be0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d7bf0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d7c00 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d7c10 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
d7c20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
d7c30 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d7c40 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ) ){. pFile
d7c50 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d7c60 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rrno;. }.
d7c70 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a return rc; . }.
d7c80 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d7c90 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 e = NO_LOCK;. r
d7ca0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d7cb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
d7cc0 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75 a file. Make su
d7cd0 72 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 re the lock has
d7ce0 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65 been released be
d7cf0 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f fore closing..*/
d7d00 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c .static int dotl
d7d10 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 ockClose(sqlite3
d7d20 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 _file *id) {. i
d7d30 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69 64 20 nt rc;. if( id
d7d40 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 ){. unixFile
d7d50 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d7d60 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c le*)id;. dotl
d7d70 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f ockUnlock(id, NO
d7d80 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69 _LOCK);. sqli
d7d90 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e te3_free(pFile->
d7da0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
d7db0 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c 6f 73 . }. rc = clos
d7dc0 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 eUnixFile(id);.
d7dd0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a return rc;.}./*
d7de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7df0 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 * End of the dot
d7e00 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 -file lock imple
d7e10 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a mentation ******
d7e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d7e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
d7e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d7ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
d7ef0 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a flock Locking **
d7f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d7f20 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f *.** Use the flo
d7f30 63 6b 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c ck() system call
d7f40 20 74 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b to do file lock
d7f50 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b ing..**.** flock
d7f60 28 29 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 () locking is li
d7f70 6b 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b ke dot-file lock
d7f80 69 6e 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 ing in that the
d7f90 76 61 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d various.** fine-
d7fa0 67 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 grain locking le
d7fb0 76 65 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 vels supported b
d7fc0 79 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c y SQLite are col
d7fd0 6c 61 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 lapsed into.** a
d7fe0 20 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 single exclusiv
d7ff0 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 e lock. In othe
d8000 72 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c r words, SHARED,
d8010 20 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a RESERVED, and.*
d8020 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 * PENDING locks
d8030 61 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 are the same thi
d8040 6e 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 ng as an EXCLUSI
d8050 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 VE lock. SQLite
d8060 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 .** still works
d8070 77 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 when you do this
d8080 2c 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 , but concurrenc
d8090 79 20 69 73 20 72 65 64 75 63 65 64 20 73 69 6e y is reduced sin
d80a0 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e ce.** only a sin
d80b0 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 gle process can
d80c0 62 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 be reading the d
d80d0 61 74 61 62 61 73 65 20 61 74 20 61 20 74 69 6d atabase at a tim
d80e0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 e..**.** Omit th
d80f0 69 73 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 is section if SQ
d8100 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d8110 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 ING_STYLE is tur
d8120 6e 65 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a ned off or if.**
d8130 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 compiling for V
d8140 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 XWORKS..*/.#if S
d8150 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
d8160 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f KING_STYLE && !O
d8170 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a S_VXWORKS../*.**
d8180 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
d8190 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
d81a0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
d81b0 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
d81c0 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
d81d0 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
d81e0 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
d81f0 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
d8200 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 eld, set *pResOu
d8210 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 t.** to a non-ze
d8220 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 ro value otherwi
d8230 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 se *pResOut is s
d8240 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 et to zero. The
d8250 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
d8260 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 is set to SQLIT
d8270 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 E_OK unless an I
d8280 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 /O error occurs
d8290 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 during lock chec
d82a0 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 king..*/.static
d82b0 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 int flockCheckRe
d82c0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
d82d0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d82e0 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e *pResOut){. in
d82f0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d8300 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 ;. int reserved
d8310 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 = 0;. unixFile
d8320 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d8330 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 ile*)id;. . Si
d8340 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
d8350 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
d8360 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 RR_CHECKRESERVED
d8370 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 LOCK; );. . as
d8380 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
d8390 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 . /* Check if
d83a0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
d83b0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
d83c0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
d83d0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d83e0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
d83f0 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
d8400 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a = 1;. }. . /*
d8410 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
d8420 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
d8430 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a cess holds it. *
d8440 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 /. if( !reserve
d8450 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 d ){. /* atte
d8460 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c mpt to get the l
d8470 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c ock */. int l
d8480 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 rc = flock(pFile
d8490 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c ->h, LOCK_EX | L
d84a0 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28 OCK_NB);. if(
d84b0 20 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f !lrc ){. /
d84c0 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 * got the lock,
d84d0 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 unlock it */.
d84e0 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 lrc = flock(p
d84f0 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e File->h, LOCK_UN
d8500 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72 );. if ( lr
d8510 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e c ) {. in
d8520 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
d8530 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c ;. /* unl
d8540 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20 ock failed with
d8550 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 an error */.
d8560 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 lrc = sqlite
d8570 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d8580 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d8590 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
d85a0 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 ; . if( I
d85b0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 S_LOCK_ERROR(lrc
d85c0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
d85d0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d85e0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d85f0 20 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 rc = lrc;.
d8600 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
d8610 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
d8620 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 int tErrno =
d8630 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 errno;. res
d8640 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 erved = 1;.
d8650 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 /* someone else
d8660 20 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72 might have it r
d8670 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 eserved */.
d8680 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 lrc = sqliteErr
d8690 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d86a0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d86b0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 IOERR_LOCK); .
d86c0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d86d0 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 ERROR(lrc) ){.
d86e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d86f0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d8700 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 . rc = lr
d8710 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
d8720 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 . }. OSTRACE4(
d8730 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
d8740 20 25 64 20 25 64 20 28 66 6c 6f 63 6b 29 5c 6e %d %d (flock)\n
d8750 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
d8760 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 69 66 reserved);..#if
d8770 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 def SQLITE_IGNOR
d8780 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 E_FLOCK_LOCK_ERR
d8790 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 ORS. if( (rc &
d87a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d SQLITE_IOERR) ==
d87b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b SQLITE_IOERR ){
d87c0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d87d0 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65 _OK;. reserve
d87e0 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 d=1;. }.#endif
d87f0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 /* SQLITE_IGNORE
d8800 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d8810 52 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 RS */. *pResOut
d8820 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 = reserved;. r
d8830 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d8840 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
d8850 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
d8860 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
d8870 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
d8880 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
d8890 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
d88a0 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
d88b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
d88c0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
d88d0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
d88e0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
d88f0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
d8900 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
d8910 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
d8920 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
d8930 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
d8940 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
d8950 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
d8960 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
d8970 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
d8980 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
d8990 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
d89a0 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
d89b0 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
d89c0 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
d89d0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
d89e0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
d89f0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
d8a00 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
d8a10 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
d8a20 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
d8a30 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
d8a40 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
d8a50 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
d8a60 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
d8a70 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
d8a80 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
d8a90 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d8aa0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d8ab0 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
d8ac0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
d8ad0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
d8ae0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
d8af0 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c *.** flock() onl
d8b00 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 y really support
d8b10 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 EXCLUSIVE locks
d8b20 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 . We track inte
d8b30 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b rmediate.** lock
d8b40 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 states in the s
d8b50 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 qlite3_file stru
d8b60 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c cture, but all l
d8b70 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a ocks SHARED or.*
d8b80 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c * above are real
d8b90 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 ly EXCLUSIVE loc
d8ba0 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 ks and exclude a
d8bb0 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ll other process
d8bc0 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 es from.** acces
d8bd0 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a s the file..**.*
d8be0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
d8bf0 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
d8c00 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 e a lock. Use t
d8c10 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f he sqlite3OsUnlo
d8c20 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ck().** routine
d8c30 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 to lower a locki
d8c40 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 ng level..*/.sta
d8c50 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 tic int flockLoc
d8c60 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d8c70 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d8c80 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
d8c90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 QLITE_OK;. unix
d8ca0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d8cb0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 nixFile*)id;..
d8cc0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d8cd0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 .. /* if we alr
d8ce0 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b eady have a lock
d8cf0 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 , it is exclusiv
d8d00 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 e. . ** Just a
d8d10 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 djust level and
d8d20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 punt on outta he
d8d30 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 re. */. if (pFi
d8d40 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e le->locktype > N
d8d50 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 O_LOCK) {. pF
d8d60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d8d70 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 locktype;. re
d8d80 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d8d90 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 }. . /* grab
d8da0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
d8db0 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 ck */. . if (f
d8dc0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
d8dd0 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 OCK_EX | LOCK_NB
d8de0 29 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 )) {. int tEr
d8df0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d8e00 20 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 /* didn't get,
d8e10 6d 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a must be busy */.
d8e20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d8e30 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d8e40 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d8e50 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 E_IOERR_LOCK);.
d8e60 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d8e70 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d8e80 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d8e90 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d8ea0 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 }. } else {.
d8eb0 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 /* got it, set
d8ec0 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 the type and re
d8ed0 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 turn ok */. p
d8ee0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d8ef0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 locktype;. }.
d8f00 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 OSTRACE4("LOCK
d8f10 20 20 20 25 64 20 25 73 20 25 73 20 28 66 6c 6f %d %s %s (flo
d8f20 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ck)\n", pFile->h
d8f30 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c , locktypeName(l
d8f40 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 ocktype), .
d8f50 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 rc==SQLITE
d8f60 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 _OK ? "ok" : "fa
d8f70 69 6c 65 64 22 29 3b 0a 23 69 66 64 65 66 20 53 iled");.#ifdef S
d8f80 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
d8f90 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 CK_LOCK_ERRORS.
d8fa0 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 if( (rc & SQLIT
d8fb0 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 E_IOERR) == SQLI
d8fc0 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 TE_IOERR ){.
d8fd0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
d8fe0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
d8ff0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c SQLITE_IGNORE_FL
d9000 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 OCK_LOCK_ERRORS
d9010 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a */. return rc;.
d9020 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 }.../*.** Lower
d9030 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
d9040 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
d9050 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f ptor pFile to lo
d9060 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
d9070 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
d9080 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
d9090 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
d90a0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
d90b0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
d90c0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
d90d0 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
d90e0 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
d90f0 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
d9100 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
d9110 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
d9120 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 .static int floc
d9130 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f kUnlock(sqlite3_
d9140 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
d9150 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 cktype) {. unix
d9160 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d9170 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a nixFile*)id;. .
d9180 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d9190 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 );. OSTRACE5("U
d91a0 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 NLOCK %d %d was
d91b0 20 25 64 20 70 69 64 3d 25 64 20 28 66 6c 6f 63 %d pid=%d (floc
d91c0 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c k)\n", pFile->h,
d91d0 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 locktype,.
d91e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
d91f0 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 ktype, getpid())
d9200 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
d9210 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 type<=SHARED_LOC
d9220 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d K );. . /* no-
d9230 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a op if possible *
d9240 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d9250 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 ocktype==locktyp
d9260 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
d9270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
d9280 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 . /* shared ca
d9290 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 n just be set be
d92a0 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 cause we always
d92b0 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 have an exclusiv
d92c0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 e */. if (lockt
d92d0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d92e0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ) {. pFile->l
d92f0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d9300 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 pe;. return S
d9310 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d9320 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 . /* no, really
d9330 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 , unlock. */. i
d9340 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 nt rc = flock(pF
d9350 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 ile->h, LOCK_UN)
d9360 3b 0a 20 20 69 66 20 28 72 63 29 20 7b 0a 20 20 ;. if (rc) {.
d9370 20 20 69 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 int r, tErrno
d9380 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d = errno;. r =
d9390 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d93a0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d93b0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d93c0 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 UNLOCK);. if(
d93d0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d93e0 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ) ){. pFile
d93f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d9400 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64 rrno;. }.#ifd
d9410 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ef SQLITE_IGNORE
d9420 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d9430 52 53 0a 20 20 20 20 69 66 28 20 28 72 20 26 20 RS. if( (r &
d9440 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d SQLITE_IOERR) ==
d9450 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b SQLITE_IOERR ){
d9460 0a 20 20 20 20 20 20 72 20 3d 20 53 51 4c 49 54 . r = SQLIT
d9470 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65 E_BUSY;. }.#e
d9480 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 ndif /* SQLITE_I
d9490 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b GNORE_FLOCK_LOCK
d94a0 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a _ERRORS */. .
d94b0 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a 20 20 return r;.
d94c0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 } else {. pFi
d94d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e le->locktype = N
d94e0 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 O_LOCK;. retu
d94f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d9500 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 }.}../*.** Close
d9510 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 a file..*/.stat
d9520 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 ic int flockClos
d9530 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
d9540 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 id) {. if( id )
d9550 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 {. flockUnloc
d9560 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
d9570 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f }. return clo
d9580 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a seUnixFile(id);.
d9590 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
d95a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d95b0 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f NG_STYLE && !OS_
d95c0 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a VXWORK */../****
d95d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
d95e0 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b End of the flock
d95f0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 lock implementa
d9600 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tion ***********
d9610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d9620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a *********/../***
d9670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d96a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d96b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
d96c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d96d0 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 **** Begin Named
d96e0 20 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 Semaphore Locki
d96f0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
d9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
d9710 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 * Named semaphor
d9720 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c e locking is onl
d9730 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 y supported on V
d9740 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 xWorks..**.** Se
d9750 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 maphore locking
d9760 69 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b is like dot-lock
d9770 20 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 and flock in th
d9780 61 74 20 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c at it really onl
d9790 79 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 45 58 y.** supports EX
d97a0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e CLUSIVE locking.
d97b0 20 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 Only a single
d97c0 70 72 6f 63 65 73 73 20 63 61 6e 20 72 65 61 64 process can read
d97d0 20 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 or write.** the
d97e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
d97f0 74 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20 t a time. This
d9800 72 65 64 75 63 65 73 20 70 6f 74 65 6e 74 69 61 reduces potentia
d9810 6c 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 l concurrency, b
d9820 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 ut.** makes the
d9830 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d9840 69 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 72 2e ion much easier.
d9850 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 .*/.#if OS_VXWOR
d9860 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 KS../*.** This r
d9870 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
d9880 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 there is a RESE
d9890 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f RVED lock held o
d98a0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a n the specified.
d98b0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 ** file by this
d98c0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
d98d0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 cess. If such a
d98e0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 lock is held, se
d98f0 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f t *pResOut.** to
d9900 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 a non-zero valu
d9910 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 e otherwise *pRe
d9920 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a sOut is set to z
d9930 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e ero. The return
d9940 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 value.** is set
d9950 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e to SQLITE_OK un
d9960 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f less an I/O erro
d9970 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
d9980 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a lock checking..*
d9990 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d /.static int sem
d99a0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d99b0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d99c0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
d99d0 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
d99e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d99f0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d9a00 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d9a10 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d9a20 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 .. SimulateIOEr
d9a30 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
d9a40 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d9a50 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 SERVEDLOCK; );.
d9a60 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c . assert( pFil
d9a70 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b e );.. /* Check
d9a80 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
d9a90 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
d9aa0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
d9ab0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d9ac0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d9ad0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
d9ae0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
d9af0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
d9b00 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
d9b10 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
d9b20 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 t. */. if( !res
d9b30 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d erved ){. sem
d9b40 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 _t *pSem = pFile
d9b50 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 ->pOpen->pSem;.
d9b60 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 struct stat s
d9b70 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 tatBuf;.. if(
d9b80 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 sem_trywait(pSe
d9b90 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 m)==-1 ){.
d9ba0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
d9bb0 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41 no;. if( EA
d9bc0 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 GAIN != tErrno )
d9bd0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
d9be0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d9bf0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d9c00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
d9c10 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 ECKRESERVEDLOCK)
d9c20 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d ;. pFile-
d9c30 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d9c40 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 rno;. } els
d9c50 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 e {. /* s
d9c60 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 omeone else has
d9c70 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 the lock when we
d9c80 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 are in NO_LOCK
d9c90 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72 */. reser
d9ca0 76 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f ved = (pFile->lo
d9cb0 63 6b 74 79 70 65 20 3c 20 53 48 41 52 45 44 5f cktype < SHARED_
d9cc0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 LOCK);. }.
d9cd0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d9ce0 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65 /* we could have
d9cf0 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69 it if we want i
d9d00 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 t */. sem_p
d9d10 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d ost(pSem);. }
d9d20 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 . }. OSTRACE4(
d9d30 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
d9d40 20 25 64 20 25 64 20 28 73 65 6d 29 5c 6e 22 2c %d %d (sem)\n",
d9d50 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 pFile->h, rc, r
d9d60 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 eserved);.. *pR
d9d70 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 esOut = reserved
d9d80 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d9d90 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
d9da0 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
d9db0 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
d9dc0 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
d9dd0 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
d9de0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
d9df0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
d9e00 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
d9e10 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
d9e20 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
d9e30 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
d9e40 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
d9e50 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
d9e60 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
d9e70 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
d9e80 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
d9e90 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
d9ea0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
d9eb0 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
d9ec0 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
d9ed0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
d9ee0 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
d9ef0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
d9f00 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
d9f10 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
d9f20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
d9f30 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
d9f40 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
d9f50 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
d9f60 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
d9f70 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
d9f80 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
d9f90 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
d9fa0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
d9fb0 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
d9fc0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d9fd0 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
d9fe0 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
d9ff0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
da000 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
da010 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
da020 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
da030 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
da040 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 IVE.**.** Semaph
da050 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20 72 ore locks only r
da060 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 eally support EX
da070 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 CLUSIVE locks.
da080 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 We track interme
da090 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 diate.** lock st
da0a0 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 ates in the sqli
da0b0 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 te3_file structu
da0c0 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b re, but all lock
da0d0 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 s SHARED or.** a
da0e0 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 bove are really
da0f0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 EXCLUSIVE locks
da100 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 and exclude all
da110 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 other processes
da120 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 from.** access t
da130 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 he file..**.** T
da140 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
da150 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
da160 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 lock. Use the
da170 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 sqlite3OsUnlock(
da180 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 ).** routine to
da190 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 lower a locking
da1a0 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 level..*/.static
da1b0 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 6c int semLock(sql
da1c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
da1d0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
da1e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
da1f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
da200 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 65 ;. int fd;. se
da210 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c m_t *pSem = pFil
da220 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a e->pOpen->pSem;.
da230 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
da240 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 E_OK;.. /* if w
da250 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 e already have a
da260 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 63 lock, it is exc
da270 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 4a lusive. . ** J
da280 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 6c ust adjust level
da290 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 74 and punt on out
da2a0 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 66 ta here. */. if
da2b0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
da2c0 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 e > NO_LOCK) {.
da2d0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
da2e0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
da2f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
da300 4b 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f K;. goto sem_
da310 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 end_lock;. }.
da320 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 . /* lock semap
da330 68 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61 69 hore now but bai
da340 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 61 l out when alrea
da350 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 dy locked. */.
da360 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 if( sem_trywait(
da370 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 pSem)==-1 ){.
da380 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
da390 59 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d 5f Y;. goto sem_
da3a0 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 end_lock;. }..
da3b0 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 /* got it, set
da3c0 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 the type and ret
da3d0 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c urn ok */. pFil
da3e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
da3f0 63 6b 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65 6e cktype;.. sem_en
da400 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 6e d_lock:. return
da410 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
da420 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 wer the locking
da430 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 level on file de
da440 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 scriptor pFile t
da450 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 o locktype. loc
da460 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 ktype.** must be
da470 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 either NO_LOCK
da480 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a or SHARED_LOCK..
da490 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 **.** If the loc
da4a0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 king level of th
da4b0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
da4c0 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 r is already at
da4d0 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 or below.** the
da4e0 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e requested lockin
da4f0 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f g level, this ro
da500 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 utine is a no-op
da510 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
da520 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 semUnlock(sqlite
da530 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
da540 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
da550 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
da560 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
da570 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 sem_t *pSem = p
da580 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 File->pOpen->pSe
da590 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 m;.. assert( pF
da5a0 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ile );. assert(
da5b0 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41 pSem );. OSTRA
da5c0 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 CE5("UNLOCK %d
da5d0 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 %d was %d pid=%d
da5e0 20 28 73 65 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 (sem)\n", pFile
da5f0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 ->h, locktype,..
da600 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
da610 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 pe, getpid());.
da620 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
da630 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
da640 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 ;. . /* no-op
da650 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 if possible */.
da660 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
da670 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 type==locktype )
da680 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
da690 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 ITE_OK;. }. .
da6a0 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a /* shared can j
da6b0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 ust be set becau
da6c0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 se we always hav
da6d0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a e an exclusive *
da6e0 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 /. if (locktype
da6f0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b ==SHARED_LOCK) {
da700 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
da710 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
da720 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
da730 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
da740 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e /* no, really un
da750 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 lock. */. if (
da760 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d sem_post(pSem)==
da770 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 -1 ) {. int r
da780 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e c, tErrno = errn
da790 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 o;. rc = sqli
da7a0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
da7b0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
da7c0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
da7d0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c K);. if( IS_L
da7e0 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
da7f0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
da800 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
da810 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
da820 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 rn rc; . }. pF
da830 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
da840 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 NO_LOCK;. retur
da850 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
da860 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 /*. ** Close a f
da870 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 ile.. */.static
da880 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c int semClose(sql
da890 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
da8a0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
da8b0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
da8c0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
da8d0 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 ;. semUnlock(
da8e0 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 id, NO_LOCK);.
da8f0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
da900 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 );. unixEnter
da910 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c Mutex();. rel
da920 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 easeLockInfo(pFi
da930 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 le->pLock);.
da940 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 releaseOpenCnt(p
da950 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 File->pOpen);.
da960 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
da970 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 ();. closeUni
da980 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 xFile(id);. }.
da990 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
da9a0 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 K;.}..#endif /*
da9b0 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a OS_VXWORKS */./*
da9c0 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 .** Named semaph
da9d0 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f ore locking is o
da9e0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e nly available on
da9f0 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a VxWorks..**.***
daa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
daa10 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65 of the named se
daa20 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70 maphore lock imp
daa30 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
daa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
daa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f ***********/.../
daaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
daae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
daaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dab00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
dab10 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a AFP Locking ***
dab20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dab30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
dab40 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20 *.** AFP is the
dab50 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f Apple Filing Pro
dab60 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61 tocol. AFP is a
dab70 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 network filesys
dab80 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 tem found.** on
dab90 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 Apple Macintosh
daba0 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68 computers - both
dabb0 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a OS9 and OSX..**
dabc0 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 .** Third-party
dabd0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
dabe0 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c of AFP are avail
dabf0 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 able. But this
dac00 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c code here.** onl
dac10 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a y works on OSX..
dac20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 */..#if defined(
dac30 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 __APPLE__) && SQ
dac40 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
dac50 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 ING_STYLE./*.**
dac60 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f The afpLockingCo
dac70 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 ntext structure
dac80 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70 contains all afp
dac90 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73 lock specific s
daca0 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 tate.*/.typedef
dacb0 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e struct afpLockin
dacc0 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b gContext afpLock
dacd0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 ingContext;.stru
dace0 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e ct afpLockingCon
dacf0 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65 text {. unsigne
dad00 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 d long long shar
dad10 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 edByte;. const
dad20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 char *dbPath;
dad30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
dad40 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 e of the open fi
dad50 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 le */.};..struct
dad60 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 ByteRangeLockPB
dad70 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 2.{. unsigned l
dad80 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b ong long offset;
dad90 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 /* offse
dada0 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 t to first byte
dadb0 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 to lock */. uns
dadc0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
dadd0 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f length; /
dade0 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 * nbr of bytes t
dadf0 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 o lock */. unsi
dae00 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 gned long long r
dae10 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a etRangeStart; /*
dae20 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 nbr of 1st byte
dae30 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 locked if succe
dae40 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 ssful */. unsig
dae50 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 ned char unLockF
dae60 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 lag; /*
dae70 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 1 = unlock, 0 =
dae80 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e lock */. unsign
dae90 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 ed char startEnd
daea0 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 Flag; /* 1
daeb0 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 =rel to end of f
daec0 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 ork, 0=rel to st
daed0 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b art */. int fd;
daee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
daef0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 /* fi
daf00 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 le desc to assoc
daf10 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 this lock with
daf20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 */.};..#define a
daf30 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 fpfsByteRangeLoc
daf40 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f k2FSCTL _
daf50 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 IOWR('z', 23, st
daf60 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
daf70 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 ckPB2)../*.** Th
daf80 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 is is a utility
daf90 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 for setting or c
dafa0 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 learing a bit-ra
dafb0 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a nge lock on an.*
dafc0 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d * AFP filesystem
dafd0 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ..** .** Return
dafe0 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
daff0 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 cess, SQLITE_BUS
db000 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f Y on failure..*/
db010 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 .static int afpS
db020 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 etLock(. const
db030 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 char *path,
db040 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
db050 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 of the file to
db060 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c be locked or unl
db070 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 ocked */. unixF
db080 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 ile *pFile,
db090 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
db0a0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
db0b0 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 r on path */. u
db0c0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
db0d0 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a g offset, /*
db0e0 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 First byte to b
db0f0 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e e locked */. un
db100 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
db110 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 length, /*
db120 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
db130 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 to lock */. int
db140 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 setLockFlag
db150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
db160 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e rue to set lock.
db170 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 False to clear
db180 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 lock */.){. st
db190 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
db1a0 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 ckPB2 pb;. int
db1b0 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c err;. . pb.unL
db1c0 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 ockFlag = setLoc
db1d0 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 kFlag ? 0 : 1;.
db1e0 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 pb.startEndFlag
db1f0 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 = 0;. pb.offse
db200 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 t = offset;. pb
db210 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 .length = length
db220 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 ; . pb.fd = pFi
db230 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 le->h;. . OSTR
db240 41 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b ACE6("AFPSETLOCK
db250 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 [%s] for %d%s i
db260 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c n range %llx:%ll
db270 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c x\n", . (setL
db280 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 ockFlag?"ON":"OF
db290 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 F"), pFile->h, (
db2a0 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 pb.fd==-1?"[test
db2b0 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 val-1]":""),.
db2c0 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 offset, length)
db2d0 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 ;. err = fsctl(
db2e0 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 path, afpfsByteR
db2f0 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 angeLock2FSCTL,
db300 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 &pb, 0);. if (
db310 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 err==-1 ) {.
db320 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 int rc;. int
db330 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
db340 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46 OSTRACE4("AF
db350 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20 PSETLOCK failed
db360 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 to fsctl() '%s'
db370 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 %d %s\n",.
db380 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72 path, tEr
db390 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45 rno, strerror(tE
db3a0 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53 rrno));.#ifdef S
db3b0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 QLITE_IGNORE_AFP
db3c0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 _LOCK_ERRORS.
db3d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
db3e0 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 Y;.#else. rc
db3f0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
db400 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
db410 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 no,.
db420 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 setLockF
db430 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 lag ? SQLITE_IOE
db440 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 RR_LOCK : SQLITE
db450 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a _IOERR_UNLOCK);.
db460 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
db470 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b _IGNORE_AFP_LOCK
db480 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69 _ERRORS */. i
db490 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
db4a0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 (rc) ){. pF
db4b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
db4c0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 tErrno;. }.
db4d0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
db4e0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 } else {. ret
db4f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
db500 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
db510 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 routine checks
db520 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 if there is a RE
db530 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 SERVED lock held
db540 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 on the specifie
db550 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 d.** file by thi
db560 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 s or any other p
db570 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 rocess. If such
db580 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 a lock is held,
db590 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 set *pResOut.**
db5a0 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 to a non-zero va
db5b0 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 lue otherwise *p
db5c0 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
db5d0 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 zero. The retu
db5e0 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 rn value.** is s
db5f0 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 et to SQLITE_OK
db600 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 unless an I/O er
db610 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
db620 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e g lock checking.
db630 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
db640 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c fpCheckReservedL
db650 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
db660 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
db670 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ut){. int rc =
db680 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
db690 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
db6a0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
db6b0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
db6c0 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 ;. . SimulateI
db6d0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
db6e0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 QLITE_IOERR_CHEC
db6f0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 KRESERVEDLOCK; )
db700 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
db710 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 File );. afpLoc
db720 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e kingContext *con
db730 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 text = (afpLocki
db740 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
db750 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
db760 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 xt;. . /* Chec
db770 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
db780 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
db790 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
db7a0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
db7b0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
db7c0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
db7d0 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 rved = 1;. }.
db7e0 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 . /* Otherwise
db7f0 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 see if some othe
db800 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 r process holds
db810 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
db820 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 !reserved ){.
db830 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 /* lock the RES
db840 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 ERVED byte */.
db850 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53 int lrc = afpS
db860 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
db870 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
db880 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
db890 31 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51 1); . if( SQ
db8a0 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a LITE_OK==lrc ){.
db8b0 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 /* if we s
db8c0 75 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 ucceeded in taki
db8d0 6e 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20 ng the reserved
db8e0 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 lock, unlock it
db8f0 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 to restore.
db900 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c ** the original
db910 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 state */.
db920 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b lrc = afpSetLock
db930 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
db940 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 , pFile, RESERVE
db950 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 D_BYTE, 1, 0);.
db960 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
db970 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65 /* if we faile
db980 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 d to get the loc
db990 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 k then someone e
db9a0 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74 lse must have it
db9b0 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76 */. reserv
db9c0 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ed = 1;. }.
db9d0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
db9e0 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 ROR(lrc) ){.
db9f0 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a rc=lrc;. }.
dba00 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 }. . OSTRACE
dba10 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 4("TEST WR-LOCK
dba20 25 64 20 25 64 20 25 64 20 28 61 66 70 29 5c 6e %d %d %d (afp)\n
dba30 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
dba40 20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 0a 20 reserved);. .
dba50 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 *pResOut = rese
dba60 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 rved;. return r
dba70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b c;.}../*.** Lock
dba80 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 the file with t
dba90 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 he lock specifie
dbaa0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c d by parameter l
dbab0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a ocktype - one.**
dbac0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
dbad0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
dbae0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
dbaf0 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
dbb00 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
dbb10 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
dbb20 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
dbb30 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
dbb40 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
dbb50 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
dbb60 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
dbb70 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
dbb80 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
dbb90 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
dbba0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
dbbb0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
dbbc0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
dbbd0 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
dbbe0 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
dbbf0 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
dbc00 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
dbc10 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
dbc20 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
dbc30 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
dbc40 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
dbc50 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
dbc60 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
dbc70 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
dbc80 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
dbc90 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
dbca0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
dbcb0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
dbcc0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
dbcd0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
dbce0 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
dbcf0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
dbd00 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
dbd10 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
dbd20 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 CLUSIVE.**.** Th
dbd30 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
dbd40 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
dbd50 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 lock. Use the s
dbd60 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 qlite3OsUnlock()
dbd70 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c .** routine to l
dbd80 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c ower a locking l
dbd90 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 evel..*/.static
dbda0 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 int afpLock(sqli
dbdb0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
dbdc0 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 t locktype){. i
dbdd0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
dbde0 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 K;. unixFile *p
dbdf0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
dbe00 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 *)id;. afpLocki
dbe10 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 ngContext *conte
dbe20 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 xt = (afpLocking
dbe30 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65 Context *) pFile
dbe40 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
dbe50 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 ;. . assert( p
dbe60 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
dbe70 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 E5("LOCK %d %
dbe80 73 20 77 61 73 20 25 73 20 70 69 64 3d 25 64 20 s was %s pid=%d
dbe90 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d (afp)\n", pFile-
dbea0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 >h,. loc
dbeb0 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 ktypeName(lockty
dbec0 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d pe), locktypeNam
dbed0 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 e(pFile->locktyp
dbee0 65 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a e), getpid());..
dbef0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 /* If there is
dbf00 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 already a lock
dbf10 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 of this type or
dbf20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 more restrictive
dbf30 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 on the. ** uni
dbf40 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e xFile, do nothin
dbf50 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 g. Don't use the
dbf60 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 afp_end_lock: e
dbf70 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a xit path, as. *
dbf80 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 * unixEnterMutex
dbf90 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 () hasn't been c
dbfa0 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a alled yet.. */.
dbfb0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
dbfc0 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 ktype>=locktype
dbfd0 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 ){. OSTRACE3(
dbfe0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f "LOCK %d %s o
dbff0 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 k (already held)
dc000 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 (afp)\n", pFile
dc010 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
dc020 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
dc030 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 ktype));. ret
dc040 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
dc050 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
dc060 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 re the locking s
dc070 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 equence is corre
dc080 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ct. */. assert
dc090 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
dc0a0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f e!=NO_LOCK || lo
dc0b0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
dc0c0 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
dc0d0 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 locktype!=PENDI
dc0e0 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 NG_LOCK );. ass
dc0f0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 ert( locktype!=R
dc100 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 ESERVED_LOCK ||
dc110 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
dc120 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
dc130 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 . /* This mut
dc140 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 ex is needed bec
dc150 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ause pFile->pLoc
dc160 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f k is shared acro
dc170 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a ss threads. */.
dc180 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
dc190 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 ();.. /* Make s
dc1a0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ure the current
dc1b0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 thread owns the
dc1c0 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 pFile.. */. rc
dc1d0 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 = transferOwner
dc1e0 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 ship(pFile);. i
dc1f0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
dc200 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 ){. unixLeav
dc210 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 eMutex();. re
dc220 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 turn rc;. }.
dc230 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 . /* A PENDING
dc240 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 lock is needed
dc250 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 before acquiring
dc260 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 a SHARED lock a
dc270 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 nd before. ** a
dc280 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c cquiring an EXCL
dc290 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 USIVE lock. For
dc2a0 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b the SHARED lock
dc2b0 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 , the PENDING wi
dc2c0 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 ll. ** be relea
dc2d0 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 sed.. */. if(
dc2e0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
dc2f0 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 _LOCK . ||
dc300 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 (locktype==EXCLU
dc310 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 SIVE_LOCK && pFi
dc320 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e le->locktype<PEN
dc330 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a DING_LOCK). ){.
dc340 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a int failed;.
dc350 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 failed = afp
dc360 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
dc370 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
dc380 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c PENDING_BYTE, 1,
dc390 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69 1);. if (fai
dc3a0 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20 led) {. rc
dc3b0 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 = failed;.
dc3c0 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 goto afp_end_loc
dc3d0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a k;. }. }. .
dc3e0 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
dc3f0 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 gets to this poi
dc400 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c nt, then actuall
dc410 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d y go ahead and m
dc420 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 ake. ** operati
dc430 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 ng system calls
dc440 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
dc450 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 d lock.. */. i
dc460 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
dc470 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
dc480 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 int lk, lrc1, lr
dc490 63 32 3b 0a 20 20 20 20 69 6e 74 20 6c 72 63 31 c2;. int lrc1
dc4a0 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 0a Errno = 0;. .
dc4b0 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 /* Now get t
dc4c0 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 he read-lock SHA
dc4d0 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 RED_LOCK */.
dc4e0 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68 65 /* note that the
dc4f0 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65 20 quality of the
dc500 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e randomness doesn
dc510 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 20 6d 't matter that m
dc520 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 uch */. lk =
dc530 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 random(); . c
dc540 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 ontext->sharedBy
dc550 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 te = (lk & 0x7ff
dc560 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 fffff)%(SHARED_S
dc570 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 IZE - 1);. lr
dc580 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 c1 = afpSetLock(
dc590 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
dc5a0 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 pFile, .
dc5b0 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b SHARED_FIRST+
dc5c0 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 context->sharedB
dc5d0 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 yte, 1, 1);.
dc5e0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
dc5f0 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 20 R(lrc1) ){.
dc600 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 lrc1Errno = pFi
dc610 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 le->lastErrno;.
dc620 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 }. /* Drop
dc630 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 the temporary P
dc640 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 ENDING lock */.
dc650 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 74 lrc2 = afpSet
dc660 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
dc670 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e Path, pFile, PEN
dc680 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 DING_BYTE, 1, 0)
dc690 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 49 ;. . if( I
dc6a0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 S_LOCK_ERROR(lrc
dc6b0 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69 1) ) {. pFi
dc6c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
dc6d0 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 lrc1Errno;.
dc6e0 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 rc = lrc1;.
dc6f0 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c goto afp_end_l
dc700 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 ock;. } else
dc710 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
dc720 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 20 R(lrc2) ){.
dc730 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 rc = lrc2;.
dc740 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c goto afp_end_l
dc750 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 ock;. } else
dc760 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 if( lrc1 != SQLI
dc770 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 TE_OK ) {.
dc780 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d rc = lrc1;. }
dc790 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 46 else {. pF
dc7a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
dc7b0 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 SHARED_LOCK;.
dc7c0 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d pFile->pOpen-
dc7d0 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a >nLock++;. }.
dc7e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
dc7f0 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20 The request was
dc800 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f for a RESERVED o
dc810 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b r EXCLUSIVE lock
dc820 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 . It is. **
dc830 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 assumed that the
dc840 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f re is a SHARED o
dc850 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f r greater lock o
dc860 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a n the file. *
dc870 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a * already.. *
dc880 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 /. int failed
dc890 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 = 0;. assert
dc8a0 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b ( 0!=pFile->lock
dc8b0 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 28 type );. if (
dc8c0 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 locktype >= RESE
dc8d0 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 RVED_LOCK && pFi
dc8e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 le->locktype < R
dc8f0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a ESERVED_LOCK) {.
dc900 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 /* Acqui
dc910 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f re a RESERVED lo
dc920 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 ck */. fa
dc930 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 iled = afpSetLoc
dc940 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
dc950 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 h, pFile, RESERV
dc960 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 ED_BYTE, 1,1);.
dc970 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 61 }. if (!fa
dc980 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 iled && locktype
dc990 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f == EXCLUSIVE_LO
dc9a0 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 CK) {. /* A
dc9b0 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 cquire an EXCLUS
dc9c0 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 IVE lock */.
dc9d0 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 . /* Re
dc9e0 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 20 move the shared
dc9f0 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 69 lock before tryi
dca00 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 77 ng the range. w
dca10 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 e'll need to .
dca20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 ** reestabli
dca30 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f sh the shared lo
dca40 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 67 ck if we can't g
dca50 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 et the afpUnloc
dca60 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 k. */.
dca70 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d 20 if( !(failed =
dca80 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 afpSetLock(conte
dca90 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c xt->dbPath, pFil
dcaa0 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 e, SHARED_FIRST
dcab0 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
dcac0 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 conte
dcad0 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 xt->sharedByte,
dcae0 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20 20 1, 0)) ){.
dcaf0 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 int failed2 =
dcb00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
dcb10 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d /* now attemm
dcb20 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65 78 pt to get the ex
dcb30 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e clusive lock ran
dcb40 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 ge */. fa
dcb50 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 iled = afpSetLoc
dcb60 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
dcb70 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 h, pFile, SHARED
dcb80 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20 20 _FIRST, .
dcb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcba0 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 SHARED_S
dcbb0 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 IZE, 1);.
dcbc0 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 28 if( failed && (
dcbd0 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 74 failed2 = afpSet
dcbe0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
dcbf0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 Path, pFile, .
dcc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dcc10 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 SHARED_FIRS
dcc20 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 T + context->sha
dcc30 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 20 redByte, 1, 1))
dcc40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
dcc50 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 73 Can't reestablis
dcc60 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 h the shared loc
dcc70 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 74 k. Sqlite can't
dcc80 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 deal, this is.
dcc90 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 72 ** a cr
dcca0 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 itical I/O error
dccb0 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 . */.
dccc0 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 28 66 rc = ((f
dccd0 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f 49 ailed & SQLITE_I
dcce0 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f OERR) == SQLITE_
dccf0 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64 32 IOERR) ? failed2
dcd00 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 : .
dcd10 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f SQLITE_IOERR_
dcd20 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 LOCK;.
dcd30 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 goto afp_end_loc
dcd40 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 k;. } .
dcd50 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
dcd60 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 rc = failed;
dcd70 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
dcd80 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 29 7b if( failed ){
dcd90 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c . rc = fail
dcda0 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ed;. }. }.
dcdb0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
dcdc0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c E_OK ){. pFil
dcdd0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
dcde0 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 cktype;. }else
dcdf0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 if( locktype==EX
dce00 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a CLUSIVE_LOCK ){.
dce10 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
dce20 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ype = PENDING_LO
dce30 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 CK;. }. .afp_e
dce40 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c nd_lock:. unixL
dce50 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f eaveMutex();. O
dce60 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 STRACE4("LOCK
dce70 20 25 64 20 25 73 20 25 73 20 28 61 66 70 29 5c %d %s %s (afp)\
dce80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
dce90 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 cktypeName(lockt
dcea0 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20 20 20 ype), .
dceb0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 rc==SQLITE_OK ?
dcec0 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 "ok" : "failed")
dced0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
dcee0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
dcef0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
dcf00 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
dcf10 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b or pFile to lock
dcf20 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
dcf30 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
dcf40 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
dcf50 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
dcf60 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
dcf70 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
dcf80 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
dcf90 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
dcfa0 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
dcfb0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
dcfc0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
dcfd0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 is a no-op..*/.s
dcfe0 74 61 74 69 63 20 69 6e 74 20 61 66 70 55 6e 6c tatic int afpUnl
dcff0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
dd000 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
dd010 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d pe) {. int rc =
dd020 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e SQLITE_OK;. un
dd030 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
dd040 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
dd050 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
dd060 78 74 20 2a 70 43 74 78 20 3d 20 28 61 66 70 4c xt *pCtx = (afpL
dd070 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
dd080 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 pFile->lockingC
dd090 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 ontext;.. asser
dd0a0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
dd0b0 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 TRACE5("UNLOCK
dd0c0 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 %d %d was %d pid
dd0d0 3d 25 64 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 =%d (afp)\n", pF
dd0e0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
dd0f0 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 ,. pFile
dd100 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 ->locktype, getp
dd110 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 id());.. assert
dd120 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
dd130 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 ED_LOCK );. if(
dd140 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dd150 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 <=locktype ){.
dd160 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
dd170 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 OK;. }. if( CH
dd180 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 ECK_THREADID(pFi
dd190 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 le) ){. retur
dd1a0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
dd1b0 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 72 . }. unixEnter
dd1c0 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 70 Mutex();. if( p
dd1d0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 File->locktype>S
dd1e0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
dd1f0 20 20 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 . if( pFile
dd200 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c ->locktype==EXCL
dd210 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 USIVE_LOCK ){.
dd220 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c rc = afpSetL
dd230 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 ock(pCtx->dbPath
dd240 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f , pFile, SHARED_
dd250 46 49 52 53 54 2c 20 53 48 41 52 45 44 5f 53 49 FIRST, SHARED_SI
dd260 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 ZE, 0);. if
dd270 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
dd280 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 && locktype==SHA
dd290 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
dd2a0 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d 65 /* only re-e
dd2b0 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 stablish the sha
dd2c0 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 65 red lock if nece
dd2d0 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 20 ssary */.
dd2e0 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 int sharedLockB
dd2f0 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 yte = SHARED_FIR
dd300 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 ST+pCtx->sharedB
dd310 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63 20 yte;. rc
dd320 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
dd330 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
dd340 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 , sharedLockByte
dd350 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d , 1, 1);. }
dd360 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
dd370 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
dd380 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
dd390 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b =PENDING_LOCK ){
dd3a0 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 . rc = afpS
dd3b0 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 etLock(pCtx->dbP
dd3c0 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 ath, pFile, PEND
dd3d0 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b ING_BYTE, 1, 0);
dd3e0 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20 . } . if(
dd3f0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
dd400 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dd410 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 >=RESERVED_LOCK
dd420 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 ){. rc = af
dd430 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 pSetLock(pCtx->d
dd440 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 bPath, pFile, RE
dd450 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 SERVED_BYTE, 1,
dd460 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 0);. }. }els
dd470 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d e if( locktype==
dd480 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f NO_LOCK ){. /
dd490 2a 20 63 6c 65 61 72 20 74 68 65 20 73 68 61 72 * clear the shar
dd4a0 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 ed lock */. i
dd4b0 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 nt sharedLockByt
dd4c0 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 e = SHARED_FIRST
dd4d0 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79 74 +pCtx->sharedByt
dd4e0 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 66 70 53 e;. rc = afpS
dd4f0 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 etLock(pCtx->dbP
dd500 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 72 ath, pFile, shar
dd510 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 30 edLockByte, 1, 0
dd520 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 );. }.. if( rc
dd530 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
dd540 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d if( locktype=
dd550 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
dd560 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 struct unixOpe
dd570 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 nCnt *pOpen = pF
dd580 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 ile->pOpen;.
dd590 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d pOpen->nLock--
dd5a0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
dd5b0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 pOpen->nLock>=0
dd5c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 );. if( pOp
dd5d0 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a en->nLock==0 ){.
dd5e0 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6c 6f rc = clo
dd5f0 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 69 sePendingFds(pFi
dd600 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 le);. }.
dd610 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 }. }. unixLea
dd620 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 veMutex();. if(
dd630 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
dd640 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
dd650 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
dd660 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
dd670 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 c;.}../*.** Clos
dd680 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e e a file & clean
dd690 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20 up AFP specific
dd6a0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 locking context
dd6b0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
dd6c0 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f fpClose(sqlite3_
dd6d0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 file *id) {. if
dd6e0 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 ( id ){. unix
dd6f0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
dd700 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
dd710 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e afpUnlock(id, N
dd720 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 O_LOCK);. uni
dd730 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
dd740 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f if( pFile->pO
dd750 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f pen && pFile->pO
dd760 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 pen->nLock ){.
dd770 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
dd780 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 are outstanding
dd790 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 locks, do not ac
dd7a0 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 tually close the
dd7b0 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 file just.
dd7c0 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 ** yet because
dd7d0 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 that would clear
dd7e0 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 those locks. I
dd7f0 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 nstead, add the
dd800 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 file. ** de
dd810 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 scriptor to pOpe
dd820 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 n->aPending. It
dd830 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 will be automat
dd840 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 ically closed wh
dd850 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 en. ** the
dd860 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 last lock is cle
dd870 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ared.. */.
dd880 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46 setPendingF
dd890 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a d(pFile);. }.
dd8a0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 releaseOpenC
dd8b0 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 nt(pFile->pOpen)
dd8c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
dd8d0 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e ee(pFile->lockin
dd8e0 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 63 gContext);. c
dd8f0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
dd900 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d ;. unixLeaveM
dd910 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 utex();. }. re
dd920 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
dd930 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 }..#endif /* def
dd940 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
dd950 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
dd960 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a _LOCKING_STYLE *
dd970 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 /./*.** The code
dd980 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41 46 above is the AF
dd990 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 P lock implement
dd9a0 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65 ation. The code
dd9b0 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 is specific.**
dd9c0 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 6f to MacOSX and do
dd9d0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 6f es not work on o
dd9e0 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66 6f ther unix platfo
dd9f0 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e 61 rms. No alterna
dda00 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c tive.** is avail
dda10 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 6f able. If you do
dda20 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 20 n't compile for
dda30 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 20 a mac, then the
dda40 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 46 "unix-afp".** VF
dda50 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 S is not availab
dda60 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a le..**.*********
dda70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
dda80 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63 6b of the AFP lock
dda90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
ddaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddab0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
ddac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddb00 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a *****/.../******
ddb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddb50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
ddb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f ********* Non-lo
ddb70 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 cking sqlite3_fi
ddb80 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a le methods *****
ddb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ddba0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
ddbb0 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f 6e he next division
ddbc0 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d contains implem
ddbd0 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c entations for al
ddbe0 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 l methods of the
ddbf0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c .** sqlite3_fil
ddc00 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 74 e object other t
ddc10 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 han the locking
ddc20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f methods. The lo
ddc30 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 cking.** methods
ddc40 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69 6e were defined in
ddc50 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 divisions above
ddc60 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 (one locking me
ddc70 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 thod per.** divi
ddc80 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 sion). Those me
ddc90 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20 63 thods that are c
ddca0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 ommon to all loc
ddcb0 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 king modes.** ar
ddcc0 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68 65 e gather togethe
ddcd0 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76 69 r into this divi
ddce0 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 sion..*/../*.**
ddcf0 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 Seek to the offs
ddd00 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 et passed as the
ddd10 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
ddd20 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 , then read cnt
ddd30 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70 .** bytes into p
ddd40 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20 Buf. Return the
ddd50 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
ddd60 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a actually read..*
ddd70 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 *.** NB: If you
ddd80 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 define USE_PREA
ddd90 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34 D or USE_PREAD64
ddda0 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 , then it might
dddb0 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 also.** be neces
dddc0 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f sary to define _
dddd0 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 XOPEN_SOURCE to
ddde0 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 61 be 500. This va
dddf0 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 ries from.** one
dde00 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 system to anoth
dde10 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74 er. Since SQLit
dde20 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e e does not defin
dde30 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 e USE_PREAD.** a
dde40 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 ny any form by d
dde50 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 efault, we will
dde60 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 not attempt to d
dde70 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 efine _XOPEN_SOU
dde80 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b RCE..** See tick
dde90 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 32 ets #2741 and #2
ddea0 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 681..**.** To av
ddeb0 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 oid stomping the
ddec0 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 errno value on
dded0 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74 68 a failed read th
ddee0 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 e lastErrno valu
ddef0 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 6f e.** is set befo
ddf00 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f re returning..*/
ddf10 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 6b .static int seek
ddf20 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c 65 AndRead(unixFile
ddf30 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e *id, sqlite3_in
ddf40 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 t64 offset, void
ddf50 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 *pBuf, int cnt)
ddf60 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 {. int got;. i
ddf70 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 64 newOffset;.
ddf80 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 TIMER_START;.#if
ddf90 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 defined(USE_PRE
ddfa0 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 AD). got = prea
ddfb0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 d(id->h, pBuf, c
ddfc0 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 nt, offset);. S
ddfd0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
ddfe0 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 69 got = -1 );.#eli
ddff0 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 f defined(USE_PR
de000 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 EAD64). got = p
de010 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 42 read64(id->h, pB
de020 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 uf, cnt, offset)
de030 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
de040 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b ror( got = -1 );
de050 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 .#else. newOffs
de060 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 et = lseek(id->h
de070 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 , offset, SEEK_S
de080 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 ET);. SimulateI
de090 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 OError( newOffse
de0a0 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 t-- );. if( new
de0b0 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 29 Offset!=offset )
de0c0 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 66 {. if( newOff
de0d0 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 set == -1 ){.
de0e0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
de0f0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
de100 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 errno;. }else
de110 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 {. ((unixFi
de120 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
de130 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d no = 0;.... }
de140 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a . return -1;.
de150 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 64 }. got = read
de160 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e (id->h, pBuf, cn
de170 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d t);.#endif. TIM
de180 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f ER_END;. if( go
de190 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 t<0 ){. ((uni
de1a0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
de1b0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
de1c0 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22 52 }. OSTRACE5("R
de1d0 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64 20 EAD %-3d %5d
de1e0 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 %7lld %llu\n", i
de1f0 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 d->h, got, offse
de200 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 t, TIMER_ELAPSED
de210 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 3b );. return got;
de220 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 .}../*.** Read d
de230 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 ata from a file
de240 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 into a buffer.
de250 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
de260 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 if all.** bytes
de270 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65 were read succe
de280 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 ssfully and SQLI
de290 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 TE_IOERR if anyt
de2a0 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f hing goes.** wro
de2b0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
de2c0 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 71 t unixRead(. sq
de2d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
de2e0 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a . void *pBuf, .
de2f0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c int amt,. sql
de300 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
de310 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 t.){. unixFile
de320 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
de330 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20 67 le *)id;. int g
de340 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 ot;. assert( id
de350 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 );.. /* If thi
de360 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65 20 s is a database
de370 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 file (not a jour
de380 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 nal, master-jour
de390 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a nal or temp. **
de3a0 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 file), the byte
de3b0 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 s in the locking
de3c0 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 range should ne
de3d0 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 ver be read or w
de3e0 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 ritten. */. ass
de3f0 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 ert( pFile->pUnu
de400 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c sed==0. ||
de410 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 offset>=PENDING
de420 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 _BYTE+512.
de430 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d || offset+amt<=
de440 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 PENDING_BYTE .
de450 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 65 65 6b );.. got = seek
de460 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 6f AndRead(pFile, o
de470 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 ffset, pBuf, amt
de480 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d );. if( got==am
de490 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
de4a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
de4b0 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a se if( got<0 ){.
de4c0 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f /* lastErrno
de4d0 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 52 set by seekAndR
de4e0 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ead */. retur
de4f0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 n SQLITE_IOERR_R
de500 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 EAD;. }else{.
de510 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
de520 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20 61 no = 0; /* not a
de530 20 73 79 73 74 65 6d 20 65 72 72 6f 72 20 2a 2f system error */
de540 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 . /* Unread p
de550 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 66 arts of the buff
de560 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d er must be zero-
de570 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 filled */. me
de580 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 mset(&((char*)pB
de590 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 uf)[got], 0, amt
de5a0 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 -got);. retur
de5b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 n SQLITE_IOERR_S
de5c0 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d HORT_READ;. }.}
de5d0 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 ../*.** Seek to
de5e0 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 64 the offset in id
de5f0 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 65 ->offset then re
de600 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e 74 ad cnt bytes int
de610 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 72 o pBuf..** Retur
de620 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
de630 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 bytes actually r
de640 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 65 ead. Update the
de650 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 offset..**.** T
de660 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 o avoid stomping
de670 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 the errno value
de680 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72 69 on a failed wri
de690 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f te the lastErrno
de6a0 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 value.** is set
de6b0 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
de6c0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
de6d0 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75 6e seekAndWrite(un
de6e0 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 ixFile *id, i64
de6f0 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76 6f offset, const vo
de700 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e id *pBuf, int cn
de710 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 t){. int got;.
de720 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a i64 newOffset;.
de730 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 TIMER_START;.#
de740 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 if defined(USE_P
de750 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 77 READ). got = pw
de760 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 rite(id->h, pBuf
de770 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a , cnt, offset);.
de780 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 53 #elif defined(US
de790 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f 74 E_PREAD64). got
de7a0 20 3d 20 70 77 72 69 74 65 36 34 28 69 64 2d 3e = pwrite64(id->
de7b0 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 h, pBuf, cnt, of
de7c0 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a 20 20 6e fset);.#else. n
de7d0 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b ewOffset = lseek
de7e0 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 (id->h, offset,
de7f0 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 69 66 28 SEEK_SET);. if(
de800 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 newOffset!=offs
de810 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 et ){. if( ne
de820 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b wOffset == -1 ){
de830 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c . ((unixFil
de840 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e e*)id)->lastErrn
de850 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d o = errno;. }
de860 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 6e else{. ((un
de870 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
de880 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 tErrno = 0;....
de890 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
de8a0 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 -1;. }. got =
de8b0 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 write(id->h, pBu
de8c0 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a f, cnt);.#endif.
de8d0 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 TIMER_END;. i
de8e0 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 f( got<0 ){.
de8f0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
de900 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
de910 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 41 no;. }.. OSTRA
de920 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d 33 CE5("WRITE %-3
de930 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 d %5d %7lld %llu
de940 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c \n", id->h, got,
de950 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 offset, TIMER_E
de960 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72 LAPSED);. retur
de970 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a n got;.}.../*.**
de980 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d Write data from
de990 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 a buffer into a
de9a0 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 file. Return S
de9b0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
de9c0 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f ess.** or some o
de9d0 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 ther error code
de9e0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 on failure..*/.s
de9f0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 72 tatic int unixWr
dea00 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 ite(. sqlite3_f
dea10 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e 73 ile *id, . cons
dea20 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 t void *pBuf, .
dea30 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 int amt,. sqli
dea40 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
dea50 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 .){. unixFile
dea60 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
dea70 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77 72 le*)id;. int wr
dea80 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 ote = 0;. asser
dea90 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72 t( id );. asser
deaa0 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20 2f t( amt>0 );.. /
deab0 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64 * If this is a d
deac0 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f atabase file (no
dead0 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 t a journal, mas
deae0 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 ter-journal or t
deaf0 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 emp. ** file),
deb00 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 the bytes in the
deb10 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 locking range s
deb20 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 hould never be r
deb30 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 ead or written.
deb40 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
deb50 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 le->pUnused==0.
deb60 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e || offset>
deb70 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 =PENDING_BYTE+51
deb80 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 2. || offs
deb90 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f et+amt<=PENDING_
deba0 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 69 66 6e BYTE . );..#ifn
debb0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 def NDEBUG. /*
debc0 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 If we are doing
debd0 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 74 a normal write t
debe0 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c o a database fil
debf0 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f e (as opposed to
dec00 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 6f . ** doing a ho
dec10 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
dec20 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 6f ck or a write to
dec30 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 72 some file other
dec40 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f 72 than a. ** nor
dec50 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c mal database fil
dec60 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 74 e) then record t
dec70 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 he fact that the
dec80 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68 database. ** h
dec90 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 as changed. If
deca0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
decb0 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 66 counter is modif
decc0 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 74 ied, record that
decd0 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e 0a . ** fact too..
dece0 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
decf0 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 ->inNormalWrite
ded00 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 ){. pFile->db
ded10 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a 20 Update = 1; /*
ded20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 The database has
ded30 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 2a been modified *
ded40 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 /. if( offset
ded50 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b 61 <=24 && offset+a
ded60 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 20 mt>=27 ){.
ded70 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 68 int rc;. ch
ded80 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a 20 ar oldCntr[4];.
ded90 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
deda0 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 rrorBenign(1);.
dedb0 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 6e rc = seekAn
dedc0 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c dRead(pFile, 24,
dedd0 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20 oldCntr, 4);.
dede0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
dedf0 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 rorBenign(0);.
dee00 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c 7c if( rc!=4 ||
dee10 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 2c memcmp(oldCntr,
dee20 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
dee30 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 3d 24-offset], 4)!=
dee40 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 0 ){. pFi
dee50 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e le->transCntrChn
dee60 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 74 g = 1; /* The t
dee70 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
dee80 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a er has changed *
dee90 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a /. }. }.
deea0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 68 }.#endif.. wh
deeb0 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 ile( amt>0 && (w
deec0 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 rote = seekAndWr
deed0 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73 65 ite(pFile, offse
deee0 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e 30 t, pBuf, amt))>0
deef0 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 ){. amt -= w
def00 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 73 65 74 rote;. offset
def10 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 += wrote;. p
def20 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 Buf = &((char*)p
def30 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d Buf)[wrote];. }
def40 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
def50 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29 2c or(( wrote=(-1),
def60 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69 6d amt=1 ));. Sim
def70 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 ulateDiskfullErr
def80 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61 6d or(( wrote=0, am
def90 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61 6d t=1 ));. if( am
defa0 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 77 t>0 ){. if( w
defb0 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 rote<0 ){.
defc0 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 74 /* lastErrno set
defd0 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74 65 by seekAndWrite
defe0 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
deff0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
df000 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ITE;. }else{.
df010 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
df020 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e tErrno = 0; /* n
df030 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f ot a system erro
df040 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 r */. retur
df050 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
df060 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
df070 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
df080 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
df090 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 ST./*.** Count t
df0a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c he number of ful
df0b0 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 lsyncs and norma
df0c0 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 l syncs. This i
df0d0 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a s used to test.*
df0e0 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 * that syncs and
df0f0 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f fullsyncs are o
df100 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 20 ccurring at the
df110 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a right times..*/.
df120 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
df130 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e qlite3_sync_coun
df140 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 t = 0;.SQLITE_AP
df150 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 I int sqlite3_fu
df160 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 llsync_count = 0
df170 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
df180 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74 20 We do not trust
df190 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76 69 systems to provi
df1a0 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64 61 de a working fda
df1b0 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65 20 tasync(). Some
df1c0 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64 6f do..** Others do
df1d0 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 65 no. To be safe
df1e0 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b 20 , we will stick
df1f0 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65 72 with the (slower
df200 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49 66 ) fsync()..** If
df210 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20 79 you know that y
df220 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73 20 our system does
df230 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79 6e support fdatasyn
df240 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a 2a c() correctly,.*
df250 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63 6f * then simply co
df260 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64 61 mpile with -Dfda
df270 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e 63 tasync=fdatasync
df280 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .*/.#if !defined
df290 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20 21 (fdatasync) && !
df2a0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
df2b0 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61 74 _).# define fdat
df2c0 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e 64 async fsync.#end
df2d0 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 if../*.** Define
df2e0 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 HAVE_FULLFSYNC
df2f0 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e 64 to 0 or 1 depend
df300 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f ing on whether o
df310 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f 46 r not.** the F_F
df320 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20 69 ULLFSYNC macro i
df330 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46 55 s defined. F_FU
df340 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72 65 LLFSYNC is curre
df350 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76 61 ntly.** only ava
df360 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f 53 ilable on Mac OS
df370 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63 6f X. But that co
df380 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 23 uld change..*/.#
df390 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59 4e ifdef F_FULLFSYN
df3a0 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f C.# define HAVE_
df3b0 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c 73 FULLFSYNC 1.#els
df3c0 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 5f e.# define HAVE_
df3d0 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e 64 FULLFSYNC 0.#end
df3e0 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 if.../*.** The f
df3f0 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63 61 sync() system ca
df400 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b ll does not work
df410 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20 6f as advertised o
df420 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20 73 n many.** unix s
df430 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f 6c ystems. The fol
df440 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72 65 lowing procedure
df450 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74 is an attempt t
df460 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f 72 o make.** it wor
df470 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 k better..**.**
df480 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 The SQLITE_NO_SY
df490 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c 65 NC macro disable
df4a0 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e 20 s all fsync()s.
df4b0 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 0a This is useful.
df4c0 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 77 ** for testing w
df4d0 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 72 hen we want to r
df4e0 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 un through the t
df4f0 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b 6c est suite quickl
df500 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73 74 y..** You are st
df510 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20 2a rongly advised *
df520 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20 77 not* to deploy w
df530 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 ith SQLITE_NO_SY
df540 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 68 NC.** enabled, h
df550 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77 69 owever, since wi
df560 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e th SQLITE_NO_SYN
df570 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f 53 C enabled, an OS
df580 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f 77 crash.** or pow
df590 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c 20 er failure will
df5a0 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20 74 likely corrupt t
df5b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
df5c0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73 ..**.** SQLite s
df5d0 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c 79 ets the dataOnly
df5e0 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69 7a flag if the siz
df5f0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 e of the file is
df600 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 54 unchanged..** T
df610 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20 64 he idea behind d
df620 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74 20 ataOnly is that
df630 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 77 it should only w
df640 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63 6f rite the file co
df650 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73 6b ntent.** to disk
df660 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65 2e , not the inode.
df670 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64 61 We only set da
df680 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66 69 taOnly if the fi
df690 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20 75 le size is .** u
df6a0 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 74 nchanged since t
df6b0 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 he file size is
df6c0 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f 64 part of the inod
df6d0 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a 2a e. However, .**
df6e0 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73 20 Ted Ts'o tells
df6f0 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e us that fdatasyn
df700 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77 72 c() will also wr
df710 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69 66 ite the inode if
df720 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69 7a the.** file siz
df730 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 e has changed.
df740 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64 69 The only real di
df750 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
df760 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a 20 fdatasync().**
df770 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65 64 and fsync(), Ted
df780 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74 68 tells us, is th
df790 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 77 at fdatasync() w
df7a0 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74 68 ill not flush th
df7b0 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74 68 e.** inode if th
df7c0 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65 72 e mtime or owner
df7d0 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65 20 or other inode
df7e0 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65 20 attributes have
df7f0 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20 6f changed..** We o
df800 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20 74 nly care about t
df810 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e 6f he file size, no
df820 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c 65 t the other file
df830 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f 0a attributes, so.
df840 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51 4c ** as far as SQL
df850 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65 64 ite is concerned
df860 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28 29 , an fdatasync()
df870 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71 75 is always adequ
df880 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20 61 ate..** So, we a
df890 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61 73 lways use fdatas
df8a0 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20 61 ync() if it is a
df8b0 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72 64 vailable, regard
df8c0 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20 76 less of.** the v
df8d0 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 alue of the data
df8e0 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73 74 Only flag..*/.st
df8f0 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 73 atic int full_fs
df900 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 ync(int fd, int
df910 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 61 fullSync, int da
df920 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 72 taOnly){. int r
df930 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c c;.. /* The fol
df940 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 6c lowing "ifdef/el
df950 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b 20 if/else/" block
df960 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 72 has the same str
df970 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 74 ucture as. ** t
df980 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 74 he one below. It
df990 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 68 is replicated h
df9a0 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 76 ere solely to av
df9b0 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 0a oid cluttering .
df9c0 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 6c ** up the real
df9d0 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 55 code with the U
df9e0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
df9f0 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a 23 ) macros.. */.#
dfa00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f ifdef SQLITE_NO_
dfa10 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 SYNC. UNUSED_PA
dfa20 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 55 RAMETER(fd);. U
dfa30 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
dfa40 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e 55 fullSync);. UNU
dfa50 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 61 SED_PARAMETER(da
dfa60 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 48 taOnly);.#elif H
dfa70 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 20 AVE_FULLFSYNC.
dfa80 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
dfa90 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 73 (dataOnly);.#els
dfaa0 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d e. UNUSED_PARAM
dfab0 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a ETER(fullSync);.
dfac0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
dfad0 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 ER(dataOnly);.#e
dfae0 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 ndif.. /* Recor
dfaf0 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 d the number of
dfb00 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64 6f times that we do
dfb10 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 a normal fsync(
dfb20 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c 4c ) and . ** FULL
dfb30 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20 75 SYNC. This is u
dfb40 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 sed during testi
dfb50 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 ng to verify tha
dfb60 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 t this procedure
dfb70 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c 65 . ** gets calle
dfb80 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 65 d with the corre
dfb90 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 ct arguments..
dfba0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
dfbb0 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c 6c _TEST. if( full
dfbc0 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f 66 Sync ) sqlite3_f
dfbd0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b ullsync_count++;
dfbe0 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f . sqlite3_sync_
dfbf0 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a count++;.#endif.
dfc00 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 . /* If we comp
dfc10 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 iled with the SQ
dfc20 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 LITE_NO_SYNC fla
dfc30 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 g, then syncing
dfc40 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a is a. ** no-op.
dfc50 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
dfc60 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 20 TE_NO_SYNC. rc
dfc70 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c = SQLITE_OK;.#el
dfc80 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e if HAVE_FULLFSYN
dfc90 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e 63 C. if( fullSync
dfca0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63 6e ){. rc = fcn
dfcb0 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53 59 tl(fd, F_FULLFSY
dfcc0 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b NC, 0);. }else{
dfcd0 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 7d . rc = 1;. }
dfce0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55 4c . /* If the FUL
dfcf0 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20 66 LFSYNC failed, f
dfd00 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74 65 all back to atte
dfd10 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63 28 mpting an fsync(
dfd20 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75 6c ).. ** It shoul
dfd30 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c 65 dn't be possible
dfd40 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20 74 for fullfsync t
dfd50 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c 6f o fail on the lo
dfd60 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 73 cal . ** file s
dfd70 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c 20 ystem (on OSX),
dfd80 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69 63 so failure indic
dfd90 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46 53 ates that FULLFS
dfda0 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20 73 YNC. ** isn't s
dfdb0 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68 69 upported for thi
dfdc0 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 53 s file system. S
dfdd0 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66 73 o, attempt an fs
dfde0 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28 66 ync . ** and (f
dfdf0 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20 74 or now) ignore t
dfe00 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20 61 he overhead of a
dfe10 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63 6e superfluous fcn
dfe20 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a 20 tl call. . **
dfe30 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20 74 It'd be better t
dfe40 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73 79 o detect fullfsy
dfe50 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65 20 nc support once
dfe60 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a 20 and avoid . **
dfe70 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20 65 the fcntl call e
dfe80 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20 69 very time sync i
dfe90 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a 20 s called.. */.
dfea0 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20 66 if( rc ) rc = f
dfeb0 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73 65 sync(fd);..#else
dfec0 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73 79 . rc = fdatasy
dfed0 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f 56 nc(fd);.#if OS_V
dfee0 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 3d XWORKS. if( rc=
dfef0 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45 4e =-1 && errno==EN
dff00 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63 20 OTSUP ){. rc
dff10 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d = fsync(fd);. }
dff20 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
dff30 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66 20 WORKS */.#endif
dff40 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f /* ifdef SQLITE_
dff50 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41 56 NO_SYNC elif HAV
dff60 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a 0a E_FULLFSYNC */..
dff70 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b 53 if( OS_VXWORKS
dff80 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a 20 && rc!= -1 ){.
dff90 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 rc = 0;. }.
dffa0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
dffb0 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 *.** Make sure a
dffc0 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 ll writes to a p
dffd0 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 articular file a
dffe0 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 re committed to
dfff0 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 disk..**.** If d
e0000 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e 20 ataOnly==0 then
e0010 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69 74 both the file it
e0020 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65 74 self and its met
e0030 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20 73 adata (file.** s
e0040 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d 65 ize, access time
e0050 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63 65 , etc) are synce
e0060 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79 21 d. If dataOnly!
e0070 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 =0 then only the
e0080 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69 73 .** file data is
e0090 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 synced..**.** U
e00a0 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f 20 nder Unix, also
e00b0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
e00c0 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e 74 he directory ent
e00d0 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 0a ry for the file.
e00e0 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 ** has been crea
e00f0 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e 67 ted by fsync-ing
e0100 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 the directory t
e0110 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
e0120 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 20 file..** If we
e0130 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 61 do not do this a
e0140 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 nd we encounter
e0150 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c a power failure,
e0160 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a 2a the directory.*
e0170 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 * entry for the
e0180 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f journal might no
e0190 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77 65 t exist after we
e01a0 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e 65 reboot. The ne
e01b0 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f 20 xt.** SQLite to
e01c0 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 20 access the file
e01d0 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 68 will not know th
e01e0 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 65 at the journal e
e01f0 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a 2a xists (because.*
e0200 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 * the directory
e0210 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a 6f entry for the jo
e0220 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72 20 urnal was never
e0230 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68 65 created) and the
e0240 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 transaction.**
e0250 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 61 will not roll ba
e0260 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c 65 ck - possibly le
e0270 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 ading to databas
e0280 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f e corruption..*/
e0290 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e02a0 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c Sync(sqlite3_fil
e02b0 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 e *id, int flags
e02c0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 ){. int rc;. u
e02d0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
e02e0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
e02f0 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e 6c . int isDataOnl
e0300 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49 54 y = (flags&SQLIT
e0310 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 29 E_SYNC_DATAONLY)
e0320 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73 79 ;. int isFullsy
e0330 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30 46 nc = (flags&0x0F
e0340 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 )==SQLITE_SYNC_F
e0350 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ULL;.. /* Check
e0360 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 4c that one of SQL
e0370 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 ITE_SYNC_NORMAL
e0380 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 73 or FULL was pass
e0390 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 28 ed */. assert((
e03a0 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c flags&0x0F)==SQL
e03b0 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 0a ITE_SYNC_NORMAL.
e03c0 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73 26 || (flags&
e03d0 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 0x0F)==SQLITE_SY
e03e0 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 20 NC_FULL. );..
e03f0 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c 20 /* Unix cannot,
e0400 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 but some systems
e0410 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
e0420 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65 72 TE_FULL from her
e0430 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69 6e e. This. ** lin
e0440 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 61 e is to test tha
e0450 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73 20 t doing so does
e0460 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70 72 not cause any pr
e0470 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 53 oblems.. */. S
e0480 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 imulateDiskfullE
e0490 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
e04a0 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 61 ITE_FULL );.. a
e04b0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
e04c0 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e 43 OSTRACE2("SYNC
e04d0 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 %-3d\n", pFi
e04e0 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20 66 le->h);. rc = f
e04f0 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d ull_fsync(pFile-
e0500 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c 20 >h, isFullsync,
e0510 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 53 isDataOnly);. S
e0520 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
e0530 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63 rc=1 );. if( rc
e0540 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
e0550 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
e0560 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
e0570 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 3b ITE_IOERR_FSYNC;
e0580 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 . }. if( pFile
e0590 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 ->dirfd>=0 ){.
e05a0 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20 4f int err;. O
e05b0 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e 43 STRACE4("DIRSYNC
e05c0 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c 6c %-3d (have_full
e05d0 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 6e fsync=%d fullsyn
e05e0 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d c=%d)\n", pFile-
e05f0 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20 20 >dirfd,.
e0600 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 HAVE_FULLFSY
e0610 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 3b NC, isFullsync);
e0620 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
e0630 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a DISABLE_DIRSYNC.
e0640 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 63 /* The direc
e0650 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e 6c tory sync is onl
e0660 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 66 y attempted if f
e0670 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20 20 ull_fsync is.
e0680 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20 6f ** turned off o
e0690 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20 20 r unavailable.
e06a0 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63 20 If a full_fsync
e06b0 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c 0a occurred above,.
e06c0 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 ** then the
e06d0 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 directory sync i
e06e0 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a 20 s superfluous..
e06f0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 */. if( (!
e0700 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 7c HAVE_FULLFSYNC |
e0710 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20 26 | !isFullsync) &
e0720 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 & full_fsync(pFi
e0730 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20 29 le->dirfd,0,0) )
e0740 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 {. /*.
e0750 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72 65 ** We have re
e0760 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65 20 ceived multiple
e0770 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e 63 reports of fsync
e0780 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20 20 () returning.
e0790 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77 68 ** errors wh
e07a0 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64 69 en applied to di
e07b0 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65 72 rectories on cer
e07c0 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65 6d tain file system
e07d0 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20 66 s.. ** A f
e07e0 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79 20 ailed directory
e07f0 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62 69 sync is not a bi
e0800 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20 73 g deal. So it s
e0810 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20 62 eems. ** b
e0820 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65 20 etter to ignore
e0830 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63 6b the error. Tick
e0840 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20 20 et #1657.
e0850 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46 69 */. /* pFi
e0860 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
e0870 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 20 errno; */.
e0880 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 /* return SQLIT
e0890 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 20 E_IOERR; */.
e08a0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72 72 }.#endif. err
e08b0 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e = close(pFile->
e08c0 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 20 dirfd); /* Only
e08d0 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 need to sync onc
e08e0 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 e, so close the
e08f0 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d 3d */. if( err==
e0900 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
e0910 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20 77 /* directory w
e0920 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65 20 hen we are done
e0930 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e */. pFile->
e0940 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20 20 dirfd = -1;.
e0950 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 }else{. pFi
e0960 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
e0970 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 errno;. rc
e0980 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 = SQLITE_IOERR_D
e0990 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d 0a IR_CLOSE;. }.
e09a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
e09b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 .}../*.** Trunca
e09c0 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 te an open file
e09d0 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 73 to a specified s
e09e0 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ize.*/.static in
e09f0 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28 73 t unixTruncate(s
e0a00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
e0a10 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 69 i64 nByte){. i
e0a20 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
e0a30 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 id );. Simulat
e0a40 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e eIOError( return
e0a50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 SQLITE_IOERR_TR
e0a60 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 3d UNCATE );. rc =
e0a70 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e 69 ftruncate(((uni
e0a80 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 xFile*)id)->h, (
e0a90 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20 20 off_t)nByte);.
e0aa0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28 28 if( rc ){. ((
e0ab0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
e0ac0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
e0ad0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
e0ae0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
e0af0 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 TE;. }else{.#if
e0b00 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 ndef NDEBUG.
e0b10 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 /* If we are doi
e0b20 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 ng a normal writ
e0b30 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 e to a database
e0b40 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 file (as opposed
e0b50 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 to. ** doing
e0b60 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 a hot-journal r
e0b70 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 ollback or a wri
e0b80 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 te to some file
e0b90 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 20 other than a.
e0ba0 20 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 ** normal datab
e0bb0 61 73 65 20 66 69 6c 65 29 20 61 6e 64 20 77 65 ase file) and we
e0bc0 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 truncate the fi
e0bd0 6c 65 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 le to zero lengt
e0be0 68 2c 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 65 h,. ** that e
e0bf0 66 66 65 63 74 69 76 65 6c 79 20 75 70 64 61 74 ffectively updat
e0c00 65 73 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f es the change co
e0c10 75 6e 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67 unter. This mig
e0c20 68 74 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a ht happen. **
e0c30 20 77 68 65 6e 20 72 65 73 74 6f 72 69 6e 67 20 when restoring
e0c40 61 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 a database using
e0c50 20 74 68 65 20 62 61 63 6b 75 70 20 41 50 49 20 the backup API
e0c60 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 from a zero-leng
e0c70 74 68 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 th. ** source
e0c80 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
e0c90 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
e0ca0 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 ->inNormalWrite
e0cb0 26 26 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 && nByte==0 ){.
e0cc0 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a ((unixFile*
e0cd0 29 69 64 29 2d 3e 74 72 61 6e 73 43 6e 74 72 43 )id)->transCntrC
e0ce0 68 6e 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 hng = 1;. }.#
e0cf0 65 6e 64 69 66 0a 0a 20 20 20 20 72 65 74 75 72 endif.. retur
e0d00 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
e0d10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d .}../*.** Determ
e0d20 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ine the current
e0d30 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 size of a file i
e0d40 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 n bytes.*/.stati
e0d50 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 53 69 c int unixFileSi
e0d60 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
e0d70 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 *id, i64 *pSize)
e0d80 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 {. int rc;. st
e0d90 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 ruct stat buf;.
e0da0 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 assert( id );.
e0db0 20 72 63 20 3d 20 66 73 74 61 74 28 28 28 75 6e rc = fstat(((un
e0dc0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 ixFile*)id)->h,
e0dd0 26 62 75 66 29 3b 0a 20 20 53 69 6d 75 6c 61 74 &buf);. Simulat
e0de0 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 31 20 29 eIOError( rc=1 )
e0df0 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b ;. if( rc!=0 ){
e0e00 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a . ((unixFile*
e0e10 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 )id)->lastErrno
e0e20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 = errno;. ret
e0e30 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
e0e40 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 _FSTAT;. }. *p
e0e50 53 69 7a 65 20 3d 20 62 75 66 2e 73 74 5f 73 69 Size = buf.st_si
e0e60 7a 65 3b 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 6f ze;.. /* When o
e0e70 70 65 6e 69 6e 67 20 61 20 7a 65 72 6f 2d 73 69 pening a zero-si
e0e80 7a 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 ze database, the
e0e90 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 findLockInfo()
e0ea0 70 72 6f 63 65 64 75 72 65 0a 20 20 2a 2a 20 77 procedure. ** w
e0eb0 72 69 74 65 73 20 61 20 73 69 6e 67 6c 65 20 62 rites a single b
e0ec0 79 74 65 20 69 6e 74 6f 20 74 68 61 74 20 66 69 yte into that fi
e0ed0 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 le in order to w
e0ee0 6f 72 6b 20 61 72 6f 75 6e 64 20 61 20 62 75 67 ork around a bug
e0ef0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d . ** in the OS-
e0f00 58 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 X msdos filesyst
e0f10 65 6d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f em. In order to
e0f20 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20 avoid problems
e0f30 77 69 74 68 20 75 70 70 65 72 0a 20 20 2a 2a 20 with upper. **
e0f40 6c 61 79 65 72 73 2c 20 77 65 20 6e 65 65 64 20 layers, we need
e0f50 74 6f 20 72 65 70 6f 72 74 20 74 68 69 73 20 66 to report this f
e0f60 69 6c 65 20 73 69 7a 65 20 61 73 20 7a 65 72 6f ile size as zero
e0f70 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 even though it
e0f80 69 73 0a 20 20 2a 2a 20 72 65 61 6c 6c 79 20 31 is. ** really 1
e0f90 2e 20 20 20 54 69 63 6b 65 74 20 23 33 32 36 30 . Ticket #3260
e0fa0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 2a 70 53 .. */. if( *pS
e0fb0 69 7a 65 3d 3d 31 20 29 20 2a 70 53 69 7a 65 20 ize==1 ) *pSize
e0fc0 3d 20 30 3b 0a 0a 0a 20 20 72 65 74 75 72 6e 20 = 0;... return
e0fd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 SQLITE_OK;.}..#i
e0fe0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e0ff0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
e1000 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
e1010 5f 5f 29 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 __)./*.** Handle
e1020 72 20 66 6f 72 20 70 72 6f 78 79 2d 6c 6f 63 6b r for proxy-lock
e1030 69 6e 67 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c ing file-control
e1040 20 76 65 72 62 73 2e 20 20 44 65 66 69 6e 65 64 verbs. Defined
e1050 20 62 65 6c 6f 77 20 69 6e 20 74 68 65 0a 2a 2a below in the.**
e1060 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e proxying lockin
e1070 67 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 g division..*/.s
e1080 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 tatic int proxyF
e1090 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 ileControl(sqlit
e10a0 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 e3_file*,int,voi
e10b0 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a d*);.#endif.../*
e10c0 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 .** Information
e10d0 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 61 and control of a
e10e0 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 n open file hand
e10f0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
e1100 74 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f t unixFileContro
e1110 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a l(sqlite3_file *
e1120 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 id, int op, void
e1130 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 *pArg){. switc
e1140 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 h( op ){. cas
e1150 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c e SQLITE_FCNTL_L
e1160 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 OCKSTATE: {.
e1170 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 *(int*)pArg =
e1180 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
e1190 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 >locktype;.
e11a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e11b0 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 K;. }. cas
e11c0 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 e SQLITE_LAST_ER
e11d0 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 RNO: {. *(i
e11e0 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 nt*)pArg = ((uni
e11f0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
e1200 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 Errno;. ret
e1210 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
e1220 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 }.#ifndef NDE
e1230 42 55 47 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 BUG. /* The p
e1240 61 67 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 ager calls this
e1250 6d 65 74 68 6f 64 20 74 6f 20 73 69 67 6e 61 6c method to signal
e1260 20 74 68 61 74 20 69 74 20 68 61 73 20 64 6f 6e that it has don
e1270 65 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 e. ** a rollb
e1280 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 ack and that the
e1290 20 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 database is the
e12a0 72 65 66 6f 72 65 20 75 6e 63 68 61 6e 67 65 64 refore unchanged
e12b0 20 61 6e 64 0a 20 20 20 20 2a 2a 20 69 74 20 68 and. ** it h
e12c0 65 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 66 6f ence it is OK fo
e12d0 72 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f r the transactio
e12e0 6e 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 n change counter
e12f0 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e to be. ** un
e1300 63 68 61 6e 67 65 64 2e 0a 20 20 20 20 2a 2f 0a changed.. */.
e1310 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
e1320 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 FCNTL_DB_UNCHANG
e1330 45 44 3a 20 7b 0a 20 20 20 20 20 20 28 28 75 6e ED: {. ((un
e1340 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 64 62 55 ixFile*)id)->dbU
e1350 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 pdate = 0;.
e1360 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e1370 4b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a K;. }.#endif.
e1380 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
e1390 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
e13a0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 && defined(__APP
e13b0 4c 45 5f 5f 29 0a 20 20 20 20 63 61 73 65 20 53 LE__). case S
e13c0 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 QLITE_SET_LOCKPR
e13d0 4f 58 59 46 49 4c 45 3a 0a 20 20 20 20 63 61 73 OXYFILE:. cas
e13e0 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 e SQLITE_GET_LOC
e13f0 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 KPROXYFILE: {.
e1400 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 return proxy
e1410 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 6f FileControl(id,o
e1420 70 2c 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 23 p,pArg);. }.#
e1430 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
e1440 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e1450 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
e1460 5f 5f 41 50 50 4c 45 5f 5f 29 20 2a 2f 0a 20 20 __APPLE__) */.
e1470 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
e1480 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a E_ERROR;.}../*.*
e1490 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 * Return the sec
e14a0 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 tor size in byte
e14b0 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 s of the underly
e14c0 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 ing block device
e14d0 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 for.** the spec
e14e0 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 ified file. This
e14f0 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 is almost alway
e1500 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 s 512 bytes, but
e1510 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 may be.** large
e1520 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 r for some devic
e1530 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 es..**.** SQLite
e1540 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 code assumes th
e1550 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e is function cann
e1560 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f ot fail. It also
e1570 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a assumes that.**
e1580 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 if two files ar
e1590 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 e created in the
e15a0 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 same file-syste
e15b0 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 m directory (i.e
e15c0 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 ..** a database
e15d0 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 and its journal
e15e0 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 file) that the s
e15f0 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 ector size will
e1600 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 be the.** same f
e1610 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 or both..*/.stat
e1620 69 63 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f ic int unixSecto
e1630 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
e1640 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 le *NotUsed){.
e1650 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e1660 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
e1670 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 urn SQLITE_DEFAU
e1680 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a LT_SECTOR_SIZE;.
e1690 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
e16a0 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72 61 the device chara
e16b0 63 74 65 72 69 73 74 69 63 73 20 66 6f 72 20 74 cteristics for t
e16c0 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 he file. This is
e16d0 20 61 6c 77 61 79 73 20 30 20 66 6f 72 20 75 6e always 0 for un
e16e0 69 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ix..*/.static in
e16f0 74 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 t unixDeviceChar
e1700 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 acteristics(sqli
e1710 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 te3_file *NotUse
e1720 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
e1730 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e1740 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
e1750 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 /*.** Here ends
e1760 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
e1770 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65 on of all sqlite
e1780 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 2e 0a 3_file methods..
e1790 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
e17a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71 ********* End sq
e17b0 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65 74 68 6f lite3_file Metho
e17c0 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ds *************
e17d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e17e0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
e17f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1830 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 64 */../*.** This d
e1840 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 ivision contains
e1850 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 definitions of
e1860 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e1870 64 73 20 6f 62 6a 65 63 74 73 20 74 68 61 74 0a ds objects that.
e1880 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 ** implement var
e1890 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63 6b 69 6e ious file lockin
e18a0 67 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 49 g strategies. I
e18b0 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 t also contains
e18c0 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20 6f definitions.** o
e18d0 66 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 f "finder" funct
e18e0 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64 65 72 2d ions. A finder-
e18f0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
e1900 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 61 to locate the a
e1910 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 73 71 ppropriate.** sq
e1920 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e1930 20 6f 62 6a 65 63 74 20 66 6f 72 20 61 20 70 61 object for a pa
e1940 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
e1950 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 41 70 e file. The pAp
e1960 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c 64 20 6f pData.** field o
e1970 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 f the sqlite3_vf
e1980 73 20 56 46 53 20 6f 62 6a 65 63 74 73 20 61 72 s VFS objects ar
e1990 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f e initialized to
e19a0 20 62 65 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a be pointers to.
e19b0 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 66 ** the correct f
e19c0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 66 inder-function f
e19d0 6f 72 20 74 68 61 74 20 56 46 53 2e 0a 2a 2a 0a or that VFS..**.
e19e0 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 20 66 ** Most finder f
e19f0 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 unctions return
e1a00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 a pointer to a f
e1a10 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f ixed sqlite3_io_
e1a20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 methods.** objec
e1a30 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 69 6e 74 t. The only int
e1a40 65 72 65 73 74 69 6e 67 20 66 69 6e 64 65 72 2d eresting finder-
e1a50 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 75 74 6f function is auto
e1a60 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 77 68 lockIoFinder, wh
e1a70 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61 74 20 ich.** looks at
e1a80 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74 the filesystem t
e1a90 79 70 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f ype and tries to
e1aa0 20 67 75 65 73 73 20 74 68 65 20 62 65 73 74 20 guess the best
e1ab0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 61 74 locking.** strat
e1ac0 65 67 79 20 66 72 6f 6d 20 74 68 61 74 2e 0a 2a egy from that..*
e1ad0 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64 65 72 2d *.** For finder-
e1ae0 66 75 6e 74 69 6f 6e 20 46 2c 20 74 77 6f 20 6f funtion F, two o
e1af0 62 6a 65 63 74 73 20 61 72 65 20 63 72 65 61 74 bjects are creat
e1b00 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 ed:.**.** (1)
e1b10 20 54 68 65 20 72 65 61 6c 20 66 69 6e 64 65 72 The real finder
e1b20 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 -function named
e1b30 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a 2a 2a "FImpt()"..**.**
e1b40 20 20 20 20 28 32 29 20 41 20 63 6f 6e 73 74 61 (2) A consta
e1b50 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 nt pointer to th
e1b60 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 is function name
e1b70 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a 0a 2a d just "F"..**.*
e1b80 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 *.** A pointer t
e1b90 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65 72 20 o the F pointer
e1ba0 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 70 is used as the p
e1bb0 41 70 70 44 61 74 61 20 76 61 6c 75 65 20 66 6f AppData value fo
e1bc0 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63 74 73 r VFS.** objects
e1bd0 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f . We have to do
e1be0 20 74 68 69 73 20 69 6e 73 74 65 61 64 20 6f 66 this instead of
e1bf0 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44 61 74 letting pAppDat
e1c00 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72 65 63 a point.** direc
e1c10 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e 64 65 tly at the finde
e1c20 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e 63 65 r-function since
e1c30 20 43 39 30 20 72 75 6c 65 73 20 70 72 65 76 65 C90 rules preve
e1c40 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20 66 72 nt a void*.** fr
e1c50 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74 6f 20 om be cast into
e1c60 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 a function point
e1c70 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 61 63 er..**.**.** Eac
e1c80 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 h instance of th
e1c90 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72 61 74 is macro generat
e1ca0 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73 3a 0a es two objects:.
e1cb0 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63 6f 6e **.** * A con
e1cc0 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f stant sqlite3_io
e1cd0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 _methods object
e1ce0 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68 61 74 call METHOD that
e1cf0 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 has locking.**
e1d00 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43 4c 4f methods CLO
e1d10 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b SE, LOCK, UNLOCK
e1d20 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a 2a 0a , CKRESLOCK..**.
e1d30 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f 20 6d ** * An I/O m
e1d40 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e ethod finder fun
e1d50 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46 49 4e ction called FIN
e1d60 44 45 52 20 74 68 61 74 20 72 65 74 75 72 6e 73 DER that returns
e1d70 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 a pointer.**
e1d80 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48 4f 44 to the METHOD
e1d90 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 70 object in the p
e1da0 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74 2e 0a revious bullet..
e1db0 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d 45 54 */.#define IOMET
e1dc0 48 4f 44 53 28 46 49 4e 44 45 52 2c 20 4d 45 54 HODS(FINDER, MET
e1dd0 48 4f 44 2c 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b HOD, CLOSE, LOCK
e1de0 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 4c 4f 43 4b , UNLOCK, CKLOCK
e1df0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
e1e00 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 \.static const s
e1e10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e1e20 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20 20 20 s METHOD = {
e1e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e1e50 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 . 1,
e1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e70 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 /* iVersion */
e1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e1ea0 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20 20 20 CLOSE,
e1eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e1ec0 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 * xClose */
e1ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e1ef0 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 20 20 unixRead,
e1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1f10 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 20 20 xRead */
e1f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e1f40 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20 20 20 unixWrite,
e1f50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e1f60 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20 20 20 xWrite */
e1f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f80 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e1f90 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20 20 20 unixTruncate,
e1fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e1fb0 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20 20 20 Truncate */
e1fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1fd0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
e1fe0 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20 20 20 nixSync,
e1ff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
e2000 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ync */
e2010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2020 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e \. un
e2030 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 ixFileSize,
e2040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 /* xFi
e2050 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 leSize */
e2060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2070 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c 4f 43 \. LOC
e2080 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K,
e2090 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
e20a0 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 k */
e20b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e20c0 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e 4c 4f \. UNLO
e20d0 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 CK,
e20e0 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
e20f0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 ck */
e2100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2110 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c 4f 43 \. CKLOC
e2120 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K,
e2130 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b /* xCheck
e2140 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 20 ReservedLock */
e2150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2160 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 \. unixFi
e2170 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 leControl,
e2180 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f /* xFileCo
e2190 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 20 20 ntrol */
e21a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e21b0 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 65 63 \. unixSec
e21c0 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20 20 20 torSize,
e21d0 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 /* xSectorS
e21e0 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ize */
e21f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2200 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65 76 69 \. unixDevi
e2210 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
e2220 73 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 61 s /* xDeviceCa
e2230 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20 20 20 pabilities */
e2240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2250 20 20 5c 0a 7d 3b 20 20 20 20 20 20 20 20 20 20 \.};
e2260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e22a0 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 \.static const
e22b0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e22c0 64 73 20 2a 46 49 4e 44 45 52 23 23 49 6d 70 6c ds *FINDER##Impl
e22d0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 (const char *z,
e22e0 75 6e 69 78 46 69 6c 65 20 2a 70 29 7b 20 20 20 unixFile *p){
e22f0 5c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d \. UNUSED_PARAM
e2300 45 54 45 52 28 7a 29 3b 20 55 4e 55 53 45 44 5f ETER(z); UNUSED_
e2310 50 41 52 41 4d 45 54 45 52 28 70 29 3b 20 20 20 PARAMETER(p);
e2320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e2340 0a 20 20 72 65 74 75 72 6e 20 26 4d 45 54 48 4f . return &METHO
e2350 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 D;
e2360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e2390 7d 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 20 20 20 20 20 20 20
e23b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e23c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e23d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 \.s
e23e0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
e23f0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
e2400 28 2a 63 6f 6e 73 74 20 46 49 4e 44 45 52 29 28 (*const FINDER)(
e2410 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 const char*,unix
e2420 46 69 6c 65 20 2a 70 29 20 20 20 20 5c 0a 20 20 File *p) \.
e2430 20 20 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c = FINDER##Impl
e2440 3b 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 61 72 ;../*.** Here ar
e2450 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 71 6c e all of the sql
e2460 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e2470 6f 62 6a 65 63 74 73 20 66 6f 72 20 65 61 63 68 objects for each
e2480 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 of the.** locki
e2490 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 ng strategies.
e24a0 46 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72 Functions that r
e24b0 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 eturn pointers t
e24c0 6f 20 74 68 65 73 65 20 6d 65 74 68 6f 64 73 0a o these methods.
e24d0 2a 2a 20 61 72 65 20 61 6c 73 6f 20 63 72 65 61 ** are also crea
e24e0 74 65 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44 ted..*/.IOMETHOD
e24f0 53 28 0a 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 S(. posixIoFind
e2500 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f er, /
e2510 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
e2520 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f 73 69 n name */. posi
e2530 78 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 xIoMethods,
e2540 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
e2550 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e2560 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 6e 69 ct name */. uni
e2570 78 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 xClose,
e2580 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
e2590 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 method */. uni
e25a0 78 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 xLock,
e25b0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
e25c0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 method */. unix
e25d0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
e25e0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
e25f0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 75 6e 69 method */. uni
e2600 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e2610 63 6b 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b ck /* xCheck
e2620 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
e2630 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f hod */.).IOMETHO
e2640 44 53 28 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 DS(. nolockIoFi
e2650 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 nder,
e2660 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
e2670 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c on name */. nol
e2680 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 ockIoMethods,
e2690 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
e26a0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
e26b0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 6e 6f ect name */. no
e26c0 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 lockClose,
e26d0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e26e0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f e method */. no
e26f0 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 lockLock,
e2700 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
e2710 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c method */. nol
e2720 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 ockUnlock,
e2730 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
e2740 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 6e 6f k method */. no
e2750 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 lockCheckReserve
e2760 64 4c 6f 63 6b 20 20 20 2f 2a 20 78 43 68 65 63 dLock /* xChec
e2770 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
e2780 74 68 6f 64 20 2a 2f 0a 29 0a 49 4f 4d 45 54 48 thod */.).IOMETH
e2790 4f 44 53 28 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f ODS(. dotlockIo
e27a0 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 Finder,
e27b0 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 /* Finder funct
e27c0 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f ion name */. do
e27d0 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 tlockIoMethods,
e27e0 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 /* sqlit
e27f0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e2800 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 ject name */. d
e2810 6f 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 otlockClose,
e2820 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f /* xClo
e2830 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 se method */. d
e2840 6f 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 otlockLock,
e2850 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
e2860 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 6f k method */. do
e2870 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 tlockUnlock,
e2880 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
e2890 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64 ck method */. d
e28a0 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 otlockCheckReser
e28b0 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68 65 vedLock /* xChe
e28c0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d ckReservedLock m
e28d0 65 74 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 66 20 ethod */.)..#if
e28e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
e28f0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 CKING_STYLE && !
e2900 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 OS_VXWORKS.IOMET
e2910 48 4f 44 53 28 0a 20 20 66 6c 6f 63 6b 49 6f 46 HODS(. flockIoF
e2920 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
e2930 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 /* Finder func
e2940 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 tion name */. f
e2950 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20 20 lockIoMethods,
e2960 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 /* sqli
e2970 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
e2980 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 bject name */.
e2990 66 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 flockClose,
e29a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c /* xCl
e29b0 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ose method */.
e29c0 66 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 flockLock,
e29d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f /* xLo
e29e0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 ck method */. f
e29f0 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 lockUnlock,
e2a00 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
e2a10 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ock method */.
e2a20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 flockCheckReserv
e2a30 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 edLock /* xCh
e2a40 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 eckReservedLock
e2a50 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 method */.).#end
e2a60 69 66 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 if..#if OS_VXWOR
e2a70 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 KS.IOMETHODS(.
e2a80 73 65 6d 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 semIoFinder,
e2a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
e2aa0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
e2ab0 65 20 2a 2f 0a 20 20 73 65 6d 49 6f 4d 65 74 68 e */. semIoMeth
e2ac0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ods,
e2ad0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
e2ae0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
e2af0 6d 65 20 2a 2f 0a 20 20 73 65 6d 43 6c 6f 73 65 me */. semClose
e2b00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e2b10 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 /* xClose meth
e2b20 6f 64 20 2a 2f 0a 20 20 73 65 6d 4c 6f 63 6b 2c od */. semLock,
e2b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2b40 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f /* xLock metho
e2b50 64 20 2a 2f 0a 20 20 73 65 6d 55 6e 6c 6f 63 6b d */. semUnlock
e2b60 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e2b70 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 /* xUnlock meth
e2b80 6f 64 20 2a 2f 0a 20 20 73 65 6d 43 68 65 63 6b od */. semCheck
e2b90 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
e2ba0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
e2bb0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a vedLock method *
e2bc0 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 /.).#endif..#if
e2bd0 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
e2be0 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 _) && SQLITE_ENA
e2bf0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e2c00 45 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 61 E.IOMETHODS(. a
e2c10 66 70 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 fpIoFinder,
e2c20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
e2c30 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
e2c40 20 2a 2f 0a 20 20 61 66 70 49 6f 4d 65 74 68 6f */. afpIoMetho
e2c50 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ds,
e2c60 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
e2c70 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
e2c80 65 20 2a 2f 0a 20 20 61 66 70 43 6c 6f 73 65 2c e */. afpClose,
e2c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2ca0 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f /* xClose metho
e2cb0 64 20 2a 2f 0a 20 20 61 66 70 4c 6f 63 6b 2c 20 d */. afpLock,
e2cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2cd0 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 /* xLock method
e2ce0 20 2a 2f 0a 20 20 61 66 70 55 6e 6c 6f 63 6b 2c */. afpUnlock,
e2cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2d00 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f /* xUnlock metho
e2d10 64 20 2a 2f 0a 20 20 61 66 70 43 68 65 63 6b 52 d */. afpCheckR
e2d20 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 eservedLock
e2d30 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
e2d40 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f edLock method */
e2d50 0a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .).#endif../*.**
e2d60 20 54 68 65 20 22 57 68 6f 6c 65 20 46 69 6c 65 The "Whole File
e2d70 20 4c 6f 63 6b 69 6e 67 22 20 66 69 6e 64 65 72 Locking" finder
e2d80 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d returns the sam
e2d90 65 20 73 65 74 20 6f 66 20 6d 65 74 68 6f 64 73 e set of methods
e2da0 20 61 73 0a 2a 2a 20 74 68 65 20 70 6f 73 69 78 as.** the posix
e2db0 20 6c 6f 63 6b 69 6e 67 20 66 69 6e 64 65 72 2e locking finder.
e2dc0 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 73 65 But it also se
e2dd0 74 73 20 74 68 65 20 53 51 4c 49 54 45 5f 57 48 ts the SQLITE_WH
e2de0 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 OLE_FILE_LOCKING
e2df0 0a 2a 2a 20 66 6c 61 67 20 74 6f 20 66 6f 72 63 .** flag to forc
e2e00 65 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 e the posix advi
e2e10 73 6f 72 79 20 6c 6f 63 6b 73 20 74 6f 20 63 6f sory locks to co
e2e20 76 65 72 20 74 68 65 20 77 68 6f 6c 65 20 66 69 ver the whole fi
e2e30 6c 65 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 le instead.** of
e2e40 20 6a 75 73 74 20 61 20 73 6d 61 6c 6c 20 73 70 just a small sp
e2e50 61 6e 20 6f 66 20 62 79 74 65 73 20 6e 65 61 72 an of bytes near
e2e60 20 74 68 65 20 31 47 69 42 20 62 6f 75 6e 64 61 the 1GiB bounda
e2e70 72 79 2e 20 20 57 68 6f 6c 65 20 46 69 6c 65 20 ry. Whole File
e2e80 4c 6f 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 75 73 Locking.** is us
e2e90 65 66 75 6c 20 6f 6e 20 4e 46 53 2d 6d 6f 75 6e eful on NFS-moun
e2ea0 74 65 64 20 66 69 6c 65 73 20 73 69 6e 63 65 20 ted files since
e2eb0 69 74 20 68 65 6c 70 73 20 4e 46 53 20 74 6f 20 it helps NFS to
e2ec0 6d 61 69 6e 74 61 69 6e 20 63 61 63 68 65 0a 2a maintain cache.*
e2ed0 2a 20 63 6f 68 65 72 65 6e 63 79 2e 20 20 42 75 * coherency. Bu
e2ee0 74 20 69 74 20 69 73 20 61 20 64 65 74 72 69 6d t it is a detrim
e2ef0 65 6e 74 20 74 6f 20 6f 74 68 65 72 20 66 69 6c ent to other fil
e2f00 65 73 79 73 74 65 6d 73 20 73 69 6e 63 65 20 69 esystems since i
e2f10 74 20 72 75 6e 73 0a 2a 2a 20 73 6c 6f 77 65 72 t runs.** slower
e2f20 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
e2f30 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e2f40 68 6f 64 73 20 2a 70 6f 73 69 78 57 66 6c 49 6f hods *posixWflIo
e2f50 46 69 6e 64 65 72 49 6d 70 6c 28 63 6f 6e 73 74 FinderImpl(const
e2f60 20 63 68 61 72 2a 7a 2c 20 75 6e 69 78 46 69 6c char*z, unixFil
e2f70 65 2a 70 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 e*p){. UNUSED_P
e2f80 41 52 41 4d 45 54 45 52 28 7a 29 3b 0a 20 20 70 ARAMETER(z);. p
e2f90 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d 20 53 51 ->fileFlags = SQ
e2fa0 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
e2fb0 4c 4f 43 4b 49 4e 47 3b 0a 20 20 72 65 74 75 72 LOCKING;. retur
e2fc0 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 n &posixIoMethod
e2fd0 73 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 s;.}.static cons
e2fe0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e2ff0 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 hods . *(*const
e3000 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 posixWflIoFinde
e3010 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 r)(const char*,u
e3020 6e 69 78 46 69 6c 65 20 2a 70 29 20 3d 20 70 6f nixFile *p) = po
e3030 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d sixWflIoFinderIm
e3040 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 pl;../*.** The p
e3050 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 roxy locking met
e3060 68 6f 64 20 69 73 20 61 20 22 73 75 70 65 72 2d hod is a "super-
e3070 6d 65 74 68 6f 64 22 20 69 6e 20 74 68 65 20 73 method" in the s
e3080 65 6e 73 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 ense that it.**
e3090 6f 70 65 6e 73 20 73 65 63 6f 6e 64 61 72 79 20 opens secondary
e30a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
e30b0 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 61 for the conch a
e30c0 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 6e nd lock files an
e30d0 64 0a 2a 2a 20 69 74 20 75 73 65 73 20 70 72 6f d.** it uses pro
e30e0 78 79 2c 20 64 6f 74 2d 66 69 6c 65 2c 20 41 46 xy, dot-file, AF
e30f0 50 2c 20 61 6e 64 20 66 6c 6f 63 6b 28 29 20 6c P, and flock() l
e3100 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20 6f ocking methods o
e3110 6e 20 74 68 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e n those.** secon
e3120 64 61 72 79 20 66 69 6c 65 73 2e 20 20 46 6f 72 dary files. For
e3130 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 this reason, th
e3140 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 74 20 e division that
e3150 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 implements.** pr
e3160 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c oxy locking is l
e3170 6f 63 61 74 65 64 20 6d 75 63 68 20 66 75 72 74 ocated much furt
e3180 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 her down in the
e3190 66 69 6c 65 2e 20 20 42 75 74 20 77 65 20 6e 65 file. But we ne
e31a0 65 64 0a 2a 2a 20 74 6f 20 67 6f 20 61 68 65 61 ed.** to go ahea
e31b0 64 20 61 6e 64 20 64 65 66 69 6e 65 20 74 68 65 d and define the
e31c0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e31d0 6f 64 73 20 61 6e 64 20 66 69 6e 64 65 72 20 66 ods and finder f
e31e0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 unction.** for p
e31f0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 68 65 72 roxy locking her
e3200 65 2e 20 20 53 6f 20 77 65 20 66 6f 72 77 61 72 e. So we forwar
e3210 64 20 64 65 63 6c 61 72 65 20 74 68 65 20 49 2f d declare the I/
e3220 4f 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 O methods..*/.#i
e3230 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
e3240 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
e3250 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e3260 59 4c 45 0a 73 74 61 74 69 63 20 69 6e 74 20 70 YLE.static int p
e3270 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 roxyClose(sqlite
e3280 33 5f 66 69 6c 65 2a 29 3b 0a 73 74 61 74 69 63 3_file*);.static
e3290 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 int proxyLock(s
e32a0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e qlite3_file*, in
e32b0 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 t);.static int p
e32c0 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 roxyUnlock(sqlit
e32d0 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
e32e0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
e32f0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
e3300 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c k(sqlite3_file*,
e3310 20 69 6e 74 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 int*);.IOMETHOD
e3320 53 28 0a 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 S(. proxyIoFind
e3330 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f er, /
e3340 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
e3350 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 n name */. prox
e3360 79 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 yIoMethods,
e3370 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
e3380 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e3390 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 72 6f ct name */. pro
e33a0 78 79 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 xyClose,
e33b0 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
e33c0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f method */. pro
e33d0 78 79 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 xyLock,
e33e0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
e33f0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 method */. prox
e3400 79 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 yUnlock,
e3410 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
e3420 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 70 72 6f method */. pro
e3430 78 79 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xyCheckReservedL
e3440 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b ock /* xCheck
e3450 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
e3460 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a hod */.).#endif.
e3470 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f ..#if defined(__
e3480 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
e3490 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e34a0 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 G_STYLE./* .** T
e34b0 68 69 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e his "finder" fun
e34c0 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 ction attempts t
e34d0 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
e34e0 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 best locking str
e34f0 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 ategy .** for th
e3500 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
e3510 22 66 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 "filePath". It
e3520 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 then returns the
e3530 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e3540 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 ods.** object th
e3550 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
e3560 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a at strategy..**.
e3570 2a 2a 20 54 68 69 73 20 69 73 20 66 6f 72 20 4d ** This is for M
e3580 61 63 4f 53 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 acOSX only..*/.s
e3590 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
e35a0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
e35b0 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 autolockIoFinder
e35c0 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 Impl(. const ch
e35d0 61 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 ar *filePath,
e35e0 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 /* name of the
e35f0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
e3600 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 . unixFile *pNe
e3610 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f w /* o
e3620 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 pen file object
e3630 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
e3640 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 file */.){. st
e3650 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 atic const struc
e3660 74 20 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 20 t Mapping {.
e3670 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
e3680 65 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20 20 esystem;
e3690 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 73 /* Filesys
e36a0 74 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a 2f tem type name */
e36b0 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 . const sqlit
e36c0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 e3_io_methods *p
e36d0 4d 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 70 Methods; /* Ap
e36e0 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e propriate lockin
e36f0 67 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 g method */. }
e3700 61 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b aMap[] = {. {
e3710 20 22 68 66 73 22 2c 20 20 20 20 26 70 6f 73 69 "hfs", &posi
e3720 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 xIoMethods },.
e3730 20 20 7b 20 22 75 66 73 22 2c 20 20 20 20 26 70 { "ufs", &p
e3740 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c osixIoMethods },
e3750 0a 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c 20 . { "afpfs",
e3760 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d &afpIoMethods }
e3770 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ,.#ifdef SQLITE_
e3780 45 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 ENABLE_AFP_LOCKI
e3790 4e 47 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 6d NG_SMB. { "sm
e37a0 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 bfs", &afpIoMet
e37b0 68 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 hods },.#else.
e37c0 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 66 { "smbfs", &f
e37d0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c lockIoMethods },
e37e0 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 77 .#endif. { "w
e37f0 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 ebdav", &nolockI
e3800 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 oMethods },.
e3810 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 { 0, 0 }. };.
e3820 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
e3830 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 statfs fsInfo;.
e3840 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
e3850 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 ckInfo;.. if( !
e3860 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 filePath ){.
e3870 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d /* If filePath==
e3880 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 NULL that means
e3890 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 we are dealing w
e38a0 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 ith a transient
e38b0 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 file. ** that
e38c0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
e38d0 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a o be locked. */.
e38e0 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f return &nolo
e38f0 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d ckIoMethods;. }
e3900 0a 20 20 69 66 28 20 73 74 61 74 66 73 28 66 69 . if( statfs(fi
e3910 6c 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 lePath, &fsInfo)
e3920 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 66 != -1 ){. if
e3930 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 ( fsInfo.f_flags
e3940 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b & MNT_RDONLY ){
e3950 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 26 6e . return &n
e3960 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a olockIoMethods;.
e3970 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
e3980 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 0; aMap[i].zFile
e3990 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 system; i++){.
e39a0 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 66 if( strcmp(f
e39b0 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 sInfo.f_fstypena
e39c0 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c me, aMap[i].zFil
e39d0 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 esystem)==0 ){.
e39e0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d return aM
e39f0 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a ap[i].pMethods;.
e3a00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
e3a10 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 }.. /* Default
e3a20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 case. Handles, a
e3a30 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 mongst others, "
e3a40 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 20 nfs".. ** Test
e3a50 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 byte-range lock
e3a60 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 49 using fcntl(). I
e3a70 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63 65 f the call succe
e3a80 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 6d eds, . ** assum
e3a90 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d e that the file-
e3aa0 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 system supports
e3ab0 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b POSIX style lock
e3ac0 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 s. . */. lockI
e3ad0 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 nfo.l_len = 1;.
e3ae0 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 lockInfo.l_star
e3af0 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 t = 0;. lockInf
e3b00 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 o.l_whence = SEE
e3b10 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 K_SET;. lockInf
e3b20 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c o.l_type = F_RDL
e3b30 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 CK;. if( fcntl(
e3b40 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b pNew->h, F_GETLK
e3b50 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 , &lockInfo)!=-1
e3b60 20 29 20 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 66 ) {. pNew->f
e3b70 69 6c 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 ileFlags = SQLIT
e3b80 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
e3b90 4b 49 4e 47 3b 0a 20 20 20 20 72 65 74 75 72 6e KING;. return
e3ba0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 &posixIoMethods
e3bb0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
e3bc0 65 74 75 72 6e 20 26 64 6f 74 6c 6f 63 6b 49 6f eturn &dotlockIo
e3bd0 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 Methods;. }.}.s
e3be0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 tatic const sqli
e3bf0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a te3_io_methods .
e3c00 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c *(*const autol
e3c10 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e ockIoFinder)(con
e3c20 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c st char*,unixFil
e3c30 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f e*) = autolockIo
e3c40 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e FinderImpl;..#en
e3c50 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f dif /* defined(_
e3c60 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
e3c70 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e3c80 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69 66 NG_STYLE */..#if
e3c90 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 OS_VXWORKS && S
e3ca0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e3cb0 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 20 0a 2a KING_STYLE./* .*
e3cc0 2a 20 54 68 69 73 20 22 66 69 6e 64 65 72 22 20 * This "finder"
e3cd0 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 function attempt
e3ce0 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 s to determine t
e3cf0 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 20 he best locking
e3d00 73 74 72 61 74 65 67 79 20 0a 2a 2a 20 66 6f 72 strategy .** for
e3d10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
e3d20 6c 65 20 22 66 69 6c 65 50 61 74 68 22 2e 20 20 le "filePath".
e3d30 49 74 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 It then returns
e3d40 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
e3d50 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 ethods.** object
e3d60 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
e3d70 20 74 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a that strategy..
e3d80 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 66 6f **.** This is fo
e3d90 72 20 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 2e 0a r VXWorks only..
e3da0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
e3db0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e3dc0 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 ds *autolockIoFi
e3dd0 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 6f 6e 73 nderImpl(. cons
e3de0 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 t char *filePath
e3df0 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 6f 66 20 , /* name of
e3e00 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
e3e10 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 e */. unixFile
e3e20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20 20 20 *pNew
e3e30 2f 2a 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 /* the open file
e3e40 20 6f 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 object */.){.
e3e50 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
e3e60 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 kInfo;.. if( !f
e3e70 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f ilePath ){. /
e3e80 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e * If filePath==N
e3e90 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 ULL that means w
e3ea0 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 e are dealing wi
e3eb0 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 th a transient f
e3ec0 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 ile. ** that
e3ed0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
e3ee0 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 be locked. */.
e3ef0 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 return &noloc
e3f00 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a kIoMethods;. }.
e3f10 0a 20 20 2f 2a 20 54 65 73 74 20 69 66 20 66 63 . /* Test if fc
e3f20 6e 74 6c 28 29 20 69 73 20 73 75 70 70 6f 72 74 ntl() is support
e3f30 65 64 20 61 6e 64 20 75 73 65 20 50 4f 53 49 58 ed and use POSIX
e3f40 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 20 style locks..
e3f50 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c ** Otherwise fal
e3f60 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 6e 61 l back to the na
e3f70 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6d 65 med semaphore me
e3f80 74 68 6f 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 thod.. */. loc
e3f90 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b kInfo.l_len = 1;
e3fa0 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 . lockInfo.l_st
e3fb0 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 art = 0;. lockI
e3fc0 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 nfo.l_whence = S
e3fd0 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 EEK_SET;. lockI
e3fe0 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 nfo.l_type = F_R
e3ff0 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 DLCK;. if( fcnt
e4000 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 5f 47 45 54 l(pNew->h, F_GET
e4010 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d LK, &lockInfo)!=
e4020 2d 31 20 29 20 7b 0a 20 20 20 20 72 65 74 75 72 -1 ) {. retur
e4030 6e 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 n &posixIoMethod
e4040 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
e4050 72 65 74 75 72 6e 20 26 73 65 6d 49 6f 4d 65 74 return &semIoMet
e4060 68 6f 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 hods;. }.}.stat
e4070 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e4080 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a _io_methods . *
e4090 28 2a 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b (*const autolock
e40a0 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 IoFinder)(const
e40b0 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 char*,unixFile*)
e40c0 20 3d 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e = autolockIoFin
e40d0 64 65 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 derImpl;..#endif
e40e0 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 26 /* OS_VXWORKS &
e40f0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
e4100 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f LOCKING_STYLE */
e4110 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 61 62 73 74 72 ../*.** An abstr
e4120 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 70 act type for a p
e4130 6f 69 6e 74 65 72 20 74 6f 20 61 20 49 4f 20 6d ointer to a IO m
e4140 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66 75 6e ethod finder fun
e4150 63 74 69 6f 6e 3a 0a 2a 2f 0a 74 79 70 65 64 65 ction:.*/.typede
e4160 66 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f f const sqlite3_
e4170 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 66 69 io_methods *(*fi
e4180 6e 64 65 72 5f 74 79 70 65 29 28 63 6f 6e 73 74 nder_type)(const
e4190 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a char*,unixFile*
e41a0 29 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a );.../**********
e41b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e41c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e41d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e41e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e41f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
e4200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
e4210 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 sqlite3_vfs meth
e4220 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ods ************
e4230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e4240 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 .**.** This divi
e4250 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 sion contains th
e4260 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
e4270 20 6f 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 of methods on t
e4280 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 he.** sqlite3_vf
e4290 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a s object..*/../*
e42a0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 .** Initialize t
e42b0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
e42c0 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 he unixFile stru
e42d0 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f cture pointed to
e42e0 20 62 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61 74 by pId..*/.stat
e42f0 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 ic int fillInUni
e4300 78 46 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 xFile(. sqlite3
e4310 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
e4320 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
e4330 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 fs object */. i
e4340 6e 74 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 nt h,
e4350 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 /* Open f
e4360 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f ile descriptor o
e4370 66 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 f file being ope
e4380 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 ned */. int dir
e4390 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 fd,
e43a0 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 /* Directory fi
e43b0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f le descriptor */
e43c0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
e43d0 2a 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 *pId, /* Wr
e43e0 69 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 ite to the unixF
e43f0 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 68 65 ile structure he
e4400 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 re */. const ch
e4410 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
e4420 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
e4430 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 ile being opened
e4440 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b */. int noLock
e4450 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
e4460 20 4f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 Omit locking if
e4470 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 true */. int i
e4480 73 44 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 sDelete
e4490 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 /* Delete on
e44a0 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f close if true */
e44b0 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 .){. const sqli
e44c0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a te3_io_methods *
e44d0 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 pLockingStyle;.
e44e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 unixFile *pNew
e44f0 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 = (unixFile *)pI
e4500 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 d;. int rc = SQ
e4510 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 LITE_OK;.. asse
e4520 72 74 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b 3d rt( pNew->pLock=
e4530 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 =NULL );. asser
e4540 74 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d 3d t( pNew->pOpen==
e4550 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 NULL );.. /* Pa
e4560 72 61 6d 65 74 65 72 20 69 73 44 65 6c 65 74 65 rameter isDelete
e4570 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f 6e is only used on
e4580 20 76 78 77 6f 72 6b 73 2e 20 45 78 70 72 65 73 vxworks. Expres
e4590 73 20 74 68 69 73 20 65 78 70 6c 69 63 69 74 6c s this explicitl
e45a0 79 20 0a 20 20 2a 2a 20 68 65 72 65 20 74 6f 20 y . ** here to
e45b0 70 72 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72 prevent compiler
e45c0 20 77 61 72 6e 69 6e 67 73 20 61 62 6f 75 74 20 warnings about
e45d0 75 6e 75 73 65 64 20 70 61 72 61 6d 65 74 65 72 unused parameter
e45e0 73 2e 0a 20 20 2a 2f 0a 20 20 55 4e 55 53 45 44 s.. */. UNUSED
e45f0 5f 50 41 52 41 4d 45 54 45 52 28 69 73 44 65 6c _PARAMETER(isDel
e4600 65 74 65 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 ete);.. OSTRACE
e4610 33 28 22 4f 50 45 4e 20 20 20 20 25 2d 33 64 20 3("OPEN %-3d
e4620 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c 65 6e %s\n", h, zFilen
e4630 61 6d 65 29 3b 20 20 20 20 0a 20 20 70 4e 65 77 ame); . pNew
e4640 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d ->h = h;. pNew-
e4650 3e 64 69 72 66 64 20 3d 20 64 69 72 66 64 3b 0a >dirfd = dirfd;.
e4660 20 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 70 SET_THREADID(p
e4670 4e 65 77 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 New);. pNew->fi
e4680 6c 65 46 6c 61 67 73 20 3d 20 30 3b 0a 0a 23 69 leFlags = 0;..#i
e4690 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 70 f OS_VXWORKS. p
e46a0 4e 65 77 2d 3e 70 49 64 20 3d 20 76 78 77 6f 72 New->pId = vxwor
e46b0 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 7a 46 69 ksFindFileId(zFi
e46c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 lename);. if( p
e46d0 4e 65 77 2d 3e 70 49 64 3d 3d 30 20 29 7b 0a 20 New->pId==0 ){.
e46e0 20 20 20 6e 6f 4c 6f 63 6b 20 3d 20 31 3b 0a 20 noLock = 1;.
e46f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
e4700 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 OMEM;. }.#endif
e4710 0a 0a 20 20 69 66 28 20 6e 6f 4c 6f 63 6b 20 29 .. if( noLock )
e4720 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 {. pLockingSt
e4730 79 6c 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49 6f 4d yle = &nolockIoM
e4740 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b ethods;. }else{
e4750 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53 74 79 . pLockingSty
e4760 6c 65 20 3d 20 28 2a 2a 28 66 69 6e 64 65 72 5f le = (**(finder_
e4770 74 79 70 65 2a 29 70 56 66 73 2d 3e 70 41 70 70 type*)pVfs->pApp
e4780 44 61 74 61 29 28 7a 46 69 6c 65 6e 61 6d 65 2c Data)(zFilename,
e4790 20 70 4e 65 77 29 3b 0a 23 69 66 20 53 51 4c 49 pNew);.#if SQLI
e47a0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e47b0 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a 20 43 G_STYLE. /* C
e47c0 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 69 ache zFilename i
e47d0 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f n the locking co
e47e0 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64 20 64 ntext (AFP and d
e47f0 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64 65 29 otlock override)
e4800 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72 6f 78 for. ** prox
e4810 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69 6f 6e yLock activation
e4820 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28 72 65 is possible (re
e4830 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20 62 61 mote proxy is ba
e4840 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65 29 0a sed on db name).
e4850 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 ** zFilename
e4860 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 remains valid u
e4870 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f ntil file is clo
e4880 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72 74 20 sed, to support
e4890 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 */. pNew->loc
e48a0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 28 76 kingContext = (v
e48b0 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65 3b 0a oid*)zFilename;.
e48c0 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 69 66 #endif. }.. if
e48d0 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 ( pLockingStyle
e48e0 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f == &posixIoMetho
e48f0 64 73 20 29 7b 0a 20 20 20 20 75 6e 69 78 45 6e ds ){. unixEn
e4900 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
e4910 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 rc = findLockInf
e4920 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 o(pNew, &pNew->p
e4930 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 Lock, &pNew->pOp
e4940 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 en);. if( rc!
e4950 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
e4960 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 /* If an err
e4970 6f 72 20 6f 63 63 75 72 65 64 20 69 6e 20 66 69 or occured in fi
e4980 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2c 20 63 6c ndLockInfo(), cl
e4990 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ose the file des
e49a0 63 72 69 70 74 6f 72 0a 20 20 20 20 20 20 2a 2a criptor. **
e49b0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 immediately, be
e49c0 66 6f 72 65 20 72 65 6c 65 61 73 69 6e 67 20 74 fore releasing t
e49d0 68 65 20 6d 75 74 65 78 2e 20 66 69 6e 64 4c 6f he mutex. findLo
e49e0 63 6b 49 6e 66 6f 28 29 20 6d 61 79 20 66 61 69 ckInfo() may fai
e49f0 6c 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 77 l. ** in tw
e4a00 6f 20 73 63 65 6e 61 72 69 6f 73 3a 0a 20 20 20 o scenarios:.
e4a10 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 **. **
e4a20 20 28 61 29 20 41 20 63 61 6c 6c 20 74 6f 20 66 (a) A call to f
e4a30 73 74 61 74 28 29 20 66 61 69 6c 65 64 2e 0a 20 stat() failed..
e4a40 20 20 20 20 20 2a 2a 20 20 20 28 62 29 20 41 20 ** (b) A
e4a50 6d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 2e 0a 20 malloc failed..
e4a60 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
e4a70 20 53 63 65 6e 61 72 69 6f 20 28 62 29 20 6d 61 Scenario (b) ma
e4a80 79 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 y only occur if
e4a90 74 68 65 20 70 72 6f 63 65 73 73 20 69 73 20 68 the process is h
e4aa0 6f 6c 64 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0a olding no other.
e4ab0 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 64 65 ** file de
e4ac0 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f scriptors open o
e4ad0 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e n the same file.
e4ae0 20 49 66 20 74 68 65 72 65 20 77 65 72 65 20 6f If there were o
e4af0 74 68 65 72 20 66 69 6c 65 0a 20 20 20 20 20 20 ther file.
e4b00 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f ** descriptors o
e4b10 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 n this file, the
e4b20 6e 20 6e 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c n no malloc woul
e4b30 64 20 62 65 20 72 65 71 75 69 72 65 64 20 62 79 d be required by
e4b40 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e 64 4c 6f . ** findLo
e4b50 63 6b 49 6e 66 6f 28 29 2e 20 49 66 20 74 68 69 ckInfo(). If thi
e4b60 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 s is the case, i
e4b70 74 20 69 73 20 71 75 69 74 65 20 73 61 66 65 20 t is quite safe
e4b80 74 6f 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a to close. *
e4b90 2a 20 68 61 6e 64 6c 65 20 68 20 2d 20 61 73 20 * handle h - as
e4ba0 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 it is guaranteed
e4bb0 20 74 68 61 74 20 6e 6f 20 70 6f 73 69 78 20 6c that no posix l
e4bc0 6f 63 6b 73 20 77 69 6c 6c 20 62 65 20 72 65 6c ocks will be rel
e4bd0 65 61 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 eased. ** b
e4be0 79 20 64 6f 69 6e 67 20 73 6f 2e 0a 20 20 20 20 y doing so..
e4bf0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 **. ** If
e4c00 20 73 63 65 6e 61 72 69 6f 20 28 61 29 20 63 61 scenario (a) ca
e4c10 75 73 65 64 20 74 68 65 20 65 72 72 6f 72 20 74 used the error t
e4c20 68 65 6e 20 74 68 69 6e 67 73 20 61 72 65 20 6e hen things are n
e4c30 6f 74 20 73 6f 20 73 61 66 65 2e 20 54 68 65 0a ot so safe. The.
e4c40 20 20 20 20 20 20 2a 2a 20 69 6d 70 6c 69 63 69 ** implici
e4c50 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 68 65 72 t assumption her
e4c60 65 20 69 73 20 74 68 61 74 20 69 66 20 66 73 74 e is that if fst
e4c70 61 74 28 29 20 66 61 69 6c 73 2c 20 74 68 69 6e at() fails, thin
e4c80 67 73 20 61 72 65 20 69 6e 0a 20 20 20 20 20 20 gs are in.
e4c90 2a 2a 20 73 75 63 68 20 62 61 64 20 73 68 61 70 ** such bad shap
e4ca0 65 20 74 68 61 74 20 64 72 6f 70 70 69 6e 67 20 e that dropping
e4cb0 61 20 6c 6f 63 6b 20 6f 72 20 74 77 6f 20 64 6f a lock or two do
e4cc0 65 73 6e 27 74 20 6d 61 74 74 65 72 20 6d 75 63 esn't matter muc
e4cd0 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 h.. */.
e4ce0 20 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 20 20 close(h);.
e4cf0 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a h = -1;. }.
e4d00 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
e4d10 65 78 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 20 53 ex();. }..#if S
e4d20 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e4d30 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
e4d40 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e4d50 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 . else if( pLoc
e4d60 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 61 66 kingStyle == &af
e4d70 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 pIoMethods ){.
e4d80 20 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 /* AFP locking
e4d90 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 uses the file p
e4da0 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 ath so it needs
e4db0 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 to be included i
e4dc0 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 61 66 70 n. ** the afp
e4dd0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e 0a LockingContext..
e4de0 20 20 20 20 2a 2f 0a 20 20 20 20 61 66 70 4c 6f */. afpLo
e4df0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
e4e00 74 78 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f tx;. pNew->lo
e4e10 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 ckingContext = p
e4e20 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 Ctx = sqlite3_ma
e4e30 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 lloc( sizeof(*pC
e4e40 74 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 tx) );. if( p
e4e50 43 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ctx==0 ){.
e4e60 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
e4e70 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 M;. }else{.
e4e80 20 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65 /* NB: zFile
e4e90 6e 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20 name exists and
e4ea0 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e remains valid un
e4eb0 74 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20 til the file is
e4ec0 63 6c 6f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 closed. **
e4ed0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65 71 according to req
e4ee0 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31 2e uirement F11141.
e4ef0 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20 6e So we do not n
e4f00 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20 20 eed to make a.
e4f10 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 ** copy of t
e4f20 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20 2a 2f 0a he filename. */.
e4f30 20 20 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 pCtx->dbPa
e4f40 74 68 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a th = zFilename;.
e4f50 20 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 srandomdev
e4f60 28 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e ();. unixEn
e4f70 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 terMutex();.
e4f80 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 rc = findLockI
e4f90 6e 66 6f 28 70 4e 65 77 2c 20 4e 55 4c 4c 2c 20 nfo(pNew, NULL,
e4fa0 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 &pNew->pOpen);.
e4fb0 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 unixLeaveMu
e4fc0 74 65 78 28 29 3b 20 20 20 20 20 20 20 20 0a 20 tex(); .
e4fd0 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
e4fe0 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 . else if( pLoc
e4ff0 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 64 6f kingStyle == &do
e5000 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 tlockIoMethods )
e5010 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 66 69 6c 65 {. /* Dotfile
e5020 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 locking uses th
e5030 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 e file path so i
e5040 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e t needs to be in
e5050 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a cluded in. **
e5060 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b the dotlockLock
e5070 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20 20 20 20 ingContext .
e5080 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4c 6f */. char *zLo
e5090 63 6b 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 20 ckFile;. int
e50a0 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 6e nFilename;. n
e50b0 46 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 Filename = (int)
e50c0 73 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 strlen(zFilename
e50d0 29 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b ) + 6;. zLock
e50e0 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 File = (char *)s
e50f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46 qlite3_malloc(nF
e5100 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 ilename);. if
e5110 28 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29 ( zLockFile==0 )
e5120 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
e5130 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
e5140 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
e5150 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 te3_snprintf(nFi
e5160 6c 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c lename, zLockFil
e5170 65 2c 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f e, "%s" DOTLOCK_
e5180 53 55 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d SUFFIX, zFilenam
e5190 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e e);. }. pN
e51a0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ew->lockingConte
e51b0 78 74 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a xt = zLockFile;.
e51c0 20 20 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f }..#if OS_VXWO
e51d0 52 4b 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70 RKS. else if( p
e51e0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 LockingStyle ==
e51f0 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b &semIoMethods ){
e5200 0a 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 . /* Named se
e5210 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 maphore locking
e5220 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
e5230 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
e5240 6f 20 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c o be. ** incl
e5250 75 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c uded in the semL
e5260 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20 ockingContext.
e5270 20 20 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74 */. unixEnt
e5280 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 erMutex();. r
e5290 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f c = findLockInfo
e52a0 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 4c (pNew, &pNew->pL
e52b0 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 65 ock, &pNew->pOpe
e52c0 6e 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 3d n);. if( (rc=
e52d0 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 =SQLITE_OK) && (
e52e0 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 pNew->pOpen->pSe
e52f0 6d 3d 3d 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20 m==NULL) ){.
e5300 20 20 63 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 char *zSemName
e5310 20 3d 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e = pNew->pOpen->
e5320 61 53 65 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 aSemName;.
e5330 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 73 71 6c int n;. sql
e5340 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 ite3_snprintf(MA
e5350 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65 6d X_PATHNAME, zSem
e5360 4e 61 6d 65 2c 20 22 2f 25 73 2e 73 65 6d 22 2c Name, "/%s.sem",
e5370 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
e5380 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 49 pNew->pI
e5390 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d d->zCanonicalNam
e53a0 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 6e e);. for( n
e53b0 3d 31 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3b =1; zSemName[n];
e53c0 20 6e 2b 2b 20 29 0a 20 20 20 20 20 20 20 20 69 n++ ). i
e53d0 66 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d f( zSemName[n]==
e53e0 27 2f 27 20 29 20 7a 53 65 6d 4e 61 6d 65 5b 6e '/' ) zSemName[n
e53f0 5d 20 3d 20 27 5f 27 3b 0a 20 20 20 20 20 20 70 ] = '_';. p
e5400 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d New->pOpen->pSem
e5410 20 3d 20 73 65 6d 5f 6f 70 65 6e 28 7a 53 65 6d = sem_open(zSem
e5420 4e 61 6d 65 2c 20 4f 5f 43 52 45 41 54 2c 20 30 Name, O_CREAT, 0
e5430 36 36 36 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 666, 1);. i
e5440 66 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e f( pNew->pOpen->
e5450 70 53 65 6d 20 3d 3d 20 53 45 4d 5f 46 41 49 4c pSem == SEM_FAIL
e5460 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 ED ){. rc
e5470 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
e5480 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 . pNew->p
e5490 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b 30 Open->aSemName[0
e54a0 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 ] = '\0';.
e54b0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 }. }. unix
e54c0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
e54d0 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 70 4e }.#endif. . pN
e54e0 65 77 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 ew->lastErrno =
e54f0 30 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 0;.#if OS_VXWORK
e5500 53 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 S. if( rc!=SQLI
e5510 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 6c TE_OK ){. unl
e5520 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ink(zFilename);.
e5530 20 20 20 20 69 73 44 65 6c 65 74 65 20 3d 20 30 isDelete = 0
e5540 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 69 73 ;. }. pNew->is
e5550 44 65 6c 65 74 65 20 3d 20 69 73 44 65 6c 65 74 Delete = isDelet
e5560 65 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 e;.#endif. if(
e5570 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
e5580 0a 20 20 20 20 69 66 28 20 64 69 72 66 64 3e 3d . if( dirfd>=
e5590 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66 64 29 0 ) close(dirfd)
e55a0 3b 20 2f 2a 20 73 69 6c 65 6e 74 20 6c 65 61 6b ; /* silent leak
e55b0 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 61 64 if fail, alread
e55c0 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 y in error */.
e55d0 20 20 69 66 28 20 68 3e 3d 30 20 29 20 63 6c 6f if( h>=0 ) clo
e55e0 73 65 28 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a se(h);. }else{.
e55f0 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f pNew->pMetho
e5600 64 20 3d 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c d = pLockingStyl
e5610 65 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 e;. OpenCount
e5620 65 72 28 2b 31 29 3b 0a 20 20 7d 0a 20 20 72 65 er(+1);. }. re
e5630 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
e5640 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 20 64 65 * Open a file de
e5650 73 63 72 69 70 74 6f 72 20 74 6f 20 74 68 65 20 scriptor to the
e5660 64 69 72 65 63 74 6f 72 79 20 63 6f 6e 74 61 69 directory contai
e5670 6e 69 6e 67 20 66 69 6c 65 20 7a 46 69 6c 65 6e ning file zFilen
e5680 61 6d 65 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 ame..** If succe
e5690 73 73 66 75 6c 2c 20 2a 70 46 64 20 69 73 20 73 ssful, *pFd is s
e56a0 65 74 20 74 6f 20 74 68 65 20 6f 70 65 6e 65 64 et to the opened
e56b0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e56c0 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f and.** SQLITE_O
e56d0 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
e56e0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
e56f0 73 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 s, either SQLITE
e5700 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f 72 20 53 51 4c _NOMEM.** or SQL
e5710 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 69 73 20 ITE_CANTOPEN is
e5720 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 46 returned and *pF
e5730 64 20 69 73 20 73 65 74 20 74 6f 20 61 6e 20 75 d is set to an u
e5740 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 76 61 6c 75 ndefined.** valu
e5750 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 e..**.** If SQLI
e5760 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
e5770 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 d, the caller is
e5780 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
e5790 20 63 6c 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 closing.** the
e57a0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e57b0 2a 70 46 64 20 75 73 69 6e 67 20 63 6c 6f 73 65 *pFd using close
e57c0 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ()..*/.static in
e57d0 74 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 t openDirectory(
e57e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
e57f0 65 6e 61 6d 65 2c 20 69 6e 74 20 2a 70 46 64 29 ename, int *pFd)
e5800 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e {. int ii;. in
e5810 74 20 66 64 20 3d 20 2d 31 3b 0a 20 20 63 68 61 t fd = -1;. cha
e5820 72 20 7a 44 69 72 6e 61 6d 65 5b 4d 41 58 5f 50 r zDirname[MAX_P
e5830 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a 0a 20 20 73 ATHNAME+1];.. s
e5840 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
e5850 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 44 MAX_PATHNAME, zD
e5860 69 72 6e 61 6d 65 2c 20 22 25 73 22 2c 20 7a 46 irname, "%s", zF
e5870 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 ilename);. for(
e5880 69 69 3d 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a ii=(int)strlen(z
e5890 44 69 72 6e 61 6d 65 29 3b 20 69 69 3e 31 20 26 Dirname); ii>1 &
e58a0 26 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 21 3d & zDirname[ii]!=
e58b0 27 2f 27 3b 20 69 69 2d 2d 29 3b 0a 20 20 69 66 '/'; ii--);. if
e58c0 28 20 69 69 3e 30 20 29 7b 0a 20 20 20 20 7a 44 ( ii>0 ){. zD
e58d0 69 72 6e 61 6d 65 5b 69 69 5d 20 3d 20 27 5c 30 irname[ii] = '\0
e58e0 27 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e ';. fd = open
e58f0 28 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52 44 4f (zDirname, O_RDO
e5900 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20 30 29 NLY|O_BINARY, 0)
e5910 3b 0a 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 ;. if( fd>=0
e5920 29 7b 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f ){.#ifdef FD_CLO
e5930 45 58 45 43 0a 20 20 20 20 20 20 66 63 6e 74 6c EXEC. fcntl
e5940 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 (fd, F_SETFD, fc
e5950 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c ntl(fd, F_GETFD,
e5960 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 0) | FD_CLOEXEC
e5970 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
e5980 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 44 49 OSTRACE3("OPENDI
e5990 52 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 66 64 R %-3d %s\n", fd
e59a0 2c 20 7a 44 69 72 6e 61 6d 65 29 3b 0a 20 20 20 , zDirname);.
e59b0 20 7d 0a 20 20 7d 0a 20 20 2a 70 46 64 20 3d 20 }. }. *pFd =
e59c0 66 64 3b 0a 20 20 72 65 74 75 72 6e 20 28 66 64 fd;. return (fd
e59d0 3e 3d 30 3f 53 51 4c 49 54 45 5f 4f 4b 3a 53 51 >=0?SQLITE_OK:SQ
e59e0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 3b 0a LITE_CANTOPEN);.
e59f0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
e5a00 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
e5a10 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 name in zBuf.
e5a20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c zBuf must be all
e5a30 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 ocated.** by the
e5a40 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73 calling process
e5a50 20 61 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67 and must be big
e5a60 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
e5a70 61 74 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73 at least.** pVfs
e5a80 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 ->mxPathname byt
e5a90 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
e5aa0 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e t getTempname(in
e5ab0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
e5ac0 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f uf){. static co
e5ad0 6e 73 74 20 63 68 61 72 20 2a 61 7a 44 69 72 73 nst char *azDirs
e5ae0 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 30 2c 0a 20 [] = {. 0,.
e5af0 20 20 20 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 0,. "/va
e5b00 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 r/tmp",. "/u
e5b10 73 72 2f 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f sr/tmp",. "/
e5b20 74 6d 70 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a tmp",. ".",.
e5b30 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f };. static co
e5b40 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
e5b50 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 r zChars[] =.
e5b60 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e "abcdefghijklmn
e5b70 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 opqrstuvwxyz".
e5b80 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d "ABCDEFGHIJKLM
e5b90 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 NOPQRSTUVWXYZ".
e5ba0 20 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b "0123456789";
e5bb0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
e5bc0 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74 20 73 i, j;. struct s
e5bd0 74 61 74 20 62 75 66 3b 0a 20 20 63 6f 6e 73 74 tat buf;. const
e5be0 20 63 68 61 72 20 2a 7a 44 69 72 20 3d 20 22 2e char *zDir = ".
e5bf0 22 3b 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 ";.. /* It's od
e5c00 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e d to simulate an
e5c10 20 69 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 io-error here,
e5c20 62 75 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 but really this
e5c30 69 73 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 is just. ** usi
e5c40 6e 67 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 ng the io-error
e5c50 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 74 infrastructure t
e5c60 6f 20 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 o test that SQLi
e5c70 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a te handles this.
e5c80 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 ** function fa
e5c90 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 53 iling. . */. S
e5ca0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
e5cb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
e5cc0 45 52 52 20 29 3b 0a 0a 20 20 61 7a 44 69 72 73 ERR );.. azDirs
e5cd0 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 [0] = sqlite3_te
e5ce0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 mp_directory;.
e5cf0 69 66 20 28 4e 55 4c 4c 20 3d 3d 20 61 7a 44 69 if (NULL == azDi
e5d00 72 73 5b 31 5d 29 20 7b 0a 20 20 20 20 61 7a 44 rs[1]) {. azD
e5d10 69 72 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76 28 irs[1] = getenv(
e5d20 22 54 4d 50 44 49 52 22 29 3b 0a 20 20 7d 0a 20 "TMPDIR");. }.
e5d30 20 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 . for(i=0; i<s
e5d40 69 7a 65 6f 66 28 61 7a 44 69 72 73 29 2f 73 69 izeof(azDirs)/si
e5d50 7a 65 6f 66 28 61 7a 44 69 72 73 5b 30 5d 29 3b zeof(azDirs[0]);
e5d60 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 i++){. if( a
e5d70 7a 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63 6f zDirs[i]==0 ) co
e5d80 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 ntinue;. if(
e5d90 73 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c 20 stat(azDirs[i],
e5da0 26 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 &buf) ) continue
e5db0 3b 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 ;. if( !S_ISD
e5dc0 49 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 IR(buf.st_mode)
e5dd0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
e5de0 69 66 28 20 61 63 63 65 73 73 28 61 7a 44 69 72 if( access(azDir
e5df0 73 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e 74 s[i], 07) ) cont
e5e00 69 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20 3d inue;. zDir =
e5e10 20 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20 20 azDirs[i];.
e5e20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a break;. }.. /*
e5e30 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
e5e40 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 output buffer is
e5e50 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f large enough fo
e5e60 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 r the temporary
e5e70 66 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e file . ** name.
e5e80 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72 If it is not, r
e5e90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
e5ea0 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 OR.. */. if( (
e5eb0 73 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73 strlen(zDir) + s
e5ec0 74 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d trlen(SQLITE_TEM
e5ed0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b P_FILE_PREFIX) +
e5ee0 20 31 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29 17) >= (size_t)
e5ef0 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 nBuf ){. retu
e5f00 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
e5f10 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 . }.. do{.
e5f20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
e5f30 28 6e 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c 20 (nBuf-17, zBuf,
e5f40 22 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50 "%s/"SQLITE_TEMP
e5f50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44 _FILE_PREFIX, zD
e5f60 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e ir);. j = (in
e5f70 74 29 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a t)strlen(zBuf);.
e5f80 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 sqlite3_rand
e5f90 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 omness(15, &zBuf
e5fa0 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d [j]);. for(i=
e5fb0 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 0; i<15; i++, j+
e5fc0 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a +){. zBuf[j
e5fd0 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 ] = (char)zChars
e5fe0 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 [ ((unsigned cha
e5ff0 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 r)zBuf[j])%(size
e6000 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b of(zChars)-1) ];
e6010 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b . }. zBuf[
e6020 6a 5d 20 3d 20 30 3b 0a 20 20 7d 77 68 69 6c 65 j] = 0;. }while
e6030 28 20 61 63 63 65 73 73 28 7a 42 75 66 2c 30 29 ( access(zBuf,0)
e6040 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==0 );. return
e6050 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 SQLITE_OK;.}..#i
e6060 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e6070 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
e6080 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
e6090 5f 5f 29 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e __)./*.** Routin
e60a0 65 20 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 61 e to transform a
e60b0 20 75 6e 69 78 46 69 6c 65 20 69 6e 74 6f 20 61 unixFile into a
e60c0 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 75 proxy-locking u
e60d0 6e 69 78 46 69 6c 65 2e 0a 2a 2a 20 49 6d 70 6c nixFile..** Impl
e60e0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 ementation in th
e60f0 65 20 70 72 6f 78 79 2d 6c 6f 63 6b 20 64 69 76 e proxy-lock div
e6100 69 73 69 6f 6e 2c 20 62 75 74 20 75 73 65 64 20 ision, but used
e6110 62 79 20 75 6e 69 78 4f 70 65 6e 28 29 0a 2a 2a by unixOpen().**
e6120 20 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 if SQLITE_PREFE
e6130 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 R_PROXY_LOCKING
e6140 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 is defined..*/.s
e6150 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 tatic int proxyT
e6160 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 ransformUnixFile
e6170 28 75 6e 69 78 46 69 6c 65 2a 2c 20 63 6f 6e 73 (unixFile*, cons
e6180 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 t char*);.#endif
e6190 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 ../*.** Search f
e61a0 6f 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c or an unused fil
e61b0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 e descriptor tha
e61c0 74 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 t was opened on
e61d0 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a the database .**
e61e0 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 file (not a jou
e61f0 72 6e 61 6c 20 6f 72 20 6d 61 73 74 65 72 2d 6a rnal or master-j
e6200 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 69 64 65 ournal file) ide
e6210 6e 74 69 66 69 65 64 20 62 79 20 70 61 74 68 6e ntified by pathn
e6220 61 6d 65 0a 2a 2a 20 7a 50 61 74 68 20 77 69 74 ame.** zPath wit
e6230 68 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 58 58 h SQLITE_OPEN_XX
e6240 58 20 66 6c 61 67 73 20 6d 61 74 63 68 69 6e 67 X flags matching
e6250 20 74 68 6f 73 65 20 70 61 73 73 65 64 20 61 73 those passed as
e6260 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 the second.** a
e6270 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 rgument to this
e6280 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
e6290 53 75 63 68 20 61 20 66 69 6c 65 20 64 65 73 63 Such a file desc
e62a0 72 69 70 74 6f 72 20 6d 61 79 20 65 78 69 73 74 riptor may exist
e62b0 20 69 66 20 61 20 64 61 74 61 62 61 73 65 20 63 if a database c
e62c0 6f 6e 6e 65 63 74 69 6f 6e 20 77 61 73 20 63 6c onnection was cl
e62d0 6f 73 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 osed.** but the
e62e0 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 20 associated file
e62f0 64 65 73 63 72 69 70 74 6f 72 20 63 6f 75 6c 64 descriptor could
e6300 20 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 62 not be closed b
e6310 65 63 61 75 73 65 20 73 6f 6d 65 0a 2a 2a 20 6f ecause some.** o
e6320 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 ther file descri
e6330 70 74 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 ptor open on the
e6340 20 73 61 6d 65 20 66 69 6c 65 20 69 73 20 68 6f same file is ho
e6350 6c 64 69 6e 67 20 61 20 66 69 6c 65 2d 6c 6f 63 lding a file-loc
e6360 6b 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 k..** Refer to c
e6370 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 75 omments in the u
e6380 6e 69 78 43 6c 6f 73 65 28 29 20 66 75 6e 63 74 nixClose() funct
e6390 69 6f 6e 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 ion and the leng
e63a0 74 68 79 20 63 6f 6d 6d 65 6e 74 0a 2a 2a 20 64 thy comment.** d
e63b0 65 73 63 72 69 62 69 6e 67 20 22 50 6f 73 69 78 escribing "Posix
e63c0 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 69 6e Advisory Lockin
e63d0 67 22 20 61 74 20 74 68 65 20 73 74 61 72 74 20 g" at the start
e63e0 6f 66 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 of this file for
e63f0 20 0a 2a 2a 20 66 75 72 74 68 65 72 20 64 65 74 .** further det
e6400 61 69 6c 73 2e 20 41 6c 73 6f 2c 20 74 69 63 6b ails. Also, tick
e6410 65 74 20 23 34 30 31 38 2e 0a 2a 2a 0a 2a 2a 20 et #4018..**.**
e6420 49 66 20 61 20 73 75 69 74 61 62 6c 65 20 66 69 If a suitable fi
e6430 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
e6440 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 69 74 20 found, then it
e6450 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
e6460 6e 6f 0a 2a 2a 20 73 75 63 68 20 66 69 6c 65 20 no.** such file
e6470 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 6c 6f descriptor is lo
e6480 63 61 74 65 64 2c 20 2d 31 20 69 73 20 72 65 74 cated, -1 is ret
e6490 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
e64a0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 66 UnixUnusedFd *f
e64b0 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28 63 6f indReusableFd(co
e64c0 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c nst char *zPath,
e64d0 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 55 int flags){. U
e64e0 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e nixUnusedFd *pUn
e64f0 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 used = 0;.. /*
e6500 44 6f 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f Do not search fo
e6510 72 20 61 6e 20 75 6e 75 73 65 64 20 66 69 6c 65 r an unused file
e6520 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 76 descriptor on v
e6530 78 77 6f 72 6b 73 2e 20 4e 6f 74 20 62 65 63 61 xworks. Not beca
e6540 75 73 65 0a 20 20 2a 2a 20 76 78 77 6f 72 6b 73 use. ** vxworks
e6550 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 6e 65 66 would not benef
e6560 69 74 20 66 72 6f 6d 20 74 68 65 20 63 68 61 6e it from the chan
e6570 67 65 20 28 69 74 20 6d 69 67 68 74 2c 20 77 65 ge (it might, we
e6580 27 72 65 20 6e 6f 74 20 73 75 72 65 29 2c 0a 20 're not sure),.
e6590 20 2a 2a 20 62 75 74 20 62 65 63 61 75 73 65 20 ** but because
e65a0 6e 6f 20 77 61 79 20 74 6f 20 74 65 73 74 20 69 no way to test i
e65b0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 t is currently a
e65c0 76 61 69 6c 61 62 6c 65 2e 20 49 74 20 69 73 20 vailable. It is
e65d0 62 65 74 74 65 72 20 0a 20 20 2a 2a 20 6e 6f 74 better . ** not
e65e0 20 74 6f 20 72 69 73 6b 20 62 72 65 61 6b 69 6e to risk breakin
e65f0 67 20 76 78 77 6f 72 6b 73 20 73 75 70 70 6f 72 g vxworks suppor
e6600 74 20 66 6f 72 20 74 68 65 20 73 61 6b 65 20 6f t for the sake o
e6610 66 20 73 75 63 68 20 61 6e 20 6f 62 73 63 75 72 f such an obscur
e6620 65 20 0a 20 20 2a 2a 20 66 65 61 74 75 72 65 2e e . ** feature.
e6630 20 20 2a 2f 0a 23 69 66 20 21 4f 53 5f 56 58 57 */.#if !OS_VXW
e6640 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 73 74 ORKS. struct st
e6650 61 74 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 at sStat;
e6660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
e6670 65 73 75 6c 74 73 20 6f 66 20 73 74 61 74 28 29 esults of stat()
e6680 20 63 61 6c 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 41 call */.. /* A
e6690 20 73 74 61 74 28 29 20 63 61 6c 6c 20 6d 61 79 stat() call may
e66a0 20 66 61 69 6c 20 66 6f 72 20 76 61 72 69 6f 75 fail for variou
e66b0 73 20 72 65 61 73 6f 6e 73 2e 20 49 66 20 74 68 s reasons. If th
e66c0 69 73 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 is happens, it i
e66d0 73 0a 20 20 2a 2a 20 61 6c 6d 6f 73 74 20 63 65 s. ** almost ce
e66e0 72 74 61 69 6e 20 74 68 61 74 20 61 6e 20 6f 70 rtain that an op
e66f0 65 6e 28 29 20 63 61 6c 6c 20 6f 6e 20 74 68 65 en() call on the
e6700 20 73 61 6d 65 20 70 61 74 68 20 77 69 6c 6c 20 same path will
e6710 61 6c 73 6f 20 66 61 69 6c 2e 0a 20 20 2a 2a 20 also fail.. **
e6720 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c For this reason,
e6730 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
e6740 75 72 73 20 69 6e 20 74 68 65 20 73 74 61 74 28 urs in the stat(
e6750 29 20 63 61 6c 6c 20 68 65 72 65 2c 20 69 74 20 ) call here, it
e6760 69 73 0a 20 20 2a 2a 20 69 67 6e 6f 72 65 64 20 is. ** ignored
e6770 61 6e 64 20 2d 31 20 69 73 20 72 65 74 75 72 6e and -1 is return
e6780 65 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 ed. The caller w
e6790 69 6c 6c 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 ill try to open
e67a0 61 20 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 a new file. **
e67b0 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 68 descriptor on th
e67c0 65 20 73 61 6d 65 20 70 61 74 68 2c 20 66 61 69 e same path, fai
e67d0 6c 2c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e l, and return an
e67e0 20 65 72 72 6f 72 20 74 6f 20 53 51 4c 69 74 65 error to SQLite
e67f0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 76 65 6e .. **. ** Even
e6800 20 69 66 20 61 20 73 75 62 73 65 71 75 65 6e 74 if a subsequent
e6810 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20 64 6f 65 open() call doe
e6820 73 20 73 75 63 63 65 65 64 2c 20 74 68 65 20 63 s succeed, the c
e6830 6f 6e 73 65 71 75 65 6e 63 65 73 20 6f 66 0a 20 onsequences of.
e6840 20 2a 2a 20 6e 6f 74 20 73 65 61 72 63 68 69 6e ** not searchin
e6850 67 20 66 6f 72 20 61 20 72 65 73 75 73 61 62 6c g for a resusabl
e6860 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
e6870 72 20 61 72 65 20 6e 6f 74 20 64 69 72 65 2e 20 r are not dire.
e6880 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 74 61 */. if( 0==sta
e6890 74 28 7a 50 61 74 68 2c 20 26 73 53 74 61 74 29 t(zPath, &sStat)
e68a0 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 ){. struct u
e68b0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 3b 0a nixOpenCnt *pO;.
e68c0 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 struct unixF
e68d0 69 6c 65 49 64 20 69 64 3b 0a 20 20 20 20 69 64 ileId id;. id
e68e0 2e 64 65 76 20 3d 20 73 53 74 61 74 2e 73 74 5f .dev = sStat.st_
e68f0 64 65 76 3b 0a 20 20 20 20 69 64 2e 69 6e 6f 20 dev;. id.ino
e6900 3d 20 73 53 74 61 74 2e 73 74 5f 69 6e 6f 3b 0a = sStat.st_ino;.
e6910 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 . unixEnterMu
e6920 74 65 78 28 29 3b 0a 20 20 20 20 66 6f 72 28 70 tex();. for(p
e6930 4f 3d 6f 70 65 6e 4c 69 73 74 3b 20 70 4f 20 26 O=openList; pO &
e6940 26 20 6d 65 6d 63 6d 70 28 26 69 64 2c 20 26 70 & memcmp(&id, &p
e6950 4f 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f O->fileId, sizeo
e6960 66 28 69 64 29 29 3b 20 70 4f 3d 70 4f 2d 3e 70 f(id)); pO=pO->p
e6970 4e 65 78 74 29 3b 0a 20 20 20 20 69 66 28 20 70 Next);. if( p
e6980 4f 20 29 7b 0a 20 20 20 20 20 20 55 6e 69 78 55 O ){. UnixU
e6990 6e 75 73 65 64 46 64 20 2a 2a 70 70 3b 0a 20 20 nusedFd **pp;.
e69a0 20 20 20 20 66 6f 72 28 70 70 3d 26 70 4f 2d 3e for(pp=&pO->
e69b0 70 55 6e 75 73 65 64 3b 20 2a 70 70 20 26 26 20 pUnused; *pp &&
e69c0 28 2a 70 70 29 2d 3e 66 6c 61 67 73 21 3d 66 6c (*pp)->flags!=fl
e69d0 61 67 73 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d ags; pp=&((*pp)-
e69e0 3e 70 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 >pNext));.
e69f0 70 55 6e 75 73 65 64 20 3d 20 2a 70 70 3b 0a 20 pUnused = *pp;.
e6a00 20 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 if( pUnused
e6a10 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 ){. *pp
e6a20 3d 20 70 55 6e 75 73 65 64 2d 3e 70 4e 65 78 74 = pUnused->pNext
e6a30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
e6a40 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 unixLeaveMut
e6a50 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 ex();. }.#endif
e6a60 20 20 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58 /* if !OS_VX
e6a70 57 4f 52 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 WORKS */. retur
e6a80 6e 20 70 55 6e 75 73 65 64 3b 0a 7d 0a 0a 2f 2a n pUnused;.}../*
e6a90 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c .** Open the fil
e6aa0 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 e zPath..** .**
e6ab0 50 72 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 Previously, the
e6ac0 53 51 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20 SQLite OS layer
e6ad0 75 73 65 64 20 74 68 72 65 65 20 66 75 6e 63 74 used three funct
e6ae0 69 6f 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 ions in place of
e6af0 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a this.** one:.**
e6b00 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f .** sqlite3O
e6b10 73 4f 70 65 6e 52 65 61 64 57 72 69 74 65 28 29 sOpenReadWrite()
e6b20 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ;.** sqlite3
e6b30 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 OsOpenReadOnly()
e6b40 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ;.** sqlite3
e6b50 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 OsOpenExclusive(
e6b60 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 );.**.** These c
e6b70 61 6c 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 alls correspond
e6b80 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
e6b90 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 combinations of
e6ba0 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 flags:.**.**
e6bb0 20 20 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e ReadWrite() ->
e6bc0 20 20 20 20 20 28 52 45 41 44 57 52 49 54 45 20 (READWRITE
e6bd0 7c 20 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 | CREATE).**
e6be0 20 52 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 ReadOnly() ->
e6bf0 20 20 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a (READONLY) .
e6c00 2a 2a 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 ** OpenExclu
e6c10 73 69 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57 sive() -> (READW
e6c20 52 49 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20 RITE | CREATE |
e6c30 45 58 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a EXCLUSIVE).**.**
e6c40 20 54 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 The old OpenExc
e6c50 6c 75 73 69 76 65 28 29 20 61 63 63 65 70 74 65 lusive() accepte
e6c60 64 20 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 d a boolean argu
e6c70 6d 65 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 ment - "delFlag"
e6c80 2e 20 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 . If.** true, th
e6c90 65 20 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 e file was confi
e6ca0 67 75 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f gured to be auto
e6cb0 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 matically delete
e6cc0 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 d when the.** fi
e6cd0 6c 65 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 le handle closed
e6ce0 2e 20 54 6f 20 61 63 68 69 65 76 65 20 74 68 65 . To achieve the
e6cf0 20 73 61 6d 65 20 65 66 66 65 63 74 20 75 73 69 same effect usi
e6d00 6e 67 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 ng this new .**
e6d10 69 6e 74 65 72 66 61 63 65 2c 20 61 64 64 20 74 interface, add t
e6d20 68 65 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 he DELETEONCLOSE
e6d30 20 66 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73 flag to those s
e6d40 70 65 63 69 66 69 65 64 20 61 62 6f 76 65 20 66 pecified above f
e6d50 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 or .** OpenExclu
e6d60 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 sive()..*/.stati
e6d70 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a c int unixOpen(.
e6d80 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
e6d90 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f Vfs, /
e6da0 2a 20 54 68 65 20 56 46 53 20 66 6f 72 20 77 68 * The VFS for wh
e6db0 69 63 68 20 74 68 69 73 20 69 73 20 74 68 65 20 ich this is the
e6dc0 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a xOpen method */.
e6dd0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
e6de0 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f ath, /
e6df0 2a 20 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69 * Pathname of fi
e6e00 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 le to be opened
e6e10 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
e6e20 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 e *pFile,
e6e30 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
e6e40 73 63 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66 scriptor to be f
e6e50 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e illed in */. in
e6e60 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
e6e70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
e6e80 70 75 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e put flags to con
e6e90 74 72 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67 trol the opening
e6ea0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 */. int *pOutF
e6eb0 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 lags
e6ec0 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61 /* Output fla
e6ed0 67 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 gs returned to S
e6ee0 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b QLite core */.){
e6ef0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 20 3d . unixFile *p =
e6f00 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 46 69 (unixFile *)pFi
e6f10 6c 65 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d le;. int fd = -
e6f20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
e6f30 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 /* File des
e6f40 63 72 69 70 74 6f 72 20 72 65 74 75 72 6e 65 64 criptor returned
e6f50 20 62 79 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 by open() */.
e6f60 69 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 int dirfd = -1;
e6f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e6f80 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 * Directory file
e6f90 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
e6fa0 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d int openFlags =
e6fb0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
e6fc0 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73 /* Flags to pass
e6fd0 20 74 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 to open() */.
e6fe0 69 6e 74 20 65 54 79 70 65 20 3d 20 66 6c 61 67 int eType = flag
e6ff0 73 26 30 78 46 46 46 46 46 46 30 30 3b 20 20 2f s&0xFFFFFF00; /
e7000 2a 20 54 79 70 65 20 6f 66 20 66 69 6c 65 20 74 * Type of file t
e7010 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
e7020 6e 6f 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 noLock;
e7030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
e7040 75 65 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 ue to omit locki
e7050 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 2a 2f ng primitives */
e7060 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
e7070 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 TE_OK;
e7080 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 52 65 /* Function Re
e7090 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20 turn Code */..
e70a0 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 int isExclusive
e70b0 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 = (flags & SQLI
e70c0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
e70d0 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 E);. int isDele
e70e0 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 te = (flags
e70f0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 & SQLITE_OPEN_DE
e7100 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 LETEONCLOSE);.
e7110 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20 int isCreate
e7120 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 = (flags & SQLI
e7130 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b TE_OPEN_CREATE);
e7140 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c . int isReadonl
e7150 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 y = (flags & S
e7160 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
e7170 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 NLY);. int isRe
e7180 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67 adWrite = (flag
e7190 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
e71a0 52 45 41 44 57 52 49 54 45 29 3b 0a 0a 20 20 2f READWRITE);.. /
e71b0 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20 * If creating a
e71c0 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66 master or main-f
e71d0 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69 ile journal, thi
e71e0 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 s function will
e71f0 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65 open. ** a file
e7200 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74 -descriptor on t
e7210 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f he directory too
e7220 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 . The first time
e7230 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a unixSync(). **
e7240 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64 is called the d
e7250 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 irectory file de
e7260 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 scriptor will be
e7270 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63 fsync()ed and c
e7280 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20 lose()d.. */.
e7290 69 6e 74 20 69 73 4f 70 65 6e 44 69 72 65 63 74 int isOpenDirect
e72a0 6f 72 79 20 3d 20 28 69 73 43 72 65 61 74 65 20 ory = (isCreate
e72b0 26 26 20 0a 20 20 20 20 20 20 28 65 54 79 70 65 && . (eType
e72c0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
e72d0 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 STER_JOURNAL ||
e72e0 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e72f0 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 EN_MAIN_JOURNAL)
e7300 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 . );.. /* If a
e7310 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 rgument zPath is
e7320 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
e7330 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
e7340 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 s required to op
e7350 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 en. ** a tempor
e7360 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 ary file. Use th
e7370 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f is buffer to sto
e7380 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 re the file name
e7390 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 in.. */. char
e73a0 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 zTmpname[MAX_PA
e73b0 54 48 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f 6e THNAME+1];. con
e73c0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d st char *zName =
e73d0 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68 zPath;.. /* Ch
e73e0 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e eck the followin
e73f0 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 g statements are
e7400 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a true: . **. *
e7410 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 * (a) Exactly
e7420 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 one of the READW
e7430 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c RITE and READONL
e7440 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 Y flags must be
e7450 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 set, and . **
e7460 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69 (b) if CREATE i
e7470 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 s set, then READ
e7480 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 WRITE must also
e7490 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a be set, and. **
e74a0 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 (c) if EXCLUS
e74b0 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e IVE is set, then
e74c0 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 CREATE must als
e74d0 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 o be set.. **
e74e0 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e (d) if DELETEON
e74f0 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 CLOSE is set, th
e7500 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 en CREATE must a
e7510 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f lso be set.. */
e7520 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 61 . assert((isRea
e7530 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 donly==0 || isRe
e7540 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 adWrite==0) && (
e7550 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 isReadWrite || i
e7560 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 sReadonly));. a
e7570 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d ssert(isCreate==
e7580 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 0 || isReadWrite
e7590 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 );. assert(isEx
e75a0 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 clusive==0 || is
e75b0 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 Create);. asser
e75c0 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c t(isDelete==0 ||
e75d0 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f isCreate);.. /
e75e0 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d * The main DB, m
e75f0 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e 64 ain journal, and
e7600 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
e7610 61 72 65 20 6e 65 76 65 72 20 61 75 74 6f 6d 61 are never automa
e7620 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65 6c tically. ** del
e7630 65 74 65 64 2e 20 4e 6f 72 20 61 72 65 20 74 68 eted. Nor are th
e7640 65 79 20 65 76 65 72 20 74 65 6d 70 6f 72 61 72 ey ever temporar
e7650 79 20 66 69 6c 65 73 2e 20 20 2a 2f 0a 20 20 61 y files. */. a
e7660 73 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 ssert( (!isDelet
e7670 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 e && zName) || e
e7680 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 Type!=SQLITE_OPE
e7690 4e 5f 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 20 61 N_MAIN_DB );. a
e76a0 73 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 ssert( (!isDelet
e76b0 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 e && zName) || e
e76c0 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 Type!=SQLITE_OPE
e76d0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 N_MAIN_JOURNAL )
e76e0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 73 ;. assert( (!is
e76f0 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 Delete && zName)
e7700 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 || eType!=SQLIT
e7710 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
e7720 55 52 4e 41 4c 20 29 3b 0a 0a 20 20 2f 2a 20 41 URNAL );.. /* A
e7730 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 75 ssert that the u
e7740 70 70 65 72 20 6c 61 79 65 72 20 68 61 73 20 73 pper layer has s
e7750 65 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 et one of the "f
e7760 69 6c 65 2d 74 79 70 65 22 20 66 6c 61 67 73 2e ile-type" flags.
e7770 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 */. assert( eT
e7780 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e7790 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c _MAIN_DB ||
e77a0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
e77b0 50 45 4e 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20 PEN_TEMP_DB .
e77c0 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 || eType==SQ
e77d0 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
e77e0 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d OURNAL || eType=
e77f0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d =SQLITE_OPEN_TEM
e7800 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 P_JOURNAL .
e7810 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 || eType==SQLI
e7820 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e TE_OPEN_SUBJOURN
e7830 41 4c 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 AL || eType==S
e7840 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 QLITE_OPEN_MASTE
e7850 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 R_JOURNAL .
e7860 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 || eType==SQLI
e7870 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e TE_OPEN_TRANSIEN
e7880 54 5f 44 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d T_DB. );.. mem
e7890 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 set(p, 0, sizeof
e78a0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 (unixFile));..
e78b0 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 if( eType==SQLIT
e78c0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 E_OPEN_MAIN_DB )
e78d0 7b 0a 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 {. UnixUnused
e78e0 46 64 20 2a 70 55 6e 75 73 65 64 3b 0a 20 20 20 Fd *pUnused;.
e78f0 20 70 55 6e 75 73 65 64 20 3d 20 66 69 6e 64 52 pUnused = findR
e7900 65 75 73 61 62 6c 65 46 64 28 7a 4e 61 6d 65 2c eusableFd(zName,
e7910 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 flags);. if(
e7920 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 pUnused ){.
e7930 20 20 66 64 20 3d 20 70 55 6e 75 73 65 64 2d 3e fd = pUnused->
e7940 66 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 fd;. }else{.
e7950 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20 73 pUnused = s
e7960 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
e7970 7a 65 6f 66 28 2a 70 55 6e 75 73 65 64 29 29 3b zeof(*pUnused));
e7980 0a 20 20 20 20 20 20 69 66 28 20 21 70 55 6e 75 . if( !pUnu
e7990 73 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 sed ){. r
e79a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
e79b0 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 EM;. }.
e79c0 7d 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 }. p->pUnused
e79d0 20 3d 20 70 55 6e 75 73 65 64 3b 0a 20 20 7d 65 = pUnused;. }e
e79e0 6c 73 65 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 lse if( !zName )
e79f0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d {. /* If zNam
e7a00 65 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 75 e is NULL, the u
e7a10 70 70 65 72 20 6c 61 79 65 72 20 69 73 20 72 65 pper layer is re
e7a20 71 75 65 73 74 69 6e 67 20 61 20 74 65 6d 70 20 questing a temp
e7a30 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 file. */. ass
e7a40 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26 20 ert(isDelete &&
e7a50 21 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 !isOpenDirectory
e7a60 29 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54 );. rc = getT
e7a70 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 empname(MAX_PATH
e7a80 4e 41 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 NAME+1, zTmpname
e7a90 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
e7aa0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e7ab0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
e7ac0 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a }. zName = z
e7ad0 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 Tmpname;. }..
e7ae0 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 /* Determine the
e7af0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c value of the fl
e7b00 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 ags parameter pa
e7b10 73 73 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 ssed to POSIX fu
e7b20 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e nction. ** open
e7b30 28 29 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62 (). These must b
e7b40 65 20 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65 e calculated eve
e7b50 6e 20 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e n if open() is n
e7b60 6f 74 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20 ot called, as.
e7b70 2a 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20 73 ** they may be s
e7b80 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 tored as part of
e7b90 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 the file handle
e7ba0 20 61 6e 64 20 75 73 65 64 20 62 79 20 74 68 65 and used by the
e7bb0 20 0a 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69 . ** 'conch fi
e7bc0 6c 65 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63 le' locking func
e7bd0 74 69 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20 tions later on.
e7be0 20 2a 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64 */. if( isRead
e7bf0 6f 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 only ) openFlag
e7c00 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 s |= O_RDONLY;.
e7c10 20 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 if( isReadWrite
e7c20 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 ) openFlags |=
e7c30 4f 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 O_RDWR;. if( is
e7c40 43 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e Create ) open
e7c50 46 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 Flags |= O_CREAT
e7c60 3b 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 ;. if( isExclus
e7c70 69 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 ive ) openFlags
e7c80 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 |= (O_EXCL|O_NOF
e7c90 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c OLLOW);. openFl
e7ca0 61 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 ags |= (O_LARGEF
e7cb0 49 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a ILE|O_BINARY);..
e7cc0 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
e7cd0 20 20 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64 mode_t openMod
e7ce0 65 20 3d 20 28 69 73 44 65 6c 65 74 65 3f 30 36 e = (isDelete?06
e7cf0 30 30 3a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 00:SQLITE_DEFAUL
e7d00 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f T_FILE_PERMISSIO
e7d10 4e 53 29 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 NS);. fd = op
e7d20 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c en(zName, openFl
e7d30 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a ags, openMode);.
e7d40 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 4f 50 OSTRACE4("OP
e7d50 45 4e 58 20 20 20 25 2d 33 64 20 25 73 20 30 25 ENX %-3d %s 0%
e7d60 6f 5c 6e 22 2c 20 66 64 2c 20 7a 4e 61 6d 65 2c o\n", fd, zName,
e7d70 20 6f 70 65 6e 46 6c 61 67 73 29 3b 0a 20 20 20 openFlags);.
e7d80 20 69 66 28 20 66 64 3c 30 20 26 26 20 65 72 72 if( fd<0 && err
e7d90 6e 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69 73 no!=EISDIR && is
e7da0 52 65 61 64 57 72 69 74 65 20 26 26 20 21 69 73 ReadWrite && !is
e7db0 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 Exclusive ){.
e7dc0 20 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 /* Failed to
e7dd0 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f open the file fo
e7de0 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 r read/write acc
e7df0 65 73 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e ess. Try read-on
e7e00 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6c 61 ly. */. fla
e7e10 67 73 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f gs &= ~(SQLITE_O
e7e20 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
e7e30 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
e7e40 29 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 );. openFla
e7e50 67 73 20 26 3d 20 7e 28 4f 5f 52 44 57 52 7c 4f gs &= ~(O_RDWR|O
e7e60 5f 43 52 45 41 54 29 3b 0a 20 20 20 20 20 20 66 _CREAT);. f
e7e70 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f lags |= SQLITE_O
e7e80 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 PEN_READONLY;.
e7e90 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d openFlags |=
e7ea0 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 O_RDONLY;.
e7eb0 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 fd = open(zName
e7ec0 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 , openFlags, ope
e7ed0 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 nMode);. }.
e7ee0 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
e7ef0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e7f00 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 CANTOPEN;.
e7f10 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 goto open_finish
e7f20 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ed;. }. }.
e7f30 61 73 73 65 72 74 28 20 66 64 3e 3d 30 20 29 3b assert( fd>=0 );
e7f40 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 . if( pOutFlags
e7f50 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 ){. *pOutFla
e7f60 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d 0a gs = flags;. }.
e7f70 0a 20 20 69 66 28 20 70 2d 3e 70 55 6e 75 73 65 . if( p->pUnuse
e7f80 64 20 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 d ){. p->pUnu
e7f90 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20 20 sed->fd = fd;.
e7fa0 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 6c p->pUnused->fl
e7fb0 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d ags = flags;. }
e7fc0 0a 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 .. if( isDelete
e7fd0 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 ){.#if OS_VXWOR
e7fe0 4b 53 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a KS. zPath = z
e7ff0 4e 61 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 Name;.#else.
e8000 75 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 unlink(zName);.#
e8010 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66 20 53 51 endif. }.#if SQ
e8020 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
e8030 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65 6c 73 65 ING_STYLE. else
e8040 7b 0a 20 20 20 20 70 2d 3e 6f 70 65 6e 46 6c 61 {. p->openFla
e8050 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a gs = openFlags;.
e8060 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 }.#endif.. if
e8070 28 20 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 ( isOpenDirector
e8080 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 y ){. rc = op
e8090 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 enDirectory(zPat
e80a0 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 h, &dirfd);.
e80b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
e80c0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 K ){. /* It
e80d0 20 69 73 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 is safe to clos
e80e0 65 20 66 64 20 61 74 20 74 68 69 73 20 70 6f 69 e fd at this poi
e80f0 6e 74 2c 20 62 65 63 61 75 73 65 20 69 74 20 69 nt, because it i
e8100 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 s guaranteed not
e8110 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 . ** to be
e8120 6f 70 65 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 open on a databa
e8130 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20 77 se file. If it w
e8140 65 72 65 20 6f 70 65 6e 20 6f 6e 20 61 20 64 61 ere open on a da
e8150 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 20 20 20 tabase file,.
e8160 20 20 20 2a 2a 20 69 74 20 77 6f 75 6c 64 20 6e ** it would n
e8170 6f 74 20 62 65 20 73 61 66 65 20 74 6f 20 63 6c ot be safe to cl
e8180 6f 73 65 20 61 73 20 74 68 69 73 20 77 6f 75 6c ose as this woul
e8190 64 20 72 65 6c 65 61 73 65 20 61 6e 79 20 6c 6f d release any lo
e81a0 63 6b 73 20 68 65 6c 64 0a 20 20 20 20 20 20 2a cks held. *
e81b0 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 62 79 * on the file by
e81c0 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 20 20 this process.
e81d0 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
e81e0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f eType!=SQLITE_O
e81f0 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29 3b 0a 20 PEN_MAIN_DB );.
e8200 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 close(fd);
e8210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
e8220 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 ilently leak if
e8230 66 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e fail, already in
e8240 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 error */.
e8250 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 goto open_finish
e8260 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 ed;. }. }..#
e8270 69 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 ifdef FD_CLOEXEC
e8280 0a 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 . fcntl(fd, F_S
e8290 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 ETFD, fcntl(fd,
e82a0 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 F_GETFD, 0) | FD
e82b0 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 _CLOEXEC);.#endi
e82c0 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 f.. noLock = eT
e82d0 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype!=SQLITE_OPEN
e82e0 5f 4d 41 49 4e 5f 44 42 3b 0a 0a 23 69 66 20 53 _MAIN_DB;..#if S
e82f0 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f QLITE_PREFER_PRO
e8300 58 59 5f 4c 4f 43 4b 49 4e 47 0a 20 20 69 66 28 XY_LOCKING. if(
e8310 20 7a 50 61 74 68 21 3d 4e 55 4c 4c 20 26 26 20 zPath!=NULL &&
e8320 21 6e 6f 4c 6f 63 6b 20 26 26 20 70 56 66 73 2d !noLock && pVfs-
e8330 3e 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 63 68 >xOpen ){. ch
e8340 61 72 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 67 ar *envforce = g
e8350 65 74 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 4f etenv("SQLITE_FO
e8360 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e RCE_PROXY_LOCKIN
e8370 47 22 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 65 G");. int use
e8380 50 72 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 20 Proxy = 0;..
e8390 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f /* SQLITE_FORCE_
e83a0 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 PROXY_LOCKING==1
e83b0 20 6d 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 77 means force alw
e83c0 61 79 73 20 75 73 65 20 70 72 6f 78 79 2c 20 30 ays use proxy, 0
e83d0 20 6d 65 61 6e 73 20 0a 20 20 20 20 2a 2a 20 6e means . ** n
e83e0 65 76 65 72 20 75 73 65 20 70 72 6f 78 79 2c 20 ever use proxy,
e83f0 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 70 NULL means use p
e8400 72 6f 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 roxy for non-loc
e8410 61 6c 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 al files only.
e8420 2a 2f 0a 20 20 20 20 69 66 28 20 65 6e 76 66 6f */. if( envfo
e8430 72 63 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 rce!=NULL ){.
e8440 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 61 74 useProxy = at
e8450 6f 69 28 65 6e 76 66 6f 72 63 65 29 3e 30 3b 0a oi(envforce)>0;.
e8460 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
e8470 20 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 struct statfs f
e8480 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 sInfo;. if(
e8490 20 73 74 61 74 66 73 28 7a 50 61 74 68 2c 20 26 statfs(zPath, &
e84a0 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 7b fsInfo) == -1 ){
e84b0 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 . /* In t
e84c0 68 65 6f 72 79 2c 20 74 68 65 20 63 6c 6f 73 65 heory, the close
e84d0 28 66 64 29 20 63 61 6c 6c 20 69 73 20 73 75 62 (fd) call is sub
e84e0 2d 6f 70 74 69 6d 61 6c 2e 20 49 66 20 74 68 65 -optimal. If the
e84f0 20 66 69 6c 65 20 6f 70 65 6e 65 64 0a 20 20 20 file opened.
e8500 20 20 20 20 20 2a 2a 20 77 69 74 68 20 66 64 20 ** with fd
e8510 69 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69 is a database fi
e8520 6c 65 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 le, and there ar
e8530 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 e other connecti
e8540 6f 6e 73 20 6f 70 65 6e 0a 20 20 20 20 20 20 20 ons open.
e8550 20 2a 2a 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 ** on that file
e8560 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e that are curren
e8570 74 6c 79 20 68 6f 6c 64 69 6e 67 20 61 64 76 69 tly holding advi
e8580 73 6f 72 79 20 6c 6f 63 6b 73 20 6f 6e 20 69 74 sory locks on it
e8590 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ,. ** the
e85a0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 6c n the call to cl
e85b0 6f 73 65 28 29 20 77 69 6c 6c 20 63 61 6e 63 65 ose() will cance
e85c0 6c 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 49 l those locks. I
e85d0 6e 20 70 72 61 63 74 69 63 65 2c 0a 20 20 20 20 n practice,.
e85e0 20 20 20 20 2a 2a 20 77 65 27 72 65 20 61 73 73 ** we're ass
e85f0 75 6d 69 6e 67 20 74 68 61 74 20 73 74 61 74 66 uming that statf
e8600 73 28 29 20 64 6f 65 73 6e 27 74 20 66 61 69 6c s() doesn't fail
e8610 20 76 65 72 79 20 6f 66 74 65 6e 2e 20 41 74 20 very often. At
e8620 6c 65 61 73 74 0a 20 20 20 20 20 20 20 20 2a 2a least. **
e8630 20 6e 6f 74 20 77 68 69 6c 65 20 6f 74 68 65 72 not while other
e8640 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e8650 73 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 s opened by the
e8660 73 61 6d 65 20 70 72 6f 63 65 73 73 20 6f 6e 0a same process on.
e8670 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 ** the s
e8680 61 6d 65 20 66 69 6c 65 20 61 72 65 20 77 6f 72 ame file are wor
e8690 6b 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20 20 king. */.
e86a0 20 20 70 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d p->lastErrno =
e86b0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
e86c0 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 7b 0a if( dirfd>=0 ){.
e86d0 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 close(
e86e0 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e dirfd); /* silen
e86f0 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c tly leak if fail
e8700 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 , in error */.
e8710 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
e8720 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 close(fd); /* si
e8730 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66 20 66 lently leak if f
e8740 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 20 2a 2f ail, in error */
e8750 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
e8760 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
e8770 53 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 S;. goto
e8780 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 open_finished;.
e8790 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 73 65 }. use
e87a0 50 72 6f 78 79 20 3d 20 21 28 66 73 49 6e 66 6f Proxy = !(fsInfo
e87b0 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f 4c 4f 43 .f_flags&MNT_LOC
e87c0 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 AL);. }. i
e87d0 66 28 20 75 73 65 50 72 6f 78 79 20 29 7b 0a 20 f( useProxy ){.
e87e0 20 20 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e rc = fillIn
e87f0 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 UnixFile(pVfs, f
e8800 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c d, dirfd, pFile,
e8810 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 zPath, noLock,
e8820 69 73 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 isDelete);.
e8830 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
e8840 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
e8850 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 = proxyTransfor
e8860 6d 55 6e 69 78 46 69 6c 65 28 28 75 6e 69 78 46 mUnixFile((unixF
e8870 69 6c 65 2a 29 70 46 69 6c 65 2c 20 22 3a 61 75 ile*)pFile, ":au
e8880 74 6f 3a 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 to:");. }.
e8890 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 goto open_f
e88a0 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 inished;. }.
e88b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72 }.#endif. . r
e88c0 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 c = fillInUnixFi
e88d0 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 le(pVfs, fd, dir
e88e0 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 fd, pFile, zPath
e88f0 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 , noLock, isDele
e8900 74 65 29 3b 0a 6f 70 65 6e 5f 66 69 6e 69 73 68 te);.open_finish
e8910 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 ed:. if( rc!=SQ
e8920 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 LITE_OK ){. s
e8930 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70 qlite3_free(p->p
e8940 55 6e 75 73 65 64 29 3b 0a 20 20 7d 0a 20 20 72 Unused);. }. r
e8950 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
e8960 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 .** Delete the f
e8970 69 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 ile at zPath. If
e8980 20 74 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 the dirSync arg
e8990 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 ument is true, f
e89a0 73 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 sync().** the di
e89b0 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 rectory after de
e89c0 6c 65 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e leting the file.
e89d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
e89e0 6e 69 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c nixDelete(. sql
e89f0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
e8a00 64 2c 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f d, /* VFS co
e8a10 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 ntaining this as
e8a20 20 74 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 the xDelete met
e8a30 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hod */. const c
e8a40 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
e8a50 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 /* Name of fi
e8a60 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 le to be deleted
e8a70 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e */. int dirSyn
e8a80 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c
e8a90 2f 2a 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e /* If true, fsyn
e8aa0 63 28 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 c() directory af
e8ab0 74 65 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c ter deleting fil
e8ac0 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
e8ad0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
e8ae0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e8af0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d (NotUsed);. Sim
e8b00 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 ulateIOError(ret
e8b10 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
e8b20 5f 44 45 4c 45 54 45 29 3b 0a 20 20 75 6e 6c 69 _DELETE);. unli
e8b30 6e 6b 28 7a 50 61 74 68 29 3b 0a 23 69 66 6e 64 nk(zPath);.#ifnd
e8b40 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ef SQLITE_DISABL
e8b50 45 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 E_DIRSYNC. if(
e8b60 64 69 72 53 79 6e 63 20 29 7b 0a 20 20 20 20 69 dirSync ){. i
e8b70 6e 74 20 66 64 3b 0a 20 20 20 20 72 63 20 3d 20 nt fd;. rc =
e8b80 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 openDirectory(zP
e8b90 61 74 68 2c 20 26 66 64 29 3b 0a 20 20 20 20 69 ath, &fd);. i
e8ba0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
e8bb0 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 ){.#if OS_VXWOR
e8bc0 4b 53 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 KS. if( fsy
e8bd0 6e 63 28 66 64 29 3d 3d 2d 31 20 29 0a 23 65 6c nc(fd)==-1 ).#el
e8be0 73 65 0a 20 20 20 20 20 20 69 66 28 20 66 73 79 se. if( fsy
e8bf0 6e 63 28 66 64 29 20 29 0a 23 65 6e 64 69 66 0a nc(fd) ).#endif.
e8c00 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 {.
e8c10 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
e8c20 52 5f 44 49 52 5f 46 53 59 4e 43 3b 0a 20 20 20 R_DIR_FSYNC;.
e8c30 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 }. if( c
e8c40 6c 6f 73 65 28 66 64 29 26 26 21 72 63 20 29 7b lose(fd)&&!rc ){
e8c50 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
e8c60 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 LITE_IOERR_DIR_C
e8c70 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 LOSE;. }.
e8c80 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
e8c90 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
e8ca0 2a 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 *.** Test the ex
e8cb0 69 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 istance of or ac
e8cc0 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 cess permissions
e8cd0 20 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 of file zPath.
e8ce0 54 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 The.** test perf
e8cf0 6f 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e ormed depends on
e8d00 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c the value of fl
e8d10 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 ags:.**.** S
e8d20 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 QLITE_ACCESS_EXI
e8d30 53 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 STS: Return 1 if
e8d40 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 the file exists
e8d50 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 .** SQLITE_A
e8d60 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a CCESS_READWRITE:
e8d70 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
e8d80 20 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e file is read an
e8d90 64 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 d writable..**
e8da0 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 SQLITE_ACCESS
e8db0 5f 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 _READONLY: Retur
e8dc0 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 n 1 if the file
e8dd0 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a is readable..**.
e8de0 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 ** Otherwise ret
e8df0 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 urn 0..*/.static
e8e00 20 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 int unixAccess(
e8e10 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
e8e20 4e 6f 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 NotUsed, /* Th
e8e30 65 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 e VFS containing
e8e40 20 74 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 this xAccess me
e8e50 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 thod */. const
e8e60 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
e8e70 20 20 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 /* Path of the
e8e80 20 66 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 file to examine
e8e90 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e8eb0 20 57 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 What do we want
e8ec0 20 74 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 to learn about
e8ed0 74 68 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 the zPath file?
e8ee0 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 */. int *pResOu
e8ef0 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t /*
e8f00 57 72 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f Write result boo
e8f10 6c 65 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a lean here */.){.
e8f20 20 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b int amode = 0;
e8f30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e8f40 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
e8f50 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
e8f60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
e8f70 4f 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a OERR_ACCESS; );.
e8f80 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 switch( flags
e8f90 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
e8fa0 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
e8fb0 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 :. amode =
e8fc0 46 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 F_OK;. brea
e8fd0 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
e8fe0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
e8ff0 49 54 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 ITE:. amode
e9000 20 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 = W_OK|R_OK;.
e9010 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
e9020 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
e9030 53 5f 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d S_READ:. am
e9040 6f 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 ode = R_OK;.
e9050 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 break;.. de
e9060 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 fault:. ass
e9070 65 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c ert(!"Invalid fl
e9080 61 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a ags argument");.
e9090 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d }. *pResOut =
e90a0 20 28 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 (access(zPath,
e90b0 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 amode)==0);. re
e90c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e90d0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 }.../*.** Turn a
e90e0 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 relative pathna
e90f0 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 me into a full p
e9100 61 74 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c athname. The rel
e9110 61 74 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 ative path.** is
e9120 20 73 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c stored as a nul
e9130 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 -terminated stri
e9140 6e 67 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 ng in the buffer
e9150 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a pointed to by.*
e9160 2a 20 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 * zPath. .**.**
e9170 7a 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 zOut points to a
e9180 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 buffer of at le
e9190 61 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e ast sqlite3_vfs.
e91a0 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 mxPathname bytes
e91b0 20 0a 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 .** (in this ca
e91c0 73 65 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 se, MAX_PATHNAME
e91d0 20 62 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c bytes). The ful
e91e0 6c 2d 70 61 74 68 20 69 73 20 77 72 69 74 74 65 l-path is writte
e91f0 6e 20 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 n to.** this buf
e9200 66 65 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 fer before retur
e9210 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ning..*/.static
e9220 69 6e 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 int unixFullPath
e9230 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f name(. sqlite3_
e9240 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
e9250 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
e9260 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a to vfs object *
e9270 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
e9280 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 zPath,
e9290 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 /* Possibly re
e92a0 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 lative input pat
e92b0 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c h */. int nOut,
e92c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e92d0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
e92e0 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e output buffer in
e92f0 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 bytes */. char
e9300 20 2a 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 *zOut
e9310 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
e9320 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b put buffer */.){
e9330 0a 0a 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 .. /* It's odd
e9340 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 to simulate an i
e9350 6f 2d 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 o-error here, bu
e9360 74 20 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 t really this is
e9370 20 6a 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 just. ** using
e9380 20 74 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e the io-error in
e9390 66 72 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 frastructure to
e93a0 74 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 test that SQLite
e93b0 20 68 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 handles this.
e93c0 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c ** function fail
e93d0 69 6e 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 ing. This functi
e93e0 6f 6e 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 on could fail if
e93f0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 , for example, t
e9400 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 he. ** current
e9410 77 6f 72 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 working director
e9420 79 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e y has been unlin
e9430 6b 65 64 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 ked.. */. Simu
e9440 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
e9450 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
e9460 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 );.. assert( p
e9470 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d Vfs->mxPathname=
e9480 3d 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b =MAX_PATHNAME );
e9490 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e94a0 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 7a 4f TER(pVfs);.. zO
e94b0 75 74 5b 6e 4f 75 74 2d 31 5d 20 3d 20 27 5c 30 ut[nOut-1] = '\0
e94c0 27 3b 0a 20 20 69 66 28 20 7a 50 61 74 68 5b 30 ';. if( zPath[0
e94d0 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 73 71 ]=='/' ){. sq
e94e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
e94f0 4f 75 74 2c 20 7a 4f 75 74 2c 20 22 25 73 22 2c Out, zOut, "%s",
e9500 20 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 zPath);. }else
e9510 7b 0a 20 20 20 20 69 6e 74 20 6e 43 77 64 3b 0a {. int nCwd;.
e9520 20 20 20 20 69 66 28 20 67 65 74 63 77 64 28 7a if( getcwd(z
e9530 4f 75 74 2c 20 6e 4f 75 74 2d 31 29 3d 3d 30 20 Out, nOut-1)==0
e9540 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
e9550 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
e9560 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 . }. nCwd
e9570 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f = (int)strlen(zO
e9580 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ut);. sqlite3
e9590 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e _snprintf(nOut-n
e95a0 43 77 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d Cwd, &zOut[nCwd]
e95b0 2c 20 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b , "/%s", zPath);
e95c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
e95d0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 LITE_OK;.}...#if
e95e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
e95f0 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a _LOAD_EXTENSION.
e9600 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 /*.** Interfaces
e9610 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 for opening a s
e9620 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 hared library, f
e9630 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 inding entry poi
e9640 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 nts.** within th
e9650 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
e9660 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 , and closing th
e9670 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
e9680 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 ..*/.#include <d
e9690 6c 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 lfcn.h>.static v
e96a0 6f 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 oid *unixDlOpen(
e96b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
e96c0 55 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 Used, const char
e96d0 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
e96e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e96f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 (NotUsed);. ret
e9700 75 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 urn dlopen(zFile
e9710 6e 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c name, RTLD_NOW |
e9720 20 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d RTLD_GLOBAL);.}
e9730 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 ../*.** SQLite c
e9740 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 alls this functi
e9750 6f 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 on immediately a
e9760 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 fter a call to u
e9770 6e 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a nixDlSym() or.**
e9780 20 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 unixDlOpen() fa
e9790 69 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e ils (returns a n
e97a0 75 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 ull pointer). If
e97b0 20 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 a more detailed
e97c0 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 error.** messag
e97d0 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 e is available,
e97e0 69 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f it is written to
e97f0 20 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 zBufOut. If no
e9800 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a error message.**
e9810 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a is available, z
e9820 42 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 BufOut is left u
e9830 6e 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 nmodified and SQ
e9840 4c 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 Lite uses a defa
e9850 75 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 ult.** error mes
e9860 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 sage..*/.static
e9870 76 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 void unixDlError
e9880 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
e9890 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c tUsed, int nBuf,
e98a0 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b char *zBufOut){
e98b0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 . char *zErr;.
e98c0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e98d0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 75 6e R(NotUsed);. un
e98e0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
e98f0 20 20 7a 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 zErr = dlerror
e9900 28 29 3b 0a 20 20 69 66 28 20 7a 45 72 72 20 29 ();. if( zErr )
e9910 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
e9920 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 printf(nBuf, zBu
e9930 66 4f 75 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 fOut, "%s", zErr
e9940 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 );. }. unixLea
e9950 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 73 74 61 veMutex();.}.sta
e9960 74 69 63 20 76 6f 69 64 20 28 2a 75 6e 69 78 44 tic void (*unixD
e9970 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 lSym(sqlite3_vfs
e9980 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 *NotUsed, void
e9990 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a *p, const char*z
e99a0 53 79 6d 29 29 28 76 6f 69 64 29 7b 0a 20 20 2f Sym))(void){. /
e99b0 2a 20 0a 20 20 2a 2a 20 47 43 43 20 77 69 74 68 * . ** GCC with
e99c0 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72 -pedantic-error
e99d0 73 20 73 61 79 73 20 74 68 61 74 20 43 39 30 20 s says that C90
e99e0 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 does not allow a
e99f0 20 76 6f 69 64 2a 20 74 6f 20 62 65 0a 20 20 2a void* to be. *
e9a00 2a 20 63 61 73 74 20 69 6e 74 6f 20 61 20 70 6f * cast into a po
e9a10 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 inter to a funct
e9a20 69 6f 6e 2e 20 20 41 6e 64 20 79 65 74 20 74 68 ion. And yet th
e9a30 65 20 6c 69 62 72 61 72 79 20 64 6c 73 79 6d 28 e library dlsym(
e9a40 29 20 72 6f 75 74 69 6e 65 0a 20 20 2a 2a 20 72 ) routine. ** r
e9a50 65 74 75 72 6e 73 20 61 20 76 6f 69 64 2a 20 77 eturns a void* w
e9a60 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 61 hich is really a
e9a70 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 pointer to a fu
e9a80 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 68 6f 77 20 nction. So how
e9a90 64 6f 20 77 65 0a 20 20 2a 2a 20 75 73 65 20 64 do we. ** use d
e9aa0 6c 73 79 6d 28 29 20 77 69 74 68 20 2d 70 65 64 lsym() with -ped
e9ab0 61 6e 74 69 63 2d 65 72 72 6f 72 73 3f 0a 20 20 antic-errors?.
e9ac0 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 **. ** Variable
e9ad0 20 78 20 62 65 6c 6f 77 20 69 73 20 64 65 66 69 x below is defi
e9ae0 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f 69 6e ned to be a poin
e9af0 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ter to a functio
e9b00 6e 20 74 61 6b 69 6e 67 0a 20 20 2a 2a 20 70 61 n taking. ** pa
e9b10 72 61 6d 65 74 65 72 73 20 76 6f 69 64 2a 20 61 rameters void* a
e9b20 6e 64 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 61 nd const char* a
e9b30 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70 nd returning a p
e9b40 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 ointer to a func
e9b50 74 69 6f 6e 2e 0a 20 20 2a 2a 20 57 65 20 69 6e tion.. ** We in
e9b60 69 74 69 61 6c 69 7a 65 20 78 20 62 79 20 61 73 itialize x by as
e9b70 73 69 67 6e 69 6e 67 20 69 74 20 61 20 70 6f 69 signing it a poi
e9b80 6e 74 65 72 20 74 6f 20 74 68 65 20 64 6c 73 79 nter to the dlsy
e9b90 6d 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 m() function..
e9ba0 2a 2a 20 28 54 68 61 74 20 61 73 73 69 67 6e 6d ** (That assignm
e9bb0 65 6e 74 20 72 65 71 75 69 72 65 73 20 61 20 63 ent requires a c
e9bc0 61 73 74 2e 29 20 20 54 68 65 6e 20 77 65 20 63 ast.) Then we c
e9bd0 61 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e all the function
e9be0 20 74 68 61 74 0a 20 20 2a 2a 20 78 20 70 6f 69 that. ** x poi
e9bf0 6e 74 73 20 74 6f 2e 20 20 0a 20 20 2a 2a 0a 20 nts to. . **.
e9c00 20 2a 2a 20 54 68 69 73 20 77 6f 72 6b 2d 61 72 ** This work-ar
e9c10 6f 75 6e 64 20 69 73 20 75 6e 6c 69 6b 65 6c 79 ound is unlikely
e9c20 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 to work correct
e9c30 6c 79 20 6f 6e 20 61 6e 79 20 73 79 73 74 65 6d ly on any system
e9c40 20 77 68 65 72 65 0a 20 20 2a 2a 20 79 6f 75 20 where. ** you
e9c50 72 65 61 6c 6c 79 20 63 61 6e 6e 6f 74 20 63 61 really cannot ca
e9c60 73 74 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f st a function po
e9c70 69 6e 74 65 72 20 69 6e 74 6f 20 76 6f 69 64 2a inter into void*
e9c80 2e 20 20 42 75 74 20 74 68 65 6e 2c 20 6f 6e 20 . But then, on
e9c90 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 68 the. ** other h
e9ca0 61 6e 64 2c 20 64 6c 73 79 6d 28 29 20 77 69 6c and, dlsym() wil
e9cb0 6c 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 73 75 l not work on su
e9cc0 63 68 20 61 20 73 79 73 74 65 6d 20 65 69 74 68 ch a system eith
e9cd0 65 72 2c 20 73 6f 20 77 65 20 68 61 76 65 0a 20 er, so we have.
e9ce0 20 2a 2a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6c ** not really l
e9cf0 6f 73 74 20 61 6e 79 74 68 69 6e 67 2e 0a 20 20 ost anything..
e9d00 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 28 2a 78 29 */. void (*(*x)
e9d10 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
e9d20 72 2a 29 29 28 76 6f 69 64 29 3b 0a 20 20 55 4e r*))(void);. UN
e9d30 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e9d40 6f 74 55 73 65 64 29 3b 0a 20 20 78 20 3d 20 28 otUsed);. x = (
e9d50 76 6f 69 64 28 2a 28 2a 29 28 76 6f 69 64 2a 2c void(*(*)(void*,
e9d60 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f const char*))(vo
e9d70 69 64 29 29 64 6c 73 79 6d 3b 0a 20 20 72 65 74 id))dlsym;. ret
e9d80 75 72 6e 20 28 2a 78 29 28 70 2c 20 7a 53 79 6d urn (*x)(p, zSym
e9d90 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 );.}.static void
e9da0 20 75 6e 69 78 44 6c 43 6c 6f 73 65 28 73 71 6c unixDlClose(sql
e9db0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
e9dc0 64 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 d, void *pHandle
e9dd0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
e9de0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
e9df0 20 20 64 6c 63 6c 6f 73 65 28 70 48 61 6e 64 6c dlclose(pHandl
e9e00 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 e);.}.#else /* i
e9e10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
e9e20 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 AD_EXTENSION is
e9e30 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 defined: */. #d
e9e40 65 66 69 6e 65 20 75 6e 69 78 44 6c 4f 70 65 6e efine unixDlOpen
e9e50 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 0. #define un
e9e60 69 78 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 ixDlError 0. #d
e9e70 65 66 69 6e 65 20 75 6e 69 78 44 6c 53 79 6d 20 efine unixDlSym
e9e80 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 0. #define un
e9e90 69 78 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 ixDlClose 0.#end
e9ea0 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 if../*.** Write
e9eb0 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 nBuf bytes of ra
e9ec0 6e 64 6f 6d 20 64 61 74 61 20 74 6f 20 74 68 65 ndom data to the
e9ed0 20 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 supplied buffer
e9ee0 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 zBuf..*/.static
e9ef0 20 69 6e 74 20 75 6e 69 78 52 61 6e 64 6f 6d 6e int unixRandomn
e9f00 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ess(sqlite3_vfs
e9f10 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 *NotUsed, int nB
e9f20 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b uf, char *zBuf){
e9f30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e9f40 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
e9f50 61 73 73 65 72 74 28 28 73 69 7a 65 5f 74 29 6e assert((size_t)n
e9f60 42 75 66 3e 3d 28 73 69 7a 65 6f 66 28 74 69 6d Buf>=(sizeof(tim
e9f70 65 5f 74 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 e_t)+sizeof(int)
e9f80 29 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 ));.. /* We hav
e9f90 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 e to initialize
e9fa0 7a 42 75 66 20 74 6f 20 70 72 65 76 65 6e 74 20 zBuf to prevent
e9fb0 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 72 65 valgrind from re
e9fc0 70 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 65 72 72 porting. ** err
e9fd0 6f 72 73 2e 20 20 54 68 65 20 72 65 70 6f 72 74 ors. The report
e9fe0 73 20 69 73 73 75 65 64 20 62 79 20 76 61 6c 67 s issued by valg
e9ff0 72 69 6e 64 20 61 72 65 20 69 6e 63 6f 72 72 65 rind are incorre
ea000 63 74 20 2d 20 77 65 20 77 6f 75 6c 64 0a 20 20 ct - we would.
ea010 2a 2a 20 70 72 65 66 65 72 20 74 68 61 74 20 74 ** prefer that t
ea020 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 62 65 he randomness be
ea030 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 6d 61 increased by ma
ea040 6b 69 6e 67 20 75 73 65 20 6f 66 20 74 68 65 0a king use of the.
ea050 20 20 2a 2a 20 75 6e 69 6e 69 74 69 61 6c 69 7a ** uninitializ
ea060 65 64 20 73 70 61 63 65 20 69 6e 20 7a 42 75 66 ed space in zBuf
ea070 20 2d 20 62 75 74 20 76 61 6c 67 72 69 6e 64 20 - but valgrind
ea080 65 72 72 6f 72 73 20 74 65 6e 64 20 74 6f 20 77 errors tend to w
ea090 6f 72 72 79 0a 20 20 2a 2a 20 73 6f 6d 65 20 75 orry. ** some u
ea0a0 73 65 72 73 2e 20 20 52 61 74 68 65 72 20 74 68 sers. Rather th
ea0b0 61 6e 20 61 72 67 75 65 2c 20 69 74 20 73 65 65 an argue, it see
ea0c0 6d 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 ms easier just t
ea0d0 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a o initialize. *
ea0e0 2a 20 74 68 65 20 77 68 6f 6c 65 20 61 72 72 61 * the whole arra
ea0f0 79 20 61 6e 64 20 73 69 6c 65 6e 63 65 20 76 61 y and silence va
ea100 6c 67 72 69 6e 64 2c 20 65 76 65 6e 20 69 66 20 lgrind, even if
ea110 74 68 61 74 20 6d 65 61 6e 73 20 6c 65 73 73 20 that means less
ea120 72 61 6e 64 6f 6d 6e 65 73 73 0a 20 20 2a 2a 20 randomness. **
ea130 69 6e 20 74 68 65 20 72 61 6e 64 6f 6d 20 73 65 in the random se
ea140 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 ed.. **. ** Wh
ea150 65 6e 20 74 65 73 74 69 6e 67 2c 20 69 6e 69 74 en testing, init
ea160 69 61 6c 69 7a 69 6e 67 20 7a 42 75 66 5b 5d 20 ializing zBuf[]
ea170 74 6f 20 7a 65 72 6f 20 69 73 20 61 6c 6c 20 77 to zero is all w
ea180 65 20 64 6f 2e 20 20 54 68 61 74 20 6d 65 61 6e e do. That mean
ea190 73 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 61 s. ** that we a
ea1a0 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 73 61 lways use the sa
ea1b0 6d 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 me random number
ea1c0 20 73 65 71 75 65 6e 63 65 2e 20 20 54 68 69 73 sequence. This
ea1d0 20 6d 61 6b 65 73 20 74 68 65 0a 20 20 2a 2a 20 makes the. **
ea1e0 74 65 73 74 73 20 72 65 70 65 61 74 61 62 6c 65 tests repeatable
ea1f0 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 .. */. memset(
ea200 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a zBuf, 0, nBuf);.
ea210 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
ea220 49 54 45 5f 54 45 53 54 29 0a 20 20 7b 0a 20 20 ITE_TEST). {.
ea230 20 20 69 6e 74 20 70 69 64 2c 20 66 64 3b 0a 20 int pid, fd;.
ea240 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 fd = open("/d
ea250 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 ev/urandom", O_R
ea260 44 4f 4e 4c 59 29 3b 0a 20 20 20 20 69 66 28 20 DONLY);. if(
ea270 66 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 74 69 fd<0 ){. ti
ea280 6d 65 5f 74 20 74 3b 0a 20 20 20 20 20 20 74 69 me_t t;. ti
ea290 6d 65 28 26 74 29 3b 0a 20 20 20 20 20 20 6d 65 me(&t);. me
ea2a0 6d 63 70 79 28 7a 42 75 66 2c 20 26 74 2c 20 73 mcpy(zBuf, &t, s
ea2b0 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 20 izeof(t));.
ea2c0 20 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b pid = getpid();
ea2d0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a . memcpy(&z
ea2e0 42 75 66 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 Buf[sizeof(t)],
ea2f0 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 &pid, sizeof(pid
ea300 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ));. assert
ea310 28 20 73 69 7a 65 6f 66 28 74 29 2b 73 69 7a 65 ( sizeof(t)+size
ea320 6f 66 28 70 69 64 29 3c 3d 28 73 69 7a 65 5f 74 of(pid)<=(size_t
ea330 29 6e 42 75 66 20 29 3b 0a 20 20 20 20 20 20 6e )nBuf );. n
ea340 42 75 66 20 3d 20 73 69 7a 65 6f 66 28 74 29 20 Buf = sizeof(t)
ea350 2b 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 + sizeof(pid);.
ea360 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
ea370 6e 42 75 66 20 3d 20 72 65 61 64 28 66 64 2c 20 nBuf = read(fd,
ea380 7a 42 75 66 2c 20 6e 42 75 66 29 3b 0a 20 20 20 zBuf, nBuf);.
ea390 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 close(fd);.
ea3a0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
ea3b0 20 72 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a return nBuf;.}.
ea3c0 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f ../*.** Sleep fo
ea3d0 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 r a little while
ea3e0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d . Return the am
ea3f0 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 ount of time sle
ea400 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d pt..** The argum
ea410 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ent is the numbe
ea420 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 r of microsecond
ea430 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 s we want to sle
ea440 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ep..** The retur
ea450 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e n value is the n
ea460 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 umber of microse
ea470 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 conds of sleep a
ea480 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 ctually.** reque
ea490 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e sted from the un
ea4a0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 derlying operati
ea4b0 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d ng system, a num
ea4c0 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 ber which.** mig
ea4d0 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68 ht be greater th
ea4e0 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
ea4f0 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 he argument, but
ea500 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 not less.** tha
ea510 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a n the argument..
ea520 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
ea530 69 78 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f ixSleep(sqlite3_
ea540 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e vfs *NotUsed, in
ea550 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b t microseconds){
ea560 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
ea570 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 struct timespe
ea580 63 20 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 c sp;.. sp.tv_s
ea590 65 63 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 ec = microsecond
ea5a0 73 20 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 s / 1000000;. s
ea5b0 70 2e 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 p.tv_nsec = (mic
ea5c0 72 6f 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 roseconds % 1000
ea5d0 30 30 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 000) * 1000;. n
ea5e0 61 6e 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 anosleep(&sp, NU
ea5f0 4c 4c 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 LL);. UNUSED_PA
ea600 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
ea610 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f ;. return micro
ea620 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 seconds;.#elif d
ea630 65 66 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 efined(HAVE_USLE
ea640 45 50 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 EP) && HAVE_USLE
ea650 45 50 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 EP. usleep(micr
ea660 6f 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 oseconds);. UNU
ea670 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
ea680 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
ea690 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 microseconds;.#
ea6a0 65 6c 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e else. int secon
ea6b0 64 73 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e ds = (microsecon
ea6c0 64 73 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 ds+999999)/10000
ea6d0 30 30 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 00;. sleep(seco
ea6e0 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 nds);. UNUSED_P
ea6f0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
ea700 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f );. return seco
ea710 6e 64 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e nds*1000000;.#en
ea720 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 dif.}../*.** The
ea730 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 following varia
ea740 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 ble, if set to a
ea750 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c non-zero value,
ea760 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 is interpreted
ea770 61 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 as.** the number
ea780 20 6f 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 of seconds sinc
ea790 65 20 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 e 1970 and is us
ea7a0 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 ed to set the re
ea7b0 73 75 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 sult of.** sqlit
ea7c0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
ea7d0 29 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 ) during testing
ea7e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
ea7f0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
ea800 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
ea810 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b urrent_time = 0;
ea820 20 20 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d /* Fake system
ea830 20 74 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 time in seconds
ea840 20 73 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a since 1970. */.
ea850 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 #endif../*.** Fi
ea860 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 nd the current t
ea870 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 ime (in Universa
ea880 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 l Coordinated Ti
ea890 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a me). Write the.
ea8a0 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 ** current time
ea8b0 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 and date as a Ju
ea8c0 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 lian Day number
ea8d0 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a into *prNow and.
ea8e0 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 ** return 0. Re
ea8f0 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 turn 1 if the ti
ea900 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e me and date cann
ea910 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a ot be found..*/.
ea920 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 static int unixC
ea930 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
ea940 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
ea950 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b double *prNow){
ea960 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
ea970 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
ea980 47 5f 50 4f 49 4e 54 29 0a 20 20 74 69 6d 65 5f G_POINT). time_
ea990 74 20 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b t t;. time(&t);
ea9a0 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 28 73 . *prNow = (((s
ea9b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 74 29 2f qlite3_int64)t)/
ea9c0 38 36 34 30 20 2b 20 32 34 34 30 35 38 37 35 29 8640 + 24405875)
ea9d0 2f 31 30 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e /10;.#elif defin
ea9e0 65 64 28 4e 4f 5f 47 45 54 54 4f 44 29 0a 20 20 ed(NO_GETTOD).
ea9f0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d 65 time_t t;. time
eaa00 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d (&t);. *prNow =
eaa10 20 74 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 t/86400.0 + 244
eaa20 30 35 38 37 2e 35 3b 0a 23 65 6c 69 66 20 4f 53 0587.5;.#elif OS
eaa30 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 _VXWORKS. struc
eaa40 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77 3b t timespec sNow;
eaa50 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d 65 . clock_gettime
eaa60 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45 2c (CLOCK_REALTIME,
eaa70 20 26 73 4e 6f 77 29 3b 0a 20 20 2a 70 72 4e 6f &sNow);. *prNo
eaa80 77 20 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20 w = 2440587.5 +
eaa90 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 sNow.tv_sec/8640
eaaa0 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 0.0 + sNow.tv_ns
eaab0 65 63 2f 38 36 34 30 30 30 30 30 30 30 30 30 30 ec/8640000000000
eaac0 30 2e 30 3b 0a 23 65 6c 73 65 0a 20 20 73 74 72 0.0;.#else. str
eaad0 75 63 74 20 74 69 6d 65 76 61 6c 20 73 4e 6f 77 uct timeval sNow
eaae0 3b 0a 20 20 67 65 74 74 69 6d 65 6f 66 64 61 79 ;. gettimeofday
eaaf0 28 26 73 4e 6f 77 2c 20 30 29 3b 0a 20 20 2a 70 (&sNow, 0);. *p
eab00 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37 2e 35 rNow = 2440587.5
eab10 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63 2f 38 + sNow.tv_sec/8
eab20 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e 74 76 6400.0 + sNow.tv
eab30 5f 75 73 65 63 2f 38 36 34 30 30 30 30 30 30 30 _usec/8640000000
eab40 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 0.0;.#endif..#if
eab50 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
eab60 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 if( sqlite3_cu
eab70 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 rrent_time ){.
eab80 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 *prNow = sqlit
eab90 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f e3_current_time/
eaba0 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 86400.0 + 244058
eabb0 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7.5;. }.#endif.
eabc0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
eabd0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
eabe0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
eabf0 2a 20 57 65 20 61 64 64 65 64 20 74 68 65 20 78 * We added the x
eac00 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 6d GetLastError() m
eac10 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 69 ethod with the i
eac20 6e 74 65 6e 74 69 6f 6e 20 6f 66 20 70 72 6f 76 ntention of prov
eac30 69 64 69 6e 67 0a 2a 2a 20 62 65 74 74 65 72 20 iding.** better
eac40 6c 6f 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20 low-level error
eac50 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20 6f 70 messages when op
eac60 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 70 erating-system p
eac70 72 6f 62 6c 65 6d 73 20 63 6f 6d 65 20 75 70 0a roblems come up.
eac80 2a 2a 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 ** during SQLite
eac90 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 operation. But
eaca0 20 73 6f 20 66 61 72 2c 20 6e 6f 6e 65 20 6f 66 so far, none of
eacb0 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 69 that has been i
eacc0 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e mplemented.** in
eacd0 20 74 68 65 20 63 6f 72 65 2e 20 20 53 6f 20 74 the core. So t
eace0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e his routine is n
eacf0 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 46 6f ever called. Fo
ead00 72 20 6e 6f 77 2c 20 69 74 20 69 73 20 6d 65 72 r now, it is mer
ead10 65 6c 79 0a 2a 2a 20 61 20 70 6c 61 63 65 2d 68 ely.** a place-h
ead20 6f 6c 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 older..*/.static
ead30 20 69 6e 74 20 75 6e 69 78 47 65 74 4c 61 73 74 int unixGetLast
ead40 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
ead50 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 s *NotUsed, int
ead60 4e 6f 74 55 73 65 64 32 2c 20 63 68 61 72 20 2a NotUsed2, char *
ead70 4e 6f 74 55 73 65 64 33 29 7b 0a 20 20 55 4e 55 NotUsed3){. UNU
ead80 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
ead90 74 55 73 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 tUsed);. UNUSED
eada0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
eadb0 65 64 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ed2);. UNUSED_P
eadc0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
eadd0 33 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 3);. return 0;.
eade0 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../*.**********
eadf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
eae00 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 nd of sqlite3_vf
eae10 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a s methods ******
eae20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eae30 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
eae40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eae50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eae60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eae70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eae80 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
eae90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaed0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
eaee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaef0 2a 20 42 65 67 69 6e 20 50 72 6f 78 79 20 4c 6f * Begin Proxy Lo
eaf00 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a cking **********
eaf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaf20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f ******.**.** Pro
eaf30 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 20 xy locking is a
eaf40 22 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 "uber-locking-me
eaf50 74 68 6f 64 22 20 69 6e 20 74 68 69 73 20 73 65 thod" in this se
eaf60 6e 73 65 3a 20 20 49 74 20 75 73 65 73 20 74 68 nse: It uses th
eaf70 65 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 e.** other locki
eaf80 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 73 65 ng methods on se
eaf90 63 6f 6e 64 61 72 79 20 6c 6f 63 6b 20 66 69 6c condary lock fil
eafa0 65 73 2e 20 20 50 72 6f 78 79 20 6c 6f 63 6b 69 es. Proxy locki
eafb0 6e 67 20 69 73 20 61 0a 2a 2a 20 6d 65 74 61 2d ng is a.** meta-
eafc0 6c 61 79 65 72 20 6f 76 65 72 20 74 6f 70 20 6f layer over top o
eafd0 66 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 f the primitive
eafe0 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e locking implemen
eaff0 74 65 64 20 61 62 6f 76 65 2e 20 20 46 6f 72 0a ted above. For.
eb000 2a 2a 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 ** this reason,
eb010 74 68 65 20 64 69 76 69 73 69 6f 6e 20 74 68 61 the division tha
eb020 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6f 66 20 t implements of
eb030 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 proxy locking is
eb040 20 64 65 66 65 72 72 65 64 0a 2a 2a 20 75 6e 74 deferred.** unt
eb050 69 6c 20 6c 61 74 65 20 69 6e 20 74 68 65 20 66 il late in the f
eb060 69 6c 65 20 28 68 65 72 65 29 20 61 66 74 65 72 ile (here) after
eb070 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 all of the othe
eb080 72 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 68 61 r I/O methods ha
eb090 76 65 0a 2a 2a 20 62 65 65 6e 20 64 65 66 69 6e ve.** been defin
eb0a0 65 64 20 2d 20 73 6f 20 74 68 61 74 20 74 68 65 ed - so that the
eb0b0 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 primitive locki
eb0c0 6e 67 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 ng methods are a
eb0d0 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 73 vailable.** as s
eb0e0 65 72 76 69 63 65 73 20 74 6f 20 68 65 6c 70 20 ervices to help
eb0f0 77 69 74 68 20 74 68 65 20 69 6d 70 6c 65 6d 65 with the impleme
eb100 6e 74 61 74 69 6f 6e 20 6f 66 20 70 72 6f 78 79 ntation of proxy
eb110 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a locking..**.***
eb120 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 *.**.** The defa
eb130 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 ult locking sche
eb140 6d 65 73 20 69 6e 20 53 51 4c 69 74 65 20 75 73 mes in SQLite us
eb150 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 e byte-range loc
eb160 6b 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 ks on the.** dat
eb170 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 6f abase file to co
eb180 6f 72 64 69 6e 61 74 65 20 73 61 66 65 2c 20 63 ordinate safe, c
eb190 6f 6e 63 75 72 72 65 6e 74 20 61 63 63 65 73 73 oncurrent access
eb1a0 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 by multiple rea
eb1b0 64 65 72 73 0a 2a 2a 20 61 6e 64 20 77 72 69 74 ders.** and writ
eb1c0 65 72 73 20 5b 68 74 74 70 3a 2f 2f 73 71 6c 69 ers [http://sqli
eb1d0 74 65 2e 6f 72 67 2f 6c 6f 63 6b 69 6e 67 76 33 te.org/lockingv3
eb1e0 2e 68 74 6d 6c 5d 2e 20 20 54 68 65 20 66 69 76 .html]. The fiv
eb1f0 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a e file locking.*
eb200 2a 20 73 74 61 74 65 73 20 28 55 4e 4c 4f 43 4b * states (UNLOCK
eb210 45 44 2c 20 50 45 4e 44 49 4e 47 2c 20 53 48 41 ED, PENDING, SHA
eb220 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 45 RED, RESERVED, E
eb230 58 43 4c 55 53 49 56 45 29 20 61 72 65 20 69 6d XCLUSIVE) are im
eb240 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20 plemented.** as
eb250 50 4f 53 49 58 20 72 65 61 64 20 26 20 77 72 69 POSIX read & wri
eb260 74 65 20 6c 6f 63 6b 73 20 6f 76 65 72 20 66 69 te locks over fi
eb270 78 65 64 20 73 65 74 20 6f 66 20 6c 6f 63 61 74 xed set of locat
eb280 69 6f 6e 73 20 28 76 69 61 20 66 73 63 74 6c 29 ions (via fsctl)
eb290 2c 0a 2a 2a 20 6f 6e 20 41 46 50 20 61 6e 64 20 ,.** on AFP and
eb2a0 53 4d 42 20 6f 6e 6c 79 20 65 78 63 6c 75 73 69 SMB only exclusi
eb2b0 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f ve byte-range lo
eb2c0 63 6b 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c cks are availabl
eb2d0 65 20 76 69 61 20 66 73 63 74 6c 0a 2a 2a 20 77 e via fsctl.** w
eb2e0 69 74 68 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 ith _IOWR('z', 2
eb2f0 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61 3, struct ByteRa
eb300 6e 67 65 4c 6f 63 6b 50 42 32 29 20 74 6f 20 74 ngeLockPB2) to t
eb310 72 61 63 6b 20 74 68 65 20 73 61 6d 65 20 35 20 rack the same 5
eb320 73 74 61 74 65 73 2e 0a 2a 2a 20 54 6f 20 73 69 states..** To si
eb330 6d 75 6c 61 74 65 20 61 20 46 5f 52 44 4c 43 4b mulate a F_RDLCK
eb340 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 72 on the shared r
eb350 61 6e 67 65 2c 20 6f 6e 20 41 46 50 20 61 20 72 ange, on AFP a r
eb360 61 6e 64 6f 6d 6c 79 20 73 65 6c 65 63 74 65 64 andomly selected
eb370 0a 2a 2a 20 61 64 64 72 65 73 73 20 69 6e 20 74 .** address in t
eb380 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 he shared range
eb390 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 20 53 is taken for a S
eb3a0 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 HARED lock, the
eb3b0 65 6e 74 69 72 65 0a 2a 2a 20 73 68 61 72 65 64 entire.** shared
eb3c0 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 range is taken
eb3d0 66 6f 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45 for an EXCLUSIVE
eb3e0 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 lock):.**.**
eb3f0 20 20 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 PENDING_BYTE
eb400 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 0x4000000
eb410 30 09 09 20 20 20 09 0a 2a 2a 20 20 20 20 20 20 0.. ..**
eb420 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 RESERVED_BYTE
eb430 20 20 20 20 30 78 34 30 30 30 30 30 30 31 0a 2a 0x40000001.*
eb440 2a 20 20 20 20 20 20 53 48 41 52 45 44 5f 52 41 * SHARED_RA
eb450 4e 47 45 20 20 20 20 20 20 20 20 30 78 34 30 30 NGE 0x400
eb460 30 30 30 30 32 20 2d 3e 20 30 78 34 30 30 30 30 00002 -> 0x40000
eb470 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 200.**.** This w
eb480 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e 20 74 68 65 orks well on the
eb490 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 local file syst
eb4a0 65 6d 2c 20 62 75 74 20 73 68 6f 77 73 20 61 20 em, but shows a
eb4b0 6e 65 61 72 6c 79 20 31 30 30 78 0a 2a 2a 20 73 nearly 100x.** s
eb4c0 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 65 61 64 20 lowdown in read
eb4d0 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 6e 20 41 performance on A
eb4e0 46 50 20 62 65 63 61 75 73 65 20 74 68 65 20 41 FP because the A
eb4f0 46 50 20 63 6c 69 65 6e 74 20 64 69 73 61 62 6c FP client disabl
eb500 65 73 0a 2a 2a 20 74 68 65 20 72 65 61 64 20 63 es.** the read c
eb510 61 63 68 65 20 77 68 65 6e 20 62 79 74 65 2d 72 ache when byte-r
eb520 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 70 ange locks are p
eb530 72 65 73 65 6e 74 2e 20 20 45 6e 61 62 6c 69 6e resent. Enablin
eb540 67 20 74 68 65 20 72 65 61 64 0a 2a 2a 20 63 61 g the read.** ca
eb550 63 68 65 20 65 78 70 6f 73 65 73 20 61 20 63 61 che exposes a ca
eb560 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 70 72 che coherency pr
eb570 6f 62 6c 65 6d 20 74 68 61 74 20 69 73 20 70 72 oblem that is pr
eb580 65 73 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f 53 20 esent on all OS
eb590 58 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 20 6e X.** supported n
eb5a0 65 74 77 6f 72 6b 20 66 69 6c 65 20 73 79 73 74 etwork file syst
eb5b0 65 6d 73 2e 20 20 4e 46 53 20 61 6e 64 20 41 46 ems. NFS and AF
eb5c0 50 20 62 6f 74 68 20 6f 62 73 65 72 76 65 20 74 P both observe t
eb5d0 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f 2d 6f he.** close-to-o
eb5e0 70 65 6e 20 73 65 6d 61 6e 74 69 63 73 20 66 6f pen semantics fo
eb5f0 72 20 65 6e 73 75 72 69 6e 67 20 63 61 63 68 65 r ensuring cache
eb600 20 63 6f 68 65 72 65 6e 63 79 0a 2a 2a 20 5b 68 coherency.** [h
eb610 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72 63 65 ttp://nfs.source
eb620 66 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71 5f 61 forge.net/#faq_a
eb630 38 5d 2c 20 77 68 69 63 68 20 64 6f 65 73 20 6e 8], which does n
eb640 6f 74 20 65 66 66 65 63 74 69 76 65 6c 79 0a 2a ot effectively.*
eb650 2a 20 61 64 64 72 65 73 73 20 74 68 65 20 72 65 * address the re
eb660 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72 20 63 quirements for c
eb670 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 oncurrent databa
eb680 73 65 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c se access by mul
eb690 74 69 70 6c 65 0a 2a 2a 20 72 65 61 64 65 72 73 tiple.** readers
eb6a0 20 61 6e 64 20 77 72 69 74 65 72 73 0a 2a 2a 20 and writers.**
eb6b0 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61 62 62 [http://www.nabb
eb6c0 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d 6f 6e le.com/SQLite-on
eb6d0 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68 65 72 -NFS-cache-coher
eb6e0 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30 31 2e ency-td15655701.
eb6f0 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 html]..**.** To
eb700 61 64 64 72 65 73 73 20 74 68 65 20 70 65 72 66 address the perf
eb710 6f 72 6d 61 6e 63 65 20 61 6e 64 20 63 61 63 68 ormance and cach
eb720 65 20 63 6f 68 65 72 65 6e 63 79 20 69 73 73 75 e coherency issu
eb730 65 73 2c 20 70 72 6f 78 79 20 66 69 6c 65 20 6c es, proxy file l
eb740 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 ocking.** change
eb750 73 20 74 68 65 20 77 61 79 20 64 61 74 61 62 61 s the way databa
eb760 73 65 20 61 63 63 65 73 73 20 69 73 20 63 6f 6e se access is con
eb770 74 72 6f 6c 6c 65 64 20 62 79 20 6c 69 6d 69 74 trolled by limit
eb780 69 6e 67 20 61 63 63 65 73 73 20 74 6f 20 61 0a ing access to a.
eb790 2a 2a 20 73 69 6e 67 6c 65 20 68 6f 73 74 20 61 ** single host a
eb7a0 74 20 61 20 74 69 6d 65 20 61 6e 64 20 6d 6f 76 t a time and mov
eb7b0 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 20 6f ing file locks o
eb7c0 66 66 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ff of the databa
eb7d0 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6f se file.** and o
eb7e0 6e 74 6f 20 61 20 70 72 6f 78 79 20 66 69 6c 65 nto a proxy file
eb7f0 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 on the local fi
eb800 6c 65 20 73 79 73 74 65 6d 2e 20 20 0a 2a 2a 0a le system. .**.
eb810 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 70 72 6f 78 **.** Using prox
eb820 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d y locks.** -----
eb830 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a ------------.**.
eb840 2a 2a 20 43 20 41 50 49 73 0a 2a 2a 0a 2a 2a 20 ** C APIs.**.**
eb850 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f sqlite3_file_co
eb860 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 ntrol(db, dbname
eb870 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 , SQLITE_SET_LOC
eb880 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a 20 20 KPROXYFILE,.**
eb890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eb8a0 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61 74 68 <proxy_path
eb8b0 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 2a > | ":auto:");.*
eb8c0 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f * sqlite3_file_
eb8d0 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61 control(db, dbna
eb8e0 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c me, SQLITE_GET_L
eb8f0 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 26 3c OCKPROXYFILE, &<
eb900 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a 2a 2a proxy_path>);.**
eb910 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61 67 6d .**.** SQL pragm
eb920 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47 4d 41 as.**.** PRAGMA
eb930 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b [database.]lock
eb940 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70 72 6f _proxy_file=<pro
eb950 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75 74 6f xy_path> | :auto
eb960 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 :.** PRAGMA [da
eb970 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f tabase.]lock_pro
eb980 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 xy_file.**.** Sp
eb990 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74 6f 3a ecifying ":auto:
eb9a0 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 " means that if
eb9b0 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e 63 68 there is a conch
eb9c0 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d 61 74 file with a mat
eb9d0 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20 49 44 ching.** host ID
eb9e0 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72 6f 78 in it, the prox
eb9f0 79 20 70 61 74 68 20 69 6e 20 74 68 65 20 63 6f y path in the co
eba00 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 nch file will be
eba10 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69 73 65 used, otherwise
eba20 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61 74 68 .** a proxy path
eba30 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 75 73 based on the us
eba40 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a 2a 2a er's temp dir.**
eba50 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28 5f 43 (via confstr(_C
eba60 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 S_DARWIN_USER_TE
eba70 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77 69 6c MP_DIR,...)) wil
eba80 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20 74 68 l be used and th
eba90 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72 6f 78 e.** actual prox
ebaa0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 67 y file name is g
ebab0 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 74 68 enerated from th
ebac0 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74 68 20 e name and path
ebad0 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 of the.** databa
ebae0 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20 65 78 se file. For ex
ebaf0 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ample:.**.**
ebb00 20 20 20 46 6f 72 20 64 61 74 61 62 61 73 65 20 For database
ebb10 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d 65 2f path "/Users/me/
ebb20 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20 20 20 foo.db" .**
ebb30 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 The lock path
ebb40 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64 69 72 will be "<tmpdir
ebb50 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73 2f 5f >/sqliteplocks/_
ebb60 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64 62 3a Users_me_foo.db:
ebb70 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e auto:").**.** On
ebb80 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78 79 20 ce a lock proxy
ebb90 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f is configured fo
ebba0 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e r a database con
ebbb0 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61 6e 20 nection, it can
ebbc0 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f 76 65 not.** be remove
ebbd0 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20 6d 61 d, however it ma
ebbe0 79 20 62 65 20 73 77 69 74 63 68 65 64 20 74 6f y be switched to
ebbf0 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 6f a different pro
ebc00 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a 20 74 xy path via.** t
ebc10 68 65 20 61 62 6f 76 65 20 41 50 49 73 20 28 61 he above APIs (a
ebc20 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f 6e 63 ssuming the conc
ebc30 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 62 65 h file is not be
ebc40 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 ing held by anot
ebc50 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f her.** connectio
ebc60 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e 20 0a n or process). .
ebc70 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70 72 6f **.**.** How pro
ebc80 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 xy locking works
ebc90 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ------------
ebca0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a -----------.**.*
ebcb0 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 * Proxy file loc
ebcc0 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72 69 6d king relies prim
ebcd0 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e 65 77 arily on two new
ebce0 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69 6c 65 supporting file
ebcf0 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 63 s: .**.** * c
ebd00 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c 69 6d onch file to lim
ebd10 69 74 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 it access to the
ebd20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
ebd30 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73 74 0a o a single host.
ebd40 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74 69 6d ** at a tim
ebd50 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70 72 6f e.**.** * pro
ebd60 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74 20 61 xy file to act a
ebd70 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20 74 68 s a proxy for th
ebd80 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 e advisory locks
ebd90 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20 20 20 normally.**
ebda0 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 taken on the d
ebdb0 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20 54 68 atabase.**.** Th
ebdc0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d 20 74 e conch file - t
ebdd0 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20 66 69 o use a proxy fi
ebde0 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73 74 20 le, sqlite must
ebdf0 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68 65 20 first "hold the
ebe00 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74 61 6b conch".** by tak
ebe10 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d 73 74 ing an sqlite-st
ebe20 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 yle shared lock
ebe30 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c on the conch fil
ebe40 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a e, reading the.*
ebe50 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64 20 63 * contents and c
ebe60 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68 6f 73 omparing the hos
ebe70 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73 74 20 t's unique host
ebe80 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29 20 61 ID (see below) a
ebe90 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f 78 79 nd lock.** proxy
ebea0 20 70 61 74 68 20 61 67 61 69 6e 73 74 20 74 68 path against th
ebeb0 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 e values stored
ebec0 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20 20 54 in the conch. T
ebed0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 he conch file is
ebee0 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 .** stored in th
ebef0 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79 e same directory
ebf00 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 as the database
ebf10 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 file and the fi
ebf20 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 70 61 le name.** is pa
ebf30 74 74 65 72 6e 65 64 20 61 66 74 65 72 20 74 68 tterned after th
ebf40 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
ebf50 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74 61 62 name as ".<datab
ebf60 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68 22 2e asename>-conch".
ebf70 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e 63 68 .** If the conch
ebf80 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 file does not e
ebf90 78 69 73 74 2c 20 6f 72 20 69 74 27 73 20 63 6f xist, or it's co
ebfa0 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 ntents do not ma
ebfb0 74 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73 74 20 tch the.** host
ebfc0 49 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78 79 20 ID and/or proxy
ebfd0 70 61 74 68 2c 20 74 68 65 6e 20 74 68 65 20 6c path, then the l
ebfe0 6f 63 6b 20 69 73 20 65 73 63 61 6c 61 74 65 64 ock is escalated
ebff0 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 to an exclusive
ec000 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74 68 65 .** lock and the
ec010 20 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 conch file cont
ec020 65 6e 74 73 20 69 73 20 75 70 64 61 74 65 64 20 ents is updated
ec030 77 69 74 68 20 74 68 65 20 68 6f 73 74 20 49 44 with the host ID
ec040 20 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20 70 61 and proxy.** pa
ec050 74 68 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 th and the lock
ec060 69 73 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f is downgraded to
ec070 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 a shared lock a
ec080 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 63 6f gain. If the co
ec090 6e 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64 20 62 nch.** is held b
ec0a0 79 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 y another proces
ec0b0 73 20 28 77 69 74 68 20 61 20 73 68 61 72 65 64 s (with a shared
ec0c0 20 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78 63 6c lock), the excl
ec0d0 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 77 69 usive lock.** wi
ec0e0 6c 6c 20 66 61 69 6c 20 61 6e 64 20 53 51 4c 49 ll fail and SQLI
ec0f0 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 TE_BUSY is retur
ec100 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ned..**.** The p
ec110 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20 73 69 roxy file - a si
ec120 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65 20 75 ngle-byte file u
ec130 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64 76 69 sed for all advi
ec140 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b 73 0a sory file locks.
ec150 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61 6b 65 ** normally take
ec160 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
ec170 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73 20 61 e file. This a
ec180 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65 20 73 llows for safe s
ec190 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 haring.** of the
ec1a0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
ec1b0 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 or multiple read
ec1c0 65 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 20 ers and writers
ec1d0 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 68 on the same.** h
ec1e0 6f 73 74 20 28 74 68 65 20 63 6f 6e 63 68 20 65 ost (the conch e
ec1f0 6e 73 75 72 65 73 20 74 68 61 74 20 74 68 65 79 nsures that they
ec200 20 61 6c 6c 20 75 73 65 20 74 68 65 20 73 61 6d all use the sam
ec210 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c e local lock fil
ec220 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 e)..**.** There
ec230 69 73 20 61 20 74 68 69 72 64 20 66 69 6c 65 20 is a third file
ec240 2d 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 - the host ID fi
ec250 6c 65 20 2d 20 75 73 65 64 20 61 73 20 61 20 70 le - used as a p
ec260 65 72 73 69 73 74 65 6e 74 20 72 65 63 6f 72 64 ersistent record
ec270 0a 2a 2a 20 6f 66 20 61 20 75 6e 69 71 75 65 20 .** of a unique
ec280 69 64 65 6e 74 69 66 69 65 72 20 66 6f 72 20 74 identifier for t
ec290 68 65 20 68 6f 73 74 2c 20 61 20 31 32 38 2d 62 he host, a 128-b
ec2a0 79 74 65 20 75 6e 69 71 75 65 20 68 6f 73 74 20 yte unique host
ec2b0 69 64 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 74 68 id file.** in th
ec2c0 65 20 70 61 74 68 20 64 65 66 69 6e 65 64 20 62 e path defined b
ec2d0 79 20 74 68 65 20 48 4f 53 54 49 44 50 41 54 48 y the HOSTIDPATH
ec2e0 20 6d 61 63 72 6f 20 28 64 65 66 61 75 6c 74 20 macro (default
ec2f0 76 61 6c 75 65 20 69 73 0a 2a 2a 20 2f 4c 69 62 value is.** /Lib
ec300 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d rary/Caches/.com
ec310 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e .apple.sqliteCon
ec320 63 68 48 6f 73 74 49 64 29 2e 0a 2a 2a 0a 2a 2a chHostId)..**.**
ec330 20 52 65 71 75 65 73 74 69 6e 67 20 74 68 65 20 Requesting the
ec340 6c 6f 63 6b 20 70 72 6f 78 79 20 64 6f 65 73 20 lock proxy does
ec350 6e 6f 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 not immediately
ec360 74 61 6b 65 20 74 68 65 20 63 6f 6e 63 68 2c 20 take the conch,
ec370 69 74 20 69 73 0a 2a 2a 20 6f 6e 6c 79 20 74 61 it is.** only ta
ec380 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 66 69 72 ken when the fir
ec390 73 74 20 72 65 71 75 65 73 74 20 74 6f 20 6c 6f st request to lo
ec3a0 63 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ck database file
ec3b0 20 69 73 20 6d 61 64 65 2e 20 20 0a 2a 2a 20 54 is made. .** T
ec3c0 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65 20 his matches the
ec3d0 73 65 6d 61 6e 74 69 63 73 20 6f 66 20 74 68 65 semantics of the
ec3e0 20 74 72 61 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 traditional loc
ec3f0 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 2c 20 77 king behavior, w
ec400 68 65 72 65 0a 2a 2a 20 6f 70 65 6e 69 6e 67 20 here.** opening
ec410 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 a connection to
ec420 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
ec430 64 6f 65 73 20 6e 6f 74 20 74 61 6b 65 20 61 20 does not take a
ec440 6c 6f 63 6b 20 6f 6e 20 69 74 2e 0a 2a 2a 20 54 lock on it..** T
ec450 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 he shared lock a
ec460 6e 64 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 nd an open file
ec470 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 20 6d descriptor are m
ec480 61 69 6e 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 aintained until
ec490 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 .** the connecti
ec4a0 6f 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 on to the databa
ec4b0 73 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 0a 2a se is closed. .*
ec4c0 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 *.** The proxy f
ec4d0 69 6c 65 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b ile and the lock
ec4e0 20 66 69 6c 65 20 61 72 65 20 6e 65 76 65 72 20 file are never
ec4f0 64 65 6c 65 74 65 64 20 73 6f 20 74 68 65 79 20 deleted so they
ec500 6f 6e 6c 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 only need.** to
ec510 62 65 20 63 72 65 61 74 65 64 20 74 68 65 20 66 be created the f
ec520 69 72 73 74 20 74 69 6d 65 20 74 68 65 79 20 61 irst time they a
ec530 72 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 re used..**.** C
ec540 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
ec550 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d ions.** --------
ec560 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
ec570 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 45 46 .** SQLITE_PREF
ec580 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 ER_PROXY_LOCKING
ec590 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 44 61 74 .**.** Dat
ec5a0 61 62 61 73 65 20 66 69 6c 65 73 20 61 63 63 65 abase files acce
ec5b0 73 73 65 64 20 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 ssed on non-loca
ec5c0 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20 61 l file systems a
ec5d0 72 65 0a 2a 2a 20 20 20 20 20 20 20 61 75 74 6f re.** auto
ec5e0 6d 61 74 69 63 61 6c 6c 79 20 63 6f 6e 66 69 67 matically config
ec5f0 75 72 65 64 20 66 6f 72 20 70 72 6f 78 79 20 6c ured for proxy l
ec600 6f 63 6b 69 6e 67 2c 20 6c 6f 63 6b 20 66 69 6c ocking, lock fil
ec610 65 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 es are.**
ec620 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 named automatica
ec630 6c 6c 79 20 75 73 69 6e 67 20 74 68 65 20 73 61 lly using the sa
ec640 6d 65 20 6c 6f 67 69 63 20 61 73 0a 2a 2a 20 20 me logic as.**
ec650 20 20 20 20 20 50 52 41 47 4d 41 20 6c 6f 63 6b PRAGMA lock
ec660 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 22 3a 61 75 _proxy_file=":au
ec670 74 6f 3a 22 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 to:".** .**
ec680 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 SQLITE_PROXY_DEB
ec690 55 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 45 UG.**.** E
ec6a0 6e 61 62 6c 65 73 20 74 68 65 20 6c 6f 67 67 69 nables the loggi
ec6b0 6e 67 20 6f 66 20 65 72 72 6f 72 20 6d 65 73 73 ng of error mess
ec6c0 61 67 65 73 20 64 75 72 69 6e 67 20 68 6f 73 74 ages during host
ec6d0 20 69 64 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 id file.**
ec6e0 20 20 72 65 74 72 69 65 76 61 6c 20 61 6e 64 20 retrieval and
ec6f0 63 72 65 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 creation.**.**
ec700 48 4f 53 54 49 44 50 41 54 48 0a 2a 2a 0a 2a 2a HOSTIDPATH.**.**
ec710 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 73 Overrides
ec720 20 74 68 65 20 64 65 66 61 75 6c 74 20 68 6f 73 the default hos
ec730 74 20 49 44 20 66 69 6c 65 20 70 61 74 68 20 6c t ID file path l
ec740 6f 63 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c ocation.**.** L
ec750 4f 43 4b 50 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a OCKPROXYDIR.**.*
ec760 2a 20 20 20 20 20 20 20 4f 76 65 72 72 69 64 65 * Override
ec770 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 64 69 s the default di
ec780 72 65 63 74 6f 72 79 20 75 73 65 64 20 66 6f 72 rectory used for
ec790 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 lock proxy file
ec7a0 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 s that.**
ec7b0 61 72 65 20 6e 61 6d 65 64 20 61 75 74 6f 6d 61 are named automa
ec7c0 74 69 63 61 6c 6c 79 20 76 69 61 20 74 68 65 20 tically via the
ec7d0 22 3a 61 75 74 6f 3a 22 20 73 65 74 74 69 6e 67 ":auto:" setting
ec7e0 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 44 .**.** SQLITE_D
ec7f0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
ec800 50 45 52 4d 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a PERMISSIONS.**.*
ec810 2a 20 20 20 20 20 20 20 50 65 72 6d 69 73 73 69 * Permissi
ec820 6f 6e 73 20 74 6f 20 75 73 65 20 77 68 65 6e 20 ons to use when
ec830 63 72 65 61 74 69 6e 67 20 61 20 64 69 72 65 63 creating a direc
ec840 74 6f 72 79 20 66 6f 72 20 73 74 6f 72 69 6e 67 tory for storing
ec850 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 6c 6f the.** lo
ec860 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 2c 20 ck proxy files,
ec870 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 4c only used when L
ec880 4f 43 4b 50 52 4f 58 59 44 49 52 20 69 73 20 6e OCKPROXYDIR is n
ec890 6f 74 20 73 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a ot set..** .*
ec8a0 2a 20 20 20 20 0a 2a 2a 20 41 73 20 6d 65 6e 74 * .** As ment
ec8b0 69 6f 6e 65 64 20 61 62 6f 76 65 2c 20 77 68 65 ioned above, whe
ec8c0 6e 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 n compiled with
ec8d0 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 SQLITE_PREFER_PR
ec8e0 4f 58 59 5f 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 OXY_LOCKING,.**
ec8f0 73 65 74 74 69 6e 67 20 74 68 65 20 65 6e 76 69 setting the envi
ec900 72 6f 6e 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 ronment variable
ec910 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 SQLITE_FORCE_PR
ec920 4f 58 59 5f 4c 4f 43 4b 49 4e 47 20 74 6f 20 31 OXY_LOCKING to 1
ec930 20 77 69 6c 6c 0a 2a 2a 20 66 6f 72 63 65 20 70 will.** force p
ec940 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 roxy locking to
ec950 62 65 20 75 73 65 64 20 66 6f 72 20 65 76 65 72 be used for ever
ec960 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 y database file
ec970 6f 70 65 6e 65 64 2c 20 61 6e 64 20 30 0a 2a 2a opened, and 0.**
ec980 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 75 74 6f will force auto
ec990 6d 61 74 69 63 20 70 72 6f 78 79 20 6c 6f 63 6b matic proxy lock
ec9a0 69 6e 67 20 74 6f 20 62 65 20 64 69 73 61 62 6c ing to be disabl
ec9b0 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 ed for all datab
ec9c0 61 73 65 0a 2a 2a 20 66 69 6c 65 73 20 28 65 78 ase.** files (ex
ec9d0 70 6c 69 63 69 74 79 20 63 61 6c 6c 69 6e 67 20 plicity calling
ec9e0 74 68 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c the SQLITE_SET_L
ec9f0 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 70 72 61 OCKPROXYFILE pra
eca00 67 6d 61 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 gma or.** sqlite
eca10 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 _file_control AP
eca20 49 20 69 73 20 6e 6f 74 20 61 66 66 65 63 74 65 I is not affecte
eca30 64 20 62 79 20 53 51 4c 49 54 45 5f 46 4f 52 43 d by SQLITE_FORC
eca40 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 E_PROXY_LOCKING)
eca50 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 ..*/../*.** Prox
eca60 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c y locking is onl
eca70 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d y available on M
eca80 61 63 4f 53 58 20 0a 2a 2f 0a 23 69 66 20 64 65 acOSX .*/.#if de
eca90 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
ecaa0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
ecab0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
ecac0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
ecad0 45 53 54 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 EST./* simulate
ecae0 6d 75 6c 74 69 70 6c 65 20 68 6f 73 74 73 20 62 multiple hosts b
ecaf0 79 20 63 72 65 61 74 69 6e 67 20 75 6e 69 71 75 y creating uniqu
ecb00 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 70 61 e hostid file pa
ecb10 74 68 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ths */.SQLITE_AP
ecb20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f I int sqlite3_ho
ecb30 73 74 69 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65 stid_num = 0;.#e
ecb40 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
ecb50 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ecb60 65 78 74 20 68 61 73 20 74 68 65 20 70 61 74 68 ext has the path
ecb70 20 61 6e 64 20 66 69 6c 65 20 73 74 72 75 63 74 and file struct
ecb80 75 72 65 73 20 66 6f 72 20 74 68 65 20 72 65 6d ures for the rem
ecb90 6f 74 65 20 0a 2a 2a 20 61 6e 64 20 6c 6f 63 61 ote .** and loca
ecba0 6c 20 70 72 6f 78 79 20 66 69 6c 65 73 20 69 6e l proxy files in
ecbb0 20 69 74 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 it.*/.typedef s
ecbc0 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 truct proxyLocki
ecbd0 6e 67 43 6f 6e 74 65 78 74 20 70 72 6f 78 79 4c ngContext proxyL
ecbe0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 ockingContext;.s
ecbf0 74 72 75 63 74 20 70 72 6f 78 79 4c 6f 63 6b 69 truct proxyLocki
ecc00 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e ngContext {. un
ecc10 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c ixFile *conchFil
ecc20 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 e; /* Op
ecc30 65 6e 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a 2f en conch file */
ecc40 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 46 69 . char *conchFi
ecc50 6c 65 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 lePath;
ecc60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
ecc70 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 75 onch file */. u
ecc80 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f nixFile *lockPro
ecc90 78 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f xy; /* O
ecca0 70 65 6e 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 pen proxy lock f
eccb0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c ile */. char *l
eccc0 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 20 20 20 ockProxyPath;
eccd0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
ecce0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 the proxy lock
eccf0 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
ecd00 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 dbPath;
ecd10 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
ecd20 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 f the open file
ecd30 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 63 68 48 65 */. int conchHe
ecd40 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ld;
ecd50 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
ecd60 20 63 6f 6e 63 68 20 69 73 20 63 75 72 72 65 6e conch is curren
ecd70 74 6c 79 20 68 65 6c 64 20 2a 2f 0a 20 20 76 6f tly held */. vo
ecd80 69 64 20 2a 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f id *oldLockingCo
ecd90 6e 74 65 78 74 3b 20 20 20 20 20 2f 2a 20 4f 72 ntext; /* Or
ecda0 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 63 6f iginal lockingco
ecdb0 6e 74 65 78 74 20 74 6f 20 72 65 73 74 6f 72 65 ntext to restore
ecdc0 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 73 on close */. s
ecdd0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
ecde0 73 20 63 6f 6e 73 74 20 2a 70 4f 6c 64 4d 65 74 s const *pOldMet
ecdf0 68 6f 64 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 hod; /* Orig
ece00 69 6e 61 6c 20 49 2f 4f 20 6d 65 74 68 6f 64 73 inal I/O methods
ece10 20 66 6f 72 20 63 6c 6f 73 65 20 2a 2f 0a 7d 3b for close */.};
ece20 0a 0a 2f 2a 20 48 4f 53 54 49 44 4c 45 4e 20 61 ../* HOSTIDLEN a
ece30 6e 64 20 43 4f 4e 43 48 4c 45 4e 20 62 6f 74 68 nd CONCHLEN both
ece40 20 69 6e 63 6c 75 64 65 20 73 70 61 63 65 20 66 include space f
ece50 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 0a 2a or the string .*
ece60 2a 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 * terminating nu
ece70 6c 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 48 4f l .*/.#define HO
ece80 53 54 49 44 4c 45 4e 20 20 20 20 20 20 20 20 20 STIDLEN
ece90 31 32 38 0a 23 64 65 66 69 6e 65 20 43 4f 4e 43 128.#define CONC
ecea0 48 4c 45 4e 20 20 20 20 20 20 20 20 20 20 28 4d HLEN (M
eceb0 41 58 50 41 54 48 4c 45 4e 2b 48 4f 53 54 49 44 AXPATHLEN+HOSTID
ecec0 4c 45 4e 2b 31 29 0a 23 69 66 6e 64 65 66 20 48 LEN+1).#ifndef H
eced0 4f 53 54 49 44 50 41 54 48 0a 23 20 64 65 66 69 OSTIDPATH.# defi
ecee0 6e 65 20 48 4f 53 54 49 44 50 41 54 48 20 20 20 ne HOSTIDPATH
ecef0 20 20 20 20 22 2f 4c 69 62 72 61 72 79 2f 43 61 "/Library/Ca
ecf00 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e ches/.com.apple.
ecf10 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 sqliteConchHostI
ecf20 64 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 62 61 d".#endif../* ba
ecf30 73 69 63 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f sically a copy o
ecf40 66 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 f unixRandomness
ecf50 20 77 69 74 68 20 64 69 66 66 65 72 65 6e 74 0a with different.
ecf60 2a 2a 20 74 65 73 74 20 62 65 68 61 76 69 6f 72 ** test behavior
ecf70 20 62 75 69 6c 74 20 69 6e 20 2a 2f 0a 73 74 61 built in */.sta
ecf80 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 6e tic int proxyGen
ecf90 65 72 61 74 65 48 6f 73 74 49 44 28 63 68 61 72 erateHostID(char
ecfa0 20 2a 70 48 6f 73 74 49 44 29 7b 0a 20 20 69 6e *pHostID){. in
ecfb0 74 20 70 69 64 2c 20 66 64 2c 20 6c 65 6e 3b 0a t pid, fd, len;.
ecfc0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
ecfd0 2a 6b 65 79 20 3d 20 28 75 6e 73 69 67 6e 65 64 *key = (unsigned
ecfe0 20 63 68 61 72 20 2a 29 70 48 6f 73 74 49 44 3b char *)pHostID;
ecff0 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28 6b 65 79 . . memset(key
ed000 2c 20 30 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b , 0, HOSTIDLEN);
ed010 0a 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 66 64 . len = 0;. fd
ed020 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 = open("/dev/ur
ed030 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 andom", O_RDONLY
ed040 29 3b 0a 20 20 69 66 28 20 66 64 3e 3d 30 20 29 );. if( fd>=0 )
ed050 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 72 65 61 64 {. len = read
ed060 28 66 64 2c 20 6b 65 79 2c 20 48 4f 53 54 49 44 (fd, key, HOSTID
ed070 4c 45 4e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 LEN);. close(
ed080 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 fd); /* silently
ed090 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 leak the fd if
ed0a0 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 7d 0a it fails */. }.
ed0b0 20 20 69 66 28 20 6c 65 6e 20 3c 20 48 4f 53 54 if( len < HOST
ed0c0 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 74 69 6d IDLEN ){. tim
ed0d0 65 5f 74 20 74 3b 0a 20 20 20 20 74 69 6d 65 28 e_t t;. time(
ed0e0 26 74 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 &t);. memcpy(
ed0f0 6b 65 79 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 key, &t, sizeof(
ed100 74 29 29 3b 0a 20 20 20 20 70 69 64 20 3d 20 67 t));. pid = g
ed110 65 74 70 69 64 28 29 3b 0a 20 20 20 20 6d 65 6d etpid();. mem
ed120 63 70 79 28 26 6b 65 79 5b 73 69 7a 65 6f 66 28 cpy(&key[sizeof(
ed130 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f t)], &pid, sizeo
ed140 66 28 70 69 64 29 29 3b 0a 20 20 7d 0a 20 20 0a f(pid));. }. .
ed150 23 69 66 64 65 66 20 4d 41 4b 45 5f 50 52 45 54 #ifdef MAKE_PRET
ed160 54 59 5f 48 4f 53 54 49 44 0a 20 20 7b 0a 20 20 TY_HOSTID. {.
ed170 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 2f 2a 20 int i;. /*
ed180 66 69 6c 74 65 72 20 74 68 65 20 62 79 74 65 73 filter the bytes
ed190 20 69 6e 74 6f 20 70 72 69 6e 74 61 62 6c 65 20 into printable
ed1a0 61 73 63 69 69 20 63 68 61 72 61 63 74 65 72 73 ascii characters
ed1b0 20 61 6e 64 20 4e 55 4c 20 74 65 72 6d 69 6e 61 and NUL termina
ed1c0 74 65 20 2a 2f 0a 20 20 20 20 6b 65 79 5b 28 48 te */. key[(H
ed1d0 4f 53 54 49 44 4c 45 4e 2d 31 29 5d 20 3d 20 30 OSTIDLEN-1)] = 0
ed1e0 78 30 30 3b 0a 20 20 20 20 66 6f 72 28 20 69 3d x00;. for( i=
ed1f0 30 3b 20 69 3c 28 48 4f 53 54 49 44 4c 45 4e 2d 0; i<(HOSTIDLEN-
ed200 31 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 1); i++ ){.
ed210 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70 unsigned char p
ed220 61 20 3d 20 6b 65 79 5b 69 5d 26 30 78 37 46 3b a = key[i]&0x7F;
ed230 0a 20 20 20 20 20 20 69 66 28 20 70 61 3c 30 78 . if( pa<0x
ed240 32 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 20 ){. ke
ed250 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 y[i] = (key[i]&0
ed260 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 x80 == 0x80) ? p
ed270 61 2b 30 78 34 30 20 3a 20 70 61 2b 30 78 32 30 a+0x40 : pa+0x20
ed280 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
ed290 28 20 70 61 3d 3d 30 78 37 46 20 29 7b 0a 20 20 ( pa==0x7F ){.
ed2a0 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 key[i] = (
ed2b0 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 key[i]&0x80 == 0
ed2c0 78 38 30 29 20 3f 20 70 61 3d 30 78 32 30 20 3a x80) ? pa=0x20 :
ed2d0 20 70 61 2b 30 78 37 45 3b 0a 20 20 20 20 20 20 pa+0x7E;.
ed2e0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
ed2f0 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 if. return SQLI
ed300 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 77 72 69 TE_OK;.}../* wri
ed310 74 65 73 20 74 68 65 20 68 6f 73 74 20 69 64 20 tes the host id
ed320 70 61 74 68 20 74 6f 20 70 61 74 68 2c 20 70 61 path to path, pa
ed330 74 68 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 th should be an
ed340 70 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 62 75 pre-allocated bu
ed350 66 66 65 72 0a 2a 2a 20 77 69 74 68 20 65 6e 6f ffer.** with eno
ed360 75 67 68 20 73 70 61 63 65 20 66 6f 72 20 61 20 ugh space for a
ed370 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 path .*/.static
ed380 76 6f 69 64 20 70 72 6f 78 79 47 65 74 48 6f 73 void proxyGetHos
ed390 74 49 44 50 61 74 68 28 63 68 61 72 20 2a 70 61 tIDPath(char *pa
ed3a0 74 68 2c 20 73 69 7a 65 5f 74 20 6c 65 6e 29 7b th, size_t len){
ed3b0 0a 20 20 73 74 72 6c 63 70 79 28 70 61 74 68 2c . strlcpy(path,
ed3c0 20 48 4f 53 54 49 44 50 41 54 48 2c 20 6c 65 6e HOSTIDPATH, len
ed3d0 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
ed3e0 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 _TEST. if( sqli
ed3f0 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3e 30 te3_hostid_num>0
ed400 20 29 7b 0a 20 20 20 20 63 68 61 72 20 73 75 66 ){. char suf
ed410 66 69 78 5b 32 5d 20 3d 20 22 31 22 3b 0a 20 20 fix[2] = "1";.
ed420 20 20 73 75 66 66 69 78 5b 30 5d 20 3d 20 73 75 suffix[0] = su
ed430 66 66 69 78 5b 30 5d 20 2b 20 73 71 6c 69 74 65 ffix[0] + sqlite
ed440 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 3b 0a 20 20 3_hostid_num;.
ed450 20 20 73 74 72 6c 63 61 74 28 70 61 74 68 2c 20 strlcat(path,
ed460 73 75 66 66 69 78 2c 20 6c 65 6e 29 3b 0a 20 20 suffix, len);.
ed470 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 53 54 52 41 }.#endif. OSTRA
ed480 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 50 41 CE3("GETHOSTIDPA
ed490 54 48 20 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 TH %s pid=%d\n"
ed4a0 2c 20 70 61 74 68 2c 20 67 65 74 70 69 64 28 29 , path, getpid()
ed4b0 29 3b 0a 7d 0a 0a 2f 2a 20 67 65 74 20 74 68 65 );.}../* get the
ed4c0 20 68 6f 73 74 20 49 44 20 66 72 6f 6d 20 61 20 host ID from a
ed4d0 73 71 6c 69 74 65 20 68 6f 73 74 69 64 20 66 69 sqlite hostid fi
ed4e0 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 le stored in the
ed4f0 20 0a 2a 2a 20 75 73 65 72 2d 73 70 65 63 69 66 .** user-specif
ed500 69 63 20 74 6d 70 20 64 69 72 65 63 74 6f 72 79 ic tmp directory
ed510 2c 20 63 72 65 61 74 65 20 74 68 65 20 49 44 20 , create the ID
ed520 69 66 20 69 74 27 73 20 6e 6f 74 20 74 68 65 72 if it's not ther
ed530 65 20 61 6c 72 65 61 64 79 20 0a 2a 2f 0a 73 74 e already .*/.st
ed540 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 atic int proxyGe
ed550 74 48 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48 tHostID(char *pH
ed560 6f 73 74 49 44 2c 20 69 6e 74 20 2a 70 45 72 72 ostID, int *pErr
ed570 6f 72 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 or){. int fd;.
ed580 20 63 68 61 72 20 70 61 74 68 5b 4d 41 58 50 41 char path[MAXPA
ed590 54 48 4c 45 4e 5d 3b 20 0a 20 20 73 69 7a 65 5f THLEN]; . size_
ed5a0 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 72 63 3d t len;. int rc=
ed5b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 72 SQLITE_OK;.. pr
ed5c0 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 oxyGetHostIDPath
ed5d0 28 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 (path, MAXPATHLE
ed5e0 4e 29 3b 0a 20 20 2f 2a 20 74 72 79 20 74 6f 20 N);. /* try to
ed5f0 63 72 65 61 74 65 20 74 68 65 20 68 6f 73 74 20 create the host
ed600 49 44 20 66 69 6c 65 2c 20 69 66 20 69 74 20 61 ID file, if it a
ed610 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 72 65 lready exists re
ed620 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ad the contents
ed630 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 */. fd = open(p
ed640 61 74 68 2c 20 4f 5f 43 52 45 41 54 7c 4f 5f 57 ath, O_CREAT|O_W
ed650 52 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 2c 20 30 36 RONLY|O_EXCL, 06
ed660 34 34 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 44);. if( fd<0
ed670 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3d 65 ){. int err=e
ed680 72 72 6e 6f 3b 0a 09 09 0a 20 20 20 20 69 66 28 rrno;.... if(
ed690 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b 0a err!=EEXIST ){.
ed6a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 #ifdef SQLITE_PR
ed6b0 4f 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 OXY_DEBUG /* set
ed6c0 20 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f the sqlite erro
ed6d0 72 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 r message instea
ed6e0 64 20 2a 2f 0a 20 20 20 20 20 20 66 70 72 69 6e d */. fprin
ed6f0 74 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 tf(stderr, "sqli
ed700 74 65 20 65 72 72 6f 72 20 63 72 65 61 74 69 6e te error creatin
ed710 67 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 g host ID file %
ed720 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 s: %s\n",.
ed730 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 path, st
ed740 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 rerror(err));.#e
ed750 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 ndif. retur
ed760 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 n SQLITE_PERM;.
ed770 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 75 6c }. /* coul
ed780 64 6e 27 74 20 63 72 65 61 74 65 20 74 68 65 20 dn't create the
ed790 66 69 6c 65 2c 20 72 65 61 64 20 69 74 20 69 6e file, read it in
ed7a0 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 66 64 20 stead */. fd
ed7b0 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 52 = open(path, O_R
ed7c0 44 4f 4e 4c 59 7c 4f 5f 45 58 43 4c 29 3b 0a 20 DONLY|O_EXCL);.
ed7d0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 23 if( fd<0 ){.#
ed7e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 52 4f ifdef SQLITE_PRO
ed7f0 58 59 5f 44 45 42 55 47 20 2f 2a 20 73 65 74 20 XY_DEBUG /* set
ed800 74 68 65 20 73 71 6c 69 74 65 20 65 72 72 6f 72 the sqlite error
ed810 20 6d 65 73 73 61 67 65 20 69 6e 73 74 65 61 64 message instead
ed820 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65 72 */. int er
ed830 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 r = errno;.
ed840 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
ed850 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6f "sqlite error o
ed860 70 65 6e 69 6e 67 20 68 6f 73 74 20 49 44 20 66 pening host ID f
ed870 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20 ile %s: %s\n",.
ed880 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74 pat
ed890 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 h, strerror(err)
ed8a0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
ed8b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
ed8c0 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 RM;. }. le
ed8d0 6e 20 3d 20 70 72 65 61 64 28 66 64 2c 20 70 48 n = pread(fd, pH
ed8e0 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e ostID, HOSTIDLEN
ed8f0 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 , 0);. if( le
ed900 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 n<0 ){. *pE
ed910 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rror = errno;.
ed920 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ed930 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 20 20 IOERR_READ;.
ed940 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f }else if( len<HO
ed950 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 STIDLEN ){.
ed960 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 *pError = 0;.
ed970 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ed980 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
ed990 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 ;. }. clos
ed9a0 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 e(fd); /* silent
ed9b0 6c 79 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 ly leak the fd i
ed9c0 66 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 f it fails */.
ed9d0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 OSTRACE3("GETH
ed9e0 4f 53 54 49 44 20 20 72 65 61 64 20 25 73 20 70 OSTID read %s p
ed9f0 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 id=%d\n", pHostI
eda00 44 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 D, getpid());.
eda10 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
eda20 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 77 65 27 else{. /* we'
eda30 72 65 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 re creating the
eda40 68 6f 73 74 20 49 44 20 66 69 6c 65 20 28 75 73 host ID file (us
eda50 65 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e e a random strin
eda60 67 20 6f 66 20 62 79 74 65 73 29 20 2a 2f 0a 20 g of bytes) */.
eda70 20 20 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 proxyGenerate
eda80 48 6f 73 74 49 44 28 70 48 6f 73 74 49 44 29 3b HostID(pHostID);
eda90 0a 20 20 20 20 6c 65 6e 20 3d 20 70 77 72 69 74 . len = pwrit
edaa0 65 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 e(fd, pHostID, H
edab0 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 OSTIDLEN, 0);.
edac0 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 if( len<0 ){.
edad0 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 *pError = e
edae0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
edaf0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
edb00 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 ITE;. }else i
edb10 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e f( len<HOSTIDLEN
edb20 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f ){. *pErro
edb30 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 r = 0;. rc
edb40 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 = SQLITE_IOERR_W
edb50 52 49 54 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 RITE;. }.
edb60 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 close(fd); /* si
edb70 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 lently leak the
edb80 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a fd if it fails *
edb90 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 /. OSTRACE3("
edba0 47 45 54 48 4f 53 54 49 44 20 20 77 72 6f 74 65 GETHOSTID wrote
edbb0 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %s pid=%d\n", p
edbc0 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29 HostID, getpid()
edbd0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
edbe0 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 ;. }.}..static
edbf0 69 6e 74 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b int proxyGetLock
edc00 50 61 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 Path(const char
edc10 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a 6c *dbPath, char *l
edc20 50 61 74 68 2c 20 73 69 7a 65 5f 74 20 6d 61 78 Path, size_t max
edc30 4c 65 6e 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b Len){. int len;
edc40 0a 20 20 69 6e 74 20 64 62 4c 65 6e 3b 0a 20 20 . int dbLen;.
edc50 69 6e 74 20 69 3b 0a 0a 23 69 66 64 65 66 20 4c int i;..#ifdef L
edc60 4f 43 4b 50 52 4f 58 59 44 49 52 0a 20 20 6c 65 OCKPROXYDIR. le
edc70 6e 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 n = strlcpy(lPat
edc80 68 2c 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52 2c h, LOCKPROXYDIR,
edc90 20 6d 61 78 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a maxLen);.#else.
edca0 23 20 69 66 64 65 66 20 5f 43 53 5f 44 41 52 57 # ifdef _CS_DARW
edcb0 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 IN_USER_TEMP_DIR
edcc0 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 66 73 74 72 . {. confstr
edcd0 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 (_CS_DARWIN_USER
edce0 5f 54 45 4d 50 5f 44 49 52 2c 20 6c 50 61 74 68 _TEMP_DIR, lPath
edcf0 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 20 6c , maxLen);. l
edd00 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 en = strlcat(lPa
edd10 74 68 2c 20 22 73 71 6c 69 74 65 70 6c 6f 63 6b th, "sqliteplock
edd20 73 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 20 s", maxLen);.
edd30 20 69 66 28 20 6d 6b 64 69 72 28 6c 50 61 74 68 if( mkdir(lPath
edd40 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 , SQLITE_DEFAULT
edd50 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
edd60 53 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20 SIONS) ){.
edd70 2f 2a 20 69 66 20 6d 6b 64 69 72 20 66 61 69 6c /* if mkdir fail
edd80 73 2c 20 68 61 6e 64 6c 65 20 61 73 20 6c 6f 63 s, handle as loc
edd90 6b 20 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 k file creation
edda0 66 61 69 6c 75 72 65 20 2a 2f 0a 23 20 20 69 66 failure */.# if
eddb0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
eddc0 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d . int err =
eddd0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 errno;. if
edde0 28 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 7b ( err!=EEXIST ){
eddf0 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 . fprintf
ede00 28 73 74 64 65 72 72 2c 20 22 70 72 6f 78 79 47 (stderr, "proxyG
ede10 65 74 4c 6f 63 6b 50 61 74 68 3a 20 6d 6b 64 69 etLockPath: mkdi
ede20 72 28 25 73 2c 30 25 6f 29 20 65 72 72 6f 72 20 r(%s,0%o) error
ede30 25 64 20 25 73 5c 6e 22 2c 20 6c 50 61 74 68 2c %d %s\n", lPath,
ede40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ede50 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
ede60 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
ede70 49 4f 4e 53 2c 20 65 72 72 2c 20 73 74 72 65 72 IONS, err, strer
ede80 72 6f 72 28 65 72 72 29 29 3b 0a 20 20 20 20 20 ror(err));.
ede90 20 7d 0a 23 20 20 65 6e 64 69 66 0a 20 20 20 20 }.# endif.
edea0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f 53 54 }else{. OST
edeb0 52 41 43 45 33 28 22 47 45 54 4c 4f 43 4b 50 41 RACE3("GETLOCKPA
edec0 54 48 20 20 6d 6b 64 69 72 20 25 73 20 70 69 64 TH mkdir %s pid
eded0 3d 25 64 5c 6e 22 2c 20 6c 50 61 74 68 2c 20 67 =%d\n", lPath, g
edee0 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 7d 0a etpid());. }.
edef0 20 20 20 20 0a 20 20 7d 0a 23 20 65 6c 73 65 0a . }.# else.
edf00 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79 28 len = strlcpy(
edf10 6c 50 61 74 68 2c 20 22 2f 74 6d 70 2f 22 2c 20 lPath, "/tmp/",
edf20 6d 61 78 4c 65 6e 29 3b 0a 23 20 65 6e 64 69 66 maxLen);.# endif
edf30 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6c .#endif.. if( l
edf40 50 61 74 68 5b 6c 65 6e 2d 31 5d 21 3d 27 2f 27 Path[len-1]!='/'
edf50 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 ){. len = st
edf60 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 2f 22 rlcat(lPath, "/"
edf70 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 7d 0a 20 , maxLen);. }.
edf80 20 0a 20 20 2f 2a 20 74 72 61 6e 73 66 6f 72 6d . /* transform
edf90 20 74 68 65 20 64 62 20 70 61 74 68 20 74 6f 20 the db path to
edfa0 61 20 75 6e 69 71 75 65 20 63 61 63 68 65 20 6e a unique cache n
edfb0 61 6d 65 20 2a 2f 0a 20 20 64 62 4c 65 6e 20 3d ame */. dbLen =
edfc0 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 (int)strlen(dbP
edfd0 61 74 68 29 3b 0a 20 20 66 6f 72 28 20 69 3d 30 ath);. for( i=0
edfe0 3b 20 69 3c 64 62 4c 65 6e 20 26 26 20 28 69 2b ; i<dbLen && (i+
edff0 6c 65 6e 2b 37 29 3c 6d 61 78 4c 65 6e 3b 20 69 len+7)<maxLen; i
ee000 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20 ++){. char c
ee010 3d 20 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 = dbPath[i];.
ee020 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 20 3d 20 lPath[i+len] =
ee030 28 63 3d 3d 27 2f 27 29 3f 27 5f 27 3a 63 3b 0a (c=='/')?'_':c;.
ee040 20 20 7d 0a 20 20 6c 50 61 74 68 5b 69 2b 6c 65 }. lPath[i+le
ee050 6e 5d 3d 27 5c 30 27 3b 0a 20 20 73 74 72 6c 63 n]='\0';. strlc
ee060 61 74 28 6c 50 61 74 68 2c 20 22 3a 61 75 74 6f at(lPath, ":auto
ee070 3a 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20 20 72 :", maxLen);. r
ee080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
ee090 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
ee0a0 20 61 20 6e 65 77 20 56 46 53 20 66 69 6c 65 20 a new VFS file
ee0b0 64 65 73 63 72 69 70 74 6f 72 20 28 73 74 6f 72 descriptor (stor
ee0c0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 ed in memory obt
ee0d0 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 ained from.** sq
ee0e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 29 20 61 6e lite3_malloc) an
ee0f0 64 20 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 d open the file
ee100 6e 61 6d 65 64 20 22 70 61 74 68 22 20 69 6e 20 named "path" in
ee110 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
ee120 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 tor..**.** The c
ee130 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 aller is respons
ee140 69 62 6c 65 20 6e 6f 74 20 6f 6e 6c 79 20 66 6f ible not only fo
ee150 72 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 66 69 r closing the fi
ee160 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a le descriptor.**
ee170 20 62 75 74 20 61 6c 73 6f 20 66 6f 72 20 66 72 but also for fr
ee180 65 65 69 6e 67 20 74 68 65 20 6d 65 6d 6f 72 79 eeing the memory
ee190 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
ee1a0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
ee1b0 70 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ptor..*/.static
ee1c0 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 55 int proxyCreateU
ee1d0 6e 69 78 46 69 6c 65 28 63 6f 6e 73 74 20 63 68 nixFile(const ch
ee1e0 61 72 20 2a 70 61 74 68 2c 20 75 6e 69 78 46 69 ar *path, unixFi
ee1f0 6c 65 20 2a 2a 70 70 46 69 6c 65 29 20 7b 0a 20 le **ppFile) {.
ee200 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 3b unixFile *pNew;
ee210 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 53 . int flags = S
ee220 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
ee230 44 42 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 DB|SQLITE_OPEN_C
ee240 52 45 41 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 REATE|SQLITE_OPE
ee250 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 69 N_READWRITE;. i
ee260 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
ee270 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 K;. sqlite3_vfs
ee280 20 64 75 6d 6d 79 56 66 73 3b 0a 0a 20 20 70 4e dummyVfs;.. pN
ee290 65 77 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a ew = (unixFile *
ee2a0 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 )sqlite3_malloc(
ee2b0 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 sizeof(unixFile)
ee2c0 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 );. if( !pNew )
ee2d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
ee2e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
ee2f0 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c memset(pNew, 0,
ee300 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 sizeof(unixFile
ee310 29 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 6c 20 75 ));.. /* Call u
ee320 6e 69 78 4f 70 65 6e 28 29 20 74 6f 20 6f 70 65 nixOpen() to ope
ee330 6e 20 74 68 65 20 70 72 6f 78 79 20 66 69 6c 65 n the proxy file
ee340 2e 20 54 68 65 20 66 6c 61 67 73 20 70 61 73 73 . The flags pass
ee350 65 64 20 74 6f 20 75 6e 69 78 4f 70 65 6e 28 29 ed to unixOpen()
ee360 0a 20 20 2a 2a 20 73 75 67 67 65 73 74 20 74 68 . ** suggest th
ee370 61 74 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e at the file bein
ee380 67 20 6f 70 65 6e 65 64 20 69 73 20 61 20 22 6d g opened is a "m
ee390 61 69 6e 20 64 61 74 61 62 61 73 65 22 2e 20 54 ain database". T
ee3a0 68 69 73 20 69 73 0a 20 20 2a 2a 20 6e 65 63 65 his is. ** nece
ee3b0 73 73 61 72 79 20 61 73 20 6f 74 68 65 72 20 66 ssary as other f
ee3c0 69 6c 65 20 74 79 70 65 73 20 64 6f 20 6e 6f 74 ile types do not
ee3d0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 75 70 necessarily sup
ee3e0 70 6f 72 74 20 6c 6f 63 6b 69 6e 67 2e 20 49 74 port locking. It
ee3f0 0a 20 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20 . ** is better
ee400 74 6f 20 75 73 65 20 75 6e 69 78 4f 70 65 6e 28 to use unixOpen(
ee410 29 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 70 65 ) instead of ope
ee420 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 69 ning the file di
ee430 72 65 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a rectly with. **
ee440 20 6f 70 65 6e 28 29 2c 20 61 73 20 75 6e 69 78 open(), as unix
ee450 4f 70 65 6e 28 29 20 73 65 74 73 20 75 70 20 74 Open() sets up t
ee460 68 65 20 76 61 72 69 6f 75 73 20 6d 65 63 68 61 he various mecha
ee470 6e 69 73 6d 73 20 72 65 71 75 69 72 65 64 20 74 nisms required t
ee480 6f 0a 20 20 2a 2a 20 6d 61 6b 65 20 73 75 72 65 o. ** make sure
ee490 20 61 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 a call to close
ee4a0 28 29 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 () does not caus
ee4b0 65 20 74 68 65 20 73 79 73 74 65 6d 20 74 6f 20 e the system to
ee4c0 64 69 73 63 61 72 64 0a 20 20 2a 2a 20 50 4f 53 discard. ** POS
ee4d0 49 58 20 6c 6f 63 6b 73 20 70 72 65 6d 61 74 75 IX locks prematu
ee4e0 72 65 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 rely.. **. **
ee4f0 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 It is important
ee500 74 68 61 74 20 74 68 65 20 78 4f 70 65 6e 20 6d that the xOpen m
ee510 65 6d 62 65 72 20 6f 66 20 74 68 65 20 56 46 53 ember of the VFS
ee520 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 74 object passed t
ee530 6f 20 0a 20 20 2a 2a 20 75 6e 69 78 4f 70 65 6e o . ** unixOpen
ee540 28 29 20 69 73 20 4e 55 4c 4c 2e 20 54 68 69 73 () is NULL. This
ee550 20 74 65 6c 6c 73 20 75 6e 69 78 4f 70 65 6e 28 tells unixOpen(
ee560 29 20 6d 61 79 20 74 72 79 20 74 6f 20 6f 70 65 ) may try to ope
ee570 6e 20 61 20 70 72 6f 78 79 2d 66 69 6c 65 20 0a n a proxy-file .
ee580 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 72 6f ** for the pro
ee590 78 79 2d 66 69 6c 65 20 28 63 72 65 61 74 69 6e xy-file (creatin
ee5a0 67 20 61 20 70 6f 74 65 6e 74 69 61 6c 20 69 6e g a potential in
ee5b0 66 69 6e 69 74 65 20 6c 6f 6f 70 29 2e 0a 20 20 finite loop)..
ee5c0 2a 2f 0a 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 */. dummyVfs.pA
ee5d0 70 70 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 ppData = (void*)
ee5e0 26 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 &autolockIoFinde
ee5f0 72 3b 0a 20 20 64 75 6d 6d 79 56 66 73 2e 78 4f r;. dummyVfs.xO
ee600 70 65 6e 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 pen = 0;. rc =
ee610 75 6e 69 78 4f 70 65 6e 28 26 64 75 6d 6d 79 56 unixOpen(&dummyV
ee620 66 73 2c 20 70 61 74 68 2c 20 28 73 71 6c 69 74 fs, path, (sqlit
ee630 65 33 5f 66 69 6c 65 20 2a 29 70 4e 65 77 2c 20 e3_file *)pNew,
ee640 66 6c 61 67 73 2c 20 26 66 6c 61 67 73 29 3b 0a flags, &flags);.
ee650 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
ee660 5f 4f 4b 20 26 26 20 28 66 6c 61 67 73 26 53 51 _OK && (flags&SQ
ee670 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
ee680 4c 59 29 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d LY) ){. pNew-
ee690 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 >pMethod->xClose
ee6a0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ((sqlite3_file *
ee6b0 29 70 4e 65 77 29 3b 0a 20 20 20 20 72 63 20 3d )pNew);. rc =
ee6c0 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
ee6d0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 ;. }.. if( rc!
ee6e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ee6f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
ee700 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d New);. pNew =
ee710 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 46 69 0;. }.. *ppFi
ee720 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 72 65 74 le = pNew;. ret
ee730 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 urn rc;.}../* ta
ee740 6b 65 73 20 74 68 65 20 63 6f 6e 63 68 20 62 79 kes the conch by
ee750 20 74 61 6b 69 6e 67 20 61 20 73 68 61 72 65 64 taking a shared
ee760 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 64 20 74 lock and read t
ee770 68 65 20 63 6f 6e 74 65 6e 74 73 20 63 6f 6e 63 he contents conc
ee780 68 2c 20 69 66 20 0a 2a 2a 20 6c 6f 63 6b 50 61 h, if .** lockPa
ee790 74 68 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 th is non-NULL,
ee7a0 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 the host ID and
ee7b0 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 20 6d lock file path m
ee7c0 75 73 74 20 6d 61 74 63 68 2e 20 20 41 20 4e 55 ust match. A NU
ee7d0 4c 4c 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 LL .** lockPath
ee7e0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6c means that the l
ee7f0 6f 63 6b 50 61 74 68 20 69 6e 20 74 68 65 20 63 ockPath in the c
ee800 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20 62 onch file will b
ee810 65 20 75 73 65 64 20 69 66 20 74 68 65 20 0a 2a e used if the .*
ee820 2a 20 68 6f 73 74 20 49 44 73 20 6d 61 74 63 68 * host IDs match
ee830 2c 20 6f 72 20 61 20 6e 65 77 20 6c 6f 63 6b 20 , or a new lock
ee840 70 61 74 68 20 77 69 6c 6c 20 62 65 20 67 65 6e path will be gen
ee850 65 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 erated automatic
ee860 61 6c 6c 79 20 0a 2a 2a 20 61 6e 64 20 77 72 69 ally .** and wri
ee870 74 74 65 6e 20 74 6f 20 74 68 65 20 63 6f 6e 63 tten to the conc
ee880 68 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 h file..*/.stati
ee890 63 20 69 6e 74 20 70 72 6f 78 79 54 61 6b 65 43 c int proxyTakeC
ee8a0 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 onch(unixFile *p
ee8b0 46 69 6c 65 29 7b 0a 20 20 70 72 6f 78 79 4c 6f File){. proxyLo
ee8c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
ee8d0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
ee8e0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
ee8f0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
ee900 74 3b 20 0a 20 20 0a 20 20 69 66 28 20 70 43 74 t; . . if( pCt
ee910 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e 30 20 29 x->conchHeld>0 )
ee920 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
ee930 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
ee940 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 . unixFile *c
ee950 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d onchFile = pCtx-
ee960 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 >conchFile;.
ee970 63 68 61 72 20 74 65 73 74 56 61 6c 75 65 5b 43 char testValue[C
ee980 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 ONCHLEN];. ch
ee990 61 72 20 63 6f 6e 63 68 56 61 6c 75 65 5b 43 4f ar conchValue[CO
ee9a0 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 NCHLEN];. cha
ee9b0 72 20 6c 6f 63 6b 50 61 74 68 5b 4d 41 58 50 41 r lockPath[MAXPA
ee9c0 54 48 4c 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 THLEN];. char
ee9d0 20 2a 74 4c 6f 63 6b 50 61 74 68 20 3d 20 4e 55 *tLockPath = NU
ee9e0 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d LL;. int rc =
ee9f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
eea00 69 6e 74 20 72 65 61 64 52 63 20 3d 20 53 51 4c int readRc = SQL
eea10 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 ITE_OK;. int
eea20 73 79 6e 63 50 65 72 6d 73 20 3d 20 30 3b 0a 0a syncPerms = 0;..
eea30 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 54 41 OSTRACE4("TA
eea40 4b 45 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 KECONCH %d for
eea50 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f %s pid=%d\n", co
eea60 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 nchFile->h,.
eea70 20 20 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e (pCtx->
eea80 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 lockProxyPath ?
eea90 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
eeaa0 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c ath : ":auto:"),
eeab0 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 20 getpid());..
eeac0 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d rc = conchFile-
eead0 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 >pMethod->xLock(
eeae0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 (sqlite3_file*)c
eeaf0 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 onchFile, SHARED
eeb00 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 _LOCK);. if(
eeb10 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
eeb20 0a 20 20 20 20 20 20 69 6e 74 20 70 45 72 72 6f . int pErro
eeb30 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 65 6d r = 0;. mem
eeb40 73 65 74 28 74 65 73 74 56 61 6c 75 65 2c 20 30 set(testValue, 0
eeb50 2c 20 43 4f 4e 43 48 4c 45 4e 29 3b 20 2f 2a 20 , CONCHLEN); /*
eeb60 63 6f 6e 63 68 20 69 73 20 66 69 78 65 64 20 73 conch is fixed s
eeb70 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 ize */. rc
eeb80 3d 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 = proxyGetHostID
eeb90 28 74 65 73 74 56 61 6c 75 65 2c 20 26 70 45 72 (testValue, &pEr
eeba0 72 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ror);. if(
eebb0 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 (rc&0xff)==SQLIT
eebc0 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 E_IOERR ){.
eebd0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
eebe0 72 6e 6f 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 rno = pError;.
eebf0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
eec00 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
eec10 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ath ){. s
eec20 74 72 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 trlcpy(&testValu
eec30 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 70 43 e[HOSTIDLEN], pC
eec40 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
eec50 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a h, MAXPATHLEN);.
eec60 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
eec70 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
eec80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
eec90 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b o end_takeconch;
eeca0 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 . }. .
eecb0 72 65 61 64 52 63 20 3d 20 75 6e 69 78 52 65 61 readRc = unixRea
eecc0 64 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 d((sqlite3_file
eecd0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 63 6f 6e *)conchFile, con
eece0 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 chValue, CONCHLE
eecf0 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 N, 0);. if( r
eed00 65 61 64 52 63 21 3d 53 51 4c 49 54 45 5f 49 4f eadRc!=SQLITE_IO
eed10 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
eed20 7b 0a 20 20 20 20 20 20 69 66 28 20 72 65 61 64 {. if( read
eed30 52 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b Rc!=SQLITE_OK ){
eed40 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 72 63 . if( (rc
eed50 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 &0xff)==SQLITE_I
eed60 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 OERR ){.
eed70 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
eed80 6e 6f 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e no = conchFile->
eed90 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 lastErrno;.
eeda0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 }. rc
eedb0 3d 20 72 65 61 64 52 63 3b 0a 20 20 20 20 20 20 = readRc;.
eedc0 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 goto end_takec
eedd0 6f 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 onch;. }.
eede0 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 63 6f /* if the co
eedf0 6e 63 68 20 68 61 73 20 64 61 74 61 20 63 6f 6d nch has data com
eee00 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 pare the content
eee10 73 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 s */. if( !
eee20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
eee30 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ath ){. /
eee40 2a 20 66 6f 72 20 61 75 74 6f 2d 6e 61 6d 65 64 * for auto-named
eee50 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 local lock file
eee60 2c 20 6a 75 73 74 20 63 68 65 63 6b 20 74 68 65 , just check the
eee70 20 68 6f 73 74 20 49 44 20 61 6e 64 20 77 65 27 host ID and we'
eee80 6c 6c 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 75 ll. ** u
eee90 73 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 se the local loc
eeea0 6b 20 66 69 6c 65 20 70 61 74 68 20 74 68 61 74 k file path that
eeeb0 27 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 's already in th
eeec0 65 72 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 ere */. i
eeed0 66 28 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 56 f( !memcmp(testV
eeee0 61 6c 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 alue, conchValue
eeef0 2c 20 48 4f 53 54 49 44 4c 45 4e 29 20 29 7b 0a , HOSTIDLEN) ){.
eef00 20 20 20 20 20 20 20 20 20 20 74 4c 6f 63 6b 50 tLockP
eef10 61 74 68 20 3d 20 28 63 68 61 72 20 2a 29 26 63 ath = (char *)&c
eef20 6f 6e 63 68 56 61 6c 75 65 5b 48 4f 53 54 49 44 onchValue[HOSTID
eef30 4c 45 4e 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 LEN];.
eef40 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e goto end_takecon
eef50 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ch;. }.
eef60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
eef70 20 20 20 2f 2a 20 77 65 27 76 65 20 67 6f 74 20 /* we've got
eef80 74 68 65 20 63 6f 6e 63 68 20 69 66 20 63 6f 6e the conch if con
eef90 63 68 56 61 6c 75 65 20 6d 61 74 63 68 65 73 20 chValue matches
eefa0 6f 75 72 20 70 61 74 68 20 61 6e 64 20 68 6f 73 our path and hos
eefb0 74 20 49 44 20 2a 2f 0a 20 20 20 20 20 20 20 20 t ID */.
eefc0 69 66 28 20 21 6d 65 6d 63 6d 70 28 74 65 73 74 if( !memcmp(test
eefd0 56 61 6c 75 65 2c 20 63 6f 6e 63 68 56 61 6c 75 Value, conchValu
eefe0 65 2c 20 43 4f 4e 43 48 4c 45 4e 29 20 29 7b 0a e, CONCHLEN) ){.
eeff0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
ef000 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 nd_takeconch;.
ef010 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
ef020 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
ef030 20 2f 2a 20 61 20 73 68 6f 72 74 20 72 65 61 64 /* a short read
ef040 20 6d 65 61 6e 73 20 77 65 27 72 65 20 22 63 72 means we're "cr
ef050 65 61 74 69 6e 67 22 20 74 68 65 20 63 6f 6e 63 eating" the conc
ef060 68 20 28 65 76 65 6e 20 74 68 6f 75 67 68 20 69 h (even though i
ef070 74 20 63 6f 75 6c 64 20 0a 20 20 20 20 20 20 2a t could . *
ef080 2a 20 68 61 76 65 20 62 65 65 6e 20 75 73 65 72 * have been user
ef090 2d 69 6e 74 65 72 76 65 6e 74 69 6f 6e 29 2c 20 -intervention),
ef0a0 69 66 20 77 65 20 61 63 71 75 69 72 65 20 74 68 if we acquire th
ef0b0 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
ef0c0 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 27 6c 6c ,. ** we'll
ef0d0 20 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 try to match th
ef0e0 65 20 63 75 72 72 65 6e 74 20 6f 6e 2d 64 69 73 e current on-dis
ef0f0 6b 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 k permissions of
ef100 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 the database.
ef110 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 79 6e */. syn
ef120 63 50 65 72 6d 73 20 3d 20 31 3b 0a 20 20 20 20 cPerms = 1;.
ef130 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 65 69 }. . /* ei
ef140 74 68 65 72 20 63 6f 6e 63 68 20 77 61 73 20 65 ther conch was e
ef150 6d 74 70 79 20 6f 72 20 64 69 64 6e 27 74 20 6d mtpy or didn't m
ef160 61 74 63 68 20 2a 2f 0a 20 20 20 20 69 66 28 20 atch */. if(
ef170 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 !pCtx->lockProxy
ef180 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 70 72 Path ){. pr
ef190 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 70 oxyGetLockPath(p
ef1a0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 6c 6f 63 Ctx->dbPath, loc
ef1b0 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 kPath, MAXPATHLE
ef1c0 4e 29 3b 0a 20 20 20 20 20 20 74 4c 6f 63 6b 50 N);. tLockP
ef1d0 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a ath = lockPath;.
ef1e0 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 strlcpy(&t
ef1f0 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c estValue[HOSTIDL
ef200 45 4e 5d 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d EN], lockPath, M
ef210 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 AXPATHLEN);.
ef220 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 75 70 }. . /* up
ef230 64 61 74 65 20 63 6f 6e 63 68 20 77 69 74 68 20 date conch with
ef240 68 6f 73 74 20 61 6e 64 20 70 61 74 68 20 28 74 host and path (t
ef250 68 69 73 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 his will fail if
ef260 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20 other process.
ef270 20 20 20 20 2a 2a 20 68 61 73 20 61 20 73 68 61 ** has a sha
ef280 72 65 64 20 6c 6f 63 6b 20 61 6c 72 65 61 64 79 red lock already
ef290 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 63 6f ) */. rc = co
ef2a0 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 nchFile->pMethod
ef2b0 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 ->xLock((sqlite3
ef2c0 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 _file*)conchFile
ef2d0 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b , EXCLUSIVE_LOCK
ef2e0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
ef2f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
ef300 20 20 72 63 20 3d 20 75 6e 69 78 57 72 69 74 65 rc = unixWrite
ef310 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ((sqlite3_file *
ef320 29 63 6f 6e 63 68 46 69 6c 65 2c 20 74 65 73 74 )conchFile, test
ef330 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c Value, CONCHLEN,
ef340 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
ef350 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
ef360 73 79 6e 63 50 65 72 6d 73 20 29 7b 0a 20 20 20 syncPerms ){.
ef370 20 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 struct stat
ef380 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 69 6e buf;. in
ef390 74 20 65 72 72 20 3d 20 66 73 74 61 74 28 70 46 t err = fstat(pF
ef3a0 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 ile->h, &buf);.
ef3b0 20 20 20 20 20 20 20 69 66 28 20 65 72 72 3d 3d if( err==
ef3c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 0 ){. /
ef3d0 2a 20 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 * try to match t
ef3e0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
ef3f0 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c 20 69 67 permissions, ig
ef400 6e 6f 72 65 20 66 61 69 6c 75 72 65 20 2a 2f 0a nore failure */.
ef410 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 #ifndef SQLITE_P
ef420 52 4f 58 59 5f 44 45 42 55 47 0a 20 20 20 20 20 ROXY_DEBUG.
ef430 20 20 20 20 20 66 63 68 6d 6f 64 28 63 6f 6e 63 fchmod(conc
ef440 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 hFile->h, buf.st
ef450 5f 6d 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 20 20 _mode);.#else.
ef460 20 20 20 20 20 20 20 20 69 66 28 20 66 63 68 6d if( fchm
ef470 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c od(conchFile->h,
ef480 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 21 3d 30 buf.st_mode)!=0
ef490 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
ef4a0 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f int code = errno
ef4b0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70 ;. fp
ef4c0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 66 rintf(stderr, "f
ef4d0 63 68 6d 6f 64 20 25 6f 20 46 41 49 4c 45 44 20 chmod %o FAILED
ef4e0 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 0a 20 with %d %s\n",.
ef4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef500 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 2e buf.
ef510 73 74 5f 6d 6f 64 65 2c 20 63 6f 64 65 2c 20 73 st_mode, code, s
ef520 74 72 65 72 72 6f 72 28 63 6f 64 65 29 29 3b 0a trerror(code));.
ef530 20 20 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 } else
ef540 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 {. f
ef550 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
ef560 66 63 68 6d 6f 64 20 25 6f 20 53 55 43 43 45 44 fchmod %o SUCCED
ef570 45 44 5c 6e 22 2c 62 75 66 2e 73 74 5f 6d 6f 64 ED\n",buf.st_mod
ef580 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a e);. }.
ef590 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
ef5a0 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 int cod
ef5b0 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 e = errno;.
ef5c0 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
ef5d0 65 72 72 2c 20 22 53 54 41 54 20 46 41 49 4c 45 err, "STAT FAILE
ef5e0 44 5b 25 64 5d 20 77 69 74 68 20 25 64 20 25 73 D[%d] with %d %s
ef5f0 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 \n", .
ef600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef610 65 72 72 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 err, code, strer
ef620 72 6f 72 28 63 6f 64 65 29 29 3b 0a 23 65 6e 64 ror(code));.#end
ef630 69 66 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 if. }.
ef640 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 }. }. c
ef650 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f onchFile->pMetho
ef660 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 d->xUnlock((sqli
ef670 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
ef680 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b ile, SHARED_LOCK
ef690 29 3b 0a 20 20 0a 65 6e 64 5f 74 61 6b 65 63 6f );. .end_takeco
ef6a0 6e 63 68 3a 0a 20 20 20 20 4f 53 54 52 41 43 45 nch:. OSTRACE
ef6b0 32 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 43 2("TRANSPROXY: C
ef6c0 4c 4f 53 45 20 20 25 64 5c 6e 22 2c 20 70 46 69 LOSE %d\n", pFi
ef6d0 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 69 66 28 20 le->h);. if(
ef6e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
ef6f0 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 pFile->openFlag
ef700 73 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 s ){. if( p
ef710 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 23 69 File->h>=0 ){.#i
ef720 66 64 65 66 20 53 54 52 49 43 54 5f 43 4c 4f 53 fdef STRICT_CLOS
ef730 45 5f 45 52 52 4f 52 0a 20 20 20 20 20 20 20 20 E_ERROR.
ef740 69 66 28 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d if( close(pFile-
ef750 3e 68 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 >h) ){.
ef760 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
ef770 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
ef780 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
ef790 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a TE_IOERR_CLOSE;.
ef7a0 20 20 20 20 20 20 20 20 7d 0a 23 65 6c 73 65 0a }.#else.
ef7b0 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 70 46 close(pF
ef7c0 69 6c 65 2d 3e 68 29 3b 20 2f 2a 20 73 69 6c 65 ile->h); /* sile
ef7d0 6e 74 6c 79 20 6c 65 61 6b 20 66 64 20 69 66 20 ntly leak fd if
ef7e0 66 61 69 6c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 fail */.#endif.
ef7f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 46 69 }. pFi
ef800 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 20 20 le->h = -1;.
ef810 20 20 69 6e 74 20 66 64 20 3d 20 6f 70 65 6e 28 int fd = open(
ef820 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 pCtx->dbPath, pF
ef830 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 0a ile->openFlags,.
ef840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef850 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 SQLITE_DEFAU
ef860 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
ef870 4f 4e 53 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 ONS);. OSTR
ef880 41 43 45 32 28 22 54 52 41 4e 53 50 52 4f 58 59 ACE2("TRANSPROXY
ef890 3a 20 4f 50 45 4e 20 20 25 64 5c 6e 22 2c 20 66 : OPEN %d\n", f
ef8a0 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 64 d);. if( fd
ef8b0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 >=0 ){. p
ef8c0 46 69 6c 65 2d 3e 68 20 3d 20 66 64 3b 0a 20 20 File->h = fd;.
ef8d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
ef8e0 20 20 20 72 63 3d 53 51 4c 49 54 45 5f 43 41 4e rc=SQLITE_CAN
ef8f0 54 4f 50 45 4e 3b 20 2f 2a 20 53 51 4c 49 54 45 TOPEN; /* SQLITE
ef900 5f 42 55 53 59 3f 20 70 72 6f 78 79 54 61 6b 65 _BUSY? proxyTake
ef910 43 6f 6e 63 68 20 63 61 6c 6c 65 64 0a 20 20 20 Conch called.
ef920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef930 20 20 20 20 20 20 20 20 20 20 20 20 64 75 72 69 duri
ef940 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 ng locking */.
ef950 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
ef960 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
ef970 4b 20 26 26 20 21 70 43 74 78 2d 3e 6c 6f 63 6b K && !pCtx->lock
ef980 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 63 Proxy ){. c
ef990 68 61 72 20 2a 70 61 74 68 20 3d 20 74 4c 6f 63 har *path = tLoc
ef9a0 6b 50 61 74 68 20 3f 20 74 4c 6f 63 6b 50 61 74 kPath ? tLockPat
ef9b0 68 20 3a 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 h : pCtx->lockPr
ef9c0 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 2f oxyPath;. /
ef9d0 2a 20 41 43 53 3a 20 4e 65 65 64 20 74 6f 20 6d * ACS: Need to m
ef9e0 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 70 61 ake a copy of pa
ef9f0 74 68 20 73 6f 6d 65 74 69 6d 65 73 20 2a 2f 0a th sometimes */.
efa00 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
efa10 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 70 CreateUnixFile(p
efa20 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63 6b ath, &pCtx->lock
efa30 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 Proxy);. }.
efa40 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
efa50 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 74 _OK ){. pCt
efa60 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 31 x->conchHeld = 1
efa70 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 74 4c 6f ;.. if( tLo
efa80 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 ckPath ){.
efa90 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 pCtx->lockProx
efaa0 79 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 yPath = sqlite3D
efab0 62 53 74 72 44 75 70 28 30 2c 20 74 4c 6f 63 6b bStrDup(0, tLock
efac0 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69 Path);. i
efad0 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f f( pCtx->lockPro
efae0 78 79 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 xy->pMethod == &
efaf0 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a afpIoMethods ){.
efb00 20 20 20 20 20 20 20 20 20 20 28 28 61 66 70 4c ((afpL
efb10 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
efb20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d pCtx->lockProxy-
efb30 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 >lockingContext)
efb40 2d 3e 64 62 50 61 74 68 20 3d 0a 20 20 20 20 20 ->dbPath =.
efb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efb60 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
efb70 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ath;. }.
efb80 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 }. } els
efb90 65 20 7b 0a 20 20 20 20 20 20 63 6f 6e 63 68 46 e {. conchF
efba0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 ile->pMethod->xU
efbb0 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 nlock((sqlite3_f
efbc0 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 ile*)conchFile,
efbd0 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a NO_LOCK);. }.
efbe0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 41 OSTRACE3("TA
efbf0 4b 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e KECONCH %d %s\n
efc00 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c ", conchFile->h,
efc10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 22 rc==SQLITE_OK?"
efc20 6f 6b 22 3a 22 66 61 69 6c 65 64 22 29 3b 0a 20 ok":"failed");.
efc30 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
efc40 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 }.}../*.** If pF
efc50 69 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b ile holds a lock
efc60 20 6f 6e 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 on a conch file
efc70 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 74 , then release t
efc80 68 61 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 hat lock..*/.sta
efc90 74 69 63 20 69 6e 74 20 70 72 6f 78 79 52 65 6c tic int proxyRel
efca0 65 61 73 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 easeConch(unixFi
efcb0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
efcc0 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
efcd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 /* Sub
efce0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 63 routine return c
efcf0 6f 64 65 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f ode */. proxyLo
efd00 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
efd10 74 78 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b tx; /* The lock
efd20 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ing context for
efd30 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 2a the proxy lock *
efd40 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f /. unixFile *co
efd50 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 nchFile;
efd60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
efd70 6f 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 onch file */..
efd80 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 pCtx = (proxyLoc
efd90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 kingContext *)pF
efda0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
efdb0 65 78 74 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 ext;. conchFile
efdc0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 = pCtx->conchFi
efdd0 6c 65 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 le;. OSTRACE4("
efde0 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 RELEASECONCH %d
efdf0 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e for %s pid=%d\n
efe00 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c ", conchFile->h,
efe10 0a 20 20 20 20 20 20 20 20 20 20 20 28 70 43 74 . (pCt
efe20 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
efe30 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f ? pCtx->lockPro
efe40 78 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a xyPath : ":auto:
efe50 22 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 "), .
efe60 67 65 74 70 69 64 28 29 29 3b 0a 20 20 70 43 74 getpid());. pCt
efe70 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 x->conchHeld = 0
efe80 3b 0a 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 ;. rc = conchFi
efe90 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e le->pMethod->xUn
efea0 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 lock((sqlite3_fi
efeb0 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 4e le*)conchFile, N
efec0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 4f 53 54 52 41 O_LOCK);. OSTRA
efed0 43 45 33 28 22 52 45 4c 45 41 53 45 43 4f 4e 43 CE3("RELEASECONC
efee0 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e H %d %s\n", con
efef0 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 chFile->h,.
eff00 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 (rc==SQLIT
eff10 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 E_OK ? "ok" : "f
eff20 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75 ailed"));. retu
eff30 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
eff40 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f Given the name o
eff50 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c f a database fil
eff60 65 2c 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e e, compute the n
eff70 61 6d 65 20 6f 66 20 69 74 73 20 63 6f 6e 63 68 ame of its conch
eff80 20 66 69 6c 65 2e 0a 2a 2a 20 53 74 6f 72 65 20 file..** Store
eff90 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 the conch filena
effa0 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 me in memory obt
effb0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
effc0 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 e3_malloc()..**
effd0 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50 61 74 68 Make *pConchPath
effe0 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 point to the ne
efff0 77 20 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 20 w name. Return
f0000 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
f0010 63 65 73 73 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 cess.** or SQLIT
f0020 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e 61 62 6c E_NOMEM if unabl
f0030 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d 65 6d 6f e to obtain memo
f0040 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 ry..**.** The ca
f0050 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 ller is responsi
f0060 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 ble for ensuring
f0070 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 that the alloca
f0080 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 ted memory.** sp
f0090 61 63 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c ace is eventuall
f00a0 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a y freed..**.** *
f00b0 70 43 6f 6e 63 68 50 61 74 68 20 69 73 20 73 65 pConchPath is se
f00c0 74 20 74 6f 20 4e 55 4c 4c 20 69 66 20 61 20 6d t to NULL if a m
f00d0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
f00e0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a error occurs..*
f00f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
f0100 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 xyCreateConchPat
f0110 68 6e 61 6d 65 28 63 68 61 72 20 2a 64 62 50 61 hname(char *dbPa
f0120 74 68 2c 20 63 68 61 72 20 2a 2a 70 43 6f 6e 63 th, char **pConc
f0130 68 50 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 3b hPath){. int i;
f0140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f0150 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
f0160 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
f0170 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c len = (int)strl
f0180 65 6e 28 64 62 50 61 74 68 29 3b 20 2f 2a 20 4c en(dbPath); /* L
f0190 65 6e 67 74 68 20 6f 66 20 64 61 74 61 62 61 73 ength of databas
f01a0 65 20 66 69 6c 65 6e 61 6d 65 20 2d 20 64 62 50 e filename - dbP
f01b0 61 74 68 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 ath */. char *c
f01c0 6f 6e 63 68 50 61 74 68 3b 20 20 20 20 20 20 20 onchPath;
f01d0 20 20 20 20 20 20 20 2f 2a 20 62 75 66 66 65 72 /* buffer
f01e0 20 69 6e 20 77 68 69 63 68 20 74 6f 20 63 6f 6e in which to con
f01f0 73 74 72 75 63 74 20 63 6f 6e 63 68 20 6e 61 6d struct conch nam
f0200 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 e */.. /* Alloc
f0210 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 ate space for th
f0220 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 e conch filename
f0230 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 and initialize
f0240 74 68 65 20 6e 61 6d 65 20 74 6f 0a 20 20 2a 2a the name to. **
f0250 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
f0260 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 original databa
f0270 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20 0a 20 20 se file. */ .
f0280 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d 20 63 6f *pConchPath = co
f0290 6e 63 68 50 61 74 68 20 3d 20 28 63 68 61 72 20 nchPath = (char
f02a0 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 *)sqlite3_malloc
f02b0 28 6c 65 6e 20 2b 20 38 29 3b 0a 20 20 69 66 28 (len + 8);. if(
f02c0 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30 20 29 7b conchPath==0 ){
f02d0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
f02e0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
f02f0 6d 65 6d 63 70 79 28 63 6f 6e 63 68 50 61 74 68 memcpy(conchPath
f0300 2c 20 64 62 50 61 74 68 2c 20 6c 65 6e 2b 31 29 , dbPath, len+1)
f0310 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 77 20 69 6e ;. . /* now in
f0320 73 65 72 74 20 61 20 22 2e 22 20 62 65 66 6f 72 sert a "." befor
f0330 65 20 74 68 65 20 6c 61 73 74 20 2f 20 63 68 61 e the last / cha
f0340 72 61 63 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 racter */. for(
f0350 20 69 3d 28 6c 65 6e 2d 31 29 3b 20 69 3e 3d 30 i=(len-1); i>=0
f0360 3b 20 69 2d 2d 20 29 7b 0a 20 20 20 20 69 66 28 ; i-- ){. if(
f0370 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 3d 27 conchPath[i]=='
f0380 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 2b 2b 3b /' ){. i++;
f0390 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
f03a0 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 63 68 50 }. }. conchP
f03b0 61 74 68 5b 69 5d 3d 27 2e 27 3b 0a 20 20 77 68 ath[i]='.';. wh
f03c0 69 6c 65 20 28 20 69 3c 6c 65 6e 20 29 7b 0a 20 ile ( i<len ){.
f03d0 20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 2b 31 conchPath[i+1
f03e0 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0a 20 20 20 ]=dbPath[i];.
f03f0 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 i++;. }.. /*
f0400 61 70 70 65 6e 64 20 74 68 65 20 22 2d 63 6f 6e append the "-con
f0410 63 68 22 20 73 75 66 66 69 78 20 74 6f 20 74 68 ch" suffix to th
f0420 65 20 66 69 6c 65 20 2a 2f 0a 20 20 6d 65 6d 63 e file */. memc
f0430 70 79 28 26 63 6f 6e 63 68 50 61 74 68 5b 69 2b py(&conchPath[i+
f0440 31 5d 2c 20 22 2d 63 6f 6e 63 68 22 2c 20 37 29 1], "-conch", 7)
f0450 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 ;. assert( (int
f0460 29 73 74 72 6c 65 6e 28 63 6f 6e 63 68 50 61 74 )strlen(conchPat
f0470 68 29 20 3d 3d 20 6c 65 6e 2b 37 20 29 3b 0a 0a h) == len+7 );..
f0480 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f0490 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 6b 65 73 OK;.}.../* Takes
f04a0 20 61 20 66 75 6c 6c 79 20 63 6f 6e 66 69 67 75 a fully configu
f04b0 72 65 64 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e red proxy lockin
f04c0 67 2d 73 74 79 6c 65 20 75 6e 69 78 20 66 69 6c g-style unix fil
f04d0 65 20 61 6e 64 20 73 77 69 74 63 68 65 73 0a 2a e and switches.*
f04e0 2a 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b * the local lock
f04f0 20 66 69 6c 65 20 70 61 74 68 20 0a 2a 2f 0a 73 file path .*/.s
f0500 74 61 74 69 63 20 69 6e 74 20 73 77 69 74 63 68 tatic int switch
f0510 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 75 6e LockProxyPath(un
f0520 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 ixFile *pFile, c
f0530 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 29 onst char *path)
f0540 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e {. proxyLockin
f0550 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d gContext *pCtx =
f0560 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f (proxyLockingCo
f0570 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f ntext*)pFile->lo
f0580 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
f0590 63 68 61 72 20 2a 6f 6c 64 50 61 74 68 20 3d 20 char *oldPath =
f05a0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
f05b0 61 74 68 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ath;. int rc =
f05c0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 SQLITE_OK;.. if
f05d0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
f05e0 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 e!=NO_LOCK ){.
f05f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f0600 42 55 53 59 3b 0a 20 20 7d 20 20 0a 0a 20 20 2f BUSY;. } .. /
f0610 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 * nothing to do
f0620 69 66 20 74 68 65 20 70 61 74 68 20 69 73 20 4e if the path is N
f0630 55 4c 4c 2c 20 3a 61 75 74 6f 3a 20 6f 72 20 6d ULL, :auto: or m
f0640 61 74 63 68 65 73 20 74 68 65 20 65 78 69 73 74 atches the exist
f0650 69 6e 67 20 70 61 74 68 20 2a 2f 0a 20 20 69 66 ing path */. if
f0660 28 20 21 70 61 74 68 20 7c 7c 20 70 61 74 68 5b ( !path || path[
f0670 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 0]=='\0' || !str
f0680 63 6d 70 28 70 61 74 68 2c 20 22 3a 61 75 74 6f cmp(path, ":auto
f0690 3a 22 29 20 7c 7c 0a 20 20 20 20 28 6f 6c 64 50 :") ||. (oldP
f06a0 61 74 68 20 26 26 20 21 73 74 72 6e 63 6d 70 28 ath && !strncmp(
f06b0 6f 6c 64 50 61 74 68 2c 20 70 61 74 68 2c 20 4d oldPath, path, M
f06c0 41 58 50 41 54 48 4c 45 4e 29 29 20 29 7b 0a 20 AXPATHLEN)) ){.
f06d0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
f06e0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
f06f0 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b unixFile *lock
f0700 50 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f Proxy = pCtx->lo
f0710 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 70 43 74 ckProxy;. pCt
f0720 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d 4e 55 4c x->lockProxy=NUL
f0730 4c 3b 0a 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e L;. pCtx->con
f0740 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 20 20 chHeld = 0;.
f0750 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79 21 3d 4e if( lockProxy!=N
f0760 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 3d ULL ){. rc=
f0770 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 lockProxy->pMeth
f0780 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 od->xClose((sqli
f0790 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f 63 6b 50 te3_file *)lockP
f07a0 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28 roxy);. if(
f07b0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f07c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
f07d0 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a ree(lockProxy);.
f07e0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
f07f0 33 5f 66 72 65 65 28 6f 6c 64 50 61 74 68 29 3b 3_free(oldPath);
f0800 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 . pCtx->lockP
f0810 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74 roxyPath = sqlit
f0820 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 70 61 e3DbStrDup(0, pa
f0830 74 68 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65 th);. }. . re
f0840 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
f0850 2a 20 70 46 69 6c 65 20 69 73 20 61 20 66 69 6c * pFile is a fil
f0860 65 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 e that has been
f0870 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f opened by a prio
f0880 72 20 78 4f 70 65 6e 20 63 61 6c 6c 2e 20 20 64 r xOpen call. d
f0890 62 50 61 74 68 0a 2a 2a 20 69 73 20 61 20 73 74 bPath.** is a st
f08a0 72 69 6e 67 20 62 75 66 66 65 72 20 61 74 20 6c ring buffer at l
f08b0 65 61 73 74 20 4d 41 58 50 41 54 48 4c 45 4e 2b east MAXPATHLEN+
f08c0 31 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 1 characters in
f08d0 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 size..**.** This
f08e0 20 72 6f 75 74 69 6e 65 20 66 69 6e 64 20 74 68 routine find th
f08f0 65 20 66 69 6c 65 6e 61 6d 65 20 61 73 73 6f 63 e filename assoc
f0900 69 61 74 65 64 20 77 69 74 68 20 70 46 69 6c 65 iated with pFile
f0910 20 61 6e 64 20 77 72 69 74 65 73 20 69 74 0a 2a and writes it.*
f0920 2a 20 69 6e 74 20 64 62 50 61 74 68 2e 0a 2a 2f * int dbPath..*/
f0930 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
f0940 79 47 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69 yGetDbPathForUni
f0950 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a xFile(unixFile *
f0960 70 46 69 6c 65 2c 20 63 68 61 72 20 2a 64 62 50 pFile, char *dbP
f0970 61 74 68 29 7b 0a 23 69 66 20 64 65 66 69 6e 65 ath){.#if define
f0980 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 69 d(__APPLE__). i
f0990 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f f( pFile->pMetho
f09a0 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f d == &afpIoMetho
f09b0 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 61 66 70 ds ){. /* afp
f09c0 20 73 74 79 6c 65 20 6b 65 65 70 73 20 61 20 72 style keeps a r
f09d0 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
f09e0 64 62 20 70 61 74 68 20 69 6e 20 74 68 65 20 66 db path in the f
f09f0 69 6c 65 50 61 74 68 20 66 69 65 6c 64 20 0a 20 ilePath field .
f0a00 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 ** of the str
f0a10 75 63 74 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 uct */. asser
f0a20 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 28 t( (int)strlen((
f0a30 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 char*)pFile->loc
f0a40 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 kingContext)<=MA
f0a50 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 XPATHLEN );.
f0a60 73 74 72 63 70 79 28 64 62 50 61 74 68 2c 20 28 strcpy(dbPath, (
f0a70 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 (afpLockingConte
f0a80 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
f0a90 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 ingContext)->dbP
f0aa0 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 ath);. }else.#e
f0ab0 6e 64 69 66 0a 20 20 69 66 28 20 70 46 69 6c 65 ndif. if( pFile
f0ac0 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 64 6f ->pMethod == &do
f0ad0 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 29 tlockIoMethods )
f0ae0 7b 0a 20 20 20 20 2f 2a 20 64 6f 74 20 6c 6f 63 {. /* dot loc
f0af0 6b 20 73 74 79 6c 65 20 75 73 65 73 20 74 68 65 k style uses the
f0b00 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
f0b10 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64 6f to store the do
f0b20 74 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 66 69 t lock. ** fi
f0b30 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 69 le path */. i
f0b40 6e 74 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e 28 nt len = strlen(
f0b50 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c (char *)pFile->l
f0b60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20 2d ockingContext) -
f0b70 20 73 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b 5f strlen(DOTLOCK_
f0b80 53 55 46 46 49 58 29 3b 0a 20 20 20 20 6d 65 6d SUFFIX);. mem
f0b90 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 68 61 cpy(dbPath, (cha
f0ba0 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 r *)pFile->locki
f0bb0 6e 67 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20 2b ngContext, len +
f0bc0 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 1);. }else{.
f0bd0 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20 73 /* all other s
f0be0 74 79 6c 65 73 20 75 73 65 20 74 68 65 20 6c 6f tyles use the lo
f0bf0 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f cking context to
f0c00 20 73 74 6f 72 65 20 74 68 65 20 64 62 20 66 69 store the db fi
f0c10 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20 61 le path */. a
f0c20 73 73 65 72 74 28 20 73 74 72 6c 65 6e 28 28 63 ssert( strlen((c
f0c30 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b har*)pFile->lock
f0c40 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 ingContext)<=MAX
f0c50 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20 73 PATHLEN );. s
f0c60 74 72 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 trcpy(dbPath, (c
f0c70 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 har *)pFile->loc
f0c80 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 kingContext);.
f0c90 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
f0ca0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
f0cb0 61 6b 65 73 20 61 6e 20 61 6c 72 65 61 64 79 20 akes an already
f0cc0 66 69 6c 6c 65 64 20 69 6e 20 75 6e 69 78 20 66 filled in unix f
f0cd0 69 6c 65 20 61 6e 64 20 61 6c 74 65 72 73 20 69 ile and alters i
f0ce0 74 20 73 6f 20 61 6c 6c 20 66 69 6c 65 20 6c 6f t so all file lo
f0cf0 63 6b 69 6e 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 cking .** will b
f0d00 65 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 e performed on t
f0d10 68 65 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 6c he local proxy l
f0d20 6f 63 6b 20 66 69 6c 65 2e 20 20 54 68 65 20 66 ock file. The f
f0d30 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 0a ollowing fields.
f0d40 2a 2a 20 61 72 65 20 70 72 65 73 65 72 76 65 64 ** are preserved
f0d50 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 in the locking
f0d60 63 6f 6e 74 65 78 74 20 73 6f 20 74 68 61 74 20 context so that
f0d70 74 68 65 79 20 63 61 6e 20 62 65 20 72 65 73 74 they can be rest
f0d80 6f 72 65 64 20 61 6e 64 20 0a 2a 2a 20 74 68 65 ored and .** the
f0d90 20 75 6e 69 78 20 73 74 72 75 63 74 75 72 65 20 unix structure
f0da0 70 72 6f 70 65 72 6c 79 20 63 6c 65 61 6e 65 64 properly cleaned
f0db0 20 75 70 20 61 74 20 63 6c 6f 73 65 20 74 69 6d up at close tim
f0dc0 65 3a 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 e:.** ->locking
f0dd0 43 6f 6e 74 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d Context.** ->pM
f0de0 65 74 68 6f 64 0a 2a 2f 0a 73 74 61 74 69 63 20 ethod.*/.static
f0df0 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f int proxyTransfo
f0e00 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 rmUnixFile(unixF
f0e10 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 ile *pFile, cons
f0e20 74 20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a t char *path) {.
f0e30 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f proxyLockingCo
f0e40 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 63 ntext *pCtx;. c
f0e50 68 61 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41 har dbPath[MAXPA
f0e60 54 48 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20 THLEN+1];
f0e70 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* Name of the d
f0e80 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
f0e90 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68 char *lockPath
f0ea0 3d 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 20 =NULL;. int rc
f0eb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a = SQLITE_OK;. .
f0ec0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
f0ed0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 ktype!=NO_LOCK )
f0ee0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
f0ef0 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 ITE_BUSY;. }.
f0f00 70 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f proxyGetDbPathFo
f0f10 72 55 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c rUnixFile(pFile,
f0f20 20 64 62 50 61 74 68 29 3b 0a 20 20 69 66 28 20 dbPath);. if(
f0f30 21 70 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d !path || path[0]
f0f40 3d 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d =='\0' || !strcm
f0f50 70 28 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 p(path, ":auto:"
f0f60 29 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 ) ){. lockPat
f0f70 68 3d 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b h=NULL;. }else{
f0f80 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 28 63 . lockPath=(c
f0f90 68 61 72 20 2a 29 70 61 74 68 3b 0a 20 20 7d 0a har *)path;. }.
f0fa0 20 20 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 . OSTRACE4("T
f0fb0 52 41 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f RANSPROXY %d fo
f0fc0 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 r %s pid=%d\n",
f0fd0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 pFile->h,.
f0fe0 20 20 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f (lockPath ?
f0ff0 20 6c 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75 lockPath : ":au
f1000 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 to:"), getpid())
f1010 3b 0a 0a 20 20 70 43 74 78 20 3d 20 73 71 6c 69 ;.. pCtx = sqli
f1020 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
f1030 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 69 of(*pCtx) );. i
f1040 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20 f( pCtx==0 ){.
f1050 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f1060 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d NOMEM;. }. mem
f1070 73 65 74 28 70 43 74 78 2c 20 30 2c 20 73 69 7a set(pCtx, 0, siz
f1080 65 6f 66 28 2a 70 43 74 78 29 29 3b 0a 0a 20 20 eof(*pCtx));..
f1090 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 rc = proxyCreate
f10a0 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 64 62 ConchPathname(db
f10b0 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e Path, &pCtx->con
f10c0 63 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 69 chFilePath);. i
f10d0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
f10e0 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f ){. rc = pro
f10f0 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 xyCreateUnixFile
f1100 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 (pCtx->conchFile
f1110 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e Path, &pCtx->con
f1120 63 68 46 69 6c 65 29 3b 0a 20 20 7d 20 20 0a 20 chFile);. } .
f1130 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
f1140 4f 4b 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29 OK && lockPath )
f1150 7b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b {. pCtx->lock
f1160 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 ProxyPath = sqli
f1170 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 6c te3DbStrDup(0, l
f1180 6f 63 6b 50 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 ockPath);. }..
f1190 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
f11a0 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c OK ){. /* all
f11b0 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 memory is alloc
f11c0 61 74 65 64 2c 20 70 72 6f 78 79 73 20 61 72 65 ated, proxys are
f11d0 20 63 72 65 61 74 65 64 20 61 6e 64 20 61 73 73 created and ass
f11e0 69 67 6e 65 64 2c 20 0a 20 20 20 20 2a 2a 20 73 igned, . ** s
f11f0 77 69 74 63 68 20 74 68 65 20 6c 6f 63 6b 69 6e witch the lockin
f1200 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d g context and pM
f1210 65 74 68 6f 64 20 74 68 65 6e 20 72 65 74 75 72 ethod then retur
f1220 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43 n.. */. pC
f1230 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 73 71 6c tx->dbPath = sql
f1240 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 ite3DbStrDup(0,
f1250 64 62 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 dbPath);. pCt
f1260 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e x->oldLockingCon
f1270 74 65 78 74 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f text = pFile->lo
f1280 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
f1290 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 pFile->locking
f12a0 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 3b 0a Context = pCtx;.
f12b0 20 20 20 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 pCtx->pOldMe
f12c0 74 68 6f 64 20 3d 20 70 46 69 6c 65 2d 3e 70 4d thod = pFile->pM
f12d0 65 74 68 6f 64 3b 0a 20 20 20 20 70 46 69 6c 65 ethod;. pFile
f12e0 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 70 72 6f ->pMethod = &pro
f12f0 78 79 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d xyIoMethods;. }
f1300 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 43 else{. if( pC
f1310 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 20 29 7b tx->conchFile ){
f1320 20 0a 20 20 20 20 20 20 72 63 20 3d 20 70 43 74 . rc = pCt
f1330 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d x->conchFile->pM
f1340 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
f1350 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 43 qlite3_file *)pC
f1360 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a tx->conchFile);.
f1370 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
f1380 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
f1390 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
f13a0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 x->conchFile);.
f13b0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
f13c0 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 _free(pCtx->conc
f13d0 68 46 69 6c 65 50 61 74 68 29 3b 20 0a 20 20 20 hFilePath); .
f13e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
f13f0 74 78 29 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 tx);. }. OSTRA
f1400 43 45 33 28 22 54 52 41 4e 53 50 52 4f 58 59 20 CE3("TRANSPROXY
f1410 20 25 64 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 %d %s\n", pFile
f1420 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
f1430 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f (rc==SQLITE_OK ?
f1440 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 "ok" : "failed"
f1450 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ));. return rc;
f1460 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .}.../*.** This
f1470 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 routine handles
f1480 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e sqlite3_file_con
f1490 74 72 6f 6c 28 29 20 63 61 6c 6c 73 20 74 68 61 trol() calls tha
f14a0 74 20 61 72 65 20 73 70 65 63 69 66 69 63 0a 2a t are specific.*
f14b0 2a 20 74 6f 20 70 72 6f 78 79 20 6c 6f 63 6b 69 * to proxy locki
f14c0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
f14d0 74 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 t proxyFileContr
f14e0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
f14f0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
f1500 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 d *pArg){. swit
f1510 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 ch( op ){. ca
f1520 73 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f se SQLITE_GET_LO
f1530 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 CKPROXYFILE: {.
f1540 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 unixFile *p
f1550 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
f1560 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 66 28 20 *)id;. if(
f1570 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d pFile->pMethod =
f1580 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 = &proxyIoMethod
f1590 73 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 6f s ){. pro
f15a0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f15b0 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c *pCtx = (proxyL
f15c0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 ockingContext*)p
f15d0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f15e0 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 72 text;. pr
f15f0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f1600 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 le);. if(
f1610 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f1620 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 Path ){.
f1630 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a *(const char *
f1640 2a 29 70 41 72 67 20 3d 20 70 43 74 78 2d 3e 6c *)pArg = pCtx->l
f1650 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 ockProxyPath;.
f1660 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
f1670 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 *(const c
f1680 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 22 3a har **)pArg = ":
f1690 61 75 74 6f 3a 20 28 6e 6f 74 20 68 65 6c 64 29 auto: (not held)
f16a0 22 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 ";. }.
f16b0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
f16c0 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 *(const char
f16d0 20 2a 2a 29 70 41 72 67 20 3d 20 4e 55 4c 4c 3b **)pArg = NULL;
f16e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
f16f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
f1700 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
f1710 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 SQLITE_SET_LOCKP
f1720 52 4f 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 ROXYFILE: {.
f1730 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f1740 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f1750 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 d;. int rc
f1760 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
f1770 20 20 20 69 6e 74 20 69 73 50 72 6f 78 79 53 74 int isProxySt
f1780 79 6c 65 20 3d 20 28 70 46 69 6c 65 2d 3e 70 4d yle = (pFile->pM
f1790 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 ethod == &proxyI
f17a0 6f 4d 65 74 68 6f 64 73 29 3b 0a 20 20 20 20 20 oMethods);.
f17b0 20 69 66 28 20 70 41 72 67 3d 3d 4e 55 4c 4c 20 if( pArg==NULL
f17c0 7c 7c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a || (const char *
f17d0 29 70 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 )pArg==0 ){.
f17e0 20 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53 if( isProxyS
f17f0 74 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 tyle ){.
f1800 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 /* turn off pr
f1810 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 6e 6f oxy locking - no
f1820 74 20 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20 t supported */.
f1830 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
f1840 4c 49 54 45 5f 45 52 52 4f 52 20 2f 2a 53 51 4c LITE_ERROR /*SQL
f1850 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 3f 20 53 51 ITE_PROTOCOL? SQ
f1860 4c 49 54 45 5f 4d 49 53 55 53 45 3f 2a 2f 3b 0a LITE_MISUSE?*/;.
f1870 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
f1880 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e /* turn
f1890 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 off proxy locki
f18a0 6e 67 20 2d 20 61 6c 72 65 61 64 79 20 6f 66 66 ng - already off
f18b0 20 2d 20 4e 4f 4f 50 20 2a 2f 0a 20 20 20 20 20 - NOOP */.
f18c0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
f18d0 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 _OK;. }.
f18e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
f18f0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
f1900 70 72 6f 78 79 50 61 74 68 20 3d 20 28 63 6f 6e proxyPath = (con
f1910 73 74 20 63 68 61 72 20 2a 29 70 41 72 67 3b 0a st char *)pArg;.
f1920 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 72 if( isPr
f1930 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20 oxyStyle ){.
f1940 20 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 proxyLocki
f1950 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 ngContext *pCtx
f1960 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 = . (
f1970 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f1980 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ext*)pFile->lock
f1990 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f19a0 20 20 20 20 20 20 69 66 28 20 21 73 74 72 63 6d if( !strcm
f19b0 70 28 70 41 72 67 2c 20 22 3a 61 75 74 6f 3a 22 p(pArg, ":auto:"
f19c0 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c ) . ||
f19d0 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 (pCtx->lockProx
f19e0 79 50 61 74 68 20 26 26 0a 20 20 20 20 20 20 20 yPath &&.
f19f0 20 20 20 20 20 20 20 20 21 73 74 72 6e 63 6d 70 !strncmp
f1a00 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 (pCtx->lockProxy
f1a10 50 61 74 68 2c 20 70 72 6f 78 79 50 61 74 68 2c Path, proxyPath,
f1a20 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 0a 20 20 MAXPATHLEN)).
f1a30 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
f1a40 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
f1a50 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 TE_OK;.
f1a60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
f1a70 20 20 20 20 72 63 20 3d 20 73 77 69 74 63 68 4c rc = switchL
f1a80 6f 63 6b 50 72 6f 78 79 50 61 74 68 28 70 46 69 ockProxyPath(pFi
f1a90 6c 65 2c 20 70 72 6f 78 79 50 61 74 68 29 3b 0a le, proxyPath);.
f1aa0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
f1ab0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
f1ac0 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 6e 20 /* turn on
f1ad0 70 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 proxy file locki
f1ae0 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ng */.
f1af0 72 63 20 3d 20 70 72 6f 78 79 54 72 61 6e 73 66 rc = proxyTransf
f1b00 6f 72 6d 55 6e 69 78 46 69 6c 65 28 70 46 69 6c ormUnixFile(pFil
f1b10 65 2c 20 70 72 6f 78 79 50 61 74 68 29 3b 0a 20 e, proxyPath);.
f1b20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
f1b30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
f1b40 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
f1b50 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ult: {. ass
f1b60 65 72 74 28 20 30 20 29 3b 20 20 2f 2a 20 54 68 ert( 0 ); /* Th
f1b70 65 20 63 61 6c 6c 20 61 73 73 75 72 65 73 20 74 e call assures t
f1b80 68 61 74 20 6f 6e 6c 79 20 76 61 6c 69 64 20 6f hat only valid o
f1b90 70 63 6f 64 65 73 20 61 72 65 20 73 65 6e 74 20 pcodes are sent
f1ba0 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f */. }. }. /
f1bb0 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 *NOTREACHED*/.
f1bc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
f1bd0 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 ROR;.}../*.** Wi
f1be0 74 68 69 6e 20 74 68 69 73 20 64 69 76 69 73 69 thin this divisi
f1bf0 6f 6e 20 28 74 68 65 20 70 72 6f 78 79 69 6e 67 on (the proxying
f1c00 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 locking impleme
f1c10 6e 74 61 74 69 6f 6e 29 20 74 68 65 20 70 72 6f ntation) the pro
f1c20 63 65 64 75 72 65 73 0a 2a 2a 20 61 62 6f 76 65 cedures.** above
f1c30 20 74 68 69 73 20 70 6f 69 6e 74 20 61 72 65 20 this point are
f1c40 61 6c 6c 20 75 74 69 6c 69 74 69 65 73 2e 20 20 all utilities.
f1c50 54 68 65 20 6c 6f 63 6b 2d 72 65 6c 61 74 65 64 The lock-related
f1c60 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 0a methods of the.
f1c70 2a 2a 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 ** proxy-locking
f1c80 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
f1c90 6f 64 20 6f 62 6a 65 63 74 20 66 6f 6c 6c 6f 77 od object follow
f1ca0 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ..*/.../*.** Thi
f1cb0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
f1cc0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
f1cd0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
f1ce0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
f1cf0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
f1d00 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
f1d10 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
f1d20 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
f1d30 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a set *pResOut.**
f1d40 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 to a non-zero v
f1d50 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a alue otherwise *
f1d60 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 pResOut is set t
f1d70 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 o zero. The ret
f1d80 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 urn value.** is
f1d90 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b set to SQLITE_OK
f1da0 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 unless an I/O e
f1db0 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
f1dc0 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 ng lock checking
f1dd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f1de0 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72 76 proxyCheckReserv
f1df0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
f1e00 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
f1e10 65 73 4f 75 74 29 20 7b 0a 20 20 75 6e 69 78 46 esOut) {. unixF
f1e20 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
f1e30 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e ixFile*)id;. in
f1e40 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 t rc = proxyTake
f1e50 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 Conch(pFile);.
f1e60 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
f1e70 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f K ){. proxyLo
f1e80 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
f1e90 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
f1ea0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
f1eb0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f1ec0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 t;. unixFile
f1ed0 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c *proxy = pCtx->l
f1ee0 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 65 ockProxy;. re
f1ef0 74 75 72 6e 20 70 72 6f 78 79 2d 3e 70 4d 65 74 turn proxy->pMet
f1f00 68 6f 64 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 hod->xCheckReser
f1f10 76 65 64 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 vedLock((sqlite3
f1f20 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c 20 70 52 _file*)proxy, pR
f1f30 65 73 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 72 65 esOut);. }. re
f1f40 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
f1f50 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
f1f60 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
f1f70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
f1f80 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 eter locktype -
f1f90 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f one.** of the fo
f1fa0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
f1fb0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f (1) SHARED_LO
f1fc0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 CK.** (2) RE
f1fd0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SERVED_LOCK.**
f1fe0 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c (3) PENDING_L
f1ff0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 OCK.** (4) E
f2000 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a XCLUSIVE_LOCK.**
f2010 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 .** Sometimes wh
f2020 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e en requesting on
f2030 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 e lock state, ad
f2040 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 ditional lock st
f2050 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 ates.** are inse
f2060 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e rted in between.
f2070 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 The locking mi
f2080 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 ght fail on one
f2090 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 of the later.**
f20a0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 transitions leav
f20b0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 ing the lock sta
f20c0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f te different fro
f20d0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 m what it starte
f20e0 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 d but.** still s
f20f0 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c hort of its goal
f2100 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
f2110 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 chart shows the
f2120 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e allowed.** tran
f2130 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 sitions and the
f2140 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 inserted interme
f2150 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a diate states:.**
f2160 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 .** UNLOCKED
f2170 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 -> SHARED.**
f2180 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 SHARED -> RESERV
f2190 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
f21a0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
f21b0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
f21c0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e RESERVED -> (PEN
f21d0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
f21e0 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 VE.** PENDING
f21f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
f2200 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
f2210 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 will only incre
f2220 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 ase a lock. Use
f2230 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e the sqlite3OsUn
f2240 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e lock().** routin
f2250 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 e to lower a loc
f2260 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 king level..*/.s
f2270 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 4c tatic int proxyL
f2280 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
f2290 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
f22a0 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 pe) {. unixFile
f22b0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
f22c0 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 ile*)id;. int r
f22d0 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e c = proxyTakeCon
f22e0 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 ch(pFile);. if(
f22f0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
f2300 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 {. proxyLocki
f2310 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 ngContext *pCtx
f2320 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 = (proxyLockingC
f2330 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e ontext *)pFile->
f2340 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
f2350 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 unixFile *pr
f2360 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b oxy = pCtx->lock
f2370 50 72 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 Proxy;. rc =
f2380 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e proxy->pMethod->
f2390 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 xLock((sqlite3_f
f23a0 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b ile*)proxy, lock
f23b0 74 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 type);. pFile
f23c0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f ->locktype = pro
f23d0 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 xy->locktype;.
f23e0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
f23f0 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 .../*.** Lower t
f2400 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
f2410 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 on file descrip
f2420 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 tor pFile to loc
f2430 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 ktype. locktype
f2440 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 .** must be eith
f2450 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 er NO_LOCK or SH
f2460 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a ARED_LOCK..**.**
f2470 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 If the locking
f2480 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c level of the fil
f2490 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
f24a0 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 already at or be
f24b0 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 low.** the reque
f24c0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 sted locking lev
f24d0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 el, this routine
f24e0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
f24f0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
f2500 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
f2510 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
f2520 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
f2530 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
f2540 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e ixFile*)id;. in
f2550 74 20 72 63 20 3d 20 70 72 6f 78 79 54 61 6b 65 t rc = proxyTake
f2560 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 Conch(pFile);.
f2570 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
f2580 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 79 4c 6f K ){. proxyLo
f2590 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
f25a0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
f25b0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
f25c0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f25d0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 t;. unixFile
f25e0 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c *proxy = pCtx->l
f25f0 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20 72 63 ockProxy;. rc
f2600 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f = proxy->pMetho
f2610 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 d->xUnlock((sqli
f2620 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 2c te3_file*)proxy,
f2630 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 locktype);.
f2640 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
f2650 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 70 = proxy->locktyp
f2660 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
f2670 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f rc;.}../*.** Clo
f2680 73 65 20 61 20 66 69 6c 65 20 74 68 61 74 20 75 se a file that u
f2690 73 65 73 20 70 72 6f 78 79 20 6c 6f 63 6b 73 2e ses proxy locks.
f26a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
f26b0 72 6f 78 79 43 6c 6f 73 65 28 73 71 6c 69 74 65 roxyClose(sqlite
f26c0 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 3_file *id) {.
f26d0 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e if( id ){. un
f26e0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
f26f0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
f2700 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 proxyLockingC
f2710 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 ontext *pCtx = (
f2720 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f2730 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 ext *)pFile->loc
f2740 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 kingContext;.
f2750 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 unixFile *lockP
f2760 72 6f 78 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 roxy = pCtx->loc
f2770 6b 50 72 6f 78 79 3b 0a 20 20 20 20 75 6e 69 78 kProxy;. unix
f2780 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 File *conchFile
f2790 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c = pCtx->conchFil
f27a0 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 e;. int rc =
f27b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a SQLITE_OK;. .
f27c0 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 if( lockProx
f27d0 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 y ){. rc =
f27e0 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 lockProxy->pMeth
f27f0 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c od->xUnlock((sql
f2800 69 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 ite3_file*)lockP
f2810 72 6f 78 79 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a roxy, NO_LOCK);.
f2820 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
f2830 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
f2840 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d 3e rc = lockProxy->
f2850 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 pMethod->xClose(
f2860 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 6c (sqlite3_file*)l
f2870 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 ockProxy);.
f2880 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
f2890 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 rc;. sqlit
f28a0 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 e3_free(lockProx
f28b0 79 29 3b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e y);. pCtx->
f28c0 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 30 3b 0a 20 lockProxy = 0;.
f28d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6f 6e }. if( con
f28e0 63 68 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 chFile ){.
f28f0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 if( pCtx->conchH
f2900 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 eld ){. r
f2910 63 20 3d 20 70 72 6f 78 79 52 65 6c 65 61 73 65 c = proxyRelease
f2920 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 Conch(pFile);.
f2930 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
f2940 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
f2950 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e }. rc = con
f2960 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d chFile->pMethod-
f2970 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 >xClose((sqlite3
f2980 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 _file*)conchFile
f2990 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
f29a0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
f29b0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
f29c0 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 conchFile);.
f29d0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
f29e0 65 65 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f ee(pCtx->lockPro
f29f0 78 79 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c xyPath);. sql
f2a00 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e ite3_free(pCtx->
f2a10 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 0a conchFilePath);.
f2a20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f2a30 28 70 43 74 78 2d 3e 64 62 50 61 74 68 29 3b 0a (pCtx->dbPath);.
f2a40 20 20 20 20 2f 2a 20 72 65 73 74 6f 72 65 20 74 /* restore t
f2a50 68 65 20 6f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b he original lock
f2a60 69 6e 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 ing context and
f2a70 70 4d 65 74 68 6f 64 20 74 68 65 6e 20 63 6c 6f pMethod then clo
f2a80 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 70 46 69 se it */. pFi
f2a90 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f2aa0 78 74 20 3d 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f xt = pCtx->oldLo
f2ab0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
f2ac0 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 pFile->pMethod
f2ad0 20 3d 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 = pCtx->pOldMet
f2ae0 68 6f 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 hod;. sqlite3
f2af0 5f 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 20 _free(pCtx);.
f2b00 20 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70 return pFile->p
f2b10 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 Method->xClose(i
f2b20 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e d);. }. return
f2b30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
f2b40 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e .#endif /* defin
f2b50 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
f2b60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
f2b70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a OCKING_STYLE */.
f2b80 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 /*.** The proxy
f2b90 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 69 73 locking style is
f2ba0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 intended for us
f2bb0 65 20 77 69 74 68 20 41 46 50 20 66 69 6c 65 73 e with AFP files
f2bc0 79 73 74 65 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 ystems..** And s
f2bd0 69 6e 63 65 20 41 46 50 20 69 73 20 6f 6e 6c 79 ince AFP is only
f2be0 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 supported on Ma
f2bf0 63 4f 53 58 2c 20 74 68 65 20 70 72 6f 78 79 20 cOSX, the proxy
f2c00 6c 6f 63 6b 69 6e 67 20 69 73 20 61 6c 73 6f 0a locking is also.
f2c10 2a 2a 20 72 65 73 74 72 69 63 74 65 64 20 74 6f ** restricted to
f2c20 20 4d 61 63 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a MacOSX..** .**.
f2c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2c40 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 *** End of the p
f2c50 72 6f 78 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d roxy lock implem
f2c60 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
f2c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
f2c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f2cd0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
f2ce0 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 e the operating
f2cf0 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 system interface
f2d00 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
f2d10 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 tine registers a
f2d20 6c 6c 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 ll VFS implement
f2d30 61 74 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d ations for unix-
f2d40 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a like operating.*
f2d50 2a 20 73 79 73 74 65 6d 73 2e 20 20 54 68 69 73 * systems. This
f2d60 20 72 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74 68 routine, and th
f2d70 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 e sqlite3_os_end
f2d80 28 29 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 () routine that
f2d90 66 6f 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 follows,.** shou
f2da0 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 ld be the only r
f2db0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 outines in this
f2dc0 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 76 69 file that are vi
f2dd0 73 69 62 6c 65 20 66 72 6f 6d 20 6f 74 68 65 72 sible from other
f2de0 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a .** files..**.**
f2df0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
f2e00 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 64 75 72 called once dur
f2e10 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 69 74 69 ing SQLite initi
f2e20 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 79 alization and by
f2e30 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68 72 a.** single thr
f2e40 65 61 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 ead. The memory
f2e50 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 allocation and
f2e60 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 mutex subsystems
f2e70 20 68 61 76 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 have not.** nec
f2e80 65 73 73 61 72 69 6c 79 20 62 65 65 6e 20 69 6e essarily been in
f2e90 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 74 itialized when t
f2ea0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
f2eb0 61 6c 6c 65 64 2c 20 61 6e 64 20 73 6f 20 74 68 alled, and so th
f2ec0 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 ey.** should not
f2ed0 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c be used..*/.SQL
f2ee0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f2ef0 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
f2f00 29 7b 20 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 ){ . /* . ** T
f2f10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 he following mac
f2f20 72 6f 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e ro defines an in
f2f30 69 74 69 61 6c 69 7a 65 72 20 66 6f 72 20 61 6e itializer for an
f2f40 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a sqlite3_vfs obj
f2f50 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 61 ect.. ** The na
f2f60 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 69 73 me of the VFS is
f2f70 20 4e 41 4d 45 2e 20 20 54 68 65 20 70 41 70 70 NAME. The pApp
f2f80 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 Data is a pointe
f2f90 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 0a 20 r to a pointer.
f2fa0 20 2a 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64 ** to the "find
f2fb0 65 72 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28 er" function. (
f2fc0 70 41 70 70 44 61 74 61 20 69 73 20 61 20 70 6f pAppData is a po
f2fd0 69 6e 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 inter to a point
f2fe0 65 72 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 er because. **
f2ff0 73 69 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20 silly C90 rules
f3000 70 72 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a prohibit a void*
f3010 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74 from being cast
f3020 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 to a function p
f3030 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 61 6e 64 20 ointer. ** and
f3040 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f so we have to go
f3050 20 74 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74 through the int
f3060 65 72 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65 ermediate pointe
f3070 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c r to avoid probl
f3080 65 6d 73 0a 20 20 2a 2a 20 77 68 65 6e 20 63 6f ems. ** when co
f3090 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 2d 70 65 mpiling with -pe
f30a0 64 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f 6e dantic-errors on
f30b0 20 47 43 43 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a GCC.). **. **
f30c0 20 54 68 65 20 46 49 4e 44 45 52 20 70 61 72 61 The FINDER para
f30d0 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 6d 61 meter to this ma
f30e0 63 72 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 cro is the name
f30f0 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 of the pointer t
f3100 6f 20 74 68 65 0a 20 20 2a 2a 20 66 69 6e 64 65 o the. ** finde
f3110 72 2d 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 r-function. The
f3120 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
f3130 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
f3140 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 er to the. ** s
f3150 71 6c 69 74 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 qlite_io_methods
f3160 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 object that imp
f3170 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65 73 69 lements the desi
f3180 72 65 64 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a red locking. **
f3190 20 62 65 68 61 76 69 6f 72 73 2e 20 20 53 65 65 behaviors. See
f31a0 20 74 68 65 20 64 69 76 69 73 69 6f 6e 20 61 62 the division ab
f31b0 6f 76 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ove that contain
f31c0 73 20 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 0a s the IOMETHODS.
f31d0 20 20 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 61 ** macro for a
f31e0 64 64 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 ddition informat
f31f0 69 6f 6e 20 6f 6e 20 66 69 6e 64 65 72 2d 66 75 ion on finder-fu
f3200 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 nctions.. **.
f3210 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72 73 20 ** Most finders
f3220 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 61 20 simply return a
f3230 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78 pointer to a fix
f3240 65 64 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ed sqlite3_io_me
f3250 74 68 6f 64 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 thods. ** objec
f3260 74 2e 20 20 42 75 74 20 74 68 65 20 22 61 75 74 t. But the "aut
f3270 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 22 20 61 olockIoFinder" a
f3280 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f vailable on MacO
f3290 53 58 20 64 6f 65 73 20 61 20 6c 69 74 74 6c 65 SX does a little
f32a0 0a 20 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 . ** more than
f32b0 74 68 61 74 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 that; it looks a
f32c0 74 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d t the filesystem
f32d0 20 74 79 70 65 20 74 68 61 74 20 68 6f 73 74 73 type that hosts
f32e0 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 the . ** datab
f32f0 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 72 69 ase file and tri
f3300 65 73 20 74 6f 20 63 68 6f 6f 73 65 20 61 6e 20 es to choose an
f3310 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 61 locking method a
f3320 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 20 ppropriate for.
f3330 20 2a 2a 20 74 68 61 74 20 66 69 6c 65 73 79 73 ** that filesys
f3340 74 65 6d 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 tem time.. */.
f3350 20 23 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53 #define UNIXVFS
f3360 28 56 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52 (VFSNAME, FINDER
f3370 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ) {
f3380 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f3390 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
f33a0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
f33b0 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 on */
f33c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 \. s
f33d0 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c izeof(unixFile),
f33e0 20 20 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 /* szOsFile
f33f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3400 20 20 20 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 \. MAX
f3410 5f 50 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 _PATHNAME,
f3420 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 /* mxPathname
f3430 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3440 20 20 20 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 \. 0,
f3450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3460 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 /* pNext */
f3470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3480 20 20 20 5c 0a 20 20 20 20 56 46 53 4e 41 4d 45 \. VFSNAME
f3490 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
f34a0 2a 20 7a 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 * zName */
f34b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f34c0 20 5c 0a 20 20 20 20 28 76 6f 69 64 2a 29 26 46 \. (void*)&F
f34d0 49 4e 44 45 52 2c 20 20 20 20 20 20 20 2f 2a 20 INDER, /*
f34e0 70 41 70 70 44 61 74 61 20 2a 2f 20 20 20 20 20 pAppData */
f34f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f3500 0a 20 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 . unixOpen,
f3510 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f /* xO
f3520 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 pen */
f3530 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3540 20 20 20 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 unixDelete,
f3550 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c /* xDel
f3560 65 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ete */
f3570 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f3580 20 75 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20 unixAccess,
f3590 20 20 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 /* xAcces
f35a0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 s */
f35b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
f35c0 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c nixFullPathname,
f35d0 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 /* xFullPat
f35e0 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 hname */
f35f0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
f3600 78 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 xDlOpen,
f3610 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f /* xDlOpen */
f3620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3630 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 \. unixD
f3640 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 lError,
f3650 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 /* xDlError */
f3660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3670 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 \. unixDlS
f3680 79 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ym, /
f3690 2a 20 78 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 * xDlSym */
f36a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f36b0 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f \. unixDlClo
f36c0 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 se, /*
f36d0 78 44 6c 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 xDlClose */
f36e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f36f0 0a 20 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e . unixRandomn
f3700 65 73 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 ess, /* xR
f3710 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 andomness */
f3720 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3730 20 20 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 unixSleep,
f3740 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 /* xSle
f3750 65 70 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 ep */
f3760 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f3770 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 unixCurrentTime
f3780 2c 20 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65 , /* xCurre
f3790 6e 74 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 ntTime */
f37a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
f37b0 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 nixGetLastError
f37c0 20 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 /* xGetLast
f37d0 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 Error */
f37e0 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 \. }..
f37f0 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 20 64 65 66 61 /*. ** All defa
f3800 75 6c 74 20 56 46 53 65 73 20 66 6f 72 20 75 6e ult VFSes for un
f3810 69 78 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 ix are contained
f3820 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
f3830 67 20 61 72 72 61 79 2e 0a 20 20 2a 2a 0a 20 20 g array.. **.
f3840 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
f3850 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 70 4e 65 sqlite3_vfs.pNe
f3860 78 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 xt field of the
f3870 56 46 53 20 6f 62 6a 65 63 74 20 69 73 20 6d 6f VFS object is mo
f3880 64 69 66 69 65 64 0a 20 20 2a 2a 20 62 79 20 74 dified. ** by t
f3890 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 77 he SQLite core w
f38a0 68 65 6e 20 74 68 65 20 56 46 53 20 69 73 20 72 hen the VFS is r
f38b0 65 67 69 73 74 65 72 65 64 2e 20 20 53 6f 20 74 egistered. So t
f38c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a he following. *
f38d0 2a 20 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 * array cannot b
f38e0 65 20 63 6f 6e 73 74 2e 0a 20 20 2a 2f 0a 20 20 e const.. */.
f38f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
f3900 66 73 20 61 56 66 73 5b 5d 20 3d 20 7b 0a 23 69 fs aVfs[] = {.#i
f3910 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
f3920 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
f3930 20 28 4f 53 5f 56 58 57 4f 52 4b 53 20 7c 7c 20 (OS_VXWORKS ||
f3940 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
f3950 5f 29 29 0a 20 20 20 20 55 4e 49 58 56 46 53 28 _)). UNIXVFS(
f3960 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 "unix",
f3970 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
f3980 72 20 29 2c 0a 23 65 6c 73 65 0a 20 20 20 20 55 r ),.#else. U
f3990 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 NIXVFS("unix",
f39a0 20 20 20 20 20 20 20 20 70 6f 73 69 78 49 6f 46 posixIoF
f39b0 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a inder ),.#endif.
f39c0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 UNIXVFS("uni
f39d0 78 2d 6e 6f 6e 65 22 2c 20 20 20 20 20 6e 6f 6c x-none", nol
f39e0 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 ockIoFinder ),.
f39f0 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 UNIXVFS("unix
f3a00 2d 64 6f 74 66 69 6c 65 22 2c 20 20 64 6f 74 6c -dotfile", dotl
f3a10 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 ockIoFinder ),.
f3a20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 UNIXVFS("unix
f3a30 2d 77 66 6c 22 2c 20 20 20 20 20 20 70 6f 73 69 -wfl", posi
f3a40 78 57 66 6c 49 6f 46 69 6e 64 65 72 20 29 2c 0a xWflIoFinder ),.
f3a50 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
f3a60 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 UNIXVFS("unix
f3a70 2d 6e 61 6d 65 64 73 65 6d 22 2c 20 73 65 6d 49 -namedsem", semI
f3a80 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 oFinder ),.#endi
f3a90 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 f.#if SQLITE_ENA
f3aa0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
f3ab0 45 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 E. UNIXVFS("u
f3ac0 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20 20 20 70 nix-posix", p
f3ad0 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a osixIoFinder ),.
f3ae0 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a #if !OS_VXWORKS.
f3af0 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 UNIXVFS("uni
f3b00 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20 66 6c 6f x-flock", flo
f3b10 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 ckIoFinder ),.#e
f3b20 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 ndif.#endif.#if
f3b30 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
f3b40 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 CKING_STYLE && d
f3b50 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
f3b60 29 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 ). UNIXVFS("u
f3b70 6e 69 78 2d 61 66 70 22 2c 20 20 20 20 20 20 61 nix-afp", a
f3b80 66 70 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 fpIoFinder ),.
f3b90 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d UNIXVFS("unix-
f3ba0 70 72 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 proxy", proxy
f3bb0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 IoFinder ),.#end
f3bc0 69 66 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e if. };. unsign
f3bd0 65 64 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 ed int i;
f3be0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
f3bf0 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 er */.. /* Regi
f3c00 73 74 65 72 20 61 6c 6c 20 56 46 53 65 73 20 64 ster all VFSes d
f3c10 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 61 56 efined in the aV
f3c20 66 73 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 fs[] array */.
f3c30 66 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65 for(i=0; i<(size
f3c40 6f 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28 of(aVfs)/sizeof(
f3c50 73 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69 sqlite3_vfs)); i
f3c60 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
f3c70 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61 _vfs_register(&a
f3c80 56 66 73 5b 69 5d 2c 20 69 3d 3d 30 29 3b 0a 20 Vfs[i], i==0);.
f3c90 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
f3ca0 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a TE_OK; .}../*.**
f3cb0 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70 Shutdown the op
f3cc0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
f3cd0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
f3ce0 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 Some operating s
f3cf0 79 73 74 65 6d 73 20 6d 69 67 68 74 20 6e 65 65 ystems might nee
f3d00 64 20 74 6f 20 64 6f 20 73 6f 6d 65 20 63 6c 65 d to do some cle
f3d10 61 6e 75 70 20 69 6e 20 74 68 69 73 20 72 6f 75 anup in this rou
f3d20 74 69 6e 65 2c 0a 2a 2a 20 74 6f 20 72 65 6c 65 tine,.** to rele
f3d30 61 73 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 ase dynamically
f3d40 61 6c 6c 6f 63 61 74 65 64 20 6f 62 6a 65 63 74 allocated object
f3d50 73 2e 20 20 42 75 74 20 6e 6f 74 20 6f 6e 20 75 s. But not on u
f3d60 6e 69 78 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 nix..** This rou
f3d70 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 tine is a no-op
f3d80 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 53 51 4c for unix..*/.SQL
f3d90 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f3da0 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 te3_os_end(void)
f3db0 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 { . return SQLI
f3dc0 54 45 5f 4f 4b 3b 20 0a 7d 0a 20 0a 23 65 6e 64 TE_OK; .}. .#end
f3dd0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
f3de0 55 4e 49 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a UNIX */../******
f3df0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
f3e00 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a os_unix.c ******
f3e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e30 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
f3e40 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
f3e50 69 6c 65 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a ile os_win.c ***
f3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3e80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
f3e90 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 004 May 22.**.**
f3ea0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
f3eb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
f3ec0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
f3ed0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
f3ee0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
f3ef0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
f3f00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
f3f10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
f3f20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
f3f30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
f3f40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
f3f50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
f3f60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
f3f70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
f3f80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
f3f90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
f3fa0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
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 2a 2a 2a ****************
f3fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
f4000 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
f4010 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 ontains code tha
f4020 74 20 69 73 20 73 70 65 63 69 66 69 63 20 74 6f t is specific to
f4030 20 77 69 6e 64 6f 77 73 2e 0a 2a 2f 0a 23 69 66 windows..*/.#if
f4040 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 20 SQLITE_OS_WIN
f4050 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f4060 54 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 This file is use
f4070 64 20 66 6f 72 20 77 69 6e 64 6f 77 73 20 6f 6e d for windows on
f4080 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 ly */.../*.** A
f4090 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 Note About Memor
f40a0 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a y Allocation:.**
f40b0 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72 20 .** This driver
f40c0 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 uses malloc()/fr
f40d0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72 61 ee() directly ra
f40e0 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 ther than going
f40f0 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 through.** the S
f4100 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20 73 QLite-wrappers s
f4110 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f qlite3_malloc()/
f4120 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 20 sqlite3_free().
f4130 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73 0a Those wrappers.
f4140 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 ** are designed
f4150 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 for use on embed
f4160 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65 72 ded systems wher
f4170 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61 72 e memory is scar
f4180 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 ce and.** malloc
f4190 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65 6e failures happen
f41a0 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 57 69 frequently. Wi
f41b0 6e 33 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70 n32 does not typ
f41c0 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a ically run on.**
f41d0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
f41e0 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64 s, and when it d
f41f0 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65 oes the develope
f4200 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65 rs normally have
f4210 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c bigger.** probl
f4220 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f ems to worry abo
f4230 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20 ut than running
f4240 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 out of memory.
f4250 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a So there is not.
f4260 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 ** a compelling
f4270 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 need to use the
f4280 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 wrappers..**.**
f4290 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67 But there is a g
f42a0 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f ood reason to no
f42b0 74 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 t use the wrappe
f42c0 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74 rs. If we use t
f42d0 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74 he.** wrappers t
f42e0 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20 hen we will get
f42f0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 simulated malloc
f4300 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 () failures with
f4310 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65 in this.** drive
f4320 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75 r. And that cau
f4330 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 ses all kinds of
f4340 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75 problems for ou
f4350 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20 r tests. We.**
f4360 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51 could enhance SQ
f4370 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74 Lite to deal wit
f4380 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c h simulated mall
f4390 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68 oc failures with
f43a0 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69 in.** the OS dri
f43b0 76 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64 ver, but the cod
f43c0 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 e to deal with t
f43d0 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75 hose failure wou
f43e0 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65 ld not.** be exe
f43f0 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20 rcised on Linux
f4400 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 (which does not
f4410 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 need to malloc()
f4420 20 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a in the driver).
f4430 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75 ** and so we wou
f4440 6c 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c ld have difficul
f4450 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72 ty writing cover
f4460 61 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68 age tests for th
f4470 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74 at.** code. Bet
f4480 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65 ter to leave the
f4490 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68 code out, we th
f44a0 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ink..**.** The p
f44b0 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73 oint of this dis
f44c0 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f cussion is as fo
f44d0 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65 llows: When cre
f44e0 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f ating a new.** O
f44f0 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65 S layer for an e
f4500 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20 mbedded system,
f4510 69 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20 if you use this
f4520 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 file as an examp
f4530 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 le,.** avoid the
f4540 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 use of malloc()
f4550 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 /free(). Those
f4560 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b routines work ok
f4570 20 6f 6e 20 77 69 6e 64 6f 77 73 0a 2a 2a 20 64 on windows.** d
f4580 65 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 esktops but not
f4590 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 so well in embed
f45a0 64 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a ded systems..*/.
f45b0 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69 6e 62 61 .#include <winba
f45c0 73 65 2e 68 3e 0a 0a 23 69 66 64 65 66 20 5f 5f se.h>..#ifdef __
f45d0 43 59 47 57 49 4e 5f 5f 0a 23 20 69 6e 63 6c 75 CYGWIN__.# inclu
f45e0 64 65 20 3c 73 79 73 2f 63 79 67 77 69 6e 2e 68 de <sys/cygwin.h
f45f0 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 >.#endif../*.**
f4600 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 Macros used to d
f4610 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
f4620 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 or not to use t
f4630 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 hreads..*/.#if d
f4640 65 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 efined(THREADSAF
f4650 45 29 20 26 26 20 54 48 52 45 41 44 53 41 46 45 E) && THREADSAFE
f4660 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
f4670 5f 57 33 32 5f 54 48 52 45 41 44 53 20 31 0a 23 _W32_THREADS 1.#
f4680 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 endif../*.** Inc
f4690 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 lude code that i
f46a0 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 s common to all
f46b0 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a os_*.c files.*/.
f46c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
f46d0 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f Include os_commo
f46e0 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c n.h in the middl
f46f0 65 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a e of os_win.c **
f4700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f4710 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
f4720 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f Begin file os_co
f4730 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
f4740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f4760 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
f4770 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
f4780 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
f4790 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
f47a0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
f47b0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
f47c0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
f47d0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
f47e0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
f47f0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
f4800 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
f4810 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
f4820 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
f4830 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
f4840 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
f4850 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
f4860 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
f4870 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
f4880 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
f4890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f48a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f48b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f48c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f48d0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
f48e0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d file contains m
f48f0 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 acros and a litt
f4900 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 le bit of code t
f4910 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f hat is common to
f4920 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 .** all of the p
f4930 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 latform-specific
f4940 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 files (os_*.c)
f4950 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 and is #included
f4960 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 into those.** f
f4970 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 iles..**.** This
f4980 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
f4990 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 #included by the
f49a0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e os_*.c files on
f49b0 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 ly. It is not a
f49c0 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 .** general purp
f49d0 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e ose header file.
f49e0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f .*/.#ifndef _OS_
f49f0 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e COMMON_H_.#defin
f4a00 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a e _OS_COMMON_H_.
f4a10 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 ./*.** At least
f4a20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c two bugs have sl
f4a30 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 ipped in because
f4a40 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 we changed the
f4a50 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 MEMORY_DEBUG.**
f4a60 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f macro to SQLITE_
f4a70 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f DEBUG and some o
f4a80 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 lder makefiles h
f4a90 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 ave not yet made
f4aa0 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 the.** switch.
f4ab0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
f4ac0 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 ode should catch
f4ad0 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 this problem at
f4ae0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a compile-time..*
f4af0 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f /.#ifdef MEMORY_
f4b00 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 DEBUG.# error "T
f4b10 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 he MEMORY_DEBUG
f4b20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 macro is obsolet
f4b30 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 e. Use SQLITE_D
f4b40 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 EBUG instead.".#
f4b50 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 endif..#ifdef SQ
f4b60 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 LITE_DEBUG.SQLIT
f4b70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
f4b80 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 lite3OSTrace = 0
f4b90 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ;.#define OSTRAC
f4ba0 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 E1(X) if
f4bb0 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
f4bc0 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
f4bd0 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 rintf(X).#define
f4be0 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 OSTRACE2(X,Y)
f4bf0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
f4c00 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
f4c10 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
f4c20 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
f4c30 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 E3(X,Y,Z) if
f4c40 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 ( sqlite3OSTrace
f4c50 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ) sqlite3DebugP
f4c60 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 rintf(X,Y,Z).#de
f4c70 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c fine OSTRACE4(X,
f4c80 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c Y,Z,A) if( sql
f4c90 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
f4ca0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
f4cb0 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e (X,Y,Z,A).#defin
f4cc0 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a e OSTRACE5(X,Y,Z
f4cd0 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 ,A,B) if( sqlite
f4ce0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
f4cf0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
f4d00 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
f4d10 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
f4d20 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 A,B,C) \. if(
f4d30 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 sqlite3OSTrace)
f4d40 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f4d50 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a tf(X,Y,Z,A,B,C).
f4d60 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 #define OSTRACE7
f4d70 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 (X,Y,Z,A,B,C,D)
f4d80 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 \. if(sqlite3
f4d90 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 OSTrace) sqlite3
f4da0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
f4db0 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 Z,A,B,C,D).#else
f4dc0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
f4dd0 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 1(X).#define OST
f4de0 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 RACE2(X,Y).#defi
f4df0 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c ne OSTRACE3(X,Y,
f4e00 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
f4e10 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 CE4(X,Y,Z,A).#de
f4e20 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c fine OSTRACE5(X,
f4e30 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 Y,Z,A,B).#define
f4e40 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c OSTRACE6(X,Y,Z,
f4e50 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f A,B,C).#define O
f4e60 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c STRACE7(X,Y,Z,A,
f4e70 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f B,C,D).#endif../
f4e80 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 *.** Macros for
f4e90 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 performance trac
f4ea0 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 ing. Normally t
f4eb0 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 urned off. Only
f4ec0 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 works.** on i48
f4ed0 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 6 hardware..*/.#
f4ee0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 ifdef SQLITE_PER
f4ef0 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a FORMANCE_TRACE..
f4f00 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 /* .** hwtime.h
f4f10 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
f4f20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 assembler code f
f4f30 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 or implementing
f4f40 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d .** high-perform
f4f50 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 ance timing rout
f4f60 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a ines..*/./******
f4f70 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
f4f80 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 hwtime.h in the
f4f90 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f middle of os_co
f4fa0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
f4fb0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
f4fc0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
f4fd0 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a ile hwtime.h ***
f4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5000 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
f5010 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 008 May 27.**.**
f5020 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
f5030 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
f5040 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
f5050 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
f5060 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
f5070 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
f5080 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
f5090 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
f50a0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
f50b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
f50c0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
f50d0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
f50e0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
f50f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
f5100 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
f5110 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
f5120 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
f5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
f5180 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
f5190 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 ontains inline a
f51a0 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 sm code for retr
f51b0 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 ieving "high-per
f51c0 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 formance".** cou
f51d0 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c nters for x86 cl
f51e0 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23 69 66 ass CPUs..*/.#if
f51f0 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a ndef _HWTIME_H_.
f5200 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f #define _HWTIME_
f5210 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f H_../*.** The fo
f5220 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
f5230 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 only works on pe
f5240 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 ntium-class (or
f5250 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 newer) processor
f5260 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 s..** It uses th
f5270 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 e RDTSC opcode t
f5280 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 o read the cycle
f5290 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 count value out
f52a0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 of the.** proce
f52b0 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 ssor and returns
f52c0 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 that value. Th
f52d0 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 is can be used f
f52e0 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 or high-res.** p
f52f0 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 rofiling..*/.#if
f5300 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
f5310 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
f5320 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 MSC_VER)) && \.
f5330 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 (defined(i3
f5340 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
f5350 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
f5360 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 ned(_M_IX86))..
f5370 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 #if defined(__G
f5380 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 NUC__).. __inli
f5390 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
f53a0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
f53b0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 (void){. uns
f53c0 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 igned int lo, hi
f53d0 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f ;. __asm__ _
f53e0 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 _volatile__ ("rd
f53f0 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 tsc" : "=a" (lo)
f5400 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 , "=d" (hi));.
f5410 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 return (sqlit
f5420 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 e_uint64)hi << 3
f5430 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 2 | lo;. }.. #
f5440 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 elif defined(_MS
f5450 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c C_VER).. __decl
f5460 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e spec(naked) __in
f5470 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 line sqlite_uint
f5480 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 64 __cdecl sqlit
f5490 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
f54a0 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 __asm {.
f54b0 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 rdtsc.
f54c0 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 ret ; r
f54d0 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 eturn value at E
f54e0 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 DX:EAX. }.
f54f0 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c }.. #endif..#el
f5500 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
f5510 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
f5520 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 (__x86_64__))..
f5530 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
f5540 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
f5550 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
f5560 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
f5570 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f ng val;. __
f5580 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
f5590 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
f55a0 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 A" (val));.
f55b0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d return val;. }
f55c0 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 . .#elif (define
f55d0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
f55e0 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 efined(__ppc__))
f55f0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
f5600 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
f5610 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
f5620 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
f5630 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 long long retva
f5640 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 l;. unsigne
f5650 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 d long junk;.
f5660 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
f5670 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 atile__ ("\n\.
f5680 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 1:
f5690 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 mftbu %1\n\.
f56a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f56b0 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 mftb %L0\n\.
f56c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f56d0 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 mftbu %0\n\.
f56e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f56f0 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e cmpw %0,%1\n
f5700 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
f5710 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a bne 1b".
f5720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f5730 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c : "=r" (retval
f5740 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b ), "=r" (junk));
f5750 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 . return re
f5760 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 tval;. }..#else
f5770 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 .. #error Need
f5780 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
f5790 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 f sqlite3Hwtime(
f57a0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
f57b0 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 orm... /*. **
f57c0 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f To compile witho
f57d0 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 ut implementing
f57e0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
f57f0 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
f5800 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 m,. ** you can
f5810 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 remove the above
f5820 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 #error and use
f5830 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
f5840 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e ** stub function
f5850 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 . You will lose
f5860 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 timing support
f5870 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 for many. ** of
f5880 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 the debugging a
f5890 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 nd testing utili
f58a0 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f ties, but it sho
f58b0 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 uld at. ** leas
f58c0 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 t compile and ru
f58d0 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 n.. */.SQLITE_P
f58e0 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f RIVATE sqlite_
f58f0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
f5900 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 time(void){ retu
f5910 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 rn ((sqlite_uint
f5920 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 64)0); }..#endif
f5930 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
f5940 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 ined(_HWTIME_H_)
f5950 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
f5960 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 **** End of hwti
f5970 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
f5980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f59a0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
f59b0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
f59c0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
f59d0 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 f in os_common.h
f59e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
f59f0 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c ***/..static sql
f5a00 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 ite_uint64 g_sta
f5a10 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 rt;.static sqlit
f5a20 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 e_uint64 g_elaps
f5a30 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 ed;.#define TIME
f5a40 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f R_START g_
f5a50 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 start=sqlite3Hwt
f5a60 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 ime().#define TI
f5a70 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 MER_END
f5a80 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 g_elapsed=sqlite
f5a90 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 3Hwtime()-g_star
f5aa0 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f t.#define TIMER_
f5ab0 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c ELAPSED g_el
f5ac0 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 apsed.#else.#def
f5ad0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a ine TIMER_START.
f5ae0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
f5af0 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f D.#define TIMER_
f5b00 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 ELAPSED ((sq
f5b10 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 lite_uint64)0).#
f5b20 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
f5b30 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 we compile with
f5b40 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 the SQLITE_TEST
f5b50 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 macro set, then
f5b60 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c the following bl
f5b70 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 ock.** of code w
f5b80 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 ill give us the
f5b90 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c ability to simul
f5ba0 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 ate a disk I/O e
f5bb0 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 rror. This.** i
f5bc0 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
f5bd0 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 ng the I/O recov
f5be0 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 ery logic..*/.#i
f5bf0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
f5c00 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f5c10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f5c20 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 _hit = 0;
f5c30 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
f5c40 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f mber of I/O Erro
f5c50 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
f5c60 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
f5c70 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 error_hardhit =
f5c80 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 0; /* Num
f5c90 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 ber of non-benig
f5ca0 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 n errors */.SQLI
f5cb0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
f5cc0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
f5cd0 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ing = 0;
f5ce0 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f /* Count down to
f5cf0 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 first I/O error
f5d00 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
f5d10 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
f5d20 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b ror_persist = 0;
f5d30 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
f5d40 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 if I/O errors pe
f5d50 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f rsist */.SQLITE_
f5d60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f5d70 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 io_error_benign
f5d80 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
f5d90 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 True if errors a
f5da0 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c re benign */.SQL
f5db0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f5dc0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
f5dd0 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 ding = 0;.SQLITE
f5de0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
f5df0 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 _diskfull = 0;.#
f5e00 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
f5e10 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 OErrorBenign(X)
f5e20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f5e30 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 _benign=(X).#def
f5e40 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
f5e50 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 ror(CODE) \. i
f5e60 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 f( (sqlite3_io_e
f5e70 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 rror_persist &&
f5e80 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f5e90 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c _hit) \. |
f5ea0 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 | sqlite3_io_err
f5eb0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 or_pending-- ==
f5ec0 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 1 ) \.
f5ed0 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 { local_ioe
f5ee0 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 rr(); CODE; }.st
f5ef0 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f atic void local_
f5f00 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 ioerr(){. IOTRA
f5f10 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b CE(("IOERR\n"));
f5f20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 . sqlite3_io_er
f5f30 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 ror_hit++;. if(
f5f40 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 !sqlite3_io_err
f5f50 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 or_benign ) sqli
f5f60 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
f5f70 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e dhit++;.}.#defin
f5f80 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 e SimulateDiskfu
f5f90 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a llError(CODE) \.
f5fa0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 if( sqlite3_d
f5fb0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
f5fc0 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 ){ \. if( sq
f5fd0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
f5fe0 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c ending == 1 ){ \
f5ff0 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f . local_io
f6000 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 err(); \.
f6010 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
f6020 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 = 1; \. s
f6030 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
f6040 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 hit = 1; \.
f6050 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d CODE; \. }
f6060 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 else{ \. s
f6070 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
f6080 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 pending--; \.
f6090 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 } \. }.#else
f60a0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
f60b0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 eIOErrorBenign(X
f60c0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
f60d0 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 teIOError(A).#de
f60e0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
f60f0 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 kfullError(A).#e
f6100 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e ndif../*.** When
f6110 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 testing, keep a
f6120 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 count of the nu
f6130 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c mber of open fil
f6140 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 es..*/.#ifdef SQ
f6150 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
f6160 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
f6170 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 _open_file_count
f6180 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 = 0;.#define Op
f6190 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 enCounter(X) sq
f61a0 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
f61b0 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 count+=(X).#else
f61c0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 .#define OpenCou
f61d0 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a nter(X).#endif..
f61e0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
f61f0 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ed(_OS_COMMON_H_
f6200 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
f6210 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
f6220 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
f6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6250 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
f6260 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
f6270 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
f6280 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e 63 20 2a ff in os_win.c *
f6290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f62a0 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d ****/../*.** Som
f62b0 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 e microsoft comp
f62c0 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 20 ilers lack this
f62d0 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 definition..*/.#
f62e0 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f 46 ifndef INVALID_F
f62f0 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 0a 23 ILE_ATTRIBUTES.#
f6300 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f define INVALID_
f6310 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 20 FILE_ATTRIBUTES
f6320 28 28 44 57 4f 52 44 29 2d 31 29 20 0a 23 65 6e ((DWORD)-1) .#en
f6330 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 dif../*.** Deter
f6340 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 mine if we are d
f6350 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 ealing with Wind
f6360 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 owsCE - which ha
f6370 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 s a much.** redu
f6380 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 ced API..*/.#if
f6390 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
f63a0 23 20 64 65 66 69 6e 65 20 41 72 65 46 69 6c 65 # define AreFile
f63b0 41 70 69 73 41 4e 53 49 28 29 20 31 0a 23 20 64 ApisANSI() 1.# d
f63c0 65 66 69 6e 65 20 46 6f 72 6d 61 74 4d 65 73 73 efine FormatMess
f63d0 61 67 65 57 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 ageW(a,b,c,d,e,f
f63e0 2c 67 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a ,g) 0.#endif../*
f63f0 0a 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20 .** WinCE lacks
f6400 6e 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 66 native support f
f6410 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 or file locking
f6420 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 so we have to fa
f6430 6b 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f ke it.** with so
f6440 6d 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f me code of our o
f6450 77 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 wn..*/.#if SQLIT
f6460 45 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 64 E_OS_WINCE.typed
f6470 65 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 4c ef struct winceL
f6480 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 61 ock {. int nRea
f6490 64 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e ders; /* N
f64a0 75 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 20 umber of reader
f64b0 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 2a locks obtained *
f64c0 2f 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 6e /. BOOL bPendin
f64d0 67 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 g; /* Indic
f64e0 61 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 6c ates a pending l
f64f0 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 ock has been obt
f6500 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 ained */. BOOL
f6510 62 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 2f bReserved; /
f6520 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 65 * Indicates a re
f6530 73 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 20 served lock has
f6540 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f been obtained */
f6550 0a 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69 . BOOL bExclusi
f6560 76 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 ve; /* Indica
f6570 74 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 tes an exclusive
f6580 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f lock has been o
f6590 62 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e btained */.} win
f65a0 63 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a ceLock;.#endif..
f65b0 2f 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 6c /*.** The winFil
f65c0 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 e structure is a
f65d0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c subclass of sql
f65e0 69 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 69 ite3_file* speci
f65f0 66 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32 fic to the win32
f6600 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 .** portability
f6610 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 layer..*/.typede
f6620 66 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 f struct winFile
f6630 20 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74 winFile;.struct
f6640 20 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f 6e winFile {. con
f6650 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
f6660 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 2f thods *pMethod;/
f6670 2a 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 20 * Must be first
f6680 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20 */. HANDLE h;
f6690 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f66a0 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 Handle for acces
f66b0 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f sing the file */
f66c0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
f66d0 20 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 79 locktype; /* Ty
f66e0 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 pe of lock curre
f66f0 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 ntly held on thi
f6700 73 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 s file */. shor
f6710 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 t sharedLockByte
f6720 3b 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 ; /* Randomly
f6730 63 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64 chosen byte used
f6740 20 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 as a shared loc
f6750 6b 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6c 61 73 k */. DWORD las
f6760 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 2f tErrno; /
f6770 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20 65 72 * The Windows er
f6780 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 rno from the las
f6790 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 t I/O error */.
f67a0 20 44 57 4f 52 44 20 73 65 63 74 6f 72 53 69 7a DWORD sectorSiz
f67b0 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 e; /* Sect
f67c0 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 or size of the d
f67d0 65 76 69 63 65 20 66 69 6c 65 20 69 73 20 6f 6e evice file is on
f67e0 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f */.#if SQLITE_O
f67f0 53 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20 S_WINCE. WCHAR
f6800 2a 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b *zDeleteOnClose;
f6810 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c /* Name of fil
f6820 65 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e e to delete when
f6830 20 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41 closing */. HA
f6840 4e 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20 NDLE hMutex;
f6850 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 /* Mutex u
f6860 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 sed to control a
f6870 63 63 65 73 73 20 74 6f 20 73 68 61 72 65 64 20 ccess to shared
f6880 6c 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44 lock */ . HAND
f6890 4c 45 20 68 53 68 61 72 65 64 3b 20 20 20 20 20 LE hShared;
f68a0 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 /* Shared me
f68b0 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65 mory segment use
f68c0 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f d for locking */
f68d0 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 . winceLock loc
f68e0 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f al; /* Lo
f68f0 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 cks obtained by
f6900 74 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 this instance of
f6910 20 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69 winFile */. wi
f6920 6e 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b nceLock *shared;
f6930 20 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 /* Global
f6940 73 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f shared lock memo
f6950 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 ry for the file
f6960 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f */.#endif.};../
f6970 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 70 72 6f *.** Forward pro
f6980 74 6f 74 79 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 totypes..*/.stat
f6990 69 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f 72 ic int getSector
f69a0 53 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 65 Size(. sqlite
f69b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 20 3_vfs *pVfs,.
f69c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 const char *zRe
f69d0 6c 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 54 lative /* UT
f69e0 46 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f F-8 file name */
f69f0 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .);../*.** The f
f6a00 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c ollowing variabl
f6a10 65 20 69 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 e is (normally)
f6a20 73 65 74 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76 set once and nev
f6a30 65 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 er changes.** th
f6a40 65 72 65 61 66 74 65 72 2e 20 20 49 74 20 72 65 ereafter. It re
f6a50 63 6f 72 64 73 20 77 68 65 74 68 65 72 20 74 68 cords whether th
f6a60 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
f6a70 65 6d 20 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f em is Win95.** o
f6a80 72 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 r WinNT..**.** 0
f6a90 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 : Operating sy
f6aa0 73 74 65 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a stem unknown..**
f6ab0 20 31 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 1: Operating
f6ac0 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39 35 2e system is Win95.
f6ad0 0a 2a 2a 20 32 3a 20 20 20 4f 70 65 72 61 74 69 .** 2: Operati
f6ae0 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e ng system is Win
f6af0 4e 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 NT..**.** In ord
f6b00 65 72 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 er to facilitate
f6b10 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 20 57 69 testing on a Wi
f6b20 6e 4e 54 20 73 79 73 74 65 6d 2c 20 74 68 65 20 nNT system, the
f6b30 74 65 73 74 20 66 69 78 74 75 72 65 0a 2a 2a 20 test fixture.**
f6b40 63 61 6e 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 can manually set
f6b50 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 31 this value to 1
f6b60 20 74 6f 20 65 6d 75 6c 61 74 65 20 57 69 6e 39 to emulate Win9
f6b70 38 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 8 behavior..*/.#
f6b80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
f6b90 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
f6ba0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 sqlite3_os_type
f6bb0 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 = 0;.#else.stat
f6bc0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f ic int sqlite3_o
f6bd0 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6e 64 s_type = 0;.#end
f6be0 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
f6bf0 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 true (non-zero)
f6c00 20 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 if we are runni
f6c10 6e 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 ng under WinNT,
f6c20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a Win2K, WinXP,.**
f6c30 20 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 or WinCE. Retu
f6c40 72 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 rn false (zero)
f6c50 66 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 for Win95, Win98
f6c60 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a , or WinME..**.*
f6c70 2a 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 * Here is an int
f6c80 65 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 eresting observa
f6c90 74 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 tion: Win95, Wi
f6ca0 6e 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c n98, and WinME l
f6cb0 61 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 ack.** the LockF
f6cc0 69 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 ileEx() API. Bu
f6cd0 74 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 t we can still s
f6ce0 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 tatically link a
f6cf0 67 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 gainst that.** A
f6d00 50 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 PI as long as we
f6d10 20 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 don't call it w
f6d20 68 65 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 hen running Win9
f6d30 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 5/98/ME. A call
f6d40 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 to.** this rout
f6d50 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 ine is used to d
f6d60 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 etermine if the
f6d70 68 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 host is Win95/98
f6d80 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f /ME or.** WinNT/
f6d90 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 2K/XP so that we
f6da0 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 will know wheth
f6db0 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e er or not we can
f6dc0 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 safely call.**
f6dd0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 the LockFileEx()
f6de0 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c API..*/.#if SQL
f6df0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 ITE_OS_WINCE.# d
f6e00 65 66 69 6e 65 20 69 73 4e 54 28 29 20 20 28 31 efine isNT() (1
f6e10 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63 ).#else. static
f6e20 20 69 6e 74 20 69 73 4e 54 28 76 6f 69 64 29 7b int isNT(void){
f6e30 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
f6e40 5f 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b 0a 20 _os_type==0 ){.
f6e50 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e OSVERSIONIN
f6e60 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 FO sInfo;.
f6e70 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f sInfo.dwOSVersio
f6e80 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 nInfoSize = size
f6e90 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 of(sInfo);.
f6ea0 20 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 GetVersionEx(&s
f6eb0 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c Info);. sql
f6ec0 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 73 ite3_os_type = s
f6ed0 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 Info.dwPlatformI
f6ee0 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f d==VER_PLATFORM_
f6ef0 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 WIN32_NT ? 2 : 1
f6f00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
f6f10 72 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 rn sqlite3_os_ty
f6f20 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 pe==2;. }.#endi
f6f30 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 f /* SQLITE_OS_W
f6f40 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 INCE */../*.** C
f6f50 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 onvert a UTF-8 s
f6f60 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f tring to microso
f6f70 66 74 20 75 6e 69 63 6f 64 65 20 28 55 54 46 2d ft unicode (UTF-
f6f80 31 36 3f 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 16?). .**.** Spa
f6f90 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
f6fa0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
f6fb0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
f6fc0 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 malloc..*/.stati
f6fd0 63 20 57 43 48 41 52 20 2a 75 74 66 38 54 6f 55 c WCHAR *utf8ToU
f6fe0 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 nicode(const cha
f6ff0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
f7000 20 69 6e 74 20 6e 43 68 61 72 3b 0a 20 20 57 43 int nChar;. WC
f7010 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 HAR *zWideFilena
f7020 6d 65 3b 0a 0a 20 20 6e 43 68 61 72 20 3d 20 4d me;.. nChar = M
f7030 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 ultiByteToWideCh
f7040 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a ar(CP_UTF8, 0, z
f7050 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 Filename, -1, NU
f7060 4c 4c 2c 20 30 29 3b 0a 20 20 7a 57 69 64 65 46 LL, 0);. zWideF
f7070 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 ilename = malloc
f7080 28 20 6e 43 68 61 72 2a 73 69 7a 65 6f 66 28 7a ( nChar*sizeof(z
f7090 57 69 64 65 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 WideFilename[0])
f70a0 20 29 3b 0a 20 20 69 66 28 20 7a 57 69 64 65 46 );. if( zWideF
f70b0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 ilename==0 ){.
f70c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
f70d0 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 nChar = MultiB
f70e0 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 yteToWideChar(CP
f70f0 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e _UTF8, 0, zFilen
f7100 61 6d 65 2c 20 2d 31 2c 20 7a 57 69 64 65 46 69 ame, -1, zWideFi
f7110 6c 65 6e 61 6d 65 2c 20 6e 43 68 61 72 29 3b 0a lename, nChar);.
f7120 20 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20 29 if( nChar==0 )
f7130 7b 0a 20 20 20 20 66 72 65 65 28 7a 57 69 64 65 {. free(zWide
f7140 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a Filename);. z
f7150 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 30 WideFilename = 0
f7160 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
f7170 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a WideFilename;.}.
f7180 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d ./*.** Convert m
f7190 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
f71a0 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 to UTF-8. Spac
f71b0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 e to hold the re
f71c0 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
f71d0 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f .** obtained fro
f71e0 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 m malloc()..*/.s
f71f0 74 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 tatic char *unic
f7200 6f 64 65 54 6f 55 74 66 38 28 63 6f 6e 73 74 20 odeToUtf8(const
f7210 57 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 WCHAR *zWideFile
f7220 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
f7230 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c te;. char *zFil
f7240 65 6e 61 6d 65 3b 0a 0a 20 20 6e 42 79 74 65 20 ename;.. nByte
f7250 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 = WideCharToMult
f7260 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30 iByte(CP_UTF8, 0
f7270 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c , zWideFilename,
f7280 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 -1, 0, 0, 0, 0)
f7290 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 ;. zFilename =
f72a0 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b malloc( nByte );
f72b0 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 . if( zFilename
f72c0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f72d0 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 n 0;. }. nByte
f72e0 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c = WideCharToMul
f72f0 74 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 tiByte(CP_UTF8,
f7300 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 0, zWideFilename
f7310 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c , -1, zFilename,
f7320 20 6e 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20 nByte,.
f7330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7340 20 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 0, 0);. i
f7350 66 28 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b f( nByte == 0 ){
f7360 0a 20 20 20 20 66 72 65 65 28 7a 46 69 6c 65 6e . free(zFilen
f7370 61 6d 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e ame);. zFilen
f7380 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ame = 0;. }. r
f7390 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b eturn zFilename;
f73a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
f73b0 74 20 61 6e 20 61 6e 73 69 20 73 74 72 69 6e 67 t an ansi string
f73c0 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e to microsoft un
f73d0 69 63 6f 64 65 2c 20 62 61 73 65 64 20 6f 6e 20 icode, based on
f73e0 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 the.** current c
f73f0 6f 64 65 70 61 67 65 20 73 65 74 74 69 6e 67 73 odepage settings
f7400 20 66 6f 72 20 66 69 6c 65 20 61 70 69 73 2e 0a for file apis..
f7410 2a 2a 20 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 ** .** Space to
f7420 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
f7430 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 d string is obta
f7440 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c ined.** from mal
f7450 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 loc..*/.static W
f7460 43 48 41 52 20 2a 6d 62 63 73 54 6f 55 6e 69 63 CHAR *mbcsToUnic
f7470 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ode(const char *
f7480 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e zFilename){. in
f7490 74 20 6e 42 79 74 65 3b 0a 20 20 57 43 48 41 52 t nByte;. WCHAR
f74a0 20 2a 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b *zMbcsFilename;
f74b0 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 . int codepage
f74c0 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 = AreFileApisANS
f74d0 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 I() ? CP_ACP : C
f74e0 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 P_OEMCP;.. nByt
f74f0 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 e = MultiByteToW
f7500 69 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 ideChar(codepage
f7510 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
f7520 2d 31 2c 20 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 -1, NULL,0)*size
f7530 6f 66 28 57 43 48 41 52 29 3b 0a 20 20 7a 4d 62 of(WCHAR);. zMb
f7540 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c csFilename = mal
f7550 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f loc( nByte*sizeo
f7560 66 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b f(zMbcsFilename[
f7570 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62 0]) );. if( zMb
f7580 63 73 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b csFilename==0 ){
f7590 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
f75a0 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c }. nByte = Mul
f75b0 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 tiByteToWideChar
f75c0 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 (codepage, 0, zF
f75d0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 ilename, -1, zMb
f75e0 63 73 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 csFilename, nByt
f75f0 65 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3d e);. if( nByte=
f7600 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a =0 ){. free(z
f7610 4d 62 63 73 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 MbcsFilename);.
f7620 20 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 zMbcsFilename
f7630 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
f7640 72 6e 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 rn zMbcsFilename
f7650 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
f7660 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 rt microsoft uni
f7670 63 6f 64 65 20 74 6f 20 6d 75 6c 74 69 62 79 74 code to multibyt
f7680 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 e character stri
f7690 6e 67 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ng, based on the
f76a0 0a 2a 2a 20 75 73 65 72 27 73 20 41 6e 73 69 20 .** user's Ansi
f76b0 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 codepage..**.**
f76c0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
f76d0 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e e returned strin
f76e0 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 g is obtained fr
f76f0 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a om.** malloc()..
f7700 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a */.static char *
f7710 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 63 6f unicodeToMbcs(co
f7720 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64 65 nst WCHAR *zWide
f7730 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 Filename){. int
f7740 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a nByte;. char *
f7750 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 zFilename;. int
f7760 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46 codepage = AreF
f7770 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 ileApisANSI() ?
f7780 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 CP_ACP : CP_OEMC
f7790 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 P;.. nByte = Wi
f77a0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
f77b0 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a e(codepage, 0, z
f77c0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 WideFilename, -1
f77d0 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 , 0, 0, 0, 0);.
f77e0 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c zFilename = mal
f77f0 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 loc( nByte );.
f7800 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 if( zFilename==0
f7810 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
f7820 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 ;. }. nByte =
f7830 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 WideCharToMultiB
f7840 79 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c yte(codepage, 0,
f7850 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 zWideFilename,
f7860 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e -1, zFilename, n
f7870 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 Byte,.
f7880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7890 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 0, 0);. if(
f78a0 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 nByte == 0 ){.
f78b0 20 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d free(zFilenam
f78c0 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d e);. zFilenam
f78d0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
f78e0 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d urn zFilename;.}
f78f0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
f7900 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 multibyte charac
f7910 74 65 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54 ter string to UT
f7920 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 F-8. Space to h
f7930 6f 6c 64 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 old the.** retur
f7940 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 ned string is ob
f7950 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c tained from mall
f7960 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oc()..*/.SQLITE_
f7970 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
f7980 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 3_win32_mbcs_to_
f7990 75 74 66 38 28 63 6f 6e 73 74 20 63 68 61 72 20 utf8(const char
f79a0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 *zFilename){. c
f79b0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 55 74 har *zFilenameUt
f79c0 66 38 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d f8;. WCHAR *zTm
f79d0 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 pWide;.. zTmpWi
f79e0 64 65 20 3d 20 6d 62 63 73 54 6f 55 6e 69 63 6f de = mbcsToUnico
f79f0 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 de(zFilename);.
f7a00 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 if( zTmpWide==0
f7a10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
f7a20 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d ;. }. zFilenam
f7a30 65 55 74 66 38 20 3d 20 75 6e 69 63 6f 64 65 54 eUtf8 = unicodeT
f7a40 6f 55 74 66 38 28 7a 54 6d 70 57 69 64 65 29 3b oUtf8(zTmpWide);
f7a50 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 . free(zTmpWide
f7a60 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c );. return zFil
f7a70 65 6e 61 6d 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a enameUtf8;.}../*
f7a80 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d .** Convert UTF-
f7a90 38 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 8 to multibyte c
f7aa0 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e haracter string.
f7ab0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
f7ac0 74 68 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 the .** returned
f7ad0 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
f7ae0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 ned from malloc(
f7af0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 )..*/.static cha
f7b00 72 20 2a 75 74 66 38 54 6f 4d 62 63 73 28 63 6f r *utf8ToMbcs(co
f7b10 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
f7b20 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 ame){. char *zF
f7b30 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 20 20 57 ilenameMbcs;. W
f7b40 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a CHAR *zTmpWide;.
f7b50 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 75 74 . zTmpWide = ut
f7b60 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c f8ToUnicode(zFil
f7b70 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 ename);. if( zT
f7b80 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 mpWide==0 ){.
f7b90 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
f7ba0 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 20 3d zFilenameMbcs =
f7bb0 20 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 7a unicodeToMbcs(z
f7bc0 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 TmpWide);. free
f7bd0 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 (zTmpWide);. re
f7be0 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 turn zFilenameMb
f7bf0 63 73 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 cs;.}..#if SQLIT
f7c00 45 5f 4f 53 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a E_OS_WINCE./****
f7c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7c50 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 *****.** This se
f7c60 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 63 ction contains c
f7c70 6f 64 65 20 66 6f 72 20 57 69 6e 43 45 20 6f 6e ode for WinCE on
f7c80 6c 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e ly..*/./*.** Win
f7c90 64 6f 77 73 43 45 20 64 6f 65 73 20 6e 6f 74 20 dowsCE does not
f7ca0 68 61 76 65 20 61 20 6c 6f 63 61 6c 74 69 6d 65 have a localtime
f7cb0 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f () function. So
f7cc0 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 73 75 62 create a.** sub
f7cd0 73 74 69 74 75 74 65 2e 0a 2a 2f 0a 73 74 72 75 stitute..*/.stru
f7ce0 63 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c ct tm *__cdecl l
f7cf0 6f 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 20 74 ocaltime(const t
f7d00 69 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20 73 74 ime_t *t).{. st
f7d10 61 74 69 63 20 73 74 72 75 63 74 20 74 6d 20 79 atic struct tm y
f7d20 3b 0a 20 20 46 49 4c 45 54 49 4d 45 20 75 54 6d ;. FILETIME uTm
f7d30 2c 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45 4d 54 , lTm;. SYSTEMT
f7d40 49 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c 69 74 IME pTm;. sqlit
f7d50 65 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a 20 20 e3_int64 t64;.
f7d60 74 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36 34 20 t64 = *t;. t64
f7d70 3d 20 28 74 36 34 20 2b 20 31 31 36 34 34 34 37 = (t64 + 1164447
f7d80 33 36 30 30 29 2a 31 30 30 30 30 30 30 30 3b 0a 3600)*10000000;.
f7d90 20 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74 65 54 uTm.dwLowDateT
f7da0 69 6d 65 20 3d 20 28 44 57 4f 52 44 29 28 74 36 ime = (DWORD)(t6
f7db0 34 20 26 20 30 78 46 46 46 46 46 46 46 46 29 3b 4 & 0xFFFFFFFF);
f7dc0 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 . uTm.dwHighDat
f7dd0 65 54 69 6d 65 3d 20 28 44 57 4f 52 44 29 28 74 eTime= (DWORD)(t
f7de0 36 34 20 3e 3e 20 33 32 29 3b 0a 20 20 46 69 6c 64 >> 32);. Fil
f7df0 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 eTimeToLocalFile
f7e00 54 69 6d 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b Time(&uTm,&lTm);
f7e10 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 53 79 73 . FileTimeToSys
f7e20 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54 temTime(&lTm,&pT
f7e30 6d 29 3b 0a 20 20 79 2e 74 6d 5f 79 65 61 72 20 m);. y.tm_year
f7e40 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d 20 31 39 = pTm.wYear - 19
f7e50 30 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 00;. y.tm_mon =
f7e60 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b pTm.wMonth - 1;
f7e70 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20 3d 20 70 . y.tm_wday = p
f7e80 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 20 Tm.wDayOfWeek;.
f7e90 20 79 2e 74 6d 5f 6d 64 61 79 20 3d 20 70 54 6d y.tm_mday = pTm
f7ea0 2e 77 44 61 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f .wDay;. y.tm_ho
f7eb0 75 72 20 3d 20 70 54 6d 2e 77 48 6f 75 72 3b 0a ur = pTm.wHour;.
f7ec0 20 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d y.tm_min = pTm
f7ed0 2e 77 4d 69 6e 75 74 65 3b 0a 20 20 79 2e 74 6d .wMinute;. y.tm
f7ee0 5f 73 65 63 20 3d 20 70 54 6d 2e 77 53 65 63 6f _sec = pTm.wSeco
f7ef0 6e 64 3b 0a 20 20 72 65 74 75 72 6e 20 26 79 3b nd;. return &y;
f7f00 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 77 69 6c 6c .}../* This will
f7f10 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 never be called
f7f20 2c 20 62 75 74 20 64 65 66 69 6e 65 64 20 74 6f , but defined to
f7f30 20 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20 63 make the code c
f7f40 6f 6d 70 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e ompile */.#defin
f7f50 65 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 61 e GetTempPathA(a
f7f60 2c 62 29 0a 0a 23 64 65 66 69 6e 65 20 4c 6f 63 ,b)..#define Loc
f7f70 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 kFile(a,b,c,d,e)
f7f80 20 20 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b winceLock
f7f90 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 File(&a, b, c, d
f7fa0 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 55 6e 6c , e).#define Unl
f7fb0 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c ockFile(a,b,c,d,
f7fc0 65 29 20 20 20 20 20 77 69 6e 63 65 55 6e 6c 6f e) winceUnlo
f7fd0 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c ckFile(&a, b, c,
f7fe0 20 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 4c d, e).#define L
f7ff0 6f 63 6b 46 69 6c 65 45 78 28 61 2c 62 2c 63 2c ockFileEx(a,b,c,
f8000 64 2c 65 2c 66 29 20 20 20 77 69 6e 63 65 4c 6f d,e,f) winceLo
f8010 63 6b 46 69 6c 65 45 78 28 26 61 2c 20 62 2c 20 ckFileEx(&a, b,
f8020 63 2c 20 64 2c 20 65 2c 20 66 29 0a 0a 23 64 65 c, d, e, f)..#de
f8030 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 fine HANDLE_TO_W
f8040 49 4e 46 49 4c 45 28 61 29 20 28 77 69 6e 46 69 INFILE(a) (winFi
f8050 6c 65 2a 29 26 28 28 63 68 61 72 2a 29 61 29 5b le*)&((char*)a)[
f8060 2d 28 69 6e 74 29 6f 66 66 73 65 74 6f 66 28 77 -(int)offsetof(w
f8070 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a inFile,h)]../*.*
f8080 2a 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b * Acquire a lock
f8090 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 on the handle h
f80a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
f80b0 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 winceMutexAcquir
f80c0 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 e(HANDLE h){.
f80d0 44 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 DWORD dwErr;.
f80e0 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 do {. dwErr
f80f0 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f = WaitForSingleO
f8100 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 bject(h, INFINIT
f8110 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 E);. } while (
f8120 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 dwErr != WAIT_OB
f8130 4a 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 JECT_0 && dwErr
f8140 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 != WAIT_ABANDONE
f8150 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 D);.}./*.** Rele
f8160 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 ase a lock acqui
f8170 72 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 red by winceMute
f8180 78 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 xAcquire().*/.#d
f8190 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 efine winceMutex
f81a0 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 Release(h) Relea
f81b0 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a seMutex(h)../*.*
f81c0 2a 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 * Create the mut
f81d0 65 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 ex and shared me
f81e0 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f mory used for lo
f81f0 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c cking in the fil
f8200 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 e.** descriptor
f8210 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 pFile.*/.static
f8220 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 BOOL winceCreate
f8230 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 Lock(const char
f8240 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 *zFilename, winF
f8250 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 ile *pFile){. W
f8260 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 CHAR *zTok;. WC
f8270 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 HAR *zName = utf
f8280 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 8ToUnicode(zFile
f8290 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 name);. BOOL bI
f82a0 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f nit = TRUE;.. /
f82b0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
f82c0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 local lockdata
f82d0 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 */. ZeroMemory(
f82e0 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 &pFile->local, s
f82f0 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 izeof(pFile->loc
f8300 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c al));.. /* Repl
f8310 61 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 ace the backslas
f8320 68 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c hes from the fil
f8330 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 ename and lowerc
f8340 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 ase it. ** to d
f8350 65 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 erive a mutex na
f8360 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 me. */. zTok =
f8370 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 CharLowerW(zName
f8380 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b );. for (;*zTok
f8390 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 ;zTok++){. if
f83a0 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 (*zTok == '\\')
f83b0 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 *zTok = '_';.
f83c0 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f }.. /* Create/o
f83d0 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 pen the named mu
f83e0 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e tex */. pFile->
f83f0 68 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d hMutex = CreateM
f8400 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 utexW(NULL, FALS
f8410 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 E, zName);. if
f8420 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 (!pFile->hMutex)
f8430 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
f8440 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
f8450 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 66 72 65 Error();. fre
f8460 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 e(zName);. re
f8470 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a turn FALSE;. }.
f8480 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 . /* Acquire th
f8490 65 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 63 e mutex before c
f84a0 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 ontinuing */. w
f84b0 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 inceMutexAcquire
f84c0 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
f84d0 0a 20 20 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74 . . /* Since t
f84e0 68 65 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65 he names of name
f84f0 64 20 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70 d mutexes, semap
f8500 68 6f 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70 hores, file mapp
f8510 69 6e 67 73 20 65 74 63 20 61 72 65 20 0a 20 20 ings etc are .
f8520 2a 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76 ** case-sensitiv
f8530 65 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 e, take advantag
f8540 65 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70 e of that by upp
f8550 65 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74 ercasing the mut
f8560 65 78 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 ex name. ** and
f8570 20 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74 using that as t
f8580 68 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61 he shared filema
f8590 70 70 69 6e 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f pping name.. */
f85a0 0a 20 20 43 68 61 72 55 70 70 65 72 57 28 7a 4e . CharUpperW(zN
f85b0 61 6d 65 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 ame);. pFile->h
f85c0 53 68 61 72 65 64 20 3d 20 43 72 65 61 74 65 46 Shared = CreateF
f85d0 69 6c 65 4d 61 70 70 69 6e 67 57 28 49 4e 56 41 ileMappingW(INVA
f85e0 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
f85f0 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 , NULL,.
f8600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
f8620 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30 AGE_READWRITE, 0
f8630 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f , sizeof(winceLo
f8640 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ck),.
f8650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8660 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d zNam
f8670 65 29 3b 20 20 0a 0a 20 20 2f 2a 20 53 65 74 20 e); .. /* Set
f8680 61 20 66 6c 61 67 20 74 68 61 74 20 69 6e 64 69 a flag that indi
f8690 63 61 74 65 73 20 77 65 27 72 65 20 74 68 65 20 cates we're the
f86a0 66 69 72 73 74 20 74 6f 20 63 72 65 61 74 65 20 first to create
f86b0 74 68 65 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74 the memory so it
f86c0 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a . ** must be z
f86d0 65 72 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 ero-initialized
f86e0 2a 2f 0a 20 20 69 66 20 28 47 65 74 4c 61 73 74 */. if (GetLast
f86f0 45 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52 Error() == ERROR
f8700 5f 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29 _ALREADY_EXISTS)
f8710 7b 0a 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41 {. bInit = FA
f8720 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 LSE;. }.. free
f8730 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 (zName);.. /* I
f8740 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 f we succeeded i
f8750 6e 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61 n making the sha
f8760 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c red memory handl
f8770 65 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20 e, map it. */.
f8780 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 if (pFile->hShar
f8790 65 64 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ed){. pFile->
f87a0 73 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c shared = (winceL
f87b0 6f 63 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69 ock*)MapViewOfFi
f87c0 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 le(pFile->hShare
f87d0 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 d, .
f87e0 20 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46 FILE_MAP_READ|F
f87f0 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30 ILE_MAP_WRITE, 0
f8800 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 , 0, sizeof(winc
f8810 65 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 eLock));. /*
f8820 49 66 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65 If mapping faile
f8830 64 2c 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61 d, close the sha
f8840 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c red memory handl
f8850 65 20 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a e and erase it *
f8860 2f 0a 20 20 20 20 69 66 20 28 21 70 46 69 6c 65 /. if (!pFile
f8870 2d 3e 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20 ->shared){.
f8880 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
f8890 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
f88a0 28 29 3b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 ();. CloseH
f88b0 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 andle(pFile->hSh
f88c0 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 ared);. pFi
f88d0 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 le->hShared = NU
f88e0 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 LL;. }. }..
f88f0 20 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 /* If shared me
f8900 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 mory could not b
f8910 65 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 e created, then
f8920 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 close the mutex
f8930 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 and fail */. if
f8940 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 (pFile->hShared
f8950 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 == NULL){. w
f8960 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 inceMutexRelease
f8970 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
f8980 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 . CloseHandle
f8990 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
f89a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 . pFile->hMut
f89b0 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 ex = NULL;. r
f89c0 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d eturn FALSE;. }
f89d0 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c . . /* Initial
f89e0 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d ize the shared m
f89f0 65 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 emory if we're s
f8a00 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 upposed to */.
f8a10 69 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 if (bInit) {.
f8a20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c ZeroMemory(pFil
f8a30 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f e->shared, sizeo
f8a40 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 f(winceLock));.
f8a50 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 }.. winceMutex
f8a60 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
f8a70 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e Mutex);. return
f8a80 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TRUE;.}../*.**
f8a90 44 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 Destroy the part
f8aa0 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 of winFile that
f8ab0 20 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 deals with winc
f8ac0 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 e locks.*/.stati
f8ad0 63 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 c void winceDest
f8ae0 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 royLock(winFile
f8af0 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 *pFile){. if (p
f8b00 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 File->hMutex){.
f8b10 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 /* Acquire th
f8b20 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 e mutex */. w
f8b30 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 inceMutexAcquire
f8b40 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
f8b50 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c .. /* The fol
f8b60 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 lowing blocks sh
f8b70 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 ould probably as
f8b80 73 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f sert in debug mo
f8b90 64 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 de, but they.
f8ba0 20 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e are to clean
f8bb0 75 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c up in case any l
f8bc0 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 ocks remained op
f8bd0 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 en */. if (pF
f8be0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
f8bf0 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ers){. pFil
f8c00 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 e->shared->nRead
f8c10 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 ers --;. }.
f8c20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
f8c30 61 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 al.bReserved){.
f8c40 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f8c50 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 ed->bReserved =
f8c60 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 FALSE;. }.
f8c70 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
f8c80 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 l.bPending){.
f8c90 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 pFile->shared
f8ca0 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c ->bPending = FAL
f8cb0 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 SE;. }. if
f8cc0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 (pFile->local.b
f8cd0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 Exclusive){.
f8ce0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f8cf0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 >bExclusive = FA
f8d00 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 LSE;. }..
f8d10 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 /* De-reference
f8d20 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f and close our co
f8d30 70 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 py of the shared
f8d40 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a memory handle *
f8d50 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f /. UnmapViewO
f8d60 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 fFile(pFile->sha
f8d70 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 red);. CloseH
f8d80 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 andle(pFile->hSh
f8d90 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 ared);.. /* D
f8da0 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 74 one with the mut
f8db0 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d ex */. winceM
f8dc0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
f8dd0 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a e->hMutex); .
f8de0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 CloseHandle(
f8df0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f8e00 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 pFile->hMute
f8e10 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a x = NULL;. }.}.
f8e20 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 ./* .** An imple
f8e30 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
f8e40 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 LockFile() API
f8e50 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 of windows for w
f8e60 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 ince.*/.static B
f8e70 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c OOL winceLockFil
f8e80 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 e(. HANDLE *phF
f8e90 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 ile,. DWORD dwF
f8ea0 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 ileOffsetLow,.
f8eb0 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 DWORD dwFileOffs
f8ec0 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 etHigh,. DWORD
f8ed0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f8ee0 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 LockLow,. DWORD
f8ef0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f8f00 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 oLockHigh.){. w
f8f10 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
f8f20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c HANDLE_TO_WINFIL
f8f30 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f E(phFile);. BOO
f8f40 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 L bReturn = FALS
f8f50 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 E;.. UNUSED_PAR
f8f60 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 66 AMETER(dwFileOff
f8f70 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55 53 setHigh);. UNUS
f8f80 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 ED_PARAMETER(nNu
f8f90 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
f8fa0 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 kHigh);.. if (!
f8fb0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 pFile->hMutex) r
f8fc0 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 eturn TRUE;. wi
f8fd0 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
f8fe0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f8ff0 0a 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e . /* Wanting an
f9000 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f exclusive lock?
f9010 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 */. if (dwFile
f9020 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 OffsetLow == (DW
f9030 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 ORD)SHARED_FIRST
f9040 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75 6d 62 . && nNumb
f9050 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f9060 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 ow == (DWORD)SHA
f9070 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 RED_SIZE){. i
f9080 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 f (pFile->shared
f9090 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 ->nReaders == 0
f90a0 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 && pFile->shared
f90b0 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 ->bExclusive ==
f90c0 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 0){. pFile
f90d0 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 ->shared->bExclu
f90e0 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 sive = TRUE;.
f90f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
f9100 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 .bExclusive = TR
f9110 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 UE;. bRetu
f9120 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d rn = TRUE;. }
f9130 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 . }.. /* Want
f9140 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b a read-only lock
f9150 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 ? */. else if (
f9160 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f9170 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f9180 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 _FIRST &&.
f9190 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79 nNumberOfBy
f91a0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 tesToLockLow ==
f91b0 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 1){. if (pFil
f91c0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c e->shared->bExcl
f91d0 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 usive == 0){.
f91e0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e pFile->local.
f91f0 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 nReaders ++;.
f9200 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f if (pFile->lo
f9210 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 cal.nReaders ==
f9220 31 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 1){. pFil
f9230 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 e->shared->nRead
f9240 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a ers ++;. }.
f9250 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
f9260 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
f9270 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 70 65 6e . /* Want a pen
f9280 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 ding lock? */.
f9290 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f else if (dwFileO
f92a0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f ffsetLow == (DWO
f92b0 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 RD)PENDING_BYTE
f92c0 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 && nNumberOfByte
f92d0 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 sToLockLow == 1)
f92e0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 {. /* If no p
f92f0 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 ending lock has
f9300 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 been acquired, t
f9310 68 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a hen acquire it *
f9320 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d /. if (pFile-
f9330 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e >shared->bPendin
f9340 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 g == 0) {.
f9350 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f9360 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a Pending = TRUE;.
f9370 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
f9380 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 al.bPending = TR
f9390 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 UE;. bRetur
f93a0 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a n = TRUE;. }.
f93b0 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 }.. /* Want a
f93c0 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 reserved lock?
f93d0 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 */. else if (dw
f93e0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d FileOffsetLow ==
f93f0 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 44 (DWORD)RESERVED
f9400 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 _BYTE && nNumber
f9410 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 OfBytesToLockLow
f9420 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 == 1){. if (
f9430 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f9440 52 65 73 65 72 76 65 64 20 3d 3d 20 30 29 20 7b Reserved == 0) {
f9450 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
f9460 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 ared->bReserved
f9470 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 = TRUE;. pF
f9480 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 ile->local.bRese
f9490 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 rved = TRUE;.
f94a0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 bReturn = TRU
f94b0 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 E;. }. }..
f94c0 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 winceMutexReleas
f94d0 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
f94e0 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 ;. return bRetu
f94f0 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 rn;.}../*.** An
f9500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
f9510 66 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 f the UnlockFile
f9520 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 API of windows
f9530 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 for wince.*/.sta
f9540 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55 6e tic BOOL winceUn
f9550 6c 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 lockFile(. HAND
f9560 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 LE *phFile,. DW
f9570 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 ORD dwFileOffset
f9580 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 Low,. DWORD dwF
f9590 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 ileOffsetHigh,.
f95a0 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 DWORD nNumberOf
f95b0 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 BytesToUnlockLow
f95c0 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 ,. DWORD nNumbe
f95d0 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
f95e0 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c High.){. winFil
f95f0 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c e *pFile = HANDL
f9600 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46 E_TO_WINFILE(phF
f9610 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 ile);. BOOL bRe
f9620 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 turn = FALSE;..
f9630 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f9640 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 R(dwFileOffsetHi
f9650 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 gh);. UNUSED_PA
f9660 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f RAMETER(nNumberO
f9670 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 fBytesToUnlockHi
f9680 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 gh);.. if (!pFi
f9690 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 le->hMutex) retu
f96a0 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 rn TRUE;. wince
f96b0 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 MutexAcquire(pFi
f96c0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 le->hMutex);..
f96d0 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 /* Releasing a r
f96e0 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e eader lock or an
f96f0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
f9700 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f */. if (dwFileO
f9710 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f ffsetLow == (DWO
f9720 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 29 RD)SHARED_FIRST)
f9730 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 {. /* Did we
f9740 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 have an exclusiv
f9750 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 e lock? */. i
f9760 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f9770 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 bExclusive){.
f9780 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65 assert(nNumbe
f9790 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
f97a0 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 Low == (DWORD)SH
f97b0 41 52 45 44 5f 53 49 5a 45 29 3b 0a 20 20 20 20 ARED_SIZE);.
f97c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 pFile->local.b
f97d0 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 Exclusive = FALS
f97e0 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e E;. pFile->
f97f0 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
f9800 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 ve = FALSE;.
f9810 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
f9820 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
f9830 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 Did we just have
f9840 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 a reader lock?
f9850 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 */. else if (
f9860 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 pFile->local.nRe
f9870 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 61 73 aders){. as
f9880 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79 sert(nNumberOfBy
f9890 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
f98a0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f98b0 53 49 5a 45 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f SIZE || nNumberO
f98c0 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f fBytesToUnlockLo
f98d0 77 20 3d 3d 20 31 29 3b 0a 20 20 20 20 20 20 70 w == 1);. p
f98e0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
f98f0 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 ders --;. i
f9900 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f9910 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 nReaders == 0).
f9920 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 {. p
f9930 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 File->shared->nR
f9940 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 eaders --;.
f9950 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e }. bReturn
f9960 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
f9970 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 }.. /* Releasi
f9980 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 ng a pending loc
f9990 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 k */. else if (
f99a0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f99b0 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e == (DWORD)PENDIN
f99c0 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 G_BYTE && nNumbe
f99d0 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
f99e0 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 Low == 1){. i
f99f0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f9a00 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 bPending){.
f9a10 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 pFile->local.bP
f9a20 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a ending = FALSE;.
f9a30 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f9a40 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 red->bPending =
f9a50 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 FALSE;. bRe
f9a60 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
f9a70 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 }. }. /* Rele
f9a80 61 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 asing a reserved
f9a90 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 lock */. else
f9aa0 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
f9ab0 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45 Low == (DWORD)RE
f9ac0 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e SERVED_BYTE && n
f9ad0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 NumberOfBytesToU
f9ae0 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a nlockLow == 1){.
f9af0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c if (pFile->l
f9b00 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20 ocal.bReserved)
f9b10 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
f9b20 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d ocal.bReserved =
f9b30 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 FALSE;. pF
f9b40 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 ile->shared->bRe
f9b50 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a served = FALSE;.
f9b60 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
f9b70 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
f9b80 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c . winceMutexRel
f9b90 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 ease(pFile->hMut
f9ba0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 ex);. return bR
f9bb0 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
f9bc0 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f An implementatio
f9bd0 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c n of the LockFil
f9be0 65 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 6e eEx() API of win
f9bf0 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a dows for wince.*
f9c00 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 /.static BOOL wi
f9c10 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a 20 nceLockFileEx(.
f9c20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c HANDLE *phFile,
f9c30 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 . DWORD dwFlags
f9c40 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 65 ,. DWORD dwRese
f9c50 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e 4e rved,. DWORD nN
f9c60 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
f9c70 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e ckLow,. DWORD n
f9c80 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f9c90 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 45 ockHigh,. LPOVE
f9ca0 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c 61 RLAPPED lpOverla
f9cb0 70 70 65 64 0a 29 7b 0a 20 20 55 4e 55 53 45 44 pped.){. UNUSED
f9cc0 5f 50 41 52 41 4d 45 54 45 52 28 64 77 52 65 73 _PARAMETER(dwRes
f9cd0 65 72 76 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 erved);. UNUSED
f9ce0 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 _PARAMETER(nNumb
f9cf0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 erOfBytesToLockH
f9d00 69 67 68 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 igh);.. /* If t
f9d10 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73 20 he caller wants
f9d20 61 20 73 68 61 72 65 64 20 72 65 61 64 20 6c 6f a shared read lo
f9d30 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69 73 ck, forward this
f9d40 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 call. ** to wi
f9d50 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 nceLockFile */.
f9d60 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 70 65 if (lpOverlappe
f9d70 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 28 44 57 d->Offset == (DW
f9d80 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 ORD)SHARED_FIRST
f9d90 20 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 &&. dwFlag
f9da0 73 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 s == 1 &&.
f9db0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9dc0 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 LockLow == (DWOR
f9dd0 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a D)SHARED_SIZE){.
f9de0 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 63 65 return wince
f9df0 4c 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c LockFile(phFile,
f9e00 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 SHARED_FIRST, 0
f9e10 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 , 1, 0);. }. r
f9e20 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f eturn FALSE;.}./
f9e30 2a 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 *.** End of the
f9e40 73 70 65 63 69 61 6c 20 63 6f 64 65 20 66 6f 72 special code for
f9e50 20 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a wince.*********
f9e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ea0 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 ****/.#endif /*
f9eb0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
f9ec0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
f9ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9f10 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 **.** The next g
f9f20 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 roup of routines
f9f30 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 implement the I
f9f40 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 /O methods speci
f9f50 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 fied.** by the s
f9f60 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
f9f70 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a s object..******
f9f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
f9fd0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a Close a file..*
f9fe0 2a 0a 2a 2a 20 49 74 20 69 73 20 72 65 70 6f 72 *.** It is repor
f9ff0 74 65 64 20 74 68 61 74 20 61 6e 20 61 74 74 65 ted that an atte
fa000 6d 70 74 20 74 6f 20 63 6c 6f 73 65 20 61 20 68 mpt to close a h
fa010 61 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f 6d 65 andle might some
fa020 74 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e 20 20 times.** fail.
fa030 54 68 69 73 20 69 73 20 61 20 76 65 72 79 20 75 This is a very u
fa040 6e 72 65 61 73 6f 6e 61 62 6c 65 20 72 65 73 75 nreasonable resu
fa050 6c 74 2c 20 62 75 74 20 77 69 6e 64 6f 77 73 20 lt, but windows
fa060 69 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a 2a 20 is notorious.**
fa070 66 6f 72 20 62 65 69 6e 67 20 75 6e 72 65 61 73 for being unreas
fa080 6f 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f 20 6e onable so I do n
fa090 6f 74 20 64 6f 75 62 74 20 74 68 61 74 20 69 74 ot doubt that it
fa0a0 20 6d 69 67 68 74 20 68 61 70 70 65 6e 2e 20 20 might happen.
fa0b0 49 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73 65 20 If.** the close
fa0c0 66 61 69 6c 73 2c 20 77 65 20 70 61 75 73 65 20 fails, we pause
fa0d0 66 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73 65 63 for 100 millisec
fa0e0 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 61 67 61 onds and try aga
fa0f0 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e 79 20 in. As.** many
fa100 61 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 as MX_CLOSE_ATTE
fa110 4d 50 54 20 61 74 74 65 6d 70 74 73 20 74 6f 20 MPT attempts to
fa120 63 6c 6f 73 65 20 74 68 65 20 68 61 6e 64 6c 65 close the handle
fa130 20 61 72 65 20 6d 61 64 65 20 62 65 66 6f 72 65 are made before
fa140 0a 2a 2a 20 67 69 76 69 6e 67 20 75 70 20 61 6e .** giving up an
fa150 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 d returning an e
fa160 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rror..*/.#define
fa170 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 MX_CLOSE_ATTEMP
fa180 54 20 33 0a 73 74 61 74 69 63 20 69 6e 74 20 77 T 3.static int w
fa190 69 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f inClose(sqlite3_
fa1a0 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 file *id){. int
fa1b0 20 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a 20 20 rc, cnt = 0;.
fa1c0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
fa1d0 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a (winFile*)id;..
fa1e0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
fa1f0 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 );. OSTRACE2("C
fa200 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c LOSE %d\n", pFil
fa210 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 e->h);. do{.
fa220 20 72 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c rc = CloseHandl
fa230 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d e(pFile->h);. }
fa240 77 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 while( rc==0 &&
fa250 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 ++cnt < MX_CLOSE
fa260 5f 41 54 54 45 4d 50 54 20 26 26 20 28 53 6c 65 _ATTEMPT && (Sle
fa270 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 ep(100), 1) );.#
fa280 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
fa290 43 45 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 45 CE.#define WINCE
fa2a0 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 _DELETION_ATTEMP
fa2b0 54 53 20 33 0a 20 20 77 69 6e 63 65 44 65 73 74 TS 3. winceDest
fa2c0 72 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a royLock(pFile);.
fa2d0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 if( pFile->zDe
fa2e0 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 leteOnClose ){.
fa2f0 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a int cnt = 0;.
fa300 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 while(.
fa310 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 DeleteFile
fa320 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 W(pFile->zDelete
fa330 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 OnClose)==0.
fa340 20 20 20 20 26 26 20 47 65 74 46 69 6c 65 41 74 && GetFileAt
fa350 74 72 69 62 75 74 65 73 57 28 70 46 69 6c 65 2d tributesW(pFile-
fa360 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 >zDeleteOnClose)
fa370 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20 20 !=0xffffffff .
fa380 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c && cnt++ <
fa390 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f WINCE_DELETION_
fa3a0 41 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a ATTEMPTS. ){.
fa3b0 20 20 20 20 20 20 20 53 6c 65 65 70 28 31 30 30 Sleep(100
fa3c0 29 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 ); /* Wait a li
fa3d0 74 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 ttle before tryi
fa3e0 6e 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 ng again */.
fa3f0 7d 0a 20 20 20 20 66 72 65 65 28 70 46 69 6c 65 }. free(pFile
fa400 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 ->zDeleteOnClose
fa410 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
fa420 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b OpenCounter(-1);
fa430 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3f 20 53 . return rc ? S
fa440 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
fa450 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a E_IOERR;.}../*.*
fa460 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 * Some microsoft
fa470 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 compilers lack
fa480 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e this definition.
fa490 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 .*/.#ifndef INVA
fa4a0 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 LID_SET_FILE_POI
fa4b0 4e 54 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e NTER.# define IN
fa4c0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 VALID_SET_FILE_P
fa4d0 4f 49 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d OINTER ((DWORD)-
fa4e0 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 1).#endif../*.**
fa4f0 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 Read data from
fa500 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 a file into a bu
fa510 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 ffer. Return SQ
fa520 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a LITE_OK if all.*
fa530 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 * bytes were rea
fa540 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 d successfully a
fa550 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 nd SQLITE_IOERR
fa560 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
fa570 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 .** wrong..*/.st
fa580 61 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64 atic int winRead
fa590 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
fa5a0 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f *id, /
fa5b0 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 * File to read f
fa5c0 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 rom */. void *p
fa5d0 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
fa5e0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e /* Write con
fa5f0 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 tent into this b
fa600 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 uffer */. int a
fa610 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 mt,
fa620 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
fa630 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 of bytes to read
fa640 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
fa650 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 t64 offset
fa660 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e /* Begin readin
fa670 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 g at this offset
fa680 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 */.){. LONG up
fa690 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 perBits = (LONG)
fa6a0 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 ((offset>>32) &
fa6b0 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 0x7fffffff);. L
fa6c0 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 ONG lowerBits =
fa6d0 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 (LONG)(offset &
fa6e0 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 0xffffffff);. D
fa6f0 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 WORD rc;. winFi
fa700 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fa710 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
fa720 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 D error;. DWORD
fa730 20 67 6f 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 got;.. assert(
fa740 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 id!=0 );. Simu
fa750 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
fa760 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
fa770 52 45 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 READ);. OSTRACE
fa780 33 28 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 3("READ %d lock=
fa790 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
fa7a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
fa7b0 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c );. rc = SetFil
fa7c0 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e ePointer(pFile->
fa7d0 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 h, lowerBits, &u
fa7e0 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 pperBits, FILE_B
fa7f0 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d EGIN);. if( rc=
fa800 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c =INVALID_SET_FIL
fa810 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 E_POINTER && (er
fa820 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 ror=GetLastError
fa830 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b ())!=NO_ERROR ){
fa840 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
fa850 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 Errno = error;.
fa860 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fa870 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 _FULL;. }. if(
fa880 20 21 52 65 61 64 46 69 6c 65 28 70 46 69 6c 65 !ReadFile(pFile
fa890 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 ->h, pBuf, amt,
fa8a0 26 67 6f 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 &got, 0) ){.
fa8b0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
fa8c0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fa8d0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
fa8e0 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b LITE_IOERR_READ;
fa8f0 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d 3d . }. if( got==
fa900 28 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 20 (DWORD)amt ){.
fa910 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fa920 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
fa930 20 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73 /* Unread parts
fa940 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d of the buffer m
fa950 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c ust be zero-fill
fa960 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 ed */. memset
fa970 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b (&((char*)pBuf)[
fa980 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 got], 0, amt-got
fa990 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
fa9a0 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
fa9b0 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a _READ;. }.}../*
fa9c0 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 .** Write data f
fa9d0 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 rom a buffer int
fa9e0 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 o a file. Retur
fa9f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
faa00 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d uccess.** or som
faa10 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f e other error co
faa20 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a de on failure..*
faa30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
faa40 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 Write(. sqlite3
faa50 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 _file *id,
faa60 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 /* File to wr
faa70 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f ite into */. co
faa80 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 nst void *pBuf,
faa90 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 /* The b
faaa0 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 ytes to be writt
faab0 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c en */. int amt,
faac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
faad0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
faae0 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f ytes to write */
faaf0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
fab00 20 6f 66 66 73 65 74 20 20 20 20 20 20 2f 2a 20 offset /*
fab10 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 Offset into the
fab20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72 file to begin wr
fab30 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 iting at */.){.
fab40 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 LONG upperBits
fab50 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 = (LONG)((offset
fab60 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 >>32) & 0x7fffff
fab70 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 ff);. LONG lowe
fab80 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f rBits = (LONG)(o
fab90 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 ffset & 0xffffff
faba0 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b ff);. DWORD rc;
fabb0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
fabc0 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
fabd0 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b ;. DWORD error;
fabe0 0a 20 20 44 57 4f 52 44 20 77 72 6f 74 65 20 3d . DWORD wrote =
fabf0 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 0;.. assert( i
fac00 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 d!=0 );. Simula
fac10 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e teIOError(return
fac20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 SQLITE_IOERR_WR
fac30 49 54 45 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 ITE);. Simulate
fac40 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65 DiskfullError(re
fac50 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
fac60 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 57 );. OSTRACE3("W
fac70 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c RITE %d lock=%d\
fac80 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 n", pFile->h, pF
fac90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a ile->locktype);.
faca0 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f rc = SetFilePo
facb0 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 inter(pFile->h,
facc0 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 lowerBits, &uppe
facd0 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 rBits, FILE_BEGI
face0 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e N);. if( rc==IN
facf0 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 VALID_SET_FILE_P
fad00 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 OINTER && (error
fad10 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 =GetLastError())
fad20 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 !=NO_ERROR ){.
fad30 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
fad40 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 no = error;.
fad50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 return SQLITE_FU
fad60 4c 4c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 LL;. }. assert
fad70 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 ( amt>0 );. whi
fad80 6c 65 28 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20 le(. amt>0.
fad90 20 20 20 20 26 26 20 28 72 63 20 3d 20 57 72 69 && (rc = Wri
fada0 74 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c teFile(pFile->h,
fadb0 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f pBuf, amt, &wro
fadc0 74 65 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20 te, 0))!=0.
fadd0 26 26 20 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a && wrote>0. ){.
fade0 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 amt -= wrote
fadf0 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 ;. pBuf = &((
fae00 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 char*)pBuf)[wrot
fae10 65 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72 e];. }. if( !r
fae20 63 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72 c || amt>(int)wr
fae30 6f 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 ote ){. pFile
fae40 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 ->lastErrno = Ge
fae50 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fae60 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fae70 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 FULL;. }. retu
fae80 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
fae90 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 ./*.** Truncate
faea0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 an open file to
faeb0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 a specified size
faec0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
faed0 69 6e 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 inTruncate(sqlit
faee0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c e3_file *id, sql
faef0 69 74 65 33 5f 69 6e 74 36 34 20 6e 42 79 74 65 ite3_int64 nByte
faf00 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 ){. LONG upperB
faf10 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6e 42 its = (LONG)((nB
faf20 79 74 65 3e 3e 33 32 29 20 26 20 30 78 37 66 66 yte>>32) & 0x7ff
faf30 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c fffff);. LONG l
faf40 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 owerBits = (LONG
faf50 29 28 6e 42 79 74 65 20 26 20 30 78 66 66 66 66 )(nByte & 0xffff
faf60 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 ffff);. DWORD r
faf70 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 c;. winFile *pF
faf80 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
faf90 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f id;. DWORD erro
fafa0 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 r;.. assert( id
fafb0 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 !=0 );. OSTRACE
fafc0 33 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 3("TRUNCATE %d %
fafd0 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 lld\n", pFile->h
fafe0 2c 20 6e 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 , nByte);. Simu
faff0 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
fb000 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
fb010 54 52 55 4e 43 41 54 45 29 3b 0a 20 20 72 63 20 TRUNCATE);. rc
fb020 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 = SetFilePointer
fb030 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 (pFile->h, lower
fb040 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 Bits, &upperBits
fb050 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 , FILE_BEGIN);.
fb060 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 if( rc==INVALID
fb070 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
fb080 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c R && (error=GetL
fb090 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f astError())!=NO_
fb0a0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 ERROR ){. pFi
fb0b0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
fb0c0 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 error;. retur
fb0d0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
fb0e0 52 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 2f RUNCATE;. }. /
fb0f0 2a 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 20 77 * SetEndOfFile w
fb100 69 6c 6c 20 66 61 69 6c 20 69 66 20 6e 42 79 74 ill fail if nByt
fb110 65 20 69 73 20 6e 65 67 61 74 69 76 65 20 2a 2f e is negative */
fb120 0a 20 20 69 66 28 20 21 53 65 74 45 6e 64 4f 66 . if( !SetEndOf
fb130 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 File(pFile->h) )
fb140 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
fb150 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
fb160 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 Error();. ret
fb170 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fb180 5f 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 _TRUNCATE;. }.
fb190 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
fb1a0 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c K;.}..#ifdef SQL
fb1b0 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 ITE_TEST./*.** C
fb1c0 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 ount the number
fb1d0 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 of fullsyncs and
fb1e0 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 normal syncs.
fb1f0 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
fb200 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e test.** that syn
fb210 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 cs and fullsyncs
fb220 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 are occuring at
fb230 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 the right times
fb240 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
fb250 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 int sqlite3_sync
fb260 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 _count = 0;.SQLI
fb270 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
fb280 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
fb290 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f t = 0;.#endif../
fb2a0 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 *.** Make sure a
fb2b0 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 ll writes to a p
fb2c0 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 articular file a
fb2d0 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 re committed to
fb2e0 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 disk..*/.static
fb2f0 69 6e 74 20 77 69 6e 53 79 6e 63 28 73 71 6c 69 int winSync(sqli
fb300 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
fb310 74 20 66 6c 61 67 73 29 7b 0a 23 69 66 6e 64 65 t flags){.#ifnde
fb320 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 f SQLITE_NO_SYNC
fb330 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c . winFile *pFil
fb340 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 e = (winFile*)id
fb350 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 ;.. assert( id!
fb360 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 =0 );. OSTRACE3
fb370 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 ("SYNC %d lock=%
fb380 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
fb390 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 pFile->locktype)
fb3a0 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 ;.#else. UNUSED
fb3b0 5f 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a _PARAMETER(id);.
fb3c0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
fb3d0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55 QLITE_TEST. UNU
fb3e0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c SED_PARAMETER(fl
fb3f0 61 67 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 ags);.#else. if
fb400 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
fb410 5f 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 _SYNC_FULL ){.
fb420 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 sqlite3_fullsy
fb430 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a nc_count++;. }.
fb440 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 sqlite3_sync_c
fb450 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
fb460 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c /* If we compil
fb470 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 ed with the SQLI
fb480 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c TE_NO_SYNC flag,
fb490 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 then syncing is
fb4a0 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 a. ** no-op.
fb4b0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
fb4c0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 20 20 72 65 74 _NO_SYNC. ret
fb4d0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 urn SQLITE_OK;.#
fb4e0 65 6c 73 65 0a 20 20 69 66 28 20 46 6c 75 73 68 else. if( Flush
fb4f0 46 69 6c 65 42 75 66 66 65 72 73 28 70 46 69 6c FileBuffers(pFil
fb500 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 72 65 74 e->h) ){. ret
fb510 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
fb520 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c }else{. pFil
fb530 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
fb540 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fb550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fb560 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 23 65 6e 64 _IOERR;. }.#end
fb570 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 if.}../*.** Dete
fb580 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e rmine the curren
fb590 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 t size of a file
fb5a0 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 in bytes.*/.sta
fb5b0 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 53 tic int winFileS
fb5c0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
fb5d0 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e *id, sqlite3_in
fb5e0 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 t64 *pSize){. D
fb5f0 57 4f 52 44 20 75 70 70 65 72 42 69 74 73 3b 0a WORD upperBits;.
fb600 20 20 44 57 4f 52 44 20 6c 6f 77 65 72 42 69 74 DWORD lowerBit
fb610 73 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 s;. winFile *pF
fb620 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
fb630 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f id;. DWORD erro
fb640 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 r;.. assert( id
fb650 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 !=0 );. Simulat
fb660 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 eIOError(return
fb670 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 SQLITE_IOERR_FST
fb680 41 54 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 AT);. lowerBits
fb690 20 3d 20 47 65 74 46 69 6c 65 53 69 7a 65 28 70 = GetFileSize(p
fb6a0 46 69 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 File->h, &upperB
fb6b0 69 74 73 29 3b 0a 20 20 69 66 28 20 20 20 28 6c its);. if( (l
fb6c0 6f 77 65 72 42 69 74 73 20 3d 3d 20 49 4e 56 41 owerBits == INVA
fb6d0 4c 49 44 5f 46 49 4c 45 5f 53 49 5a 45 29 0a 20 LID_FILE_SIZE).
fb6e0 20 20 20 20 26 26 20 28 28 65 72 72 6f 72 20 3d && ((error =
fb6f0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 GetLastError())
fb700 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 29 0a != NO_ERROR) ).
fb710 20 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c {. pFile->l
fb720 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 astErrno = error
fb730 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
fb740 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b ITE_IOERR_FSTAT;
fb750 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 . }. *pSize =
fb760 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 (((sqlite3_int64
fb770 29 75 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 )upperBits)<<32)
fb780 20 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 + lowerBits;.
fb790 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
fb7a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 ;.}../*.** LOCKF
fb7b0 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 ILE_FAIL_IMMEDIA
fb7c0 54 45 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 TELY is undefine
fb7d0 64 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 d on some Window
fb7e0 73 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 s systems..*/.#i
fb7f0 66 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 fndef LOCKFILE_F
fb800 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a AIL_IMMEDIATELY.
fb810 23 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c # define LOCKFIL
fb820 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 E_FAIL_IMMEDIATE
fb830 4c 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a LY 1.#endif../*.
fb840 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 ** Acquire a rea
fb850 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 der lock..** Dif
fb860 66 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 ferent API routi
fb870 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 nes are called d
fb880 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 epending on whet
fb890 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a her or not this.
fb8a0 2a 2a 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 ** is Win95 or W
fb8b0 69 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 inNT..*/.static
fb8c0 69 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 int getReadLock(
fb8d0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b winFile *pFile){
fb8e0 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 . int res;. if
fb8f0 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
fb900 4f 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b OVERLAPPED ovlp;
fb910 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 . ovlp.Offset
fb920 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
fb930 0a 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 . ovlp.Offset
fb940 48 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 High = 0;. ov
fb950 6c 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 lp.hEvent = 0;.
fb960 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c res = LockFil
fb970 65 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f eEx(pFile->h, LO
fb980 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 CKFILE_FAIL_IMME
fb990 44 49 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 DIATELY,.
fb9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0,
fb9b0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c SHARED_SIZE, 0,
fb9c0 20 26 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 &ovlp);./* isNT
fb9d0 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
fb9e0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
fb9f0 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
fba00 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
fba10 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
fba20 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
fba30 65 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 e{. int lk;.
fba40 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f sqlite3_rando
fba50 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 mness(sizeof(lk)
fba60 2c 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c , &lk);. pFil
fba70 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e->sharedLockByt
fba80 65 20 3d 20 28 73 68 6f 72 74 29 28 28 6c 6b 20 e = (short)((lk
fba90 26 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 & 0x7fffffff)%(S
fbaa0 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 29 HARED_SIZE - 1))
fbab0 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b ;. res = Lock
fbac0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 File(pFile->h, S
fbad0 48 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c HARED_FIRST+pFil
fbae0 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e->sharedLockByt
fbaf0 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e e, 0, 1, 0);.#en
fbb00 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 dif. }. if( re
fbb10 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 s == 0 ){. pF
fbb20 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
fbb30 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b GetLastError();
fbb40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 . }. return re
fbb50 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f s;.}../*.** Undo
fbb60 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 a readlock.*/.s
fbb70 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b tatic int unlock
fbb80 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 ReadLock(winFile
fbb90 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 *pFile){. int
fbba0 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 res;. if( isNT(
fbbb0 29 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 ) ){. res = U
fbbc0 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d nlockFile(pFile-
fbbd0 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 >h, SHARED_FIRST
fbbe0 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 , 0, SHARED_SIZE
fbbf0 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 , 0);./* isNT()
fbc00 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f is 1 if SQLITE_O
fbc10 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 S_WINCE==1, so t
fbc20 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 his else is neve
fbc30 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a r executed. .*/.
fbc40 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
fbc50 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
fbc60 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b res = Unlock
fbc70 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 File(pFile->h, S
fbc80 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 70 46 HARED_FIRST + pF
fbc90 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 ile->sharedLockB
fbca0 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 yte, 0, 1, 0);.#
fbcb0 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 endif. }. if(
fbcc0 72 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 res == 0 ){.
fbcd0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
fbce0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fbcf0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
fbd00 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f res;.}../*.** Lo
fbd10 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
fbd20 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
fbd30 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
fbd40 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
fbd50 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
fbd60 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
fbd70 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
fbd80 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
fbd90 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
fbda0 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
fbdb0 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
fbdc0 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
fbdd0 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
fbde0 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
fbdf0 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
fbe00 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
fbe10 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
fbe20 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
fbe30 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
fbe40 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
fbe50 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
fbe60 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
fbe70 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
fbe80 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
fbe90 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
fbea0 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
fbeb0 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
fbec0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
fbed0 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
fbee0 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
fbef0 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
fbf00 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
fbf10 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
fbf20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
fbf30 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
fbf40 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
fbf50 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
fbf60 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
fbf70 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
fbf80 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
fbf90 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
fbfa0 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
fbfb0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
fbfc0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
fbfd0 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 l only increase
fbfe0 61 20 6c 6f 63 6b 2e 20 20 54 68 65 20 77 69 6e a lock. The win
fbff0 55 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 Unlock() routine
fc000 0a 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c .** erases all l
fc010 6f 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 ocks at once and
fc020 20 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 returns us imme
fc030 64 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 diately to locki
fc040 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 ng level 0..** I
fc050 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
fc060 65 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c e to lower the l
fc070 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 ocking level one
fc080 20 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e step at a time.
fc090 20 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f You.** must go
fc0a0 20 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 straight to loc
fc0b0 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f king level 0..*/
fc0c0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c .static int winL
fc0d0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
fc0e0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
fc0f0 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 pe){. int rc =
fc100 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a SQLITE_OK; /*
fc110 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f Return code fro
fc120 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f m subroutines */
fc130 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 . int res = 1;
fc140 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
fc150 75 6c 74 20 6f 66 20 61 20 77 69 6e 64 6f 77 73 ult of a windows
fc160 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 lock call */.
fc170 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b int newLocktype;
fc180 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 /* Set pF
fc190 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f ile->locktype to
fc1a0 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f this value befo
fc1b0 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 re exiting */.
fc1c0 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f int gotPendingLo
fc1d0 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 ck = 0;/* True i
fc1e0 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20 f we acquired a
fc1f0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 PENDING lock thi
fc200 73 20 74 69 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 s time */. winF
fc210 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
fc220 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f nFile*)id;. DWO
fc230 52 44 20 65 72 72 6f 72 20 3d 20 4e 4f 5f 45 52 RD error = NO_ER
fc240 52 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ROR;.. assert(
fc250 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 id!=0 );. OSTRA
fc260 43 45 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 CE5("LOCK %d %d
fc270 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 was %d(%d)\n",.
fc280 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
fc290 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 h, locktype, pFi
fc2a0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 le->locktype, pF
fc2b0 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 ile->sharedLockB
fc2c0 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 yte);.. /* If t
fc2d0 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
fc2e0 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 a lock of this t
fc2f0 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 ype or more rest
fc300 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 rictive on the.
fc310 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e ** OsFile, do n
fc320 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
fc330 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 e the end_lock:
fc340 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 exit path, as.
fc350 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 ** sqlite3OsEnte
fc360 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 rMutex() hasn't
fc370 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e been called yet.
fc380 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
fc390 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 e->locktype>=loc
fc3a0 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 ktype ){. ret
fc3b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
fc3c0 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 }.. /* Make su
fc3d0 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 re the locking s
fc3e0 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 equence is corre
fc3f0 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ct. */. assert
fc400 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
fc410 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f e!=NO_LOCK || lo
fc420 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
fc430 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
fc440 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 locktype!=PENDI
fc450 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 NG_LOCK );. ass
fc460 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 ert( locktype!=R
fc470 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 ESERVED_LOCK ||
fc480 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
fc490 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
fc4a0 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 . /* Lock the P
fc4b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 ENDING_LOCK byte
fc4c0 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 if we need to a
fc4d0 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 cquire a PENDING
fc4e0 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 lock or. ** a
fc4f0 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 SHARED lock. If
fc500 20 77 65 20 61 72 65 20 61 63 71 75 69 72 69 6e we are acquirin
fc510 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c g a SHARED lock,
fc520 20 74 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e the acquisition
fc530 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e of. ** the PEN
fc540 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 DING_LOCK byte i
fc550 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a s temporary.. *
fc560 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 /. newLocktype
fc570 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 = pFile->locktyp
fc580 65 3b 0a 20 20 69 66 28 20 20 20 28 70 46 69 6c e;. if( (pFil
fc590 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f e->locktype==NO_
fc5a0 4c 4f 43 4b 29 0a 20 20 20 20 20 7c 7c 20 28 20 LOCK). || (
fc5b0 20 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 (locktype==EXC
fc5c0 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20 20 20 LUSIVE_LOCK).
fc5d0 20 20 20 20 20 20 26 26 20 28 70 46 69 6c 65 2d && (pFile-
fc5e0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 >locktype==RESER
fc5f0 56 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a VED_LOCK)). ){.
fc600 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b int cnt = 3;
fc610 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6e 74 2d . while( cnt-
fc620 2d 3e 30 20 26 26 20 28 72 65 73 20 3d 20 4c 6f ->0 && (res = Lo
fc630 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
fc640 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 PENDING_BYTE, 0
fc650 2c 20 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20 , 1, 0))==0 ){.
fc660 20 20 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69 /* Try 3 ti
fc670 6d 65 73 20 74 6f 20 67 65 74 20 74 68 65 20 70 mes to get the p
fc680 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 ending lock. Th
fc690 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d e pending lock m
fc6a0 69 67 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a ight be. **
fc6b0 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 held by another
fc6c0 20 72 65 61 64 65 72 20 70 72 6f 63 65 73 73 20 reader process
fc6d0 77 68 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 who will release
fc6e0 20 69 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e it momentarily.
fc6f0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
fc700 4f 53 54 52 41 43 45 32 28 22 63 6f 75 6c 64 20 OSTRACE2("could
fc710 6e 6f 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e not get a PENDIN
fc720 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e G lock. cnt=%d\n
fc730 22 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 53 ", cnt);. S
fc740 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 leep(1);. }.
fc750 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 gotPendingLoc
fc760 6b 20 3d 20 72 65 73 3b 0a 20 20 20 20 69 66 28 k = res;. if(
fc770 20 21 72 65 73 20 29 7b 0a 20 20 20 20 20 20 65 !res ){. e
fc780 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
fc790 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ror();. }. }
fc7a0 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 .. /* Acquire a
fc7b0 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a shared lock. *
fc7c0 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 /. if( locktype
fc7d0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 ==SHARED_LOCK &&
fc7e0 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 res ){. asse
fc7f0 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
fc800 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a ype==NO_LOCK );.
fc810 20 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 res = getRea
fc820 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 dLock(pFile);.
fc830 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
fc840 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
fc850 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
fc860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 }else{. e
fc870 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
fc880 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ror();. }. }
fc890 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 .. /* Acquire a
fc8a0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 RESERVED lock.
fc8b0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
fc8c0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe==RESERVED_LOC
fc8d0 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 K && res ){.
fc8e0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
fc8f0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
fc900 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 LOCK );. res
fc910 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
fc920 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 ->h, RESERVED_BY
fc930 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 TE, 0, 1, 0);.
fc940 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
fc950 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d newLocktype =
fc960 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a RESERVED_LOCK;.
fc970 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
fc980 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 error = GetLast
fc990 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 Error();. }.
fc9a0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 }.. /* Acquire
fc9b0 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a a PENDING lock.
fc9c0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
fc9d0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
fc9e0 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 OCK && res ){.
fc9f0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 newLocktype =
fca00 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 PENDING_LOCK;.
fca10 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b gotPendingLock
fca20 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
fca30 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 Acquire an EXCLU
fca40 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 SIVE lock. */.
fca50 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
fca60 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 XCLUSIVE_LOCK &&
fca70 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 res ){. asse
fca80 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
fca90 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype>=SHARED_LOCK
fcaa0 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e );. res = un
fcab0 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 lockReadLock(pFi
fcac0 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 le);. OSTRACE
fcad0 32 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 2("unreadlock =
fcae0 25 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 20 20 %d\n", res);.
fcaf0 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 res = LockFile(
fcb00 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fcb10 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 _FIRST, 0, SHARE
fcb20 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 D_SIZE, 0);.
fcb30 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 if( res ){.
fcb40 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 newLocktype = E
fcb50 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 XCLUSIVE_LOCK;.
fcb60 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
fcb70 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 error = GetLastE
fcb80 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 4f 53 rror();. OS
fcb90 54 52 41 43 45 32 28 22 65 72 72 6f 72 2d 63 6f TRACE2("error-co
fcba0 64 65 20 3d 20 25 64 5c 6e 22 2c 20 65 72 72 6f de = %d\n", erro
fcbb0 72 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 r);. getRea
fcbc0 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 dLock(pFile);.
fcbd0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
fcbe0 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 we are holding
fcbf0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 a PENDING lock t
fcc00 68 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65 20 hat ought to be
fcc10 72 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 released, then.
fcc20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20 6e ** release it n
fcc30 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 ow.. */. if( g
fcc40 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 otPendingLock &&
fcc50 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
fcc60 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e D_LOCK ){. Un
fcc70 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
fcc80 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c h, PENDING_BYTE,
fcc90 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 0, 1, 0);. }..
fcca0 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
fccb0 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 state of the loc
fccc0 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 k has held in th
fccd0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
fcce0 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 r then. ** retu
fccf0 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 rn the appropria
fcd00 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a te result code..
fcd10 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 29 */. if( res )
fcd20 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
fcd30 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
fcd40 20 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 OSTRACE4("LOC
fcd50 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 69 K FAILED %d tryi
fcd60 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 6f ng for %d but go
fcd70 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e t %d\n", pFile->
fcd80 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f h,. lo
fcd90 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 cktype, newLockt
fcda0 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d ype);. pFile-
fcdb0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
fcdc0 6f 72 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c or;. rc = SQL
fcdd0 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 ITE_BUSY;. }.
fcde0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
fcdf0 3d 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 = (u8)newLocktyp
fce00 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a e;. return rc;.
fce10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
fce20 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
fce30 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
fce40 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
fce50 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
fce60 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
fce70 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
fce80 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
fce90 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 ock is held, ret
fcea0 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c urn.** non-zero,
fceb0 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e otherwise zero.
fcec0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fced0 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c inCheckReservedL
fcee0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
fcef0 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f *id, int *pResO
fcf00 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ut){. int rc;.
fcf10 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fcf20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fcf30 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
fcf40 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d );. if( pFile-
fcf50 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 >locktype>=RESER
fcf60 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 VED_LOCK ){.
fcf70 72 63 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 rc = 1;. OSTR
fcf80 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f ACE3("TEST WR-LO
fcf90 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 CK %d %d (local)
fcfa0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
fcfb0 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 c);. }else{.
fcfc0 20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 rc = LockFile(p
fcfd0 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 File->h, RESERVE
fcfe0 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 D_BYTE, 0, 1, 0)
fcff0 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a ;. if( rc ){.
fd000 20 20 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 UnlockFile
fd010 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 (pFile->h, RESER
fd020 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 VED_BYTE, 0, 1,
fd030 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 0);. }. rc
fd040 20 3d 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 = !rc;. OSTR
fd050 41 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f ACE3("TEST WR-LO
fd060 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 CK %d %d (remote
fd070 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
fd080 72 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 rc);. }. *pRes
fd090 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 Out = rc;. retu
fd0a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
fd0b0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
fd0c0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
fd0d0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
fd0e0 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 r id to locktype
fd0f0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
fd100 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
fd110 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
fd120 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
fd130 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
fd140 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
fd150 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
fd160 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
fd170 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
fd180 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
fd190 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
fd1a0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 no-op..**.** It
fd1b0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
fd1c0 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e for this routin
fd1d0 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 e to fail if the
fd1e0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
fd1f0 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 .** is NO_LOCK.
fd200 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 If the second a
fd210 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 rgument is SHARE
fd220 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 D_LOCK then this
fd230 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 routine.** migh
fd240 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f t return SQLITE_
fd250 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 IOERR;.*/.static
fd260 20 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 int winUnlock(s
fd270 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fd280 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a int locktype){.
fd290 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 int type;. wi
fd2a0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
fd2b0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 winFile*)id;. i
fd2c0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
fd2d0 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 K;. assert( pFi
fd2e0 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 le!=0 );. asser
fd2f0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 t( locktype<=SHA
fd300 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 RED_LOCK );. OS
fd310 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25 TRACE5("UNLOCK %
fd320 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25 d to %d was %d(%
fd330 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c d)\n", pFile->h,
fd340 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 locktype,.
fd350 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b pFile->lock
fd360 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 type, pFile->sha
fd370 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20 redLockByte);.
fd380 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f type = pFile->lo
fd390 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 cktype;. if( ty
fd3a0 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe>=EXCLUSIVE_LO
fd3b0 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b CK ){. Unlock
fd3c0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 File(pFile->h, S
fd3d0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 HARED_FIRST, 0,
fd3e0 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b SHARED_SIZE, 0);
fd3f0 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 . if( locktyp
fd400 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
fd410 26 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 & !getReadLock(p
fd420 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 2f File) ){. /
fd430 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 * This should ne
fd440 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20 ver happen. We
fd450 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 should always be
fd460 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a able to. *
fd470 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20 * reacquire the
fd480 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 read lock */.
fd490 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
fd4a0 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 OERR_UNLOCK;.
fd4b0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 }. }. if( typ
fd4c0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
fd4d0 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 ){. UnlockFi
fd4e0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 le(pFile->h, RES
fd4f0 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 ERVED_BYTE, 0, 1
fd500 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 , 0);. }. if(
fd510 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
fd520 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 K && type>=SHARE
fd530 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e D_LOCK ){. un
fd540 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 lockReadLock(pFi
fd550 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 le);. }. if( t
fd560 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype>=PENDING_LOC
fd570 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 K ){. UnlockF
fd580 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 ile(pFile->h, PE
fd590 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 NDING_BYTE, 0, 1
fd5a0 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c , 0);. }. pFil
fd5b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 e->locktype = (u
fd5c0 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 8)locktype;. re
fd5d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
fd5e0 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 * Control and qu
fd5f0 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 ery of the open
fd600 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a file handle..*/.
fd610 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 static int winFi
fd620 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
fd630 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fd640 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
fd650 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
fd660 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
fd670 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 _FCNTL_LOCKSTATE
fd680 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a : {. *(int*
fd690 29 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 6c )pArg = ((winFil
fd6a0 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 e*)id)->locktype
fd6b0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
fd6c0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
fd6d0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
fd6e0 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 LAST_ERRNO: {.
fd6f0 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
fd700 3d 20 28 69 6e 74 29 28 28 77 69 6e 46 69 6c 65 = (int)((winFile
fd710 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
fd720 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
fd730 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
fd740 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
fd750 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a ITE_ERROR;.}../*
fd760 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
fd770 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 ector size in by
fd780 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 tes of the under
fd790 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 lying block devi
fd7a0 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 ce for.** the sp
fd7b0 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 ecified file. Th
fd7c0 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 is is almost alw
fd7d0 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 ays 512 bytes, b
fd7e0 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 ut may be.** lar
fd7f0 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 ger for some dev
fd800 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 ices..**.** SQLi
fd810 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 te code assumes
fd820 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 this function ca
fd830 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c nnot fail. It al
fd840 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a so assumes that.
fd850 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 ** if two files
fd860 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 are created in t
fd870 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 he same file-sys
fd880 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 tem directory (i
fd890 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 .e..** a databas
fd8a0 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 e and its journa
fd8b0 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 l file) that the
fd8c0 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c sector size wil
fd8d0 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 l be the.** same
fd8e0 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 for both..*/.st
fd8f0 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74 atic int winSect
fd900 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 orSize(sqlite3_f
fd910 69 6c 65 20 2a 69 64 29 7b 0a 20 20 61 73 73 65 ile *id){. asse
fd920 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 72 rt( id!=0 );. r
fd930 65 74 75 72 6e 20 28 69 6e 74 29 28 28 28 77 69 eturn (int)(((wi
fd940 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 73 65 63 74 nFile*)id)->sect
fd950 6f 72 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a orSize);.}../*.*
fd960 2a 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f * Return a vecto
fd970 72 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 r of device char
fd980 61 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a acteristics..*/.
fd990 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 static int winDe
fd9a0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
fd9b0 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ics(sqlite3_file
fd9c0 20 2a 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f *id){. UNUSED_
fd9d0 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 20 PARAMETER(id);.
fd9e0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
fd9f0 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 .** This vector
fda00 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 defines all the
fda10 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e methods that can
fda20 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a operate on an.*
fda30 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 * sqlite3_file f
fda40 6f 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 or win32..*/.sta
fda50 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
fda60 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 3_io_methods win
fda70 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 IoMethod = {. 1
fda80 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
fda90 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
fdaa0 73 69 6f 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f sion */. winClo
fdab0 73 65 2c 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 se,. winRead,.
fdac0 20 77 69 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e winWrite,. win
fdad0 54 72 75 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 Truncate,. winS
fdae0 79 6e 63 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 ync,. winFileSi
fdaf0 7a 65 2c 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 ze,. winLock,.
fdb00 20 77 69 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 winUnlock,. wi
fdb10 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f nCheckReservedLo
fdb20 63 6b 2c 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e ck,. winFileCon
fdb30 74 72 6f 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f trol,. winSecto
fdb40 72 53 69 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 rSize,. winDevi
fdb50 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
fdb60 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a s.};../*********
fdb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdbb0 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 **.** Here ends
fdbc0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 the I/O methods
fdbd0 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 that form the sq
fdbe0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
fdbf0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 object..**.** T
fdc00 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 he next block of
fdc10 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 code implements
fdc20 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 the VFS methods
fdc30 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**************
fdc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fdc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
fdc80 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
fdc90 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 UTF-8 filename
fdca0 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f into whatever fo
fdcb0 72 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e rm the underlyin
fdcc0 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 g.** operating s
fdcd0 79 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 ystem wants file
fdce0 6e 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63 65 names in. Space
fdcf0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 to hold the res
fdd00 75 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e ult.** is obtain
fdd10 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 ed from malloc a
fdd20 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 nd must be freed
fdd30 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a by the calling.
fdd40 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a ** function..*/.
fdd50 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e static void *con
fdd60 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
fdd70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
fdd80 6c 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 lename){. void
fdd90 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b *zConverted = 0;
fdda0 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
fddb0 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 . zConverted
fddc0 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 = utf8ToUnicode(
fddd0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 zFilename);./* i
fdde0 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 sNT() is 1 if SQ
fddf0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 LITE_OS_WINCE==1
fde00 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 , so this else i
fde10 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 s never executed
fde20 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 . .*/.#if SQLITE
fde30 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
fde40 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 else{. zConve
fde50 72 74 65 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 rted = utf8ToMbc
fde60 73 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 s(zFilename);.#e
fde70 6e 64 69 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 ndif. }. /* ca
fde80 6c 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 ller will handle
fde90 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a out of memory *
fdea0 2f 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 /. return zConv
fdeb0 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 erted;.}../*.**
fdec0 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 Create a tempora
fded0 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 ry file name in
fdee0 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 zBuf. zBuf must
fdef0 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 be big enough t
fdf00 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 o.** hold at pVf
fdf10 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 s->mxPathname ch
fdf20 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 aracters..*/.sta
fdf30 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e tic int getTempn
fdf40 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 ame(int nBuf, ch
fdf50 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 ar *zBuf){. sta
fdf60 74 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b tic char zChars[
fdf70 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 ] =. "abcdefg
fdf80 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 hijklmnopqrstuvw
fdf90 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 xyz". "ABCDEF
fdfa0 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 GHIJKLMNOPQRSTUV
fdfb0 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 WXYZ". "01234
fdfc0 35 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 56789";. size_t
fdfd0 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 i, j;. char zT
fdfe0 65 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 empPath[MAX_PATH
fdff0 2b 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 +1];. if( sqlit
fe000 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 e3_temp_director
fe010 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 y ){. sqlite3
fe020 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 _snprintf(MAX_PA
fe030 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 TH-30, zTempPath
fe040 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f , "%s", sqlite3_
fe050 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b temp_directory);
fe060 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e . }else if( isN
fe070 54 28 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 T() ){. char
fe080 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 *zMulti;. WCH
fe090 41 52 20 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 AR zWidePath[MAX
fe0a0 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 _PATH];. GetT
fe0b0 65 6d 70 50 61 74 68 57 28 4d 41 58 5f 50 41 54 empPathW(MAX_PAT
fe0c0 48 2d 33 30 2c 20 7a 57 69 64 65 50 61 74 68 29 H-30, zWidePath)
fe0d0 3b 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 ;. zMulti = u
fe0e0 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 nicodeToUtf8(zWi
fe0f0 64 65 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 dePath);. if(
fe100 20 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 zMulti ){.
fe110 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
fe120 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a f(MAX_PATH-30, z
fe130 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 TempPath, "%s",
fe140 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 zMulti);. f
fe150 72 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 ree(zMulti);.
fe160 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
fe170 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
fe180 4d 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 M;. }./* isNT
fe190 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 () is 1 if SQLIT
fe1a0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 E_OS_WINCE==1, s
fe1b0 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e o this else is n
fe1c0 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a ever executed. .
fe1d0 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 ** Since the ASC
fe1e0 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 II version of th
fe1f0 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 ese Windows API
fe200 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 do not exist for
fe210 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 WINCE,.** it's
fe220 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 important to not
fe230 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 reference them
fe240 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 for WINCE builds
fe250 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
fe260 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
fe270 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a lse{. char *z
fe280 55 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a Utf8;. char z
fe290 4d 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 MbcsPath[MAX_PAT
fe2a0 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 H];. GetTempP
fe2b0 61 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 athA(MAX_PATH-30
fe2c0 2c 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 , zMbcsPath);.
fe2d0 20 20 7a 55 74 66 38 20 3d 20 73 71 6c 69 74 65 zUtf8 = sqlite
fe2e0 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 3_win32_mbcs_to_
fe2f0 75 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b utf8(zMbcsPath);
fe300 0a 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29 . if( zUtf8 )
fe310 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
fe320 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
fe330 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c H-30, zTempPath,
fe340 20 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 "%s", zUtf8);.
fe350 20 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29 free(zUtf8)
fe360 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
fe370 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fe380 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 _NOMEM;. }.#e
fe390 6e 64 69 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 ndif. }. for(i
fe3a0 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 =sqlite3Strlen30
fe3b0 28 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 (zTempPath); i>0
fe3c0 20 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d && zTempPath[i-
fe3d0 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 1]=='\\'; i--){}
fe3e0 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 . zTempPath[i]
fe3f0 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 = 0;. sqlite3_s
fe400 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c nprintf(nBuf-30,
fe410 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 zBuf,.
fe420 20 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 "%s\\"
fe430 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 SQLITE_TEMP_FILE
fe440 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 _PREFIX, zTempPa
fe450 74 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 th);. j = sqlit
fe460 65 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 e3Strlen30(zBuf)
fe470 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 ;. sqlite3_rand
fe480 6f 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75 66 omness(20, &zBuf
fe490 5b 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b [j]);. for(i=0;
fe4a0 20 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 i<20; i++, j++)
fe4b0 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 {. zBuf[j] =
fe4c0 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 (char)zChars[ ((
fe4d0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 unsigned char)zB
fe4e0 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a uf[j])%(sizeof(z
fe4f0 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d Chars)-1) ];. }
fe500 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a . zBuf[j] = 0;.
fe510 20 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d 50 OSTRACE2("TEMP
fe520 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 FILENAME: %s\n"
fe530 2c 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 , zBuf);. retur
fe540 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
fe550 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 ./*.** The retur
fe560 6e 20 76 61 6c 75 65 20 6f 66 20 67 65 74 4c 61 n value of getLa
fe570 73 74 45 72 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 stErrorMsg.** is
fe580 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 zero if the err
fe590 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 or message fits
fe5a0 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f in the buffer, o
fe5b0 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 r non-zero.** ot
fe5c0 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 herwise (if the
fe5d0 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e message was trun
fe5e0 63 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 cated)..*/.stati
fe5f0 63 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72 72 c int getLastErr
fe600 6f 72 4d 73 67 28 69 6e 74 20 6e 42 75 66 2c 20 orMsg(int nBuf,
fe610 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 2f char *zBuf){. /
fe620 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 * FormatMessage
fe630 72 65 74 75 72 6e 73 20 30 20 6f 6e 20 66 61 69 returns 0 on fai
fe640 6c 75 72 65 2e 20 20 4f 74 68 65 72 77 69 73 65 lure. Otherwise
fe650 20 69 74 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 it. ** returns
fe660 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 54 the number of T
fe670 43 48 41 52 73 20 77 72 69 74 74 65 6e 20 74 6f CHARs written to
fe680 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 2a 2a the output. **
fe690 20 62 75 66 66 65 72 2c 20 65 78 63 6c 75 64 69 buffer, excludi
fe6a0 6e 67 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69 ng the terminati
fe6b0 6e 67 20 6e 75 6c 6c 20 63 68 61 72 2e 0a 20 20 ng null char..
fe6c0 2a 2f 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 */. DWORD error
fe6d0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fe6e0 29 3b 0a 20 20 44 57 4f 52 44 20 64 77 4c 65 6e );. DWORD dwLen
fe6f0 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4f = 0;. char *zO
fe700 75 74 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69 ut = 0;.. if( i
fe710 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 sNT() ){. WCH
fe720 41 52 20 2a 7a 54 65 6d 70 57 69 64 65 20 3d 20 AR *zTempWide =
fe730 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20 NULL;. dwLen
fe740 3d 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 = FormatMessageW
fe750 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f (FORMAT_MESSAGE_
fe760 41 4c 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20 ALLOCATE_BUFFER
fe770 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 | FORMAT_MESSAGE
fe780 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 20 46 _FROM_SYSTEM | F
fe790 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 49 47 ORMAT_MESSAGE_IG
fe7a0 4e 4f 52 45 5f 49 4e 53 45 52 54 53 2c 0a 20 20 NORE_INSERTS,.
fe7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe7c0 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 NULL,.
fe7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe7e0 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c error,
fe7f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
fe800 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 0,.
fe810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe820 20 20 20 20 20 20 20 20 20 20 28 4c 50 57 53 54 (LPWST
fe830 52 29 20 26 7a 54 65 6d 70 57 69 64 65 2c 0a 20 R) &zTempWide,.
fe840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe850 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
fe860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe870 20 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20 0);.
fe880 69 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29 7b if( dwLen > 0 ){
fe890 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 . /* alloca
fe8a0 74 65 20 61 20 62 75 66 66 65 72 20 61 6e 64 20 te a buffer and
fe8b0 63 6f 6e 76 65 72 74 20 74 6f 20 55 54 46 38 20 convert to UTF8
fe8c0 2a 2f 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 */. zOut =
fe8d0 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 unicodeToUtf8(zT
fe8e0 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 20 20 20 empWide);.
fe8f0 2f 2a 20 66 72 65 65 20 74 68 65 20 73 79 73 74 /* free the syst
fe900 65 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 em buffer alloca
fe910 74 65 64 20 62 79 20 46 6f 72 6d 61 74 4d 65 73 ted by FormatMes
fe920 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 4c 6f sage */. Lo
fe930 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 57 69 64 calFree(zTempWid
fe940 65 29 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e e);. }./* isN
fe950 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 T() is 1 if SQLI
fe960 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 TE_OS_WINCE==1,
fe970 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 so this else is
fe980 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 never executed.
fe990 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 .** Since the AS
fe9a0 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 CII version of t
fe9b0 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 hese Windows API
fe9c0 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f do not exist fo
fe9d0 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 r WINCE,.** it's
fe9e0 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f important to no
fe9f0 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d t reference them
fea00 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 for WINCE build
fea10 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 s..*/.#if SQLITE
fea20 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
fea30 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
fea40 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 zTemp = NULL;.
fea50 20 20 64 77 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 dwLen = Format
fea60 4d 65 73 73 61 67 65 41 28 46 4f 52 4d 41 54 5f MessageA(FORMAT_
fea70 4d 45 53 53 41 47 45 5f 41 4c 4c 4f 43 41 54 45 MESSAGE_ALLOCATE
fea80 5f 42 55 46 46 45 52 20 7c 20 46 4f 52 4d 41 54 _BUFFER | FORMAT
fea90 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 _MESSAGE_FROM_SY
feaa0 53 54 45 4d 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 STEM | FORMAT_ME
feab0 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 SSAGE_IGNORE_INS
feac0 45 52 54 53 2c 0a 20 20 20 20 20 20 20 20 20 20 ERTS,.
fead0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feae0 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 NULL,.
feaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feb00 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 error,.
feb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feb20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 0,.
feb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feb40 20 20 28 4c 50 53 54 52 29 20 26 7a 54 65 6d 70 (LPSTR) &zTemp
feb50 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
feb60 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 0,.
feb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feb80 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 20 0);.
feb90 20 20 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30 if( dwLen > 0
feba0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c ){. /* all
febb0 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61 ocate a buffer a
febc0 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54 nd convert to UT
febd0 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a 4f 75 74 F8 */. zOut
febe0 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 = sqlite3_win32
febf0 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 _mbcs_to_utf8(zT
fec00 65 6d 70 29 3b 0a 20 20 20 20 20 20 2f 2a 20 66 emp);. /* f
fec10 72 65 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 ree the system b
fec20 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 uffer allocated
fec30 62 79 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 by FormatMessage
fec40 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 61 6c 46 */. LocalF
fec50 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 ree(zTemp);.
fec60 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 }.#endif. }. i
fec70 66 28 20 30 20 3d 3d 20 64 77 4c 65 6e 20 29 7b f( 0 == dwLen ){
fec80 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
fec90 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 rintf(nBuf, zBuf
feca0 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 , "OsError 0x%x
fecb0 28 25 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72 (%u)", error, er
fecc0 72 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ror);. }else{.
fecd0 20 20 20 2f 2a 20 63 6f 70 79 20 61 20 6d 61 78 /* copy a max
fece0 69 6d 75 6d 20 6f 66 20 6e 42 75 66 20 63 68 61 imum of nBuf cha
fecf0 72 73 20 74 6f 20 6f 75 74 70 75 74 20 62 75 66 rs to output buf
fed00 66 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 fer */. sqlit
fed10 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
fed20 2c 20 7a 42 75 66 2c 20 22 25 73 22 2c 20 7a 4f , zBuf, "%s", zO
fed30 75 74 29 3b 0a 20 20 20 20 2f 2a 20 66 72 65 65 ut);. /* free
fed40 20 74 68 65 20 55 54 46 38 20 62 75 66 66 65 72 the UTF8 buffer
fed50 20 2a 2f 0a 20 20 20 20 66 72 65 65 28 7a 4f 75 */. free(zOu
fed60 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
fed70 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 0;.}../*.** Ope
fed80 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 n a file..*/.sta
fed90 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 tic int winOpen(
feda0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
fedb0 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 pVfs, /*
fedc0 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f Not used */. co
fedd0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
fede0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
fedf0 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54 46 of the file (UTF
fee00 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 -8) */. sqlite3
fee10 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 _file *id,
fee20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
fee30 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 SQLite file hand
fee40 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 le here */. int
fee50 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
fee60 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d /* Open m
fee70 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 ode flags */. i
fee80 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 nt *pOutFlags
fee90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
feea0 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 20 us return flags
feeb0 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 68 */.){. HANDLE h
feec0 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73 69 ;. DWORD dwDesi
feed0 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57 4f redAccess;. DWO
feee0 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b 0a RD dwShareMode;.
feef0 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74 69 DWORD dwCreati
fef00 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 onDisposition;.
fef10 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41 6e DWORD dwFlagsAn
fef20 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30 3b dAttributes = 0;
fef30 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
fef40 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 65 6d INCE. int isTem
fef50 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 p = 0;.#endif.
fef60 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
fef70 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
fef80 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
fef90 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
fefa0 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 /* Filename i
fefb0 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a 2f n OS encoding */
fefc0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
fefd0 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 Utf8Name = zName
fefe0 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 ; /* Filename
feff0 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 in UTF-8 encodi
ff000 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d ng */. char zTm
ff010 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b 31 pname[MAX_PATH+1
ff020 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 ]; /* Buf
ff030 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65 61 fer used to crea
ff040 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65 te temp filename
ff050 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 */.. assert( i
ff060 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 d!=0 );. UNUSED
ff070 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
ff080 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 ;.. /* If the s
ff090 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
ff0a0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
ff0b0 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 is NULL, generat
ff0c0 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 e a . ** tempor
ff0d0 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f ary file name to
ff0e0 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 use . */. if(
ff0f0 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 !zUtf8Name ){.
ff100 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 int rc = getT
ff110 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 empname(MAX_PATH
ff120 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 +1, zTmpname);.
ff130 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
ff140 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 E_OK ){. re
ff150 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
ff160 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a zUtf8Name = z
ff170 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 Tmpname;. }..
ff180 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 /* Convert the f
ff190 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 ilename to the s
ff1a0 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 ystem encoding.
ff1b0 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 */. zConverted
ff1c0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
ff1d0 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 ename(zUtf8Name)
ff1e0 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 ;. if( zConvert
ff1f0 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ed==0 ){. ret
ff200 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
ff210 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 ;. }.. if( fla
ff220 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
ff230 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 _READWRITE ){.
ff240 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 dwDesiredAcces
ff250 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 s = GENERIC_READ
ff260 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 | GENERIC_WRITE
ff270 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 ;. }else{. d
ff280 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 3d wDesiredAccess =
ff290 20 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 GENERIC_READ;.
ff2a0 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f }. /* SQLITE_O
ff2b0 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 73 PEN_EXCLUSIVE is
ff2c0 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 used to make su
ff2d0 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66 69 re that a new fi
ff2e0 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 72 65 61 le is . ** crea
ff2f0 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65 73 ted. SQLite does
ff300 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69 6e n't use it to in
ff310 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69 76 dicate "exclusiv
ff320 65 20 61 63 63 65 73 73 22 20 0a 20 20 2a 2a 20 e access" . **
ff330 61 73 20 69 74 20 69 73 20 75 73 75 61 6c 6c 79 as it is usually
ff340 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20 2a understood.. *
ff350 2f 0a 20 20 61 73 73 65 72 74 28 21 28 66 6c 61 /. assert(!(fla
ff360 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
ff370 5f 45 58 43 4c 55 53 49 56 45 29 20 7c 7c 20 28 _EXCLUSIVE) || (
ff380 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
ff390 50 45 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20 20 PEN_CREATE));.
ff3a0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
ff3b0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
ff3c0 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 61 E ){. /* Crea
ff3d0 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65 2c 20 tes a new file,
ff3e0 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65 73 20 only if it does
ff3f0 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
ff400 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 t. */. /* If
ff410 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 2c the file exists,
ff420 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 it fails. */.
ff430 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 dwCreationDisp
ff440 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 41 54 45 osition = CREATE
ff450 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69 66 _NEW;. }else if
ff460 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
ff470 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a _OPEN_CREATE ){.
ff480 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69 73 /* Open exis
ff490 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63 72 ting file, or cr
ff4a0 65 61 74 65 20 69 66 20 69 74 20 64 6f 65 73 6e eate if it doesn
ff4b0 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 20 20 20 't exist */.
ff4c0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 dwCreationDispos
ff4d0 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c 57 ition = OPEN_ALW
ff4e0 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 AYS;. }else{.
ff4f0 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69 6c /* Opens a fil
ff500 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65 78 e, only if it ex
ff510 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77 43 ists. */. dwC
ff520 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 reationDispositi
ff530 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 on = OPEN_EXISTI
ff540 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61 72 NG;. }. dwShar
ff550 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 41 eMode = FILE_SHA
ff560 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f 53 RE_READ | FILE_S
ff570 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 20 69 66 HARE_WRITE;. if
ff580 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
ff590 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
ff5a0 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 OSE ){.#if SQLIT
ff5b0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 E_OS_WINCE. d
ff5c0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 wFlagsAndAttribu
ff5d0 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 tes = FILE_ATTRI
ff5e0 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 BUTE_HIDDEN;.
ff5f0 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c isTemp = 1;.#el
ff600 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e se. dwFlagsAn
ff610 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 dAttributes = FI
ff620 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45 4d LE_ATTRIBUTE_TEM
ff630 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20 20 PORARY.
ff640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff650 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 | FILE_ATT
ff660 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 RIBUTE_HIDDEN.
ff670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff680 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 | F
ff690 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f ILE_FLAG_DELETE_
ff6a0 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 ON_CLOSE;.#endif
ff6b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 . }else{. dw
ff6c0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 FlagsAndAttribut
ff6d0 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 es = FILE_ATTRIB
ff6e0 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a UTE_NORMAL;. }.
ff6f0 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72 6f /* Reports fro
ff700 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20 61 m the internet a
ff710 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d 61 re that performa
ff720 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20 20 nce is always.
ff730 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49 4c ** better if FIL
ff740 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 E_FLAG_RANDOM_AC
ff750 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20 54 CESS is used. T
ff760 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a icket #2699. */.
ff770 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
ff780 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 NCE. dwFlagsAnd
ff790 41 74 74 72 69 62 75 74 65 73 20 7c 3d 20 46 49 Attributes |= FI
ff7a0 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 LE_FLAG_RANDOM_A
ff7b0 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20 20 CCESS;.#endif.
ff7c0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
ff7d0 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 h = CreateFile
ff7e0 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 W((WCHAR*)zConve
ff7f0 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 rted,. dwD
ff800 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 esiredAccess,.
ff810 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 dwShareMode
ff820 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 ,. NULL,.
ff830 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e dwCreation
ff840 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 Disposition,.
ff850 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
ff860 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 tributes,.
ff870 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a 20 NULL. );./*
ff880 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
ff890 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
ff8a0 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
ff8b0 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
ff8c0 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
ff8d0 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
ff8e0 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
ff8f0 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
ff900 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
ff910 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
ff920 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
ff930 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
ff940 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
ff950 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
ff960 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d }else{. h =
ff970 20 43 72 65 61 74 65 46 69 6c 65 41 28 28 63 68 CreateFileA((ch
ff980 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a ar*)zConverted,.
ff990 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65 64 dwDesired
ff9a0 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64 Access,. d
ff9b0 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 wShareMode,.
ff9c0 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 NULL,.
ff9d0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 dwCreationDispos
ff9e0 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 ition,. dw
ff9f0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 FlagsAndAttribut
ffa00 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a es,. NULL.
ffa10 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 );.#endif.
ffa20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 4c }. if( h==INVAL
ffa30 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 ID_HANDLE_VALUE
ffa40 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e ){. free(zCon
ffa50 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66 28 verted);. if(
ffa60 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
ffa70 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 OPEN_READWRITE )
ffa80 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 77 {. return w
ffa90 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e 61 inOpen(pVfs, zNa
ffaa0 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 20 me, id, .
ffab0 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53 51 ((flags|SQ
ffac0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
ffad0 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 4e LY)&~SQLITE_OPEN
ffae0 5f 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f 75 _READWRITE), pOu
ffaf0 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 6c tFlags);. }el
ffb00 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
ffb10 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
ffb20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
ffb30 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 ( pOutFlags ){.
ffb40 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 if( flags & S
ffb50 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
ffb60 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a 70 RITE ){. *p
ffb70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 OutFlags = SQLIT
ffb80 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
ffb90 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
ffba0 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 *pOutFlags =
ffbb0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
ffbc0 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ONLY;. }. }.
ffbd0 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 memset(pFile,
ffbe0 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 0, sizeof(*pFile
ffbf0 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 ));. pFile->pMe
ffc00 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74 thod = &winIoMet
ffc10 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 hod;. pFile->h
ffc20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 = h;. pFile->la
ffc30 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 stErrno = NO_ERR
ffc40 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65 63 OR;. pFile->sec
ffc50 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 53 65 63 torSize = getSec
ffc60 74 6f 72 53 69 7a 65 28 70 56 66 73 2c 20 7a 55 torSize(pVfs, zU
ffc70 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53 51 tf8Name);.#if SQ
ffc80 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
ffc90 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 51 if( (flags & (SQ
ffca0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
ffcb0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ITE|SQLITE_OPEN_
ffcc0 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20 MAIN_DB)) ==.
ffcd0 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
ffce0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
ffcf0 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d TE|SQLITE_OPEN_M
ffd00 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26 AIN_DB). &
ffd10 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f & !winceCreateLo
ffd20 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29 ck(zName, pFile)
ffd30 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48 . ){. CloseH
ffd40 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72 andle(h);. fr
ffd50 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
ffd60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ffd70 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a E_CANTOPEN;. }.
ffd80 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a if( isTemp ){.
ffd90 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 pFile->zDele
ffda0 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e teOnClose = zCon
ffdb0 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a verted;. }else.
ffdc0 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66 #endif. {. f
ffdd0 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
ffde0 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 . }. OpenCount
ffdf0 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e er(+1);. return
ffe00 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
ffe10 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 *.** Delete the
ffe20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a named file..**.*
ffe30 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64 * Note that wind
ffe40 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c ows does not all
ffe50 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20 ow a file to be
ffe60 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20 deleted if some
ffe70 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 other.** process
ffe80 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53 has it open. S
ffe90 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 73 ometimes a virus
ffea0 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65 scanner or inde
ffeb0 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 xing program.**
ffec0 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72 will open a jour
ffed0 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79 nal file shortly
ffee0 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72 65 after it is cre
ffef0 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f ated in order to
fff00 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 do.** whatever
fff10 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20 it does. While
fff20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65 this other proce
fff30 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 ss is holding th
fff40 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 e.** file open,
fff50 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c we will be unabl
fff60 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 e to delete it.
fff70 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 To work around
fff80 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c this.** problem,
fff90 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69 we delay 100 mi
fffa0 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 lliseconds and t
fffb0 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61 ry to delete aga
fffc0 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 in. Up.** to MX
fffd0 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 _DELETION_ATTEMP
fffe0 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 Ts deletion atte
ffff0 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 66 mpts are run bef
10000 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70 ore giving.** up
10001 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 and returning a
10002 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 n error..*/.#def
10003 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f ine MX_DELETION_
10004 41 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74 69 ATTEMPTS 5.stati
10005 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 28 c int winDelete(
10006 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
10007 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f pVfs, /
10008 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 * Not used on wi
10009 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 n32 */. const c
1000a 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
1000b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1000c 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a file to delete *
1000d 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 /. int syncDir
1000e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1000f 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 /* Not used on w
10010 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 in32 */.){. int
10011 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 cnt = 0;. DWOR
10012 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 72 D rc;. DWORD er
10013 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 ror = 0;. void
10014 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f *zConverted = co
10015 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d nvertUtf8Filenam
10016 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
10017 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
10018 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 (pVfs);. UNUSED
10019 5f 50 41 52 41 4d 45 54 45 52 28 73 79 6e 63 44 _PARAMETER(syncD
1001a 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 ir);. if( zConv
1001b 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 erted==0 ){.
1001c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1001d 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c MEM;. }. Simul
1001e 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 ateIOError(retur
1001f 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 n SQLITE_IOERR_D
10020 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69 73 ELETE);. if( is
10021 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b 0a NT() ){. do{.
10022 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 DeleteFile
10023 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 W(zConverted);.
10024 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 }while( (
10025 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 ((rc = GetFileA
10026 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e 76 ttributesW(zConv
10027 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c erted)) != INVAL
10028 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
10029 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ES).
1002a 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 || ((error =
1002b 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 GetLastError())
1002c 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f == ERROR_ACCESS_
1002d 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 DENIED)).
1002e 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 && (++cnt <
1002f 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 MX_DELETION_ATTE
10030 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 MPTS).
10031 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c && (Sleep(100),
10032 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 1) );./* isNT()
10033 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
10034 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
10035 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
10036 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a er executed. .**
10037 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 Since the ASCII
10038 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 version of thes
10039 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f e Windows API do
1003a 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 not exist for W
1003b 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d INCE,.** it's im
1003c 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 portant to not r
1003d 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f eference them fo
1003e 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a r WINCE builds..
1003f 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
10040 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
10041 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 e{. do{.
10042 20 44 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 6f DeleteFileA(zCo
10043 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 nverted);. }w
10044 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 hile( ( ((rc
10045 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 = GetFileAttrib
10046 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 64 utesA(zConverted
10047 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 )) != INVALID_FI
10048 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 LE_ATTRIBUTES).
10049 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c ||
1004a 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 ((error = GetLa
1004b 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 stError()) == ER
1004c 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 ROR_ACCESS_DENIE
1004d 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 D)). &
1004e 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 & (++cnt < MX_DE
1004f 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 LETION_ATTEMPTS)
10050 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 . && (
10051 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 Sleep(100), 1) )
10052 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 ;.#endif. }. f
10053 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
10054 0a 20 20 4f 53 54 52 41 43 45 32 28 22 44 45 4c . OSTRACE2("DEL
10055 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a ETE \"%s\"\n", z
10056 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 74 Filename);. ret
10057 75 72 6e 20 28 20 20 20 28 72 63 20 3d 3d 20 49 urn ( (rc == I
10058 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 NVALID_FILE_ATTR
10059 49 42 55 54 45 53 29 20 0a 20 20 20 20 20 20 20 IBUTES) .
1005a 20 20 20 26 26 20 28 65 72 72 6f 72 20 3d 3d 20 && (error ==
1005b 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 ERROR_FILE_NOT_F
1005c 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 54 45 5f OUND)) ? SQLITE_
1005d 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
1005e 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a R_DELETE;.}../*.
1005f 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78 69 ** Check the exi
10060 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74 75 stance and statu
10061 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a s of a file..*/.
10062 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 41 63 static int winAc
10063 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f cess(. sqlite3_
10064 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
10065 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f /* Not used o
10066 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e n win32 */. con
10067 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
10068 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 me, /* Name
10069 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b of file to check
1006a 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
1006b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1006c 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 /* Type of test
1006d 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 to make on this
1006e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a file */. int *
1006f 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 pResOut
10070 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 /* OUT: Re
10071 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f sult */.){. DWO
10072 52 44 20 61 74 74 72 3b 0a 20 20 69 6e 74 20 72 RD attr;. int r
10073 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 7a c = 0;. void *z
10074 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 Converted = conv
10075 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
10076 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e zFilename);. UN
10077 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
10078 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e Vfs);. if( zCon
10079 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 verted==0 ){.
1007a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1007b 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OMEM;. }. if(
1007c 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 61 74 isNT() ){. at
1007d 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 tr = GetFileAttr
1007e 69 62 75 74 65 73 57 28 28 57 43 48 41 52 2a 29 ibutesW((WCHAR*)
1007f 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 zConverted);./*
10080 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
10081 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
10082 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
10083 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
10084 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
10085 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
10086 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
10087 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
10088 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
10089 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
1008a 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
1008b 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
1008c 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
1008d 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
1008e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74 74 }else{. att
1008f 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 r = GetFileAttri
10090 62 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a 43 butesA((char*)zC
10091 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 onverted);.#endi
10092 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f f. }. free(zCo
10093 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 77 69 74 nverted);. swit
10094 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 ch( flags ){.
10095 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 case SQLITE_ACC
10096 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 ESS_READ:. ca
10097 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
10098 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 _EXISTS:. r
10099 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c 49 c = attr!=INVALI
1009a 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 D_FILE_ATTRIBUTE
1009b 53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a S;. break;.
1009c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
1009d 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 ACCESS_READWRITE
1009e 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 61 74 :. rc = (at
1009f 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 49 42 tr & FILE_ATTRIB
100a0 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d 30 UTE_READONLY)==0
100a1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
100a2 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 default:.
100a3 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 6c assert(!"Inval
100a4 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e id flags argumen
100a5 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 t");. }. *pRes
100a6 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 Out = rc;. retu
100a7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
100a8 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 ../*.** Turn a r
100a9 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 elative pathname
100aa 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 into a full pat
100ab 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 68 hname. Write th
100ac 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 e full.** pathna
100ad 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 me into zOut[].
100ae 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20 zOut[] will be
100af 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d at least pVfs->m
100b0 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 xPathname.** byt
100b1 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 es in size..*/.s
100b2 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75 6c tatic int winFul
100b3 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c lPathname(. sql
100b4 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
100b5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
100b6 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a inter to vfs obj
100b7 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ect */. const c
100b8 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 har *zRelative,
100b9 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 /* Possib
100ba 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 ly relative inpu
100bb 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 t path */. int
100bc 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 nFull,
100bd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
100be 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 e of output buff
100bf 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 er in bytes */.
100c0 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 char *zFull
100c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
100c2 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 * Output buffer
100c3 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65 66 */.){. .#if def
100c4 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 ined(__CYGWIN__)
100c5 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
100c6 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63 79 TER(nFull);. cy
100c7 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c gwin_conv_to_ful
100c8 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 65 l_win32_path(zRe
100c9 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b 0a lative, zFull);.
100ca 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
100cb 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 OK;.#endif..#if
100cc 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
100cd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
100ce 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a 20 ER(nFull);. /*
100cf 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e WinCE has no con
100d0 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69 cept of a relati
100d1 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 ve pathname, or
100d2 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f so I am told. */
100d3 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
100d4 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 ntf(pVfs->mxPath
100d5 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 name, zFull, "%s
100d6 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 ", zRelative);.
100d7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
100d8 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 K;.#endif..#if !
100d9 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
100da 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 && !defined(__CY
100db 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 GWIN__). int nB
100dc 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f yte;. void *zCo
100dd 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 61 72 20 nverted;. char
100de 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44 5f *zOut;. UNUSED_
100df 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 PARAMETER(nFull)
100e0 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d ;. zConverted =
100e1 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 convertUtf8File
100e2 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 3b name(zRelative);
100e3 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b . if( isNT() ){
100e4 0a 20 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d . WCHAR *zTem
100e5 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 p;. nByte = G
100e6 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 28 etFullPathNameW(
100e7 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 (WCHAR*)zConvert
100e8 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 ed, 0, 0, 0) + 3
100e9 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 ;. zTemp = ma
100ea 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 lloc( nByte*size
100eb 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a of(zTemp[0]) );.
100ec 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 if( zTemp==0
100ed 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a ){. free(z
100ee 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
100ef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
100f0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 NOMEM;. }.
100f1 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 GetFullPathName
100f2 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 W((WCHAR*)zConve
100f3 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 rted, nByte, zTe
100f4 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 mp, 0);. free
100f5 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
100f6 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 zOut = unicode
100f7 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 ToUtf8(zTemp);.
100f8 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a free(zTemp);.
100f9 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
100fa 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
100fb 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
100fc 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
100fd 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
100fe 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
100ff 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
10100 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
10101 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
10102 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
10103 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
10104 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
10105 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
10106 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
10107 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
10108 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 char *zTemp;.
10109 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c nByte = GetFull
1010a 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 2a PathNameA((char*
1010b 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 )zConverted, 0,
1010c 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 0, 0) + 3;. z
1010d 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e Temp = malloc( n
1010e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d Byte*sizeof(zTem
1010f 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 p[0]) );. if(
10110 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 zTemp==0 ){.
10111 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
10112 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ed);. retur
10113 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
10114 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 6c }. GetFul
10115 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 lPathNameA((char
10116 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 *)zConverted, nB
10117 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a yte, zTemp, 0);.
10118 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
10119 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d ted);. zOut =
1011a 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d sqlite3_win32_m
1011b 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d bcs_to_utf8(zTem
1011c 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 p);. free(zTe
1011d 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a mp);.#endif. }.
1011e 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 20 if( zOut ){.
1011f 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
10120 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e tf(pVfs->mxPathn
10121 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 ame, zFull, "%s"
10122 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 65 , zOut);. fre
10123 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 e(zOut);. ret
10124 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
10125 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
10126 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
10127 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
10128 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 65 63 *.** Get the sec
10129 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 tor size of the
1012a 64 65 76 69 63 65 20 75 73 65 64 20 74 6f 20 73 device used to s
1012b 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f tore.** file..*/
1012c 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 53 .static int getS
1012d 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 73 ectorSize(. s
1012e 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1012f 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 ,. const char
10130 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20 20 *zRelative
10131 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e 61 /* UTF-8 file na
10132 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 me */.){. DWORD
10133 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 bytesPerSector
10134 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 = SQLITE_DEFAULT
10135 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 _SECTOR_SIZE;.
10136 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 /* GetDiskFreeSp
10137 61 63 65 20 69 73 20 6e 6f 74 20 73 75 70 70 6f ace is not suppo
10138 72 74 65 64 20 75 6e 64 65 72 20 57 49 4e 43 45 rted under WINCE
10139 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f */.#if SQLITE_O
1013a 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 S_WINCE. UNUSED
1013b 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
1013c 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
1013d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 65 29 3b ETER(zRelative);
1013e 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 20 7a 46 .#else. char zF
1013f 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 48 ullpath[MAX_PATH
10140 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 +1];. int rc;.
10141 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20 30 DWORD dwRet = 0
10142 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 75 6d 6d ;. DWORD dwDumm
10143 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 65 y;.. /*. ** We
10144 20 6e 65 65 64 20 74 6f 20 67 65 74 20 74 68 65 need to get the
10145 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65 20 full path name
10146 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a of the file. **
10147 20 74 6f 20 67 65 74 20 74 68 65 20 64 72 69 76 to get the driv
10148 65 20 6c 65 74 74 65 72 20 74 6f 20 6c 6f 6f 6b e letter to look
10149 20 75 70 20 74 68 65 20 73 65 63 74 6f 72 0a 20 up the sector.
1014a 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 ** size.. */.
1014b 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c 50 61 74 rc = winFullPat
1014c 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 52 65 6c hname(pVfs, zRel
1014d 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 54 48 2c ative, MAX_PATH,
1014e 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 69 zFullpath);. i
1014f 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f f( rc == SQLITE_
10150 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 76 6f 69 OK ). {. voi
10151 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 d *zConverted =
10152 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
10153 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 29 3b 0a ame(zFullpath);.
10154 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 if( zConvert
10155 65 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ed ){. if(
10156 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 20 20 isNT() ){.
10157 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 74 /* trim path t
10158 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 66 o just drive ref
10159 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 erence */.
1015a 20 20 57 43 48 41 52 20 2a 70 20 3d 20 7a 43 6f WCHAR *p = zCo
1015b 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20 20 nverted;.
1015c 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a 20 for(;*p;p++){.
1015d 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 20 if( *p
1015e 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20 20 == '\\' ){.
1015f 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 27 *p = '\0'
10160 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 ;. br
10161 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
10162 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
10163 20 20 20 64 77 52 65 74 20 3d 20 47 65 74 44 69 dwRet = GetDi
10164 73 6b 46 72 65 65 53 70 61 63 65 57 28 28 57 43 skFreeSpaceW((WC
10165 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c HAR*)zConverted,
10166 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
10167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10168 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 &dwDummy,.
10169 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1016a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
1016b 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 2c 0a bytesPerSector,.
1016c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1016d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1016e 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 20 &dwDummy,.
1016f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64 &d
10171 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 7d wDummy);. }
10172 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
10173 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75 trim path to ju
10174 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e st drive referen
10175 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 ce */. ch
10176 61 72 20 2a 70 20 3d 20 28 63 68 61 72 20 2a 29 ar *p = (char *)
10177 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 zConverted;.
10178 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 for(;*p;p++)
10179 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1017a 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 *p == '\\' ){.
1017b 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 *p = '
1017c 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 \0';.
1017d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1017e 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1017f 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 dwRet = Ge
10180 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 28 tDiskFreeSpaceA(
10181 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 (char*)zConverte
10182 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 d,.
10183 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10184 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 &dwDummy,.
10185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10186 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10187 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 &bytesPerSector
10188 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
10189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1018a 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 &dwDummy,.
1018b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1018c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1018d 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 &dwDummy);.
1018e 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 }. free(zC
1018f 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d onverted);. }
10190 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74 20 . if( !dwRet
10191 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50 65 ){. bytesPe
10192 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 rSector = SQLITE
10193 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
10194 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a SIZE;. }. }.
10195 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
10196 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53 65 (int) bytesPerSe
10197 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64 65 ctor; .}..#ifnde
10198 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
10199 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a AD_EXTENSION./*.
1019a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f ** Interfaces fo
1019b 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 r opening a shar
1019c 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 ed library, find
1019d 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 ing entry points
1019e 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 .** within the s
1019f 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 hared library, a
101a0 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 nd closing the s
101a1 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a hared library..*
101a2 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 /./*.** Interfac
101a3 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 es for opening a
101a4 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
101a5 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 finding entry p
101a6 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 oints.** within
101a7 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
101a8 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 ry, and closing
101a9 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
101aa 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
101ab 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 71 id *winDlOpen(sq
101ac 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
101ad 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
101ae 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 4c lename){. HANDL
101af 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f E h;. void *zCo
101b0 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 nverted = conver
101b1 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 tUtf8Filename(zF
101b2 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 ilename);. UNUS
101b3 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
101b4 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 s);. if( zConve
101b5 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 rted==0 ){. r
101b6 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 eturn 0;. }. i
101b7 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
101b8 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 h = LoadLibrary
101b9 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 W((WCHAR*)zConve
101ba 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 rted);./* isNT()
101bb 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
101bc 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
101bd 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
101be 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a er executed. .**
101bf 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 Since the ASCII
101c0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 version of thes
101c1 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f e Windows API do
101c2 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 not exist for W
101c3 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d INCE,.** it's im
101c4 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 portant to not r
101c5 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f eference them fo
101c6 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a r WINCE builds..
101c7 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
101c8 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 _WINCE==0. }els
101c9 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c e{. h = LoadL
101ca 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29 7a ibraryA((char*)z
101cb 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 Converted);.#end
101cc 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 if. }. free(zC
101cd 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65 74 onverted);. ret
101ce 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a urn (void*)h;.}.
101cf 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44 static void winD
101d0 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
101d1 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 fs *pVfs, int nB
101d2 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 uf, char *zBufOu
101d3 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 t){. UNUSED_PAR
101d4 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
101d5 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 getLastErrorMsg(
101d6 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b 0a nBuf, zBufOut);.
101d7 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 79 }.void (*winDlSy
101d8 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 m(sqlite3_vfs *p
101d9 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 Vfs, void *pHand
101da 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a le, const char *
101db 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 7b zSymbol))(void){
101dc 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
101dd 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20 53 TER(pVfs);.#if S
101de 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
101df 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63 41 /* The GetProcA
101e0 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69 6e ddressA() routin
101e1 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 e is only availa
101e2 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f ble on wince. */
101e3 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28 . return (void(
101e4 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63 *)(void))GetProc
101e5 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c 45 AddressA((HANDLE
101e6 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f )pHandle, zSymbo
101e7 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 41 l);.#else. /* A
101e8 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 73 ll other windows
101e9 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 63 platforms expec
101ea 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 t GetProcAddress
101eb 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a 20 () to take. **
101ec 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20 72 an Ansi string r
101ed 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
101ee 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69 6e _UNICODE settin
101ef 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 g */. return (v
101f0 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74 oid(*)(void))Get
101f1 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 4e ProcAddress((HAN
101f2 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 DLE)pHandle, zSy
101f3 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a mbol);.#endif.}.
101f4 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 28 void winDlClose(
101f5 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
101f6 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 s, void *pHandle
101f7 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
101f8 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 46 METER(pVfs);. F
101f9 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e 44 reeLibrary((HAND
101fa 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 LE)pHandle);.}.#
101fb 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 else /* if SQLIT
101fc 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
101fd 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 NSION is defined
101fe 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 77 : */. #define w
101ff 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 inDlOpen 0. #d
10200 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f 72 efine winDlError
10201 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 0. #define win
10202 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 DlSym 0. #def
10203 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30 ine winDlClose 0
10204 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
10205 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 Write up to nBuf
10206 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d bytes of random
10207 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a ness into zBuf..
10208 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
10209 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 nRandomness(sqli
1020a 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
1020b 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
1020c 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 Buf){. int n =
1020d 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 0;. UNUSED_PARA
1020e 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 METER(pVfs);.#if
1020f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
10210 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66 TEST). n = nBuf
10211 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c ;. memset(zBuf,
10212 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0, nBuf);.#else
10213 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53 59 . if( sizeof(SY
10214 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 2d STEMTIME)<=nBuf-
10215 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d 54 n ){. SYSTEMT
10216 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53 79 IME x;. GetSy
10217 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 20 stemTime(&x);.
10218 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
10219 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 ], &x, sizeof(x)
1021a 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
1021b 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28 of(x);. }. if(
1021c 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d sizeof(DWORD)<=
1021d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 nBuf-n ){. DW
1021e 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75 72 ORD pid = GetCur
1021f 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 3b rentProcessId();
10220 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 . memcpy(&zBu
10221 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 f[n], &pid, size
10222 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e 20 of(pid));. n
10223 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a += sizeof(pid);.
10224 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 }. if( sizeof
10225 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 (DWORD)<=nBuf-n
10226 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e 74 ){. DWORD cnt
10227 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 28 = GetTickCount(
10228 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
10229 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 Buf[n], &cnt, si
1022a 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 20 zeof(cnt));.
1022b 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29 n += sizeof(cnt)
1022c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 ;. }. if( size
1022d 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 of(LARGE_INTEGER
1022e 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 )<=nBuf-n ){.
1022f 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69 LARGE_INTEGER i
10230 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66 6f ;. QueryPerfo
10231 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69 rmanceCounter(&i
10232 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
10233 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65 Buf[n], &i, size
10234 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d of(i));. n +=
10235 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a sizeof(i);. }.
10236 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
10237 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 n;.}.../*.** Sle
10238 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 ep for a little
10239 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 while. Return t
1023a 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d he amount of tim
1023b 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74 e slept..*/.stat
1023c 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28 ic int winSleep(
1023d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1023e 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29 s, int microsec)
1023f 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 6f {. Sleep((micro
10240 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a sec+999)/1000);.
10241 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
10242 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 ER(pVfs);. retu
10243 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 rn ((microsec+99
10244 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d 9)/1000)*1000;.}
10245 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
10246 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
10247 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
10248 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f zero value, beco
10249 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a mes the result.*
1024a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 * returned from
1024b 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
1024c 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 Time(). This is
1024d 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
1024e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c g..*/.#ifdef SQL
1024f 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
10250 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
10251 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 current_time = 0
10252 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
10253 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 Find the current
10254 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 time (in Univer
10255 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 sal Coordinated
10256 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 Time). Write th
10257 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d e.** current tim
10258 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 e and date as a
10259 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 Julian Day numbe
1025a 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e r into *prNow an
1025b 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 d.** return 0.
1025c 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
1025d 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 time and date ca
1025e 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
1025f 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 /.int winCurrent
10260 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 Time(sqlite3_vfs
10261 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a *pVfs, double *
10262 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49 prNow){. FILETI
10263 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45 ME ft;. /* FILE
10264 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 69 TIME structure i
10265 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65 s a 64-bit value
10266 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
10267 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 20 e number of .
10268 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 100-nanosecond
10269 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 65 intervals since
1026a 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 31 January 1, 1601
1026b 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 35 (= JD 2305813.5
1026c 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ). . */. sqlit
1026d 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b 20 e3_int64 timeW;
1026e 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73 20 /* Whole days
1026f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
10270 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20 46 64 timeF; /* F
10271 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20 2a ractional Days *
10272 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /.. /* Number o
10273 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 f 100-nanosecond
10274 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61 20 intervals in a
10275 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20 20 single day */.
10276 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
10277 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 ite3_int64 ntuPe
10278 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30 rDay = . 10
10279 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 000000*(sqlite3_
1027a 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20 20 int64)86400;..
1027b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 30 /* Number of 100
1027c 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 -nanosecond inte
1027d 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f 66 rvals in half of
1027e 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74 a day */. stat
1027f 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
10280 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61 6c _int64 ntuPerHal
10281 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30 fDay = . 10
10282 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 000000*(sqlite3_
10283 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20 20 int64)43200;..
10284 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 6f /* 2^32 - to avo
10285 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e 64 id use of LL and
10286 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 63 warnings in gcc
10287 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e */. static con
10288 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 st sqlite3_int64
10289 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20 3d max32BitValue =
1028a 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 . (sqlite3
1028b 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30 _int64)200000000
1028c 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 0 + (sqlite3_int
1028d 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b 20 64)2000000000 +
1028e 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 (sqlite3_int64)2
1028f 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20 53 94967296;..#if S
10290 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
10291 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d 65 SYSTEMTIME time
10292 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d ;. GetSystemTim
10293 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 e(&time);. /* i
10294 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 f SystemTimeToFi
10295 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c 20 leTime() fails,
10296 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e it returns zero.
10297 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74 65 */. if (!Syste
10298 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 mTimeToFileTime(
10299 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20 &time,&ft)){.
1029a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 return 1;. }.#
1029b 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 6d else. GetSystem
1029c 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 20 TimeAsFileTime(
1029d 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 &ft );.#endif.
1029e 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1029f 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57 20 (pVfs);. timeW
102a0 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 = (((sqlite3_int
102a1 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74 65 64)ft.dwHighDate
102a2 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56 61 Time)*max32BitVa
102a3 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33 5f lue) + (sqlite3_
102a4 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44 61 int64)ft.dwLowDa
102a5 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46 20 teTime;. timeF
102a6 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65 72 = timeW % ntuPer
102a7 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Day; /*
102a8 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 fractional days
102a9 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 (100-nanosecond
102aa 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 s) */. timeW =
102ab 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44 61 timeW / ntuPerDa
102ac 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77 y; /* w
102ad 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 74 hole days */. t
102ae 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 32 imeW = timeW + 2
102af 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20 20 305813;
102b0 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 /* add whole
102b1 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35 38 days (from 23058
102b2 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 46 13.5) */. timeF
102b3 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50 65 = timeF + ntuPe
102b4 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20 2f rHalfDay; /
102b5 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61 79 * add half a day
102b6 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35 (from 2305813.5
102b7 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 ) */. timeW = t
102b8 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e 74 imeW + (timeF/nt
102b9 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61 64 uPerDay); /* ad
102ba 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20 68 d whole day if h
102bb 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e 65 alf day made one
102bc 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 69 */. timeF = ti
102bd 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79 3b meF % ntuPerDay;
102be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6d /* com
102bf 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69 6f pute new fractio
102c0 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a 70 nal days */. *p
102c1 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 74 rNow = (double)t
102c2 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65 29 imeW + ((double)
102c3 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65 29 timeF / (double)
102c4 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66 64 ntuPerDay);.#ifd
102c5 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
102c6 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72 if( sqlite3_cur
102c7 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 rent_time ){.
102c8 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75 62 *prNow = ((doub
102c9 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65 le)sqlite3_curre
102ca 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62 6c nt_time + (doubl
102cb 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75 62 e)43200) / (doub
102cc 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75 62 le)86400 + (doub
102cd 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d 0a le)2440587;. }.
102ce 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
102cf 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 0;.}../*.** The
102d0 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69 idea is that thi
102d1 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 s function works
102d2 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74 like a combinat
102d3 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 ion of.** GetLas
102d4 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72 tError() and For
102d5 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20 matMessage() on
102d6 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e windows (or errn
102d7 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f o and.** strerro
102d8 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e 20 r_r() on unix).
102d9 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69 After an error i
102da 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e s returned by an
102db 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c OS.** function,
102dc 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 SQLite calls th
102dd 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 is function with
102de 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74 zBuf pointing t
102df 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66 o.** a buffer of
102e0 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 65 nBuf bytes. The
102e1 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64 OS layer should
102e2 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a populate the.**
102e3 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20 6e buffer with a n
102e4 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ul-terminated UT
102e5 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f F-8 encoded erro
102e6 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73 r message.** des
102e7 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74 cribing the last
102e8 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76 IO error to hav
102e9 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 69 e occurred withi
102ea 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a n the calling.**
102eb 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 thread..**.** I
102ec 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 f the error mess
102ed 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 age is too large
102ee 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 65 for the supplie
102ef 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20 d buffer,.** it
102f0 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 should be trunca
102f1 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e 20 ted. The return
102f2 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 73 value of xGetLas
102f3 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 tError.** is zer
102f4 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d o if the error m
102f5 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 essage fits in t
102f6 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f he buffer, or no
102f7 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 n-zero.** otherw
102f8 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 ise (if the mess
102f9 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 age was truncate
102fa 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 d). If non-zero
102fb 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 is returned,.**
102fc 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e then it is not n
102fd 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c ecessary to incl
102fe 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d ude the nul-term
102ff 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72 inator character
10300 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75 .** in the outpu
10301 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 t buffer..**.**
10302 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e Not supplying an
10303 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 error message w
10304 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65 ill have no adve
10305 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e rse effect.** on
10306 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 66 SQLite. It is f
10307 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 69 ine to have an i
10308 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 mplementation th
10309 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75 at never.** retu
1030a 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 rns an error mes
1030b 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e sage:.**.** in
1030c 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 t xGetLastError(
1030d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
1030e 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 s, int nBuf, cha
1030f 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20 r *zBuf){.**
10310 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d assert(zBuf[0]=
10311 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72 ='\0');.** r
10312 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a eturn 0;.** }.
10313 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 **.** However if
10314 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
10315 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 69 e is supplied, i
10316 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70 t will be incorp
10317 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c orated.** by sql
10318 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 72 ite into the err
10319 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 6c or message avail
1031a 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72 able to the user
1031b 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 using.** sqlite
1031c 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 3_errmsg(), poss
1031d 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 ibly making IO e
1031e 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f 20 rrors easier to
1031f 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 debug..*/.static
10320 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45 int winGetLastE
10321 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
10322 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
10323 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 , char *zBuf){.
10324 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
10325 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 R(pVfs);. retur
10326 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 n getLastErrorMs
10327 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d g(nBuf, zBuf);.}
10328 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
10329 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c ze and deinitial
1032a 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e ize the operatin
1032b 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 g system interfa
1032c 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ce..*/.SQLITE_AP
1032d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
1032e 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 _init(void){. s
1032f 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
10330 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20 s winVfs = {.
10331 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
10332 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
10333 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69 */. sizeof(wi
10334 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f nFile), /* szO
10335 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58 sFile */. MAX
10336 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 20 _PATH,
10337 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f /* mxPathname */
10338 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 . 0,
10339 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
1033a 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 2c */. "win32",
1033b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e /* zN
1033c 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
1033d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1033e 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20 pAppData */. .
1033f 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20 winOpen,
10340 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a /* xOpen *
10341 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c /. winDelete,
10342 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c /* xDel
10343 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 ete */. winAc
10344 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a cess, /*
10345 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 xAccess */.
10346 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c winFullPathname,
10347 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e /* xFullPathn
10348 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c ame */. winDl
10349 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a Open, /*
1034a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 xDlOpen */.
1034b 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 winDlError,
1034c 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a /* xDlError *
1034d 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20 /. winDlSym,
1034e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 /* xDlS
1034f 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43 ym */. winDlC
10350 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 lose, /*
10351 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 xDlClose */.
10352 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 winRandomness,
10353 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 /* xRandomnes
10354 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 s */. winSlee
10355 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 p, /* x
10356 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e Sleep */. win
10357 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 CurrentTime,
10358 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 /* xCurrentTime
10359 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 73 */. winGetLas
1035a 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 tError /* xGe
1035b 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 tLastError */.
1035c 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 };.. sqlite3_vf
1035d 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 s_register(&winV
1035e 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e fs, 1);. return
1035f 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 SQLITE_OK; .}.S
10360 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
10361 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 lite3_os_end(voi
10362 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 d){ . return SQ
10363 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 LITE_OK;.}..#end
10364 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
10365 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a WIN */../*******
10366 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f ******* End of o
10367 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_win.c ********
10368 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1036a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1036b 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
1036c 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a le bitvec.c ****
1036d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1036e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1036f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
10370 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a 08 February 16.*
10371 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
10372 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
10373 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
10374 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
10375 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
10376 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
10377 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
10378 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
10379 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1037a 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1037b 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1037c 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1037d 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
1037e 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
1037f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
10380 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
10381 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
10382 0a 2a 2a 0a 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 2a 2a 2a 2a ****************
10385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
10387 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
10388 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 ments an object
10389 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 that represents
1038a 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a a fixed-length.*
1038b 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 * bitmap. Bits
1038c 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 are numbered sta
1038d 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a rting with 1..**
1038e 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 .** A bitmap is
1038f 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 used to record w
10390 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20 hich pages of a
10391 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 database file ha
10392 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e ve been.** journ
10393 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74 alled during a t
10394 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77 ransaction, or w
10395 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20 hich pages have
10396 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22 the "dont-write"
10397 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55 .** property. U
10398 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65 sually only a fe
10399 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74 w pages are meet
1039a 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f either conditio
1039b 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74 n..** So the bit
1039c 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73 map is usually s
1039d 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f parse and has lo
1039e 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a w cardinality..*
1039f 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 * But sometimes
103a0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 (for example whe
103a1 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20 n during a DROP
103a2 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65 of a large table
103a3 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c ) most.** or all
103a4 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e of the pages in
103a5 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 a database can
103a6 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 get journalled.
103a7 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c In those cases,
103a8 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20 .** the bitmap
103a9 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69 becomes dense wi
103aa 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c th high cardinal
103ab 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69 ity. The algori
103ac 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f thm needs .** to
103ad 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 handle both cas
103ae 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 es well..**.** T
103af 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 he size of the b
103b0 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77 itmap is fixed w
103b1 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69 hen the object i
103b2 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a s created..**.**
103b3 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c All bits are cl
103b4 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74 ear when the bit
103b5 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20 map is created.
103b6 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 Individual bits
103b7 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f .** may be set o
103b8 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 r cleared one at
103b9 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 a time..**.** T
103ba 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 est operations a
103bb 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d re about 100 tim
103bc 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 es more common t
103bd 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f hat set operatio
103be 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 ns..** Clear ope
103bf 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 rations are exce
103c0 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 edingly rare. T
103c1 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 here are usually
103c2 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e between.** 5 an
103c3 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74 d 500 set operat
103c4 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20 ions per Bitvec
103c5 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74 object, though t
103c6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 he number of set
103c7 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d s can.** sometim
103c8 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e es grow into ten
103c9 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f s of thousands o
103ca 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73 r larger. The s
103cb 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 ize of the.** Bi
103cc 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74 tvec object is t
103cd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
103ce 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
103cf 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a se file at the.*
103d0 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61 * start of a tra
103d1 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 nsaction, and is
103d2 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 thus usually le
103d3 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68 ss than a few th
103d4 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 ousand,.** but c
103d5 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61 an be as large a
103d6 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 s 2 billion for
103d7 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74 a really big dat
103d8 61 62 61 73 65 2e 0a 2a 2f 0a 0a 2f 2a 20 53 69 abase..*/../* Si
103d9 7a 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 ze of the Bitvec
103da 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 structure in by
103db 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tes. */.#define
103dc 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 BITVEC_SZ
103dd 20 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a (sizeof(void*)*
103de 31 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e 20 128) /* 512 on
103df 33 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 20 32bit. 1024 on
103e0 36 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 75 64bit */../* Rou
103e1 6e 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a nd the union siz
103e2 65 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 e down to the ne
103e3 61 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 6f arest pointer bo
103e4 75 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 68 undary, since th
103e5 61 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 at's how .** it
103e6 77 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 will be aligned
103e7 77 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 65 within the Bitve
103e8 63 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 c struct. */.#de
103e9 66 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 5a fine BITVEC_USIZ
103ea 45 20 20 20 20 20 28 28 28 42 49 54 56 45 43 5f E (((BITVEC_
103eb 53 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 SZ-(3*sizeof(u32
103ec 29 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 )))/sizeof(Bitve
103ed 63 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 c*))*sizeof(Bitv
103ee 65 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f ec*))../* Type o
103ef 66 20 74 68 65 20 61 72 72 61 79 20 22 65 6c 65 f the array "ele
103f0 6d 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 69 ment" for the bi
103f1 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 tmap representat
103f2 69 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 ion. .** Should
103f3 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2c be a power of 2,
103f4 20 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 and ideally, ev
103f5 65 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 6f enly divide into
103f6 20 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a BITVEC_USIZE. .
103f7 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 20 ** Setting this
103f8 74 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c 20 to the "natural
103f9 77 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 6f word" size of yo
103fa 75 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 6f ur CPU may impro
103fb 76 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 ve.** performanc
103fc 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 e. */.#define BI
103fd 54 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 TVEC_TELEM u
103fe 38 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 8./* Size, in bi
103ff 74 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61 ts, of the bitma
10400 70 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 p element. */.#d
10401 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 efine BITVEC_SZE
10402 4c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 LEM 8./* Numb
10403 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 er of elements i
10404 6e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 79 n a bitmap array
10405 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 . */.#define BIT
10406 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 VEC_NELEM (B
10407 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 ITVEC_USIZE/size
10408 6f 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 of(BITVEC_TELEM)
10409 29 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 )./* Number of b
1040a 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 its in the bitma
1040b 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 p array. */.#def
1040c 69 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 ine BITVEC_NBIT
1040d 20 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 4c (BITVEC_NEL
1040e 45 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d EM*BITVEC_SZELEM
1040f 29 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 )../* Number of
10410 75 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 61 u32 values in ha
10411 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 sh table. */.#de
10412 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 fine BITVEC_NINT
10413 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 (BITVEC_US
10414 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 IZE/sizeof(u32))
10415 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 ./* Maximum numb
10416 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
10417 20 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 6f hash table befo
10418 72 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 re .** sub-divid
10419 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 ing and re-hashi
1041a 6e 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 ng. */.#define B
1041b 49 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 20 ITVEC_MXHASH
1041c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a (BITVEC_NINT/2).
1041d 2f 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 74 /* Hashing funct
1041e 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 73 ion for the aHas
1041f 68 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e h representation
10420 2e 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 ..** Empirical t
10421 65 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 68 esting showed th
10422 61 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 at the *37 multi
10423 70 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 plier .** (an ar
10424 62 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 6e bitrary prime)in
10425 20 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 the hash functi
10426 6f 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 on provided .**
10427 6e 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 no fewer collisi
10428 6f 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d ons than the no-
10429 6f 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e op *1. */.#defin
1042a 65 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29 e BITVEC_HASH(X)
1042b 20 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56 (((X)*1)%BITV
1042c 45 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e EC_NINT)..#defin
1042d 65 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 20 e BITVEC_NPTR
1042e 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 (BITVEC_USIZE
1042f 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 2a /sizeof(Bitvec *
10430 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 )).../*.** A bit
10431 6d 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e map is an instan
10432 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
10433 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
10434 2a 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 70 *.** This bitmap
10435 20 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 69 records the exi
10436 73 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f stance of zero o
10437 72 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 r more bits.** w
10438 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 ith values betwe
10439 65 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 en 1 and iSize,
1043a 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a inclusive..**.**
1043b 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 There are three
1043c 20 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 73 possible repres
1043d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
1043e 20 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 bitmap..** If i
1043f 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 Size<=BITVEC_NBI
10440 54 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 T, then Bitvec.u
10441 2e 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 .aBitmap[] is a
10442 73 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d straight.** bitm
10443 61 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 73 ap. The least s
10444 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 69 ignificant bit i
10445 73 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 s bit 1..**.** I
10446 66 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e f iSize>BITVEC_N
10447 42 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72 BIT and iDivisor
10448 3d 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e ==0 then Bitvec.
10449 75 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 u.aHash[] is.**
1044a 61 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 a hash table tha
1044b 74 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 t will hold up t
1044c 6f 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 o BITVEC_MXHASH
1044d 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e distinct values.
1044e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
1044f 2c 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 73 , the value i is
10450 20 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 6f redirected into
10451 20 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f 4e one of BITVEC_N
10452 50 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 PTR.** sub-bitma
10453 70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ps pointed to by
10454 20 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 5b Bitvec.u.apSub[
10455 5d 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d ]. Each subbitm
10456 61 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 ap.** handles up
10457 20 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 70 to iDivisor sep
10458 61 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 20 arate values of
10459 69 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c i. apSub[0] hol
1045a 64 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 ds.** values bet
1045b 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 ween 1 and iDivi
1045c 73 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 sor. apSub[1] h
1045d 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 olds values betw
1045e 65 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b een.** iDivisor+
1045f 31 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 1 and 2*iDivisor
10460 2e 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 . apSub[N] hold
10461 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e s values between
10462 0a 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 .** N*iDivisor+1
10463 20 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 and (N+1)*iDivi
10464 73 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 69 sor. Each subbi
10465 74 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a tmap is normaliz
10466 65 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 ed.** to hold de
10467 61 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 al with values b
10468 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 etween 1 and iDi
10469 76 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 visor..*/.struct
1046a 20 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 20 Bitvec {. u32
1046b 69 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d iSize; /* M
1046c 61 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 aximum bit index
1046d 2e 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 20 . Max iSize is
1046e 34 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 4,294,967,296. *
1046f 2f 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 /. u32 nSet;
10470 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10471 20 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 bits that are s
10472 65 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 et - only valid
10473 66 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 20 for aHash.
10474 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 ** e
10475 6c 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 lement. Max is
10476 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f BITVEC_NINT. Fo
10477 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 r BITVEC_SZ of 5
10478 31 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 12,.
10479 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 6f ** this wo
1047a 75 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 uld be 125. */.
1047b 20 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20 u32 iDivisor;
1047c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 /* Number of bi
1047d 74 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61 ts handled by ea
1047e 63 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 ch apSub[] entry
1047f 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 . */.
10480 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 /* Should
10481 20 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65 >=0 for apSub e
10482 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 lement. */.
10483 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10484 4d 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20 Max iDivisor is
10485 6d 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45 max(u32) / BITVE
10486 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a C_NPTR + 1. */.
10487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10488 20 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 45 /* For a BITVE
10489 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 C_SZ of 512, thi
1048a 73 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 s would be 34,35
1048b 39 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 9,739. */. unio
1048c 6e 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f 54 n {. BITVEC_T
1048d 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 ELEM aBitmap[BIT
1048e 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f VEC_NELEM]; /
1048f 2a 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 65 * Bitmap represe
10490 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 ntation */. u
10491 33 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 32 aHash[BITVEC_
10492 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 NINT]; /* H
10493 61 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 73 ash table repres
10494 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 entation */.
10495 42 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 49 Bitvec *apSub[BI
10496 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 TVEC_NPTR]; /*
10497 52 65 63 75 72 73 69 76 65 20 72 65 70 72 65 73 Recursive repres
10498 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 entation */. }
10499 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 u;.};../*.** Cre
1049a 61 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 70 ate a new bitmap
1049b 20 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 object able to
1049c 68 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 77 handle bits betw
1049d 65 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 2c een 0 and iSize,
1049e 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 .** inclusive.
1049f 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
104a0 20 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 to the new obje
104a1 63 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c ct. Return NULL
104a2 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 if .** malloc f
104a3 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ails..*/.SQLITE_
104a4 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a PRIVATE Bitvec *
104a5 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 sqlite3BitvecCre
104a6 61 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0a ate(u32 iSize){.
104a7 20 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 Bitvec *p;. a
104a8 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 ssert( sizeof(*p
104a9 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a )==BITVEC_SZ );.
104aa 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
104ab 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
104ac 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 29 *p) );. if( p )
104ad 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d {. p->iSize =
104ae 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 iSize;. }. re
104af 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
104b0 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
104b1 20 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 73 the i-th bit is
104b2 20 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 72 set. Return tr
104b3 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 ue or false..**
104b4 49 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 If p is NULL (if
104b5 20 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 20 the bitmap has
104b6 6e 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 not been created
104b7 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 ) or if.** i is
104b8 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 out of range, th
104b9 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e en return false.
104ba 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
104bb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 TE int sqlite3Bi
104bc 74 76 65 63 54 65 73 74 28 42 69 74 76 65 63 20 tvecTest(Bitvec
104bd 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 66 *p, u32 i){. if
104be 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
104bf 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 0;. if( i>p->iS
104c0 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 ize || i==0 ) re
104c1 74 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 turn 0;. i--;.
104c2 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 while( p->iDivi
104c3 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 sor ){. u32 b
104c4 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 in = i/p->iDivis
104c5 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d or;. i = i%p-
104c6 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 >iDivisor;. p
104c7 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 = p->u.apSub[bi
104c8 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 n];. if (!p)
104c9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
104ca 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
104cb 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 ( p->iSize<=BITV
104cc 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 EC_NBIT ){. r
104cd 65 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 eturn (p->u.aBit
104ce 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 map[i/BITVEC_SZE
104cf 4c 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 LEM] & (1<<(i&(B
104d0 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 ITVEC_SZELEM-1))
104d1 29 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b ))!=0;. } else{
104d2 0a 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 . u32 h = BIT
104d3 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 VEC_HASH(i++);.
104d4 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 while( p->u.a
104d5 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 Hash[h] ){.
104d6 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b if( p->u.aHash[
104d7 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31 h]==i ) return 1
104d8 3b 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31 ;. h = (h+1
104d9 29 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b ) % BITVEC_NINT;
104da 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
104db 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a n 0;. }.}../*.*
104dc 2a 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 62 * Set the i-th b
104dd 69 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e it. Return 0 on
104de 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 success and an
104df 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a error code if.**
104e0 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 anything goes w
104e1 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rong..**.** This
104e2 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 63 routine might c
104e3 61 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 73 ause sub-bitmaps
104e4 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 to be allocated
104e5 2e 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 6f . Failing.** to
104e6 20 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 get the memory
104e7 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 needed to hold t
104e8 68 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 73 he sub-bitmap is
104e9 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 61 the only.** tha
104ea 74 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 77 t can go wrong w
104eb 69 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 61 ith an insert, a
104ec 73 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 20 ssuming p and i
104ed 61 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a are valid..**.**
104ee 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e The calling fun
104ef 63 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72 ction must ensur
104f0 65 20 74 68 61 74 20 70 20 69 73 20 61 20 76 61 e that p is a va
104f1 6c 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 63 lid Bitvec objec
104f2 74 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 t.** and that th
104f3 65 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20 e value for "i"
104f4 69 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20 is within range
104f5 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 of the Bitvec ob
104f6 6a 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 ject..** Otherwi
104f7 73 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 se the behavior
104f8 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
104f9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
104fa 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
104fb 63 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c 20 cSet(Bitvec *p,
104fc 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 3b u32 i){. u32 h;
104fd 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
104fe 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
104ff 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b assert( i>0 );
10500 0a 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d . assert( i<=p-
10501 3e 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b >iSize );. i--;
10502 0a 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 . while((p->iSi
10503 7a 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 ze > BITVEC_NBIT
10504 29 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 ) && p->iDivisor
10505 29 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 ) {. u32 bin
10506 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i/p->iDivisor;
10507 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 . i = i%p->iD
10508 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 ivisor;. if(
10509 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d p->u.apSub[bin]=
1050a 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 =0 ){. p->u
1050b 2e 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 .apSub[bin] = sq
1050c 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
1050d 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 e( p->iDivisor )
1050e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 ;. if( p->u
1050f 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 .apSub[bin]==0 )
10510 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
10511 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
10512 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 p = p->u.apSub[b
10513 69 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 in];. }. if( p
10514 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f ->iSize<=BITVEC_
10515 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 NBIT ){. p->u
10516 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 .aBitmap[i/BITVE
10517 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c C_SZELEM] |= 1 <
10518 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 < (i&(BITVEC_SZE
10519 4c 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 LEM-1));. ret
1051a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1051b 20 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f }. h = BITVEC_
1051c 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 HASH(i++);. /*
1051d 69 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 if there wasn't
1051e 61 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e a hash collision
1051f 2c 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e , and this doesn
10520 27 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 't */. /* compl
10521 65 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 etely fill the h
10522 61 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 ash, then just a
10523 64 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f dd it without */
10524 0a 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 . /* worring ab
10525 6f 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 out sub-dividing
10526 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e and re-hashing.
10527 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e */. if( !p->u.
10528 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 aHash[h] ){.
10529 69 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 if (p->nSet<(BIT
1052a 56 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 VEC_NINT-1)) {.
1052b 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 goto bitvec
1052c 5f 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 _set_end;. }
1052d 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 else {. got
1052e 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 o bitvec_set_reh
1052f 61 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ash;. }. }.
10530 20 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 /* there was a
10531 63 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b collision, check
10532 20 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 to see if it's
10533 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 already */. /*
10534 69 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c in hash, if not,
10535 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 try to find a s
10536 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 pot for it */.
10537 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e do {. if( p->
10538 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 u.aHash[h]==i )
10539 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1053a 3b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 ;. h++;. i
1053b 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e f( h>=BITVEC_NIN
1053c 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 T ) h = 0;. } w
1053d 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 hile( p->u.aHash
1053e 5b 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 [h] );. /* we d
1053f 69 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e idn't find it in
10540 20 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f the hash. h po
10541 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
10542 74 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 t */. /* availa
10543 62 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 ble free spot. c
10544 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
10545 68 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 his is going to
10546 2a 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 */. /* make our
10547 20 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 hash too "full"
10548 2e 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 . */.bitvec_set
10549 5f 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 _rehash:. if( p
1054a 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d ->nSet>=BITVEC_M
1054b 58 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 XHASH ){. uns
1054c 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 igned int j;.
1054d 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 int rc;. u32
1054e 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 6c *aiValues = sql
1054f 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 ite3StackAllocRa
10550 77 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 w(0, sizeof(p->u
10551 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 66 .aHash));. if
10552 28 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b ( aiValues==0 ){
10553 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
10554 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
10555 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
10556 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d cpy(aiValues, p-
10557 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 >u.aHash, sizeof
10558 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 (p->u.aHash));.
10559 20 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 memset(p->u
1055a 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f .apSub, 0, sizeo
1055b 66 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b 0a f(p->u.apSub));.
1055c 20 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f p->iDiviso
1055d 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 r = (p->iSize +
1055e 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 BITVEC_NPTR - 1)
1055f 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 /BITVEC_NPTR;.
10560 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
10561 42 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b BitvecSet(p, i);
10562 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 . for(j=0;
10563 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a j<BITVEC_NINT; j
10564 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ++){. if(
10565 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 aiValues[j] ) r
10566 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 c |= sqlite3Bitv
10567 65 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 ecSet(p, aiValue
10568 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 s[j]);. }.
10569 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 sqlite3Stac
1056a 6b 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 65 kFree(0, aiValue
1056b 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e s);. return
1056c 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 62 rc;. }. }.b
1056d 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 itvec_set_end:.
1056e 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d p->nSet++;. p-
1056f 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b >u.aHash[h] = i;
10570 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
10571 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
10572 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 ear the i-th bit
10573 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 73 ..**.** pBuf mus
10574 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 t be a pointer t
10575 6f 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 45 o at least BITVE
10576 43 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 65 C_SZ bytes of te
10577 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 0a mporary storage.
10578 2a 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 6c ** that BitvecCl
10579 65 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 72 ear can use to r
1057a 65 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 20 ebuilt its hash
1057b 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 table..*/.SQLITE
1057c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1057d 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
1057e 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 (Bitvec *p, u32
1057f 69 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a i, void *pBuf){.
10580 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
10581 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 urn;. assert( i
10582 3e 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 >0 );. i--;. w
10583 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f hile( p->iDiviso
10584 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e r ){. u32 bin
10585 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 = i/p->iDivisor
10586 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 ;. i = i%p->i
10587 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d Divisor;. p =
10588 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d p->u.apSub[bin]
10589 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a ;. if (!p) {.
1058a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
1058b 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d }. }. if( p-
1058c 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e >iSize<=BITVEC_N
1058d 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e BIT ){. p->u.
1058e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 aBitmap[i/BITVEC
1058f 5f 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 _SZELEM] &= ~(1
10590 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a << (i&(BITVEC_SZ
10591 45 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 6c ELEM-1)));. }el
10592 73 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 se{. unsigned
10593 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 20 int j;. u32
10594 2a 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 66 *aiValues = pBuf
10595 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 ;. memcpy(aiV
10596 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 alues, p->u.aHas
10597 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 h, sizeof(p->u.a
10598 48 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d 73 Hash));. mems
10599 65 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 et(p->u.aHash, 0
1059a 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 , sizeof(p->u.aH
1059b 61 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 ash));. p->nS
1059c 65 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 et = 0;. for(
1059d 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 j=0; j<BITVEC_NI
1059e 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 NT; j++){.
1059f 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 if( aiValues[j]
105a0 26 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d && aiValues[j]!=
105a1 28 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 (i+1) ){.
105a2 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f u32 h = BITVEC_
105a3 48 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d HASH(aiValues[j]
105a4 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e -1);. p->
105a5 6e 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nSet++;.
105a6 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 while( p->u.aHas
105a7 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 h[h] ){.
105a8 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 h++;.
105a9 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e if( h>=BITVEC_N
105aa 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 INT ) h = 0;.
105ab 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
105ac 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 ->u.aHash[h] = a
105ad 69 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 iValues[j];.
105ae 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
105af 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 ./*.** Destroy a
105b0 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 bitmap object.
105b1 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d Reclaim all mem
105b2 6f 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c ory used..*/.SQL
105b3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
105b4 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
105b5 73 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 29 stroy(Bitvec *p)
105b6 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
105b7 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e eturn;. if( p->
105b8 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 iDivisor ){.
105b9 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
105ba 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 for(i=0; i<B
105bb 49 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 ITVEC_NPTR; i++)
105bc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 {. sqlite3B
105bd 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d 3e itvecDestroy(p->
105be 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 u.apSub[i]);.
105bf 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
105c0 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a _free(p);.}../*.
105c1 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 ** Return the va
105c2 6c 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a 65 lue of the iSize
105c3 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 parameter speci
105c4 66 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 63 fied when Bitvec
105c5 20 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 *p.** was creat
105c6 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
105c7 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
105c8 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 3BitvecSize(Bitv
105c9 65 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ec *p){. return
105ca 20 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 p->iSize;.}..#i
105cb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
105cc 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f T_BUILTIN_TEST./
105cd 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 *.** Let V[] be
105ce 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 69 an array of unsi
105cf 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 20 gned characters
105d0 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 6f sufficient to ho
105d1 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 ld.** up to N bi
105d2 74 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 6e ts. Let I be an
105d3 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e integer between
105d4 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 0 and N. 0<=I<
105d5 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 N..** Then the f
105d6 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 ollowing macros
105d7 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 can be used to s
105d8 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 et, clear, or te
105d9 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c st.** individual
105da 20 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e 0a bits within V..
105db 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 49 */.#define SETBI
105dc 54 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 3e T(V,I) V[I>
105dd 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 >3] |= (1<<(I&7)
105de 29 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 42 ).#define CLEARB
105df 49 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e IT(V,I) V[I>>
105e0 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 3] &= ~(1<<(I&7)
105e1 29 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 49 ).#define TESTBI
105e2 54 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 3e T(V,I) (V[I>
105e3 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 21 >3]&(1<<(I&7)))!
105e4 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 =0../*.** This r
105e5 6f 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 outine runs an e
105e6 78 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f 66 xtensive test of
105e7 20 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 65 the Bitvec code
105e8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 ..**.** The inpu
105e9 74 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 t is an array of
105ea 20 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 61 integers that a
105eb 63 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d cts as a program
105ec 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 .** to test the
105ed 42 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74 Bitvec. The int
105ee 65 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65 egers are opcode
105ef 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 s followed.** by
105f0 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 0, 1, or 3 oper
105f1 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 ands, depending
105f2 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 on the opcode.
105f3 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 Another.** opcod
105f4 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 e follows immedi
105f5 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 ately after the
105f6 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a last operand..**
105f7 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 20 .** There are 6
105f8 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 opcodes numbered
105f9 20 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 from 0 through
105fa 35 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 5. 0 is the.**
105fb 22 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e "halt" opcode an
105fc 64 20 63 61 75 73 65 73 20 74 68 65 20 74 65 73 d causes the tes
105fd 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 t to end..**.**
105fe 20 20 20 30 20 20 20 20 20 20 20 20 20 20 48 61 0 Ha
105ff 6c 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 lt and return th
10600 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f e number of erro
10601 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 rs.** 1 N S X
10602 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 62 Set N bits b
10603 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 eginning with S
10604 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 and incrementing
10605 20 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 by X.** 2 N
10606 53 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 62 S X Clear N b
10607 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 its beginning wi
10608 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 th S and increme
10609 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 nting by X.**
1060a 20 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 20 3 N Set
1060b 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 N randomly chose
1060c 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e n bits.** 4 N
1060d 20 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e 20 Clear N
1060e 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 randomly chosen
1060f 62 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 bits.** 5 N S
10610 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 X Set N bits
10611 20 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e from S incremen
10612 74 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c t X in array onl
10613 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 y, not in bitvec
10614 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 .**.** The opcod
10615 65 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 70 es 1 through 4 p
10616 65 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 erform set and c
10617 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 lear operations
10618 61 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a are performed.**
10619 20 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65 on both a Bitve
1061a 63 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 c object and on
1061b 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f a linear array o
1061c 66 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20 f bits obtained
1061d 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 from malloc..**
1061e 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f Opcode 5 works o
1061f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 n the linear arr
10620 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 ay only, not on
10621 74 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f the Bitvec..** O
10622 70 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20 pcode 5 is used
10623 74 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 to deliberately
10624 69 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69 induce a fault i
10625 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f n order to.** co
10626 6e 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f 72 nfirm that error
10627 20 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 detection works
10628 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 ..**.** At the c
10629 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 onclusion of the
1062a 20 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 72 test the linear
1062b 20 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 72 array is compar
1062c 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 ed.** against th
1062d 65 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e e Bitvec object.
1062e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 If there are a
1062f 6e 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c 0a ny differences,.
10630 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 ** an error is r
10631 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 eturned. If the
10632 79 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 y are the same,
10633 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 zero is returned
10634 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d ..**.** If a mem
10635 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
10636 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 rror occurs, ret
10637 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 urn -1..*/.SQLIT
10638 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
10639 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 lite3BitvecBuilt
1063a 69 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 inTest(int sz, i
1063b 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 nt *aOp){. Bitv
1063c 65 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 3b ec *pBitvec = 0;
1063d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1063e 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 *pV = 0;. int
1063f 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 rc = -1;. int i
10640 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 20 , nx, pc, op;.
10641 76 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b void *pTmpSpace;
10642 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
10643 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65 the Bitvec to be
10644 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69 tested and a li
10645 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20 near array of.
10646 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61 ** bits to act a
10647 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 s the reference
10648 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73 */. pBitvec = s
10649 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
1064a 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d te( sz );. pV =
1064b 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
1064c 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b (sz+7)/8 + 1 );
1064d 0a 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 73 . pTmpSpace = s
1064e 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 49 qlite3_malloc(BI
1064f 54 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 20 TVEC_SZ);. if(
10650 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 pBitvec==0 || pV
10651 3d 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 65 ==0 || pTmpSpace
10652 3d 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 76 ==0 ) goto bitv
10653 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 ec_end;. memset
10654 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 (pV, 0, (sz+7)/8
10655 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 4c + 1);.. /* NUL
10656 4c 20 70 42 69 74 76 65 63 20 74 65 73 74 73 20 L pBitvec tests
10657 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 */. sqlite3Bitv
10658 65 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 73 ecSet(0, 1);. s
10659 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 qlite3BitvecClea
1065a 72 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 63 r(0, 1, pTmpSpac
1065b 65 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 e);.. /* Run th
1065c 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 e program */. p
1065d 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 c = 0;. while(
1065e 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d (op = aOp[pc])!=
1065f 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 0 ){. switch(
10660 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 op ){. cas
10661 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 e 1:. case
10662 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 2:. case 5:
10663 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 {. nx =
10664 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4;. i = a
10665 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 Op[pc+2] - 1;.
10666 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 aOp[pc+2]
10667 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 += aOp[pc+3];.
10668 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
10669 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 }. case
1066a 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 3:. case 4:
1066b 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a . default:
1066c 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 {. nx =
1066d 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 2;. sqlit
1066e 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 e3_randomness(si
1066f 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 zeof(i), &i);.
10670 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
10671 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
10672 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 f( (--aOp[pc+1])
10673 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 > 0 ) nx = 0;.
10674 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 pc += nx;.
10675 20 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 i = (i & 0x7fff
10676 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 ffff)%sz;. if
10677 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b ( (op & 1)!=0 ){
10678 0a 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 . SETBIT(pV
10679 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 , (i+1));.
1067a 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 if( op!=5 ){.
1067b 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1067c 42 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 BitvecSet(pBitve
1067d 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 c, i+1) ) goto b
1067e 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 itvec_end;.
1067f 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
10680 20 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c CLEARBIT(pV,
10681 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 (i+1));. s
10682 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 qlite3BitvecClea
10683 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c 20 r(pBitvec, i+1,
10684 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 pTmpSpace);.
10685 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 }. }.. /* Test
10686 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
10687 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 e linear array e
10688 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 xactly matches t
10689 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f he. ** Bitvec o
1068a 62 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 bject. Start wi
1068b 74 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f th the assumptio
1068c 6e 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 n that they do.
1068d 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 ** match (rc==0
1068e 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f ). Change rc to
1068f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 non-zero if a d
10690 69 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 iscrepancy. **
10691 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 is found.. */.
10692 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 rc = sqlite3Bit
10693 76 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 vecTest(0,0) + s
10694 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
10695 28 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a (pBitvec, sz+1).
10696 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 + sqli
10697 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 te3BitvecTest(pB
10698 69 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 20 itvec, 0).
10699 20 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 69 + (sqlite3Bi
1069a 74 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 63 tvecSize(pBitvec
1069b 29 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69 ) - sz);. for(i
1069c 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b =1; i<=sz; i++){
1069d 0a 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42 . if( (TESTB
1069e 49 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74 IT(pV,i))!=sqlit
1069f 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 e3BitvecTest(pBi
106a0 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20 tvec,i) ){.
106a1 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 rc = i;. b
106a2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
106a3 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 . /* Free alloc
106a4 61 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a ated structure *
106a5 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20 /.bitvec_end:.
106a6 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d sqlite3_free(pTm
106a7 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 pSpace);. sqlit
106a8 65 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 e3_free(pV);. s
106a9 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
106aa 72 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 roy(pBitvec);.
106ab 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
106ac 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
106ad 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 IT_BUILTIN_TEST
106ae 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
106af 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 *** End of bitve
106b0 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c.c ************
106b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106b3 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
106b4 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 *** Begin file p
106b5 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a cache.c ********
106b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106b8 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 **/./*.** 2008 A
106b9 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 ugust 05.**.** T
106ba 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
106bb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
106bc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
106bd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
106be 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
106bf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
106c0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
106c1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
106c2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
106c3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
106c4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
106c5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
106c6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
106c7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
106c8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
106c9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
106ca 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
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 2a 2a 2a 2a 2a 2a 2a 2a ****************
106ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106cf 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
106d0 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 file implements
106d1 74 68 61 74 20 70 61 67 65 20 63 61 63 68 65 2e that page cache.
106d2 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d .*/../*.** A com
106d3 70 6c 65 74 65 20 70 61 67 65 20 63 61 63 68 65 plete page cache
106d4 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
106d5 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
106d6 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 61 e..*/.struct PCa
106d7 63 68 65 20 7b 0a 20 20 50 67 48 64 72 20 2a 70 che {. PgHdr *p
106d8 44 69 72 74 79 2c 20 2a 70 44 69 72 74 79 54 61 Dirty, *pDirtyTa
106d9 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c il; /* L
106da 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 ist of dirty pag
106db 65 73 20 69 6e 20 4c 52 55 20 6f 72 64 65 72 20 es in LRU order
106dc 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 53 79 6e */. PgHdr *pSyn
106dd 63 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 ced;
106de 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
106df 20 73 79 6e 63 65 64 20 70 61 67 65 20 69 6e 20 synced page in
106e0 64 69 72 74 79 20 70 61 67 65 20 6c 69 73 74 20 dirty page list
106e1 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
106e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106e3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
106e4 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64 er of referenced
106e5 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 pages */. int
106e6 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 nMax;
106e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106e8 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 63 61 /* Configured ca
106e9 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e che size */. in
106ea 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 t szPage;
106eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106ec 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 /* Size of eve
106ed 72 79 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 ry page in this
106ee 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73 cache */. int s
106ef 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 zExtra;
106f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
106f1 2a 20 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 * Size of extra
106f2 73 70 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 space for each p
106f3 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 age */. int bPu
106f4 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 rgeable;
106f5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
106f6 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
106f7 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
106f8 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
106f9 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 tress)(void*,PgH
106fa 64 72 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 dr*); /* C
106fb 61 6c 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 all to try make
106fc 61 20 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a a page clean */.
106fd 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b void *pStress;
106fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106ff 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
10700 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a t to xStress */.
10701 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
10702 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pCache;
10703 20 20 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62 /* Pluggab
10704 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 le cache module
10705 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 */. PgHdr *pPag
10706 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 e1;
10707 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 /* Refe
10708 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
10709 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d */.};../*.** Som
1070a 65 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 28 e of the assert(
1070b 29 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 73 ) macros in this
1070c 20 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 78 code are too ex
1070d 70 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a pensive to run.*
1070e 2a 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f * even during no
1070f 72 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e 20 rmal debugging.
10710 20 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72 Use them only r
10711 61 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 arely on long-ru
10712 6e 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20 nning.** tests.
10713 20 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 65 Enable the expe
10714 6e 73 69 76 65 20 61 73 73 65 72 74 73 20 75 73 nsive asserts us
10715 69 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c ing the.** -DSQL
10716 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
10717 53 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 6f SIVE_ASSERT=1 co
10718 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
10719 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c n..*/.#ifdef SQL
1071a 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
1071b 53 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 65 SIVE_ASSERT.# de
1071c 66 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 fine expensive_a
1071d 73 73 65 72 74 28 58 29 20 20 61 73 73 65 72 74 ssert(X) assert
1071e 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 (X).#else.# defi
1071f 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 ne expensive_ass
10720 65 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f ert(X).#endif../
10721 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10722 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10723 2a 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d ** Linked List M
10724 61 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a anagement ******
10725 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10726 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
10727 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 EBUG) && defined
10728 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 (SQLITE_ENABLE_E
10729 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29 XPENSIVE_ASSERT)
1072a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
1072b 74 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53 t the pCache->pS
1072c 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69 ynced variable i
1072d 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e s set correctly.
1072e 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 If it.** is not
1072f 2c 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e , either fail an
10730 20 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72 assert or retur
10731 6e 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73 n zero. Otherwis
10732 65 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e e, return.** non
10733 2d 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f -zero. This is o
10734 6e 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 nly used in debu
10735 67 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 gging builds, as
10736 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
10737 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
10738 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
10739 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b ynced(pCache) );
1073a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
1073b 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 cacheCheckSynced
1073c 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
1073d 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 {. PgHdr *p;.
1073e 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
1073f 69 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61 irtyTail; p!=pCa
10740 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d che->pSynced; p=
10741 70 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0a p->pDirtyPrev){.
10742 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
10743 52 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 Ref || (p->flags
10744 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
10745 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) );. }. retur
10746 6e 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 n (p==0 || p->nR
10747 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 ef || (p->flags&
10748 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
10749 3d 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ==0);.}.#endif /
1074a 2a 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c * !NDEBUG && SQL
1074b 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e ITE_ENABLE_EXPEN
1074c 53 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a SIVE_ASSERT */..
1074d 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67 /*.** Remove pag
1074e 65 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 e pPage from the
1074f 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
10750 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ages..*/.static
10751 76 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f 76 void pcacheRemov
10752 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 50 eFromDirtyList(P
10753 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 gHdr *pPage){.
10754 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 PCache *p = pPag
10755 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 e->pCache;.. as
10756 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
10757 72 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 rtyNext || pPage
10758 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 ==p->pDirtyTail
10759 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
1075a 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 7c ge->pDirtyPrev |
1075b 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 | pPage==p->pDir
1075c 74 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 ty );.. /* Upda
1075d 74 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70 te the PCache1.p
1075e 53 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 Synced variable
1075f 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f if necessary. */
10760 0a 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65 . if( p->pSynce
10761 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 d==pPage ){.
10762 50 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d PgHdr *pSynced =
10763 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
10764 65 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 ev;. while( p
10765 53 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e 63 Synced && (pSync
10766 65 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f ed->flags&PGHDR_
10767 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 NEED_SYNC) ){.
10768 20 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 53 pSynced = pS
10769 79 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 65 ynced->pDirtyPre
1076a 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e v;. }. p->
1076b 70 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 pSynced = pSynce
1076c 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 d;. }.. if( pP
1076d 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 age->pDirtyNext
1076e 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 ){. pPage->pD
1076f 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 irtyNext->pDirty
10770 50 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44 Prev = pPage->pD
10771 69 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c 73 irtyPrev;. }els
10772 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
10773 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54 Page==p->pDirtyT
10774 61 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 ail );. p->pD
10775 69 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 irtyTail = pPage
10776 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 ->pDirtyPrev;.
10777 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 }. if( pPage->p
10778 44 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 20 DirtyPrev ){.
10779 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
1077a 65 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d ev->pDirtyNext =
1077b 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 pPage->pDirtyNe
1077c 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 xt;. }else{.
1077d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d assert( pPage==
1077e 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 20 p->pDirty );.
1077f 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 p->pDirty = pPa
10780 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a ge->pDirtyNext;.
10781 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 }. pPage->pDi
10782 72 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 rtyNext = 0;. p
10783 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 Page->pDirtyPrev
10784 20 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 69 = 0;.. expensi
10785 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 ve_assert( pcach
10786 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 eCheckSynced(p)
10787 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 );.}../*.** Add
10788 70 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 68 page pPage to th
10789 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 e head of the di
1078a 72 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 65 rty list (PCache
1078b 31 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 20 1.pDirty is set
1078c 74 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a 2f to.** pPage)..*/
1078d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
1078e 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 cheAddToDirtyLis
1078f 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b t(PgHdr *pPage){
10790 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 . PCache *p = p
10791 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 Page->pCache;..
10792 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
10793 70 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 26 pDirtyNext==0 &&
10794 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
10795 65 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 72 ev==0 && p->pDir
10796 74 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20 ty!=pPage );..
10797 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
10798 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 t = p->pDirty;.
10799 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 if( pPage->pDir
1079a 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73 tyNext ){. as
1079b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
1079c 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 rtyNext->pDirtyP
1079d 72 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 rev==0 );. pP
1079e 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d age->pDirtyNext-
1079f 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 >pDirtyPrev = pP
107a0 61 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 44 age;. }. p->pD
107a1 69 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 20 irty = pPage;.
107a2 69 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 61 if( !p->pDirtyTa
107a3 69 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 il ){. p->pDi
107a4 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 3b rtyTail = pPage;
107a5 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e 70 . }. if( !p->p
107a6 53 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 50 Synced && 0==(pP
107a7 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 age->flags&PGHDR
107a8 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 _NEED_SYNC) ){.
107a9 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 p->pSynced =
107aa 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 70 pPage;. }. exp
107ab 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 ensive_assert( p
107ac 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 cacheCheckSynced
107ad 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (p) );.}../*.**
107ae 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 Wrapper around t
107af 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 he pluggable cac
107b0 68 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 6f hes xUnpin metho
107b1 64 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 20 d. If the cache
107b2 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 64 is.** being used
107b3 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 for an in-memor
107b4 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 y database, this
107b5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
107b6 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
107b7 76 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 6e void pcacheUnpin
107b8 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 (PgHdr *p){. PC
107b9 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 ache *pCache = p
107ba 2d 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 20 ->pCache;. if(
107bb 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
107bc 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d le ){. if( p-
107bd 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 >pgno==1 ){.
107be 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 pCache->pPage1
107bf 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
107c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
107c1 66 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 fig.pcache.xUnpi
107c2 6e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 n(pCache->pCache
107c3 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a , p, 0);. }.}..
107c4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
107c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
107c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
107c7 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74 **** General Int
107c8 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a erfaces ******.*
107c9 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
107ca 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 and shutdown the
107cb 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 page cache subs
107cc 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f ystem. Neither o
107cd 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 f these .** func
107ce 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 64 tions are thread
107cf 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f safe..*/.SQLITE_
107d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
107d1 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c te3PcacheInitial
107d2 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 ize(void){. if(
107d3 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
107d4 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 nfig.pcache.xIni
107d5 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 t==0 ){. sqli
107d6 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 te3PCacheSetDefa
107d7 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ult();. }. ret
107d8 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
107d9 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
107da 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 Init(sqlite3Glob
107db 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
107dc 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pArg);.}.SQLITE_
107dd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
107de 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f ite3PcacheShutdo
107df 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 wn(void){. if(
107e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
107e1 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 74 fig.pcache.xShut
107e2 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 down ){. sqli
107e3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
107e4 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e pcache.xShutdown
107e5 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
107e6 6e 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 nfig.pcache.pArg
107e7 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
107e8 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
107e9 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 in bytes of a PC
107ea 61 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a ache object..*/.
107eb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
107ec 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
107ed 53 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 75 Size(void){ retu
107ee 72 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 rn sizeof(PCache
107ef 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 ); }../*.** Crea
107f0 74 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 20 te a new PCache
107f1 6f 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 20 object. Storage
107f2 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 space to hold th
107f3 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 20 e object.** has
107f4 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c already been all
107f5 6f 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 61 ocated and is pa
107f6 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 70 ssed in as the p
107f7 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 68 pointer. .** Th
107f8 65 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65 e caller discove
107f9 72 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 rs how much spac
107fa 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c e needs to be al
107fb 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 63 located by .** c
107fc 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 alling sqlite3Pc
107fd 61 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53 acheSize()..*/.S
107fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107ff 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
10800 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 Open(. int szPa
10801 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ge,
10802 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
10803 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 every page */.
10804 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 int szExtra,
10805 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10806 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f Extra space asso
10807 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 ciated with each
10808 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 page */. int b
10809 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 Purgeable,
1080a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1080b 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 if pages are on
1080c 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f backing store */
1080d 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 . int (*xStress
1080e 29 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 2c )(void*,PgHdr*),
1080f 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 /* Call to try t
10810 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 o make pages cle
10811 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 an */. void *pS
10812 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 tress,
10813 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
10814 20 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 to xStress */.
10815 20 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 20 PCache *p
10816 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10817 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 Preallocated sp
10818 61 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 ace for the PCac
10819 68 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65 he */.){. memse
1081a 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 t(p, 0, sizeof(P
1081b 43 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 7a Cache));. p->sz
1081c 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 Page = szPage;.
1081d 20 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a p->szExtra = sz
1081e 45 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 72 Extra;. p->bPur
1081f 67 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 61 geable = bPurgea
10820 62 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 73 ble;. p->xStres
10821 73 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 70 s = xStress;. p
10822 2d 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 72 ->pStress = pStr
10823 65 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d ess;. p->nMax =
10824 20 31 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 100;.}../*.** C
10825 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 hange the page s
10826 69 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 6f ize for PCache o
10827 62 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c 65 bject. The calle
10828 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 r must ensure th
10829 61 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 at there.** are
1082a 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 no outstanding p
1082b 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 age references w
1082c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
1082d 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
1082e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1082f 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10830 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 eSetPageSize(PCa
10831 63 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 che *pCache, int
10832 20 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 65 szPage){. asse
10833 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 rt( pCache->nRef
10834 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 ==0 && pCache->p
10835 44 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66 Dirty==0 );. if
10836 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
10837 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ){. sqlite3G
10838 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
10839 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 he.xDestroy(pCac
1083a 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20 he->pCache);.
1083b 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
1083c 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 = 0;. }. pCach
1083d 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 e->szPage = szPa
1083e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 ge;.}../*.** Try
1083f 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 67 to obtain a pag
10840 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 e from the cache
10841 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10842 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
10843 63 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 43 cacheFetch(. PC
10844 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20 ache *pCache,
10845 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 /* Obtain th
10846 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73 e page from this
10847 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
10848 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
10849 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
1084a 20 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 to obtain */.
1084b 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 int createFlag,
1084c 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
1084d 2c 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66 , create page if
1084e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 it does not exi
1084f 73 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 st already */.
10850 50 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 20 PgHdr **ppPage
10851 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
10852 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a he page here */.
10853 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 ){. PgHdr *pPag
10854 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 72 e = 0;. int eCr
10855 65 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 eate;.. assert(
10856 20 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 pCache!=0 );.
10857 61 73 73 65 72 74 28 20 63 72 65 61 74 65 46 6c assert( createFl
10858 61 67 3d 3d 31 20 7c 7c 20 63 72 65 61 74 65 46 ag==1 || createF
10859 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 lag==0 );. asse
1085a 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 rt( pgno>0 );..
1085b 20 2f 2a 20 49 66 20 74 68 65 20 70 6c 75 67 67 /* If the plugg
1085c 61 62 6c 65 20 63 61 63 68 65 20 28 73 71 6c 69 able cache (sqli
1085d 74 65 33 5f 70 63 61 63 68 65 2a 29 20 68 61 73 te3_pcache*) has
1085e 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 not been alloca
1085f 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 ted,. ** alloca
10860 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a te it now.. */.
10861 20 20 69 66 28 20 21 70 43 61 63 68 65 2d 3e 70 if( !pCache->p
10862 43 61 63 68 65 20 26 26 20 63 72 65 61 74 65 46 Cache && createF
10863 6c 61 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 lag ){. sqlit
10864 65 33 5f 70 63 61 63 68 65 20 2a 70 3b 0a 20 20 e3_pcache *p;.
10865 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 int nByte;.
10866 20 6e 42 79 74 65 20 3d 20 70 43 61 63 68 65 2d nByte = pCache-
10867 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63 68 65 >szPage + pCache
10868 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73 69 7a 65 ->szExtra + size
10869 6f 66 28 50 67 48 64 72 29 3b 0a 20 20 20 20 70 of(PgHdr);. p
1086a 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
1086b 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43 Config.pcache.xC
1086c 72 65 61 74 65 28 6e 42 79 74 65 2c 20 70 43 61 reate(nByte, pCa
1086d 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 29 che->bPurgeable)
1086e 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a ;. if( !p ){.
1086f 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
10870 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
10871 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
10872 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10873 78 43 61 63 68 65 73 69 7a 65 28 70 2c 20 70 43 xCachesize(p, pC
10874 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 ache->nMax);.
10875 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
10876 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65 43 72 65 = p;. }.. eCre
10877 61 74 65 20 3d 20 63 72 65 61 74 65 46 6c 61 67 ate = createFlag
10878 20 2a 20 28 31 20 2b 20 28 21 70 43 61 63 68 65 * (1 + (!pCache
10879 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 ->bPurgeable ||
1087a 21 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 !pCache->pDirty)
1087b 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d );. if( pCache-
1087c 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 70 >pCache ){. p
1087d 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c Page = sqlite3Gl
1087e 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
1087f 65 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d e.xFetch(pCache-
10880 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 65 >pCache, pgno, e
10881 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 Create);. }..
10882 69 66 28 20 21 70 50 61 67 65 20 26 26 20 65 43 if( !pPage && eC
10883 72 65 61 74 65 3d 3d 31 20 29 7b 0a 20 20 20 20 reate==1 ){.
10884 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 20 PgHdr *pPg;..
10885 20 2f 2a 20 46 69 6e 64 20 61 20 64 69 72 74 79 /* Find a dirty
10886 20 70 61 67 65 20 74 6f 20 77 72 69 74 65 2d 6f page to write-o
10887 75 74 20 61 6e 64 20 72 65 63 79 63 6c 65 2e 20 ut and recycle.
10888 46 69 72 73 74 20 74 72 79 20 74 6f 20 66 69 6e First try to fin
10889 64 20 61 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 d a . ** page
1088a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 that does not r
1088b 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c equire a journal
1088c 2d 73 79 6e 63 20 28 6f 6e 65 20 77 69 74 68 20 -sync (one with
1088d 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 0a PGHDR_NEED_SYNC.
1088e 20 20 20 20 2a 2a 20 63 6c 65 61 72 65 64 29 2c ** cleared),
1088f 20 62 75 74 20 69 66 20 74 68 61 74 20 69 73 20 but if that is
10890 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 73 65 74 not possible set
10891 74 6c 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 tle for any othe
10892 72 20 0a 20 20 20 20 2a 2a 20 75 6e 72 65 66 65 r . ** unrefe
10893 72 65 6e 63 65 64 20 64 69 72 74 79 20 70 61 67 renced dirty pag
10894 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 78 e.. */. ex
10895 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 pensive_assert(
10896 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
10897 64 28 70 43 61 63 68 65 29 20 29 3b 0a 20 20 20 d(pCache) );.
10898 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d for(pPg=pCache-
10899 3e 70 53 79 6e 63 65 64 3b 20 0a 20 20 20 20 20 >pSynced; .
1089a 20 20 20 70 50 67 20 26 26 20 28 70 50 67 2d 3e pPg && (pPg->
1089b 6e 52 65 66 20 7c 7c 20 28 70 50 67 2d 3e 66 6c nRef || (pPg->fl
1089c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
1089d 59 4e 43 29 29 3b 20 0a 20 20 20 20 20 20 20 20 YNC)); .
1089e 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 pPg=pPg->pDirtyP
1089f 72 65 76 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 rev. );. i
108a0 66 28 20 21 70 50 67 20 29 7b 0a 20 20 20 20 20 f( !pPg ){.
108a1 20 66 6f 72 28 70 50 67 3d 70 43 61 63 68 65 2d for(pPg=pCache-
108a2 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 50 67 >pDirtyTail; pPg
108a3 20 26 26 20 70 50 67 2d 3e 6e 52 65 66 3b 20 70 && pPg->nRef; p
108a4 50 67 3d 70 50 67 2d 3e 70 44 69 72 74 79 50 72 Pg=pPg->pDirtyPr
108a5 65 76 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ev);. }. i
108a6 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 20 20 f( pPg ){.
108a7 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 int rc;. rc
108a8 20 3d 20 70 43 61 63 68 65 2d 3e 78 53 74 72 65 = pCache->xStre
108a9 73 73 28 70 43 61 63 68 65 2d 3e 70 53 74 72 65 ss(pCache->pStre
108aa 73 73 2c 20 70 50 67 29 3b 0a 20 20 20 20 20 20 ss, pPg);.
108ab 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
108ac 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f K && rc!=SQLITE_
108ad 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 BUSY ){.
108ae 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
108af 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 }. }.. pP
108b0 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f age = sqlite3Glo
108b1 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
108b2 2e 78 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e .xFetch(pCache->
108b3 70 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29 pCache, pgno, 2)
108b4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 ;. }.. if( pPa
108b5 67 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70 ge ){. if( !p
108b6 50 61 67 65 2d 3e 70 44 61 74 61 20 29 7b 0a 20 Page->pData ){.
108b7 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 memset(pPag
108b8 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 e, 0, sizeof(PgH
108b9 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a dr) + pCache->sz
108ba 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 70 50 Extra);. pP
108bb 61 67 65 2d 3e 70 45 78 74 72 61 20 3d 20 28 76 age->pExtra = (v
108bc 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d 3b 0a oid*)&pPage[1];.
108bd 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 44 61 pPage->pDa
108be 74 61 20 3d 20 28 76 6f 69 64 20 2a 29 26 28 28 ta = (void *)&((
108bf 63 68 61 72 20 2a 29 70 50 61 67 65 29 5b 73 69 char *)pPage)[si
108c0 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 zeof(PgHdr) + pC
108c1 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 5d 3b 0a ache->szExtra];.
108c2 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 pPage->pCa
108c3 63 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20 che = pCache;.
108c4 20 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 pPage->pgno
108c5 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 = pgno;. }.
108c6 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
108c7 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 >pCache==pCache
108c8 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
108c9 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f Page->pgno==pgno
108ca 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
108cb 70 50 61 67 65 2d 3e 70 45 78 74 72 61 3d 3d 28 pPage->pExtra==(
108cc 76 6f 69 64 20 2a 29 26 70 50 61 67 65 5b 31 5d void *)&pPage[1]
108cd 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d );.. if( 0==
108ce 70 50 61 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20 pPage->nRef ){.
108cf 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 pCache->nRe
108d0 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 f++;. }. p
108d1 50 61 67 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 Page->nRef++;.
108d2 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b if( pgno==1 ){
108d3 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70 . pCache->p
108d4 50 61 67 65 31 20 3d 20 70 50 61 67 65 3b 0a 20 Page1 = pPage;.
108d5 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70 50 61 }. }. *ppPa
108d6 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 72 65 ge = pPage;. re
108d7 74 75 72 6e 20 28 70 50 61 67 65 3d 3d 30 20 26 turn (pPage==0 &
108d8 26 20 65 43 72 65 61 74 65 29 20 3f 20 53 51 4c & eCreate) ? SQL
108d9 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 ITE_NOMEM : SQLI
108da 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
108db 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 Decrement the re
108dc 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e ference count on
108dd 20 61 20 70 61 67 65 2e 20 49 66 20 74 68 65 20 a page. If the
108de 70 61 67 65 20 69 73 20 63 6c 65 61 6e 20 61 6e page is clean an
108df 64 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e d the.** referen
108e0 63 65 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 ce count drops t
108e1 6f 20 30 2c 20 74 68 65 6e 20 69 74 20 69 73 20 o 0, then it is
108e2 6d 61 64 65 20 65 6c 69 62 6c 65 20 66 6f 72 20 made elible for
108e3 72 65 63 79 63 6c 69 6e 67 2e 0a 2a 2f 0a 53 51 recycling..*/.SQ
108e4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
108e5 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 d sqlite3PcacheR
108e6 65 6c 65 61 73 65 28 50 67 48 64 72 20 2a 70 29 elease(PgHdr *p)
108e7 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e {. assert( p->n
108e8 52 65 66 3e 30 20 29 3b 0a 20 20 70 2d 3e 6e 52 Ref>0 );. p->nR
108e9 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e ef--;. if( p->n
108ea 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 43 Ref==0 ){. PC
108eb 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 ache *pCache = p
108ec 2d 3e 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43 ->pCache;. pC
108ed 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 ache->nRef--;.
108ee 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 if( (p->flags&
108ef 50 47 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 PGHDR_DIRTY)==0
108f0 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 ){. pcacheU
108f1 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c npin(p);. }el
108f2 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 se{. /* Mov
108f3 65 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 e the page to th
108f4 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 e head of the di
108f5 72 74 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 rty list. */.
108f6 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 pcacheRemoveF
108f7 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b romDirtyList(p);
108f8 0a 20 20 20 20 20 20 70 63 61 63 68 65 41 64 64 . pcacheAdd
108f9 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a ToDirtyList(p);.
108fa 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
108fb 2a 2a 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 ** Increase the
108fc 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
108fd 6f 66 20 61 20 73 75 70 70 6c 69 65 64 20 70 61 of a supplied pa
108fe 67 65 20 62 79 20 31 2e 0a 2a 2f 0a 53 51 4c 49 ge by 1..*/.SQLI
108ff 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10900 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
10901 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 (PgHdr *p){. as
10902 73 65 72 74 28 70 2d 3e 6e 52 65 66 3e 30 29 3b sert(p->nRef>0);
10903 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a . p->nRef++;.}.
10904 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61 ./*.** Drop a pa
10905 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 ge from the cach
10906 65 2e 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 e. There must be
10907 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 72 65 66 exactly one ref
10908 65 72 65 6e 63 65 20 74 6f 20 74 68 65 0a 2a 2a erence to the.**
10909 20 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 page. This func
1090a 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 61 tion deletes tha
1090b 74 20 72 65 66 65 72 65 6e 63 65 2c 20 73 6f 20 t reference, so
1090c 61 66 74 65 72 20 69 74 20 72 65 74 75 72 6e 73 after it returns
1090d 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 6f 69 the.** page poi
1090e 6e 74 65 64 20 74 6f 20 62 79 20 70 20 69 73 20 nted to by p is
1090f 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49 invalid..*/.SQLI
10910 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10911 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
10912 70 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 p(PgHdr *p){. P
10913 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 Cache *pCache;.
10914 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
10915 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e ==1 );. if( p->
10916 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
10917 59 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 Y ){. pcacheR
10918 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 emoveFromDirtyLi
10919 73 74 28 70 29 3b 0a 20 20 7d 0a 20 20 70 43 61 st(p);. }. pCa
1091a 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
1091b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d . pCache->nRef-
1091c 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f -;. if( p->pgno
1091d 3d 3d 31 20 29 7b 0a 20 20 20 20 70 43 61 63 68 ==1 ){. pCach
1091e 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 e->pPage1 = 0;.
1091f 20 7d 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 }. sqlite3Glob
10920 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10921 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70 xUnpin(pCache->p
10922 43 61 63 68 65 2c 20 70 2c 20 31 29 3b 0a 7d 0a Cache, p, 1);.}.
10923 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
10924 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 the page is mar
10925 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 49 66 ked as dirty. If
10926 20 69 74 20 69 73 6e 27 74 20 64 69 72 74 79 20 it isn't dirty
10927 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 already,.** make
10928 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 it so..*/.SQLIT
10929 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1092a 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1092b 44 69 72 74 79 28 50 67 48 64 72 20 2a 70 29 7b Dirty(PgHdr *p){
1092c 0a 20 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e . p->flags &= ~
1092d 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 PGHDR_DONT_WRITE
1092e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
1092f 52 65 66 3e 30 20 29 3b 0a 20 20 69 66 28 20 30 Ref>0 );. if( 0
10930 3d 3d 28 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 ==(p->flags & PG
10931 48 44 52 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 HDR_DIRTY) ){.
10932 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 p->flags |= PG
10933 48 44 52 5f 44 49 52 54 59 3b 0a 20 20 20 20 70 HDR_DIRTY;. p
10934 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c cacheAddToDirtyL
10935 69 73 74 28 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a ist( p);. }.}..
10936 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
10937 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b the page is mark
10938 65 64 20 61 73 20 63 6c 65 61 6e 2e 20 49 66 20 ed as clean. If
10939 69 74 20 69 73 6e 27 74 20 63 6c 65 61 6e 20 61 it isn't clean a
1093a 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61 6b 65 20 lready,.** make
1093b 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 it so..*/.SQLITE
1093c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1093d 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
1093e 6c 65 61 6e 28 50 67 48 64 72 20 2a 70 29 7b 0a lean(PgHdr *p){.
1093f 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 if( (p->flags
10940 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 29 & PGHDR_DIRTY) )
10941 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f {. pcacheRemo
10942 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 veFromDirtyList(
10943 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 p);. p->flags
10944 20 26 3d 20 7e 28 50 47 48 44 52 5f 44 49 52 54 &= ~(PGHDR_DIRT
10945 59 7c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e Y|PGHDR_NEED_SYN
10946 43 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e C);. if( p->n
10947 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ref==0 ){.
10948 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a pcacheUnpin(p);.
10949 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1094a 2a 2a 20 4d 61 6b 65 20 65 76 65 72 79 20 70 61 ** Make every pa
1094b 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 ge in the cache
1094c 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 clean..*/.SQLITE
1094d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1094e 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
1094f 41 6c 6c 28 50 43 61 63 68 65 20 2a 70 43 61 63 All(PCache *pCac
10950 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b he){. PgHdr *p;
10951 0a 20 20 77 68 69 6c 65 28 20 28 70 20 3d 20 70 . while( (p = p
10952 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 21 3d Cache->pDirty)!=
10953 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
10954 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 PcacheMakeClean(
10955 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
10956 20 43 6c 65 61 72 20 74 68 65 20 50 47 48 44 52 Clear the PGHDR
10957 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 _NEED_SYNC flag
10958 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74 79 20 70 from all dirty p
10959 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ages..*/.SQLITE_
1095a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1095b 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 ite3PcacheClearS
1095c 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20 yncFlags(PCache
1095d 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 *pCache){. PgHd
1095e 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 r *p;. for(p=pC
1095f 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b ache->pDirty; p;
10960 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 p=p->pDirtyNext
10961 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 ){. p->flags
10962 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 &= ~PGHDR_NEED_S
10963 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 YNC;. }. pCach
10964 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 43 61 e->pSynced = pCa
10965 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b che->pDirtyTail;
10966 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
10967 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
10968 20 6f 66 20 70 61 67 65 20 70 20 74 6f 20 6e 65 of page p to ne
10969 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 51 4c 49 54 wPgno. .*/.SQLIT
1096a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1096b 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
1096c 28 50 67 48 64 72 20 2a 70 2c 20 50 67 6e 6f 20 (PgHdr *p, Pgno
1096d 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50 43 61 63 newPgno){. PCac
1096e 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e he *pCache = p->
1096f 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 pCache;. assert
10970 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
10971 20 61 73 73 65 72 74 28 20 6e 65 77 50 67 6e 6f assert( newPgno
10972 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 >0 );. sqlite3G
10973 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
10974 68 65 2e 78 52 65 6b 65 79 28 70 43 61 63 68 65 he.xRekey(pCache
10975 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 70 2d 3e ->pCache, p, p->
10976 70 67 6e 6f 2c 20 6e 65 77 50 67 6e 6f 29 3b 0a pgno, newPgno);.
10977 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50 p->pgno = newP
10978 67 6e 6f 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 gno;. if( (p->f
10979 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
1097a 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 26 50 ) && (p->flags&P
1097b 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 GHDR_NEED_SYNC)
1097c 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d ){. pcacheRem
1097d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 oveFromDirtyList
1097e 28 70 29 3b 0a 20 20 20 20 70 63 61 63 68 65 41 (p);. pcacheA
1097f 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28 70 29 ddToDirtyList(p)
10980 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
10981 72 6f 70 20 65 76 65 72 79 20 63 61 63 68 65 20 rop every cache
10982 65 6e 74 72 79 20 77 68 6f 73 65 20 70 61 67 65 entry whose page
10983 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 number is great
10984 65 72 20 74 68 61 6e 20 22 70 67 6e 6f 22 2e 20 er than "pgno".
10985 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 6d 75 The.** caller mu
10986 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 st ensure that t
10987 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 here are no outs
10988 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
10989 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 0a es to any pages.
1098a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 70 61 ** other than pa
1098b 67 65 20 31 20 77 69 74 68 20 61 20 70 61 67 65 ge 1 with a page
1098c 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 number greater
1098d 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a 2a 0a 2a 2a than pgno..**.**
1098e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 72 If there is a r
1098f 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 eference to page
10990 20 31 20 61 6e 64 20 74 68 65 20 70 67 6e 6f 20 1 and the pgno
10991 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 parameter passed
10992 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 to this.** func
10993 74 69 6f 6e 20 69 73 20 30 2c 20 74 68 65 6e 20 tion is 0, then
10994 74 68 65 20 64 61 74 61 20 61 72 65 61 20 61 73 the data area as
10995 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61 sociated with pa
10996 67 65 20 31 20 69 73 20 7a 65 72 6f 65 64 2c 20 ge 1 is zeroed,
10997 62 75 74 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 but.** the page
10998 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 64 72 object is not dr
10999 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 opped..*/.SQLITE
1099a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1099b 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 lite3PcacheTrunc
1099c 61 74 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 ate(PCache *pCac
1099d 68 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a he, Pgno pgno){.
1099e 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 if( pCache->pC
1099f 61 63 68 65 20 29 7b 0a 20 20 20 20 50 67 48 64 ache ){. PgHd
109a0 72 20 2a 70 3b 0a 20 20 20 20 50 67 48 64 72 20 r *p;. PgHdr
109a1 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66 6f 72 28 *pNext;. for(
109a2 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 p=pCache->pDirty
109a3 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 ; p; p=pNext){.
109a4 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e pNext = p->
109a5 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 20 20 pDirtyNext;.
109a6 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3e 70 67 if( p->pgno>pg
109a7 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 no ){. as
109a8 73 65 72 74 28 20 70 2d 3e 66 6c 61 67 73 26 50 sert( p->flags&P
109a9 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 GHDR_DIRTY );.
109aa 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 sqlite3Pca
109ab 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 29 3b cheMakeClean(p);
109ac 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
109ad 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 26 if( pgno==0 &
109ae 26 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 & pCache->pPage1
109af 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ){. memset
109b0 28 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 2d (pCache->pPage1-
109b1 3e 70 44 61 74 61 2c 20 30 2c 20 70 43 61 63 68 >pData, 0, pCach
109b2 65 2d 3e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 e->szPage);.
109b3 20 20 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20 20 pgno = 1;.
109b4 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f }. sqlite3Glo
109b5 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
109b6 2e 78 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 .xTruncate(pCach
109b7 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f 2b e->pCache, pgno+
109b8 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 1);. }.}../*.**
109b9 20 43 6c 6f 73 65 20 61 20 63 61 63 68 65 2e 0a Close a cache..
109ba 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
109bb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
109bc 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 68 65 acheClose(PCache
109bd 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 66 28 *pCache){. if(
109be 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
109bf 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
109c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
109c1 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68 e.xDestroy(pCach
109c2 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a e->pCache);. }.
109c3 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 73 63 61 72 }../* .** Discar
109c4 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f d the contents o
109c5 66 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a f the cache..*/.
109c6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
109c7 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
109c8 65 43 6c 65 61 72 28 50 43 61 63 68 65 20 2a 70 eClear(PCache *p
109c9 43 61 63 68 65 29 7b 0a 20 20 73 71 6c 69 74 65 Cache){. sqlite
109ca 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 3PcacheTruncate(
109cb 70 43 61 63 68 65 2c 20 30 29 3b 0a 7d 0a 0a 2f pCache, 0);.}../
109cc 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c *.** Merge two l
109cd 69 73 74 73 20 6f 66 20 70 61 67 65 73 20 63 6f ists of pages co
109ce 6e 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 nnected by pDirt
109cf 79 20 61 6e 64 20 69 6e 20 70 67 6e 6f 20 6f 72 y and in pgno or
109d0 64 65 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 62 der..** Do not b
109d1 6f 74 68 20 66 69 78 69 6e 67 20 74 68 65 20 70 oth fixing the p
109d2 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 65 DirtyPrev pointe
109d3 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 rs..*/.static Pg
109d4 48 64 72 20 2a 70 63 61 63 68 65 4d 65 72 67 65 Hdr *pcacheMerge
109d5 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 DirtyList(PgHdr
109d6 2a 70 41 2c 20 50 67 48 64 72 20 2a 70 42 29 7b *pA, PgHdr *pB){
109d7 0a 20 20 50 67 48 64 72 20 72 65 73 75 6c 74 2c . PgHdr result,
109d8 20 2a 70 54 61 69 6c 3b 0a 20 20 70 54 61 69 6c *pTail;. pTail
109d9 20 3d 20 26 72 65 73 75 6c 74 3b 0a 20 20 77 68 = &result;. wh
109da 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b ile( pA && pB ){
109db 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 70 67 6e . if( pA->pgn
109dc 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 o<pB->pgno ){.
109dd 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 pTail->pDirt
109de 79 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 70 54 y = pA;. pT
109df 61 69 6c 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 ail = pA;.
109e0 70 41 20 3d 20 70 41 2d 3e 70 44 69 72 74 79 3b pA = pA->pDirty;
109e1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
109e2 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 pTail->pDirty
109e3 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 54 61 69 = pB;. pTai
109e4 6c 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 l = pB;. pB
109e5 20 3d 20 70 42 2d 3e 70 44 69 72 74 79 3b 0a 20 = pB->pDirty;.
109e6 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 }. }. if( p
109e7 41 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e A ){. pTail->
109e8 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 7d pDirty = pA;. }
109e9 65 6c 73 65 20 69 66 28 20 70 42 20 29 7b 0a 20 else if( pB ){.
109ea 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 pTail->pDirty
109eb 20 3d 20 70 42 3b 0a 20 20 7d 65 6c 73 65 7b 0a = pB;. }else{.
109ec 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 pTail->pDirt
109ed 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 y = 0;. }. ret
109ee 75 72 6e 20 72 65 73 75 6c 74 2e 70 44 69 72 74 urn result.pDirt
109ef 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 y;.}../*.** Sort
109f0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 70 61 67 the list of pag
109f1 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 es in accending
109f2 6f 72 64 65 72 20 62 79 20 70 67 6e 6f 2e 20 20 order by pgno.
109f3 50 61 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 6e Pages are.** con
109f4 6e 65 63 74 65 64 20 62 79 20 70 44 69 72 74 79 nected by pDirty
109f5 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 20 pointers. The
109f6 70 44 69 72 74 79 50 72 65 76 20 70 6f 69 6e 74 pDirtyPrev point
109f7 65 72 73 20 61 72 65 0a 2a 2a 20 63 6f 72 72 75 ers are.** corru
109f8 70 74 65 64 20 62 79 20 74 68 69 73 20 73 6f 72 pted by this sor
109f9 74 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74 t..**.** Since t
109fa 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d here cannot be m
109fb 6f 72 65 20 74 68 61 6e 20 32 5e 33 31 20 64 69 ore than 2^31 di
109fc 73 74 69 6e 63 74 20 70 61 67 65 73 20 69 6e 20 stinct pages in
109fd 61 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 74 a database,.** t
109fe 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 6d here cannot be m
109ff 6f 72 65 20 74 68 61 6e 20 33 31 20 62 75 63 6b ore than 31 buck
10a00 65 74 73 20 72 65 71 75 69 72 65 64 20 62 79 20 ets required by
10a01 74 68 65 20 6d 65 72 67 65 20 73 6f 72 74 65 72 the merge sorter
10a02 2e 0a 2a 2a 20 4f 6e 65 20 65 78 74 72 61 20 62 ..** One extra b
10a03 75 63 6b 65 74 20 69 73 20 61 64 64 65 64 20 74 ucket is added t
10a04 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 o catch overflow
10a05 20 69 6e 20 63 61 73 65 20 73 6f 6d 65 74 68 69 in case somethi
10a06 6e 67 0a 2a 2a 20 65 76 65 72 20 63 68 61 6e 67 ng.** ever chang
10a07 65 73 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 70 es to make the p
10a08 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 revious sentence
10a09 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 23 incorrect..*/.#
10a0a 64 65 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 define N_SORT_BU
10a0b 43 4b 45 54 20 20 33 32 0a 73 74 61 74 69 63 20 CKET 32.static
10a0c 50 67 48 64 72 20 2a 70 63 61 63 68 65 53 6f 72 PgHdr *pcacheSor
10a0d 74 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 tDirtyList(PgHdr
10a0e 20 2a 70 49 6e 29 7b 0a 20 20 50 67 48 64 72 20 *pIn){. PgHdr
10a0f 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 *a[N_SORT_BUCKET
10a10 5d 2c 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a ], *p;. int i;.
10a11 20 20 6d 65 6d 73 65 74 28 61 2c 20 30 2c 20 73 memset(a, 0, s
10a12 69 7a 65 6f 66 28 61 29 29 3b 0a 20 20 77 68 69 izeof(a));. whi
10a13 6c 65 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 le( pIn ){. p
10a14 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20 = pIn;. pIn
10a15 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 = p->pDirty;.
10a16 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a p->pDirty = 0;.
10a17 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57 for(i=0; ALW
10a18 41 59 53 28 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 AYS(i<N_SORT_BUC
10a19 4b 45 54 2d 31 29 3b 20 69 2b 2b 29 7b 0a 20 20 KET-1); i++){.
10a1a 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20 if( a[i]==0
10a1b 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 ){. a[i]
10a1c 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 72 65 = p;. bre
10a1d 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ak;. }else{
10a1e 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 63 61 . p = pca
10a1f 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
10a20 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20 t(a[i], p);.
10a21 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20 a[i] = 0;.
10a22 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
10a23 69 66 28 20 4e 45 56 45 52 28 69 3d 3d 4e 5f 53 if( NEVER(i==N_S
10a24 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 20 29 7b ORT_BUCKET-1) ){
10a25 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 67 65 74 . /* To get
10a26 20 68 65 72 65 2c 20 74 68 65 72 65 20 6e 65 65 here, there nee
10a27 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f 53 4f 52 d to be 2^(N_SOR
10a28 54 5f 42 55 43 4b 45 54 29 20 65 6c 65 6d 65 6e T_BUCKET) elemen
10a29 74 73 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 74 ts in. ** t
10a2a 68 65 20 69 6e 70 75 74 20 6c 69 73 74 2e 20 20 he input list.
10a2b 42 75 74 20 74 68 61 74 20 69 73 20 69 6d 70 6f But that is impo
10a2c 73 73 69 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f ssible.. */
10a2d 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 63 . a[i] = pc
10a2e 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 acheMergeDirtyLi
10a2f 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 st(a[i], p);.
10a30 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30 }. }. p = a[0
10a31 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c ];. for(i=1; i<
10a32 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69 N_SORT_BUCKET; i
10a33 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 61 ++){. p = pca
10a34 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 cheMergeDirtyLis
10a35 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d 0a t(p, a[i]);. }.
10a36 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
10a37 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 *.** Return a li
10a38 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 st of all dirty
10a39 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 pages in the cac
10a3a 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 he, sorted by pa
10a3b 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51 ge number..*/.SQ
10a3c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 LITE_PRIVATE PgH
10a3d 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 dr *sqlite3Pcach
10a3e 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 eDirtyList(PCach
10a3f 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 e *pCache){. Pg
10a40 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d Hdr *p;. for(p=
10a41 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 pCache->pDirty;
10a42 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 p; p=p->pDirtyNe
10a43 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 xt){. p->pDir
10a44 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 ty = p->pDirtyNe
10a45 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e xt;. }. return
10a46 20 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 pcacheSortDirty
10a47 4c 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69 List(pCache->pDi
10a48 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 rty);.}../* .**
10a49 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
10a4a 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 number of refer
10a4b 65 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c 64 enced pages held
10a4c 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a 2a by the cache..*
10a4d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10a4e 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
10a4f 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68 heRefCount(PCach
10a50 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 e *pCache){. re
10a51 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65 turn pCache->nRe
10a52 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 f;.}../*.** Retu
10a53 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
10a54 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
10a55 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64 he page supplied
10a56 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
10a57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10a58 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
10a59 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 achePageRefcount
10a5a 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 65 (PgHdr *p){. re
10a5b 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d 0a turn p->nRef;.}.
10a5c 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ./* .** Return t
10a5d 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
10a5e 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
10a5f 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
10a60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10a61 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f ite3PcachePageco
10a62 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 unt(PCache *pCac
10a63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 he){. int nPage
10a64 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 63 = 0;. if( pCac
10a65 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 he->pCache ){.
10a66 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 65 nPage = sqlite
10a67 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10a68 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28 ache.xPagecount(
10a69 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b pCache->pCache);
10a6a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50 . }. return nP
10a6b 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 age;.}..#ifdef S
10a6c 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
10a6d 20 47 65 74 20 74 68 65 20 73 75 67 67 65 73 74 Get the suggest
10a6e 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 ed cache-size va
10a6f 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 lue..*/.SQLITE_P
10a70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
10a71 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 e3PcacheGetCache
10a72 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 size(PCache *pCa
10a73 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 che){. return p
10a74 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a 23 Cache->nMax;.}.#
10a75 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 endif../*.** Set
10a76 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 the suggested c
10a77 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e ache-size value.
10a78 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10a79 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
10a7a 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a cacheSetCachesiz
10a7b 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 e(PCache *pCache
10a7c 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
10a7d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 pCache->nMax =
10a7e 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 43 mxPage;. if( pC
10a7f 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a ache->pCache ){.
10a80 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
10a81 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 lConfig.pcache.x
10a82 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 65 Cachesize(pCache
10a83 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 65 ->pCache, mxPage
10a84 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65 );. }.}..#if de
10a85 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45 fined(SQLITE_CHE
10a86 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66 CK_PAGES) || def
10a87 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
10a88 47 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c G)./*.** For all
10a89 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 dirty pages cur
10a8a 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61 rently in the ca
10a8b 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 che, invoke the
10a8c 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c specified.** cal
10a8d 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f lback. This is o
10a8e 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 nly used if the
10a8f 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
10a90 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64 ES macro is.** d
10a91 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
10a92 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
10a93 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 qlite3PcacheIter
10a94 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 ateDirty(PCache
10a95 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a *pCache, void (*
10a96 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 xIter)(PgHdr *))
10a97 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 {. PgHdr *pDirt
10a98 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d y;. for(pDirty=
10a99 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 pCache->pDirty;
10a9a 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70 pDirty; pDirty=p
10a9b 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78 Dirty->pDirtyNex
10a9c 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44 t){. xIter(pD
10a9d 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e irty);. }.}.#en
10a9e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
10a9f 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 **** End of pcac
10aa0 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a he.c ***********
10aa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10aa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10aa3 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
10aa4 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
10aa5 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a pcache1.c ******
10aa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10aa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10aa8 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
10aa9 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a November 05.**.*
10aaa 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
10aab 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
10aac 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
10aad 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
10aae 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
10aaf 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
10ab0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
10ab1 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
10ab2 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
10ab3 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
10ab4 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
10ab5 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
10ab6 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
10ab7 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
10ab8 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
10ab9 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
10aba 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
10abb 0a 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 2a 2a 2a 2a 2a 2a 2a ****************
10abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10abf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
10ac0 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
10ac1 6d 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c ments the defaul
10ac2 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 t page cache imp
10ac3 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65 lementation (the
10ac4 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 .** sqlite3_pcac
10ac5 68 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49 he interface). I
10ac6 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 t also contains
10ac7 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c part of the impl
10ac8 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 ementation.** of
10ac9 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 the SQLITE_CONF
10aca 49 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64 IG_PAGECACHE and
10acb 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
10acc 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72 _memory() featur
10acd 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 es..** If the de
10ace 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 fault page cache
10acf 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
10ad0 69 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68 is overriden, th
10ad1 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a en neither of.**
10ad2 20 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75 these two featu
10ad3 72 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c res are availabl
10ad4 65 2e 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 66 20 e..*/...typedef
10ad5 73 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50 struct PCache1 P
10ad6 43 61 63 68 65 31 3b 0a 74 79 70 65 64 65 66 20 Cache1;.typedef
10ad7 73 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67 struct PgHdr1 Pg
10ad8 48 64 72 31 3b 0a 74 79 70 65 64 65 66 20 73 74 Hdr1;.typedef st
10ad9 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 ruct PgFreeslot
10ada 50 67 46 72 65 65 73 6c 6f 74 3b 0a 0a 2f 2a 20 PgFreeslot;../*
10adb 50 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 75 Pointers to stru
10adc 63 74 75 72 65 73 20 6f 66 20 74 68 69 73 20 74 ctures of this t
10add 79 70 65 20 61 72 65 20 63 61 73 74 20 61 6e 64 ype are cast and
10ade 20 72 65 74 75 72 6e 65 64 20 61 73 20 0a 2a 2a returned as .**
10adf 20 6f 70 61 71 75 65 20 73 71 6c 69 74 65 33 5f opaque sqlite3_
10ae0 70 63 61 63 68 65 2a 20 68 61 6e 64 6c 65 73 0a pcache* handles.
10ae1 2a 2f 0a 73 74 72 75 63 74 20 50 43 61 63 68 65 */.struct PCache
10ae2 31 20 7b 0a 20 20 2f 2a 20 43 61 63 68 65 20 63 1 {. /* Cache c
10ae3 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70 61 72 onfiguration par
10ae4 61 6d 65 74 65 72 73 2e 20 50 61 67 65 20 73 69 ameters. Page si
10ae5 7a 65 20 28 73 7a 50 61 67 65 29 20 61 6e 64 20 ze (szPage) and
10ae6 74 68 65 20 70 75 72 67 65 61 62 6c 65 0a 20 20 the purgeable.
10ae7 2a 2a 20 66 6c 61 67 20 28 62 50 75 72 67 65 61 ** flag (bPurgea
10ae8 62 6c 65 29 20 61 72 65 20 73 65 74 20 77 68 65 ble) are set whe
10ae9 6e 20 74 68 65 20 63 61 63 68 65 20 69 73 20 63 n the cache is c
10aea 72 65 61 74 65 64 2e 20 6e 4d 61 78 20 6d 61 79 reated. nMax may
10aeb 20 62 65 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 be . ** modifi
10aec 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62 ed at any time b
10aed 79 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 y a call to the
10aee 70 63 61 63 68 65 31 43 61 63 68 65 53 69 7a 65 pcache1CacheSize
10aef 28 29 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2a 20 () method.. **
10af0 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10af1 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10af2 65 6e 20 61 63 63 65 73 73 69 6e 67 20 6e 4d 61 en accessing nMa
10af3 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 7a x.. */. int sz
10af4 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
10af5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10af6 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 Size of allocat
10af7 65 64 20 70 61 67 65 73 20 69 6e 20 62 79 74 65 ed pages in byte
10af8 73 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 s */. int bPurg
10af9 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 eable;
10afa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
10afb 75 65 20 69 66 20 63 61 63 68 65 20 69 73 20 70 ue if cache is p
10afc 75 72 67 65 61 62 6c 65 20 2a 2f 0a 20 20 75 6e urgeable */. un
10afd 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e 3b signed int nMin;
10afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10aff 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d /* Minimum num
10b00 62 65 72 20 6f 66 20 70 61 67 65 73 20 72 65 73 ber of pages res
10b01 65 72 76 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 erved */. unsig
10b02 6e 65 64 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 ned int nMax;
10b03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b04 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 22 63 61 * Configured "ca
10b05 63 68 65 5f 73 69 7a 65 22 20 76 61 6c 75 65 20 che_size" value
10b06 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73 68 20 74 61 */.. /* Hash ta
10b07 62 6c 65 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 ble of all pages
10b08 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 . The following
10b09 76 61 72 69 61 62 6c 65 73 20 6d 61 79 20 6f 6e variables may on
10b0a 6c 79 20 62 65 20 61 63 63 65 73 73 65 64 0a 20 ly be accessed.
10b0b 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 61 63 63 ** when the acc
10b0c 65 73 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 essor is holding
10b0d 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 the global mute
10b0e 78 20 28 73 65 65 20 70 63 61 63 68 65 31 45 6e x (see pcache1En
10b0f 74 65 72 4d 75 74 65 78 28 29 20 0a 20 20 2a 2a terMutex() . **
10b10 20 61 6e 64 20 70 63 61 63 68 65 31 4c 65 61 76 and pcache1Leav
10b11 65 4d 75 74 65 78 28 29 29 2e 0a 20 20 2a 2f 0a eMutex()).. */.
10b12 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
10b13 52 65 63 79 63 6c 61 62 6c 65 3b 20 20 20 20 20 Recyclable;
10b14 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10b15 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
10b16 4c 52 55 20 6c 69 73 74 20 2a 2f 0a 20 20 75 6e LRU list */. un
10b17 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 signed int nPage
10b18 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10b19 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 /* Total numbe
10b1a 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 r of pages in ap
10b1b 48 61 73 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e Hash */. unsign
10b1c 65 64 20 69 6e 74 20 6e 48 61 73 68 3b 20 20 20 ed int nHash;
10b1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b1e 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
10b1f 20 69 6e 20 61 70 48 61 73 68 5b 5d 20 2a 2f 0a in apHash[] */.
10b20 20 20 50 67 48 64 72 31 20 2a 2a 61 70 48 61 73 PgHdr1 **apHas
10b21 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
10b22 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 /* Hash ta
10b23 62 6c 65 20 66 6f 72 20 66 61 73 74 20 6c 6f 6f ble for fast loo
10b24 6b 75 70 20 62 79 20 6b 65 79 20 2a 2f 0a 0a 20 kup by key */..
10b25 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4d unsigned int iM
10b26 61 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 axKey;
10b27 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 /* Largest
10b28 6b 65 79 20 73 65 65 6e 20 73 69 6e 63 65 20 78 key seen since x
10b29 54 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a 7d 3b Truncate() */.};
10b2a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63 61 63 ../*.** Each cac
10b2b 68 65 20 65 6e 74 72 79 20 69 73 20 72 65 70 72 he entry is repr
10b2c 65 73 65 6e 74 65 64 20 62 79 20 61 6e 20 69 6e esented by an in
10b2d 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
10b2e 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 llowing .** stru
10b2f 63 74 75 72 65 2e 20 41 20 62 75 66 66 65 72 20 cture. A buffer
10b30 6f 66 20 50 67 48 64 72 31 2e 70 43 61 63 68 65 of PgHdr1.pCache
10b31 2d 3e 73 7a 50 61 67 65 20 62 79 74 65 73 20 69 ->szPage bytes i
10b32 73 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 s allocated .**
10b33 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 directly before
10b34 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 this structure i
10b35 6e 20 6d 65 6d 6f 72 79 20 28 73 65 65 20 74 68 n memory (see th
10b36 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 e PGHDR1_TO_PAGE
10b37 28 29 20 0a 2a 2a 20 6d 61 63 72 6f 20 62 65 6c () .** macro bel
10b38 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 ow)..*/.struct P
10b39 67 48 64 72 31 20 7b 0a 20 20 75 6e 73 69 67 6e gHdr1 {. unsign
10b3a 65 64 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20 ed int iKey;
10b3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 /* Key
10b3c 76 61 6c 75 65 20 28 70 61 67 65 20 6e 75 6d 62 value (page numb
10b3d 65 72 29 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 er) */. PgHdr1
10b3e 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 *pNext;
10b3f 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 /* Next
10b40 69 6e 20 68 61 73 68 20 74 61 62 6c 65 20 63 68 in hash table ch
10b41 61 69 6e 20 2a 2f 0a 20 20 50 43 61 63 68 65 31 ain */. PCache1
10b42 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 *pCache;
10b43 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 /* Cache
10b44 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 that currently
10b45 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a owns this page *
10b46 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
10b47 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 Next;
10b48 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 4c 52 /* Next in LR
10b49 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e U list of unpinn
10b4a 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 ed pages */. Pg
10b4b 48 64 72 31 20 2a 70 4c 72 75 50 72 65 76 3b 20 Hdr1 *pLruPrev;
10b4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b4d 50 72 65 76 69 6f 75 73 20 69 6e 20 4c 52 55 20 Previous in LRU
10b4e 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 list of unpinned
10b4f 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pages */.};../*
10b50 0a 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69 .** Free slots i
10b51 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20 n the allocator
10b52 75 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75 used to divide u
10b53 70 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f p the buffer pro
10b54 76 69 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 vided using.** t
10b55 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 he SQLITE_CONFIG
10b56 5f 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61 _PAGECACHE mecha
10b57 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 nism..*/.struct
10b58 50 67 46 72 65 65 73 6c 6f 74 20 7b 0a 20 20 50 PgFreeslot {. P
10b59 67 46 72 65 65 73 6c 6f 74 20 2a 70 4e 65 78 74 gFreeslot *pNext
10b5a 3b 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 ; /* Next free
10b5b 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a slot */.};../*.*
10b5c 2a 20 47 6c 6f 62 61 6c 20 64 61 74 61 20 75 73 * Global data us
10b5d 65 64 20 62 79 20 74 68 69 73 20 63 61 63 68 65 ed by this cache
10b5e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 ..*/.static SQLI
10b5f 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 50 43 TE_WSD struct PC
10b60 61 63 68 65 47 6c 6f 62 61 6c 20 7b 0a 20 20 73 acheGlobal {. s
10b61 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
10b62 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 tex;
10b63 20 20 20 2f 2a 20 73 74 61 74 69 63 20 6d 75 74 /* static mut
10b64 65 78 20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f ex MUTEX_STATIC_
10b65 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d LRU */.. int nM
10b66 61 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 axPage;
10b67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10b68 20 53 75 6d 20 6f 66 20 6e 4d 61 78 50 61 67 65 Sum of nMaxPage
10b69 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 for purgeable c
10b6a 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e aches */. int n
10b6b 4d 69 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 MinPage;
10b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10b6d 2a 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 50 61 67 * Sum of nMinPag
10b6e 65 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 e for purgeable
10b6f 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 caches */. int
10b70 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 20 20 20 nCurrentPage;
10b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b72 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 75 72 /* Number of pur
10b73 67 65 61 62 6c 65 20 70 61 67 65 73 20 61 6c 6c geable pages all
10b74 6f 63 61 74 65 64 20 2a 2f 0a 20 20 50 67 48 64 ocated */. PgHd
10b75 72 31 20 2a 70 4c 72 75 48 65 61 64 2c 20 2a 70 r1 *pLruHead, *p
10b76 4c 72 75 54 61 69 6c 3b 20 20 20 20 20 20 20 20 LruTail;
10b77 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 /* LRU list of u
10b78 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f npinned pages */
10b79 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 .. /* Variables
10b7a 20 72 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49 related to SQLI
10b7b 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
10b7c 43 48 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f CHE settings. */
10b7d 0a 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20 . int szSlot;
10b7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b7f 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
10b80 66 20 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74 f each free slot
10b81 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 */. void *pSta
10b82 72 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 rt, *pEnd;
10b83 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 /* Bou
10b84 6e 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 65 nds of pagecache
10b85 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f malloc range */
10b86 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 . PgFreeslot *p
10b87 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 Free;
10b88 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70 /* Free p
10b89 61 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 age blocks */.
10b8a 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 int isInit;
10b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b8c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
10b8d 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 7d 20 nitialized */.}
10b8e 70 63 61 63 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a pcache1_g;../*.*
10b8f 2a 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 68 * All code in th
10b90 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 is file should a
10b91 63 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 6c ccess the global
10b92 20 73 74 72 75 63 74 75 72 65 20 61 62 6f 76 65 structure above
10b93 20 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c 69 61 via the.** alia
10b94 73 20 22 70 63 61 63 68 65 31 22 2e 20 54 68 69 s "pcache1". Thi
10b95 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 s ensures that t
10b96 68 65 20 57 53 44 20 65 6d 75 6c 61 74 69 6f 6e he WSD emulation
10b97 20 69 73 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a is used when.**
10b98 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 73 compiling for s
10b99 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e ystems that do n
10b9a 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 6c 20 ot support real
10b9b 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 WSD..*/.#define
10b9c 70 63 61 63 68 65 31 20 28 47 4c 4f 42 41 4c 28 pcache1 (GLOBAL(
10b9d 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f struct PCacheGlo
10b9e 62 61 6c 2c 20 70 63 61 63 68 65 31 5f 67 29 29 bal, pcache1_g))
10b9f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 50 ../*.** When a P
10ba0 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 gHdr1 structure
10ba1 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 is allocated, th
10ba2 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 43 61 e associated PCa
10ba3 63 68 65 31 2e 73 7a 50 61 67 65 0a 2a 2a 20 62 che1.szPage.** b
10ba4 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 ytes of data are
10ba5 20 6c 6f 63 61 74 65 64 20 64 69 72 65 63 74 6c located directl
10ba6 79 20 62 65 66 6f 72 65 20 69 74 20 69 6e 20 6d y before it in m
10ba7 65 6d 6f 72 79 20 28 69 2e 65 2e 20 74 68 65 20 emory (i.e. the
10ba8 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 total.** size of
10ba9 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
10baa 69 73 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 is sizeof(PgHdr1
10bab 29 2b 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 )+PCache1.szPage
10bac 20 62 79 74 65 29 2e 20 54 68 65 0a 2a 2a 20 50 byte). The.** P
10bad 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 GHDR1_TO_PAGE()
10bae 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20 70 6f macro takes a po
10baf 69 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72 inter to a PgHdr
10bb0 31 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 2a 1 structure as.*
10bb1 2a 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 61 6e * an argument an
10bb2 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
10bb3 74 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 ter to the assoc
10bb4 69 61 74 65 64 20 62 6c 6f 63 6b 20 6f 66 20 73 iated block of s
10bb5 7a 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 2e 20 zPage.** bytes.
10bb6 54 68 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 The PAGE_TO_PGHD
10bb7 52 31 28 29 20 6d 61 63 72 6f 20 64 6f 65 73 20 R1() macro does
10bb8 74 68 65 20 6f 70 70 6f 73 69 74 65 3a 20 69 74 the opposite: it
10bb9 73 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a s argument is.**
10bba 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
10bbb 62 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 20 block of szPage
10bbc 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 6e bytes of data an
10bbd 64 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c d the return val
10bbe 75 65 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 ue is.** a point
10bbf 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 er to the associ
10bc0 61 74 65 64 20 50 67 48 64 72 31 20 73 74 72 75 ated PgHdr1 stru
10bc1 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 cture..**.** a
10bc2 73 73 65 72 74 28 20 50 47 48 44 52 31 5f 54 4f ssert( PGHDR1_TO
10bc3 5f 50 41 47 45 28 50 41 47 45 5f 54 4f 5f 50 47 _PAGE(PAGE_TO_PG
10bc4 48 44 52 31 28 70 43 61 63 68 65 2c 20 58 29 29 HDR1(pCache, X))
10bc5 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e ==X );.*/.#defin
10bc6 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 e PGHDR1_TO_PAGE
10bc7 28 70 29 20 20 20 20 28 76 6f 69 64 2a 29 28 28 (p) (void*)((
10bc8 28 63 68 61 72 2a 29 70 29 20 2d 20 70 2d 3e 70 (char*)p) - p->p
10bc9 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 0a 23 Cache->szPage).#
10bca 64 65 66 69 6e 65 20 50 41 47 45 5f 54 4f 5f 50 define PAGE_TO_P
10bcb 47 48 44 52 31 28 63 2c 20 70 29 20 28 50 67 48 GHDR1(c, p) (PgH
10bcc 64 72 31 2a 29 28 28 28 63 68 61 72 2a 29 70 29 dr1*)(((char*)p)
10bcd 20 2b 20 63 2d 3e 73 7a 50 61 67 65 29 0a 0a 2f + c->szPage)../
10bce 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65 *.** Macros to e
10bcf 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74 nter and leave t
10bd0 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6d 75 he global LRU mu
10bd1 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 tex..*/.#define
10bd2 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10bd3 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 x() sqlite3_mute
10bd4 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 2e x_enter(pcache1.
10bd5 6d 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20 70 mutex).#define p
10bd6 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10bd7 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 () sqlite3_mutex
10bd8 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d _leave(pcache1.m
10bd9 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a utex)../********
10bda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10bde 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
10bdf 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f * Page Allocatio
10be0 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f n/SQLITE_CONFIG_
10be1 50 43 41 43 48 45 20 52 65 6c 61 74 65 64 20 46 PCACHE Related F
10be2 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a unctions *******
10be3 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
10be4 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
10be5 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 called during i
10be6 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 66 nitialization if
10be7 20 61 20 73 74 61 74 69 63 20 62 75 66 66 65 72 a static buffer
10be8 20 69 73 20 0a 2a 2a 20 73 75 70 70 6c 69 65 64 is .** supplied
10be9 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 to use for the
10bea 70 61 67 65 2d 63 61 63 68 65 20 62 79 20 70 61 page-cache by pa
10beb 73 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 ssing the SQLITE
10bec 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
10bed 45 0a 2a 2a 20 76 65 72 62 20 74 6f 20 73 71 6c E.** verb to sql
10bee 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 50 ite3_config(). P
10bef 61 72 61 6d 65 74 65 72 20 70 42 75 66 20 70 6f arameter pBuf po
10bf0 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 ints to an alloc
10bf1 61 74 69 6f 6e 20 6c 61 72 67 65 0a 2a 2a 20 65 ation large.** e
10bf2 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61 69 6e nough to contain
10bf3 20 27 6e 27 20 62 75 66 66 65 72 73 20 6f 66 20 'n' buffers of
10bf4 27 73 7a 27 20 62 79 74 65 73 20 65 61 63 68 2e 'sz' bytes each.
10bf5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10bf6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
10bf7 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70 CacheBufferSetup
10bf8 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 (void *pBuf, int
10bf9 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 sz, int n){. i
10bfa 66 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 f( pcache1.isIni
10bfb 74 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 t ){. PgFrees
10bfc 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 73 7a 20 3d lot *p;. sz =
10bfd 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b ROUNDDOWN8(sz);
10bfe 0a 20 20 20 20 70 63 61 63 68 65 31 2e 73 7a 53 . pcache1.szS
10bff 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 20 20 70 63 lot = sz;. pc
10c00 61 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20 70 ache1.pStart = p
10c01 42 75 66 3b 0a 20 20 20 20 70 63 61 63 68 65 31 Buf;. pcache1
10c02 2e 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 .pFree = 0;.
10c03 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 while( n-- ){.
10c04 20 20 20 20 70 20 3d 20 28 50 67 46 72 65 65 73 p = (PgFrees
10c05 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20 20 20 lot*)pBuf;.
10c06 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 p->pNext = pcac
10c07 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 20 he1.pFree;.
10c08 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d pcache1.pFree =
10c09 20 70 3b 0a 20 20 20 20 20 20 70 42 75 66 20 3d p;. pBuf =
10c0a 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a (void*)&((char*
10c0b 29 70 42 75 66 29 5b 73 7a 5d 3b 0a 20 20 20 20 )pBuf)[sz];.
10c0c 7d 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 45 }. pcache1.pE
10c0d 6e 64 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 7d nd = pBuf;. }.}
10c0e 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 ../*.** Malloc f
10c0f 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 77 69 74 unction used wit
10c10 68 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 6f hin this file to
10c11 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 allocate space
10c12 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 0a from the buffer.
10c13 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 ** configured us
10c14 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ing sqlite3_conf
10c15 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
10c16 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 _PAGECACHE) opti
10c17 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a 2a 20 73 75 on. If no .** su
10c18 63 68 20 62 75 66 66 65 72 20 65 78 69 73 74 73 ch buffer exists
10c19 20 6f 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 or there is no
10c1a 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 69 74 space left in it
10c1b 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
10c1c 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 63 6b 20 74 falls .** back t
10c1d 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 o sqlite3Malloc(
10c1e 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10c1f 64 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 d *pcache1Alloc(
10c20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f int nByte){. vo
10c21 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 id *p;. assert(
10c22 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
10c23 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 eld(pcache1.mute
10c24 78 29 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 x) );. if( nByt
10c25 65 3c 3d 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f e<=pcache1.szSlo
10c26 74 20 26 26 20 70 63 61 63 68 65 31 2e 70 46 72 t && pcache1.pFr
10c27 65 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ee ){. assert
10c28 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 ( pcache1.isInit
10c29 20 29 3b 0a 20 20 20 20 70 20 3d 20 28 50 67 48 );. p = (PgH
10c2a 64 72 31 20 2a 29 70 63 61 63 68 65 31 2e 70 46 dr1 *)pcache1.pF
10c2b 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 ree;. pcache1
10c2c 2e 70 46 72 65 65 20 3d 20 70 63 61 63 68 65 31 .pFree = pcache1
10c2d 2e 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 .pFree->pNext;.
10c2e 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
10c2f 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
10c30 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 S_PAGECACHE_SIZE
10c31 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 , nByte);. sq
10c32 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
10c33 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
10c34 45 43 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b ECACHE_USED, 1);
10c35 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f . }else{.. /
10c36 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
10c37 20 62 75 66 66 65 72 20 75 73 69 6e 67 20 73 71 buffer using sq
10c38 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 lite3Malloc. Bef
10c39 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 65 78 ore doing so, ex
10c3a 69 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 67 6c it the. ** gl
10c3b 6f 62 61 6c 20 70 63 61 63 68 65 20 6d 75 74 65 obal pcache mute
10c3c 78 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 x and unlock the
10c3d 20 70 61 67 65 72 2d 63 61 63 68 65 20 6f 62 6a pager-cache obj
10c3e 65 63 74 20 70 43 61 63 68 65 2e 20 54 68 69 73 ect pCache. This
10c3f 20 69 73 20 0a 20 20 20 20 2a 2a 20 73 6f 20 74 is . ** so t
10c40 68 61 74 20 69 66 20 74 68 65 20 61 74 74 65 6d hat if the attem
10c41 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 pt to allocate a
10c42 20 6e 65 77 20 62 75 66 66 65 72 20 63 61 75 73 new buffer caus
10c43 65 73 20 74 68 65 20 74 68 65 20 0a 20 20 20 20 es the the .
10c44 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 73 6f ** configured so
10c45 66 74 2d 68 65 61 70 2d 6c 69 6d 69 74 20 74 6f ft-heap-limit to
10c46 20 62 65 20 62 72 65 61 63 68 65 64 2c 20 69 74 be breached, it
10c47 20 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c will be possibl
10c48 65 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 6c e to. ** recl
10c49 61 69 6d 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 aim memory from
10c4a 74 68 69 73 20 70 61 67 65 72 2d 63 61 63 68 65 this pager-cache
10c4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 61 .. */. pca
10c4c 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10c4d 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 ;. p = sqlite
10c4e 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 3Malloc(nByte);.
10c4f 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 pcache1Enter
10c50 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 Mutex();. if(
10c51 20 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 p ){. int
10c52 73 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c sz = sqlite3Mall
10c53 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 ocSize(p);.
10c54 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
10c55 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
10c56 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c PAGECACHE_OVERFL
10c57 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 OW, sz);. }.
10c58 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
10c59 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 ../*.** Free an
10c5a 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
10c5b 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 70 obtained from p
10c5c 63 61 63 68 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a cache1Alloc()..*
10c5d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10c5e 61 63 68 65 31 46 72 65 65 28 76 6f 69 64 20 2a ache1Free(void *
10c5f 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 p){. assert( sq
10c60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
10c61 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 (pcache1.mutex)
10c62 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 );. if( p==0 )
10c63 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 3e return;. if( p>
10c64 3d 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 =pcache1.pStart
10c65 26 26 20 70 3c 70 63 61 63 68 65 31 2e 70 45 6e && p<pcache1.pEn
10c66 64 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 d ){. PgFrees
10c67 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 lot *pSlot;.
10c68 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
10c69 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 (SQLITE_STATUS_P
10c6a 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d AGECACHE_USED, -
10c6b 31 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 1);. pSlot =
10c6c 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a (PgFreeslot*)p;.
10c6d 20 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 pSlot->pNext
10c6e 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 = pcache1.pFree
10c6f 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 ;. pcache1.pF
10c70 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d ree = pSlot;. }
10c71 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53 else{. int iS
10c72 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ize = sqlite3Mal
10c73 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 locSize(p);.
10c74 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
10c75 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 (SQLITE_STATUS_P
10c76 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f AGECACHE_OVERFLO
10c77 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 W, -iSize);.
10c78 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
10c79 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c . }.}../*.** Al
10c7a 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 locate a new pag
10c7b 65 20 6f 62 6a 65 63 74 20 69 6e 69 74 69 61 6c e object initial
10c7c 6c 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ly associated wi
10c7d 74 68 20 63 61 63 68 65 20 70 43 61 63 68 65 2e th cache pCache.
10c7e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 .*/.static PgHdr
10c7f 31 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 1 *pcache1AllocP
10c80 61 67 65 28 50 43 61 63 68 65 31 20 2a 70 43 61 age(PCache1 *pCa
10c81 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 che){. int nByt
10c82 65 20 3d 20 73 69 7a 65 6f 66 28 50 67 48 64 72 e = sizeof(PgHdr
10c83 31 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50 1) + pCache->szP
10c84 61 67 65 3b 0a 20 20 76 6f 69 64 20 2a 70 50 67 age;. void *pPg
10c85 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 = pcache1Alloc(
10c86 6e 42 79 74 65 29 3b 0a 20 20 50 67 48 64 72 31 nByte);. PgHdr1
10c87 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 67 20 29 *p;. if( pPg )
10c88 7b 0a 20 20 20 20 70 20 3d 20 50 41 47 45 5f 54 {. p = PAGE_T
10c89 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10c8a 20 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20 70 pPg);. if( p
10c8b 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
10c8c 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 e ){. pcach
10c8d 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2b e1.nCurrentPage+
10c8e 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 +;. }. }else
10c8f 7b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d {. p = 0;. }
10c90 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
10c91 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61 67 /*.** Free a pag
10c92 65 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61 74 e object allocat
10c93 65 64 20 62 79 20 70 63 61 63 68 65 31 41 6c 6c ed by pcache1All
10c94 6f 63 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 ocPage()..**.**
10c95 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 The pointer is a
10c96 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 4e 55 4c llowed to be NUL
10c97 4c 2c 20 77 68 69 63 68 20 69 73 20 70 72 75 64 L, which is prud
10c98 65 6e 74 2e 20 20 42 75 74 20 69 74 20 74 75 72 ent. But it tur
10c99 6e 73 20 6f 75 74 0a 2a 2a 20 74 68 61 74 20 74 ns out.** that t
10c9a 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 he current imple
10c9b 6d 65 6e 74 61 74 69 6f 6e 20 68 61 70 70 65 6e mentation happen
10c9c 73 20 74 6f 20 6e 65 76 65 72 20 63 61 6c 6c 20 s to never call
10c9d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
10c9e 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e with a NULL poin
10c9f 74 65 72 2c 20 73 6f 20 77 65 20 6d 61 72 6b 20 ter, so we mark
10ca0 74 68 65 20 4e 55 4c 4c 20 74 65 73 74 20 77 69 the NULL test wi
10ca1 74 68 20 41 4c 57 41 59 53 28 29 2e 0a 2a 2f 0a th ALWAYS()..*/.
10ca2 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10ca3 68 65 31 46 72 65 65 50 61 67 65 28 50 67 48 64 he1FreePage(PgHd
10ca4 72 31 20 2a 70 29 7b 0a 20 20 69 66 28 20 41 4c r1 *p){. if( AL
10ca5 57 41 59 53 28 70 29 20 29 7b 0a 20 20 20 20 69 WAYS(p) ){. i
10ca6 66 28 20 70 2d 3e 70 43 61 63 68 65 2d 3e 62 50 f( p->pCache->bP
10ca7 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 urgeable ){.
10ca8 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 pcache1.nCurre
10ca9 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a ntPage--;. }.
10caa 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 28 pcache1Free(
10cab 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 PGHDR1_TO_PAGE(p
10cac 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ));. }.}../*.**
10cad 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e Malloc function
10cae 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 used by SQLite
10caf 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 to obtain space
10cb0 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 from the buffer
10cb1 63 6f 6e 66 69 67 75 72 65 64 0a 2a 2a 20 75 73 configured.** us
10cb2 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ing sqlite3_conf
10cb3 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
10cb4 5f 50 41 47 45 43 41 43 48 45 29 20 6f 70 74 69 _PAGECACHE) opti
10cb5 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 63 68 20 62 on. If no such b
10cb6 75 66 66 65 72 0a 2a 2a 20 65 78 69 73 74 73 2c uffer.** exists,
10cb7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 this function f
10cb8 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20 73 71 6c alls back to sql
10cb9 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f ite3Malloc()..*/
10cba 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10cbb 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
10cbc 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73 7a 29 7b eMalloc(int sz){
10cbd 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 70 63 . void *p;. pc
10cbe 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10cbf 29 3b 0a 20 20 70 20 3d 20 70 63 61 63 68 65 31 );. p = pcache1
10cc0 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20 20 70 63 61 Alloc(sz);. pca
10cc1 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10cc2 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
10cc3 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61 ./*.** Free an a
10cc4 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
10cc5 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
10cc6 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
10cc7 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
10cc8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
10cc9 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a 3PageFree(void *
10cca 70 29 7b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 p){. pcache1Ent
10ccb 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 erMutex();. pca
10ccc 63 68 65 31 46 72 65 65 28 70 29 3b 0a 20 20 70 che1Free(p);. p
10ccd 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10cce 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ();.}../********
10ccf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cd3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
10cd4 2a 20 47 65 6e 65 72 61 6c 20 49 6d 70 6c 65 6d * General Implem
10cd5 65 6e 74 61 74 69 6f 6e 20 46 75 6e 63 74 69 6f entation Functio
10cd6 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ns *************
10cd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10cd8 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
10cd9 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
10cda 20 75 73 65 64 20 74 6f 20 72 65 73 69 7a 65 20 used to resize
10cdb 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 75 the hash table u
10cdc 73 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 sed by the cache
10cdd 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 passed.** as th
10cde 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
10cdf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 ..**.** The glob
10ce0 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 al mutex must be
10ce1 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 held when this
10ce2 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
10ce3 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
10ce4 74 20 70 63 61 63 68 65 31 52 65 73 69 7a 65 48 t pcache1ResizeH
10ce5 61 73 68 28 50 43 61 63 68 65 31 20 2a 70 29 7b ash(PCache1 *p){
10ce6 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 70 4e 65 . PgHdr1 **apNe
10ce7 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e w;. unsigned in
10ce8 74 20 6e 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e t nNew;. unsign
10ce9 65 64 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 ed int i;.. ass
10cea 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10ceb 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10cec 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 mutex) );.. nNe
10ced 77 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a w = p->nHash*2;.
10cee 20 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29 if( nNew<256 )
10cef 7b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36 {. nNew = 256
10cf0 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 ;. }.. pcache1
10cf1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10cf2 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 if( p->nHash ){
10cf3 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
10cf4 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 gnMalloc(); }.
10cf5 61 70 4e 65 77 20 3d 20 28 50 67 48 64 72 31 20 apNew = (PgHdr1
10cf6 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f **)sqlite3_mallo
10cf7 63 28 73 69 7a 65 6f 66 28 50 67 48 64 72 31 20 c(sizeof(PgHdr1
10cf8 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 *)*nNew);. if(
10cf9 70 2d 3e 6e 48 61 73 68 20 29 7b 20 73 71 6c 69 p->nHash ){ sqli
10cfa 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
10cfb 6f 63 28 29 3b 20 7d 0a 20 20 70 63 61 63 68 65 oc(); }. pcache
10cfc 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10cfd 20 69 66 28 20 61 70 4e 65 77 20 29 7b 0a 20 20 if( apNew ){.
10cfe 20 20 6d 65 6d 73 65 74 28 61 70 4e 65 77 2c 20 memset(apNew,
10cff 30 2c 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 0, sizeof(PgHdr1
10d00 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 20 20 66 *)*nNew);. f
10d01 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 48 61 or(i=0; i<p->nHa
10d02 73 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 sh; i++){.
10d03 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 PgHdr1 *pPage;.
10d04 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 4e 65 PgHdr1 *pNe
10d05 78 74 20 3d 20 70 2d 3e 61 70 48 61 73 68 5b 69 xt = p->apHash[i
10d06 5d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 ];. while(
10d07 28 70 50 61 67 65 20 3d 20 70 4e 65 78 74 29 21 (pPage = pNext)!
10d08 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e =0 ){. un
10d09 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 70 signed int h = p
10d0a 50 61 67 65 2d 3e 69 4b 65 79 20 25 20 6e 4e 65 Page->iKey % nNe
10d0b 77 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 w;. pNext
10d0c 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b = pPage->pNext;
10d0d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e . pPage->
10d0e 70 4e 65 78 74 20 3d 20 61 70 4e 65 77 5b 68 5d pNext = apNew[h]
10d0f 3b 0a 20 20 20 20 20 20 20 20 61 70 4e 65 77 5b ;. apNew[
10d10 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 h] = pPage;.
10d11 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 }. }. sq
10d12 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 lite3_free(p->ap
10d13 48 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 61 70 Hash);. p->ap
10d14 48 61 73 68 20 3d 20 61 70 4e 65 77 3b 0a 20 20 Hash = apNew;.
10d15 20 20 70 2d 3e 6e 48 61 73 68 20 3d 20 6e 4e 65 p->nHash = nNe
10d16 77 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e w;. }.. return
10d17 20 28 70 2d 3e 61 70 48 61 73 68 20 3f 20 53 51 (p->apHash ? SQ
10d18 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
10d19 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a _NOMEM);.}../*.*
10d1a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
10d1b 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c is used internal
10d1c 6c 79 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 ly to remove the
10d1d 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f 6d page pPage from
10d1e 20 74 68 65 20 0a 2a 2a 20 67 6c 6f 62 61 6c 20 the .** global
10d1f 4c 52 55 20 6c 69 73 74 2c 20 69 66 20 69 73 20 LRU list, if is
10d20 70 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 70 part of it. If p
10d21 50 61 67 65 20 69 73 20 6e 6f 74 20 70 61 72 74 Page is not part
10d22 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 0a 2a of the global.*
10d23 2a 20 4c 52 55 20 6c 69 73 74 2c 20 74 68 65 6e * LRU list, then
10d24 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10d25 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
10d26 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 The global mute
10d27 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 x must be held w
10d28 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
10d29 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
10d2a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10d2b 68 65 31 50 69 6e 50 61 67 65 28 50 67 48 64 72 he1PinPage(PgHdr
10d2c 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 61 73 73 1 *pPage){. ass
10d2d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10d2e 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10d2f 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
10d30 70 50 61 67 65 20 26 26 20 28 70 50 61 67 65 2d pPage && (pPage-
10d31 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 50 61 >pLruNext || pPa
10d32 67 65 3d 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 ge==pcache1.pLru
10d33 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 69 66 28 Tail) ){. if(
10d34 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 pPage->pLruPrev
10d35 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 2d ){. pPage-
10d36 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e >pLruPrev->pLruN
10d37 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 ext = pPage->pLr
10d38 75 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 uNext;. }.
10d39 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 if( pPage->pLru
10d3a 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 50 Next ){. pP
10d3b 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70 age->pLruNext->p
10d3c 4c 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 2d LruPrev = pPage-
10d3d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d >pLruPrev;. }
10d3e 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 65 31 . if( pcache1
10d3f 2e 70 4c 72 75 48 65 61 64 3d 3d 70 50 61 67 65 .pLruHead==pPage
10d40 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ){. pcache
10d41 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 1.pLruHead = pPa
10d42 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 ge->pLruNext;.
10d43 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 63 }. if( pcac
10d44 68 65 31 2e 70 4c 72 75 54 61 69 6c 3d 3d 70 50 he1.pLruTail==pP
10d45 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 61 age ){. pca
10d46 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 che1.pLruTail =
10d47 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b pPage->pLruPrev;
10d48 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 . }. pPage
10d49 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a ->pLruNext = 0;.
10d4a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 pPage->pLruP
10d4b 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 rev = 0;. pPa
10d4c 67 65 2d 3e 70 43 61 63 68 65 2d 3e 6e 52 65 63 ge->pCache->nRec
10d4d 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20 20 7d 0a 7d yclable--;. }.}
10d4e 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 .../*.** Remove
10d4f 74 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 the page supplie
10d50 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
10d51 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 from the hash t
10d52 61 62 6c 65 20 0a 2a 2a 20 28 50 43 61 63 68 65 able .** (PCache
10d53 31 2e 61 70 48 61 73 68 20 73 74 72 75 63 74 75 1.apHash structu
10d54 72 65 29 20 74 68 61 74 20 69 74 20 69 73 20 63 re) that it is c
10d55 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 urrently stored
10d56 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c in..**.** The gl
10d57 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 obal mutex must
10d58 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 be held when thi
10d59 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
10d5a 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 lled..*/.static
10d5b 76 6f 69 64 20 70 63 61 63 68 65 31 52 65 6d 6f void pcache1Remo
10d5c 76 65 46 72 6f 6d 48 61 73 68 28 50 67 48 64 72 veFromHash(PgHdr
10d5d 31 20 2a 70 50 61 67 65 29 7b 0a 20 20 75 6e 73 1 *pPage){. uns
10d5e 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 50 igned int h;. P
10d5f 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10d60 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a pPage->pCache;.
10d61 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 0a PgHdr1 **pp;..
10d62 20 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 h = pPage->iKe
10d63 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 y % pCache->nHas
10d64 68 3b 0a 20 20 66 6f 72 28 70 70 3d 26 70 43 61 h;. for(pp=&pCa
10d65 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 che->apHash[h];
10d66 28 2a 70 70 29 21 3d 70 50 61 67 65 3b 20 70 70 (*pp)!=pPage; pp
10d67 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 3b =&(*pp)->pNext);
10d68 0a 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e . *pp = (*pp)->
10d69 70 4e 65 78 74 3b 0a 0a 20 20 70 43 61 63 68 65 pNext;.. pCache
10d6a 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a ->nPage--;.}../*
10d6b 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
10d6c 20 63 75 72 72 65 6e 74 6c 79 20 6d 6f 72 65 20 currently more
10d6d 74 68 61 6e 20 70 63 61 63 68 65 2e 6e 4d 61 78 than pcache.nMax
10d6e 50 61 67 65 20 70 61 67 65 73 20 61 6c 6c 6f 63 Page pages alloc
10d6f 61 74 65 64 2c 20 74 72 79 0a 2a 2a 20 74 6f 20 ated, try.** to
10d70 72 65 63 79 63 6c 65 20 70 61 67 65 73 20 74 6f recycle pages to
10d71 20 72 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 reduce the numb
10d72 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 er allocated to
10d73 70 63 61 63 68 65 2e 6e 4d 61 78 50 61 67 65 2e pcache.nMaxPage.
10d74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10d75 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
10d76 78 50 61 67 65 28 76 6f 69 64 29 7b 0a 20 20 61 xPage(void){. a
10d77 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
10d78 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
10d79 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 77 68 1.mutex) );. wh
10d7a 69 6c 65 28 20 70 63 61 63 68 65 31 2e 6e 43 75 ile( pcache1.nCu
10d7b 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65 rrentPage>pcache
10d7c 31 2e 6e 4d 61 78 50 61 67 65 20 26 26 20 70 63 1.nMaxPage && pc
10d7d 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 20 29 ache1.pLruTail )
10d7e 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 20 {. PgHdr1 *p
10d7f 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 = pcache1.pLruTa
10d80 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 il;. pcache1P
10d81 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 70 inPage(p);. p
10d82 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d cache1RemoveFrom
10d83 48 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63 61 Hash(p);. pca
10d84 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b che1FreePage(p);
10d85 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 . }.}../*.** Di
10d86 73 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73 20 scard all pages
10d87 66 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63 68 from cache pCach
10d88 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 e with a page nu
10d89 6d 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65 29 mber (key value)
10d8a 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 .** greater tha
10d8b 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69 4c n or equal to iL
10d8c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 imit. Any pinned
10d8d 20 70 61 67 65 73 20 74 68 61 74 20 6d 65 65 74 pages that meet
10d8e 20 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65 72 this .** criter
10d8f 69 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64 20 ia are unpinned
10d90 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 before they are
10d91 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a discarded..**.**
10d92 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 The global mute
10d93 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 x must be held w
10d94 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
10d95 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a n is called..*/.
10d96 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10d97 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 he1TruncateUnsaf
10d98 65 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 e(. PCache1 *pC
10d99 61 63 68 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65 ache, . unsigne
10d9a 64 20 69 6e 74 20 69 4c 69 6d 69 74 20 0a 29 7b d int iLimit .){
10d9b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 75 6e 73 . TESTONLY( uns
10d9c 69 67 6e 65 64 20 69 6e 74 20 6e 50 61 67 65 20 igned int nPage
10d9d 3d 20 30 3b 20 29 20 20 20 20 20 20 2f 2a 20 55 = 0; ) /* U
10d9e 73 65 64 20 74 6f 20 61 73 73 65 72 74 20 70 43 sed to assert pC
10d9f 61 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 20 63 ache->nPage is c
10da0 6f 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e 73 69 orrect */. unsi
10da1 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 61 73 gned int h;. as
10da2 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
10da3 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 tex_held(pcache1
10da4 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 .mutex) );. for
10da5 28 68 3d 30 3b 20 68 3c 70 43 61 63 68 65 2d 3e (h=0; h<pCache->
10da6 6e 48 61 73 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 nHash; h++){.
10da7 20 50 67 48 64 72 31 20 2a 2a 70 70 20 3d 20 26 PgHdr1 **pp = &
10da8 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
10da9 5d 3b 20 0a 20 20 20 20 50 67 48 64 72 31 20 2a ]; . PgHdr1 *
10daa 70 50 61 67 65 3b 0a 20 20 20 20 77 68 69 6c 65 pPage;. while
10dab 28 20 28 70 50 61 67 65 20 3d 20 2a 70 70 29 21 ( (pPage = *pp)!
10dac 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =0 ){. if(
10dad 70 50 61 67 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69 pPage->iKey>=iLi
10dae 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 mit ){. p
10daf 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a Cache->nPage--;.
10db0 20 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50 *pp = pP
10db1 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 age->pNext;.
10db2 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 pcache1PinPa
10db3 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
10db4 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
10db5 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ge(pPage);.
10db6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
10db7 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65 pp = &pPage->pNe
10db8 78 74 3b 0a 20 20 20 20 20 20 20 20 54 45 53 54 xt;. TEST
10db9 4f 4e 4c 59 28 20 6e 50 61 67 65 2b 2b 3b 20 29 ONLY( nPage++; )
10dba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
10dbb 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 }. assert( pCa
10dbc 63 68 65 2d 3e 6e 50 61 67 65 3d 3d 6e 50 61 67 che->nPage==nPag
10dbd 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a e );.}../*******
10dbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dbf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dc2 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
10dc3 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ** sqlite3_pcach
10dc4 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a e Methods ******
10dc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10dc7 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
10dc8 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
10dc9 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
10dca 63 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74 68 cache.xInit meth
10dcb 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e od..*/.static in
10dcc 74 20 70 63 61 63 68 65 31 49 6e 69 74 28 76 6f t pcache1Init(vo
10dcd 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
10dce 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
10dcf 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 (NotUsed);. ass
10dd0 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 ert( pcache1.isI
10dd1 6e 69 74 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 nit==0 );. mems
10dd2 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 et(&pcache1, 0,
10dd3 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 sizeof(pcache1))
10dd4 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 ;. if( sqlite3G
10dd5 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 lobalConfig.bCor
10dd6 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 70 63 eMutex ){. pc
10dd7 61 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71 ache1.mutex = sq
10dd8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
10dd9 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
10dda 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20 7d 0a TATIC_LRU);. }.
10ddb 20 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 pcache1.isInit
10ddc 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 = 1;. return S
10ddd 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
10dde 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10ddf 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10de0 5f 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 _pcache.xShutdow
10de1 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 4e 6f 74 n method..** Not
10de2 65 20 74 68 61 74 20 74 68 65 20 73 74 61 74 69 e that the stati
10de3 63 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 65 c mutex allocate
10de4 64 20 69 6e 20 78 49 6e 69 74 20 64 6f 65 73 20 d in xInit does
10de5 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 .** not need to
10de6 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 be freed..*/.sta
10de7 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
10de8 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e Shutdown(void *N
10de9 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
10dea 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
10deb 73 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 sed);. assert(
10dec 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74 21 3d pcache1.isInit!=
10ded 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 0 );. memset(&p
10dee 63 61 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f cache1, 0, sizeo
10def 66 28 70 63 61 63 68 65 31 29 29 3b 0a 7d 0a 0a f(pcache1));.}..
10df0 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10df1 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10df2 74 65 33 5f 70 63 61 63 68 65 2e 78 43 72 65 61 te3_pcache.xCrea
10df3 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a te method..**.**
10df4 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
10df5 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 cache..*/.static
10df6 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
10df7 2a 70 63 61 63 68 65 31 43 72 65 61 74 65 28 69 *pcache1Create(i
10df8 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62 nt szPage, int b
10df9 50 75 72 67 65 61 62 6c 65 29 7b 0a 20 20 50 43 Purgeable){. PC
10dfa 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 0a 0a ache1 *pCache;..
10dfb 20 20 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 pCache = (PCac
10dfc 68 65 31 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 he1 *)sqlite3_ma
10dfd 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 43 61 63 lloc(sizeof(PCac
10dfe 68 65 31 29 29 3b 0a 20 20 69 66 28 20 70 43 61 he1));. if( pCa
10dff 63 68 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 che ){. memse
10e00 74 28 70 43 61 63 68 65 2c 20 30 2c 20 73 69 7a t(pCache, 0, siz
10e01 65 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 eof(PCache1));.
10e02 20 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 pCache->szPag
10e03 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 e = szPage;.
10e04 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10e05 6c 65 20 3d 20 28 62 50 75 72 67 65 61 62 6c 65 le = (bPurgeable
10e06 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 ? 1 : 0);. i
10e07 66 28 20 62 50 75 72 67 65 61 62 6c 65 20 29 7b f( bPurgeable ){
10e08 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e . pCache->n
10e09 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 Min = 10;.
10e0a 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10e0b 78 28 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 x();. pcach
10e0c 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2b 3d 20 70 e1.nMinPage += p
10e0d 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 20 Cache->nMin;.
10e0e 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
10e0f 75 74 65 78 28 29 3b 0a 20 20 20 20 7d 0a 20 20 utex();. }.
10e10 7d 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 }. return (sqli
10e11 74 65 33 5f 70 63 61 63 68 65 20 2a 29 70 43 61 te3_pcache *)pCa
10e12 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d che;.}../*.** Im
10e13 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
10e14 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 the sqlite3_pcac
10e15 68 65 2e 78 43 61 63 68 65 73 69 7a 65 20 6d 65 he.xCachesize me
10e16 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e thod. .**.** Con
10e17 66 69 67 75 72 65 20 74 68 65 20 63 61 63 68 65 figure the cache
10e18 5f 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 _size limit for
10e19 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 a cache..*/.stat
10e1a 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 43 ic void pcache1C
10e1b 61 63 68 65 73 69 7a 65 28 73 71 6c 69 74 65 33 achesize(sqlite3
10e1c 5f 70 63 61 63 68 65 20 2a 70 2c 20 69 6e 74 20 _pcache *p, int
10e1d 6e 4d 61 78 29 7b 0a 20 20 50 43 61 63 68 65 31 nMax){. PCache1
10e1e 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10e1f 68 65 31 20 2a 29 70 3b 0a 20 20 69 66 28 20 70 he1 *)p;. if( p
10e20 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
10e21 65 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 e ){. pcache1
10e22 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
10e23 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 pcache1.nMaxPa
10e24 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d 20 70 43 ge += (nMax - pC
10e25 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a 20 20 20 ache->nMax);.
10e26 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 pCache->nMax =
10e27 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61 63 68 65 nMax;. pcache
10e28 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 1EnforceMaxPage(
10e29 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 );. pcache1Le
10e2a 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
10e2b 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
10e2c 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10e2d 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 50 qlite3_pcache.xP
10e2e 61 67 65 63 6f 75 6e 74 20 6d 65 74 68 6f 64 2e agecount method.
10e2f 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
10e30 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 pcache1Pagecount
10e31 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 (sqlite3_pcache
10e32 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 *p){. int n;.
10e33 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10e34 78 28 29 3b 0a 20 20 6e 20 3d 20 28 28 50 43 61 x();. n = ((PCa
10e35 63 68 65 31 20 2a 29 70 29 2d 3e 6e 50 61 67 65 che1 *)p)->nPage
10e36 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 ;. pcache1Leave
10e37 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 Mutex();. retur
10e38 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d n n;.}../*.** Im
10e39 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
10e3a 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 the sqlite3_pcac
10e3b 68 65 2e 78 46 65 74 63 68 20 6d 65 74 68 6f 64 he.xFetch method
10e3c 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63 68 20 61 . .**.** Fetch a
10e3d 20 70 61 67 65 20 62 79 20 6b 65 79 20 76 61 6c page by key val
10e3e 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 ue..**.** Whethe
10e3f 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65 77 20 70 r or not a new p
10e40 61 67 65 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 age may be alloc
10e41 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75 6e ated by this fun
10e42 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e ction depends on
10e43 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
10e44 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 the createFlag
10e45 61 72 67 75 6d 65 6e 74 2e 20 20 30 20 6d 65 61 argument. 0 mea
10e46 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 ns do not alloca
10e47 74 65 20 61 20 6e 65 77 0a 2a 2a 20 70 61 67 65 te a new.** page
10e48 2e 20 20 31 20 6d 65 61 6e 73 20 61 6c 6c 6f 63 . 1 means alloc
10e49 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 69 ate a new page i
10e4a 66 20 73 70 61 63 65 20 69 73 20 65 61 73 69 6c f space is easil
10e4b 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 32 20 y available. 2
10e4c 0a 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 74 72 79 .** means to try
10e4d 20 72 65 61 6c 6c 79 20 68 61 72 64 20 74 6f 20 really hard to
10e4e 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
10e4f 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 age..**.** For a
10e50 20 6e 6f 6e 2d 70 75 72 67 65 61 62 6c 65 20 63 non-purgeable c
10e51 61 63 68 65 20 28 61 20 63 61 63 68 65 20 75 73 ache (a cache us
10e52 65 64 20 61 73 20 74 68 65 20 73 74 6f 72 61 67 ed as the storag
10e53 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f e for an in-memo
10e54 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 20 ry.** database)
10e55 74 68 65 72 65 20 69 73 20 72 65 61 6c 6c 79 20 there is really
10e56 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 no difference be
10e57 74 77 65 65 6e 20 63 72 65 61 74 65 46 6c 61 67 tween createFlag
10e58 20 31 20 61 6e 64 20 32 2e 20 20 53 6f 0a 2a 2a 1 and 2. So.**
10e59 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e the calling fun
10e5a 63 74 69 6f 6e 20 28 70 63 61 63 68 65 2e 63 29 ction (pcache.c)
10e5b 20 77 69 6c 6c 20 6e 65 76 65 72 20 68 61 76 65 will never have
10e5c 20 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 a createFlag of
10e5d 20 31 20 6f 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 70 1 on.** a non-p
10e5e 75 72 67 61 62 6c 65 20 63 61 63 68 65 2e 0a 2a urgable cache..*
10e5f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 *.** There are t
10e60 68 72 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 hree different a
10e61 70 70 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 pproaches to obt
10e62 61 69 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 aining space for
10e63 20 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 a page,.** depe
10e64 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c nding on the val
10e65 75 65 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 ue of parameter
10e66 63 72 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 createFlag (whic
10e67 68 20 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 h may be 0, 1 or
10e68 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 2)..**.** 1.
10e69 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 Regardless of th
10e6a 65 20 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 e value of creat
10e6b 65 46 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 eFlag, the cache
10e6c 20 69 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 is searched for
10e6d 20 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 a .** copy
10e6e 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 of the requeste
10e6f 64 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 d page. If one i
10e70 73 20 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 s found, it is r
10e71 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 eturned..**.**
10e72 20 32 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 2. If createFla
10e73 67 3d 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 g==0 and the pag
10e74 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
10e75 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e in the cache, N
10e76 55 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 ULL is.** r
10e77 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 eturned..**.**
10e78 20 33 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 3. If createFla
10e79 67 20 69 73 20 31 2c 20 61 6e 64 20 74 68 65 20 g is 1, and the
10e7a 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 page is not alre
10e7b 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 ady in the cache
10e7c 2c 0a 2a 2a 20 20 20 20 20 20 61 6e 64 20 69 66 ,.** and if
10e7d 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 either of the f
10e7e 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 ollowing are tru
10e7f 65 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3a 0a e, return NULL:.
10e80 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 **.** (a)
10e81 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
10e82 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68 ges pinned by th
10e83 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74 e cache is great
10e84 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 er than.**
10e85 20 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 PCache1.nMa
10e86 78 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 28 x, or.** (
10e87 62 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 b) the number of
10e88 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 79 pages pinned by
10e89 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67 72 the cache is gr
10e8a 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 eater than.**
10e8b 20 20 20 20 20 20 20 20 74 68 65 20 73 75 6d 20 the sum
10e8c 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 of nMax for all
10e8d 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
10e8e 2c 20 6c 65 73 73 20 74 68 65 20 73 75 6d 20 6f , less the sum o
10e8f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 f .**
10e90 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 nMin for all oth
10e91 65 72 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 er purgeable cac
10e92 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 34 2e hes. .**.** 4.
10e93 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
10e94 66 69 72 73 74 20 74 68 72 65 65 20 63 6f 6e 64 first three cond
10e95 69 74 69 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 itions apply and
10e96 20 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 the cache is ma
10e97 72 6b 65 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 rked.** as
10e98 70 75 72 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 purgeable, and i
10e99 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c f one of the fol
10e9a 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a lowing is true:.
10e9b 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 **.** (a)
10e9c 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 The number of pa
10e9d 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ges allocated fo
10e9e 72 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61 r the cache is a
10e9f 6c 72 65 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 lready .**
10ea0 20 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 PCache1.nMa
10ea1 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 x, or.**.**
10ea2 20 20 28 62 29 20 54 68 65 20 6e 75 6d 62 65 72 (b) The number
10ea3 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 of pages alloca
10ea4 74 65 64 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 ted for all purg
10ea5 65 61 62 6c 65 20 63 61 63 68 65 73 20 69 73 0a eable caches is.
10ea6 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 6c 72 ** alr
10ea7 65 61 64 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 eady equal to or
10ea8 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
10ea9 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f e sum of nMax fo
10eaa 72 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 r all.**
10eab 20 20 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 purgeable cac
10eac 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 hes,.**.**
10ead 74 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 then attempt to
10eae 72 65 63 79 63 6c 65 20 61 20 70 61 67 65 20 66 recycle a page f
10eaf 72 6f 6d 20 74 68 65 20 4c 52 55 20 6c 69 73 74 rom the LRU list
10eb0 2e 20 49 66 20 69 74 20 69 73 20 74 68 65 20 72 . If it is the r
10eb1 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a ight.** siz
10eb2 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 e, return the re
10eb3 63 79 63 6c 65 64 20 62 75 66 66 65 72 2e 20 4f cycled buffer. O
10eb4 74 68 65 72 77 69 73 65 2c 20 66 72 65 65 20 74 therwise, free t
10eb5 68 65 20 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a he buffer and.**
10eb6 20 20 20 20 20 20 70 72 6f 63 65 65 64 20 74 6f proceed to
10eb7 20 73 74 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 step 5. .**.**
10eb8 20 20 35 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 5. Otherwise,
10eb9 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 allocate and ret
10eba 75 72 6e 20 61 20 6e 65 77 20 70 61 67 65 20 62 urn a new page b
10ebb 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 uffer..*/.static
10ebc 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 46 65 void *pcache1Fe
10ebd 74 63 68 28 73 71 6c 69 74 65 33 5f 70 63 61 63 tch(sqlite3_pcac
10ebe 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 he *p, unsigned
10ebf 69 6e 74 20 69 4b 65 79 2c 20 69 6e 74 20 63 72 int iKey, int cr
10ec0 65 61 74 65 46 6c 61 67 29 7b 0a 20 20 75 6e 73 eateFlag){. uns
10ec1 69 67 6e 65 64 20 69 6e 74 20 6e 50 69 6e 6e 65 igned int nPinne
10ec2 64 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 d;. PCache1 *pC
10ec3 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 ache = (PCache1
10ec4 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 *)p;. PgHdr1 *p
10ec5 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 Page = 0;.. ass
10ec6 65 72 74 28 20 70 43 61 63 68 65 2d 3e 62 50 75 ert( pCache->bPu
10ec7 72 67 65 61 62 6c 65 20 7c 7c 20 63 72 65 61 74 rgeable || creat
10ec8 65 46 6c 61 67 21 3d 31 20 29 3b 0a 20 20 70 63 eFlag!=1 );. pc
10ec9 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10eca 29 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 );. if( createF
10ecb 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 lag==1 ) sqlite3
10ecc 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
10ecd 63 28 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 c();.. /* Searc
10ece 68 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 h the hash table
10ecf 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 for an existing
10ed0 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69 66 28 entry. */. if(
10ed1 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3e 30 pCache->nHash>0
10ed2 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 ){. unsigned
10ed3 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 int h = iKey %
10ed4 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 pCache->nHash;.
10ed5 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70 43 61 for(pPage=pCa
10ed6 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 che->apHash[h];
10ed7 70 50 61 67 65 26 26 70 50 61 67 65 2d 3e 69 4b pPage&&pPage->iK
10ed8 65 79 21 3d 69 4b 65 79 3b 20 70 50 61 67 65 3d ey!=iKey; pPage=
10ed9 70 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b 0a 20 pPage->pNext);.
10eda 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 }.. if( pPage
10edb 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 || createFlag==0
10edc 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 50 ){. pcache1P
10edd 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
10ede 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 goto fetch_ou
10edf 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 t;. }.. /* Ste
10ee0 70 20 33 20 6f 66 20 68 65 61 64 65 72 20 63 6f p 3 of header co
10ee1 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20 6e 50 69 6e mment. */. nPin
10ee2 6e 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 6e 50 ned = pCache->nP
10ee3 61 67 65 20 2d 20 70 43 61 63 68 65 2d 3e 6e 52 age - pCache->nR
10ee4 65 63 79 63 6c 61 62 6c 65 3b 0a 20 20 69 66 28 ecyclable;. if(
10ee5 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 26 createFlag==1 &
10ee6 26 20 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e & (. nPin
10ee7 6e 65 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d ned>=(pcache1.nM
10ee8 61 78 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e axPage+pCache->n
10ee9 4d 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e Min-pcache1.nMin
10eea 50 61 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 Page). || nP
10eeb 69 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e inned>=(pCache->
10eec 6e 4d 61 78 20 2a 20 39 20 2f 20 31 30 29 0a 20 nMax * 9 / 10).
10eed 20 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 )){. goto fe
10eee 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
10eef 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 if( pCache->nPag
10ef0 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 e>=pCache->nHash
10ef1 20 26 26 20 70 63 61 63 68 65 31 52 65 73 69 7a && pcache1Resiz
10ef2 65 48 61 73 68 28 70 43 61 63 68 65 29 20 29 7b eHash(pCache) ){
10ef3 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 68 5f . goto fetch_
10ef4 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 out;. }.. /* S
10ef5 74 65 70 20 34 2e 20 54 72 79 20 74 6f 20 72 65 tep 4. Try to re
10ef6 63 79 63 6c 65 20 61 20 70 61 67 65 20 62 75 66 cycle a page buf
10ef7 66 65 72 20 69 66 20 61 70 70 72 6f 70 72 69 61 fer if appropria
10ef8 74 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 te. */. if( pCa
10ef9 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
10efa 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 && pcache1.pLruT
10efb 61 69 6c 20 26 26 20 28 0a 20 20 20 20 20 28 70 ail && (. (p
10efc 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31 3e 3d Cache->nPage+1>=
10efd 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20 7c 7c pCache->nMax) ||
10efe 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10eff 74 50 61 67 65 3e 3d 70 63 61 63 68 65 31 2e 6e tPage>=pcache1.n
10f00 4d 61 78 50 61 67 65 0a 20 20 29 29 7b 0a 20 20 MaxPage. )){.
10f01 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 pPage = pcache
10f02 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 1.pLruTail;.
10f03 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f pcache1RemoveFro
10f04 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 mHash(pPage);.
10f05 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 pcache1PinPage
10f06 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 (pPage);. if(
10f07 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e pPage->pCache->
10f08 73 7a 50 61 67 65 21 3d 70 43 61 63 68 65 2d 3e szPage!=pCache->
10f09 73 7a 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 szPage ){.
10f0a 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10f0b 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 50 pPage);. pP
10f0c 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c age = 0;. }el
10f0d 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 se{. pcache
10f0e 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 20 2d 1.nCurrentPage -
10f0f 3d 20 28 70 50 61 67 65 2d 3e 70 43 61 63 68 65 = (pPage->pCache
10f10 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 2d 20 70 ->bPurgeable - p
10f11 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
10f12 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 e);. }. }..
10f13 20 2f 2a 20 53 74 65 70 20 35 2e 20 49 66 20 61 /* Step 5. If a
10f14 20 75 73 61 62 6c 65 20 70 61 67 65 20 62 75 66 usable page buf
10f15 66 65 72 20 68 61 73 20 73 74 69 6c 6c 20 6e 6f fer has still no
10f16 74 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20 0a 20 t been found, .
10f17 20 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 61 ** attempt to a
10f18 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6f 6e llocate a new on
10f19 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 e. . */. if( !
10f1a 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 pPage ){. pPa
10f1b 67 65 20 3d 20 70 63 61 63 68 65 31 41 6c 6c 6f ge = pcache1Allo
10f1c 63 50 61 67 65 28 70 43 61 63 68 65 29 3b 0a 20 cPage(pCache);.
10f1d 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 }.. if( pPage
10f1e 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 ){. unsigned
10f1f 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25 20 70 int h = iKey % p
10f20 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
10f21 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b pCache->nPage+
10f22 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 4b +;. pPage->iK
10f23 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 ey = iKey;. p
10f24 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 Page->pNext = pC
10f25 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b ache->apHash[h];
10f26 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 . pPage->pCac
10f27 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20 20 20 he = pCache;.
10f28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 pPage->pLruPrev
10f29 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
10f2a 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 >pLruNext = 0;.
10f2b 20 20 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50 47 *(void **)(PG
10f2c 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 HDR1_TO_PAGE(pPa
10f2d 67 65 29 29 20 3d 20 30 3b 0a 20 20 20 20 70 43 ge)) = 0;. pC
10f2e 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 ache->apHash[h]
10f2f 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 = pPage;. }..fe
10f30 74 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 tch_out:. if( p
10f31 50 61 67 65 20 26 26 20 69 4b 65 79 3e 70 43 61 Page && iKey>pCa
10f32 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a che->iMaxKey ){.
10f33 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 pCache->iMax
10f34 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20 7d 0a Key = iKey;. }.
10f35 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 if( createFlag
10f36 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 45 6e 64 ==1 ) sqlite3End
10f37 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
10f38 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10f39 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 tex();. return
10f3a 28 70 50 61 67 65 20 3f 20 50 47 48 44 52 31 5f (pPage ? PGHDR1_
10f3b 54 4f 5f 50 41 47 45 28 70 50 61 67 65 29 20 3a TO_PAGE(pPage) :
10f3c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 0);.}.../*.** I
10f3d 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10f3e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10f3f 63 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74 68 6f che.xUnpin metho
10f40 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 d..**.** Mark a
10f41 70 61 67 65 20 61 73 20 75 6e 70 69 6e 6e 65 64 page as unpinned
10f42 20 28 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 61 (eligible for a
10f43 73 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65 63 79 synchronous recy
10f44 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61 74 69 cling)..*/.stati
10f45 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 55 6e c void pcache1Un
10f46 70 69 6e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 pin(sqlite3_pcac
10f47 68 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70 50 67 he *p, void *pPg
10f48 2c 20 69 6e 74 20 72 65 75 73 65 55 6e 6c 69 6b , int reuseUnlik
10f49 65 6c 79 29 7b 0a 20 20 50 43 61 63 68 65 31 20 ely){. PCache1
10f4a 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10f4b 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 e1 *)p;. PgHdr1
10f4c 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 *pPage = PAGE_T
10f4d 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10f4e 20 70 50 67 29 3b 0a 20 0a 20 20 61 73 73 65 72 pPg);. . asser
10f4f 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 t( pPage->pCache
10f50 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 70 63 ==pCache );. pc
10f51 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10f52 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 61 );.. /* It is a
10f53 6e 20 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 n error to call
10f54 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 this function if
10f55 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72 the page is alr
10f56 65 61 64 79 20 0a 20 20 2a 2a 20 70 61 72 74 20 eady . ** part
10f57 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 of the global LR
10f58 55 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 61 U list.. */. a
10f59 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 4c ssert( pPage->pL
10f5a 72 75 50 72 65 76 3d 3d 30 20 26 26 20 70 50 61 ruPrev==0 && pPa
10f5b 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 30 20 ge->pLruNext==0
10f5c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 );. assert( pca
10f5d 63 68 65 31 2e 70 4c 72 75 48 65 61 64 21 3d 70 che1.pLruHead!=p
10f5e 50 61 67 65 20 26 26 20 70 63 61 63 68 65 31 2e Page && pcache1.
10f5f 70 4c 72 75 54 61 69 6c 21 3d 70 50 61 67 65 20 pLruTail!=pPage
10f60 29 3b 0a 0a 20 20 69 66 28 20 72 65 75 73 65 55 );.. if( reuseU
10f61 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70 63 61 63 68 nlikely || pcach
10f62 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e e1.nCurrentPage>
10f63 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 pcache1.nMaxPage
10f64 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 52 ){. pcache1R
10f65 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 emoveFromHash(pP
10f66 61 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 age);. pcache
10f67 31 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 1FreePage(pPage)
10f68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
10f69 2a 20 41 64 64 20 74 68 65 20 70 61 67 65 20 74 * Add the page t
10f6a 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 o the global LRU
10f6b 20 6c 69 73 74 2e 20 4e 6f 72 6d 61 6c 6c 79 2c list. Normally,
10f6c 20 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 the page is add
10f6d 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 ed to. ** the
10f6e 20 68 65 61 64 20 6f 66 20 74 68 65 20 6c 69 73 head of the lis
10f6f 74 20 28 6c 61 73 74 20 70 61 67 65 20 74 6f 20 t (last page to
10f70 62 65 20 72 65 63 79 63 6c 65 64 29 2e 20 48 6f be recycled). Ho
10f71 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 20 wever, if the .
10f72 20 20 20 2a 2a 20 72 65 75 73 65 55 6e 6c 69 6b ** reuseUnlik
10f73 65 6c 79 20 66 6c 61 67 20 70 61 73 73 65 64 20 ely flag passed
10f74 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
10f75 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 70 61 is true, the pa
10f76 67 65 20 69 73 20 61 64 64 65 64 0a 20 20 20 20 ge is added.
10f77 2a 2a 20 74 6f 20 74 68 65 20 74 61 69 6c 20 6f ** to the tail o
10f78 66 20 74 68 65 20 6c 69 73 74 20 28 66 69 72 73 f the list (firs
10f79 74 20 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 t page to be rec
10f7a 79 63 6c 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 ycled).. */.
10f7b 20 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 if( pcache1.p
10f7c 4c 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20 20 LruHead ){.
10f7d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10f7e 64 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 d->pLruPrev = pP
10f7f 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 age;. pPage
10f80 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 63 61 ->pLruNext = pca
10f81 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 0a 20 che1.pLruHead;.
10f82 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 pcache1.pLr
10f83 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 uHead = pPage;.
10f84 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
10f85 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10f86 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 = pPage;.
10f87 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10f88 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a = pPage;. }.
10f89 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 pCache->nRec
10f8a 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a yclable++;. }..
10f8b 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10f8c 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
10f8d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
10f8e 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
10f8f 61 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 ache.xRekey meth
10f90 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 od. .*/.static v
10f91 6f 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 oid pcache1Rekey
10f92 28 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 (. sqlite3_pcac
10f93 68 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70 he *p,. void *p
10f94 50 67 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 Pg,. unsigned i
10f95 6e 74 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 nt iOld,. unsig
10f96 6e 65 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a ned int iNew.){.
10f97 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10f98 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
10f99 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 ;. PgHdr1 *pPag
10f9a 65 20 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 e = PAGE_TO_PGHD
10f9b 52 31 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b R1(pCache, pPg);
10f9c 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a . PgHdr1 **pp;.
10f9d 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10f9e 3b 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ; . assert( pPa
10f9f 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 ge->iKey==iOld )
10fa0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
10fa1 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 e->pCache==pCach
10fa2 65 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 e );.. pcache1E
10fa3 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 nterMutex();..
10fa4 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d h = iOld%pCache-
10fa5 3e 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26 >nHash;. pp = &
10fa6 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
10fa7 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70 ];. while( (*pp
10fa8 29 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 )!=pPage ){.
10fa9 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 pp = &(*pp)->pNe
10faa 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 xt;. }. *pp =
10fab 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20 pPage->pNext;..
10fac 20 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65 h = iNew%pCache
10fad 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65 ->nHash;. pPage
10fae 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 ->iKey = iNew;.
10faf 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 pPage->pNext =
10fb0 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 pCache->apHash[h
10fb1 5d 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48 ];. pCache->apH
10fb2 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a ash[h] = pPage;.
10fb3 20 20 69 66 28 20 69 4e 65 77 3e 70 43 61 63 68 if( iNew>pCach
10fb4 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 e->iMaxKey ){.
10fb5 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 pCache->iMaxKe
10fb6 79 20 3d 20 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 y = iNew;. }..
10fb7 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10fb8 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ex();.}../*.** I
10fb9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10fba 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10fbb 63 68 65 2e 78 54 72 75 6e 63 61 74 65 20 6d 65 che.xTruncate me
10fbc 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 thod. .**.** Dis
10fbd 63 61 72 64 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 card all unpinne
10fbe 64 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 d pages in the c
10fbf 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67 65 ache with a page
10fc0 20 6e 75 6d 62 65 72 20 65 71 75 61 6c 20 74 6f number equal to
10fc1 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 74 .** or greater t
10fc2 68 61 6e 20 70 61 72 61 6d 65 74 65 72 20 69 4c han parameter iL
10fc3 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 imit. Any pinned
10fc4 20 70 61 67 65 73 20 77 69 74 68 20 61 20 70 61 pages with a pa
10fc5 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 65 71 75 ge number.** equ
10fc6 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 al to or greater
10fc7 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 61 72 65 than iLimit are
10fc8 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70 69 implicitly unpi
10fc9 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nned..*/.static
10fca 76 6f 69 64 20 70 63 61 63 68 65 31 54 72 75 6e void pcache1Trun
10fcb 63 61 74 65 28 73 71 6c 69 74 65 33 5f 70 63 61 cate(sqlite3_pca
10fcc 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 che *p, unsigned
10fcd 20 69 6e 74 20 69 4c 69 6d 69 74 29 7b 0a 20 20 int iLimit){.
10fce 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 PCache1 *pCache
10fcf 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a = (PCache1 *)p;.
10fd0 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10fd1 74 65 78 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 tex();. if( iLi
10fd2 6d 69 74 3c 3d 70 43 61 63 68 65 2d 3e 69 4d 61 mit<=pCache->iMa
10fd3 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 63 61 63 xKey ){. pcac
10fd4 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 he1TruncateUnsaf
10fd5 65 28 70 43 61 63 68 65 2c 20 69 4c 69 6d 69 74 e(pCache, iLimit
10fd6 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 );. pCache->i
10fd7 4d 61 78 4b 65 79 20 3d 20 69 4c 69 6d 69 74 2d MaxKey = iLimit-
10fd8 31 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 1;. }. pcache1
10fd9 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a LeaveMutex();.}.
10fda 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10fdb 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10fdc 69 74 65 33 5f 70 63 61 63 68 65 2e 78 44 65 73 ite3_pcache.xDes
10fdd 74 72 6f 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a troy method. .**
10fde 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 63 61 .** Destroy a ca
10fdf 63 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 che allocated us
10fe0 69 6e 67 20 70 63 61 63 68 65 31 43 72 65 61 74 ing pcache1Creat
10fe1 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 e()..*/.static v
10fe2 6f 69 64 20 70 63 61 63 68 65 31 44 65 73 74 72 oid pcache1Destr
10fe3 6f 79 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 oy(sqlite3_pcach
10fe4 65 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 31 e *p){. PCache1
10fe5 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10fe6 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 he1 *)p;. pcach
10fe7 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a e1EnterMutex();.
10fe8 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 pcache1Truncat
10fe9 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 eUnsafe(pCache,
10fea 30 29 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 0);. pcache1.nM
10feb 61 78 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 axPage -= pCache
10fec 2d 3e 6e 4d 61 78 3b 0a 20 20 70 63 61 63 68 65 ->nMax;. pcache
10fed 31 2e 6e 4d 69 6e 50 61 67 65 20 2d 3d 20 70 43 1.nMinPage -= pC
10fee 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 70 63 ache->nMin;. pc
10fef 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 ache1EnforceMaxP
10ff0 61 67 65 28 29 3b 0a 20 20 70 63 61 63 68 65 31 age();. pcache1
10ff1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10ff2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61 sqlite3_free(pCa
10ff3 63 68 65 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 che->apHash);.
10ff4 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61 sqlite3_free(pCa
10ff5 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 che);.}../*.** T
10ff6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10ff7 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e called during in
10ff8 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71 itialization (sq
10ff9 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
10ffa 28 29 29 20 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c ()) to.** instal
10ffb 6c 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 6c l the default pl
10ffc 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f uggable cache mo
10ffd 64 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 dule, assuming t
10ffe 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f 74 0a he user has not.
10fff 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f 76 69 ** already provi
11000 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 ded an alternati
11001 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ve..*/.SQLITE_PR
11002 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
11003 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75 e3PCacheSetDefau
11004 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 lt(void){. stat
11005 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ic sqlite3_pcach
11006 65 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c e_methods defaul
11007 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 tMethods = {.
11008 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
11009 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 72 /* pAr
1100a 67 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 g */. pcache1
1100b 49 6e 69 74 2c 20 20 20 20 20 20 20 20 20 20 20 Init,
1100c 20 20 2f 2a 20 78 49 6e 69 74 20 2a 2f 0a 20 20 /* xInit */.
1100d 20 20 70 63 61 63 68 65 31 53 68 75 74 64 6f 77 pcache1Shutdow
1100e 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 n, /* xS
1100f 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 20 20 20 70 hutdown */. p
11010 63 61 63 68 65 31 43 72 65 61 74 65 2c 20 20 20 cache1Create,
11011 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 /* xCrea
11012 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 te */. pcache
11013 31 43 61 63 68 65 73 69 7a 65 2c 20 20 20 20 20 1Cachesize,
11014 20 20 20 2f 2a 20 78 43 61 63 68 65 73 69 7a 65 /* xCachesize
11015 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 50 */. pcache1P
11016 61 67 65 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 agecount,
11017 20 2f 2a 20 78 50 61 67 65 63 6f 75 6e 74 20 2a /* xPagecount *
11018 2f 0a 20 20 20 20 70 63 61 63 68 65 31 46 65 74 /. pcache1Fet
11019 63 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ch, /
1101a 2a 20 78 46 65 74 63 68 20 2a 2f 0a 20 20 20 20 * xFetch */.
1101b 70 63 61 63 68 65 31 55 6e 70 69 6e 2c 20 20 20 pcache1Unpin,
1101c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 70 /* xUnp
1101d 69 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 in */. pcache
1101e 31 52 65 6b 65 79 2c 20 20 20 20 20 20 20 20 20 1Rekey,
1101f 20 20 20 2f 2a 20 78 52 65 6b 65 79 20 2a 2f 0a /* xRekey */.
11020 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 pcache1Trunc
11021 61 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ate, /*
11022 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20 20 xTruncate */.
11023 20 70 63 61 63 68 65 31 44 65 73 74 72 6f 79 20 pcache1Destroy
11024 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 /* xDe
11025 73 74 72 6f 79 20 2a 2f 0a 20 20 7d 3b 0a 20 20 stroy */. };.
11026 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 sqlite3_config(S
11027 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 QLITE_CONFIG_PCA
11028 43 48 45 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 CHE, &defaultMet
11029 68 6f 64 73 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 hods);.}..#ifdef
1102a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
1102b 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 EMORY_MANAGEMENT
1102c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1102d 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 tion is called t
1102e 6f 20 66 72 65 65 20 73 75 70 65 72 66 6c 75 6f o free superfluo
1102f 75 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 us dynamically a
11030 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a llocated memory.
11031 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 70 ** held by the p
11032 61 67 65 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d ager system. Mem
11033 6f 72 79 20 69 6e 20 75 73 65 20 62 79 20 61 6e ory in use by an
11034 79 20 53 51 4c 69 74 65 20 70 61 67 65 72 20 61 y SQLite pager a
11035 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 llocated.** by t
11036 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 he current threa
11037 64 20 6d 61 79 20 62 65 20 73 71 6c 69 74 65 33 d may be sqlite3
11038 5f 66 72 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a _free()ed..**.**
11039 20 6e 52 65 71 20 69 73 20 74 68 65 20 6e 75 6d nReq is the num
1103a 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1103b 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 2e memory required.
1103c 20 4f 6e 63 65 20 74 68 69 73 20 6d 75 63 68 20 Once this much
1103d 68 61 73 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65 has.** been rele
1103e 61 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 ased, the functi
1103f 6f 6e 20 72 65 74 75 72 6e 73 2e 20 54 68 65 20 on returns. The
11040 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 return value is
11041 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
11042 20 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 6f 66 .** of bytes of
11043 20 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73 65 64 memory released
11044 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11045 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
11046 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f cacheReleaseMemo
11047 72 79 28 69 6e 74 20 6e 52 65 71 29 7b 0a 20 20 ry(int nReq){.
11048 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 int nFree = 0;.
11049 20 69 66 28 20 70 63 61 63 68 65 31 2e 70 53 74 if( pcache1.pSt
1104a 61 72 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50 67 art==0 ){. Pg
1104b 48 64 72 31 20 2a 70 3b 0a 20 20 20 20 70 63 61 Hdr1 *p;. pca
1104c 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
1104d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6e 52 ;. while( (nR
1104e 65 71 3c 30 20 7c 7c 20 6e 46 72 65 65 3c 6e 52 eq<0 || nFree<nR
1104f 65 71 29 20 26 26 20 28 70 3d 70 63 61 63 68 65 eq) && (p=pcache
11050 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 1.pLruTail) ){.
11051 20 20 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 nFree += sq
11052 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
11053 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 PGHDR1_TO_PAGE(p
11054 29 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ));. pcache
11055 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 1PinPage(p);.
11056 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 pcache1Remove
11057 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 FromHash(p);.
11058 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
11059 67 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ge(p);. }.
1105a 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
1105b 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ex();. }. retu
1105c 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 rn nFree;.}.#end
1105d 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 if /* SQLITE_ENA
1105e 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
1105f 45 4d 45 4e 54 20 2a 2f 0a 0a 23 69 66 64 65 66 EMENT */..#ifdef
11060 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a SQLITE_TEST./*.
11061 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
11062 20 69 73 20 75 73 65 64 20 62 79 20 74 65 73 74 is used by test
11063 20 70 72 6f 63 65 64 75 72 65 73 20 74 6f 20 69 procedures to i
11064 6e 73 70 65 63 74 20 74 68 65 20 69 6e 74 65 72 nspect the inter
11065 6e 61 6c 20 73 74 61 74 65 0a 2a 2a 20 6f 66 20 nal state.** of
11066 74 68 65 20 67 6c 6f 62 61 6c 20 63 61 63 68 65 the global cache
11067 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11068 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
11069 50 63 61 63 68 65 53 74 61 74 73 28 0a 20 20 69 PcacheStats(. i
1106a 6e 74 20 2a 70 6e 43 75 72 72 65 6e 74 2c 20 20 nt *pnCurrent,
1106b 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 /* OUT: Tota
1106c 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
1106d 73 20 63 61 63 68 65 64 20 2a 2f 0a 20 20 69 6e s cached */. in
1106e 74 20 2a 70 6e 4d 61 78 2c 20 20 20 20 20 20 20 t *pnMax,
1106f 20 20 20 2f 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 /* OUT: Globa
11070 6c 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 20 l maximum cache
11071 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 size */. int *p
11072 6e 4d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 2f nMin, /
11073 2a 20 4f 55 54 3a 20 53 75 6d 20 6f 66 20 50 43 * OUT: Sum of PC
11074 61 63 68 65 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 ache1.nMin for p
11075 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 urgeable caches
11076 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 65 63 79 */. int *pnRecy
11077 63 6c 61 62 6c 65 20 20 20 20 2f 2a 20 4f 55 54 clable /* OUT
11078 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f : Total number o
11079 66 20 70 61 67 65 73 20 61 76 61 69 6c 61 62 6c f pages availabl
1107a 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 20 e for recycling
1107b 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 31 20 2a */.){. PgHdr1 *
1107c 70 3b 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c p;. int nRecycl
1107d 61 62 6c 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 able = 0;. for(
1107e 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 p=pcache1.pLruHe
1107f 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 ad; p; p=p->pLru
11080 4e 65 78 74 29 7b 0a 20 20 20 20 6e 52 65 63 79 Next){. nRecy
11081 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 20 20 clable++;. }.
11082 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70 63 61 *pnCurrent = pca
11083 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 che1.nCurrentPag
11084 65 3b 0a 20 20 2a 70 6e 4d 61 78 20 3d 20 70 63 e;. *pnMax = pc
11085 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 3b 0a ache1.nMaxPage;.
11086 20 20 2a 70 6e 4d 69 6e 20 3d 20 70 63 61 63 68 *pnMin = pcach
11087 65 31 2e 6e 4d 69 6e 50 61 67 65 3b 0a 20 20 2a e1.nMinPage;. *
11088 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 6e pnRecyclable = n
11089 52 65 63 79 63 6c 61 62 6c 65 3b 0a 7d 0a 23 65 Recyclable;.}.#e
1108a 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
1108b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 ***** End of pca
1108c 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a che1.c *********
1108d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1108e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1108f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
11090 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
11091 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a rowset.c ******
11092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11093 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11094 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
11095 20 44 65 63 65 6d 62 65 72 20 33 0a 2a 2a 0a 2a December 3.**.*
11096 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
11097 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
11098 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
11099 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1109a 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1109b 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1109c 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1109d 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1109e 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1109f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
110a0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
110a1 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
110a2 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
110a3 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
110a4 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
110a5 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
110a6 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
110a7 0a 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 2a 2a 2a 2a 2a 2a ****************
110aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
110ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
110ac 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 This module imp
110ad 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 lements an objec
110ae 74 20 77 65 20 63 61 6c 6c 20 61 20 22 52 6f 77 t we call a "Row
110af 53 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 Set"..**.** The
110b0 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 RowSet object is
110b1 20 61 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 a collection of
110b2 20 72 6f 77 69 64 73 2e 20 20 52 6f 77 69 64 73 rowids. Rowids
110b3 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
110b4 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 into the RowSet
110b5 20 69 6e 20 61 6e 20 61 72 62 69 74 72 61 72 79 in an arbitrary
110b6 20 6f 72 64 65 72 2e 20 20 49 6e 73 65 72 74 73 order. Inserts
110b7 0a 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 72 .** can be inter
110b8 6d 69 78 65 64 20 77 69 74 68 20 74 65 73 74 73 mixed with tests
110b9 20 74 6f 20 73 65 65 20 69 66 20 61 20 67 69 76 to see if a giv
110ba 65 6e 20 72 6f 77 69 64 20 68 61 73 20 62 65 65 en rowid has bee
110bb 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 n.** previously
110bc 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 inserted into th
110bd 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 e RowSet..**.**
110be 41 66 74 65 72 20 61 6c 6c 20 69 6e 73 65 72 74 After all insert
110bf 73 20 61 72 65 20 66 69 6e 69 73 68 65 64 2c 20 s are finished,
110c0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
110c1 6f 20 65 78 74 72 61 63 74 20 74 68 65 0a 2a 2a o extract the.**
110c2 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
110c3 20 52 6f 77 53 65 74 20 69 6e 20 73 6f 72 74 65 RowSet in sorte
110c4 64 20 6f 72 64 65 72 2e 20 20 4f 6e 63 65 20 74 d order. Once t
110c5 68 69 73 20 65 78 74 72 61 63 74 69 6f 6e 0a 2a his extraction.*
110c6 2a 20 70 72 6f 63 65 73 73 20 68 61 73 20 73 74 * process has st
110c7 61 72 74 65 64 2c 20 6e 6f 20 6e 65 77 20 65 6c arted, no new el
110c8 65 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 69 6e ements may be in
110c9 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 serted..**.** He
110ca 6e 63 65 2c 20 74 68 65 20 70 72 69 6d 69 74 69 nce, the primiti
110cb 76 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f ve operations fo
110cc 72 20 61 20 52 6f 77 53 65 74 20 61 72 65 3a 0a r a RowSet are:.
110cd 2a 2a 0a 2a 2a 20 20 20 20 43 52 45 41 54 45 0a **.** CREATE.
110ce 2a 2a 20 20 20 20 49 4e 53 45 52 54 0a 2a 2a 20 ** INSERT.**
110cf 20 20 20 54 45 53 54 0a 2a 2a 20 20 20 20 53 4d TEST.** SM
110d0 41 4c 4c 45 53 54 0a 2a 2a 20 20 20 20 44 45 53 ALLEST.** DES
110d1 54 52 4f 59 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 TROY.**.** The C
110d2 52 45 41 54 45 20 61 6e 64 20 44 45 53 54 52 4f REATE and DESTRO
110d3 59 20 70 72 69 6d 69 74 69 76 65 73 20 61 72 65 Y primitives are
110d4 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 the constructor
110d5 20 61 6e 64 20 64 65 73 74 72 75 63 74 6f 72 2c and destructor,
110d6 0a 2a 2a 20 6f 62 76 69 6f 75 73 6c 79 2e 20 20 .** obviously.
110d7 54 68 65 20 49 4e 53 45 52 54 20 70 72 69 6d 69 The INSERT primi
110d8 74 69 76 65 20 61 64 64 73 20 61 20 6e 65 77 20 tive adds a new
110d9 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 52 element to the R
110da 6f 77 53 65 74 2e 0a 2a 2a 20 54 45 53 54 20 63 owSet..** TEST c
110db 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 hecks to see if
110dc 61 6e 20 65 6c 65 6d 65 6e 74 20 69 73 20 61 6c an element is al
110dd 72 65 61 64 79 20 69 6e 20 74 68 65 20 52 6f 77 ready in the Row
110de 53 65 74 2e 20 20 53 4d 41 4c 4c 45 53 54 0a 2a Set. SMALLEST.*
110df 2a 20 65 78 74 72 61 63 74 73 20 74 68 65 20 6c * extracts the l
110e0 65 61 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 east value from
110e1 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a the RowSet..**.*
110e2 2a 20 54 68 65 20 49 4e 53 45 52 54 20 70 72 69 * The INSERT pri
110e3 6d 69 74 69 76 65 20 6d 69 67 68 74 20 61 6c 6c mitive might all
110e4 6f 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c ocate additional
110e5 20 6d 65 6d 6f 72 79 2e 20 20 4d 65 6d 6f 72 79 memory. Memory
110e6 20 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 is.** allocated
110e7 20 69 6e 20 63 68 75 6e 6b 73 20 73 6f 20 6d 6f in chunks so mo
110e8 73 74 20 49 4e 53 45 52 54 73 20 64 6f 20 6e 6f st INSERTs do no
110e9 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 allocation. Th
110ea 65 72 65 20 69 73 20 61 6e 20 0a 2a 2a 20 75 70 ere is an .** up
110eb 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 per bound on the
110ec 20 73 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 size of allocat
110ed 65 64 20 6d 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d ed memory. No m
110ee 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 0a 2a emory is freed.*
110ef 2a 20 75 6e 74 69 6c 20 44 45 53 54 52 4f 59 2e * until DESTROY.
110f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54 20 .**.** The TEST
110f1 70 72 69 6d 69 74 69 76 65 20 69 6e 63 6c 75 64 primitive includ
110f2 65 73 20 61 20 22 62 61 74 63 68 22 20 6e 75 6d es a "batch" num
110f3 62 65 72 2e 20 20 54 68 65 20 54 45 53 54 20 70 ber. The TEST p
110f4 72 69 6d 69 74 69 76 65 0a 2a 2a 20 77 69 6c 6c rimitive.** will
110f5 20 6f 6e 6c 79 20 73 65 65 20 65 6c 65 6d 65 6e only see elemen
110f6 74 73 20 74 68 61 74 20 77 65 72 65 20 69 6e 73 ts that were ins
110f7 65 72 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 erted before the
110f8 20 6c 61 73 74 20 63 68 61 6e 67 65 0a 2a 2a 20 last change.**
110f9 69 6e 20 74 68 65 20 62 61 74 63 68 20 6e 75 6d in the batch num
110fa 62 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ber. In other w
110fb 6f 72 64 73 2c 20 69 66 20 61 6e 20 49 4e 53 45 ords, if an INSE
110fc 52 54 20 6f 63 63 75 72 73 20 62 65 74 77 65 65 RT occurs betwee
110fd 6e 0a 2a 2a 20 74 77 6f 20 54 45 53 54 73 20 77 n.** two TESTs w
110fe 68 65 72 65 20 74 68 65 20 54 45 53 54 73 20 68 here the TESTs h
110ff 61 76 65 20 74 68 65 20 73 61 6d 65 20 62 61 74 ave the same bat
11100 63 68 20 6e 75 62 6d 65 72 2c 20 74 68 65 6e 20 ch nubmer, then
11101 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 61 64 64 the.** value add
11102 65 64 20 62 79 20 74 68 65 20 49 4e 53 45 52 54 ed by the INSERT
11103 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 76 69 73 will not be vis
11104 69 62 6c 65 20 74 6f 20 74 68 65 20 73 65 63 6f ible to the seco
11105 6e 64 20 54 45 53 54 2e 0a 2a 2a 20 54 68 65 20 nd TEST..** The
11106 69 6e 69 74 69 61 6c 20 62 61 74 63 68 20 6e 75 initial batch nu
11107 6d 62 65 72 20 69 73 20 7a 65 72 6f 2c 20 73 6f mber is zero, so
11108 20 69 66 20 74 68 65 20 76 65 72 79 20 66 69 72 if the very fir
11109 73 74 20 54 45 53 54 20 63 6f 6e 74 61 69 6e 73 st TEST contains
1110a 0a 2a 2a 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 62 .** a non-zero b
1110b 61 74 63 68 20 6e 75 6d 62 65 72 2c 20 69 74 20 atch number, it
1110c 77 69 6c 6c 20 73 65 65 20 61 6c 6c 20 70 72 69 will see all pri
1110d 6f 72 20 49 4e 53 45 52 54 73 2e 0a 2a 2a 0a 2a or INSERTs..**.*
1110e 2a 20 4e 6f 20 49 4e 53 45 52 54 73 20 6d 61 79 * No INSERTs may
1110f 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 61 20 occurs after a
11110 53 4d 41 4c 4c 45 53 54 2e 20 20 41 6e 20 61 73 SMALLEST. An as
11111 73 65 72 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 sertion will fai
11112 6c 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 l if.** that is
11113 61 74 74 65 6d 70 74 65 64 2e 0a 2a 2a 0a 2a 2a attempted..**.**
11114 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 6e 20 The cost of an
11115 49 4e 53 45 52 54 20 69 73 20 72 6f 75 67 68 6c INSERT is roughl
11116 79 20 63 6f 6e 73 74 61 6e 74 2e 20 20 28 53 6f y constant. (So
11117 6d 65 74 69 6d 65 20 6e 65 77 20 6d 65 6d 6f 72 metime new memor
11118 79 0a 2a 2a 20 68 61 73 20 74 6f 20 62 65 20 61 y.** has to be a
11119 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e 20 49 llocated on an I
1111a 4e 53 45 52 54 2e 29 20 20 54 68 65 20 63 6f 73 NSERT.) The cos
1111b 74 20 6f 66 20 61 20 54 45 53 54 20 77 69 74 68 t of a TEST with
1111c 20 61 20 6e 65 77 0a 2a 2a 20 62 61 74 63 68 20 a new.** batch
1111d 6e 75 6d 62 65 72 20 69 73 20 4f 28 4e 6c 6f 67 number is O(Nlog
1111e 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 N) where N is th
1111f 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d e number of elem
11120 65 6e 74 73 20 69 6e 20 74 68 65 20 52 6f 77 53 ents in the RowS
11121 65 74 2e 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 et..** The cost
11122 6f 66 20 61 20 54 45 53 54 20 75 73 69 6e 67 20 of a TEST using
11123 74 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 6e the same batch n
11124 75 6d 62 65 72 20 69 73 20 4f 28 6c 6f 67 4e 29 umber is O(logN)
11125 2e 20 20 54 68 65 20 63 6f 73 74 0a 2a 2a 20 6f . The cost.** o
11126 66 20 74 68 65 20 66 69 72 73 74 20 53 4d 41 4c f the first SMAL
11127 4c 45 53 54 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 LEST is O(NlogN)
11128 2e 20 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 . Second and su
11129 62 73 65 71 75 65 6e 74 20 53 4d 41 4c 4c 45 53 bsequent SMALLES
1112a 54 0a 2a 2a 20 70 72 69 6d 69 74 69 76 65 73 20 T.** primitives
1112b 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 74 69 6d are constant tim
1112c 65 2e 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 e. The cost of
1112d 44 45 53 54 52 4f 59 20 69 73 20 4f 28 4e 29 2e DESTROY is O(N).
1112e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
1112f 61 6e 20 61 64 64 65 64 20 63 6f 73 74 20 6f 66 an added cost of
11130 20 4f 28 4e 29 20 77 68 65 6e 20 73 77 69 74 63 O(N) when switc
11131 68 69 6e 67 20 62 65 74 77 65 65 6e 20 54 45 53 hing between TES
11132 54 20 61 6e 64 0a 2a 2a 20 53 4d 41 4c 4c 45 53 T and.** SMALLES
11133 54 20 70 72 69 6d 69 74 69 76 65 73 2e 0a 2a 2f T primitives..*/
11134 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74 20 .../*.** Target
11135 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 size for allocat
11136 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a 23 ion chunks..*/.#
11137 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41 4c define ROWSET_AL
11138 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31 30 LOCATION_SIZE 10
11139 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 24../*.** The nu
1113a 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 65 mber of rowset e
1113b 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f 63 ntries per alloc
1113c 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f 0a ation chunk..*/.
1113d 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 45 #define ROWSET_E
1113e 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 20 NTRY_PER_CHUNK
1113f 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
11140 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53 45 ((ROWSE
11141 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a T_ALLOCATION_SIZ
11142 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 E-8)/sizeof(stru
11143 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29 ct RowSetEntry))
11144 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74 ../*.** Each ent
11145 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20 69 ry in a RowSet i
11146 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
11147 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f the following o
11148 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 bject..*/.struct
11149 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 20 RowSetEntry {
1114a 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36 34 . i64
1114b 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
1114c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f /* RO
1114d 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74 68 WID value for th
1114e 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 is entry */. st
1114f 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11150 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 52 *pRight; /* R
11151 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c 61 ight subtree (la
11152 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f 72 rger entries) or
11153 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 list */. struc
11154 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
11155 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 74 Left; /* Left
11156 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c 65 subtree (smalle
11157 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d 3b r entries) */.};
11158 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 6e ../*.** RowSetEn
11159 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65 20 try objects are
1115a 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 72 allocated in lar
1115b 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 61 ge chunks (insta
1115c 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 nces of the.** f
1115d 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
1115e 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d 65 re) to reduce me
1115f 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
11160 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a 2a overhead. The.*
11161 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 70 * chunks are kep
11162 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 t on a linked li
11163 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 st so that they
11164 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 can be deallocat
11165 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 52 ed.** when the R
11166 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f 79 owSet is destroy
11167 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f ed..*/.struct Ro
11168 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 74 wSetChunk {. st
11169 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b ruct RowSetChunk
1116a 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 20 *pNextChunk;
1116b 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 /* Next chu
1116c 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 68 nk on list of th
1116d 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 em all */. stru
1116e 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 61 ct RowSetEntry a
1116f 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e 54 Entry[ROWSET_ENT
11170 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f RY_PER_CHUNK]; /
11171 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 72 * Allocated entr
11172 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ies */.};../*.**
11173 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 A RowSet in an
11174 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
11175 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
11176 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 ure..**.** A typ
11177 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74 72 edef of this str
11178 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64 20 ucture if found
11179 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a in sqliteInt.h..
1117a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 */.struct RowSet
1117b 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 {. struct RowS
1117c 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b etChunk *pChunk;
1117d 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 /* List of a
1117e 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 ll chunk allocat
1117f 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ions */. sqlite
11180 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 3 *db;
11181 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
11182 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
11183 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ion */. struct
11184 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e RowSetEntry *pEn
11185 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 try; /* List
11186 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e 67 of entries using
11187 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 72 pRight */. str
11188 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11189 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c *pLast; /* L
1118a 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 ast entry on the
1118b 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0a pEntry list */.
1118c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
1118d 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20 20 ntry *pFresh;
1118e 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e 65 /* Source of ne
1118f 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 w entry objects
11190 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
11191 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b 20 etEntry *pTree;
11192 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74 72 /* Binary tr
11193 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a 2f ee of entries */
11194 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 20 . u16 nFresh;
11195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11196 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f /* Number of o
11197 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 68 bjects on pFresh
11198 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 65 */. u8 isSorte
11199 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
1119a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1119b 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65 64 pEntry is sorted
1119c 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68 3b */. u8 iBatch;
1119d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1119e 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
1119f 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f 0a insert batch */.
111a0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 62 };../*.** Turn b
111a1 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 ulk memory into
111a2 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e a RowSet object.
111a3 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d N bytes of mem
111a4 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69 6c ory.** are avail
111a5 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e 20 able at pSpace.
111a6 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 The db pointer
111a7 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65 6d is used as a mem
111a8 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 66 ory context.** f
111a9 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 6e or any subsequen
111aa 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 t allocations th
111ab 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 72 at need to occur
111ac 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f ..** Return a po
111ad 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 inter to the new
111ae 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e 0a RowSet object..
111af 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62 65 **.** It must be
111b0 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 4e the case that N
111b1 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 is sufficient t
111b2 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74 2e o make a Rowset.
111b3 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20 61 If not.** an a
111b4 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 6f ssertion fault o
111b5 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 ccurs..** .** If
111b6 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 N is larger tha
111b7 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 75 n the minimum, u
111b8 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 61 se the surplus a
111b9 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 s an initial.**
111ba 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 6e allocation of en
111bb 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 20 tries available
111bc 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a 2f to be filled..*/
111bd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
111be 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52 RowSet *sqlite3R
111bf 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65 owSetInit(sqlite
111c0 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53 70 3 *db, void *pSp
111c1 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e ace, unsigned in
111c2 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20 2a t N){. RowSet *
111c3 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20 3e p;. assert( N >
111c4 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 = ROUND8(sizeof(
111c5 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d 20 70 53 *p)) );. p = pS
111c6 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 68 75 6e pace;. p->pChun
111c7 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d k = 0;. p->db =
111c8 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79 db;. p->pEntry
111c9 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 = 0;. p->pLast
111ca 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65 = 0;. p->pTree
111cb 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65 73 = 0;. p->pFres
111cc 68 20 3d 20 28 73 74 72 75 63 74 20 52 6f 77 53 h = (struct RowS
111cd 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44 38 etEntry*)(ROUND8
111ce 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20 28 (sizeof(*p)) + (
111cf 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e 6e char*)p);. p->n
111d0 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e Fresh = (u16)((N
111d1 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 - ROUND8(sizeof
111d2 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 28 73 74 (*p)))/sizeof(st
111d3 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
111d4 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 ));. p->isSorte
111d5 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 42 61 74 d = 1;. p->iBat
111d6 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e ch = 0;. return
111d7 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 p;.}../*.** Dea
111d8 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 68 75 6e llocate all chun
111d9 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 53 65 74 ks from a RowSet
111da 2e 20 20 54 68 69 73 20 66 72 65 65 73 20 61 6c . This frees al
111db 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 0a 2a 2a l memory that.**
111dc 20 74 68 65 20 52 6f 77 53 65 74 20 68 61 73 20 the RowSet has
111dd 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 72 20 69 allocated over i
111de 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 20 54 68 ts lifetime. Th
111df 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a is routine is.**
111e0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
111e1 66 6f 72 20 74 68 65 20 52 6f 77 53 65 74 2e 0a for the RowSet..
111e2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
111e3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f E void sqlite3Ro
111e4 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 74 wSetClear(RowSet
111e5 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20 52 *p){. struct R
111e6 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 owSetChunk *pChu
111e7 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b nk, *pNextChunk;
111e8 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70 2d . for(pChunk=p-
111e9 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 3b >pChunk; pChunk;
111ea 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 78 74 43 pChunk = pNextC
111eb 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e 65 78 74 hunk){. pNext
111ec 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e Chunk = pChunk->
111ed 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 20 20 pNextChunk;.
111ee 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d sqlite3DbFree(p-
111ef 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b 0a 20 20 >db, pChunk);.
111f0 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 }. p->pChunk =
111f1 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 0;. p->nFresh =
111f2 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 0;. p->pEntry
111f3 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 = 0;. p->pLast
111f4 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65 20 = 0;. p->pTree
111f5 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 = 0;. p->isSort
111f6 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ed = 1;.}../*.**
111f7 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76 61 Insert a new va
111f8 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 lue into a RowSe
111f9 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 6c t..**.** The mal
111fa 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6f locFailed flag o
111fb 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 f the database c
111fc 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65 74 onnection is set
111fd 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 if a.** memory
111fe 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
111ff 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
11200 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
11201 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77 RowSetInsert(Row
11202 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77 69 Set *p, i64 rowi
11203 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 d){. struct Row
11204 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 SetEntry *pEntry
11205 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65 6e ; /* The new en
11206 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 try */. struct
11207 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61 RowSetEntry *pLa
11208 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 st; /* The las
11209 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a 2f t prior entry */
1120a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
1120b 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 65 );. if( p->nFre
1120c 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 sh==0 ){. str
1120d 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 uct RowSetChunk
1120e 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 20 *pNew;. pNew
1120f 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
11210 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a 65 cRaw(p->db, size
11211 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 of(*pNew));.
11212 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 if( pNew==0 ){.
11213 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
11214 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 }. pNew->pNe
11215 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43 68 xtChunk = p->pCh
11216 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68 75 unk;. p->pChu
11217 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 nk = pNew;. p
11218 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e 65 77 2d ->pFresh = pNew-
11219 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e >aEntry;. p->
1121a 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 45 54 5f nFresh = ROWSET_
1121b 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 3b ENTRY_PER_CHUNK;
1121c 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20 . }. pEntry =
1121d 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a 20 20 70 p->pFresh++;. p
1121e 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 20 70 45 ->nFresh--;. pE
1121f 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 69 64 3b ntry->v = rowid;
11220 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 . pEntry->pRigh
11221 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 74 20 3d t = 0;. pLast =
11222 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 69 66 28 p->pLast;. if(
11223 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69 66 pLast ){. if
11224 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26 26 ( p->isSorted &&
11225 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e 76 rowid<=pLast->v
11226 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 53 ){. p->isS
11227 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d orted = 0;. }
11228 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 52 69 67 . pLast->pRig
11229 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d ht = pEntry;. }
1122a 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1122b 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 ( p->pEntry==0 )
1122c 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 20 49 4e ; /* Fires if IN
1122d 53 45 52 54 20 61 66 74 65 72 20 53 4d 41 4c 4c SERT after SMALL
1122e 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d 3e 70 45 EST */. p->pE
1122f 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 ntry = pEntry;.
11230 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 }. p->pLast =
11231 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pEntry;.}../*.**
11232 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 Merge two lists
11233 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 20 of RowSetEntry
11234 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d 6f 76 65 objects. Remove
11235 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a duplicates..**.
11236 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 6c 69 73 ** The input lis
11237 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 64 ts are connected
11238 20 76 69 61 20 70 52 69 67 68 74 20 70 6f 69 6e via pRight poin
11239 74 65 72 73 20 61 6e 64 20 61 72 65 20 0a 2a 2a ters and are .**
1123a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63 68 assumed to each
1123b 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20 73 already be in s
1123c 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f 0a orted order..*/.
1123d 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f static struct Ro
1123e 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 wSetEntry *rowSe
1123f 74 4d 65 72 67 65 28 0a 20 20 73 74 72 75 63 74 tMerge(. struct
11240 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 41 RowSetEntry *pA
11241 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73 6f , /* First so
11242 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65 20 rted list to be
11243 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 74 72 75 merged */. stru
11244 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
11245 70 42 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 pB /* Second
11246 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 sorted list to
11247 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 29 7b 0a be merged */.){.
11248 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11249 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 73 74 72 ntry head;. str
1124a 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
1124b 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 61 69 6c *pTail;.. pTail
1124c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 68 69 6c = &head;. whil
1124d 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a 20 e( pA && pB ){.
1124e 20 20 20 61 73 73 65 72 74 28 20 70 41 2d 3e 70 assert( pA->p
1124f 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e Right==0 || pA->
11250 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e 76 v<=pA->pRight->v
11251 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
11252 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c pB->pRight==0 ||
11253 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 pB->v<=pB->pRig
11254 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66 28 ht->v );. if(
11255 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b 0a pA->v<pB->v ){.
11256 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 pTail->pRi
11257 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 20 20 20 ght = pA;.
11258 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b pA = pA->pRight;
11259 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 . pTail = p
1125a 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 20 Tail->pRight;.
1125b 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d 3e }else if( pB->
1125c 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20 20 v<pA->v ){.
1125d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d pTail->pRight =
1125e 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d 20 pB;. pB =
1125f 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 pB->pRight;.
11260 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d pTail = pTail-
11261 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c >pRight;. }el
11262 73 65 7b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 se{. pA = p
11263 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d A->pRight;. }
11264 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29 7b . }. if( pA ){
11265 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 2d . assert( pA-
11266 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 >pRight==0 || pA
11267 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d ->v<=pA->pRight-
11268 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d >v );. pTail-
11269 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 >pRight = pA;.
1126a 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
1126b 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 42 2d 3e t( pB==0 || pB->
1126c 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d pRight==0 || pB-
1126d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e >v<=pB->pRight->
1126e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d 3e v );. pTail->
1126f 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 7d pRight = pB;. }
11270 0a 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e 70 . return head.p
11271 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Right;.}../*.**
11272 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 Sort all element
11273 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 20 s on the pEntry
11274 6c 69 73 74 20 6f 66 20 74 68 65 20 52 6f 77 53 list of the RowS
11275 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69 6e et into ascendin
11276 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74 61 g order..*/ .sta
11277 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 53 tic void rowSetS
11278 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a ort(RowSet *p){.
11279 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
1127a 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 ;. struct RowSe
1127b 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b 0a tEntry *pEntry;.
1127c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
1127d 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 5b 34 30 ntry *aBucket[40
1127e 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d ];.. assert( p-
1127f 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 29 3b 0a >isSorted==0 );.
11280 20 20 6d 65 6d 73 65 74 28 61 42 75 63 6b 65 74 memset(aBucket
11281 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 42 75 63 , 0, sizeof(aBuc
11282 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 ket));. while(
11283 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 p->pEntry ){.
11284 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e pEntry = p->pEn
11285 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 try;. p->pEnt
11286 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52 69 ry = pEntry->pRi
11287 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d ght;. pEntry-
11288 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 >pRight = 0;.
11289 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b 65 for(i=0; aBucke
1128a 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 t[i]; i++){.
1128b 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 pEntry = rowSe
1128c 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b 69 tMerge(aBucket[i
1128d 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 20 20 20 ], pEntry);.
1128e 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 30 aBucket[i] = 0
1128f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 42 75 63 ;. }. aBuc
11290 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 72 79 3b ket[i] = pEntry;
11291 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20 . }. pEntry =
11292 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 0;. for(i=0; i<
11293 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 2f sizeof(aBucket)/
11294 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 5b 30 sizeof(aBucket[0
11295 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 45 ]); i++){. pE
11296 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72 ntry = rowSetMer
11297 67 65 28 70 45 6e 74 72 79 2c 20 61 42 75 63 6b ge(pEntry, aBuck
11298 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 2d et[i]);. }. p-
11299 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 >pEntry = pEntry
1129a 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 ;. p->pLast = 0
1129b 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 ;. p->isSorted
1129c 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 = 1;.}.../*.** T
1129d 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20 69 he input, pIn, i
1129e 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 20 s a binary tree
1129f 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66 20 (or subtree) of
112a0 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 RowSetEntry obje
112a1 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 cts..** Convert
112a2 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f 20 61 this tree into a
112a3 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63 6f 6e linked list con
112a4 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 70 52 nected by the pR
112a5 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a ight pointers.**
112a6 20 61 6e 64 20 72 65 74 75 72 6e 20 70 6f 69 6e and return poin
112a7 74 65 72 73 20 74 6f 20 74 68 65 20 66 69 72 73 ters to the firs
112a8 74 20 61 6e 64 20 6c 61 73 74 20 65 6c 65 6d 65 t and last eleme
112a9 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 77 20 6c nts of the new l
112aa 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
112ab 6f 69 64 20 72 6f 77 53 65 74 54 72 65 65 54 6f oid rowSetTreeTo
112ac 4c 69 73 74 28 0a 20 20 73 74 72 75 63 74 20 52 List(. struct R
112ad 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 49 6e 2c owSetEntry *pIn,
112ae 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
112af 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 72 of the input tr
112b0 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ee */. struct R
112b1 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 46 owSetEntry **ppF
112b2 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 72 69 74 irst, /* Writ
112b3 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6f 75 e head of the ou
112b4 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 2a tput list here *
112b5 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 /. struct RowSe
112b6 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 73 74 20 tEntry **ppLast
112b7 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 61 /* Write ta
112b8 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 il of the output
112b9 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b list here */.){
112ba 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 21 3d . assert( pIn!=
112bb 30 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 2d 3e 0 );. if( pIn->
112bc 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 74 72 pLeft ){. str
112bd 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
112be 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 74 54 72 *p;. rowSetTr
112bf 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 4c eeToList(pIn->pL
112c0 65 66 74 2c 20 70 70 46 69 72 73 74 2c 20 26 70 eft, ppFirst, &p
112c1 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 );. p->pRight
112c2 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c 73 65 7b = pIn;. }else{
112c3 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d 20 . *ppFirst =
112c4 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 pIn;. }. if( p
112c5 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 In->pRight ){.
112c6 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 rowSetTreeToLi
112c7 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 st(pIn->pRight,
112c8 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 70 70 &pIn->pRight, pp
112c9 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Last);. }else{.
112ca 20 20 20 20 2a 70 70 4c 61 73 74 20 3d 20 70 49 *ppLast = pI
112cb 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 n;. }. assert(
112cc 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 52 69 67 (*ppLast)->pRig
112cd 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a 0a ht==0 );.}.../*.
112ce 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f 72 ** Convert a sor
112cf 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 6d ted list of elem
112d0 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 65 64 20 ents (connected
112d1 62 79 20 70 52 69 67 68 74 29 20 69 6e 74 6f 20 by pRight) into
112d2 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 72 65 65 a binary.** tree
112d3 20 77 69 74 68 20 64 65 70 74 68 20 6f 66 20 69 with depth of i
112d4 44 65 70 74 68 2e 20 20 41 20 64 65 70 74 68 20 Depth. A depth
112d5 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 65 20 74 of 1 means the t
112d6 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 ree contains a s
112d7 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 20 74 61 ingle.** node ta
112d8 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 68 65 61 ken from the hea
112d9 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20 41 d of *ppList. A
112da 20 64 65 70 74 68 20 6f 66 20 32 20 6d 65 61 6e depth of 2 mean
112db 73 20 61 20 74 72 65 65 20 77 69 74 68 0a 2a 2a s a tree with.**
112dc 20 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20 41 three nodes. A
112dd 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a nd so forth..**.
112de 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20 65 ** Use as many e
112df 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 ntries from the
112e0 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72 65 input list as re
112e1 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61 74 quired and updat
112e2 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c 69 73 74 e the.** *ppList
112e3 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
112e4 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e 74 73 unused elements
112e5 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20 49 of the list. I
112e6 66 20 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20 6c f the input.** l
112e7 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6f ist contains too
112e8 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 2c 20 74 few elements, t
112e9 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 20 61 6e hen construct an
112ea 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 65 65 incomplete tree
112eb 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20 2a 70 .** and leave *p
112ec 70 4c 69 73 74 20 73 65 74 20 74 6f 20 4e 55 4c pList set to NUL
112ed 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 L..**.** Return
112ee 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
112ef 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 63 6f 6e root of the con
112f0 73 74 72 75 63 74 65 64 20 62 69 6e 61 72 79 20 structed binary
112f1 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tree..*/.static
112f2 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
112f3 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 65 70 54 ry *rowSetNDeepT
112f4 72 65 65 28 0a 20 20 73 74 72 75 63 74 20 52 6f ree(. struct Ro
112f5 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 69 wSetEntry **ppLi
112f6 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 70 74 68 st,. int iDepth
112f7 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 .){. struct Row
112f8 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 SetEntry *p;
112f9 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 /* Root of
112fa 74 68 65 20 6e 65 77 20 74 72 65 65 20 2a 2f 0a the new tree */.
112fb 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
112fc 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 ntry *pLeft;
112fd 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 /* Left subtree
112fe 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 4c 69 73 */. if( *ppLis
112ff 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 t==0 ){. retu
11300 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
11301 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a 20 20 20 iDepth==1 ){.
11302 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 20 p = *ppList;.
11303 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e 70 *ppList = p->p
11304 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c Right;. p->pL
11305 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 eft = p->pRight
11306 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 0;. return
11307 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20 3d p;. }. pLeft =
11308 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 rowSetNDeepTree
11309 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d (ppList, iDepth-
1130a 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 4c 69 73 1);. p = *ppLis
1130b 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b t;. if( p==0 ){
1130c 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 . return pLef
1130d 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 65 66 t;. }. p->pLef
1130e 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 2a 70 70 t = pLeft;. *pp
1130f 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 List = p->pRight
11310 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 ;. p->pRight =
11311 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 rowSetNDeepTree(
11312 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d 31 ppList, iDepth-1
11313 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
11314 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
11315 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66 a sorted list of
11316 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20 61 elements into a
11317 20 62 69 6e 61 72 79 20 74 72 65 65 2e 20 4d 61 binary tree. Ma
11318 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 61 ke the tree.** a
11319 73 20 64 65 65 70 20 61 73 20 69 74 20 6e 65 65 s deep as it nee
1131a 64 73 20 74 6f 20 62 65 20 69 6e 20 6f 72 64 65 ds to be in orde
1131b 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 r to contain the
1131c 20 65 6e 74 69 72 65 20 6c 69 73 74 2e 0a 2a 2f entire list..*/
1131d 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 .static struct R
1131e 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 owSetEntry *rowS
1131f 65 74 4c 69 73 74 54 6f 54 72 65 65 28 73 74 72 etListToTree(str
11320 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11321 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 *pList){. int i
11322 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 Depth;
11323 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 74 68 65 /* Depth of the
11324 20 74 72 65 65 20 73 6f 20 66 61 72 20 2a 2f 0a tree so far */.
11325 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11326 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20 2f ntry *p; /
11327 2a 20 43 75 72 72 65 6e 74 20 74 72 65 65 20 72 * Current tree r
11328 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 oot */. struct
11329 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 RowSetEntry *pLe
1132a 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 ft; /* Left su
1132b 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73 65 btree */.. asse
1132c 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 0a rt( pList!=0 );.
1132d 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20 70 p = pList;. p
1132e 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 List = p->pRight
1132f 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 ;. p->pLeft = p
11330 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
11331 66 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70 4c for(iDepth=1; pL
11332 69 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b 0a ist; iDepth++){.
11333 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0a 20 pLeft = p;.
11334 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20 p = pList;.
11335 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 pList = p->pRi
11336 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65 66 ght;. p->pLef
11337 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 20 20 70 t = pLeft;. p
11338 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 65 ->pRight = rowSe
11339 74 4e 44 65 65 70 54 72 65 65 28 26 70 4c 69 73 tNDeepTree(&pLis
1133a 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 20 7d 0a t, iDepth);. }.
1133b 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
1133c 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 *.** Convert the
1133d 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 45 6e 74 list in p->pEnt
1133e 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 74 65 64 ry into a sorted
1133f 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20 6e list if it is n
11340 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 61 6c 72 ot.** sorted alr
11341 65 61 64 79 2e 20 20 49 66 20 74 68 65 72 65 20 eady. If there
11342 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 is a binary tree
11343 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c 20 74 68 on p->pTree, th
11344 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 69 74 en.** convert it
11345 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 74 6f 6f into a list too
11346 20 61 6e 64 20 6d 65 72 67 65 20 69 74 20 69 6e and merge it in
11347 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e 74 72 79 to the p->pEntry
11348 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 list..*/.static
11349 20 76 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c 69 void rowSetToLi
1134a 73 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 st(RowSet *p){.
1134b 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 65 if( !p->isSorte
1134c 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 53 d ){. rowSetS
1134d 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 ort(p);. }. if
1134e 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0a 20 20 ( p->pTree ){.
1134f 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11350 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70 54 ntry *pHead, *pT
11351 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 65 74 54 ail;. rowSetT
11352 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e 70 54 72 reeToList(p->pTr
11353 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54 61 ee, &pHead, &pTa
11354 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 54 72 65 il);. p->pTre
11355 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 45 e = 0;. p->pE
11356 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72 ntry = rowSetMer
11357 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c 20 70 48 ge(p->pEntry, pH
11358 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ead);. }.}../*.
11359 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 73 ** Extract the s
1135a 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 6e 74 20 mallest element
1135b 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 2e from the RowSet.
1135c 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65 6c .** Write the el
1135d 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 52 6f 77 ement into *pRow
1135e 69 64 2e 20 20 52 65 74 75 72 6e 20 31 20 6f 6e id. Return 1 on
1135f 20 73 75 63 63 65 73 73 2e 20 20 52 65 74 75 72 success. Retur
11360 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 20 52 6f n.** 0 if the Ro
11361 77 53 65 74 20 69 73 20 61 6c 72 65 61 64 79 20 wSet is already
11362 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 empty..**.** Aft
11363 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 er this routine
11364 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c has been called,
11365 20 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77 53 the sqlite3RowS
11366 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a 20 72 6f etInsert().** ro
11367 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62 65 utine may not be
11368 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20 20 called again.
11369 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1136a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 6f TE int sqlite3Ro
1136b 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 20 wSetNext(RowSet
1136c 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64 29 *p, i64 *pRowid)
1136d 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 {. rowSetToList
1136e 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 (p);. if( p->pE
1136f 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52 6f ntry ){. *pRo
11370 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d wid = p->pEntry-
11371 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 >v;. p->pEntr
11372 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e 70 y = p->pEntry->p
11373 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 70 Right;. if( p
11374 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a 20 ->pEntry==0 ){.
11375 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 sqlite3RowS
11376 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20 20 etClear(p);.
11377 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
11378 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
11379 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a urn 0;. }.}../*
1137a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
1137b 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f 77 if element iRow
1137c 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64 20 id was inserted
1137d 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f 77 into the the row
1137e 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f set as.** part o
1137f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62 61 74 f any insert bat
11380 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42 61 74 ch prior to iBat
11381 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20 6f 72 ch. Return 1 or
11382 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 0..*/.SQLITE_PR
11383 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
11384 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53 3RowSetTest(RowS
11385 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38 20 et *pRowSet, u8
11386 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33 5f iBatch, sqlite3_
11387 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a 20 int64 iRowid){.
11388 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
11389 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 20 69 42 try *p;. if( iB
1138a 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e 69 atch!=pRowSet->i
1138b 42 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 28 Batch ){. if(
1138c 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 pRowSet->pEntry
1138d 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 53 65 74 ){. rowSet
1138e 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 74 29 3b ToList(pRowSet);
1138f 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e . pRowSet->
11390 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 4c 69 pTree = rowSetLi
11391 73 74 54 6f 54 72 65 65 28 70 52 6f 77 53 65 74 stToTree(pRowSet
11392 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 20 20 20 ->pEntry);.
11393 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 pRowSet->pEntry
11394 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 52 6f 77 = 0;. pRow
11395 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a Set->pLast = 0;.
11396 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 77 53 65 }. pRowSe
11397 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 42 61 74 t->iBatch = iBat
11398 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 52 ch;. }. p = pR
11399 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b 0a 20 20 owSet->pTree;.
1139a 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 while( p ){.
1139b 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64 20 if( p->v<iRowid
1139c 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e ){. p = p->
1139d 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 pRight;. }els
1139e 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 6f 77 69 e if( p->v>iRowi
1139f 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 d ){. p = p
113a0 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 65 6c ->pLeft;. }el
113a1 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e se{. return
113a2 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
113a3 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a return 0;.}../**
113a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
113a5 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a 2a of rowset.c ***
113a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
113a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
113aa 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 20 in file pager.c
113ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
113ae 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
113af 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
113b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
113b1 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
113b2 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
113b3 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
113b4 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
113b5 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
113b6 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
113b7 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
113b8 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
113b9 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
113ba 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
113bb 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
113bc 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
113bd 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
113be 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
113bf 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
113c0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
113c5 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 ***.** This is t
113c6 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
113c7 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 n of the page ca
113c8 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f 72 che subsystem or
113c9 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a 2a "pager"..** .**
113ca 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 73 The pager is us
113cb 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 64 ed to access a d
113cc 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 6c atabase disk fil
113cd 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 e. It implement
113ce 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d 6d s.** atomic comm
113cf 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 it and rollback
113d0 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 20 through the use
113d1 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c of a journal fil
113d2 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 70 e that.** is sep
113d3 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 64 arate from the d
113d4 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
113d5 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 6d he pager also im
113d6 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a 2a plements file.**
113d7 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 76 locking to prev
113d8 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 65 ent two processe
113d9 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 74 s from writing t
113da 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
113db 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 .** file simulta
113dc 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 20 neously, or one
113dd 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 61 process from rea
113de 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 ding the databas
113df 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 68 e while.** anoth
113e0 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a er is writing..*
113e1 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
113e2 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a _OMIT_DISKIO../*
113e3 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 .** Macros for t
113e4 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 roubleshooting.
113e5 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
113e6 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e off.*/.#if 0.in
113e7 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 t sqlite3PagerTr
113e8 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 ace=1; /* True
113e9 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e to enable tracin
113ea 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c g */.#define sql
113eb 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 ite3DebugPrintf
113ec 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 printf.#define P
113ed 41 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20 AGERTRACE(X)
113ee 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 if( sqlite3Page
113ef 72 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 rTrace ){ sqlite
113f0 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 3DebugPrintf X;
113f1 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 }.#else.#define
113f2 50 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 PAGERTRACE(X).#e
113f3 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
113f4 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 following two ma
113f5 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 cros are used wi
113f6 74 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 thin the PAGERTR
113f7 41 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f ACE() macros abo
113f8 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f ve.** to print o
113f9 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 ut file-descript
113fa 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 ors. .**.** PAGE
113fb 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f RID() takes a po
113fc 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 inter to a Pager
113fd 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 struct as its a
113fe 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 rgument. The.**
113ff 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d associated file-
11400 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 descriptor is re
11401 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 turned. FILEHAND
11402 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 LEID() takes an
11403 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 sqlite3_file.**
11404 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 struct as its ar
11405 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 gument..*/.#defi
11406 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 ne PAGERID(p) ((
11407 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 int)(p->fd)).#de
11408 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 fine FILEHANDLEI
11409 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a D(fd) ((int)fd).
1140a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 ./*.** The page
1140b 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 cache as a whole
1140c 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e is always in on
1140d 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
1140e 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a ng.** states:.**
1140f 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f .** PAGER_UNLO
11410 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 CK The pa
11411 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ge cache is not
11412 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e currently readin
11413 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 g or .**
11414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 w
11415 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 riting the datab
11416 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 ase file. There
11417 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 is no.**
11418 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11419 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d data held in mem
1141a 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 ory. This is th
1141b 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 e initial.**
1141c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1141d 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 state..**.**
1141e 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 PAGER_SHARED
1141f 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 The page c
11420 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 ache is reading
11421 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a the database..**
11422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11423 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 Writing i
11424 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e s not permitted.
11425 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a There can be.*
11426 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
11427 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 multiple
11428 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 readers accessi
11429 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 ng the same data
1142a 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 base.**
1142b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 fi
1142c 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 le at the same t
1142d 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 ime..**.** PAG
1142e 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 ER_RESERVED
1142f 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 This process ha
11430 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 s reserved the d
11431 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 atabase for writ
11432 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ing.**
11433 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 but
11434 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 has not yet mad
11435 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 e any changes.
11436 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 Only one process
11437 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
11438 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 at a t
11439 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 ime can reserve
1143a 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
1143b 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 he original.**
1143c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1143d 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 database fi
1143e 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 le has not been
1143f 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 modified so othe
11440 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
11441 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 proce
11442 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 sses may still b
11443 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e e reading the on
11444 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 -disk.**
11445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
11446 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
11447 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c .** PAGER_EXCL
11448 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 USIVE The pa
11449 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 ge cache is writ
1144a 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 ing the database
1144b 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ..**
1144c 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 Acces
1144d 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 s is exclusive.
1144e 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 No other proces
1144f 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 ses or.**
11450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11451 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 threads can be r
11452 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e eading or writin
11453 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 g while one.**
11454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11455 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 process is
11456 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 writing..**.**
11457 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 PAGER_SYNCED
11458 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d The pager m
11459 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 oves to this sta
1145a 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 te from PAGER_EX
1145b 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 CLUSIVE.**
1145c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1145d 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 after all dirty
1145e 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
1145f 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a written to the.
11460 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
11461 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 databas
11462 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 e file and the f
11463 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e ile has been syn
11464 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 ced to.**
11465 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11466 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 disk. All that r
11467 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 emains to do is
11468 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 to remove or.**
11469 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1146a 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 truncate t
1146b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1146c 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 and the transact
1146d 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ion .**
1146e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 wi
1146f 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e ll be committed.
11470 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
11471 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 cache comes up i
11472 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 n PAGER_UNLOCK.
11473 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 The first time
11474 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 a.** sqlite3Page
11475 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 rGet() occurs, t
11476 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 he state transit
11477 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 ions to PAGER_SH
11478 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 ARED..** After a
11479 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 ll pages have be
1147a 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e en released usin
1147b 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e g sqlite_page_un
1147c 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 ref(),.** the st
1147d 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 ate transitions
1147e 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e back to PAGER_UN
1147f 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 LOCK. The first
11480 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 time.** that sq
11481 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
11482 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 ) is called, the
11483 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f state transitio
11484 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 ns to.** PAGER_R
11485 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 ESERVED. (Note
11486 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 that sqlite3Page
11487 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c rWrite() can onl
11488 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f y be.** called o
11489 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 n an outstanding
1148a 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e page which mean
1148b 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 s that the pager
1148c 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 must.** be in P
1148d 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f AGER_SHARED befo
1148e 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e re it transition
1148f 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 s to PAGER_RESER
11490 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 VED.).** PAGER_R
11491 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 ESERVED means th
11492 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f at there is an o
11493 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 pen rollback jou
11494 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 rnal..** The tra
11495 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 nsition to PAGER
11496 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 _EXCLUSIVE occur
11497 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 s before any cha
11498 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 nges.** are made
11499 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1149a 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 file, though wr
1149b 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c ites to the roll
1149c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 back.** journal
1149d 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 occurs with just
1149e 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e PAGER_RESERVED.
1149f 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 After an sqlit
114a0 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
114a1 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 ).** or sqlite3P
114a2 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 agerCommitPhaseT
114a3 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 wo(), the state
114a4 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 can go back to P
114a5 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 AGER_SHARED,.**
114a6 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 or it can stay a
114a7 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 t PAGER_EXCLUSIV
114a8 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 E if we are in e
114a9 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 xclusive access
114aa 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 mode..*/.#define
114ab 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 PAGER_UNLOCK
114ac 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 0.#define PAG
114ad 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 ER_SHARED 1
114ae 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 /* same as SH
114af 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 ARED_LOCK */.#de
114b0 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 fine PAGER_RESER
114b1 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 VED 2 /* sa
114b2 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c me as RESERVED_L
114b3 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 OCK */.#define P
114b4 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 AGER_EXCLUSIVE
114b5 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 4 /* same as
114b6 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a EXCLUSIVE_LOCK *
114b7 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
114b8 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f SYNCED 5../
114b9 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 *.** A macro use
114ba 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 d for invoking t
114bb 68 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 he codec if ther
114bc 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 e is one.*/.#ifd
114bd 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
114be 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 DEC.# define COD
114bf 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c EC1(P,D,N,X,E) \
114c0 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 . if( P->xCod
114c1 65 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 ec && P->xCodec(
114c2 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 P->pCodec,D,N,X)
114c3 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 ==0 ){ E; }.# de
114c4 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c fine CODEC2(P,D,
114c5 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 N,X,E,O) \. i
114c6 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 f( P->xCodec==0
114c7 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d ){ O=(char*)D; }
114c8 65 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28 else \. if( (
114c9 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f O=(char*)(P->xCo
114ca 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c dec(P->pCodec,D,
114cb 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 N,X)))==0 ){ E;
114cc 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
114cd 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c CODEC1(P,D,N,X,
114ce 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f E) /* NO-OP */
114cf 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 .# define CODEC2
114d0 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d (P,D,N,X,E,O) O=
114d1 28 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a (char*)D.#endif.
114d2 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
114d3 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f um allowed secto
114d4 72 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 r size. 64KiB. I
114d5 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a f the xSectorsiz
114d6 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 e() method .** r
114d7 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c eturns a value l
114d8 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c arger than this,
114d9 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 then MAX_SECTOR
114da 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e _SIZE is used in
114db 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 stead..** This c
114dc 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 ould conceivably
114dd 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f cause corruptio
114de 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f n following a po
114df 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a wer failure on.*
114e0 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e * such a system.
114e1 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74 This is current
114e2 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 ly an undocument
114e3 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 ed limit..*/.#de
114e4 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f fine MAX_SECTOR_
114e5 53 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a SIZE 0x10000../*
114e6 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
114e7 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
114e8 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
114e9 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 located for each
114ea 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 active.** savep
114eb 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65 oint and stateme
114ec 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
114ed 6e 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c n the system. Al
114ee 6c 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65 l such structure
114ef 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 s.** are stored
114f0 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 in the Pager.aSa
114f1 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c vepoint[] array,
114f2 20 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 which is alloca
114f3 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a ted and.** resiz
114f4 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
114f5 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a Realloc()..**.**
114f6 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e When a savepoin
114f7 74 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68 t is created, th
114f8 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e PagerSavepoint
114f9 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c .iHdrOffset fiel
114fa 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 d is.** set to 0
114fb 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 . If a journal-h
114fc 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e eader is written
114fd 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a into the main j
114fe 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 ournal while.**
114ff 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 the savepoint is
11500 20 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48 active, then iH
11501 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 drOffset is set
11502 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 to the byte offs
11503 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 et .** immediate
11504 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ly following the
11505 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 last journal re
11506 63 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74 cord written int
11507 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f o the main.** jo
11508 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65 urnal before the
11509 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e journal-header.
1150a 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 This is require
1150b 64 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69 d during savepoi
1150c 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 nt.** rollback (
1150d 73 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63 see pagerPlaybac
1150e 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a kSavepoint())..*
1150f 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
11510 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
11511 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a PagerSavepoint;.
11512 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 struct PagerSave
11513 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f point {. i64 iO
11514 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
11515 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 /* Starti
11516 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 ng offset in mai
11517 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 n journal */. i
11518 36 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20 64 iHdrOffset;
11519 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1151a 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 ee above */. Bi
1151b 74 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 tvec *pInSavepoi
1151c 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 nt; /* Se
1151d 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 t of pages in th
1151e 69 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a is savepoint */.
1151f 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 Pgno nOrig;
11520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11521 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 * Original numbe
11522 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69 r of pages in fi
11523 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 le */. Pgno iSu
11524 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 bRec;
11525 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
11526 20 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e first record in
11527 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a sub-journal */.
11528 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e };../*.** A open
11529 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 page cache is a
1152a 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
1152b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
1152c 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 72 cture..**.** err
1152d 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 Code.**.** Pag
1152e 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62 er.errCode may b
1152f 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f e set to SQLITE_
11530 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f IOERR, SQLITE_CO
11531 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 6f RRUPT, or.** o
11532 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f r SQLITE_FULL. O
11533 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 nce one of the f
11534 69 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 irst three error
11535 73 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 s occurs, it per
11536 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 69 sists.** and i
11537 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68 s returned as th
11538 65 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72 e result of ever
11539 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50 y major pager AP
1153a 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 I call. The.**
1153b 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 SQLITE_FULL re
1153c 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 turn code is sli
1153d 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e ghtly different.
1153e 20 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c It persists onl
1153f 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 20 y until the.**
11540 20 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c next successful
11541 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72 rollback is per
11542 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61 formed on the pa
11543 67 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c ger cache. Also,
11544 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c .** SQLITE_FUL
11545 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 L does not affec
11546 74 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 t the sqlite3Pag
11547 65 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 erGet() and sqli
11548 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 te3PagerLookup()
11549 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 79 .** APIs, they
1154a 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 73 may still be us
1154b 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e ed successfully.
1154c 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 6c .**.** dbSizeVal
1154d 69 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 id, dbSize, dbOr
1154e 69 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69 igSize, dbFileSi
1154f 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 67 ze.**.** Manag
11550 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ing the size of
11551 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11552 65 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 20 e in pages is a
11553 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 74 little complicat
11554 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 72 ed..** The var
11555 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 69 iable Pager.dbSi
11556 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 ze contains the
11557 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
11558 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 that the databas
11559 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 72 e.** image cur
1155a 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e rently contains.
1155b 20 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65 As the database
1155c 20 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20 image grows or
1155d 73 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20 shrinks this.**
1155e 20 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 variable is up
1155f 64 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 dated. The varia
11560 62 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 ble Pager.dbFile
11561 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 Size contains th
11562 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f 66 e number.** of
11563 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 pages in the da
11564 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
11565 73 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 s may be differe
11566 6e 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 62 nt from Pager.db
11567 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f 6d Size.** if som
11568 65 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 e pages have bee
11569 6e 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 n appended to th
1156a 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
1156b 20 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 69 but not yet wri
1156c 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 72 tten.** out fr
1156d 6f 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 om the cache to
1156e 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 the actual file
1156f 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 74 on disk. Or if t
11570 68 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 65 he image has bee
11571 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 64 n.** truncated
11572 20 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 by an increment
11573 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74 al-vacuum operat
11574 69 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e 64 ion. The Pager.d
11575 62 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 62 bOrigSize variab
11576 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e 73 le.** contains
11577 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
11578 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
11579 62 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e 20 base image when
1157a 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 the current.**
1157b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
1157c 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f 6e opened. The con
1157d 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72 tents of all thr
1157e 65 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 69 ee of these vari
1157f 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f 6e ables is.** on
11580 6c 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f ly guaranteed to
11581 20 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74 be correct if t
11582 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72 he boolean Pager
11583 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20 .dbSizeValid is
11584 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 4f true..**.** TO
11585 44 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 63 DO: Under what c
11586 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 53 onditions is dbS
11587 69 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 6c izeValid set? Cl
11588 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 61 eared?.**.** cha
11589 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a ngeCountDone.**.
1158a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 ** This boolea
1158b 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 n variable is us
1158c 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ed to make sure
1158d 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 2d that the change-
1158e 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 counter .** (t
1158f 68 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72 he 4-byte header
11590 20 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 6f field at byte o
11591 66 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 ffset 24 of the
11592 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 69 database file) i
11593 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 s .** not upda
11594 74 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 ted more often t
11595 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 0a han necessary. .
11596 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 65 **.** It is se
11597 74 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 74 t to true when t
11598 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
11599 72 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 r field is updat
1159a 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 ed, which .**
1159b 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 can only happen
1159c 69 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 if an exclusive
1159d 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 lock is held on
1159e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1159f 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c e..** It is cl
115a0 65 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 61 eared (set to fa
115a1 6c 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 6e lse) whenever an
115a2 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
115a3 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 is .** relinqu
115a4 69 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 74 ished on the dat
115a5 61 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 68 abase file. Each
115a6 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 time a transact
115a7 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
115a8 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e 67 ,.** The chang
115a9 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 eCountDone flag
115aa 69 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 66 is inspected. If
115ab 20 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 it is true, the
115ac 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 work of.** up
115ad 64 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 dating the chang
115ae 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 e-counter is omi
115af 74 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 tted for the cur
115b0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
115b1 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d ..**.** This m
115b2 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 echanism means t
115b3 68 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 hat when running
115b4 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
115b5 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e de, a connection
115b6 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 .** need only
115b7 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e update the chan
115b8 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c ge-counter once,
115b9 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 for the first t
115ba 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 ransaction.**
115bb 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a committed..**.**
115bc 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a dbModified.**.*
115bd 2a 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 69 * The dbModifi
115be 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 ed flag is set w
115bf 68 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 61 henever a databa
115c0 73 65 20 70 61 67 65 20 69 73 20 64 69 72 74 69 se page is dirti
115c1 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 ed..** It is c
115c2 6c 65 61 72 65 64 20 61 74 20 74 68 65 20 65 6e leared at the en
115c3 64 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61 d of each transa
115c4 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 ction..**.** I
115c5 74 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63 t is used when c
115c6 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 68 ommitting or oth
115c7 65 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 20 erwise ending a
115c8 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 0a transaction. If.
115c9 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 66 ** the dbModif
115ca 69 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 ied flag is clea
115cb 72 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 6b r then less work
115cc 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e has to be done.
115cd 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 74 .**.** journalSt
115ce 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 arted.**.** Th
115cf 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 is flag is set w
115d0 68 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 20 henever the the
115d1 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 main journal is
115d2 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 synced. .**.**
115d3 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 The point of th
115d4 69 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 20 is flag is that
115d5 69 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 61 it must be set a
115d6 66 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 66 fter the .** f
115d7 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 irst journal hea
115d8 64 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 6c der in a journal
115d9 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 file has been s
115da 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a ynced to disk..*
115db 2a 20 20 20 41 66 74 65 72 20 74 68 69 73 20 68 * After this h
115dc 61 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 77 as happened, new
115dd 20 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 20 pages appended
115de 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
115df 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 65 .** do not nee
115e0 64 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 d the PGHDR_NEED
115e1 5f 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c 20 _SYNC flag set,
115e2 61 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6e as they do not n
115e3 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 74 eed.** to wait
115e4 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 73 for a journal s
115e5 79 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 20 ync before they
115e6 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f can be written o
115e7 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 64 ut to.** the d
115e8 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 65 atabase file (se
115e9 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 e function pager
115ea 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 20 _write())..**
115eb 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a 2a .** setMaster.**
115ec 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 .** This varia
115ed 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 65 ble is used to e
115ee 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d nsure that the m
115ef 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
115f0 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 66 le name.** (if
115f1 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 72 any) is only wr
115f2 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
115f3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 65 ournal file once
115f4 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 63 ..**.** When c
115f5 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 6e ommitting a tran
115f6 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 73 saction, the mas
115f7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
115f8 20 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a 2a name (if any).*
115f9 2a 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 74 * may be writt
115fa 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 en into the jour
115fb 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 74 nal file while t
115fc 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c he pager is stil
115fd 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 5f l in.** PAGER_
115fe 52 45 53 45 52 56 45 44 20 73 74 61 74 65 20 28 RESERVED state (
115ff 73 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f see CommitPhaseO
11600 6e 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 74 ne() for the act
11601 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 68 ion). It.** th
11602 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 75 en attempts to u
11603 70 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 63 pgrade to an exc
11604 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 20 lusive lock. If
11605 74 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 this attempt.**
11606 20 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 fails, then SQ
11607 4c 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 65 LITE_BUSY may be
11608 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
11609 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 73 user and the us
1160a 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 65 er.** may atte
1160b 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 mpt to commit th
1160c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67 e transaction ag
1160d 61 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c 69 ain later (calli
1160e 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 68 ng.** CommitPh
1160f 61 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 2e aseOne() again).
11610 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 This flag is us
11611 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 ed to ensure tha
11612 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 74 t the .** mast
11613 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
11614 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 is only written
11615 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
11616 69 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a ile the first.**
11617 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 68 time CommitPh
11618 61 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c aseOne() is call
11619 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 ed..**.** doNotS
1161a 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 ync.**.** This
1161b 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
1161c 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 and cleared by
1161d 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1161e 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53 e()..**.** needS
1161f 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f ync.**.** TODO
11620 3a 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 61 : It might be ea
11621 73 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 73 sier to set this
11622 20 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 69 variable in wri
11623 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a 2a teJournalHdr().*
11624 2a 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 73 * and writeMas
11625 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e 6c terJournal() onl
11626 79 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d 65 y. Change its me
11627 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e 63 aning to "unsync
11628 65 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 73 ed data.** has
11629 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
1162a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a 2a the journal"..*
1162b 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 *.** subjInMemor
1162c 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 69 y.**.** This i
1162d 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 s a boolean vari
1162e 61 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 74 able. If true, t
1162f 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 hen any required
11630 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 sub-journal.**
11631 20 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61 is opened as a
11632 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
11633 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c nal file. If fal
11634 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f se, then in-memo
11635 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72 ry.** sub-jour
11636 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 nals are only us
11637 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 ed for in-memory
11638 20 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a 2f pager files..*/
11639 0a 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a .struct Pager {.
1163a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
1163b 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Vfs; /*
1163c 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f OS functions to
1163d 20 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 use for IO */.
1163e 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 u8 exclusiveMod
1163f 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e; /*
11640 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 Boolean. True if
11641 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 locking_mode==E
11642 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 XCLUSIVE */. u8
11643 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 journalMode;
11644 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20 /* On
11645 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55 of the PAGER_JOU
11646 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 RNALMODE_* value
11647 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 s */. u8 useJou
11648 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 rnal;
11649 20 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c /* Use a roll
1164a 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 back journal on
1164b 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 this file */. u
1164c 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 8 noReadlock;
1164d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f /* Do
1164e 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f not bother to o
1164f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 btain readlocks
11650 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20 */. u8 noSync;
11651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11652 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20 /* Do not sync
11653 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74 the journal if t
11654 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c rue */. u8 full
11655 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Sync;
11656 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61 /* Do extra
11657 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f syncs of the jo
11658 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74 urnal for robust
11659 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e ness */. u8 syn
1165a 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 c_flags;
1165b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
1165c 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 SYNC_NORMAL or S
1165d 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38 YNC_FULL */. u8
1165e 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 tempFile;
1165f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 /* zFi
11660 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 lename is a temp
11661 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 orary file */.
11662 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 u8 readOnly;
11663 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11664 72 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f rue for a read-o
11665 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a nly database */.
11666 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 u8 memDb;
11667 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11668 20 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 True to inhibit
11669 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f all file I/O */
1166a 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f .. /* The follo
1166b 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 wing block conta
1166c 69 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 ins those class
1166d 6d 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 65 members that are
1166e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a dynamically. *
1166f 2a 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e * modified durin
11670 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 g normal operati
11671 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 76 ons. The other v
11672 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 ariables in this
11673 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 structure. **
11674 61 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 74 are either const
11675 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 ant throughout t
11676 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 he lifetime of t
11677 68 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c 73 he pager, or els
11678 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 73 e. ** used to s
11679 74 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 69 tore configurati
1167a 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 on parameters th
1167b 61 74 20 61 66 66 65 63 74 20 74 68 65 20 77 61 at affect the wa
1167c 79 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 2a y the pager . *
1167d 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a 2a * operates.. **
1167e 0a 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 65 . ** The 'state
1167f 27 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 65 ' variable is de
11680 73 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 20 scribed in more
11681 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 74 detail along wit
11682 68 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 72 h the. ** descr
11683 69 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 iptions of the v
11684 61 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 6b alues it may tak
11685 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b e - PAGER_UNLOCK
11686 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 68 etc. Many of th
11687 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 72 e. ** other var
11688 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 62 iables in this b
11689 6c 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 62 lock are describ
1168a 65 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e ed in the commen
1168b 74 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a 2a t directly . **
1168c 20 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 73 above this clas
1168d 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 s definition..
1168e 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 20 */. u8 state;
1168f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11690 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b /* PAGER_UNLOCK
11691 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 45 , _SHARED, _RESE
11692 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 RVED, etc. */.
11693 75 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 u8 dbModified;
11694 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11695 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72 65 rue if there are
11696 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 any changes to
11697 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 6e the Db */. u8 n
11698 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 20 eedSync;
11699 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1169a 69 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 73 if an fsync() is
1169b 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 6a needed on the j
1169c 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 6a ournal */. u8 j
1169d 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 20 ournalStarted;
1169e 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
1169f 69 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f 75 if header of jou
116a0 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 2a rnal is synced *
116a1 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 /. u8 changeCou
116a2 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 ntDone;
116a3 2f 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 /* Set after inc
116a4 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 rementing the ch
116a5 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a ange-counter */.
116a6 20 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 u8 setMaster;
116a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
116a8 20 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e True if a m-j n
116a9 61 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 ame has been wri
116aa 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a tten to jrnl */.
116ab 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 u8 doNotSync;
116ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
116ad 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 Boolean. While
116ae 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 true, do not spi
116af 6c 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a ll the cache */.
116b0 20 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 64 u8 dbSizeValid
116b1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
116b2 20 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a 65 Set when dbSize
116b3 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 is correct */.
116b4 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 u8 subjInMemory
116b5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
116b6 54 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d True to use in-m
116b7 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 emory sub-journa
116b8 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 ls */. Pgno dbS
116b9 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
116ba 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
116bb 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 pages in the da
116bc 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f tabase */. Pgno
116bd 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 dbOrigSize;
116be 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a /* dbSiz
116bf 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 e before the cur
116c0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
116c1 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c */. Pgno dbFil
116c2 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 eSize;
116c3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 /* Number of p
116c4 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
116c5 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 base file */. i
116c6 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 nt errCode;
116c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
116c8 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e e of several kin
116c9 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a ds of errors */.
116ca 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 int nRec;
116cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
116cc 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c 65 Pages journalle
116cd 64 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d 68 d since last j-h
116ce 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a 2f eader written */
116cf 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 . u32 cksumInit
116d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
116d1 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 * Quasi-random v
116d2 61 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 alue added to ev
116d3 65 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a ery checksum */.
116d4 20 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 20 u32 nSubRec;
116d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
116d6 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 Number of recor
116d7 64 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 75 ds written to su
116d8 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 42 b-journal */. B
116d9 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 itvec *pInJourna
116da 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e l; /* On
116db 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 e bit for each p
116dc 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
116dd 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 ase file */. sq
116de 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 lite3_file *fd;
116df 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
116e0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
116e1 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 database */. s
116e2 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 qlite3_file *jfd
116e3 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 ; /* Fi
116e4 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f le descriptor fo
116e5 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a r main journal *
116e6 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
116e7 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 20 *sjfd;
116e8 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
116e9 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e or for sub-journ
116ea 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 al */. i64 jour
116eb 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 nalOff;
116ec 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77 /* Current w
116ed 72 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 rite offset in t
116ee 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
116ef 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c */. i64 journal
116f0 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Hdr;
116f1 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 /* Byte offset
116f2 74 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 to previous jour
116f3 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 nal header */.
116f4 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
116f5 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 41 aSavepoint; /* A
116f6 72 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 73 rray of active s
116f7 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 avepoints */. i
116f8 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 nt nSavepoint;
116f9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
116fa 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
116fb 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b 5d in aSavepoint[]
116fc 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c */. char dbFil
116fd 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 eVers[16];
116fe 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 /* Changes whe
116ff 6e 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 never database f
11700 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 ile changes */.
11701 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 3b u32 sectorSize;
11702 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11703 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 Assumed sector s
11704 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 ize during rollb
11705 61 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e 45 ack */.. u16 nE
11706 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 xtra;
11707 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69 /* Add thi
11708 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20 s many bytes to
11709 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 each in-memory p
1170a 61 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 age */. i16 nRe
1170b 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 serve;
1170c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1170d 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61 f unused bytes a
1170e 74 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 t end of each pa
1170f 67 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 ge */. u32 vfsF
11710 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
11711 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 /* Flags for
11712 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 sqlite3_vfs.xOp
11713 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 61 en() */. int pa
11714 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 geSize;
11715 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
11716 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 of bytes in a pa
11717 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 ge */. Pgno mxP
11718 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 gno;
11719 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 /* Maximum a
1171a 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74 llowed size of t
1171b 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
1171c 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
1171d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1171e 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 Name of the data
1171f 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 base file */. c
11720 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 har *zJournal;
11721 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
11722 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 me of the journa
11723 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 l file */. int
11724 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 (*xBusyHandler)(
11725 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74 void*); /* Funct
11726 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e ion to call when
11727 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 busy */. void
11728 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 *pBusyHandlerArg
11729 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 ; /* Contex
1172a 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 t argument for x
1172b 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 23 BusyHandler */.#
1172c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1172d 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d T. int nHit, nM
1172e 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 iss;
1172f 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e /* Cache hits an
11730 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69 d missing */. i
11731 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65 nt nRead, nWrite
11732 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ; /* Da
11733 74 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61 tabase pages rea
11734 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e d/written */.#en
11735 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 dif. void (*xRe
11736 69 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 29 initer)(DbPage*)
11737 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 ; /* Call this r
11738 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f outine when relo
11739 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 ading pages */.#
1173a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
1173b 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 _CODEC. void *(
1173c 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 *xCodec)(void*,v
1173d 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 oid*,Pgno,int);
1173e 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 /* Routine for e
1173f 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20 n/decoding data
11740 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 */. void (*xCod
11741 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 ecSizeChng)(void
11742 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e *,int,int); /* N
11743 6f 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69 otify of page si
11744 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 ze changes */.
11745 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 void (*xCodecFre
11746 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 e)(void*);
11747 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 /* Destru
11748 63 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f 64 ctor for the cod
11749 65 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 ec */. void *pC
1174a 6f 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 odec;
1174b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
1174c 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e ument to xCodec.
1174d 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 .. methods */.#e
1174e 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d ndif. char *pTm
1174f 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 pSpace;
11750 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 /* Pager.page
11751 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 Size bytes of sp
11752 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 ace for tmp use
11753 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c */. i64 journal
11754 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20 SizeLimit;
11755 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66 /* Size limit f
11756 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f or persistent jo
11757 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 urnal files */.
11758 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 65 PCache *pPCache
11759 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1175a 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 Pointer to page
1175b 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a cache object */.
1175c 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
1175d 20 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a *pBackup; /*
1175e 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74 Pointer to list
1175f 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b of ongoing back
11760 75 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0a up processes */.
11761 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f };../*.** The fo
11762 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 llowing global v
11763 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f ariables hold co
11764 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a unters used for.
11765 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f ** testing purpo
11766 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65 ses only. These
11767 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f variables do no
11768 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20 t exist in.** a
11769 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c non-testing buil
1176a 64 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 d. These variab
1176b 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65 les are not thre
1176c 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64 ad-safe..*/.#ifd
1176d 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
1176e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1176f 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 lite3_pager_read
11770 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 db_count = 0;
11771 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 /* Number of fu
11772 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72 ll pages read fr
11773 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f om DB */.SQLITE_
11774 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
11775 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f pager_writedb_co
11776 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 unt = 0; /* Nu
11777 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 mber of full pag
11778 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42 es written to DB
11779 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
1177a 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 nt sqlite3_pager
1177b 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20 _writej_count =
1177c 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 0; /* Number
1177d 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e of pages written
1177e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 to journal */.#
1177f 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e define PAGER_IN
11780 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 CR(v) v++.#else
11781 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f .# define PAGER_
11782 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a INCR(v).#endif..
11783 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 ../*.** Journal
11784 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68 files begin with
11785 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
11786 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68 agic string. Th
11787 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 e data.** was ob
11788 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 tained from /dev
11789 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 /random. It is
1178a 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 used only as a s
1178b 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a anity check..**.
1178c 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e ** Since version
1178d 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 2.8.0, the jour
1178e 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 nal format conta
1178f 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 ins additional s
11790 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e anity.** checkin
11791 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 g information.
11792 49 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69 If the power fai
11793 6c 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 ls while the jou
11794 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a 2a rnal is being.**
11795 20 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 written, semi-r
11796 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61 andom garbage da
11797 74 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 ta might appear
11798 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a in the journal.*
11799 2a 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77 * file after pow
1179a 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20 er is restored.
1179b 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 If an attempt i
1179c 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 s then made.** t
1179d 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e o roll the journ
1179e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74 al back, the dat
1179f 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63 abase could be c
117a0 6f 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61 orrupted. The a
117a1 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e dditional.** san
117a2 69 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74 ity checking dat
117a3 61 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 a is an attempt
117a4 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 to discover the
117a5 67 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a garbage in the.*
117a6 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 * journal and ig
117a7 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 nore it..**.** T
117a8 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 he sanity checki
117a9 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ng information f
117aa 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e or the new journ
117ab 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 al format consis
117ac 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 ts.** of a 32-bi
117ad 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 t checksum on ea
117ae 63 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e ch page of data.
117af 20 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63 The checksum c
117b0 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 overs both.** th
117b1 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e e page number an
117b2 64 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 d the pPager->pa
117b3 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 geSize bytes of
117b4 64 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 data for the pag
117b5 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d e..** This cksum
117b6 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
117b7 74 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 to a 32-bit rand
117b8 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 om value that ap
117b9 70 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 pears in the.**
117ba 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 journal file rig
117bb 68 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61 ht after the hea
117bc 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d der. The random
117bd 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20 initializer is
117be 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 important,.** be
117bf 63 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61 cause garbage da
117c0 74 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ta that appears
117c1 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 at the end of a
117c2 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c journal is likel
117c3 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77 y.** data that w
117c4 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 as once in other
117c5 20 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65 files that have
117c6 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65 now been delete
117c7 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 d. If the.** ga
117c8 72 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20 rbage data came
117c9 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 from an obsolete
117ca 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 journal file, t
117cb 68 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 he checksums mig
117cc 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 ht.** be correct
117cd 2e 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61 . But by initia
117ce 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b lizing the check
117cf 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 sum to random va
117d0 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 lue which.** is
117d1 64 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 different for ev
117d2 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 ery journal, we
117d3 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69 minimize that ri
117d4 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f sk..*/.static co
117d5 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
117d6 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b r aJournalMagic[
117d7 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 ] = {. 0xd9, 0x
117d8 64 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20 d5, 0x05, 0xf9,
117d9 30 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33 0x20, 0xa1, 0x63
117da 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a , 0xd7,.};../*.*
117db 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 * The size of th
117dc 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 72 e of each page r
117dd 65 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f 75 ecord in the jou
117de 72 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 79 rnal is given by
117df 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e .** the followin
117e0 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66 g macro..*/.#def
117e1 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 ine JOURNAL_PG_S
117e2 5a 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61 Z(pPager) ((pPa
117e3 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b ger->pageSize) +
117e4 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 8)../*.** The j
117e5 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69 ournal header si
117e6 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 ze for this page
117e7 72 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c r. This is usual
117e8 6c 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 ly the same .**
117e9 73 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c 65 size as a single
117ea 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 65 disk sector. Se
117eb 65 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f 72 e also setSector
117ec 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 Size()..*/.#defi
117ed 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 ne JOURNAL_HDR_S
117ee 5a 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 Z(pPager) (pPage
117ef 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a r->sectorSize)..
117f0 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 /*.** The macro
117f1 4d 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 MEMDB is true if
117f2 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 we are dealing
117f3 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 with an in-memor
117f4 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 y database..** W
117f5 65 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d e do this as a m
117f6 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 acro so that if
117f7 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f the SQLITE_OMIT_
117f8 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 MEMORYDB macro i
117f9 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 s set,.** the va
117fa 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c lue of MEMDB wil
117fb 6c 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 l be a constant
117fc 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 and the compiler
117fd 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a will optimize.*
117fe 2a 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 * out code that
117ff 77 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 would never exec
11800 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ute..*/.#ifdef S
11801 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 QLITE_OMIT_MEMOR
11802 59 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d YDB.# define MEM
11803 44 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 DB 0.#else.# def
11804 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 ine MEMDB pPager
11805 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a ->memDb.#endif..
11806 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
11807 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d m legal page num
11808 62 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 31 ber is (2^31 - 1
11809 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 )..*/.#define PA
1180a 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 34 GER_MAX_PGNO 214
1180b 37 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 66 7483647..#ifndef
1180c 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 55 NDEBUG ./*.** U
1180d 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 sage:.**.** as
1180e 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 sert( assert_pag
1180f 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 er_state(pPager)
11810 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e );.*/.static in
11811 74 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 t assert_pager_s
11812 74 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 tate(Pager *pPag
11813 65 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 6d er){.. /* A tem
11814 70 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 73 p-file is always
11815 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 53 in PAGER_EXCLUS
11816 49 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 4e IVE or PAGER_SYN
11817 43 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 CED state. */.
11818 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
11819 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 tempFile==0 || p
1181a 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
1181b 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b GER_EXCLUSIVE );
1181c 0a 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e 67 .. /* The chang
1181d 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 eCountDone flag
1181e 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 6f is always set fo
1181f 72 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f 0a r temp-files */.
11820 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
11821 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c ->tempFile==0 ||
11822 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
11823 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 72 ountDone );.. r
11824 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 eturn 1;.}.#endi
11825 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
11826 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 true if it is ne
11827 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 cessary to write
11828 20 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 page *pPg into
11829 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
1182a 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 .** A page needs
1182b 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 to be written i
1182c 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 nto the sub-jour
1182d 6e 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 nal if there exi
1182e 73 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f sts one.** or mo
1182f 72 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e re open savepoin
11830 74 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a ts for which:.**
11831 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 .** * The page
11832 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 -number is less
11833 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
11834 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
11835 6e 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 nOrig, and.**
11836 2a 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 * The bit corres
11837 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 ponding to the p
11838 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f age-number is no
11839 74 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 t set in.**
1183a 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 PagerSavepoint.p
1183b 49 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a InSavepoint..*/.
1183c 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 static int subjR
1183d 65 71 75 69 72 65 73 50 61 67 65 28 50 67 48 64 equiresPage(PgHd
1183e 72 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 r *pPg){. Pgno
1183f 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f pgno = pPg->pgno
11840 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
11841 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b r = pPg->pPager;
11842 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
11843 69 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e i=0; i<pPager->n
11844 53 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b Savepoint; i++){
11845 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f . PagerSavepo
11846 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 int *p = &pPager
11847 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b ->aSavepoint[i];
11848 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 . if( p->nOri
11849 67 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 g>=pgno && 0==sq
1184a 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
1184b 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c p->pInSavepoint,
1184c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 pgno) ){.
1184d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1184e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1184f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
11850 74 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 true if the page
11851 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
11852 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
11853 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
11854 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 ageInJournal(PgH
11855 64 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 dr *pPg){. retu
11856 72 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 rn sqlite3Bitvec
11857 54 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 Test(pPg->pPager
11858 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 ->pInJournal, pP
11859 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a g->pgno);.}../*.
1185a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 ** Read a 32-bit
1185b 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 integer from th
1185c 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 e given file des
1185d 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 criptor. Store
1185e 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 the integer.** t
1185f 68 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a hat is read in *
11860 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 pRes. Return SQ
11861 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 LITE_OK if every
11862 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 thing worked, or
11863 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 an.** error cod
11864 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 e is something g
11865 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a oes wrong..**.**
11866 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 All values are
11867 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 stored on disk a
11868 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f s big-endian..*/
11869 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 .static int read
1186a 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 32bits(sqlite3_f
1186b 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 ile *fd, i64 off
1186c 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b set, u32 *pRes){
1186d 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
1186e 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 ac[4];. int rc
1186f 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
11870 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 (fd, ac, sizeof(
11871 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 ac), offset);.
11872 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11873 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d K ){. *pRes =
11874 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 sqlite3Get4byte
11875 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 (ac);. }. retu
11876 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11877 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 Write a 32-bit i
11878 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 nteger into a st
11879 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 ring buffer in b
1187a 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f ig-endian byte o
1187b 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rder..*/.#define
1187c 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 put32bits(A,B)
1187d 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 sqlite3Put4byte
1187e 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a ((u8*)A,B)../*.*
1187f 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 * Write a 32-bit
11880 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 integer into th
11881 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 e given file des
11882 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e criptor. Return
11883 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e SQLITE_OK.** on
11884 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 success or an e
11885 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d rror code is som
11886 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ething goes wron
11887 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
11888 20 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c write32bits(sql
11889 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 ite3_file *fd, i
1188a 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 64 offset, u32 v
1188b 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 al){. char ac[4
1188c 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 ];. put32bits(a
1188d 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 c, val);. retur
1188e 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 n sqlite3OsWrite
1188f 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 (fd, ac, 4, offs
11890 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 et);.}../*.** Th
11891 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 e argument to th
11892 69 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 69 is macro is a fi
11893 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 74 le descriptor (t
11894 79 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ype sqlite3_file
11895 2a 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 *)..** Return 0
11896 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 if it is not ope
11897 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28 n, or non-zero (
11898 62 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 74 but not 1) if it
11899 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 is..**.** This
1189a 69 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 65 is so that expre
1189b 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 ssions can be wr
1189c 69 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 itten as:.**.**
1189d 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
1189e 67 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e ger->jfd) ){ ...
1189f 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f .**.** instead o
118a0 66 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50 f.**.** if( pP
118a1 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 ager->jfd->pMeth
118a2 6f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64 ods ){ ....*/.#d
118a3 65 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 64 efine isOpen(pFd
118a4 29 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f ) ((pFd)->pMetho
118a5 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 ds)../*.** If fi
118a6 6c 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 le pFd is open,
118a7 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e call sqlite3OsUn
118a8 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f lock() on it..*/
118a9 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e .static int osUn
118aa 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
118ab 65 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 e *pFd, int eLoc
118ac 6b 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 65 k){. if( !isOpe
118ad 6e 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 65 n(pFd) ){. re
118ae 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
118af 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c }. return sql
118b0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 ite3OsUnlock(pFd
118b1 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a , eLock);.}../*.
118b2 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
118b3 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 determines whet
118b4 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 her or not the a
118b5 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
118b6 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 mization.** can
118b7 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69 be used with thi
118b8 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 s pager. The opt
118b9 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 imization can be
118ba 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 used if:.**.**
118bb 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 (a) the value r
118bc 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 eturned by OsDev
118bd 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
118be 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 cs() indicates t
118bf 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 hat.** a da
118c0 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 tabase page may
118c1 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 be written atomi
118c2 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 cally, and.** (
118c3 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 b) the value ret
118c4 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f urned by OsSecto
118c5 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 rSize() is less
118c6 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a than or equal.**
118c7 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 to the pag
118c8 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e size..**.** Th
118c9 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
118ca 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 6e s also always en
118cb 61 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 abled for tempor
118cc 61 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 73 ary files. It is
118cd 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 .** an error to
118ce 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 call this functi
118cf 6f 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 20 on if pPager is
118d0 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e 2d opened on an in-
118d1 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 memory.** databa
118d2 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
118d3 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 optimization ca
118d4 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 20 nnot be used, 0
118d5 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
118d6 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c 0a it can be used,.
118d7 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 ** then the valu
118d8 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 e returned is th
118d9 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f e size of the jo
118da 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 urnal file when
118db 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 72 it.** contains r
118dc 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f 72 ollback data for
118dd 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 exactly one pag
118de 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c e..*/.#ifdef SQL
118df 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
118e0 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 69 C_WRITE.static i
118e1 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a nt jrnlBufferSiz
118e2 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
118e3 7b 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d {. assert( !MEM
118e4 44 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 DB );. if( !pPa
118e5 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b ger->tempFile ){
118e6 0a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 20 . int dc;
118e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118e8 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 /* Device
118e9 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
118ea 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 63 */. int nSec
118eb 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 tor;
118ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 /* Sec
118ed 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 tor size */.
118ee 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 int szPage;
118ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118f0 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a /* Page size *
118f1 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 /.. assert( i
118f2 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
118f3 29 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 71 ) );. dc = sq
118f4 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 lite3OsDeviceCha
118f5 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 racteristics(pPa
118f6 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e 53 ger->fd);. nS
118f7 65 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d 3e ector = pPager->
118f8 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 sectorSize;.
118f9 73 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d szPage = pPager-
118fa 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 20 >pageSize;..
118fb 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f assert(SQLITE_IO
118fc 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 CAP_ATOMIC512==(
118fd 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 73 512>>8));. as
118fe 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 sert(SQLITE_IOCA
118ff 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 P_ATOMIC64K==(65
11900 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 66 536>>8));. if
11901 28 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 45 ( 0==(dc&(SQLITE
11902 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73 _IOCAP_ATOMIC|(s
11903 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e 53 zPage>>8)) || nS
11904 65 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 7b ector>szPage) ){
11905 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
11906 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 . }. }.. re
11907 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 turn JOURNAL_HDR
11908 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a 4f _SZ(pPager) + JO
11909 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 URNAL_PG_SZ(pPag
1190a 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f er);.}.#endif../
1190b 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 *.** If SQLITE_C
1190c 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 HECK_PAGES is de
1190d 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f fined then we do
1190e 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 some sanity che
1190f 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 cking.** on the
11910 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 cache using a ha
11911 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 sh function. Th
11912 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 is is used for t
11913 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 esting.** and de
11914 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f bugging only..*/
11915 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
11916 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a HECK_PAGES./*.**
11917 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 Return a 32-bit
11918 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 hash of the pag
11919 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 e data for pPage
1191a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 ..*/.static u32
1191b 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 pager_datahash(i
1191c 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e nt nByte, unsign
1191d 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b ed char *pData){
1191e 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b . u32 hash = 0;
1191f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
11920 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b i=0; i<nByte; i+
11921 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 +){. hash = (
11922 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 hash*1039) + pDa
11923 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 ta[i];. }. ret
11924 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 urn hash;.}.stat
11925 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 ic u32 pager_pag
11926 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 ehash(PgHdr *pPa
11927 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 ge){. return pa
11928 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 ger_datahash(pPa
11929 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 ge->pPager->page
1192a 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 Size, (unsigned
1192b 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 char *)pPage->pD
1192c 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 ata);.}.static v
1192d 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 61 oid pager_set_pa
1192e 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 gehash(PgHdr *pP
1192f 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 age){. pPage->p
11930 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f ageHash = pager_
11931 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29 3b pagehash(pPage);
11932 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 .}../*.** The CH
11933 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 ECK_PAGE macro t
11934 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 akes a PgHdr* as
11935 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 an argument. If
11936 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
11937 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 GES.** is define
11938 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 d, and NDEBUG is
11939 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e not defined, an
1193a 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
1193b 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 ent checks.** th
1193c 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 65 at the page is e
1193d 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 ither dirty or s
1193e 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 till matches the
1193f 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 calculated page
11940 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e -hash..*/.#defin
11941 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 e CHECK_PAGE(x)
11942 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 checkPage(x).sta
11943 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 tic void checkPa
11944 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a ge(PgHdr *pPg){.
11945 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
11946 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 = pPg->pPager;.
11947 20 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 assert( !pPg->p
11948 61 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 ageHash || pPage
11949 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 r->errCode.
1194a 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 || (pPg->flags&
1194b 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 PGHDR_DIRTY) ||
1194c 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 pPg->pageHash==p
1194d 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
1194e 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 g) );.}..#else.#
1194f 64 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 define pager_dat
11950 61 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 ahash(X,Y) 0.#d
11951 65 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 efine pager_page
11952 68 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 hash(X) 0.#defi
11953 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 ne CHECK_PAGE(x)
11954 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 .#endif /* SQLI
11955 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a TE_CHECK_PAGES *
11956 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 /../*.** When th
11957 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 is is called the
11958 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
11959 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d r pager pPager m
1195a 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 ust be open..**
1195b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 This function at
1195c 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 61 tempts to read a
1195d 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1195e 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 file name from t
1195f 68 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 he .** end of th
11960 65 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 73 e file and, if s
11961 75 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 65 uccessful, copie
11962 73 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 s it into memory
11963 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79 supplied .** by
11964 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 the caller. See
11965 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 comments above
11966 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e writeMasterJourn
11967 61 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f 72 al() for the for
11968 6d 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 mat.** used to s
11969 74 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a 6f tore a master jo
1196a 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
1196b 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 at the end of a
1196c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a journal file..**
1196d 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 .** zMaster must
1196e 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 point to a buff
1196f 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e er of at least n
11970 4d 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c Master bytes all
11971 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 ocated by.** the
11972 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 caller. This sh
11973 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f ould be sqlite3_
11974 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 vfs.mxPathname+1
11975 20 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 (to ensure ther
11976 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 e is.** enough s
11977 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 pace to write th
11978 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11979 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d name). If the m
1197a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a aster journal.**
1197b 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 name in the jou
1197c 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 rnal is longer t
1197d 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 han nMaster byte
1197e 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a s (including a.*
1197f 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 * nul-terminator
11980 29 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 ), then this is
11981 68 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f handled as if no
11982 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11983 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 name.** were pre
11984 73 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 sent in the jour
11985 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 nal..**.** If a
11986 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
11987 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 ile name is pres
11988 65 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f ent at the end o
11989 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a f the journal.**
1198a 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 file, then it i
1198b 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 s copied into th
1198c 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 e buffer pointed
1198d 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e 20 to by zMaster.
1198e 41 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 A.** nul-termina
1198f 74 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 65 tor byte is appe
11990 6e 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 66 nded to the buff
11991 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 er following the
11992 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e master.** journ
11993 61 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a al file name..**
11994 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 74 .** If it is det
11995 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 ermined that no
11996 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
11997 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 ile name is pres
11998 65 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b ent .** zMaster[
11999 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 0] is set to 0 a
1199a 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
1199b 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
1199c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1199d 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72 while reading fr
1199e 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 om the journal f
1199f 69 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a ile, an SQLite.*
119a0 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 * error code is
119a1 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
119a2 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74 tic int readMast
119a3 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 erJournal(sqlite
119a4 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 3_file *pJrnl, c
119a5 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33 har *zMaster, u3
119a6 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 2 nMaster){. in
119a7 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
119a8 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
119a9 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 rn code */. u32
119aa 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 len;
119ab 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
119ac 68 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d 61 h in bytes of ma
119ad 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
119ae 65 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20 e */. i64 szJ;
119af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119b0 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 /* Total size
119b1 69 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 72 in bytes of jour
119b2 6e 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a nal file pJrnl *
119b3 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 /. u32 cksum;
119b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
119b5 2a 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 61 * MJ checksum va
119b6 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f lue read from jo
119b7 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 urnal */. u32 u
119b8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
119b9 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 /* Unsigne
119ba 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a d loop counter *
119bb 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
119bc 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f r aMagic[8]; /
119bd 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f * A buffer to ho
119be 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 ld the magic hea
119bf 64 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 der */. zMaster
119c0 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69 [0] = '\0';.. i
119c1 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 f( SQLITE_OK!=(r
119c2 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
119c3 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a eSize(pJrnl, &sz
119c4 4a 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36 J)). || szJ<16
119c5 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b . || SQLITE_OK
119c6 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 !=(rc = read32bi
119c7 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 ts(pJrnl, szJ-16
119c8 2c 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c , &len)). || l
119c9 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20 en>=nMaster .
119ca 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 || SQLITE_OK!=(r
119cb 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 c = read32bits(p
119cc 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 Jrnl, szJ-12, &c
119cd 6b 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c ksum)). || SQL
119ce 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
119cf 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e lite3OsRead(pJrn
119d0 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a l, aMagic, 8, sz
119d1 4a 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63 J-8)). || memc
119d2 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 mp(aMagic, aJour
119d3 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 20 nalMagic, 8).
119d4 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 || SQLITE_OK!=(r
119d5 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
119d6 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 d(pJrnl, zMaster
119d7 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 , len, szJ-16-le
119d8 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 n)). ){. ret
119d9 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f urn rc;. }.. /
119da 2a 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65 * See if the che
119db 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68 cksum matches th
119dc 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
119dd 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 name */. for(u
119de 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b =0; u<len; u++){
119df 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d . cksum -= zM
119e0 61 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20 aster[u];. }.
119e1 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20 if( cksum ){.
119e2 20 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 6b /* If the check
119e3 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 20 sum doesn't add
119e4 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20 up, then one or
119e5 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 6b more of the disk
119e6 20 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20 sectors. **
119e7 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d containing the m
119e8 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
119e9 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 70 lename is corrup
119ea 74 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a ted. This means.
119eb 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c ** definitel
119ec 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 y roll back, so
119ed 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 just return SQLI
119ee 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 TE_OK and report
119ef 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20 a (nul). **
119f0 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 master-journal f
119f1 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a ilename.. */.
119f2 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d len = 0;. }
119f3 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 . zMaster[len]
119f4 3d 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65 = '\0';. . re
119f5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
119f6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
119f7 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 the offset of th
119f8 65 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 e sector boundar
119f9 79 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 74 y at or immediat
119fa 65 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e ely .** followin
119fb 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 70 g the value in p
119fc 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
119fd 66 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 65 f, assuming a se
119fe 63 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 ctor .** size of
119ff 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
11a00 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a ize bytes..**.**
11a01 20 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f i.e for a secto
11a02 72 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a r size of 512:.*
11a03 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 *.** Pager.jou
11a04 72 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20 rnalOff
11a05 20 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a Return value.**
11a06 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -------------
11a07 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
11a08 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 ----------.**
11a09 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
11a0a 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 20 0.**
11a0b 20 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 512
11a0c 20 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 512.*
11a0d 2a 20 20 20 31 30 30 20 20 20 20 20 20 20 20 20 * 100
11a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 51
11a0f 32 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 20 2.** 2000
11a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a11 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 2048.** .*/.sta
11a12 74 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 tic i64 journalH
11a13 64 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 2a drOffset(Pager *
11a14 70 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f pPager){. i64 o
11a15 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 ffset = 0;. i64
11a16 20 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 c = pPager->jou
11a17 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 rnalOff;. if( c
11a18 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d ){. offset =
11a19 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f ((c-1)/JOURNAL_
11a1a 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b HDR_SZ(pPager) +
11a1b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 1) * JOURNAL_HD
11a1c 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 R_SZ(pPager);.
11a1d 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 }. assert( offs
11a1e 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 et%JOURNAL_HDR_S
11a1f 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a Z(pPager)==0 );.
11a20 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 assert( offset
11a21 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 >=c );. assert(
11a22 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 (offset-c)<JOUR
11a23 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
11a24 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f r) );. return o
11a25 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ffset;.}../*.**
11a26 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
11a27 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 must be open wh
11a28 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
11a29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a is called..**.*
11a2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
11a2b 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 is a no-op if th
11a2c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 e journal file h
11a2d 61 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 74 as not been writ
11a2e 74 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e ten to.** within
11a2f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
11a30 6e 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69 nsaction (i.e. i
11a31 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f f Pager.journalO
11a32 66 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 ff==0)..**.** If
11a33 20 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 6e doTruncate is n
11a34 6f 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 50 on-zero or the P
11a35 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ager.journalSize
11a36 4c 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 69 Limit variable i
11a37 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 74 s.** set to 0, t
11a38 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65 hen truncate the
11a39 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f journal file to
11a3a 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 zero bytes in s
11a3b 69 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a ize. Otherwise,.
11a3c 2a 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d 62 ** zero the 28-b
11a3d 79 74 65 20 68 65 61 64 65 72 20 61 74 20 74 68 yte header at th
11a3e 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a e start of the j
11a3f 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20 ournal file. In
11a40 65 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a either case, .**
11a41 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 if the pager is
11a42 20 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 not in no-sync
11a43 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a mode, sync the j
11a44 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65 ournal file imme
11a45 64 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65 diately .** afte
11a46 72 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 75 r writing or tru
11a47 6e 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a ncating it..**.*
11a48 2a 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e * If Pager.journ
11a49 61 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 alSizeLimit is s
11a4a 65 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65 et to a positive
11a4b 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 , non-zero value
11a4c 2c 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 , and.** followi
11a4d 6e 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f ng the truncatio
11a4e 6e 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 n or zeroing des
11a4f 63 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65 cribed above the
11a50 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a size of the .**
11a51 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e journal file in
11a52 20 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72 bytes is larger
11a53 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 than this value
11a54 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 , then truncate
11a55 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 the.** journal f
11a56 69 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75 ile to Pager.jou
11a57 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79 rnalSizeLimit by
11a58 74 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c tes. The journal
11a59 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f file does.** no
11a5a 74 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 6e t need to be syn
11a5b 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ced following th
11a5c 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a is operation..**
11a5d 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 .** If an IO err
11a5e 6f 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e 64 or occurs, aband
11a5f 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e on processing an
11a60 64 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f 20 d return the IO
11a61 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f error code..** O
11a62 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e therwise, return
11a63 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 SQLITE_OK..*/.s
11a64 74 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f tatic int zeroJo
11a65 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a urnalHdr(Pager *
11a66 70 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 pPager, int doTr
11a67 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 uncate){. int r
11a68 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
11a69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a6a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11a6b 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
11a6c 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
11a6d 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
11a6e 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f if( pPager->jo
11a6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 urnalOff ){.
11a70 63 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 74 const i64 iLimit
11a71 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
11a72 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 alSizeLimit;
11a73 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f /* Local cache o
11a74 66 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f f jsl */.. IO
11a75 54 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 TRACE(("JZEROHDR
11a76 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 %p\n", pPager))
11a77 0a 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63 . if( doTrunc
11a78 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 ate || iLimit==0
11a79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
11a7a 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
11a7b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 (pPager->jfd, 0)
11a7c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
11a7d 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
11a7e 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d char zeroHdr[28]
11a7f 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63 = {0};. rc
11a80 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
11a81 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a e(pPager->jfd, z
11a82 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a eroHdr, sizeof(z
11a83 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 eroHdr), 0);.
11a84 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
11a85 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 QLITE_OK && !pPa
11a86 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 ger->noSync ){.
11a87 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11a88 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 3OsSync(pPager->
11a89 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 jfd, SQLITE_SYNC
11a8a 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 _DATAONLY|pPager
11a8b 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 ->sync_flags);.
11a8c 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 }.. /* At
11a8d 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74 this point the t
11a8e 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f ransaction is co
11a8f 6d 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20 mmitted but the
11a90 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20 write lock .
11a91 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64 ** is still held
11a92 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66 on the file. If
11a93 20 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65 there is a size
11a94 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65 limit configure
11a95 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 d for . ** th
11a96 65 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 e persistent jou
11a97 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75 rnal and the jou
11a98 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e rnal file curren
11a99 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 tly consumes mor
11a9a 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74 e. ** space t
11a9b 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61 han that limit a
11a9c 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63 llows for, trunc
11a9d 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72 ate it now. Ther
11a9e 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 e is no need.
11a9f 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20 ** to sync the
11aa0 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 file following t
11aa1 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 his operation..
11aa2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 */. if( rc
11aa3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
11aa4 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20 Limit>0 ){.
11aa5 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72 i64 sz;. r
11aa6 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
11aa7 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 eSize(pPager->jf
11aa8 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69 d, &sz);. i
11aa9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11aaa 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b && sz>iLimit ){
11aab 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
11aac 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
11aad 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 pPager->jfd, iLi
11aae 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 mit);. }.
11aaf 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
11ab0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
11ab1 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d e journal file m
11ab2 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e ust be open when
11ab3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
11ab4 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e called. A journ
11ab5 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f al.** header (JO
11ab6 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 URNAL_HDR_SZ byt
11ab7 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 es) is written i
11ab8 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
11ab9 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 file at the.** c
11aba 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e urrent location.
11abb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 .**.** The forma
11abc 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 t for the journa
11abd 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 l header is as f
11abe 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 ollows:.** - 8 b
11abf 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e ytes: Magic iden
11ac0 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 tifying journal
11ac1 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 format..** - 4 b
11ac2 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 ytes: Number of
11ac3 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e records in journ
11ac4 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e al, or -1 no-syn
11ac5 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a c mode is on..**
11ac6 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 - 4 bytes: Rand
11ac7 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 om number used f
11ac8 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a or page hash..**
11ac9 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 - 4 bytes: Init
11aca 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 ial database pag
11acb 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 e count..** - 4
11acc 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 bytes: Sector si
11acd 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 ze used by the p
11ace 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 rocess that wrot
11acf 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a e this journal..
11ad0 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 ** - 4 bytes: Da
11ad1 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 tabase page size
11ad2 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 ..** .** Followe
11ad3 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 d by (JOURNAL_HD
11ad4 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 R_SZ - 28) bytes
11ad5 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 of unused space
11ad6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11ad7 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
11ad8 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11ad9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
11ada 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
11adb 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11adc 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a code */. char *
11add 7a 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 zHeader = pPager
11ade 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f 2a ->pTmpSpace; /*
11adf 20 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 Temporary space
11ae0 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 68 used to build h
11ae1 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e eader */. u32 n
11ae2 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d Header = pPager-
11ae3 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 2f >pageSize; /
11ae4 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 * Size of buffer
11ae5 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a pointed to by z
11ae6 48 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 Header */. u32
11ae7 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 nWrite;
11ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ae9 2f 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 64 /* Bytes of head
11aea 65 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 65 er sector writte
11aeb 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 20 n */. int ii;
11aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11aed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
11aee 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 op counter */..
11aef 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
11af0 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 pPager->jfd) );
11af1 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 /* Journal
11af2 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 file must be ope
11af3 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 65 n. */.. if( nHe
11af4 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 ader>JOURNAL_HDR
11af5 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 _SZ(pPager) ){.
11af6 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 nHeader = JOU
11af7 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11af8 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 er);. }.. /* I
11af9 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 f there are acti
11afa 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e ve savepoints an
11afb 64 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 d any of them we
11afc 72 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a 2a re created . **
11afd 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 since the most
11afe 72 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 recent journal h
11aff 65 61 64 65 72 20 77 61 73 20 77 72 69 74 74 65 eader was writte
11b00 6e 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a 20 n, update the .
11b01 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 ** PagerSavepoi
11b02 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 nt.iHdrOffset fi
11b03 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 elds now.. */.
11b04 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 for(ii=0; ii<pP
11b05 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
11b06 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 ; ii++){. if(
11b07 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f pPager->aSavepo
11b08 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 int[ii].iHdrOffs
11b09 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 et==0 ){. p
11b0a 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
11b0b 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74 t[ii].iHdrOffset
11b0c 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
11b0d 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d alOff;. }. }
11b0e 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 .. pPager->jour
11b0f 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d nalHdr = pPager-
11b10 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f >journalOff = jo
11b11 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 urnalHdrOffset(p
11b12 50 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20 Pager);.. /* .
11b13 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 ** Write the nR
11b14 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e ec Field - the n
11b15 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 umber of page re
11b16 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f cords that follo
11b17 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 w this. ** jour
11b18 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d nal header. Norm
11b19 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 ally, zero is wr
11b1a 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 itten to this va
11b1b 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 lue at this time
11b1c 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 .. ** After the
11b1d 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 records are add
11b1e 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 ed to the journa
11b1f 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e l (and the journ
11b20 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a al synced, . **
11b21 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 if in full-sync
11b22 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f mode), the zero
11b23 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 is overwritten
11b24 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 with the true nu
11b25 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 mber. ** of rec
11b26 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f ords (see syncJo
11b27 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 urnal()).. **.
11b28 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 ** A faster alt
11b29 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 ernative is to w
11b2a 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20 rite 0xFFFFFFFF
11b2b 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c to the nRec fiel
11b2c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 d. When. ** rea
11b2d 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c ding the journal
11b2e 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c this value tell
11b2f 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 s SQLite to assu
11b30 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a me that the. **
11b31 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 rest of the jou
11b32 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 rnal file contai
11b33 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 ns valid page re
11b34 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 cords. This assu
11b35 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 mption. ** is d
11b36 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 angerous, as if
11b37 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 a failure occurr
11b38 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e ed whilst writin
11b39 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c g to the journal
11b3a 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 . ** file it ma
11b3b 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 y contain some g
11b3c 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65 arbage data. The
11b3d 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61 re are two scena
11b3e 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 rios. ** where
11b3f 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65 this risk can be
11b40 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 ignored:. **.
11b41 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 ** * When the
11b42 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d pager is in no-
11b43 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 sync mode. Corru
11b44 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 ption can follow
11b45 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 a. ** powe
11b46 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 r failure in thi
11b47 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 s case anyway..
11b48 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 **. ** * Whe
11b49 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 n the SQLITE_IOC
11b4a 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66 AP_SAFE_APPEND f
11b4b 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73 lag is set. This
11b4c 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a guarantees. **
11b4d 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67 that garbag
11b4e 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20 e data is never
11b4f 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 appended to the
11b50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 journal file..
11b51 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f */. assert( isO
11b52 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
11b53 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e || pPager->noSyn
11b54 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 c );. if( (pPag
11b55 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 er->noSync) || (
11b56 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
11b57 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
11b58 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 ALMODE_MEMORY).
11b59 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 || (sqlite3OsD
11b5a 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
11b5b 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
11b5c 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
11b5d 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b FE_APPEND) . ){
11b5e 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 . memcpy(zHea
11b5f 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 der, aJournalMag
11b60 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 ic, sizeof(aJour
11b61 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 20 nalMagic));.
11b62 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
11b63 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
11b64 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66 alMagic)], 0xfff
11b65 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b fffff);. }else{
11b66 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 61 . memset(zHea
11b67 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 der, 0, sizeof(a
11b68 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 29 JournalMagic)+4)
11b69 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
11b6a 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73 random check-has
11b6b 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f h initialiser */
11b6c 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 . sqlite3_rand
11b6d 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50 omness(sizeof(pP
11b6e 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 ager->cksumInit)
11b6f 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d , &pPager->cksum
11b70 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69 Init);. put32bi
11b71 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 ts(&zHeader[size
11b72 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
11b73 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b )+4], pPager->ck
11b74 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54 sumInit);. /* T
11b75 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62 he initial datab
11b76 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 ase size */. pu
11b77 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 t32bits(&zHeader
11b78 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
11b79 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65 Magic)+8], pPage
11b7a 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a r->dbOrigSize);.
11b7b 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 /* The assumed
11b7c 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 sector size for
11b7d 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f this process */
11b7e 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 . put32bits(&zH
11b7f 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
11b80 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c urnalMagic)+12],
11b81 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
11b82 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 ize);.. /* The
11b83 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 page size */. p
11b84 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
11b85 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
11b86 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61 lMagic)+16], pPa
11b87 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
11b88 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 . /* Initializi
11b89 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 ng the tail of t
11b8a 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 he buffer is not
11b8b 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 65 necessary. Eve
11b8c 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 rything. ** wor
11b8d 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 66 ks find if the f
11b8e 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 ollowing memset(
11b8f 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 ) is omitted. B
11b90 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a ut initializing.
11b91 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 ** the memory
11b92 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 6e prevents valgrin
11b93 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 d from complaini
11b94 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 69 ng, so we are wi
11b95 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 lling to. ** ta
11b96 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e ke the performan
11b97 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d ce hit.. */. m
11b98 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b 73 emset(&zHeader[s
11b99 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
11b9a 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 20 gic)+20], 0,.
11b9b 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 73 nHeader-(s
11b9c 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
11b9d 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f 2a gic)+20));.. /*
11b9e 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69 In theory, it i
11b9f 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79 s only necessary
11ba0 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38 to write the 28
11ba1 20 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20 bytes that the
11ba2 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 . ** journal he
11ba3 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f ader consumes to
11ba4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11ba5 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63 e here. Then inc
11ba6 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a 2a rement the . **
11ba7 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
11ba8 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a 4f f variable by JO
11ba9 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f 20 URNAL_HDR_SZ so
11baa 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a 20 that the next .
11bab 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 72 ** record is wr
11bac 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f 6c itten to the fol
11bad 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 6c lowing sector (l
11bae 65 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e 20 eaving a gap in
11baf 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 68 the file. ** th
11bb0 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c 69 at will be impli
11bb1 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e 20 citly filled in
11bb2 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a 2a by the OS).. **
11bb3 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 74 . ** However it
11bb4 20 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f 76 has been discov
11bb5 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f 6d ered that on som
11bb6 65 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 70 e systems this p
11bb7 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a 2a attern can . **
11bb8 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c be significantl
11bb9 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 6f y slower than co
11bba 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 69 ntiguously writi
11bbb 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 ng data to the f
11bbc 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 69 ile,. ** even i
11bbd 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 70 f that means exp
11bbe 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 20 licitly writing
11bbf 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f 63 data to the bloc
11bc0 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 52 k of . ** (JOUR
11bc1 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 NAL_HDR_SZ - 28)
11bc2 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c bytes that will
11bc3 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 6f not be used. So
11bc4 20 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 20 that is what.
11bc5 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 2a ** is done. . *
11bc6 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 20 *. ** The loop
11bc7 69 73 20 72 65 71 75 69 72 65 64 20 68 65 72 65 is required here
11bc8 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 63 in case the sec
11bc9 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 tor-size is larg
11bca 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a er than the . *
11bcb 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 * database page
11bcc 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 20 size. Since the
11bcd 7a 48 65 61 64 65 72 20 62 75 66 66 65 72 20 69 zHeader buffer i
11bce 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 67 s only Pager.pag
11bcf 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73 eSize. ** bytes
11bd0 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74 in size, more t
11bd1 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 han one call to
11bd2 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 sqlite3OsWrite()
11bd3 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64 may be required
11bd4 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74 . ** to populat
11bd5 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 e the entire jou
11bd6 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 74 rnal header sect
11bd7 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 or.. */ . for(
11bd8 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 nWrite=0; rc==SQ
11bd9 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c LITE_OK&&nWrite<
11bda 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
11bdb 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d Pager); nWrite+=
11bdc 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f nHeader){. IO
11bdd 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 TRACE(("JHDR %p
11bde 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 %lld %d\n", pPag
11bdf 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 er, pPager->jour
11be0 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 nalHdr, nHeader)
11be1 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ). rc = sqlit
11be2 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
11be3 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 ->jfd, zHeader,
11be4 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d nHeader, pPager-
11be5 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 >journalOff);.
11be6 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11be7 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b lOff += nHeader;
11be8 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
11be9 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
11bea 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 journal file mus
11beb 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 t be open when t
11bec 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 his is called. A
11bed 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
11bee 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c file.** (JOURNAL
11bef 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 _HDR_SZ bytes) i
11bf0 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 s read from the
11bf1 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
11bf2 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a in the journal.
11bf3 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 ** file. The cur
11bf4 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e rent location in
11bf5 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11bf6 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a e is given by.**
11bf7 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11bf8 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 Off. See comment
11bf9 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e s above function
11bfa 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 writeJournalHdr
11bfb 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63 () for.** a desc
11bfc 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a ription of the j
11bfd 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f ournal header fo
11bfe 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rmat..**.** If t
11bff 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 he header is rea
11c00 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 d successfully,
11c01 2a 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f *pNRec is set to
11c02 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
11c03 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 * page records f
11c04 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 ollowing this he
11c05 61 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a ader and *pDbSiz
11c06 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
11c07 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 size of the.** d
11c08 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 atabase before t
11c09 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 he transaction b
11c0a 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 egan, in pages.
11c0b 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b Also, pPager->ck
11c0c 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 sumInit.** is se
11c0d 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 t to the value r
11c0e 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ead from the jou
11c0f 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c rnal header. SQL
11c10 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
11c11 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 ed.** in this ca
11c12 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
11c13 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
11c14 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 file appears to
11c15 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 be corrupted, SQ
11c16 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 LITE_DONE is.**
11c17 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e returned and *pN
11c18 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65 Rec and *PDbSize
11c19 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 are undefined.
11c1a 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f If JOURNAL_HDR_
11c1b 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e SZ bytes.** cann
11c1c 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 ot be read from
11c1d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11c1e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 an error code i
11c1f 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 s returned..*/.s
11c20 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f tatic int readJo
11c21 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 urnalHdr(. Page
11c22 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
11c23 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
11c24 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e r object */. in
11c25 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a t isHot,. i64 j
11c26 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20 ournalSize,
11c27 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
11c28 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72 of the open jour
11c29 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 nal file in byte
11c2a 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65 s */. u32 *pNRe
11c2b 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
11c2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 /* OUT: Valu
11c2d 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 e read from the
11c2e 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20 nRec field */.
11c2f 75 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20 u32 *pDbSize
11c30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11c31 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72 OUT: Value of or
11c32 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 iginal database
11c33 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b size field */.){
11c34 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
11c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c36 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
11c37 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 /. unsigned cha
11c38 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20 r aMagic[8];
11c39 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 /* A buffer to
11c3a 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 hold the magic h
11c3b 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 eader */. i64 i
11c3c 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 HdrOff;
11c3d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
11c3e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 t of journal hea
11c3f 64 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a der being read *
11c40 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f /.. assert( isO
11c41 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
11c42 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 ); /* Jour
11c43 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 nal file must be
11c44 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 open. */.. /*
11c45 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f Advance Pager.jo
11c46 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20 urnalOff to the
11c47 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 start of the nex
11c48 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65 t sector. If the
11c49 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 . ** journal fi
11c4a 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 le is too small
11c4b 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 for there to be
11c4c 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20 a header stored
11c4d 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 at this. ** poi
11c4e 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 nt, return SQLIT
11c4f 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 E_DONE.. */. p
11c50 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11c51 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 f = journalHdrOf
11c52 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 fset(pPager);.
11c53 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 if( pPager->jour
11c54 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 nalOff+JOURNAL_H
11c55 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 DR_SZ(pPager) >
11c56 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 journalSize ){.
11c57 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
11c58 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64 _DONE;. }. iHd
11c59 72 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a rOff = pPager->j
11c5a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a ournalOff;.. /*
11c5b 20 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72 Read in the fir
11c5c 73 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 st 8 bytes of th
11c5d 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
11c5e 2e 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 . If they do not
11c5f 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20 match. ** the
11c60 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f magic string fo
11c61 75 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74 und at the start
11c62 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c of each journal
11c63 20 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a header, return.
11c64 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 ** SQLITE_DONE
11c65 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
11c66 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 occurs, return
11c67 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f an error code. O
11c68 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70 therwise,. ** p
11c69 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 roceed.. */. i
11c6a 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 f( isHot || iHdr
11c6b 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 Off!=pPager->jou
11c6c 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72 rnalHdr ){. r
11c6d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
11c6e 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 d(pPager->jfd, a
11c6f 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d Magic, sizeof(aM
11c70 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b agic), iHdrOff);
11c71 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
11c72 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
11c73 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 }. if( me
11c74 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f mcmp(aMagic, aJo
11c75 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 urnalMagic, size
11c76 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 of(aMagic))!=0 )
11c77 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
11c78 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
11c79 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 }. }.. /* Read
11c7a 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 the first three
11c7b 20 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f 32-bit fields o
11c7c 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 f the journal he
11c7d 61 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20 ader: The nRec.
11c7e 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63 ** field, the c
11c7f 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69 hecksum-initiali
11c80 7a 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61 zer and the data
11c81 62 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65 base size at the
11c82 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 start. ** of t
11c83 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
11c84 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
11c85 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 code if anything
11c86 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a goes wrong.. *
11c87 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f /. if( SQLITE_O
11c88 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 K!=(rc = read32b
11c89 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c its(pPager->jfd,
11c8a 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 iHdrOff+8, pNRe
11c8b 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 c)). || SQLITE
11c8c 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
11c8d 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
11c8e 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 d, iHdrOff+12, &
11c8f 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
11c90 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 t)). || SQLITE
11c91 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
11c92 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
11c93 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 d, iHdrOff+16, p
11c94 44 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20 DbSize)). ){.
11c95 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
11c96 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
11c97 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b journalOff==0 ){
11c98 0a 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69 . u32 iPageSi
11c99 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
11c9a 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66 /* Page-size f
11c9b 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 ield of journal
11c9c 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 header */. u3
11c9d 32 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20 2 iSectorSize;
11c9e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
11c9f 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 ctor-size field
11ca0 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 of journal heade
11ca1 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61 r */. u16 iPa
11ca2 67 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20 geSize16;
11ca3 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
11ca4 20 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36 iPageSize in 16
11ca5 2d 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f -bit variable */
11ca6 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 .. /* Read th
11ca7 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 e page-size and
11ca8 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 sector-size jour
11ca9 6e 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 nal header field
11caa 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 s. */. if( SQ
11cab 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
11cac 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 ead32bits(pPager
11cad 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 ->jfd, iHdrOff+2
11cae 30 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 0, &iSectorSize)
11caf 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 ). || SQLITE
11cb0 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
11cb1 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
11cb2 64 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 d, iHdrOff+24, &
11cb3 69 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 iPageSize)).
11cb4 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
11cb5 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f rc;. }.. /
11cb6 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
11cb7 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f values read fro
11cb8 6d 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 m the page-size
11cb9 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 and sector-size
11cba 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 fields. ** ar
11cbb 65 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 e within range.
11cbc 54 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27 To be 'in range'
11cbd 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 , both values ne
11cbe 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 ed to be a power
11cbf 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 . ** of two g
11cc0 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
11cc1 71 75 61 6c 20 74 6f 20 35 31 32 20 6f 72 20 33 qual to 512 or 3
11cc2 32 2c 20 61 6e 64 20 6e 6f 74 20 67 72 65 61 74 2, and not great
11cc3 65 72 20 74 68 61 6e 20 74 68 65 69 72 20 0a 20 er than their .
11cc4 20 20 20 2a 2a 20 72 65 73 70 65 63 74 69 76 65 ** respective
11cc5 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 6d 61 compile time ma
11cc6 78 69 6d 75 6d 20 6c 69 6d 69 74 73 2e 0a 20 20 ximum limits..
11cc7 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 61 */. if( iPa
11cc8 67 65 53 69 7a 65 3c 35 31 32 20 20 20 20 20 20 geSize<512
11cc9 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 69 || i
11cca 53 65 63 74 6f 72 53 69 7a 65 3c 33 32 0a 20 20 SectorSize<32.
11ccb 20 20 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 3e || iPageSize>
11ccc 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
11ccd 53 49 5a 45 20 7c 7c 20 69 53 65 63 74 6f 72 53 SIZE || iSectorS
11cce 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 ize>MAX_SECTOR_S
11ccf 49 5a 45 0a 20 20 20 20 20 7c 7c 20 28 28 69 50 IZE. || ((iP
11cd0 61 67 65 53 69 7a 65 2d 31 29 26 69 50 61 67 65 ageSize-1)&iPage
11cd1 53 69 7a 65 29 21 3d 30 20 20 20 7c 7c 20 28 28 Size)!=0 || ((
11cd2 69 53 65 63 74 6f 72 53 69 7a 65 2d 31 29 26 69 iSectorSize-1)&i
11cd3 53 65 63 74 6f 72 53 69 7a 65 29 21 3d 30 20 0a SectorSize)!=0 .
11cd4 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ){. /*
11cd5 49 66 20 74 68 65 20 65 69 74 68 65 72 20 74 68 If the either th
11cd6 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 72 20 73 e page-size or s
11cd7 65 63 74 6f 72 2d 73 69 7a 65 20 69 6e 20 74 68 ector-size in th
11cd8 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 e journal-header
11cd9 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e is . ** in
11cda 76 61 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20 valid, then the
11cdb 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f process that wro
11cdc 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 te the journal-h
11cdd 65 61 64 65 72 20 6d 75 73 74 20 68 61 76 65 20 eader must have
11cde 0a 20 20 20 20 20 20 2a 2a 20 63 72 61 73 68 65 . ** crashe
11cdf 64 20 62 65 66 6f 72 65 20 74 68 65 20 68 65 61 d before the hea
11ce0 64 65 72 20 77 61 73 20 73 79 6e 63 65 64 2e 20 der was synced.
11ce1 49 6e 20 74 68 69 73 20 63 61 73 65 20 73 74 6f In this case sto
11ce2 70 20 72 65 61 64 69 6e 67 20 0a 20 20 20 20 20 p reading .
11ce3 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ** the journal
11ce4 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20 file here..
11ce5 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
11ce6 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
11ce7 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 }.. /* Upda
11ce8 74 65 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 te the page-size
11ce9 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 to match the va
11cea 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 lue read from th
11ceb 65 20 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 e journal. .
11cec 2a 2a 20 55 73 65 20 61 20 74 65 73 74 63 61 73 ** Use a testcas
11ced 65 28 29 20 6d 61 63 72 6f 20 74 6f 20 6d 61 6b e() macro to mak
11cee 65 20 73 75 72 65 20 74 68 61 74 20 6d 61 6c 6c e sure that mall
11cef 6f 63 20 66 61 69 6c 75 72 65 20 77 69 74 68 69 oc failure withi
11cf0 6e 20 0a 20 20 20 20 2a 2a 20 50 61 67 65 72 53 n . ** PagerS
11cf1 65 74 50 61 67 65 73 69 7a 65 28 29 20 69 73 20 etPagesize() is
11cf2 74 65 73 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 tested.. */.
11cf3 20 20 20 69 50 61 67 65 53 69 7a 65 31 36 20 3d iPageSize16 =
11cf4 20 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 3b (u16)iPageSize;
11cf5 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11cf6 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 3PagerSetPagesiz
11cf7 65 28 70 50 61 67 65 72 2c 20 26 69 50 61 67 65 e(pPager, &iPage
11cf8 53 69 7a 65 31 36 2c 20 2d 31 29 3b 0a 20 20 20 Size16, -1);.
11cf9 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 testcase( rc!=S
11cfa 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 QLITE_OK );.
11cfb 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
11cfc 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53 69 TE_OK || iPageSi
11cfd 7a 65 31 36 3d 3d 28 75 31 36 29 69 50 61 67 65 ze16==(u16)iPage
11cfe 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 Size );.. /*
11cff 55 70 64 61 74 65 20 74 68 65 20 61 73 73 75 6d Update the assum
11d00 65 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 74 ed sector-size t
11d01 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 o match the valu
11d02 65 20 75 73 65 64 20 62 79 20 0a 20 20 20 20 2a e used by . *
11d03 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 * the process th
11d04 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73 20 at created this
11d05 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 73 journal. If this
11d06 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 journal was.
11d07 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 ** created by a
11d08 20 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 process other t
11d09 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 han this one, th
11d0a 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
11d0b 20 20 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 ** is being
11d0c 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 called from with
11d0d 69 6e 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 in pager_playbac
11d0e 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 k(). The local v
11d0f 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 50 alue. ** of P
11d10 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
11d11 69 73 20 72 65 73 74 6f 72 65 64 20 61 74 20 74 is restored at t
11d12 68 65 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72 he end of that r
11d13 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 outine.. */.
11d14 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f pPager->secto
11d15 72 53 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 53 rSize = iSectorS
11d16 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 ize;. }.. pPag
11d17 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b er->journalOff +
11d18 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a = JOURNAL_HDR_SZ
11d19 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 (pPager);. retu
11d1a 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
11d1b 20 57 72 69 74 65 20 74 68 65 20 73 75 70 70 6c Write the suppl
11d1c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ied master journ
11d1d 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 al name into the
11d1e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
11d1f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 65 r pager.** pPage
11d20 72 20 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 r at the current
11d21 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 6d location. The m
11d22 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
11d23 6d 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 6c me must be the l
11d24 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 69 ast.** thing wri
11d25 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e 61 tten to a journa
11d26 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70 l file. If the p
11d27 61 67 65 72 20 69 73 20 69 6e 20 66 75 6c 6c 2d ager is in full-
11d28 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a 2a sync mode, the.*
11d29 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 * journal file d
11d2a 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 64 76 escriptor is adv
11d2b 61 6e 63 65 64 20 74 6f 20 74 68 65 20 6e 65 78 anced to the nex
11d2c 74 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 t sector boundar
11d2d 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 y before.** anyt
11d2e 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 2e hing is written.
11d2f 20 54 68 65 20 66 6f 72 6d 61 74 20 69 73 3a 0a The format is:.
11d30 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 **.** + 4 byte
11d31 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f s: PAGER_MJ_PGNO
11d32 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 73 ..** + N bytes
11d33 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c : Master journal
11d34 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66 filename in utf
11d35 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 -8..** + 4 byt
11d36 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f 66 es: N (length of
11d37 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11d38 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 2c 20 6e name in bytes, n
11d39 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 o nul-terminator
11d3a 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 )..** + 4 byte
11d3b 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 s: Master journa
11d3c 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e l name checksum.
11d3d 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74 65 73 3a .** + 8 bytes:
11d3e 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d aJournalMagic[]
11d3f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 ..**.** The mast
11d40 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 er journal page
11d41 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20 checksum is the
11d42 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73 sum of the bytes
11d43 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a in the master.*
11d44 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 * journal name,
11d45 77 68 65 72 65 20 65 61 63 68 20 62 79 74 65 20 where each byte
11d46 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
11d47 73 20 61 20 73 69 67 6e 65 64 20 38 2d 62 69 74 s a signed 8-bit
11d48 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 integer..**.**
11d49 49 66 20 7a 4d 61 73 74 65 72 20 69 73 20 61 20 If zMaster is a
11d4a 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 NULL pointer (oc
11d4b 63 75 72 73 20 66 6f 72 20 61 20 73 69 6e 67 6c curs for a singl
11d4c 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 e database trans
11d4d 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 action), .** thi
11d4e 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f s call is a no-o
11d4f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
11d50 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 writeMasterJour
11d51 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 nal(Pager *pPage
11d52 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a r, const char *z
11d53 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 Master){. int r
11d54 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
11d55 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11d56 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
11d57 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20 20 20 20 int nMaster;
11d58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d59 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 /* Length of st
11d5a 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a ring zMaster */.
11d5b 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 i64 iHdrOff;
11d5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d5d 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 /* Offset of
11d5e 68 65 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61 header in journa
11d5f 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 l file */. i64
11d60 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 jrnlSize;
11d61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11d62 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 Size of journal
11d63 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a file on disk */.
11d64 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b u32 cksum = 0;
11d65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d66 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f /* Checksum o
11d67 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 f string zMaster
11d68 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61 73 */.. if( !zMas
11d69 74 65 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 ter || pPager->s
11d6a 65 74 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20 70 etMaster. || p
11d6b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11d6c 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11d6d 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 LMODE_MEMORY .
11d6e 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
11d6f 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
11d70 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a OURNALMODE_OFF .
11d71 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ){. return
11d72 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
11d73 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
11d74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 er = 1;. assert
11d75 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
11d76 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 >jfd) );.. /* C
11d77 61 6c 63 75 6c 61 74 65 20 74 68 65 20 6c 65 6e alculate the len
11d78 67 74 68 20 69 6e 20 62 79 74 65 73 20 61 6e 64 gth in bytes and
11d79 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6f 66 the checksum of
11d7a 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 66 6f zMaster */. fo
11d7b 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20 7a 4d 61 r(nMaster=0; zMa
11d7c 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e ster[nMaster]; n
11d7d 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20 20 20 63 Master++){. c
11d7e 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b ksum += zMaster[
11d7f 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 nMaster];. }..
11d80 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c 6c 2d 73 /* If in full-s
11d81 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76 61 6e 63 ync mode, advanc
11d82 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 64 69 e to the next di
11d83 73 6b 20 73 65 63 74 6f 72 20 62 65 66 6f 72 65 sk sector before
11d84 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a 20 74 68 writing. ** th
11d85 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11d86 20 6e 61 6d 65 2e 20 54 68 69 73 20 69 73 20 69 name. This is i
11d87 6e 20 63 61 73 65 20 74 68 65 20 70 72 65 76 69 n case the previ
11d88 6f 75 73 20 70 61 67 65 20 77 72 69 74 74 65 6e ous page written
11d89 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 to. ** the jou
11d8a 72 6e 61 6c 20 68 61 73 20 61 6c 72 65 61 64 79 rnal has already
11d8b 20 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 20 20 been synced..
11d8c 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
11d8d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 >fullSync ){.
11d8e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11d8f 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 Off = journalHdr
11d90 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a Offset(pPager);.
11d91 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 }. iHdrOff =
11d92 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11d93 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 ff;.. /* Write
11d94 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
11d95 61 6c 20 64 61 74 61 20 74 6f 20 74 68 65 20 65 al data to the e
11d96 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 nd of the journa
11d97 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 l file. If. **
11d98 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
11d99 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f return the erro
11d9a 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 r code to the ca
11d9b 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ller.. */. if(
11d9c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 (0 != (rc = wri
11d9d 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d te32bits(pPager-
11d9e 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2c 20 50 >jfd, iHdrOff, P
11d9f 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
11da0 67 65 72 29 29 29 29 0a 20 20 20 7c 7c 20 28 30 ger)))). || (0
11da1 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 != (rc = sqlite
11da2 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
11da3 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 6e >jfd, zMaster, n
11da4 4d 61 73 74 65 72 2c 20 69 48 64 72 4f 66 66 2b Master, iHdrOff+
11da5 34 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 4))). || (0 !=
11da6 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 (rc = write32bi
11da7 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
11da8 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 iHdrOff+4+nMaste
11da9 72 2c 20 6e 4d 61 73 74 65 72 29 29 29 0a 20 20 r, nMaster))).
11daa 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 || (0 != (rc =
11dab 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67 write32bits(pPag
11dac 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11dad 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 63 6b +4+nMaster+4, ck
11dae 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 sum))). || (0
11daf 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 != (rc = sqlite3
11db0 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
11db1 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 jfd, aJournalMag
11db2 69 63 2c 20 38 2c 20 69 48 64 72 4f 66 66 2b 34 ic, 8, iHdrOff+4
11db3 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29 0a 20 20 +nMaster+8))).
11db4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
11db5 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e ;. }. pPager->
11db6 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e journalOff += (n
11db7 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20 20 70 50 Master+20);. pP
11db8 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d ager->needSync =
11db9 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 !pPager->noSync
11dba 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 ;.. /* If the p
11dbb 61 67 65 72 20 69 73 20 69 6e 20 70 65 72 69 73 ager is in peris
11dbc 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 tent-journal mod
11dbd 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 79 73 e, then the phys
11dbe 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e ical . ** journ
11dbf 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 65 al-file may exte
11dc0 6e 64 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 nd past the end
11dc1 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f of the master-jo
11dc2 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 urnal name. **
11dc3 61 6e 64 20 38 20 62 79 74 65 73 20 6f 66 20 6d and 8 bytes of m
11dc4 61 67 69 63 20 64 61 74 61 20 6a 75 73 74 20 77 agic data just w
11dc5 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 69 ritten to the fi
11dc6 6c 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 2a le. This is . *
11dc7 2a 20 64 61 6e 67 65 72 6f 75 73 20 62 65 63 61 * dangerous beca
11dc8 75 73 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 use the code to
11dc9 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d 6a rollback a hot-j
11dca 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a ournal file. **
11dcb 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c will not be abl
11dcc 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 61 e to find the ma
11dcd 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster-journal nam
11dce 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 0a e to determine .
11dcf 20 20 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 ** whether or
11dd0 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 not the journal
11dd1 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 is hot. . **.
11dd2 2a 2a 20 45 61 73 69 65 73 74 20 74 68 69 6e 67 ** Easiest thing
11dd3 20 74 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 73 to do in this s
11dd4 63 65 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 72 cenario is to tr
11dd5 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e uncate the journ
11dd6 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 6f al . ** file to
11dd7 20 74 68 65 20 72 65 71 75 69 72 65 64 20 73 69 the required si
11dd8 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 ze.. */ . if(
11dd9 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
11dda 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
11ddb 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ze(pPager->jfd,
11ddc 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20 26 &jrnlSize)). &
11ddd 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67 65 & jrnlSize>pPage
11dde 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 r->journalOff.
11ddf 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
11de0 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 te3OsTruncate(pP
11de1 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 ager->jfd, pPage
11de2 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a r->journalOff);.
11de3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
11de4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 .}../*.** Find a
11de5 20 70 61 67 65 20 69 6e 20 74 68 65 20 68 61 73 page in the has
11de6 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 69 74 h table given it
11de7 73 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 52 s page number. R
11de8 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
11de9 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 6f er to the page o
11dea 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 65 r NULL if the re
11deb 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 quested page is
11dec 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 not .** already
11ded 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 in memory..*/.st
11dee 61 74 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 atic PgHdr *page
11def 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a r_lookup(Pager *
11df0 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e pPager, Pgno pgn
11df1 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 o){. PgHdr *p;
11df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11df3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
11df4 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a n value */.. /*
11df5 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
11df6 62 6c 65 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 ble for a call t
11df7 6f 20 50 63 61 63 68 65 46 65 74 63 68 28 29 20 o PcacheFetch()
11df8 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d with createFlag=
11df9 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c =0 to. ** fail,
11dfa 20 73 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d 70 since no attemp
11dfb 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64 79 t to allocate dy
11dfc 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69 6c namic memory wil
11dfd 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a l be made.. */.
11dfe 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 50 (void)sqlite3P
11dff 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 cacheFetch(pPage
11e00 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f r->pPCache, pgno
11e01 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 75 , 0, &p);. retu
11e02 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 rn p;.}../*.** U
11e03 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65 72 20 nless the pager
11e04 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74 is in error-stat
11e05 65 2c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 e, discard all i
11e06 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2e 20 n-memory pages.
11e07 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 If.** the pager
11e08 69 73 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74 is in error-stat
11e09 65 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c e, then this cal
11e0a 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a l is a no-op..**
11e0b 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79 20 63 61 .** TODO: Why ca
11e0c 6e 20 77 65 20 6e 6f 74 20 72 65 73 65 74 20 74 n we not reset t
11e0d 68 65 20 70 61 67 65 72 20 77 68 69 6c 65 20 69 he pager while i
11e0e 6e 20 65 72 72 6f 72 20 73 74 61 74 65 3f 0a 2a n error state?.*
11e0f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 /.static void pa
11e10 67 65 72 5f 72 65 73 65 74 28 50 61 67 65 72 20 ger_reset(Pager
11e11 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
11e12 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 67 65 SQLITE_OK==pPage
11e13 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 r->errCode ){.
11e14 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 sqlite3BackupR
11e15 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 estart(pPager->p
11e16 42 61 63 6b 75 70 29 3b 0a 20 20 20 20 73 71 6c Backup);. sql
11e17 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 ite3PcacheClear(
11e18 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
11e19 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 ;. pPager->db
11e1a 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 SizeValid = 0;.
11e1b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 }.}../*.** Free
11e1c 20 61 6c 6c 20 73 74 72 75 63 74 75 72 65 73 20 all structures
11e1d 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 in the Pager.aSa
11e1e 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 20 vepoint[] array
11e1f 61 6e 64 20 73 65 74 20 62 6f 74 68 0a 2a 2a 20 and set both.**
11e20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
11e21 20 61 6e 64 20 50 61 67 65 72 2e 6e 53 61 76 65 and Pager.nSave
11e22 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f 2e 20 43 point to zero. C
11e23 6c 6f 73 65 20 74 68 65 20 73 75 62 2d 6a 6f 75 lose the sub-jou
11e24 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 rnal.** if it is
11e25 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 open and the pa
11e26 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 78 ger is not in ex
11e27 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f clusive mode..*/
11e28 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
11e29 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 easeAllSavepoint
11e2a 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 s(Pager *pPager)
11e2b 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 {. int ii;
11e2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
11e2d 72 61 74 6f 72 20 66 6f 72 20 6c 6f 6f 70 69 6e rator for loopin
11e2e 67 20 74 68 72 6f 75 67 68 20 50 61 67 65 72 2e g through Pager.
11e2f 61 53 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 aSavepoint */.
11e30 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 for(ii=0; ii<pPa
11e31 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
11e32 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 ii++){. sqli
11e33 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
11e34 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f (pPager->aSavepo
11e35 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 int[ii].pInSavep
11e36 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 oint);. }. if(
11e37 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 !pPager->exclus
11e38 69 76 65 4d 6f 64 65 20 7c 7c 20 73 71 6c 69 74 iveMode || sqlit
11e39 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 e3IsMemJournal(p
11e3a 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a Pager->sjfd) ){.
11e3b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
11e3c 73 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 se(pPager->sjfd)
11e3d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
11e3e 66 72 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61 free(pPager->aSa
11e3f 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67 vepoint);. pPag
11e40 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d er->aSavepoint =
11e41 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 0;. pPager->nS
11e42 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 avepoint = 0;.
11e43 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 pPager->nSubRec
11e44 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 = 0;.}../*.** Se
11e45 74 20 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72 t the bit number
11e46 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67 pgno in the Pag
11e47 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 erSavepoint.pInS
11e48 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 avepoint .** bit
11e49 76 65 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e vecs of all open
11e4a 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74 savepoints. Ret
11e4b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
11e4c 20 73 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f successful.** o
11e4d 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 r SQLITE_NOMEM i
11e4e 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 f a malloc failu
11e4f 72 65 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 re occurs..*/.st
11e50 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61 atic int addToSa
11e51 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50 vepointBitvecs(P
11e52 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
11e53 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 no pgno){. int
11e54 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ii;
11e55 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
11e56 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 unter */. int r
11e57 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
11e58 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 /* Result c
11e59 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 ode */.. for(ii
11e5a 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e =0; ii<pPager->n
11e5b 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 Savepoint; ii++)
11e5c 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 {. PagerSavep
11e5d 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 oint *p = &pPage
11e5e 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
11e5f 5d 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c ];. if( pgno<
11e60 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20 =p->nOrig ){.
11e61 20 20 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 rc |= sqlite3
11e62 42 69 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e BitvecSet(p->pIn
11e63 53 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 Savepoint, pgno)
11e64 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
11e65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
11e66 45 4d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 EM );. asse
11e67 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
11e68 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f K || rc==SQLITE_
11e69 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 NOMEM );. }.
11e6a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
11e6b 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 }../*.** Unlock
11e6c 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11e6d 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e e. This function
11e6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 is a no-op if t
11e6f 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69 he pager.** is i
11e70 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
11e71 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
11e72 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c ager is currentl
11e73 79 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 y in error state
11e74 2c 20 64 69 73 63 61 72 64 20 74 68 65 20 63 6f , discard the co
11e75 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68 ntents of .** th
11e76 65 20 63 61 63 68 65 20 61 6e 64 20 72 65 73 65 e cache and rese
11e77 74 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 t the Pager stru
11e78 63 74 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73 cture internal s
11e79 74 61 74 65 2e 20 49 66 20 74 68 65 72 65 20 69 tate. If there i
11e7a 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 s.** an open jou
11e7b 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20 rnal-file, then
11e7c 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20 the next time a
11e7d 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f shared-lock is o
11e7e 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 btained.** on th
11e7f 65 20 70 61 67 65 72 20 66 69 6c 65 20 28 62 79 e pager file (by
11e80 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
11e81 65 72 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20 er process), it
11e82 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74 will be.** treat
11e83 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 ed as a hot-jour
11e84 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62 nal and rolled b
11e85 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ack..*/.static v
11e86 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b oid pager_unlock
11e87 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11e88 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
11e89 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b exclusiveMode ){
11e8a 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 . int rc;
11e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11e8c 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
11e8d 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61 */.. /* Alwa
11e8e 79 73 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 ys close the jou
11e8f 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 rnal file when d
11e90 72 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 ropping the data
11e91 62 61 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a base lock.. *
11e92 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f * Otherwise, ano
11e93 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 ther connection
11e94 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 with journal_mod
11e95 65 3d 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20 e=delete might.
11e96 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 ** delete the
11e97 20 66 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75 file out from u
11e98 6e 64 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a nder us.. */.
11e99 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
11e9a 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
11e9b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 . sqlite3Bitv
11e9c 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
11e9d 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 ->pInJournal);.
11e9e 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f pPager->pInJo
11e9f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 urnal = 0;. r
11ea0 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 eleaseAllSavepoi
11ea1 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 nts(pPager);..
11ea2 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 /* If the file
11ea3 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f is unlocked, so
11ea4 6d 65 62 6f 64 79 20 65 6c 73 65 20 6d 69 67 68 mebody else migh
11ea5 74 20 63 68 61 6e 67 65 20 69 74 2e 20 54 68 65 t change it. The
11ea6 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 73 . ** values s
11ea7 74 6f 72 65 64 20 69 6e 20 50 61 67 65 72 2e 64 tored in Pager.d
11ea8 62 53 69 7a 65 20 65 74 63 2e 20 6d 69 67 68 74 bSize etc. might
11ea9 20 62 65 63 6f 6d 65 20 69 6e 76 61 6c 69 64 20 become invalid
11eaa 69 66 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 68 if. ** this h
11eab 61 70 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 appens. TODO: Re
11eac 61 6c 6c 79 2c 20 74 68 69 73 20 64 6f 65 73 6e ally, this doesn
11ead 27 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 't need to be cl
11eae 65 61 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e 74 eared. ** unt
11eaf 69 6c 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f il the change-co
11eb0 75 6e 74 65 72 20 63 68 65 63 6b 20 66 61 69 6c unter check fail
11eb1 73 20 69 6e 20 50 61 67 65 72 53 68 61 72 65 64 s in PagerShared
11eb2 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 Lock().. */.
11eb3 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a pPager->dbSiz
11eb4 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 eValid = 0;..
11eb5 20 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 rc = osUnlock(p
11eb6 50 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f Pager->fd, NO_LO
11eb7 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 CK);. if( rc
11eb8 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
11eb9 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 >errCode = rc;.
11eba 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 }. IOTRACE
11ebb 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c (("UNLOCK %p\n",
11ebc 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f pPager)).. /
11ebd 2a 20 49 66 20 50 61 67 65 72 2e 65 72 72 43 6f * If Pager.errCo
11ebe 64 65 20 69 73 20 73 65 74 2c 20 74 68 65 20 63 de is set, the c
11ebf 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
11ec0 61 67 65 72 20 63 61 63 68 65 20 63 61 6e 6e 6f ager cache canno
11ec1 74 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 75 73 t be. ** trus
11ec2 74 65 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68 ted. Now that th
11ec3 65 20 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 e pager file is
11ec4 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f unlocked, the co
11ec5 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 ntents of the.
11ec6 20 20 2a 2a 20 63 61 63 68 65 20 63 61 6e 20 62 ** cache can b
11ec7 65 20 64 69 73 63 61 72 64 65 64 20 61 6e 64 20 e discarded and
11ec8 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 the error code s
11ec9 61 66 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a 20 afely cleared..
11eca 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 */. if( pP
11ecb 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
11ecc 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
11ecd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
11ece 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 pPager->errC
11ecf 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b ode = SQLITE_OK;
11ed0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
11ed1 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 ager_reset(pPage
11ed2 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 r);. }.. p
11ed3 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
11ed4 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 ntDone = 0;.
11ed5 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
11ed6 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 PAGER_UNLOCK;.
11ed7 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
11ed8 66 69 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a fied = 0;. }.}.
11ed9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
11eda 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 tion should be c
11edb 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f alled when an IO
11edc 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 ERR, CORRUPT or
11edd 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 FULL error.** ma
11ede 79 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e y have occurred.
11edf 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
11ee0 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ent is a pointer
11ee1 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a to the pager .*
11ee2 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 * structure, the
11ee3 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f second the erro
11ee4 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 r-code about to
11ee5 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 be returned by a
11ee6 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 pager .** API f
11ee7 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c unction. The val
11ee8 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 ue returned is a
11ee9 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 copy of the sec
11eea 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a ond argument .**
11eeb 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
11eec 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 n. .**.** If the
11eed 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
11eee 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 is SQLITE_IOERR
11eef 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 , SQLITE_CORRUPT
11ef0 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c , or SQLITE_FULL
11ef1 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 .** the error be
11ef2 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 comes persistent
11ef3 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 . Until the pers
11ef4 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 isten error is c
11ef5 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 leared,.** subse
11ef6 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 quent API calls
11ef7 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69 on this Pager wi
11ef8 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 ll immediately r
11ef9 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a eturn the same .
11efa 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a ** error code..*
11efb 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e *.** A persisten
11efc 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 t error indicate
11efd 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 s that the conte
11efe 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
11eff 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f -cache .** canno
11f00 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 t be trusted. Th
11f01 69 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 is state can be
11f02 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c cleared by compl
11f03 65 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 etely discarding
11f04 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 .** the content
11f05 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 s of the pager-c
11f06 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 ache. If a trans
11f07 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 action was activ
11f08 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 e when.** the pe
11f09 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f rsistent error o
11f0a 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 ccurred, then th
11f0b 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
11f0c 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 al may need.** t
11f0d 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f o be replayed to
11f0e 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e restore the con
11f0f 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 tents of the dat
11f10 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 abase file (as i
11f11 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 f.** it were a h
11f12 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a ot-journal)..*/.
11f13 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
11f14 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 _error(Pager *pP
11f15 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 ager, int rc){.
11f16 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 int rc2 = rc &
11f17 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 0xff;. assert(
11f18 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
11f19 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 !MEMDB );. ass
11f1a 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 ert(. pPag
11f1b 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c er->errCode==SQL
11f1c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 ITE_FULL ||.
11f1d 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
11f1e 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c de==SQLITE_OK ||
11f1f 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d . (pPager-
11f20 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 >errCode & 0xff)
11f21 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 ==SQLITE_IOERR.
11f22 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 );. if( rc2==S
11f23 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 QLITE_FULL || rc
11f24 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 2==SQLITE_IOERR
11f25 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 ){. pPager->e
11f26 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d rrCode = rc;. }
11f27 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11f28 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 ./*.** Execute a
11f29 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 rollback if a t
11f2a 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
11f2b 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 tive and unlock
11f2c 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 the .** database
11f2d 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 file. .**.** If
11f2e 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61 the pager has a
11f2f 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 lready entered t
11f30 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
11f31 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a do not attempt .
11f32 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
11f33 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e at this time. In
11f34 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c stead, pager_unl
11f35 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ock() is called.
11f36 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 The.** call to
11f37 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 pager_unlock() w
11f38 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 ill discard all
11f39 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c in-memory pages,
11f3a 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 unlock.** the d
11f3b 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
11f3c 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 clear the error
11f3d 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 state. If this
11f3e 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 means that.** th
11f3f 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 ere is a hot-jou
11f40 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 rnal left in the
11f41 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 file-system, th
11f42 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f e next connectio
11f43 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 n.** to obtain a
11f44 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 shared lock on
11f45 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63 68 the pager (which
11f46 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 may be this one
11f47 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 ) will.** roll i
11f48 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 t back..**.** If
11f49 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 6e the pager has n
11f4a 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 ot already enter
11f4b 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ed the error sta
11f4c 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 te, but an IO or
11f4d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 .** malloc error
11f4e 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 occurs during a
11f4f 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 rollback, then
11f50 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 this will itself
11f51 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 cause .** the p
11f52 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 ager to enter th
11f53 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 e error state. W
11f54 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 hich will be cle
11f55 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 ared by the.** c
11f56 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c all to pager_unl
11f57 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 ock(), as descri
11f58 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 bed above..*/.st
11f59 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 atic void pagerU
11f5a 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
11f5b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
11f5c 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
11f5d 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f rrCode==SQLITE_O
11f5e 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 K && pPager->sta
11f5f 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
11f60 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ED ){. sqlite
11f61 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
11f62 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 oc();. sqlite
11f63 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
11f64 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 Pager);. sqli
11f65 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
11f66 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 oc();. }. page
11f67 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 r_unlock(pPager)
11f68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
11f69 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 routine ends a t
11f6a 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 ransaction. A tr
11f6b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 ansaction is usu
11f6c 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a ally ended by .*
11f6d 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 * either a COMMI
11f6e 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 T or a ROLLBACK
11f6f 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 operation. This
11f70 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 routine may be c
11f71 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 alled .** after
11f72 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f rollback of a ho
11f73 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 t-journal, or if
11f74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
11f75 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a while opening.*
11f76 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * the journal fi
11f77 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 le or writing th
11f78 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 e very first jou
11f79 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 rnal-header of a
11f7a 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 .** database tra
11f7b 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a nsaction..** .**
11f7c 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
11f7d 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 in PAGER_SHARED
11f7e 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b or PAGER_UNLOCK
11f7f 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 state when this
11f80 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 .** routine is c
11f81 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e alled, it is a n
11f82 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 o-op (returns SQ
11f83 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK)..**.**
11f84 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 Otherwise, any a
11f85 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
11f86 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a are released..*
11f87 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 *.** If the jour
11f88 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e nal file is open
11f89 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 , then it is "fi
11f8a 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 nalized". Once a
11f8b 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c journal .** fil
11f8c 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c e has been final
11f8d 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 ized it is not p
11f8e 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 ossible to use i
11f8f 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 t to roll back a
11f90 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
11f91 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 . Nor will it be
11f92 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 considered to b
11f93 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 e a hot-journal
11f94 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e by this.** or an
11f95 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 y other database
11f96 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 connection. Exa
11f97 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e ctly how a journ
11f98 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a al is finalized.
11f99 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 ** depends on wh
11f9a 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
11f9b 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
11f9c 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
11f9d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 ode and.** the c
11f9e 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d urrent journal-m
11f9f 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e ode (Pager.journ
11fa0 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 alMode value), a
11fa1 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
11fa2 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d journalMode==
11fa3 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f MEMORY.** Jo
11fa4 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 urnal file descr
11fa5 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 iptor is simply
11fa6 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 closed. This des
11fa7 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 troys an .**
11fa8 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
11fa9 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 al..**.** jour
11faa 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 nalMode==TRUNCAT
11fab 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c E.** Journal
11fac 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 file is truncat
11fad 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 ed to zero bytes
11fae 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 in size..**.**
11faf 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 journalMode==P
11fb0 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 ERSIST.** Th
11fb1 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65 73 e first 28 bytes
11fb2 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11fb3 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e file are zeroed.
11fb4 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 This invalidate
11fb5 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 s.** the fir
11fb6 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 st journal heade
11fb7 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 r in the file, a
11fb8 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 nd hence the ent
11fb9 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 ire journal.**
11fba 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 file. An inva
11fbb 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 lid journal file
11fbc 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 cannot be rolle
11fbd 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 d back..**.**
11fbe 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c journalMode==DEL
11fbf 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a ETE.** The j
11fc0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 ournal file is c
11fc1 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 losed and delete
11fc2 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f d using sqlite3O
11fc3 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a sDelete()..**.**
11fc4 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67 65 If the page
11fc5 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
11fc6 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
11fc7 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 this method of f
11fc8 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 inalizing.**
11fc9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11fca 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e e is never used.
11fcb 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 Instead, if the
11fcc 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a journalMode is.
11fcd 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e ** DELETE an
11fce 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 d the pager is i
11fcf 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
11fd0 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 , the method des
11fd1 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 cribed under.**
11fd2 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d journalMode=
11fd3 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65 64 =PERSIST is used
11fd4 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 instead..**.**
11fd5 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 After the journa
11fd6 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 l is finalized,
11fd7 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f if running in no
11fd8 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n-exclusive mode
11fd9 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d , the.** pager m
11fda 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 oves to PAGER_SH
11fdb 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 ARED state (and
11fdc 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c downgrades the l
11fdd 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 ock on the.** da
11fde 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f tabase file acco
11fdf 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 rdingly)..**.**
11fe0 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
11fe1 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
11fe2 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 sive mode and is
11fe3 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 in PAGER_SYNCED
11fe4 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f state,.** it mo
11fe5 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 ves to PAGER_EXC
11fe6 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 LUSIVE. No locks
11fe7 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 are downgraded
11fe8 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a when running in.
11fe9 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 ** exclusive mod
11fea 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f e..**.** SQLITE_
11feb 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
11fec 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 f no error occur
11fed 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f s. If an error o
11fee 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 ccurs during.**
11fef 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 any of the IO op
11ff0 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 erations to fina
11ff1 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lize the journal
11ff2 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 file or unlock
11ff3 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
11ff4 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f then the IO erro
11ff5 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
11ff6 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
11ff7 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 If the .** opera
11ff8 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 tion to finalize
11ff9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
11ffa 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 e fails, then th
11ffb 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 e code still.**
11ffc 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 tries to unlock
11ffd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11ffe 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c e if not in excl
11fff 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 usive mode. If t
12000 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 he.** unlock ope
12001 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 ration fails as
12002 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 well, then the f
12003 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 irst error code
12004 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 related.** to th
12005 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e e first error en
12006 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a countered (the j
12007 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 ournal finalizat
12008 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 ion one) is.** r
12009 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
1200a 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 ic int pager_end
1200b 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 _transaction(Pag
1200c 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
1200d 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e hasMaster){. in
1200e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1200f 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 ; /* Error
12010 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 code from journa
12011 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f l finalization o
12012 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e peration */. in
12013 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f t rc2 = SQLITE_O
12014 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 K; /* Error
12015 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c code from db fil
12016 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 e unlock operati
12017 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 on */.. if( pPa
12018 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 ger->state<PAGER
12019 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 _RESERVED ){.
1201a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1201b 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 K;. }. release
1201c 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 AllSavepoints(pP
1201d 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 ager);.. assert
1201e 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
1201f 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d >jfd) || pPager-
12020 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
12021 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 ;. if( isOpen(p
12022 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a Pager->jfd) ){..
12023 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 /* Finalize
12024 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12025 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c . */. if( sql
12026 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c ite3IsMemJournal
12027 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
12028 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
12029 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
1202a 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
1202b 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a LMODE_MEMORY );.
1202c 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
1202d 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
1202e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1202f 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12030 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
12031 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
12032 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
12033 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
12034 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
12035 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
12036 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12037 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12038 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 OsTruncate(pPage
12039 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 r->jfd, 0);.
1203a 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 }. pPager
1203b 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
1203c 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
1203d 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
1203e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
1203f 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 ( pPager->exclus
12040 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c iveMode . ||
12041 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12042 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
12043 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a NALMODE_PERSIST.
12044 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ){. rc
12045 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 = zeroJournalHdr
12046 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 (pPager, hasMast
12047 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 er);. pager
12048 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
12049 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 c);. pPager
1204a 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
1204b 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
1204c 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
1204d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
1204e 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
1204f 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 nch may be execu
12050 74 65 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a ted with Pager.j
12051 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f ournalMode==MEMO
12052 52 59 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 RY if. ** a
12053 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 hot-journal was
12054 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 just rolled bac
12055 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 k. In this case
12056 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
12057 20 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 ** file should
12058 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 be closed and d
12059 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 eleted. If this
1205a 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 connection write
1205b 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 s to. ** th
1205c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1205d 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 it will do so u
1205e 73 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 sing an in-memor
1205f 79 20 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20 y journal. */.
12060 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
12061 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
12062 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
12063 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 ODE_DELETE .
12064 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 || pPager
12065 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
12066 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
12067 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29 _MEMORY . )
12068 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f ;. sqlite3O
12069 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
1206a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 fd);. if( !
1206b 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
1206c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1206d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1206e 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 (pPager->pVfs, p
1206f 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
12070 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
12071 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }..#ifdef SQLIT
12072 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
12073 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 sqlite3PcacheI
12074 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67 terateDirty(pPag
12075 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 er->pPCache, pag
12076 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 er_set_pagehash)
12077 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 ;.#endif.. sq
12078 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
12079 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 All(pPager->pPCa
1207a 63 68 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 che);. sqlite
1207b 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
1207c 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
1207d 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e l);. pPager->
1207e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a pInJournal = 0;.
1207f 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 pPager->nRec
12080 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 = 0;. }.. if(
12081 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 !pPager->exclus
12082 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 iveMode ){. r
12083 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 c2 = osUnlock(pP
12084 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 ager->fd, SHARED
12085 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 _LOCK);. pPag
12086 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
12087 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 R_SHARED;. pP
12088 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e ager->changeCoun
12089 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c tDone = 0;. }el
1208a 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 se if( pPager->s
1208b 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 tate==PAGER_SYNC
1208c 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 ED ){. pPager
1208d 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
1208e 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 EXCLUSIVE;. }.
1208f 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
12090 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 er = 0;. pPager
12091 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a ->needSync = 0;.
12092 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
12093 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 fied = 0;.. /*
12094 54 4f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 TODO: Is this op
12095 74 69 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 timal? Why is th
12096 65 20 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 e db size invali
12097 64 61 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a dated here . **
12098 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 when the databa
12099 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 se file is not u
1209a 6e 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 nlocked? */. pP
1209b 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
1209c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 = 0;. sqlite3P
1209d 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 cacheTruncate(pP
1209e 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
1209f 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a Pager->dbSize);.
120a0 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a if( !MEMDB ){.
120a1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
120a2 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d zeValid = 0;. }
120a3 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d .. return (rc==
120a4 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 SQLITE_OK?rc2:rc
120a5 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 );.}../*.** Para
120a6 6d 65 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 meter aData must
120a7 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 point to a buff
120a8 65 72 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 er of pPager->pa
120a9 67 65 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 geSize bytes.**
120aa 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 of data. Compute
120ab 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 and return a ch
120ac 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 ecksum based ont
120ad 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
120ae 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 the .** page of
120af 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 data and the cu
120b0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 rrent value of p
120b1 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
120b2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ..**.** This is
120b3 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b not a real check
120b4 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c sum. It is reall
120b5 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f y just the sum o
120b6 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d f the .** random
120b7 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 initial value (
120b8 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 pPager->cksumIni
120b9 74 29 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 t) and every 200
120ba 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 th byte.** of th
120bb 65 20 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 e page data, sta
120bc 72 74 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 rting with byte
120bd 6f 66 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e offset (pPager->
120be 70 61 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a pageSize%200)..*
120bf 2a 20 45 61 63 68 20 62 79 74 65 20 69 73 20 69 * Each byte is i
120c0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e nterpreted as an
120c1 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 8-bit unsigned
120c2 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 integer..**.** C
120c3 68 61 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d hanging the form
120c4 75 6c 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 ula used to comp
120c5 75 74 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 ute this checksu
120c6 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a m results in an.
120c7 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 ** incompatible
120c8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
120c9 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f mat..**.** If jo
120ca 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e urnal corruption
120cb 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 occurs due to a
120cc 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 power failure,
120cd 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 the most likely
120ce 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 .** scenario is
120cf 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 that one end or
120d0 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 the other of the
120d1 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 record will be
120d2 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 changed. .** It
120d3 69 73 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b is much less lik
120d4 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f ely that the two
120d5 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 ends of the jou
120d6 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c rnal record will
120d7 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 be.** correct a
120d8 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 nd the middle be
120d9 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c corrupt. Thus,
120da 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 this "checksum"
120db 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 scheme,.** thou
120dc 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 gh fast and simp
120dd 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 le, catches the
120de 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 mostly likely ki
120df 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e nd of corruption
120e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 ..*/.static u32
120e1 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 pager_cksum(Page
120e2 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 r *pPager, const
120e3 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 u8 *aData){. u
120e4 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 32 cksum = pPage
120e5 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 r->cksumInit;
120e6 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 /* Checksu
120e7 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 m value to retur
120e8 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 n */. int i = p
120e9 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d Pager->pageSize-
120ea 32 30 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 200; /*
120eb 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
120ec 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b . while( i>0 ){
120ed 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 . cksum += aD
120ee 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d ata[i];. i -=
120ef 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 200;. }. retu
120f0 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a rn cksum;.}../*.
120f1 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 ** Read a single
120f2 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 page from eithe
120f3 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 r the journal fi
120f4 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e le (if isMainJrn
120f5 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d l==1) or.** from
120f6 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
120f7 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d (if isMainJrnl=
120f8 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b =0) and playback
120f9 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 that page..** T
120fa 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 he page begins a
120fb 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 t offset *pOffse
120fc 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e t into the file.
120fd 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a The *pOffset.**
120fe 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 value is increa
120ff 73 65 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 sed to the start
12100 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 of the next pag
12101 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c e in the journal
12102 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 ..**.** The isMa
12103 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 inJrnl flag is t
12104 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 74 rue if this is t
12105 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
12106 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 journal and.**
12107 66 61 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 false for the st
12108 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
12109 20 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 The main rollb
1210a 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 ack journal uses
1210b 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 .** checksums -
1210c 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f the statement jo
1210d 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a urnal does not..
1210e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
1210f 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
12110 70 61 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 page record read
12111 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 from the (sub-)
12112 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 journal file.**
12113 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
12114 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 the current valu
12115 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a e of Pager.dbSiz
12116 65 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b e, then playback
12117 20 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 is.** skipped a
12118 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 nd SQLITE_OK is
12119 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1211a 49 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 If pDone is not
1211b 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 NULL, then it is
1211c 20 61 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 a record of pag
1211d 65 73 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 es that have alr
1211e 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 eady.** been pla
1211f 79 65 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 yed back. If th
12120 65 20 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 e page at *pOffs
12121 65 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 et has already b
12122 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a een played back.
12123 2a 2a 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 ** (if the corre
12124 73 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 sponding pDone b
12125 69 74 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 it is set) then
12126 73 6b 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 skip the playbac
12127 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 k..** Make sure
12128 74 68 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f the pDone bit co
12129 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 rresponding to t
1212a 68 65 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 he *pOffset page
1212b 20 69 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 is set.** prior
1212c 20 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a to returning..*
1212d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
1212e 20 72 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 record is succe
1212f 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f ssfully read fro
12130 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 m the (sub-)jour
12131 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 nal file.** and
12132 70 6c 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 played back, the
12133 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 n SQLITE_OK is r
12134 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 eturned. If an I
12135 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a O error occurs.*
12136 2a 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 * while reading
12137 74 68 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 the record from
12138 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
12139 6c 20 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 l file or while
1213a 77 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 writing.** to th
1213b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
1213c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 then the IO err
1213d 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
1213e 6e 65 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 ned. If data.**
1213f 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 is successfully
12140 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 read from the (s
12141 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ub-)journal file
12142 20 62 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 but appears to
12143 62 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c be.** corrupted,
12144 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 SQLITE_DONE is
12145 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 returned. Data i
12146 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 s considered cor
12147 72 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f rupted in.** two
12148 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a circumstances:.
12149 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 ** .** * If th
1214a 65 20 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 e record page-nu
1214b 6d 62 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 mber is illegal
1214c 28 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 (0 or PAGER_MJ_P
1214d 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 GNO), or.** *
1214e 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 If the record is
1214f 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 being rolled ba
12150 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ck from the main
12151 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a journal file.**
12152 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 and the che
12153 63 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 cksum field does
12154 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 not match the r
12155 65 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a ecord content..*
12156 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 *.** Neither of
12157 74 68 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 these two scenar
12158 69 6f 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 ios are possible
12159 20 64 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f during a savepo
1215a 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a int rollback..**
1215b 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 .** If this is a
1215c 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 savepoint rollb
1215d 61 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 ack, then memory
1215e 20 6d 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 may have to be
1215f 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 dynamically.** a
12160 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 llocated by this
12161 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 function. If th
12162 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20 61 is is the case a
12163 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e nd an allocation
12164 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 fails,.** SQLIT
12165 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 E_NOMEM is retur
12166 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
12167 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 nt pager_playbac
12168 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 k_one_page(. Pa
12169 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 ger *pPager,
1216a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1216b 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 he pager being p
1216c 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 layed back */.
1216d 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 int isMainJrnl,
1216e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1216f 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 1 -> main journ
12170 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 al. 0 -> sub-jou
12171 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 rnal. */. int i
12172 73 55 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 sUnsync,
12173 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
12174 20 69 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d if reading from
12175 20 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a unsynced main j
12176 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 ournal */. i64
12177 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 *pOffset,
12178 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
12179 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f set of record to
1217a 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 playback */. i
1217b 6e 74 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 nt isSavepnt,
1217c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1217d 54 72 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 True for a savep
1217e 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f oint rollback */
1217f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 . Bitvec *pDone
12180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12181 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 /* Bitvec of pa
12182 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 ges already play
12183 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 ed back */.){.
12184 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 int rc;. PgHdr
12185 2a 70 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 *pPg;
12186 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 /* An ex
12187 69 73 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 isting page in t
12188 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 he cache */. Pg
12189 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 no pgno;
1218a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1218b 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f he page number o
1218c 66 20 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 f a page in jour
1218d 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 nal */. u32 cks
1218e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 um;
1218f 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 /* Checks
12190 75 6d 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 um used for sani
12191 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 ty checking */.
12192 20 63 68 61 72 20 2a 61 44 61 74 61 3b 20 20 20 char *aData;
12193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12194 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 * Temporary stor
12195 61 67 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 age for the page
12196 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
12197 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 le *jfd;
12198 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 /* The file
12199 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 descriptor for t
1219a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1219b 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 */.. assert( (i
1219c 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 sMainJrnl&~1)==0
1219d 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 ); /* isMa
1219e 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 inJrnl is 0 or 1
1219f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 */. assert( (i
121a0 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 sSavepnt&~1)==0
121a1 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 ); /* isSa
121a2 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 vepnt is 0 or 1
121a3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d */. assert( isM
121a4 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 ainJrnl || pDone
121a5 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 ); /* pDone
121a6 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 always used on
121a7 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a sub-journals */.
121a8 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 assert( isSave
121a9 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 pnt || pDone==0
121aa 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 ); /* pDone ne
121ab 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d ver used on non-
121ac 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 savepoint */..
121ad 61 44 61 74 61 20 3d 20 70 50 61 67 65 72 2d 3e aData = pPager->
121ae 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 61 73 73 pTmpSpace;. ass
121af 65 72 74 28 20 61 44 61 74 61 20 29 3b 20 20 20 ert( aData );
121b0 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 /* Temp st
121b1 6f 72 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 orage must have
121b2 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c already been all
121b3 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 ocated */.. /*
121b4 52 65 61 64 20 74 68 65 20 70 61 67 65 20 6e 75 Read the page nu
121b5 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 64 61 mber and page da
121b6 74 61 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ta from the jour
121b7 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e nal or sub-journ
121b8 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 52 65 al. ** file. Re
121b9 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f turn an error co
121ba 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 de to the caller
121bb 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 if an IO error
121bc 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a 20 20 6a occurs.. */. j
121bd 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 fd = isMainJrnl
121be 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3a 20 ? pPager->jfd :
121bf 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b 0a 20 20 pPager->sjfd;.
121c0 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
121c1 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 2c 20 26 jfd, *pOffset, &
121c2 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63 21 pgno);. if( rc!
121c3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
121c4 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 urn rc;. rc = s
121c5 71 6c 69 74 65 33 4f 73 52 65 61 64 28 6a 66 64 qlite3OsRead(jfd
121c6 2c 20 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 , (u8*)aData, pP
121c7 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
121c8 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 (*pOffset)+4);.
121c9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
121ca 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
121cb 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 *pOffset += pP
121cc 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b ager->pageSize +
121cd 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 4 + isMainJrnl*
121ce 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 4;.. /* Sanity
121cf 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 checking on the
121d0 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d page. This is m
121d1 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 ore important th
121d2 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a at I originally.
121d3 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 ** thought. I
121d4 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 f a power failur
121d5 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 e occurs while t
121d6 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 he journal is be
121d7 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a ing written,. *
121d8 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 * it could cause
121d9 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f invalid data to
121da 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f be written into
121db 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 the journal. W
121dc 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 e need to. ** d
121dd 65 74 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c etect this inval
121de 69 64 20 64 61 74 61 20 28 77 69 74 68 20 68 69 id data (with hi
121df 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 gh probability)
121e0 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 and ignore it..
121e1 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d */. if( pgno==
121e2 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 0 || pgno==PAGER
121e3 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 _MJ_PGNO(pPager)
121e4 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
121e5 21 69 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 !isSavepnt );.
121e6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
121e7 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 DONE;. }. if(
121e8 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 pgno>(Pgno)pPage
121e9 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c r->dbSize || sql
121ea 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
121eb 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 Done, pgno) ){.
121ec 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
121ed 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 _OK;. }. if( i
121ee 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 sMainJrnl ){.
121ef 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 rc = read32bits
121f0 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 (jfd, (*pOffset)
121f1 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 -4, &cksum);.
121f2 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
121f3 20 72 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 rc;. if( !is
121f4 53 61 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 Savepnt && pager
121f5 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28 _cksum(pPager, (
121f6 75 38 2a 29 61 44 61 74 61 29 21 3d 63 6b 73 75 u8*)aData)!=cksu
121f7 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 m ){. retur
121f8 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 n SQLITE_DONE;.
121f9 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
121fa 70 44 6f 6e 65 20 26 26 20 28 72 63 20 3d 20 73 pDone && (rc = s
121fb 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
121fc 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 29 21 3d 53 pDone, pgno))!=S
121fd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
121fe 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
121ff 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12200 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 ->state==PAGER_R
12201 45 53 45 52 56 45 44 20 7c 7c 20 70 50 61 67 65 ESERVED || pPage
12202 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
12203 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 EXCLUSIVE );..
12204 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 /* If the pager
12205 69 73 20 69 6e 20 52 45 53 45 52 56 45 44 20 73 is in RESERVED s
12206 74 61 74 65 2c 20 74 68 65 6e 20 74 68 65 72 65 tate, then there
12207 20 6d 75 73 74 20 62 65 20 61 20 63 6f 70 79 20 must be a copy
12208 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 of this. ** pag
12209 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 e in the pager c
1220a 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63 61 ache. In this ca
1220b 73 65 20 6a 75 73 74 20 75 70 64 61 74 65 20 74 se just update t
1220c 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2c 0a he pager cache,.
1220d 20 20 2a 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 ** not the dat
1220e 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 abase file. The
1220f 70 61 67 65 20 69 73 20 6c 65 66 74 20 6d 61 72 page is left mar
12210 6b 65 64 20 64 69 72 74 79 20 69 6e 20 74 68 69 ked dirty in thi
12211 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a s case.. **. *
12212 2a 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 * An exception t
12213 6f 20 74 68 65 20 61 62 6f 76 65 20 72 75 6c 65 o the above rule
12214 3a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 : If the databas
12215 65 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 e is in no-sync
12216 6d 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 mode. ** and a
12217 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20 64 75 page is moved du
12218 72 69 6e 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e ring an incremen
12219 74 61 6c 20 76 61 63 75 75 6d 20 74 68 65 6e 20 tal vacuum then
1221a 74 68 65 20 70 61 67 65 20 6d 61 79 0a 20 20 2a the page may. *
1221b 2a 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 * not be in the
1221c 70 61 67 65 72 20 63 61 63 68 65 2e 20 4c 61 74 pager cache. Lat
1221d 65 72 3a 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 er: if a malloc(
1221e 29 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 ) or IO error oc
1221f 63 75 72 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 curs. ** during
12220 20 61 20 4d 6f 76 65 70 61 67 65 28 29 20 63 61 a Movepage() ca
12221 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 ll, then the pag
12222 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 e may not be in
12223 74 68 65 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 the cache. ** e
12224 69 74 68 65 72 2e 20 53 6f 20 74 68 65 20 63 6f ither. So the co
12225 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 ndition describe
12226 64 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 70 d in the above p
12227 61 72 61 67 72 61 70 68 20 69 73 20 6e 6f 74 0a aragraph is not.
12228 20 20 2a 2a 20 61 73 73 65 72 74 28 29 61 62 6c ** assert()abl
12229 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 e.. **. ** If
1222a 69 6e 20 45 58 43 4c 55 53 49 56 45 20 73 74 61 in EXCLUSIVE sta
1222b 74 65 2c 20 74 68 65 6e 20 77 65 20 75 70 64 61 te, then we upda
1222c 74 65 20 74 68 65 20 70 61 67 65 72 20 63 61 63 te the pager cac
1222d 68 65 20 69 66 20 69 74 20 65 78 69 73 74 73 0a he if it exists.
1222e 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 69 ** and the mai
1222f 6e 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 n file. The page
12230 20 69 73 20 74 68 65 6e 20 6d 61 72 6b 65 64 20 is then marked
12231 6e 6f 74 20 64 69 72 74 79 2e 0a 20 20 2a 2a 0a not dirty.. **.
12232 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 31 31 37 ** Ticket #117
12233 31 3a 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e 1: The statemen
12234 74 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 t journal might
12235 63 6f 6e 74 61 69 6e 20 70 61 67 65 20 63 6f 6e contain page con
12236 74 65 6e 74 20 74 68 61 74 20 69 73 0a 20 20 2a tent that is. *
12237 2a 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d * different from
12238 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e the page conten
12239 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f t at the start o
1223a 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
1223b 6e 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f 63 63 n.. ** This occ
1223c 75 72 73 20 77 68 65 6e 20 61 20 70 61 67 65 20 urs when a page
1223d 69 73 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72 is changed prior
1223e 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
1223f 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a a statement. *
12240 2a 20 74 68 65 6e 20 63 68 61 6e 67 65 64 20 61 * then changed a
12241 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 65 20 gain within the
12242 73 74 61 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e statement. When
12243 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75 rolling back su
12244 63 68 20 61 0a 20 20 2a 2a 20 73 74 61 74 65 6d ch a. ** statem
12245 65 6e 74 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 ent we must not
12246 77 72 69 74 65 20 74 6f 20 74 68 65 20 6f 72 69 write to the ori
12247 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 75 ginal database u
12248 6e 6c 65 73 73 20 77 65 20 6b 6e 6f 77 0a 20 20 nless we know.
12249 2a 2a 20 66 6f 72 20 63 65 72 74 61 69 6e 20 74 ** for certain t
1224a 68 61 74 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 hat original pag
1224b 65 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 20 73 e contents are s
1224c 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6d ynced into the m
1224d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a ain rollback. *
1224e 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 74 68 65 * journal. Othe
1224f 72 77 69 73 65 2c 20 61 20 70 6f 77 65 72 20 6c rwise, a power l
12250 6f 73 73 20 6d 69 67 68 74 20 6c 65 61 76 65 20 oss might leave
12251 6d 6f 64 69 66 69 65 64 20 64 61 74 61 20 69 6e modified data in
12252 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 the. ** databa
12253 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 se file without
12254 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 an entry in the
12255 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
12256 20 74 68 61 74 20 63 61 6e 0a 20 20 2a 2a 20 72 that can. ** r
12257 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 estore the datab
12258 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 ase to its origi
12259 6e 61 6c 20 66 6f 72 6d 2e 20 20 54 77 6f 20 63 nal form. Two c
1225a 6f 6e 64 69 74 69 6f 6e 73 20 6d 75 73 74 20 62 onditions must b
1225b 65 0a 20 20 2a 2a 20 6d 65 74 20 62 65 66 6f 72 e. ** met befor
1225c 65 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 e writing to the
1225d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e database files.
1225e 20 28 31 29 20 74 68 65 20 64 61 74 61 62 61 73 (1) the databas
1225f 65 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6c e must be. ** l
12260 6f 63 6b 65 64 2e 20 20 28 32 29 20 77 65 20 6b ocked. (2) we k
12261 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6f 72 69 now that the ori
12262 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 ginal page conte
12263 6e 74 20 69 73 20 66 75 6c 6c 79 20 73 79 6e 63 nt is fully sync
12264 65 64 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6d ed. ** in the m
12265 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 65 69 74 68 ain journal eith
12266 65 72 20 62 65 63 61 75 73 65 20 74 68 65 20 70 er because the p
12267 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 age is not in ca
12268 63 68 65 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a che or else. **
12269 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 72 the page is mar
1226a 6b 65 64 20 61 73 20 6e 65 65 64 53 79 6e 63 3d ked as needSync=
1226b 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 =0.. **. ** 20
1226c 30 38 2d 30 34 2d 31 34 3a 20 20 57 68 65 6e 20 08-04-14: When
1226d 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 76 61 attempting to va
1226e 63 75 75 6d 20 61 20 63 6f 72 72 75 70 74 20 64 cuum a corrupt d
1226f 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 74 atabase file, it
12270 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c . ** is possibl
12271 65 20 74 6f 20 66 61 69 6c 20 61 20 73 74 61 74 e to fail a stat
12272 65 6d 65 6e 74 20 6f 6e 20 61 20 64 61 74 61 62 ement on a datab
12273 61 73 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f ase that does no
12274 74 20 79 65 74 20 65 78 69 73 74 2e 0a 20 20 2a t yet exist.. *
12275 2a 20 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 * Do not attempt
12276 20 74 6f 20 77 72 69 74 65 20 69 66 20 64 61 74 to write if dat
12277 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e abase file has n
12278 65 76 65 72 20 62 65 65 6e 20 6f 70 65 6e 65 64 ever been opened
12279 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 20 3d 20 70 .. */. pPg = p
1227a 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 ager_lookup(pPag
1227b 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 er, pgno);. ass
1227c 65 72 74 28 20 70 50 67 20 7c 7c 20 21 4d 45 4d ert( pPg || !MEM
1227d 44 42 20 29 3b 0a 20 20 50 41 47 45 52 54 52 41 DB );. PAGERTRA
1227e 43 45 28 28 22 50 4c 41 59 42 41 43 4b 20 25 64 CE(("PLAYBACK %d
1227f 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 page %d hash(%0
12280 38 78 29 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 8x) %s\n",.
12281 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
12282 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 ager), pgno, pag
12283 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 er_datahash(pPag
12284 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 er->pageSize, (u
12285 38 2a 29 61 44 61 74 61 29 2c 0a 20 20 20 20 20 8*)aData),.
12286 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e (isMainJrn
12287 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 l?"main-journal"
12288 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a :"sub-journal").
12289 20 20 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61 ));. if( (pPa
1228a 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
1228b 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 R_EXCLUSIVE).
1228c 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d && (pPg==0 || 0=
1228d 3d 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 =(pPg->flags&PGH
1228e 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 DR_NEED_SYNC)).
1228f 20 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 && isOpen(pPag
12290 65 72 2d 3e 66 64 29 0a 20 20 20 26 26 20 21 69 er->fd). && !i
12291 73 55 6e 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20 sUnsync. ){.
12292 20 69 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e i64 ofst = (pgn
12293 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 o-1)*(i64)pPager
12294 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 ->pageSize;.
12295 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
12296 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ite(pPager->fd,
12297 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67 (u8*)aData, pPag
12298 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 er->pageSize, of
12299 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 67 6e st);. if( pgn
1229a 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 o>pPager->dbFile
1229b 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50 Size ){. pP
1229c 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 ager->dbFileSize
1229d 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20 = pgno;. }.
1229e 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 if( pPager->p
1229f 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 20 20 Backup ){.
122a0 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 61 CODEC1(pPager, a
122a1 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 Data, pgno, 3, r
122a2 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b c=SQLITE_NOMEM);
122a3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 61 . sqlite3Ba
122a4 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 65 ckupUpdate(pPage
122a5 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f r->pBackup, pgno
122a6 2c 20 28 75 38 2a 29 61 44 61 74 61 29 3b 0a 20 , (u8*)aData);.
122a7 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 CODEC2(pPag
122a8 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c er, aData, pgno,
122a9 20 37 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 7, rc=SQLITE_NO
122aa 4d 45 4d 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 MEM, aData);.
122ab 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 }. }else if( !
122ac 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50 isMainJrnl && pP
122ad 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 g==0 ){. /* I
122ae 66 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c f this is a roll
122af 62 61 63 6b 20 6f 66 20 61 20 73 61 76 65 70 6f back of a savepo
122b0 69 6e 74 20 61 6e 64 20 64 61 74 61 20 77 61 73 int and data was
122b1 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 0a not written to.
122b2 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 ** the datab
122b3 61 73 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 ase and the page
122b4 20 69 73 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 is not in-memor
122b5 79 2c 20 74 68 65 72 65 20 69 73 20 61 20 70 6f y, there is a po
122b6 74 65 6e 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70 tential. ** p
122b7 72 6f 62 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65 roblem. When the
122b8 20 70 61 67 65 20 69 73 20 6e 65 78 74 20 66 65 page is next fe
122b9 74 63 68 65 64 20 62 79 20 74 68 65 20 62 2d 74 tched by the b-t
122ba 72 65 65 20 6c 61 79 65 72 2c 20 69 74 20 0a 20 ree layer, it .
122bb 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 ** will be re
122bc 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ad from the data
122bd 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 base file, which
122be 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
122bf 62 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 be . ** curre
122c0 6e 74 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 nt. . **.
122c1 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 20 63 ** There are a c
122c2 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 ouple of differe
122c3 6e 74 20 77 61 79 73 20 74 68 69 73 20 63 61 6e nt ways this can
122c4 20 68 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 happen. All are
122c5 20 71 75 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62 quite. ** ob
122c6 73 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e scure. When runn
122c7 69 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f ing in synchrono
122c8 75 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 us mode, this ca
122c9 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20 n only happen .
122ca 20 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67 ** if the pag
122cb 65 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 e is on the free
122cc 2d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61 -list at the sta
122cd 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 rt of the transa
122ce 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 ction, then.
122cf 2a 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 ** populated, th
122d0 65 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20 73 en moved using s
122d1 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
122d2 61 67 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 age().. **.
122d3 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f ** The solutio
122d4 6e 20 69 73 20 74 6f 20 61 64 64 20 61 6e 20 69 n is to add an i
122d5 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 74 6f n-memory page to
122d6 20 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 the cache conta
122d7 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 ining. ** the
122d8 20 64 61 74 61 20 6a 75 73 74 20 72 65 61 64 20 data just read
122d9 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 from the sub-jou
122da 72 6e 61 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70 rnal. Mark the p
122db 61 67 65 20 61 73 20 64 69 72 74 79 20 0a 20 20 age as dirty .
122dc 20 20 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20 ** and if the
122dd 70 61 67 65 72 20 72 65 71 75 69 72 65 73 20 61 pager requires a
122de 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 journal-sync, t
122df 68 65 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 hen mark the pag
122e0 65 20 61 73 20 0a 20 20 20 20 2a 2a 20 72 65 71 e as . ** req
122e1 75 69 72 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c uiring a journal
122e2 2d 73 79 6e 63 20 62 65 66 6f 72 65 20 69 74 20 -sync before it
122e3 69 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 is written..
122e4 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
122e5 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 sSavepnt );.
122e6 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 if( (rc = sqlite
122e7 33 50 61 67 65 72 41 63 71 75 69 72 65 28 70 50 3PagerAcquire(pP
122e8 61 67 65 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67 ager, pgno, &pPg
122e9 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b , 1))!=SQLITE_OK
122ea 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
122eb 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 rc;. }. p
122ec 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 Pg->flags &= ~PG
122ed 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 HDR_NEED_READ;.
122ee 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
122ef 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a MakeDirty(pPg);.
122f0 20 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 7b }. if( pPg ){
122f1 0a 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 . /* No page
122f2 73 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 65 should ever be e
122f3 78 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 xplicitly rolled
122f4 20 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e back that is in
122f5 20 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 20 use, except.
122f6 20 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 77 ** for page 1 w
122f7 68 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e 20 hich is held in
122f8 75 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 use in order to
122f9 6b 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e keep the lock on
122fa 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 the. ** data
122fb 62 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f 77 base active. How
122fc 65 76 65 72 20 73 75 63 68 20 61 20 70 61 67 65 ever such a page
122fd 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 may be rolled b
122fe 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 0a ack as a result.
122ff 20 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 ** of an int
12300 65 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 75 ernal error resu
12301 6c 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f lting in an auto
12302 6d 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 matic call to.
12303 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 ** sqlite3Page
12304 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 rRollback()..
12305 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 */. void *pD
12306 61 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 3d ata;. pData =
12307 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 pPg->pData;.
12308 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 28 memcpy(pData, (
12309 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67 65 u8*)aData, pPage
1230a 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 r->pageSize);.
1230b 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 pPager->xReini
1230c 74 65 72 28 70 50 67 29 3b 0a 20 20 20 20 69 66 ter(pPg);. if
1230d 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 ( isMainJrnl &&
1230e 28 21 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 2a (!isSavepnt || *
1230f 70 4f 66 66 73 65 74 3c 3d 70 50 61 67 65 72 2d pOffset<=pPager-
12310 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 20 29 7b 0a >journalHdr) ){.
12311 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
12312 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 contents of this
12313 20 70 61 67 65 20 77 65 72 65 20 6a 75 73 74 20 page were just
12314 72 65 73 74 6f 72 65 64 20 66 72 6f 6d 20 74 68 restored from th
12315 65 20 6d 61 69 6e 20 0a 20 20 20 20 20 20 2a 2a e main . **
12316 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 journal file, t
12317 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 hen its content
12318 6d 75 73 74 20 62 65 20 61 73 20 74 68 65 79 20 must be as they
12319 77 65 72 65 20 77 68 65 6e 20 74 68 65 20 0a 20 were when the .
1231a 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 ** transact
1231b 69 6f 6e 20 77 61 73 20 66 69 72 73 74 20 6f 70 ion was first op
1231c 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 ened. In this ca
1231d 73 65 20 77 65 20 63 61 6e 20 6d 61 72 6b 20 74 se we can mark t
1231e 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a he page. **
1231f 20 61 73 20 63 6c 65 61 6e 2c 20 73 69 6e 63 65 as clean, since
12320 20 74 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e there will be n
12321 6f 20 6e 65 65 64 20 74 6f 20 77 72 69 74 65 20 o need to write
12322 69 74 20 6f 75 74 20 74 6f 20 74 68 65 2e 0a 20 it out to the..
12323 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
12324 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 65 78 There is one ex
12325 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 ception to this
12326 72 75 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 rule. If the pag
12327 65 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 e is being rolle
12328 64 0a 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 d. ** back
12329 61 73 20 70 61 72 74 20 6f 66 20 61 20 73 61 76 as part of a sav
1232a 65 70 6f 69 6e 74 20 28 6f 72 20 73 74 61 74 65 epoint (or state
1232b 6d 65 6e 74 29 20 72 6f 6c 6c 62 61 63 6b 20 66 ment) rollback f
1232c 72 6f 6d 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a rom an . **
1232d 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f unsynced portio
1232e 6e 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f n of the main jo
1232f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e urnal file, then
12330 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a it is not safe.
12331 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 72 6b ** to mark
12332 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 the page as cle
12333 61 6e 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 an. This is beca
12334 75 73 65 20 6d 61 72 6b 69 6e 67 20 74 68 65 20 use marking the
12335 70 61 67 65 20 61 73 0a 20 20 20 20 20 20 2a 2a page as. **
12336 20 63 6c 65 61 6e 20 77 69 6c 6c 20 63 6c 65 61 clean will clea
12337 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 r the PGHDR_NEED
12338 5f 53 59 4e 43 20 66 6c 61 67 2e 20 53 69 6e 63 _SYNC flag. Sinc
12339 65 20 74 68 65 20 70 61 67 65 20 69 73 0a 20 20 e the page is.
1233a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 69 ** already i
1233b 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
1233c 6c 65 20 28 72 65 63 6f 72 64 65 64 20 69 6e 20 le (recorded in
1233d 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c Pager.pInJournal
1233e 29 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 74 ) and. ** t
1233f 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 he PGHDR_NEED_SY
12340 4e 43 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 NC flag is clear
12341 65 64 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 ed, if the page
12342 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 is written to.
12343 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 ** again wit
12344 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 hin this transac
12345 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 tion, it will be
12346 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 marked as dirty
12347 20 62 75 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 but. ** th
12348 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
12349 43 20 66 6c 61 67 20 77 69 6c 6c 20 6e 6f 74 20 C flag will not
1234a 62 65 20 73 65 74 2e 20 49 74 20 63 6f 75 6c 64 be set. It could
1234b 20 74 68 65 6e 20 70 6f 74 65 6e 74 69 61 6c 6c then potentiall
1234c 79 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 77 72 y. ** be wr
1234d 69 74 74 65 6e 20 6f 75 74 20 69 6e 74 6f 20 74 itten out into t
1234e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1234f 20 62 65 66 6f 72 65 20 69 74 73 20 6a 6f 75 72 before its jour
12350 6e 61 6c 20 66 69 6c 65 0a 20 20 20 20 20 20 2a nal file. *
12351 2a 20 73 65 67 6d 65 6e 74 20 69 73 20 73 79 6e * segment is syn
12352 63 65 64 2e 20 49 66 20 61 20 63 72 61 73 68 20 ced. If a crash
12353 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6f 72 occurs during or
12354 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 2c following this,
12355 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 . ** databa
12356 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 se corruption ma
12357 79 20 65 6e 73 75 65 2e 0a 20 20 20 20 20 20 2a y ensue.. *
12358 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 /. sqlite3P
12359 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 cacheMakeClean(p
1235a 50 67 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 Pg);. }.#ifde
1235b 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
1235c 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 AGES. pPg->pa
1235d 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
1235e 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 agehash(pPg);.#e
1235f 6e 64 69 66 0a 20 20 20 20 2f 2a 20 49 66 20 74 ndif. /* If t
12360 68 69 73 20 77 61 73 20 70 61 67 65 20 31 2c 20 his was page 1,
12361 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65 then restore the
12362 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e value of Pager.
12363 64 62 46 69 6c 65 56 65 72 73 2e 0a 20 20 20 20 dbFileVers..
12364 2a 2a 20 44 6f 20 74 68 69 73 20 62 65 66 6f 72 ** Do this befor
12365 65 20 61 6e 79 20 64 65 63 6f 64 69 6e 67 2e 20 e any decoding.
12366 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d */. if( pgno=
12367 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 =1 ){. memc
12368 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 py(&pPager->dbFi
12369 6c 65 56 65 72 73 2c 20 26 28 28 75 38 2a 29 70 leVers, &((u8*)p
1236a 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a 65 6f 66 Data)[24],sizeof
1236b 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 (pPager->dbFileV
1236c 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 ers));. }..
1236d 20 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 /* Decode the
1236e 70 61 67 65 20 6a 75 73 74 20 72 65 61 64 20 66 page just read f
1236f 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20 rom disk */.
12370 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 CODEC1(pPager, p
12371 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c Data, pPg->pgno,
12372 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f 3, rc=SQLITE_NO
12373 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 MEM);. sqlite
12374 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 70 3PcacheRelease(p
12375 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 Pg);. }. retur
12376 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 n rc;.}../*.** P
12377 61 72 61 6d 65 74 65 72 20 7a 4d 61 73 74 65 72 arameter zMaster
12378 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
12379 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
1237a 20 66 69 6c 65 2e 20 41 20 73 69 6e 67 6c 65 20 file. A single
1237b 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
1237c 74 68 61 74 20 72 65 66 65 72 72 65 64 20 74 6f that referred to
1237d 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1237e 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 6a 75 73 nal file has jus
1237f 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 t been rolled ba
12380 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ck..** This rout
12381 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 69 74 ine checks if it
12382 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 is possible to
12383 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
12384 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 0a r journal file,.
12385 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73 6f 20 69 ** and does so i
12386 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 f it is..**.** A
12387 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74 65 72 20 rgument zMaster
12388 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 50 61 67 may point to Pag
12389 65 72 2e 70 54 6d 70 53 70 61 63 65 2e 20 53 6f er.pTmpSpace. So
1238a 20 74 68 61 74 20 62 75 66 66 65 72 20 69 73 20 that buffer is
1238b 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c 61 62 6c not .** availabl
1238c 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 69 6e e for use within
1238d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
1238e 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 6d 61 73 **.** When a mas
1238f 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
12390 20 69 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 is created, it
12391 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 is populated wit
12392 68 20 74 68 65 20 6e 61 6d 65 73 20 0a 2a 2a 20 h the names .**
12393 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 of all of its ch
12394 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e ild journals, on
12395 65 20 61 66 74 65 72 20 61 6e 6f 74 68 65 72 2c e after another,
12396 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 75 74 formatted as ut
12397 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 f-8 .** encoded
12398 74 65 78 74 2e 20 54 68 65 20 65 6e 64 20 6f 66 text. The end of
12399 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 each child jour
1239a 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d 61 72 6b nal file is mark
1239b 65 64 20 77 69 74 68 20 61 20 0a 2a 2a 20 6e 75 ed with a .** nu
1239c 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 l-terminator byt
1239d 65 20 28 30 78 30 30 29 2e 20 69 2e 65 2e 20 74 e (0x00). i.e. t
1239e 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e he entire conten
1239f 74 73 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a ts of a master j
123a0 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 66 ournal.** file f
123a1 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e or a transaction
123a2 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77 6f 20 64 involving two d
123a3 61 74 61 62 61 73 65 73 20 6d 69 67 68 74 20 62 atabases might b
123a4 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f 68 6f 6d e:.**.** "/hom
123a5 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 72 e/bill/a.db-jour
123a6 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f 62 69 6c nal\x00/home/bil
123a7 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 l/b.db-journal\x
123a8 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d 61 73 74 00".**.** A mast
123a9 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
123aa 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64 65 6c 65 may only be dele
123ab 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20 6f 66 20 ted once all of
123ac 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a 20 6a 6f its child .** jo
123ad 75 72 6e 61 6c 73 20 68 61 76 65 20 62 65 65 6e urnals have been
123ae 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
123af 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
123b0 6e 20 72 65 61 64 73 20 74 68 65 20 63 6f 6e 74 n reads the cont
123b1 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 ents of the mast
123b2 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er-journal file
123b3 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 into .** memory
123b4 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 and loops throug
123b5 68 20 65 61 63 68 20 6f 66 20 74 68 65 20 63 68 h each of the ch
123b6 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ild journal name
123b7 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63 68 20 63 s. For.** each c
123b8 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 hild journal, it
123b9 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a 2a 0a 2a checks if:.**.*
123ba 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 * * if the chi
123bb 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 ld journal exist
123bc 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a 2a 2a 20 s, and if so.**
123bd 20 20 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 * if the child
123be 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e journal contain
123bf 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
123c0 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
123c1 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 7a 4d 61 .** file zMa
123c2 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ster.**.** If a
123c3 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 child journal ca
123c4 6e 20 62 65 20 66 6f 75 6e 64 20 74 68 61 74 20 n be found that
123c5 6d 61 74 63 68 65 73 20 62 6f 74 68 20 6f 66 20 matches both of
123c6 74 68 65 20 63 72 69 74 65 72 69 61 0a 2a 2a 20 the criteria.**
123c7 61 62 6f 76 65 2c 20 74 68 69 73 20 66 75 6e 63 above, this func
123c8 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 74 tion returns wit
123c9 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 hout doing anyth
123ca 69 6e 67 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ing. Otherwise,
123cb 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68 20 63 68 if.** no such ch
123cc 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 ild journal can
123cd 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c 65 20 7a be found, file z
123ce 4d 61 73 74 65 72 20 69 73 20 64 65 6c 65 74 65 Master is delete
123cf 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 66 69 d from.** the fi
123d0 6c 65 2d 73 79 73 74 65 6d 20 75 73 69 6e 67 20 le-system using
123d1 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
123d2 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 )..**.** If an I
123d3 4f 20 65 72 72 6f 72 20 77 69 74 68 69 6e 20 74 O error within t
123d4 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e his function, an
123d5 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
123d6 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a eturned. This.**
123d7 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 function alloca
123d8 74 65 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 tes memory by ca
123d9 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c lling sqlite3Mal
123da 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20 61 6c 6c loc(). If an all
123db 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 ocation.** fails
123dc 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 , SQLITE_NOMEM i
123dd 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
123de 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 49 4f 20 rwise, if no IO
123df 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 or malloc errors
123e0 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53 51 4c 49 .** occur, SQLI
123e1 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
123e2 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 d..**.** TODO: T
123e3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c his function all
123e4 6f 63 61 74 65 73 20 61 20 73 69 6e 67 6c 65 20 ocates a single
123e5 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 block of memory
123e6 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68 65 20 65 to load.** the e
123e7 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f ntire contents o
123e8 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
123e9 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 rnal file. This
123ea 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61 20 63 6f could be.** a co
123eb 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62 79 74 65 uple of kilobyte
123ec 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74 65 6e 74 s or so - potent
123ed 69 61 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 61 ially larger tha
123ee 6e 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 73 n the page .** s
123ef 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ize..*/.static i
123f0 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 nt pager_delmast
123f1 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 er(Pager *pPager
123f2 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d , const char *zM
123f3 61 73 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 aster){. sqlite
123f4 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 3_vfs *pVfs = pP
123f5 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e ager->pVfs;. in
123f6 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
123f7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
123f8 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 n code */. sqli
123f9 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 te3_file *pMaste
123fa 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 r; /* Malloc'
123fb 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c d master-journal
123fc 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
123fd 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
123fe 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b 20 20 20 le *pJournal;
123ff 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63 68 69 6c /* Malloc'd chil
12400 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 d-journal file d
12401 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63 escriptor */. c
12402 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 har *zMasterJour
12403 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 nal = 0; /* Cont
12404 65 6e 74 73 20 6f 66 20 6d 61 73 74 65 72 20 6a ents of master j
12405 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
12406 20 69 36 34 20 6e 4d 61 73 74 65 72 4a 6f 75 72 i64 nMasterJour
12407 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 nal; /* Si
12408 7a 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 ze of master jou
12409 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 rnal file */..
1240a 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 /* Allocate spac
1240b 65 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 70 e for both the p
1240c 4a 6f 75 72 6e 61 6c 20 61 6e 64 20 70 4d 61 73 Journal and pMas
1240d 74 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 ter file descrip
1240e 74 6f 72 73 2e 0a 20 20 2a 2a 20 49 66 20 73 75 tors.. ** If su
1240f 63 63 65 73 73 66 75 6c 2c 20 6f 70 65 6e 20 74 ccessful, open t
12410 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
12411 6c 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 69 l file for readi
12412 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74 ng.. */. pMast
12413 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 er = (sqlite3_fi
12414 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c le *)sqlite3Mall
12415 6f 63 5a 65 72 6f 28 70 56 66 73 2d 3e 73 7a 4f ocZero(pVfs->szO
12416 73 46 69 6c 65 20 2a 20 32 29 3b 0a 20 20 70 4a sFile * 2);. pJ
12417 6f 75 72 6e 61 6c 20 3d 20 28 73 71 6c 69 74 65 ournal = (sqlite
12418 33 5f 66 69 6c 65 20 2a 29 28 28 28 75 38 20 2a 3_file *)(((u8 *
12419 29 70 4d 61 73 74 65 72 29 20 2b 20 70 56 66 73 )pMaster) + pVfs
1241a 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69 ->szOsFile);. i
1241b 66 28 20 21 70 4d 61 73 74 65 72 20 29 7b 0a 20 f( !pMaster ){.
1241c 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
1241d 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 OMEM;. }else{.
1241e 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 const int fla
1241f 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 gs = (SQLITE_OPE
12420 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 N_READONLY|SQLIT
12421 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f E_OPEN_MASTER_JO
12422 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 3d URNAL);. rc =
12423 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
12424 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4d Vfs, zMaster, pM
12425 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 29 aster, flags, 0)
12426 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d ;. }. if( rc!=
12427 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
12428 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12429 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
1242a 73 46 69 6c 65 53 69 7a 65 28 70 4d 61 73 74 65 sFileSize(pMaste
1242b 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f 75 72 6e r, &nMasterJourn
1242c 61 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 al);. if( rc!=S
1242d 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
1242e 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a delmaster_out;..
1242f 20 20 69 66 28 20 6e 4d 61 73 74 65 72 4a 6f 75 if( nMasterJou
12430 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20 20 63 68 rnal>0 ){. ch
12431 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 20 ar *zJournal;.
12432 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 50 char *zMasterP
12433 74 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 tr = 0;. int
12434 6e 4d 61 73 74 65 72 50 74 72 20 3d 20 70 56 66 nMasterPtr = pVf
12435 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b s->mxPathname+1;
12436 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 .. /* Load th
12437 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 e entire master
12438 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 journal file int
12439 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 o space obtained
1243a 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 73 71 6c from. ** sql
1243b 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e ite3_malloc() an
1243c 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 d pointed to by
1243d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2e 20 zMasterJournal.
1243e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 . */. zMas
1243f 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c terJournal = sql
12440 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 ite3Malloc((int)
12441 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b nMasterJournal +
12442 20 6e 4d 61 73 74 65 72 50 74 72 20 2b 20 31 29 nMasterPtr + 1)
12443 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 ;. if( !zMast
12444 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 erJournal ){.
12445 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
12446 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f OMEM;. goto
12447 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12448 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65 }. zMaste
12449 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a rPtr = &zMasterJ
1244a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f ournal[nMasterJo
1244b 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 20 20 72 63 urnal+1];. rc
1244c 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
1244d 28 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 (pMaster, zMaste
1244e 72 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e rJournal, (int)n
1244f 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 MasterJournal, 0
12450 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
12451 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
12452 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 delmaster_out;.
12453 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 zMasterJourna
12454 6c 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c l[nMasterJournal
12455 5d 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 4a 6f 75 ] = 0;.. zJou
12456 72 6e 61 6c 20 3d 20 7a 4d 61 73 74 65 72 4a 6f rnal = zMasterJo
12457 75 72 6e 61 6c 3b 0a 20 20 20 20 77 68 69 6c 65 urnal;. while
12458 28 20 28 7a 4a 6f 75 72 6e 61 6c 2d 7a 4d 61 73 ( (zJournal-zMas
12459 74 65 72 4a 6f 75 72 6e 61 6c 29 3c 6e 4d 61 73 terJournal)<nMas
1245a 74 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 terJournal ){.
1245b 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a int exists;.
1245c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1245d 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c e3OsAccess(pVfs,
1245e 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 zJournal, SQLIT
1245f 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
12460 20 26 65 78 69 73 74 73 29 3b 0a 20 20 20 20 20 &exists);.
12461 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12462 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
12463 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 to delmaster_out
12464 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
12465 69 66 28 20 65 78 69 73 74 73 20 29 7b 0a 20 20 if( exists ){.
12466 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
12467 74 68 65 20 6a 6f 75 72 6e 61 6c 73 20 70 6f 69 the journals poi
12468 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 6d nted to by the m
12469 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78 aster journal ex
1246a 69 73 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a ists.. **
1246b 20 4f 70 65 6e 20 69 74 20 61 6e 64 20 63 68 65 Open it and che
1246c 63 6b 20 69 66 20 69 74 20 70 6f 69 6e 74 73 20 ck if it points
1246d 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f at the master jo
1246e 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 20 20 urnal. If.
1246f 20 20 2a 2a 20 73 6f 2c 20 72 65 74 75 72 6e 20 ** so, return
12470 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 without deleting
12471 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
12472 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 nal file..
12473 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
12474 20 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 c;. int
12475 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f flags = (SQLITE_
12476 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 OPEN_READONLY|SQ
12477 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
12478 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 20 OURNAL);.
12479 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
1247a 70 65 6e 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e pen(pVfs, zJourn
1247b 61 6c 2c 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c al, pJournal, fl
1247c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ags, 0);.
1247d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1247e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1247f 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
12480 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
12481 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 rc = read
12482 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 4a MasterJournal(pJ
12483 6f 75 72 6e 61 6c 2c 20 7a 4d 61 73 74 65 72 50 ournal, zMasterP
12484 74 72 2c 20 6e 4d 61 73 74 65 72 50 74 72 29 3b tr, nMasterPtr);
12485 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
12486 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72 6e 61 6c OsClose(pJournal
12487 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
12488 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
12489 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 goto d
1248a 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 elmaster_out;.
1248b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
1248c 20 63 20 3d 20 7a 4d 61 73 74 65 72 50 74 72 5b c = zMasterPtr[
1248d 30 5d 21 3d 30 20 26 26 20 73 74 72 63 6d 70 28 0]!=0 && strcmp(
1248e 7a 4d 61 73 74 65 72 50 74 72 2c 20 7a 4d 61 73 zMasterPtr, zMas
1248f 74 65 72 29 3d 3d 30 3b 0a 20 20 20 20 20 20 20 ter)==0;.
12490 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 20 20 if( c ){.
12491 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 /* We have a
12492 20 6d 61 74 63 68 2e 20 44 6f 20 6e 6f 74 20 64 match. Do not d
12493 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 elete the master
12494 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a journal file. *
12495 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f /. goto
12496 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a delmaster_out;.
12497 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12498 7d 0a 20 20 20 20 20 20 7a 4a 6f 75 72 6e 61 6c }. zJournal
12499 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c += (sqlite3Strl
1249a 65 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 en30(zJournal)+1
1249b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a );. }. }. .
1249c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1249d 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 Delete(pVfs, zMa
1249e 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c 6d 61 ster, 0);..delma
1249f 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 ster_out:. if(
124a0 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 29 zMasterJournal )
124a1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
124a2 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 ee(zMasterJourna
124a3 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 l);. } . if(
124a4 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 73 pMaster ){. s
124a5 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4d qlite3OsClose(pM
124a6 61 73 74 65 72 29 3b 0a 20 20 20 20 61 73 73 65 aster);. asse
124a7 72 74 28 20 21 69 73 4f 70 65 6e 28 70 4a 6f 75 rt( !isOpen(pJou
124a8 72 6e 61 6c 29 20 29 3b 0a 20 20 7d 0a 20 20 73 rnal) );. }. s
124a9 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61 73 qlite3_free(pMas
124aa 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ter);. return r
124ab 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 c;.}.../*.** Thi
124ac 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
124ad 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ed to change the
124ae 20 61 63 74 75 61 6c 20 73 69 7a 65 20 6f 66 20 actual size of
124af 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a the database .**
124b0 20 66 69 6c 65 20 69 6e 20 74 68 65 20 66 69 6c file in the fil
124b1 65 2d 73 79 73 74 65 6d 2e 20 54 68 69 73 20 6f e-system. This o
124b2 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e nly happens when
124b3 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 committing a tr
124b4 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 6f 72 ansaction,.** or
124b5 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
124b6 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 6e 63 transaction (inc
124b7 6c 75 64 69 6e 67 20 72 6f 6c 6c 69 6e 67 20 62 luding rolling b
124b8 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 ack a hot-journa
124b9 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 l)..**.** If the
124ba 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
124bb 69 6c 65 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c ile is not open,
124bc 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 or an exclusive
124bd 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 0a 2a 2a 20 lock is not.**
124be 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74 held, this funct
124bf 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 ion is a no-op.
124c0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 Otherwise, the s
124c1 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
124c2 69 73 0a 2a 2a 20 63 68 61 6e 67 65 64 20 74 6f is.** changed to
124c3 20 6e 50 61 67 65 20 70 61 67 65 73 20 28 6e 50 nPage pages (nP
124c4 61 67 65 2a 70 50 61 67 65 72 2d 3e 70 61 67 65 age*pPager->page
124c5 53 69 7a 65 20 62 79 74 65 73 29 2e 20 49 66 20 Size bytes). If
124c6 74 68 65 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 the file.** on d
124c7 69 73 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 isk is currently
124c8 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6e 50 61 larger than nPa
124c9 67 65 20 70 61 67 65 73 2c 20 74 68 65 6e 20 75 ge pages, then u
124ca 73 65 20 74 68 65 20 56 46 53 0a 2a 2a 20 78 54 se the VFS.** xT
124cb 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64 runcate() method
124cc 20 74 6f 20 74 72 75 6e 63 61 74 65 20 69 74 2e to truncate it.
124cd 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 74 20 6d 69 .**.** Or, it mi
124ce 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 ght might be the
124cf 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66 case that the f
124d0 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73 ile on disk is s
124d1 6d 61 6c 6c 65 72 20 74 68 61 6e 20 0a 2a 2a 20 maller than .**
124d2 6e 50 61 67 65 20 70 61 67 65 73 2e 20 53 6f 6d nPage pages. Som
124d3 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
124d4 65 6d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f em implementatio
124d5 6e 73 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75 ns can get confu
124d6 73 65 64 20 69 66 20 0a 2a 2a 20 79 6f 75 20 74 sed if .** you t
124d7 72 79 20 74 6f 20 74 72 75 6e 63 61 74 65 20 61 ry to truncate a
124d8 20 66 69 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69 file to some si
124d9 7a 65 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 ze that is large
124da 72 20 74 68 61 6e 20 69 74 20 0a 2a 2a 20 63 75 r than it .** cu
124db 72 72 65 6e 74 6c 79 20 69 73 2c 20 73 6f 20 64 rrently is, so d
124dc 65 74 65 63 74 20 74 68 69 73 20 63 61 73 65 20 etect this case
124dd 61 6e 64 20 77 72 69 74 65 20 61 20 73 69 6e 67 and write a sing
124de 6c 65 20 7a 65 72 6f 20 62 79 74 65 20 74 6f 20 le zero byte to
124df 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 .** the end of t
124e0 68 65 20 6e 65 77 20 66 69 6c 65 20 69 6e 73 74 he new file inst
124e1 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 ead..**.** If su
124e2 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e ccessful, return
124e3 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 SQLITE_OK. If a
124e4 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
124e5 73 20 77 68 69 6c 65 20 6d 6f 64 69 66 79 69 6e s while modifyin
124e6 67 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 g.** the databas
124e7 65 20 66 69 6c 65 2c 20 72 65 74 75 72 6e 20 74 e file, return t
124e8 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f he error code to
124e9 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a the caller..*/.
124ea 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
124eb 5f 74 72 75 6e 63 61 74 65 28 50 61 67 65 72 20 _truncate(Pager
124ec 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 *pPager, Pgno nP
124ed 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d age){. int rc =
124ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
124ef 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
124f0 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 =PAGER_EXCLUSIVE
124f1 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
124f2 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 69 36 r->fd) ){. i6
124f3 34 20 63 75 72 72 65 6e 74 53 69 7a 65 2c 20 6e 4 currentSize, n
124f4 65 77 53 69 7a 65 3b 0a 20 20 20 20 2f 2a 20 54 ewSize;. /* T
124f5 4f 44 4f 3a 20 49 73 20 69 74 20 73 61 66 65 20 ODO: Is it safe
124f6 74 6f 20 75 73 65 20 50 61 67 65 72 2e 64 62 46 to use Pager.dbF
124f7 69 6c 65 53 69 7a 65 20 68 65 72 65 3f 20 2a 2f ileSize here? */
124f8 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
124f9 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 3OsFileSize(pPag
124fa 65 72 2d 3e 66 64 2c 20 26 63 75 72 72 65 6e 74 er->fd, ¤t
124fb 53 69 7a 65 29 3b 0a 20 20 20 20 6e 65 77 53 69 Size);. newSi
124fc 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 ze = pPager->pag
124fd 65 53 69 7a 65 2a 28 69 36 34 29 6e 50 61 67 65 eSize*(i64)nPage
124fe 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
124ff 4c 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65 LITE_OK && curre
12500 6e 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20 ntSize!=newSize
12501 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72 ){. if( cur
12502 72 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65 rentSize>newSize
12503 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
12504 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
12505 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e te(pPager->fd, n
12506 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d ewSize);. }
12507 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
12508 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
12509 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 22 22 e(pPager->fd, ""
1250a 2c 20 31 2c 20 6e 65 77 53 69 7a 65 2d 31 29 3b , 1, newSize-1);
1250b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1250c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1250d 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 ){. pPag
1250e 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d er->dbFileSize =
1250f 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a nPage;. }.
12510 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
12511 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12512 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 Set the value of
12513 20 74 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f the Pager.secto
12514 72 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20 66 rSize variable f
12515 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 or the given.**
12516 70 61 67 65 72 20 62 61 73 65 64 20 6f 6e 20 74 pager based on t
12517 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
12518 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 d by the xSector
12519 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f Size method.** o
1251a 66 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 f the open datab
1251b 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 73 65 ase file. The se
1251c 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 ctor size will b
1251d 65 20 75 73 65 64 20 75 73 65 64 20 0a 2a 2a 20 e used used .**
1251e 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
1251f 20 73 69 7a 65 20 61 6e 64 20 61 6c 69 67 6e 6d size and alignm
12520 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 ent of journal h
12521 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a 20 6d 61 eader and .** ma
12522 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 ster journal poi
12523 6e 74 65 72 73 20 77 69 74 68 69 6e 20 63 72 65 nters within cre
12524 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ated journal fil
12525 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 65 es..**.** For te
12526 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 74 68 mporary files th
12527 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 e effective sect
12528 6f 72 20 73 69 7a 65 20 69 73 20 61 6c 77 61 79 or size is alway
12529 73 20 35 31 32 20 62 79 74 65 73 2e 0a 2a 2a 0a s 512 bytes..**.
1252a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f ** Otherwise, fo
1252b 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61 72 79 20 r non-temporary
1252c 66 69 6c 65 73 2c 20 74 68 65 20 65 66 66 65 63 files, the effec
1252d 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 tive sector size
1252e 20 69 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 is.** the value
1252f 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 returned by the
12530 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d xSectorSize() m
12531 65 74 68 6f 64 20 72 6f 75 6e 64 65 64 20 75 70 ethod rounded up
12532 20 74 6f 20 33 32 20 69 66 0a 2a 2a 20 69 74 20 to 32 if.** it
12533 69 73 20 6c 65 73 73 20 74 68 61 6e 20 33 32 2c is less than 32,
12534 20 6f 72 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e or rounded down
12535 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 to MAX_SECTOR_S
12536 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20 69 73 20 IZE if it.** is
12537 67 72 65 61 74 65 72 20 74 68 61 6e 20 4d 41 58 greater than MAX
12538 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f _SECTOR_SIZE..*/
12539 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
1253a 53 65 63 74 6f 72 53 69 7a 65 28 50 61 67 65 72 SectorSize(Pager
1253b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73 *pPager){. ass
1253c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
1253d 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 er->fd) || pPage
1253e 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a r->tempFile );..
1253f 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 if( !pPager->t
12540 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 2f empFile ){. /
12541 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 64 6f * Sector size do
12542 65 73 6e 27 74 20 6d 61 74 74 65 72 20 66 6f 72 esn't matter for
12543 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
12544 2e 20 41 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65 . Also, the file
12545 0a 20 20 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 . ** may not
12546 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 have been opened
12547 20 79 65 74 2c 20 69 6e 20 77 68 69 63 68 20 63 yet, in which c
12548 61 73 65 20 74 68 65 20 4f 73 53 65 63 74 6f 72 ase the OsSector
12549 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 63 61 Size(). ** ca
1254a 6c 6c 20 77 69 6c 6c 20 73 65 67 66 61 75 6c 74 ll will segfault
1254b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
1254c 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
1254d 3d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f = sqlite3OsSecto
1254e 72 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 rSize(pPager->fd
1254f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 );. }. if( pPa
12550 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3c ger->sectorSize<
12551 33 32 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 32 ){. pPager
12552 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 35 ->sectorSize = 5
12553 31 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 12;. }. if( pP
12554 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
12555 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 >MAX_SECTOR_SIZE
12556 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
12557 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3e MAX_SECTOR_SIZE>
12558 3d 35 31 32 20 29 3b 0a 20 20 20 20 70 50 61 67 =512 );. pPag
12559 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d er->sectorSize =
1255a 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 MAX_SECTOR_SIZE
1255b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ;. }.}../*.** P
1255c 6c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 layback the jour
1255d 6e 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 nal and thus res
1255e 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 tore the databas
1255f 65 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 e file to.** the
12560 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e state it was in
12561 20 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 before we start
12562 65 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ed making change
12563 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a s. .**.** The j
12564 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d ournal file form
12565 61 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 at is as follows
12566 3a 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 : .**.** (1) 8
12567 20 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 byte prefix. A
12568 20 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 copy of aJourna
12569 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 lMagic[]..** (2
1256a 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e ) 4 byte big-en
1256b 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 dian integer whi
1256c 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ch is the number
1256d 20 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 of valid page r
1256e 65 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 ecords.**
1256f 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 in the journal.
12570 20 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 If this value i
12571 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 s 0xffffffff, th
12572 65 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a en compute the.*
12573 2a 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f * number o
12574 66 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 f page records f
12575 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 rom the journal
12576 73 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 size..** (3) 4
12577 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e byte big-endian
12578 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
12579 73 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 s the initial va
1257a 6c 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 lue for the .**
1257b 20 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 sanity che
1257c 63 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 cksum..** (4)
1257d 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 4 byte integer w
1257e 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 hich is the numb
1257f 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 er of pages to t
12580 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 runcate the.**
12581 20 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f database to
12582 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 during a rollba
12583 63 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 ck..** (5) 4 b
12584 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
12585 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
12586 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e the sector size.
12587 20 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 The header.**
12588 20 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 is this ma
12589 6e 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ny bytes in size
1258a 2e 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 ..** (6) 4 byt
1258b 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
1258c 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 eger which is th
1258d 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 e page size..**
1258e 20 28 37 29 20 20 7a 65 72 6f 20 70 61 64 64 69 (7) zero paddi
1258f 6e 67 20 6f 75 74 20 74 6f 20 74 68 65 20 6e 65 ng out to the ne
12590 78 74 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 0a xt sector size..
12591 2a 2a 20 20 28 38 29 20 20 5a 65 72 6f 20 6f 72 ** (8) Zero or
12592 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 more pages inst
12593 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 ances, each as f
12594 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 ollows:.**
12595 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 + 4 byte page
12596 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 number..**
12597 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 + pPager->pa
12598 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 geSize bytes of
12599 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 data..**
1259a 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 + 4 byte checks
1259b 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 um.**.** When we
1259c 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f speak of the jo
1259d 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 urnal header, we
1259e 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 mean the first
1259f 37 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 7 items above..*
125a0 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 * Each entry in
125a1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 the journal is a
125a2 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
125a3 65 20 38 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a e 8th item..**.*
125a4 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 * Call the value
125a5 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 from the second
125a6 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 bullet "nRec".
125a7 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d nRec is the num
125a8 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 ber of.** valid
125a9 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 page entries in
125aa 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e the journal. In
125ab 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 most cases, you
125ac 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 can compute the
125ad 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 .** value of nRe
125ae 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 c from the size
125af 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
125b0 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 ile. But if a p
125b1 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 ower.** failure
125b2 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 occurred while t
125b3 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 he journal was b
125b4 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 eing written, it
125b5 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a could be the.**
125b6 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 case that the s
125b7 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ize of the journ
125b8 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 al file had alre
125b9 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 ady been increas
125ba 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 ed but.** the ex
125bb 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 tra entries had
125bc 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 not yet made it
125bd 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 safely to disk.
125be 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c In such a case,
125bf 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
125c0 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 nRec computed f
125c1 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a rom the file siz
125c2 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c e would be too l
125c3 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 arge. For.** th
125c4 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c at reason, we al
125c5 77 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 ways use the nRe
125c6 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 c value in the h
125c7 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eader..**.** If
125c8 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 the nRec value i
125c9 73 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 s 0xffffffff it
125ca 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 means that nRec
125cb 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 should be comput
125cc 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 ed.** from the f
125cd 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 ile size. This
125ce 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 value is used wh
125cf 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 en the user sele
125d0 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 cts the.** no-sy
125d1 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 nc option for th
125d2 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f e journal. A po
125d3 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c wer failure coul
125d4 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 d lead to corrup
125d5 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 tion.** in this
125d6 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 case. But for t
125d7 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f hings like tempo
125d8 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 rary table (whic
125d9 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c h will be.** del
125da 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f eted when the po
125db 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 wer is restored)
125dc 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 we don't care.
125dd 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 .**.** If the f
125de 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 ile opened as th
125df 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
125e0 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 s not a well-for
125e1 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 med.** journal f
125e2 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 ile then all pag
125e3 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 es up to the fir
125e4 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 st corrupted pag
125e5 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 e are rolled.**
125e6 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 back (or no page
125e7 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s if the journal
125e8 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 header is corru
125e9 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e pted). The journ
125ea 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 al file.** is th
125eb 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 en deleted and S
125ec 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
125ed 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f d, just as if no
125ee 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a corruption had.
125ef 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 ** been encounte
125f0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e red..**.** If an
125f1 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 I/O or malloc()
125f2 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
125f3 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 he journal-file
125f4 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a is not deleted.*
125f5 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 * and an error c
125f6 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
125f7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 .**.** The isHot
125f8 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 parameter indic
125f9 61 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65 ates that we are
125fa 20 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 trying to rollb
125fb 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ack a journal.**
125fc 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 that might be a
125fd 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f hot journal. O
125fe 72 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 r, it could be t
125ff 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
12600 69 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 is .** preserved
12601 20 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 because of JOUR
12602 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 NALMODE_PERSIST
12603 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 or JOURNALMODE_T
12604 52 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 RUNCATE..** If t
12605 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c he journal reall
12606 79 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 y is hot, reset
12607 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 the pager cache
12608 70 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a prior rolling.**
12609 20 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e back any conten
1260a 74 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e t. If the journ
1260b 61 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 al is merely per
1260c 73 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 sistent, no rese
1260d 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a t is.** needed..
1260e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
1260f 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 ger_playback(Pag
12610 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
12611 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 isHot){. sqlite
12612 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 3_vfs *pVfs = pP
12613 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 ager->pVfs;. i6
12614 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
12615 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
12616 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
12617 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 le in bytes */.
12618 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 u32 nRec;
12619 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1261a 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 ber of Records i
1261b 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f n the journal */
1261c 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 . u32 u;
1261d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
1261e 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 nsigned loop cou
1261f 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d nter */. Pgno m
12620 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 xPg = 0;
12621 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 /* Size of th
12622 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 e original file
12623 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e in pages */. in
12624 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
12625 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
12626 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f code of a subro
12627 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 utine */. int r
12628 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 es = 1;
12629 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 /* Value ret
1262a 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
1262b 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 OsAccess() */.
1262c 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 char *zMaster =
1262d 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 0; /* Name
1262e 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
1262f 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a al file if any *
12630 2f 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 /. int needPage
12631 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 rReset; /*
12632 54 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 True to reset pa
12633 67 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 ge prior to firs
12634 74 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 t page rollback
12635 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 */.. /* Figure
12636 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 out how many rec
12637 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 ords are in the
12638 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 journal. Abort
12639 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 early if. ** th
1263a 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 e journal is emp
1263b 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ty.. */. asser
1263c 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
1263d 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d ->jfd) );. rc =
1263e 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
1263f 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ze(pPager->jfd,
12640 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 &szJ);. if( rc!
12641 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a =SQLITE_OK || sz
12642 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f J==0 ){. goto
12643 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 end_playback;.
12644 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 }.. /* Read th
12645 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
12646 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a name from the j
12647 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 ournal, if it is
12648 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 present.. ** I
12649 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e f a master journ
1264a 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 al file name is
1264b 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 specified, but t
1264c 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 he file is not.
1264d 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 ** present on d
1264e 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f isk, then the jo
1264f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 urnal is not hot
12650 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 and does not ne
12651 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c ed to be. ** pl
12652 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a ayed back.. **.
12653 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e ** TODO: Techn
12654 69 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f ically the follo
12655 77 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72 wing is an error
12656 20 62 65 63 61 75 73 65 20 69 74 20 61 73 73 75 because it assu
12657 6d 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 mes that. ** bu
12658 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53 ffer Pager.pTmpS
12659 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e pace is (mxPathn
1265a 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20 ame+1) bytes or
1265b 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 larger. i.e. tha
1265c 74 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e t. ** (pPager->
1265d 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 pageSize >= pPag
1265e 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 er->pVfs->mxPath
1265f 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f name+1). Using o
12660 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 s_unix.c,. **
12661 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 31 mxPathname is 51
12662 32 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 20 2, which is the
12663 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e 69 same as the mini
12664 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 mum allowable va
12665 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 lue. ** for pag
12666 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d eSize.. */. zM
12667 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e aster = pPager->
12668 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 pTmpSpace;. rc
12669 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
1266a 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c nal(pPager->jfd,
1266b 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
1266c 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
1266d 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 3d me+1);. if( rc=
1266e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d =SQLITE_OK && zM
1266f 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 aster[0] ){.
12670 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
12671 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 cess(pVfs, zMast
12672 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 er, SQLITE_ACCES
12673 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b S_EXISTS, &res);
12674 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d . }. zMaster =
12675 20 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 0;. if( rc!=SQ
12676 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 LITE_OK || !res
12677 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f ){. goto end_
12678 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 playback;. }.
12679 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
1267a 66 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 ff = 0;. needPa
1267b 67 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74 gerReset = isHot
1267c 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f ;.. /* This loo
1267d 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 p terminates eit
1267e 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 4a her when a readJ
1267f 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a ournalHdr() or .
12680 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 ** pager_playb
12681 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 ack_one_page() c
12682 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c 49 all returns SQLI
12683 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f TE_DONE or an IO
12684 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 error . ** occ
12685 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 urs. . */. whi
12686 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 le( 1 ){. int
12687 20 69 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a isUnsync = 0;..
12688 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 /* Read the
12689 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 next journal hea
1268a 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 der from the jou
1268b 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 rnal file. If t
1268c 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 here are. **
1268d 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 not enough bytes
1268e 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 left in the jou
1268f 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 rnal file for a
12690 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c complete header,
12691 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 or. ** it is
12692 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e corrupted, then
12693 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 a process must
12694 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 of failed while
12695 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 writing it..
12696 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 ** This indicate
12697 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e s nothing more n
12698 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 eeds to be rolle
12699 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 d back.. */.
1269a 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 rc = readJour
1269b 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69 nalHdr(pPager, i
1269c 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 sHot, szJ, &nRec
1269d 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 , &mxPg);. if
1269e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1269f 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 ){ . if( rc
126a0 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
126a1 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
126a2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
126a3 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f . goto end_
126a4 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a playback;. }.
126a5 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 . /* If nRec
126a6 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 is 0xffffffff, t
126a7 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c hen this journal
126a8 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 was created by
126a9 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a a process. **
126aa 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 working in no-s
126ab 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d ync mode. This m
126ac 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 eans that the re
126ad 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 st of the journa
126ae 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f l. ** file co
126af 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c nsists of pages,
126b0 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f there are no mo
126b1 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 re journal heade
126b2 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 rs. Compute.
126b3 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
126b4 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68 nRec based on th
126b5 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 is assumption..
126b6 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 */. if( nR
126b7 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29 ec==0xffffffff )
126b8 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
126b9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
126ba 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
126bb 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 SZ(pPager) );.
126bc 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 nRec = (int)
126bd 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f ((szJ - JOURNAL_
126be 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f HDR_SZ(pPager))/
126bf 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 JOURNAL_PG_SZ(pP
126c0 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 ager));. }..
126c1 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 /* If nRec is
126c2 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 0 and this roll
126c3 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 back is of a tra
126c4 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 nsaction created
126c5 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 by this. **
126c6 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 process and if t
126c7 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c his is the final
126c8 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a header in the j
126c9 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 ournal, then it
126ca 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 means. ** tha
126cb 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 t this part of t
126cc 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 he journal was b
126cd 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 eing filled but
126ce 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
126cf 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 . ** synced t
126d0 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 o disk. Compute
126d1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
126d2 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 ages based on th
126d3 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 e remaining.
126d4 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 ** size of the f
126d5 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
126d6 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72 ** The third ter
126d7 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 m of the test wa
126d8 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 s added to fix t
126d9 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 icket #2565..
126da 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 ** When rolling
126db 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 back a hot jour
126dc 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 nal, nRec==0 alw
126dd 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 ays means that t
126de 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 he next. ** c
126df 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 hunk of the jour
126e0 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 nal contains zer
126e1 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f o pages to be ro
126e2 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a lled back. But.
126e3 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e ** when doin
126e4 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 g a ROLLBACK and
126e5 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 the nRec==0 chu
126e6 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63 nk is the last c
126e7 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 hunk in. ** t
126e8 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d he journal, it m
126e9 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f eans that the jo
126ea 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 urnal might cont
126eb 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 ain additional.
126ec 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74 ** pages that
126ed 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c need to be roll
126ee 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74 ed back and that
126ef 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
126f0 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f ages . ** sho
126f1 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 uld be computed
126f2 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 based on the jou
126f3 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a rnal file size..
126f4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e */. if( n
126f5 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 Rec==0 && !isHot
126f6 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 &&. pPag
126f7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a er->journalHdr+J
126f8 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
126f9 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a ager)==pPager->j
126fa 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 ournalOff ){.
126fb 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 nRec = (int)(
126fc 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a (szJ - pPager->j
126fd 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 ournalOff) / JOU
126fe 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
126ff 72 29 29 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 r));. isUns
12700 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a ync = 1;. }..
12701 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
12702 73 20 74 68 65 20 66 69 72 73 74 20 68 65 61 64 s the first head
12703 65 72 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 er read from the
12704 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 journal, trunca
12705 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 te the. ** da
12706 74 61 62 61 73 65 20 66 69 6c 65 20 62 61 63 6b tabase file back
12707 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c to its original
12708 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 size.. */.
12709 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f if( pPager->jo
1270a 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 urnalOff==JOURNA
1270b 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
1270c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
1270d 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 ager_truncate(pP
1270e 61 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 ager, mxPg);.
1270f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
12710 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
12711 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 goto end_playbac
12712 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
12713 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
12714 3d 20 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 = mxPg;. }..
12715 20 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 /* Copy origi
12716 6e 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 nal pages out of
12717 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 the journal and
12718 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a back into the .
12719 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
1271a 66 69 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 file and/or page
1271b 20 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 cache.. */.
1271c 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 for(u=0; u<nR
1271d 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 ec; u++){.
1271e 69 66 28 20 6e 65 65 64 50 61 67 65 72 52 65 73 if( needPagerRes
1271f 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 et ){. pa
12720 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
12721 29 3b 0a 20 20 20 20 20 20 20 20 6e 65 65 64 50 );. needP
12722 61 67 65 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 agerReset = 0;.
12723 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 }. rc
12724 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
12725 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 _one_page(pPager
12726 2c 31 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 ,1,isUnsync,&pPa
12727 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c ger->journalOff,
12728 30 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 0,0);. if(
12729 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1272a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
1272b 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a =SQLITE_DONE ){.
1272c 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
1272d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1272e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
1272f 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 nalOff = szJ;.
12730 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
12731 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
12732 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 /* If we
12733 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 are unable to r
12734 6f 6c 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 6e ollback, quit an
12735 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 d return the err
12736 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 or. **
12737 63 6f 64 65 2e 20 20 54 68 69 73 20 77 69 6c 6c code. This will
12738 20 63 61 75 73 65 20 74 68 65 20 70 61 67 65 72 cause the pager
12739 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 to enter the er
1273a 72 6f 72 20 73 74 61 74 65 0a 20 20 20 20 20 20 ror state.
1273b 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e ** so that n
1273c 6f 20 66 75 72 74 68 65 72 20 68 61 72 6d 20 77 o further harm w
1273d 69 6c 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 ill be done. Pe
1273e 72 68 61 70 73 20 74 68 65 20 6e 65 78 74 0a 20 rhaps the next.
1273f 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 ** proc
12740 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e ess to come alon
12741 67 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 g will be able t
12742 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 o rollback the d
12743 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 atabase..
12744 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
12745 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 goto end_playbac
12746 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
12747 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
12748 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a /*NOTREACHED*/.
12749 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a assert( 0 );..
1274a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 end_playback:.
1274b 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 /* Following a r
1274c 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 74 ollback, the dat
1274d 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c abase file shoul
1274e 64 20 62 65 20 62 61 63 6b 20 69 6e 20 69 74 73 d be back in its
1274f 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 original. ** s
12750 74 61 74 65 20 70 72 69 6f 72 20 74 6f 20 74 68 tate prior to th
12751 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 e start of the t
12752 72 61 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 ransaction, so i
12753 6e 76 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 nvoke the. ** S
12754 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 QLITE_FCNTL_DB_U
12755 4e 43 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f NCHANGED file-co
12756 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 ntrol method to
12757 64 69 73 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a disable the. **
12758 20 61 73 73 65 72 74 69 6f 6e 20 74 68 61 74 20 assertion that
12759 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
1275a 63 6f 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 counter was modi
1275b 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 fied.. */. ass
1275c 65 72 74 28 0a 20 20 20 20 70 50 61 67 65 72 2d ert(. pPager-
1275d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 >fd->pMethods==0
1275e 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f ||. sqlite3O
1275f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 sFileControl(pPa
12760 67 65 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 ger->fd,SQLITE_F
12761 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 CNTL_DB_UNCHANGE
12762 44 2c 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a D,0)>=SQLITE_OK.
12763 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
12764 69 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68 is playback is h
12765 61 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 appening automat
12766 69 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75 ically as a resu
12767 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a lt of an IO or .
12768 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f ** malloc erro
12769 72 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 20 r that occurred
1276a 61 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 after the change
1276b 2d 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 -counter was upd
1276c 61 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 ated but . ** b
1276d 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 efore the transa
1276e 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 ction was commit
1276f 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68 ted, then the ch
12770 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 ange-counter .
12771 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 ** modification
12772 6d 61 79 20 6a 75 73 74 20 68 61 76 65 20 62 65 may just have be
12773 65 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66 20 en reverted. If
12774 74 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20 this happens in
12775 65 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 exclusive . **
12776 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65 mode, then subse
12777 71 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f quent transactio
12778 6e 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 ns performed by
12779 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 the connection w
1277a 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 ill not. ** upd
1277b 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 ate the change-c
1277c 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 ounter at all. T
1277d 68 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 his may lead to
1277e 63 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 cache inconsiste
1277f 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d ncy. ** problem
12780 73 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 s for other proc
12781 65 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f esses at some po
12782 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 int in the futur
12783 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a e. So, just. **
12784 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 61 in case this ha
12785 73 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 s happened, clea
12786 72 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e r the changeCoun
12787 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a tDone flag now..
12788 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 */. pPager->c
12789 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
1278a 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
1278b 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e;.. if( rc==SQ
1278c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a LITE_OK ){. z
1278d 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d Master = pPager-
1278e 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 >pTmpSpace;.
1278f 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a rc = readMasterJ
12790 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a ournal(pPager->j
12791 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 fd, zMaster, pPa
12792 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 ger->pVfs->mxPat
12793 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 hname+1);. te
12794 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 stcase( rc!=SQLI
12795 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 TE_OK );. }. i
12796 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
12797 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 ){. rc = pag
12798 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 er_end_transacti
12799 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 on(pPager, zMast
1279a 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 er[0]!='\0');.
1279b 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d testcase( rc!=
1279c 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d SQLITE_OK );. }
1279d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1279e 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b E_OK && zMaster[
1279f 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 0] && res ){.
127a0 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 /* If there was
127a1 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
127a2 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 l and this routi
127a3 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 ne will return s
127a4 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 uccess,. ** s
127a5 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 ee if it is poss
127a6 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 ible to delete t
127a7 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
127a8 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 l.. */. rc
127a9 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 = pager_delmast
127aa 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 er(pPager, zMast
127ab 65 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 er);. testcas
127ac 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b e( rc!=SQLITE_OK
127ad 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 );. }.. /* Th
127ae 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 e Pager.sectorSi
127af 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 ze variable may
127b0 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 have been update
127b1 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a d while rolling.
127b2 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 ** back a jour
127b3 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61 nal created by a
127b4 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20 process with a
127b5 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72 different sector
127b6 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 size. ** value
127b7 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68 . Reset it to th
127b8 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20 e correct value
127b9 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 for this process
127ba 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 .. */. setSect
127bb 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a orSize(pPager);.
127bc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
127bd 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 /*.** Playback s
127be 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65 70 6f avepoint pSavepo
127bf 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 int. Or, if pSav
127c0 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 epoint==NULL, th
127c1 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 en playback.** t
127c2 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 he entire master
127c3 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 journal file. T
127c4 68 65 20 63 61 73 65 20 70 53 61 76 65 70 6f 69 he case pSavepoi
127c5 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 nt==NULL occurs
127c6 77 68 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 when .** a ROLLB
127c7 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 ACK TO command i
127c8 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 s invoked on a S
127c9 41 56 45 50 4f 49 4e 54 20 74 68 61 74 20 69 73 AVEPOINT that is
127ca 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a a transaction .
127cb 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a ** savepoint..**
127cc 0a 2a 2a 20 57 68 65 6e 20 70 53 61 76 65 70 6f .** When pSavepo
127cd 69 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 int is not NULL
127ce 28 6d 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 (meaning a non-t
127cf 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 ransaction savep
127d0 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e oint is .** bein
127d1 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 g rolled back),
127d2 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 then the rollbac
127d3 6b 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 k consists of up
127d4 20 74 6f 20 74 68 72 65 65 20 73 74 61 67 65 73 to three stages
127d5 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 ,.** performed i
127d6 6e 20 74 68 65 20 6f 72 64 65 72 20 73 70 65 63 n the order spec
127d7 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a ified:.**.** *
127d8 20 50 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 Pages are playe
127d9 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 d back from the
127da 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 main journal sta
127db 72 74 69 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a rting at byte.**
127dc 20 20 20 20 20 6f 66 66 73 65 74 20 50 61 67 65 offset Page
127dd 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 rSavepoint.iOffs
127de 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e et and continuin
127df 67 20 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 g to .** Pag
127e0 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 erSavepoint.iHdr
127e1 4f 66 66 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 Offset, or to th
127e2 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 e end of the mai
127e3 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 n journal.**
127e4 20 66 69 6c 65 20 69 66 20 50 61 67 65 72 53 61 file if PagerSa
127e5 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 vepoint.iHdrOffs
127e6 65 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a et is zero..**.*
127e7 2a 20 20 20 2a 20 49 66 20 50 61 67 65 72 53 61 * * If PagerSa
127e8 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 vepoint.iHdrOffs
127e9 65 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 et is not zero,
127ea 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 70 then pages are p
127eb 6c 61 79 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 layed.** bac
127ec 6b 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 k starting from
127ed 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
127ee 65 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 er immediately f
127ef 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 ollowing .**
127f0 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
127f1 69 48 64 72 4f 66 66 73 65 74 20 74 6f 20 74 68 iHdrOffset to th
127f2 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 e end of the mai
127f3 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a n journal file..
127f4 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 **.** * Pages
127f5 61 72 65 20 74 68 65 6e 20 70 6c 61 79 65 64 20 are then played
127f6 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 back from the su
127f7 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 b-journal file,
127f8 73 74 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 starting.**
127f9 77 69 74 68 20 74 68 65 20 50 61 67 65 72 53 61 with the PagerSa
127fa 76 65 70 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 vepoint.iSubRec
127fb 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 and continuing t
127fc 6f 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 o the end of.**
127fd 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 the journal
127fe 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f file..**.** Thro
127ff 75 67 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 ughout the rollb
12800 61 63 6b 20 70 72 6f 63 65 73 73 2c 20 65 61 63 ack process, eac
12801 68 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 h time a page is
12802 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 rolled back, th
12803 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 e.** correspondi
12804 6e 67 20 62 69 74 20 69 73 20 73 65 74 20 69 6e ng bit is set in
12805 20 61 20 62 69 74 76 65 63 20 73 74 72 75 63 74 a bitvec struct
12806 75 72 65 20 28 76 61 72 69 61 62 6c 65 20 70 44 ure (variable pD
12807 6f 6e 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d one in the.** im
12808 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c plementation bel
12809 6f 77 29 2e 20 54 68 69 73 20 69 73 20 75 73 65 ow). This is use
1280a 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 d to ensure that
1280b 20 61 20 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a a page is only.
1280c 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 ** rolled back t
1280d 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 he first time it
1280e 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 is encountered
1280f 69 6e 20 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 in either journa
12810 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 l..**.** If pSav
12811 65 70 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 epoint is NULL,
12812 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 6f then pages are o
12813 6e 6c 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 nly played back
12814 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a from the main.**
12815 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 journal file. T
12816 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
12817 66 6f 72 20 61 20 62 69 74 76 65 63 20 69 6e 20 for a bitvec in
12818 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a this case..**.**
12819 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c In either case,
1281a 20 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63 6b before playback
1281b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 commences the P
1281c 61 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72 69 ager.dbSize vari
1281d 61 62 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 able.** is reset
1281e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74 68 to the value th
1281f 61 74 20 69 74 20 68 65 6c 64 20 61 74 20 74 68 at it held at th
12820 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 73 e start of the s
12821 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 avepoint .** (or
12822 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e transaction). N
12823 6f 20 70 61 67 65 20 77 69 74 68 20 61 20 70 61 o page with a pa
12824 67 65 2d 6e 75 6d 62 65 72 20 67 72 65 61 74 65 ge-number greate
12825 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 r than this valu
12826 65 0a 2a 2a 20 69 73 20 70 6c 61 79 65 64 20 62 e.** is played b
12827 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 ack. If one is e
12828 6e 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69 73 ncountered it is
12829 20 73 69 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e simply skipped.
1282a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
1282b 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 agerPlaybackSave
1282c 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 point(Pager *pPa
1282d 67 65 72 2c 20 50 61 67 65 72 53 61 76 65 70 6f ger, PagerSavepo
1282e 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 int *pSavepoint)
1282f 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 {. i64 szJ;
12830 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12831 45 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f Effective size o
12832 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
12833 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 al */. i64 iHdr
12834 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Off;
12835 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 74 /* End of first
12836 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e segment of main
12837 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 -journal records
12838 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 */. int rc = S
12839 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f QLITE_OK; /
1283a 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
1283b 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 . Bitvec *pDone
1283c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 = 0; /* B
1283d 69 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20 itvec to ensure
1283e 70 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 63 pages played bac
1283f 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a k only once */..
12840 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12841 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 ->state>=PAGER_S
12842 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 HARED );.. /* A
12843 6c 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65 63 llocate a bitvec
12844 20 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 65 to use to store
12845 20 74 68 65 20 73 65 74 20 6f 66 20 70 61 67 65 the set of page
12846 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f s rolled back */
12847 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e . if( pSavepoin
12848 74 20 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d t ){. pDone =
12849 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 sqlite3BitvecCr
1284a 65 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74 2d eate(pSavepoint-
1284b 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 20 69 66 28 >nOrig);. if(
1284c 20 21 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 !pDone ){.
1284d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1284e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a OMEM;. }. }.
1284f 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 61 . /* Set the da
12850 74 61 62 61 73 65 20 73 69 7a 65 20 62 61 63 6b tabase size back
12851 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 74 to the value it
12852 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 20 was before the
12853 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 savepoint . **
12854 62 65 69 6e 67 20 72 65 76 65 72 74 65 64 20 77 being reverted w
12855 61 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a as opened.. */.
12856 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
12857 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 = pSavepoint ?
12858 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 pSavepoint->nOri
12859 67 20 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 g : pPager->dbOr
1285a 69 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 igSize;.. /* Us
1285b 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 e pPager->journa
1285c 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 65 lOff as the effe
1285d 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 ctive size of th
1285e 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a e main rollback.
1285f 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 ** journal. T
12860 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d he actual file m
12861 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 ight be larger t
12862 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a han this in. **
12863 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
12864 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50 DE_TRUNCATE or P
12865 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
12866 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 61 _PERSIST. But a
12867 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 nything. ** pas
12868 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 t pPager->journa
12869 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 lOff is off-limi
1286a 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 ts to us.. */.
1286b 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a szJ = pPager->j
1286c 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a ournalOff;.. /*
1286d 20 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e Begin by rollin
1286e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 g back records f
1286f 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 rom the main jou
12870 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 rnal starting at
12871 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 . ** PagerSavep
12872 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 oint.iOffset and
12873 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 continuing to t
12874 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 he next journal
12875 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 header.. ** The
12876 72 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f re might be reco
12877 72 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 rds in the main
12878 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 journal that hav
12879 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a e a page number.
1287a 20 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 ** greater tha
1287b 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 n the current da
1287c 74 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 tabase size (pPa
1287d 67 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 ger->dbSize) but
1287e 20 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c those. ** will
1287f 20 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f be skipped auto
12880 6d 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 matically. Page
12881 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 s are added to p
12882 44 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a Done as they. *
12883 2a 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 * are played bac
12884 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 k.. */. if( pS
12885 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
12886 69 48 64 72 4f 66 66 20 3d 20 70 53 61 76 65 70 iHdrOff = pSavep
12887 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 oint->iHdrOffset
12888 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 ? pSavepoint->i
12889 48 64 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b HdrOffset : szJ;
1288a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
1288b 72 6e 61 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 rnalOff = pSavep
1288c 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 oint->iOffset;.
1288d 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 while( rc==SQ
1288e 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 LITE_OK && pPage
1288f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 r->journalOff<iH
12890 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 72 drOff ){. r
12891 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 c = pager_playba
12892 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 ck_one_page(pPag
12893 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 er, 1, 0, &pPage
12894 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 r->journalOff, 1
12895 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a , pDone);. }.
12896 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d assert( rc!=
12897 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 SQLITE_DONE );.
12898 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
12899 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
1289a 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 0;. }.. /* Co
1289b 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 ntinue rolling b
1289c 61 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 74 20 ack records out
1289d 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
1289e 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a nal starting at.
1289f 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6a ** the first j
128a0 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 ournal header se
128a1 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e en and continuin
128a2 67 20 75 6e 74 69 6c 20 74 68 65 20 65 66 66 65 g until the effe
128a3 63 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f ctive end. ** o
128a4 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
128a5 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e al file. Contin
128a6 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f ue to skip out-o
128a7 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 6e f-range pages an
128a8 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 d. ** continue
128a9 61 64 64 69 6e 67 20 70 61 67 65 73 20 72 6f 6c adding pages rol
128aa 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e led back to pDon
128ab 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 e.. */. while(
128ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
128ad 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
128ae 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 lOff<szJ ){.
128af 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 u32 ii;
128b0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
128b1 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a er */. u32 nJ
128b2 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 Rec = 0; /*
128b3 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 Number of Journa
128b4 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 l Records */.
128b5 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 u32 dummy;.
128b6 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c rc = readJournal
128b7 48 64 72 28 70 50 61 67 65 72 2c 20 30 2c 20 73 Hdr(pPager, 0, s
128b8 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d zJ, &nJRec, &dum
128b9 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 my);. assert(
128ba 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc!=SQLITE_DONE
128bb 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 );.. /*.
128bc 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e ** The "pPager->
128bd 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e journalHdr+JOURN
128be 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
128bf 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e )==pPager->journ
128c0 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 alOff". ** te
128c1 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f st is related to
128c2 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 ticket #2565.
128c3 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69 See the discussi
128c4 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a on in the. **
128c5 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
128c6 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 ) function for a
128c7 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
128c8 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
128c9 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a if( nJRec==0 .
128ca 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e && pPager->
128cb 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e journalHdr+JOURN
128cc 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
128cd 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e )==pPager->journ
128ce 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 alOff. ){.
128cf 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 nJRec = (u32)
128d0 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e ((szJ - pPager->
128d1 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 journalOff)/JOUR
128d2 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 NAL_PG_SZ(pPager
128d3 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f ));. }. fo
128d4 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 r(ii=0; rc==SQLI
128d5 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 TE_OK && ii<nJRe
128d6 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 c && pPager->jou
128d7 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b rnalOff<szJ; ii+
128d8 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 +){. rc = p
128d9 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e ager_playback_on
128da 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 e_page(pPager, 1
128db 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f , 0, &pPager->jo
128dc 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f urnalOff, 1, pDo
128dd 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 ne);. }. a
128de 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
128df 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 E_DONE );. }.
128e0 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
128e1 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d TE_OK || pPager-
128e2 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a >journalOff==szJ
128e3 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c );.. /* Finall
128e4 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 y, rollback pag
128e5 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d es from the sub-
128e6 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 journal. Page t
128e7 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 hat were. ** pr
128e8 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 eviously rolled
128e9 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 back out of the
128ea 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e main journal (an
128eb 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70 d are hence in p
128ec 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 Done). ** will
128ed 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 be skipped. Out
128ee 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 -of-range pages
128ef 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 are also skipped
128f0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 .. */. if( pSa
128f1 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 vepoint ){. u
128f2 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 32 ii;
128f3 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
128f4 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 r */. i64 off
128f5 73 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 set = pSavepoint
128f6 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50 61 ->iSubRec*(4+pPa
128f7 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a ger->pageSize);.
128f8 20 20 20 20 66 6f 72 28 69 69 3d 70 53 61 76 65 for(ii=pSave
128f9 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 point->iSubRec;
128fa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
128fb 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 ii<pPager->nSub
128fc 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 Rec; ii++){.
128fd 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 assert( offset
128fe 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e ==ii*(4+pPager->
128ff 70 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 pageSize) );.
12900 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
12901 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
12902 70 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f pPager, 0, 0, &o
12903 66 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 ffset, 1, pDone)
12904 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
12905 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 rt( rc!=SQLITE_D
12906 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 ONE );. }.. sq
12907 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
12908 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 oy(pDone);. if(
12909 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1290a 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
1290b 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a urnalOff = szJ;.
1290c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1290d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
1290e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d the maximum num
1290f 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 ber of in-memory
12910 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 pages that are
12911 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 allowed..*/.SQLI
12912 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12913 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 sqlite3PagerSetC
12914 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a achesize(Pager *
12915 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 pPager, int mxPa
12916 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 ge){. sqlite3Pc
12917 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 acheSetCachesize
12918 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
12919 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a , mxPage);.}../*
1291a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72 .** Adjust the r
1291b 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65 obustness of the
1291c 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d database to dam
1291d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 age due to OS cr
1291e 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 ashes.** or powe
1291f 72 20 66 61 69 6c 75 72 65 73 20 62 79 20 63 68 r failures by ch
12920 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 anging the numbe
12921 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68 r of syncs()s wh
12922 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 en writing.** th
12923 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
12924 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 al. There are t
12925 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a hree levels:.**.
12926 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20 20 ** OFF
12927 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20 sqlite3OsSync()
12928 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e is never called.
12929 20 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65 This is the de
1292a 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 fault.**
1292b 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 for tempor
1292c 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e ary and transien
1292d 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 t files..**.**
1292e 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 NORMAL The
1292f 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 journal is synce
12930 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72 d once before wr
12931 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 ites begin on th
12932 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
12933 20 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 database. Thi
12934 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 s is normally ad
12935 65 71 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f equate protectio
12936 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 n, but.**
12937 20 20 20 20 20 20 20 69 74 20 69 73 20 74 68 65 it is the
12938 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 oretically possi
12939 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 ble, though very
1293a 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 unlikely,.**
1293b 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 that
1293c 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f an inopertune po
1293d 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c wer failure coul
1293e 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72 d leave the jour
1293f 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 nal.**
12940 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 20 77 in a state w
12941 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 hich would cause
12942 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 damage to the d
12943 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 atabase.**
12944 20 20 20 20 20 20 20 20 77 68 65 6e 20 69 74 20 when it
12945 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a is rolled back..
12946 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 **.** FULL
12947 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 The journal i
12948 73 20 73 79 6e 63 65 64 20 74 77 69 63 65 20 62 s synced twice b
12949 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 efore writes beg
1294a 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 in on the.**
1294b 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 databa
1294c 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64 se (with some ad
1294d 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1294e 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 tion - the nRec
1294f 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 field.**
12950 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 of the jou
12951 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65 rnal header - be
12952 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62 ing written in b
12953 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a etween the two.*
12954 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 * s
12955 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61 73 yncs). If we as
12956 73 75 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e sume that writin
12957 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 g a.**
12958 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 single disk
12959 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 sector is atomic
1295a 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 , then this mode
1295b 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 provides.**
1295c 20 20 20 20 20 20 20 20 20 20 61 73 73 75 72 61 assura
1295d 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 nce that the jou
1295e 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 rnal will not be
1295f 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68 corrupted to th
12960 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
12961 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 point of causi
12962 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 ng damage to the
12963 20 64 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 database during
12964 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
12965 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 Numeric values
12966 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
12967 74 68 65 73 65 20 73 74 61 74 65 73 20 61 72 65 these states are
12968 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d OFF==1, NORMAL=
12969 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2,.** and FULL=3
1296a 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
1296b 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 ITE_OMIT_PAGER_P
1296c 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 RAGMAS.SQLITE_PR
1296d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1296e 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 e3PagerSetSafety
1296f 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61 Level(Pager *pPa
12970 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 ger, int level,
12971 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b int bFullFsync){
12972 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e . pPager->noSyn
12973 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c c = (level==1 |
12974 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 | pPager->tempFi
12975 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 le) ?1:0;. pPag
12976 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 er->fullSync = (
12977 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 level==3 && !pPa
12978 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f ger->tempFile) ?
12979 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 1:0;. pPager->s
1297a 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 ync_flags = (bFu
1297b 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 llFsync?SQLITE_S
1297c 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f YNC_FULL:SQLITE_
1297d 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 SYNC_NORMAL);.
1297e 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 if( pPager->noSy
1297f 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 nc ) pPager->nee
12980 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e dSync = 0;.}.#en
12981 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
12982 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 ollowing global
12983 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 variable is incr
12984 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 emented whenever
12985 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 the library.**
12986 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e attempts to open
12987 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
12988 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 e. This informa
12989 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 tion is used for
1298a 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 .** testing and
1298b 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 analysis only.
1298c 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1298d 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
1298e 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 I int sqlite3_op
1298f 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 entemp_count = 0
12990 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
12991 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 Open a temporary
12992 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 file..**.** Wri
12993 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 te the file desc
12994 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 riptor into *pFi
12995 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 le. Return SQLIT
12996 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
12997 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 .** or some othe
12998 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 r error code if
12999 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 we fail. The OS
1299a 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c will automatical
1299b 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 ly .** delete th
1299c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 e temporary file
1299d 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 when it is clos
1299e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c ed..**.** The fl
1299f 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 ags passed to th
129a0 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f 70 65 e VFS layer xOpe
129a1 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 68 6f n() call are tho
129a2 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 se specified.**
129a3 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 66 73 by parameter vfs
129a4 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 68 20 Flags ORed with
129a5 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
129a6 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f *.** SQLITE_
129a7 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a OPEN_READWRITE.*
129a8 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 * SQLITE_OPE
129a9 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 N_CREATE.**
129aa 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
129ab 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c USIVE.** SQL
129ac 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
129ad 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 NCLOSE.*/.static
129ae 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74 65 int pagerOpente
129af 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 mp(. Pager *pPa
129b0 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 ger, /* T
129b1 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 he pager object
129b2 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
129b3 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 e *pFile, /* Wr
129b4 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ite the file des
129b5 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a criptor here */.
129b6 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 int vfsFlags
129b7 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 /* Flags
129b8 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 passed through
129b9 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b to the VFS */.){
129ba 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
129bb 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
129bc 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 rn code */..#ifd
129bd 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
129be 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d sqlite3_opentem
129bf 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 p_count++; /* U
129c0 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 sed for testing
129c1 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c and analysis onl
129c2 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 y */.#endif.. v
129c3 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 fsFlags |= SQLI
129c4 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
129c5 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f E | SQLITE_OPEN_
129c6 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 CREATE |.
129c7 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e SQLITE_OPEN
129c8 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c _EXCLUSIVE | SQL
129c9 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
129ca 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 NCLOSE;. rc = s
129cb 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 qlite3OsOpen(pPa
129cc 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 ger->pVfs, 0, pF
129cd 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 ile, vfsFlags, 0
129ce 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 );. assert( rc!
129cf 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 =SQLITE_OK || is
129d0 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 Open(pFile) );.
129d1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
129d2 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73 *.** Set the bus
129d3 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 y handler functi
129d4 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 on..**.** The pa
129d5 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 ger invokes the
129d6 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 busy-handler if
129d7 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 sqlite3OsLock()
129d8 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 returns .** SQLI
129d9 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 72 79 TE_BUSY when try
129da 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 ing to upgrade f
129db 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 rom no-lock to a
129dc 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a SHARED lock,.**
129dd 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e 67 20 or when trying
129de 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 to upgrade from
129df 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
129e0 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 to an EXCLUSIVE
129e1 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 .** lock. It doe
129e2 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 s *not* invoke t
129e3 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 he busy handler
129e4 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 when upgrading f
129e5 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f rom.** SHARED to
129e6 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 77 68 RESERVED, or wh
129e7 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f en upgrading fro
129e8 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 43 4c m SHARED to EXCL
129e9 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 USIVE.** (which
129ea 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 68 6f occurs during ho
129eb 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
129ec 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a ck). Summary:.**
129ed 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 6f 6e .** Transition
129ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129ef 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 | Invoke
129f0 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a s xBusyHandler.*
129f1 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * ------------
129f2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129f3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129f4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 ------------.**
129f5 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 NO_LOCK
129f6 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 -> SHARED_LOCK
129f7 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 | Yes.** S
129f8 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 HARED_LOCK ->
129f9 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 RESERVED_LOCK
129fa 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 | No.** SHARE
129fb 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c D_LOCK -> EXCL
129fc 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e USIVE_LOCK | N
129fd 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44 5f o.** RESERVED_
129fe 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 LOCK -> EXCLUSIV
129ff 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a E_LOCK | Yes.*
12a00 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 *.** If the busy
12a01 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 -handler callbac
12a02 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 k returns non-ze
12a03 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 ro, the lock is
12a04 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 66 20 .** retried. If
12a05 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c it returns zero,
12a06 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 then the SQLITE
12a07 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 0a 2a _BUSY error is.*
12a08 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 * returned to th
12a09 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 e caller of the
12a0a 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69 pager API functi
12a0b 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
12a0c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
12a0d 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 e3PagerSetBusyha
12a0e 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 2a ndler(. Pager *
12a0f 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 pPager,
12a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12a11 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f Pager object */
12a12 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 . int (*xBusyHa
12a13 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 ndler)(void *),
12a14 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
12a15 65 72 20 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c er to busy-handl
12a16 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 er function */.
12a17 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 void *pBusyHand
12a18 6c 65 72 41 72 67 20 20 20 20 20 20 20 20 20 20 lerArg
12a19 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e /* Argumen
12a1a 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 42 75 t to pass to xBu
12a1b 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 syHandler */.){
12a1c 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75 73 . pPager->xBus
12a1d 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 yHandler = xBusy
12a1e 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 Handler;. pPage
12a1f 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 r->pBusyHandlerA
12a20 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 rg = pBusyHandle
12a21 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rArg;.}../*.** R
12a22 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e eport the curren
12a23 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 t page size and
12a24 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 number of reserv
12a25 65 64 20 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a ed bytes back.**
12a26 20 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a to the codec..*
12a27 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
12a28 48 41 53 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 HAS_CODEC.static
12a29 20 76 6f 69 64 20 70 61 67 65 72 52 65 70 6f 72 void pagerRepor
12a2a 74 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 tSize(Pager *pPa
12a2b 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 ger){. if( pPag
12a2c 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 er->xCodecSizeCh
12a2d 6e 67 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 ng ){. pPager
12a2e 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 ->xCodecSizeChng
12a2f 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c (pPager->pCodec,
12a30 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
12a31 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
12a32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 (i
12a33 6e 74 29 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 nt)pPager->nRese
12a34 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 rve);. }.}.#els
12a35 65 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72 e.# define pager
12a36 52 65 70 6f 72 74 53 69 7a 65 28 58 29 20 20 20 ReportSize(X)
12a37 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 /* No-op if we
12a38 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 do not support
12a39 61 20 63 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 a codec */.#endi
12a3a 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 f../*.** Change
12a3b 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 75 73 the page size us
12a3c 65 64 20 62 79 20 74 68 65 20 50 61 67 65 72 20 ed by the Pager
12a3d 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 77 20 object. The new
12a3e 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 page size .** is
12a3f 20 70 61 73 73 65 64 20 69 6e 20 2a 70 50 61 67 passed in *pPag
12a40 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 eSize..**.** If
12a41 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
12a42 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
12a43 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
12a44 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 on is called, it
12a45 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 .** is a no-op.
12a46 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e The value return
12a47 65 64 20 69 73 20 74 68 65 20 65 72 72 6f 72 20 ed is the error
12a48 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 state error code
12a49 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f (i.e. .** one o
12a4a 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 f SQLITE_IOERR,
12a4b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f SQLITE_CORRUPT o
12a4c 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a r SQLITE_FULL)..
12a4d 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
12a4e 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 if all of the f
12a4f 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 ollowing are tru
12a50 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 e:.**.** * the
12a51 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 28 new page size (
12a52 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 65 53 value of *pPageS
12a53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 20 28 61 ize) is valid (a
12a54 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f power .** o
12a55 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 f two between 51
12a56 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 2 and SQLITE_MAX
12a57 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c _PAGE_SIZE, incl
12a58 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a usive), and.**.*
12a59 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72 65 20 * * there are
12a5a 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 no outstanding p
12a5b 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20 age references,
12a5c 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 and.**.** * th
12a5d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 65 69 e database is ei
12a5e 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d ther not an in-m
12a5f 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 6f emory database o
12a60 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 r it is.** a
12a61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
12a62 62 61 73 65 20 74 68 61 74 20 63 75 72 72 65 6e base that curren
12a63 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 tly consists of
12a64 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a zero pages..**.*
12a65 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 72 * then the pager
12a66 20 6f 62 6a 65 63 74 20 70 61 67 65 20 73 69 7a object page siz
12a67 65 20 69 73 20 73 65 74 20 74 6f 20 2a 70 50 61 e is set to *pPa
12a68 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 geSize..**.** If
12a69 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 the page size i
12a6a 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 s changed, then
12a6b 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 this function us
12a6c 65 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d es sqlite3PagerM
12a6d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f alloc() .** to o
12a6e 62 74 61 69 6e 20 61 20 6e 65 77 20 50 61 67 65 btain a new Page
12a6f 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 r.pTmpSpace buff
12a70 65 72 2e 20 49 66 20 74 68 69 73 20 61 6c 6c 6f er. If this allo
12a71 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a cation attempt .
12a72 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 ** fails, SQLITE
12a73 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e _NOMEM is return
12a74 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 ed and the page
12a75 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 size remains unc
12a76 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 hanged. .** In a
12a77 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 73 2c 20 ll other cases,
12a78 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
12a79 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
12a7a 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 the page size is
12a7b 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 65 69 not changed, ei
12a7c 74 68 65 72 20 62 65 63 61 75 73 65 20 6f 6e 65 ther because one
12a7d 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 61 74 of the enumerat
12a7e 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 ed.** conditions
12a7f 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 above is not tr
12a80 75 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 61 ue, the pager wa
12a81 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 s in error state
12a82 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 when this.** fu
12a83 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 nction was calle
12a84 64 2c 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 d, or because th
12a85 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
12a86 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 69 6c ion attempt fail
12a87 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 ed, .** then *pP
12a88 61 67 65 53 69 7a 65 20 69 73 20 73 65 74 20 74 ageSize is set t
12a89 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 o the old, retai
12a8a 6e 65 64 20 70 61 67 65 20 73 69 7a 65 20 62 65 ned page size be
12a8b 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
12a8c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
12a8d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
12a8e 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 erSetPagesize(Pa
12a8f 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75 31 36 ger *pPager, u16
12a90 20 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 *pPageSize, int
12a91 20 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 69 6e nReserve){. in
12a92 74 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 t rc = pPager->e
12a93 72 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 rrCode;.. if( r
12a94 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
12a95 20 20 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 u16 pageSize
12a96 20 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 = *pPageSize;.
12a97 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 53 assert( pageS
12a98 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 ize==0 || (pageS
12a99 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 ize>=512 && page
12a9a 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 Size<=SQLITE_MAX
12a9b 5f 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 _PAGE_SIZE) );.
12a9c 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e if( (pPager->
12a9d 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 memDb==0 || pPag
12a9e 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 er->dbSize==0).
12a9f 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 63 && sqlite3Pc
12aa0 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 acheRefCount(pPa
12aa1 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 ger->pPCache)==0
12aa2 20 0a 20 20 20 20 20 26 26 20 70 61 67 65 53 69 . && pageSi
12aa3 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d ze && pageSize!=
12aa4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12aa5 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 . ){. c
12aa6 68 61 72 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 har *pNew = (cha
12aa7 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 4d r *)sqlite3PageM
12aa8 61 6c 6c 6f 63 28 70 61 67 65 53 69 7a 65 29 3b alloc(pageSize);
12aa9 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 . if( !pNew
12aaa 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
12aab 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
12aac 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
12aad 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 pager_reset(
12aae 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 pPager);.
12aaf 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
12ab0 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 e = pageSize;.
12ab1 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
12ab2 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 eFree(pPager->pT
12ab3 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 mpSpace);.
12ab4 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 pPager->pTmpSp
12ab5 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 ace = pNew;.
12ab6 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
12ab7 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 61 eSetPageSize(pPa
12ab8 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 ger->pPCache, pa
12ab9 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d geSize);. }
12aba 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 . }. *pPag
12abb 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 50 61 eSize = (u16)pPa
12abc 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 ger->pageSize;.
12abd 20 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c if( nReserve<
12abe 30 20 29 20 6e 52 65 73 65 72 76 65 20 3d 20 70 0 ) nReserve = p
12abf 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b Pager->nReserve;
12ac0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 52 65 . assert( nRe
12ac1 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 serve>=0 && nRes
12ac2 65 72 76 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 erve<1000 );.
12ac3 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 pPager->nReserv
12ac4 65 20 3d 20 28 69 31 36 29 6e 52 65 73 65 72 76 e = (i16)nReserv
12ac5 65 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70 6f e;. pagerRepo
12ac6 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a rtSize(pPager);.
12ac7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
12ac8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
12ac9 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
12aca 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70 61 67 e "temporary pag
12acb 65 22 20 62 75 66 66 65 72 20 68 65 6c 64 20 69 e" buffer held i
12acc 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 nternally.** by
12acd 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 73 the pager. This
12ace 20 69 73 20 61 20 62 75 66 66 65 72 20 74 68 61 is a buffer tha
12acf 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20 t is big enough
12ad0 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 to hold the.** e
12ad1 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 ntire content of
12ad2 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 a database page
12ad3 2e 20 20 54 68 69 73 20 62 75 66 66 65 72 20 69 . This buffer i
12ad4 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c s used internall
12ad5 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c y.** during roll
12ad6 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 back and will be
12ad7 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65 overwritten whe
12ad8 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b never a rollback
12ad9 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75 74 .** occurs. But
12ada 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 other modules a
12adb 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 20 69 re free to use i
12adc 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 t too, as long a
12add 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b s.** no rollback
12ade 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e 67 2e s are happening.
12adf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
12ae0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
12ae1 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 PagerTempSpace(P
12ae2 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
12ae3 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
12ae4 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a pTmpSpace;.}../*
12ae5 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 .** Attempt to s
12ae6 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 et the maximum d
12ae7 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 atabase page cou
12ae8 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 nt if mxPage is
12ae9 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 positive. .** Ma
12aea 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 ke no changes if
12aeb 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72 6f 20 mxPage is zero
12aec 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20 41 6e or negative. An
12aed 64 20 6e 65 76 65 72 20 72 65 64 75 63 65 20 74 d never reduce t
12aee 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 he.** maximum pa
12aef 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 ge count below t
12af0 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
12af1 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
12af2 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 .**.** Regardles
12af3 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 s of mxPage, ret
12af4 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 urn the current
12af5 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 maximum page cou
12af6 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 nt..*/.SQLITE_PR
12af7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12af8 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 3PagerMaxPageCou
12af9 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
12afa 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
12afb 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 7b if( mxPage>0 ){
12afc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 . pPager->mxP
12afd 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 gno = mxPage;.
12afe 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 }. sqlite3Pager
12aff 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
12b00 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 , 0);. return p
12b01 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d Pager->mxPgno;.}
12b02 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
12b03 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 owing set of rou
12b04 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 tines are used t
12b05 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 69 o disable the si
12b06 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 mulated.** I/O e
12b07 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 rror mechanism.
12b08 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
12b09 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f 69 are used to avoi
12b0a 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 d simulated.** e
12b0b 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73 20 rrors in places
12b0c 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 where we do not
12b0d 63 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f 72 care about error
12b0e 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 s..**.** Unless
12b0f 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 -DSQLITE_TEST=1
12b10 69 73 20 75 73 65 64 2c 20 74 68 65 73 65 20 72 is used, these r
12b11 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 outines are all
12b12 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 no-ops.** and ge
12b13 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a nerate no code..
12b14 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
12b15 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
12b16 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 extern int sqli
12b17 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
12b18 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ding;.SQLITE_API
12b19 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 extern int sqli
12b1a 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
12b1b 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 ;.static int sav
12b1c 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 ed_cnt;.void dis
12b1d 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 able_simulated_i
12b1e 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a o_errors(void){.
12b1f 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 saved_cnt = sq
12b20 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
12b21 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 ending;. sqlite
12b22 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
12b23 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 ng = -1;.}.void
12b24 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 enable_simulated
12b25 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 _io_errors(void)
12b26 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 {. sqlite3_io_e
12b27 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 rror_pending = s
12b28 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 aved_cnt;.}.#els
12b29 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 62 e.# define disab
12b2a 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
12b2b 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 6e errors().# defin
12b2c 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 e enable_simulat
12b2d 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 ed_io_errors().#
12b2e 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 endif../*.** Rea
12b2f 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 d the first N by
12b30 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 tes from the beg
12b31 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 inning of the fi
12b32 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a le into memory.*
12b33 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 6f 69 * that pDest poi
12b34 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 nts to. .**.** I
12b35 66 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 f the pager was
12b36 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e opened on a tran
12b37 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c sient file (zFil
12b38 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a ename==""), or.*
12b39 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 * opened on a fi
12b3a 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 le less than N b
12b3b 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 ytes in size, th
12b3c 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 e output buffer
12b3d 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 is.** zeroed and
12b3e 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
12b3f 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 ned. The rationa
12b40 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 20 74 le for this is t
12b41 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e hat this .** fun
12b42 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
12b43 20 72 65 61 64 20 64 61 74 61 62 61 73 65 20 68 read database h
12b44 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 eaders, and a ne
12b45 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a w transient or.*
12b46 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 74 * zero sized dat
12b47 61 62 61 73 65 20 68 61 73 20 61 20 68 65 61 64 abase has a head
12b48 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 er than consists
12b49 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 entirely of zer
12b4a 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e oes..**.** If an
12b4b 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72 74 y IO error apart
12b4c 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 from SQLITE_IOE
12b4d 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 73 RR_SHORT_READ is
12b4e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a encountered,.**
12b4f 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
12b50 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
12b51 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 he caller and th
12b52 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
12b53 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 e.** output buff
12b54 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f er undefined..*/
12b55 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12b56 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
12b57 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 ReadFileheader(P
12b58 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
12b59 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 t N, unsigned ch
12b5a 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e ar *pDest){. in
12b5b 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
12b5c 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74 ;. memset(pDest
12b5d 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 , 0, N);. asser
12b5e 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
12b5f 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d ->fd) || pPager-
12b60 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 >tempFile );. i
12b61 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
12b62 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 ->fd) ){. IOT
12b63 52 41 43 45 28 28 22 44 42 48 44 52 20 25 70 20 RACE(("DBHDR %p
12b64 30 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 0 %d\n", pPager,
12b65 20 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 N)). rc = sq
12b66 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
12b67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e er->fd, pDest, N
12b68 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
12b69 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 ==SQLITE_IOERR_S
12b6a 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 HORT_READ ){.
12b6b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
12b6c 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 K;. }. }. r
12b6d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
12b6e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f ** Return the to
12b6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
12b70 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
12b71 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 ase file associa
12b72 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 ted .** with pPa
12b73 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 ger. Normally, t
12b74 68 69 73 20 69 73 20 63 61 6c 63 75 6c 61 74 65 his is calculate
12b75 64 20 61 73 20 28 3c 64 62 20 66 69 6c 65 20 73 d as (<db file s
12b76 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e ize>/<page-size>
12b77 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 )..** However, i
12b78 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 65 f the file is be
12b79 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 tween 1 and <pag
12b7a 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 69 6e e-size> bytes in
12b7b 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 size, then .**
12b7c 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64 65 72 this is consider
12b7d 65 64 20 61 20 31 20 70 61 67 65 20 66 69 6c 65 ed a 1 page file
12b7e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
12b7f 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 ager is in error
12b80 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 state when this
12b81 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
12b82 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a led, then the.**
12b83 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 error state err
12b84 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
12b85 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 ned and *pnPage
12b86 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 left unchanged.
12b87 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 Or,.** if the fi
12b88 6c 65 20 73 79 73 74 65 6d 20 68 61 73 20 74 6f le system has to
12b89 20 62 65 20 71 75 65 72 69 65 64 20 66 6f 72 20 be queried for
12b8a 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
12b8b 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 file and.** the
12b8c 71 75 65 72 79 20 61 74 74 65 6d 70 74 20 72 65 query attempt re
12b8d 74 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f turns an IO erro
12b8e 72 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 r, the IO error
12b8f 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
12b90 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 .** and *pnPage
12b91 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 is left unchange
12b92 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 d..**.** Otherwi
12b93 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e se, if everythin
12b94 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
12b95 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 then SQLITE_OK
12b96 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 is returned.** a
12b97 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 73 65 nd *pnPage is se
12b98 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 t to the number
12b99 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
12b9a 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c database..*/.SQL
12b9b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
12b9c 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
12b9d 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 count(Pager *pPa
12b9e 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 ger, int *pnPage
12b9f 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b ){. Pgno nPage;
12ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12ba1 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
12ba2 6e 20 76 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f n via *pnPage */
12ba3 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
12ba4 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 ger is already i
12ba5 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
12ba6 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 e, return the er
12ba7 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 ror code. */. i
12ba8 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f f( pPager->errCo
12ba9 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e de ){. return
12baa 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
12bab 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 ;. }.. /* Dete
12bac 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 rmine the number
12bad 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
12bae 20 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 file. Store thi
12baf 73 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 s in nPage. */.
12bb0 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
12bb1 69 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 izeValid ){.
12bb2 6e 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e nPage = pPager->
12bb3 64 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b dbSize;. }else{
12bb4 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 . int rc;
12bb5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12bb6 45 72 72 6f 72 20 72 65 74 75 72 6e 65 64 20 62 Error returned b
12bb7 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a y OsFileSize() *
12bb8 2f 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 30 3b /. i64 n = 0;
12bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12bba 20 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 File size in by
12bbb 74 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 tes returned by
12bbc 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a OsFileSize() */.
12bbd 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f . assert( isO
12bbe 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12bbf 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
12bc0 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 69 ile );. if( i
12bc1 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
12bc2 29 20 26 26 20 28 30 20 21 3d 20 28 72 63 20 3d ) && (0 != (rc =
12bc3 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 sqlite3OsFileSi
12bc4 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 ze(pPager->fd, &
12bc5 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 70 61 n))) ){. pa
12bc6 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
12bc7 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65 74 , rc);. ret
12bc8 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
12bc9 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 if( n>0 && n<p
12bca 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 Pager->pageSize
12bcb 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d ){. nPage =
12bcc 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1;. }else{.
12bcd 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 50 67 nPage = (Pg
12bce 6e 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e no)(n / pPager->
12bcf 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d pageSize);. }
12bd0 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
12bd1 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e >state!=PAGER_UN
12bd2 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 LOCK ){. pP
12bd3 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e ager->dbSize = n
12bd4 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 Page;. pPag
12bd5 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d er->dbFileSize =
12bd6 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 nPage;. pP
12bd7 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
12bd8 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d d = 1;. }. }
12bd9 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 .. /* If the cu
12bda 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
12bdb 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c pages in the fil
12bdc 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 e is greater tha
12bdd 6e 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 n the . ** conf
12bde 69 67 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 igured maximum p
12bdf 61 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 ager number, inc
12be0 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 rease the allowe
12be1 64 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 d limit so. **
12be2 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 63 61 that the file ca
12be3 6e 20 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a n be read.. */.
12be4 20 20 69 66 28 20 6e 50 61 67 65 3e 70 50 61 67 if( nPage>pPag
12be5 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 er->mxPgno ){.
12be6 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f pPager->mxPgno
12be7 20 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a = (Pgno)nPage;.
12be8 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 }.. /* Set th
12be9 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c e output variabl
12bea 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c e and return SQL
12beb 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 ITE_OK */. if(
12bec 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 pnPage ){. *p
12bed 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 nPage = nPage;.
12bee 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
12bef 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
12bf0 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 Try to obtain a
12bf1 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f lock of type lo
12bf2 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64 61 cktype on the da
12bf3 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 0a tabase file. If.
12bf4 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 ** a similar or
12bf5 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 greater lock is
12bf6 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 already held, th
12bf7 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
12bf8 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 no-op.** (retur
12bf9 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ning SQLITE_OK i
12bfa 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a mmediately)..**.
12bfb 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 74 ** Otherwise, at
12bfc 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 tempt to obtain
12bfd 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 the lock using s
12bfe 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 qlite3OsLock().
12bff 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 Invoke .** the b
12c00 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 usy callback if
12c01 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 the lock is curr
12c02 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 ently not availa
12c03 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 ble. Repeat .**
12c04 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 63 until the busy c
12c05 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 allback returns
12c06 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 false or until t
12c07 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a he attempt to .*
12c08 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 * obtain the loc
12c09 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a k succeeds..**.*
12c0a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
12c0b 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e OK on success an
12c0c 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
12c0d 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 if we cannot obt
12c0e 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e ain.** the lock.
12c0f 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 If the lock is
12c10 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65 73 73 obtained success
12c11 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20 50 fully, set the P
12c12 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 ager.state .** v
12c13 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 ariable to lockt
12c14 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 ype before retur
12c15 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ning..*/.static
12c16 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f int pager_wait_o
12c17 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 n_lock(Pager *pP
12c18 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 ager, int lockty
12c19 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 pe){. int rc;
12c1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c1b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
12c1c 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
12c1d 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 /* The OS lock
12c1e 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74 values must be t
12c1f 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50 he same as the P
12c20 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 ager lock values
12c21 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41 */. assert( PA
12c22 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 GER_SHARED==SHAR
12c23 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 ED_LOCK );. ass
12c24 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52 ert( PAGER_RESER
12c25 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f VED==RESERVED_LO
12c26 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
12c27 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d PAGER_EXCLUSIVE=
12c28 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
12c29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
12c2a 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c file is currentl
12c2b 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 y unlocked then
12c2c 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 the size must be
12c2d 20 75 6e 6b 6e 6f 77 6e 2e 20 49 74 0a 20 20 2a unknown. It. *
12c2e 2a 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 * must not have
12c2f 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 74 been modified at
12c30 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a this point.. *
12c31 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 /. assert( pPag
12c32 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
12c33 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 61 67 65 _SHARED || pPage
12c34 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d r->dbSizeValid==
12c35 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
12c36 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
12c37 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50 GER_SHARED || pP
12c38 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
12c39 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 ==0 );.. /* Che
12c3a 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 ck that this is
12c3b 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20 28 either a no-op (
12c3c 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 because the requ
12c3d 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a 20 ested lock is .
12c3e 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 ** already held
12c3f 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 , or one of the
12c40 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68 61 transistions tha
12c41 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c t the busy-handl
12c42 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 er. ** may be i
12c43 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20 61 nvoked during, a
12c44 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
12c45 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20 20 comment above.
12c46 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 ** sqlite3PagerS
12c47 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29 2e etBusyhandler().
12c48 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
12c49 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d (pPager->state>=
12c4a 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20 20 locktype).
12c4b 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74 61 || (pPager->sta
12c4c 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b te==PAGER_UNLOCK
12c4d 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 && locktype==PA
12c4e 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 GER_SHARED).
12c4f 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 || (pPager->s
12c50 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 tate==PAGER_RESE
12c51 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70 65 RVED && locktype
12c52 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 ==PAGER_EXCLUSIV
12c53 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 70 E). );.. if( p
12c54 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c 6f Pager->state>=lo
12c55 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 63 cktype ){. rc
12c56 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
12c57 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a }else{. do {.
12c58 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12c59 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
12c5a 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a >fd, locktype);.
12c5b 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d }while( rc==
12c5c 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 SQLITE_BUSY && p
12c5d 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
12c5e 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75 73 ler(pPager->pBus
12c5f 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b 0a yHandlerArg) );.
12c60 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12c61 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 TE_OK ){. p
12c62 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 28 Pager->state = (
12c63 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 u8)locktype;.
12c64 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43 IOTRACE(("LOC
12c65 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 K %p %d\n", pPag
12c66 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a 20 er, locktype)).
12c67 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
12c68 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 n rc;.}../*.** F
12c69 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 54 72 unction assertTr
12c6a 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 uncateConstraint
12c6b 28 70 50 61 67 65 72 29 20 63 68 65 63 6b 73 20 (pPager) checks
12c6c 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 that one of the
12c6d 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 .** following is
12c6e 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 64 69 true for all di
12c6f 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e rty pages curren
12c70 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65 2d tly in the page-
12c71 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 cache:.**.** a
12c72 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 ) The page numbe
12c73 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f r is less than o
12c74 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 73 r equal to the s
12c75 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 20 ize of the .**
12c76 20 20 20 20 63 75 72 72 65 6e 74 20 64 61 74 61 current data
12c77 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e 20 70 base image, in p
12c78 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20 20 ages, OR.**.**
12c79 20 62 29 20 69 66 20 74 68 65 20 70 61 67 65 20 b) if the page
12c7a 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77 72 69 content were wri
12c7b 74 74 65 6e 20 61 74 20 74 68 69 73 20 74 69 6d tten at this tim
12c7c 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a e, it would not.
12c7d 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63 65 73 ** be neces
12c7e 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 sary to write th
12c7f 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e e current conten
12c80 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73 75 62 t out to the sub
12c81 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 -journal.**
12c82 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 (as determined
12c83 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75 62 6a by function subj
12c84 52 65 71 75 69 72 65 73 50 61 67 65 28 29 29 2e RequiresPage()).
12c85 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f .**.** If the co
12c86 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74 65 64 ndition asserted
12c87 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
12c88 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c n were not true,
12c89 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69 72 74 and the.** dirt
12c8a 79 20 70 61 67 65 20 77 65 72 65 20 74 6f 20 62 y page were to b
12c8b 65 20 64 69 73 63 61 72 64 65 64 20 66 72 6f 6d e discarded from
12c8c 20 74 68 65 20 63 61 63 68 65 20 76 69 61 20 74 the cache via t
12c8d 68 65 20 70 61 67 65 72 53 74 72 65 73 73 28 29 he pagerStress()
12c8e 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70 61 67 .** routine, pag
12c8f 65 72 53 74 72 65 73 73 28 29 20 77 6f 75 6c 64 erStress() would
12c90 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20 63 not write the c
12c91 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f 6e 74 urrent page cont
12c92 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 61 ent to.** the da
12c93 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
12c94 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72 61 6e a savepoint tran
12c95 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72 6f 6c saction were rol
12c96 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 0a 2a led back after.*
12c97 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65 64 2c * this happened,
12c98 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65 68 the correct beh
12c99 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62 65 20 aviour would be
12c9a 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 to restore the c
12c9b 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74 65 6e urrent.** conten
12c9c 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 48 t of the page. H
12c9d 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74 68 owever, since th
12c9e 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f is content is no
12c9f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65 69 74 t present in eit
12ca0 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 her.** the datab
12ca1 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 65 20 ase file or the
12ca2 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 portion of the r
12ca3 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
12ca4 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 and .** sub-jour
12ca5 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 nal rolled back
12ca6 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f 75 6c the content coul
12ca7 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f 72 65 d not be restore
12ca8 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 d and the.** dat
12ca9 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f 75 6c abase image woul
12caa 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 d become corrupt
12cab 2e 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72 . It is therefor
12cac 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68 61 74 e fortunate that
12cad 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63 75 6d .** this circum
12cae 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20 61 72 stance cannot ar
12caf 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ise..*/.#if defi
12cb0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
12cb1 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 ).static void as
12cb2 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
12cb3 74 72 61 69 6e 74 43 62 28 50 67 48 64 72 20 2a traintCb(PgHdr *
12cb4 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 pPg){. assert(
12cb5 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
12cb6 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73 73 65 _DIRTY );. asse
12cb7 72 74 28 20 21 73 75 62 6a 52 65 71 75 69 72 65 rt( !subjRequire
12cb8 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20 70 50 sPage(pPg) || pP
12cb9 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e 70 50 g->pgno<=pPg->pP
12cba 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b 0a ager->dbSize );.
12cbb 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 }.static void as
12cbc 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
12cbd 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a 70 50 traint(Pager *pP
12cbe 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 ager){. sqlite3
12cbf 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 PcacheIterateDir
12cc0 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ty(pPager->pPCac
12cc1 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e 63 61 he, assertTrunca
12cc2 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 29 3b teConstraintCb);
12cc3 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e .}.#else.# defin
12cc4 65 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 e assertTruncate
12cc5 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 Constraint(pPage
12cc6 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a r).#endif../*.**
12cc7 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e Truncate the in
12cc8 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
12cc9 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e file image to n
12cca 50 61 67 65 20 70 61 67 65 73 2e 20 54 68 69 73 Page pages. This
12ccb 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f .** function do
12ccc 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 es not actually
12ccd 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 modify the datab
12cce 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b ase file on disk
12ccf 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 . It .** just se
12cd0 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ts the internal
12cd1 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 state of the pag
12cd2 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 er object so tha
12cd3 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 t the .** trunca
12cd4 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e tion will be don
12cd5 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 e when the curre
12cd6 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
12cd7 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a s committed..*/.
12cd8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
12cd9 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
12cda 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 TruncateImage(Pa
12cdb 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e ger *pPager, Pgn
12cdc 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65 o nPage){. asse
12cdd 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 rt( pPager->dbSi
12cde 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73 zeValid );. ass
12cdf 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 ert( pPager->dbS
12ce0 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 ize>=nPage );.
12ce1 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12ce2 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
12ce3 45 52 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65 ERVED );. pPage
12ce4 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 r->dbSize = nPag
12ce5 65 3b 0a 20 20 61 73 73 65 72 74 54 72 75 6e 63 e;. assertTrunc
12ce6 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 ateConstraint(pP
12ce7 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
12ce8 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 Shutdown the pag
12ce9 65 20 63 61 63 68 65 2e 20 20 46 72 65 65 20 61 e cache. Free a
12cea 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20 63 6c ll memory and cl
12ceb 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e 0a 2a ose all files..*
12cec 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e 73 61 *.** If a transa
12ced 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70 72 6f ction was in pro
12cee 67 72 65 73 73 20 77 68 65 6e 20 74 68 69 73 20 gress when this
12cef 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
12cf0 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61 6e 73 d, that.** trans
12cf1 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 action is rolled
12cf2 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 74 73 back. All outs
12cf3 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 61 72 tanding pages ar
12cf4 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a 2a 2a e invalidated.**
12cf5 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d 6f 72 and their memor
12cf6 79 20 69 73 20 66 72 65 65 64 2e 20 20 41 6e 79 y is freed. Any
12cf7 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
12cf8 61 20 70 61 67 65 20 61 73 73 6f 63 69 61 74 65 a page associate
12cf9 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 20 70 d.** with this p
12cfa 61 67 65 20 63 61 63 68 65 20 61 66 74 65 72 20 age cache after
12cfb 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 this function re
12cfc 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c turns will likel
12cfd 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 61 y.** result in a
12cfe 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a coredump..**.**
12cff 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
12d00 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 2e 20 lways succeeds.
12d01 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e If a transaction
12d02 20 69 73 20 61 63 74 69 76 65 20 61 6e 20 61 74 is active an at
12d03 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 tempt.** is made
12d04 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b to roll it back
12d05 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
12d06 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 curs during the
12d07 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 20 68 rollback .** a h
12d08 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 62 ot journal may b
12d09 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69 e left in the fi
12d0a 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e 6f 20 lesystem but no
12d0b 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
12d0c 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c d.** to the call
12d0d 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
12d0e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12d0f 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 3PagerClose(Page
12d10 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 64 69 r *pPager){. di
12d11 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f sable_simulated_
12d12 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 73 io_errors();. s
12d13 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
12d14 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 50 61 nMalloc();. pPa
12d15 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 30 ger->errCode = 0
12d16 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c ;. pPager->excl
12d17 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 3b 0a 20 usiveMode = 0;.
12d18 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
12d19 67 65 72 29 3b 0a 20 20 69 66 28 20 4d 45 4d 44 ger);. if( MEMD
12d1a 42 20 29 7b 0a 20 20 20 20 70 61 67 65 72 5f 75 B ){. pager_u
12d1b 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 nlock(pPager);.
12d1c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 }else{. /* S
12d1d 65 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c et Pager.journal
12d1e 48 64 72 20 74 6f 20 2d 31 20 66 6f 72 20 74 68 Hdr to -1 for th
12d1f 65 20 62 65 6e 65 66 69 74 20 6f 66 20 74 68 65 e benefit of the
12d20 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
12d21 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 ) . ** call w
12d22 68 69 63 68 20 6d 61 79 20 62 65 20 6d 61 64 65 hich may be made
12d23 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 from within pag
12d24 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 erUnlockAndRollb
12d25 61 63 6b 28 29 2e 20 49 66 20 69 74 0a 20 20 20 ack(). If it.
12d26 20 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c 20 74 ** is not -1, t
12d27 68 65 6e 20 74 68 65 20 75 6e 73 79 6e 63 65 64 hen the unsynced
12d28 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e 20 6f portion of an o
12d29 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 pen journal file
12d2a 20 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65 20 70 may. ** be p
12d2b 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 layed back into
12d2c 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 the database. If
12d2d 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
12d2e 20 6f 63 63 75 72 73 20 77 68 69 6c 65 0a 20 20 occurs while.
12d2f 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 ** this is hap
12d30 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 61 pening, the data
12d31 62 61 73 65 20 6d 61 79 20 62 65 63 6f 6d 65 20 base may become
12d32 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a 2f 0a corrupt.. */.
12d33 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
12d34 6e 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20 20 20 nalHdr = -1;.
12d35 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 pagerUnlockAndR
12d36 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
12d37 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e . }. sqlite3En
12d38 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
12d39 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 . enable_simula
12d3a 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b ted_io_errors();
12d3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
12d3c 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41 47 CLOSE %d\n", PAG
12d3d 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a ERID(pPager)));.
12d3e 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f 53 IOTRACE(("CLOS
12d3f 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 E %p\n", pPager)
12d40 29 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f ). sqlite3OsClo
12d41 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a se(pPager->fd);.
12d42 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 sqlite3PageFre
12d43 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 e(pPager->pTmpSp
12d44 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 ace);. sqlite3P
12d45 63 61 63 68 65 43 6c 6f 73 65 28 70 50 61 67 65 cacheClose(pPage
12d46 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 23 69 r->pPCache);..#i
12d47 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
12d48 43 4f 44 45 43 0a 20 20 69 66 28 20 70 50 61 67 CODEC. if( pPag
12d49 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29 er->xCodecFree )
12d4a 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 pPager->xCodecF
12d4b 72 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 ree(pPager->pCod
12d4c 65 63 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 ec);.#endif.. a
12d4d 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
12d4e 61 53 61 76 65 70 6f 69 6e 74 20 26 26 20 21 70 aSavepoint && !p
12d4f 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 Pager->pInJourna
12d50 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 l );. assert( !
12d51 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
12d52 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 fd) && !isOpen(p
12d53 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 3b 0a Pager->sjfd) );.
12d54 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
12d55 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 pPager);. retur
12d56 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
12d57 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 #if !defined(NDE
12d58 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 BUG) || defined(
12d59 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a SQLITE_TEST)./*.
12d5a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 ** Return the pa
12d5b 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 61 ge number for pa
12d5c 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 ge pPg..*/.SQLIT
12d5d 45 5f 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 E_PRIVATE Pgno s
12d5e 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e qlite3PagerPagen
12d5f 75 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 50 umber(DbPage *pP
12d60 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 g){. return pPg
12d61 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 ->pgno;.}.#endif
12d62 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e ../*.** Incremen
12d63 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 t the reference
12d64 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 20 70 count for page p
12d65 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 Pg..*/.SQLITE_PR
12d66 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
12d67 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 e3PagerRef(DbPag
12d68 65 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 e *pPg){. sqlit
12d69 65 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29 e3PcacheRef(pPg)
12d6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 ;.}../*.** Sync
12d6b 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 the journal. In
12d6c 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d 61 6b other words, mak
12d6d 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 70 e sure all the p
12d6e 61 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 2a ages that have.*
12d6f 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 * been written t
12d70 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 o the journal ha
12d71 76 65 20 61 63 74 75 61 6c 6c 79 20 72 65 61 63 ve actually reac
12d72 68 65 64 20 74 68 65 20 73 75 72 66 61 63 65 20 hed the surface
12d73 6f 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 61 of the.** disk a
12d74 6e 64 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 nd can be restor
12d75 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e 74 20 ed in the event
12d76 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c of a hot-journal
12d77 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
12d78 20 49 66 20 74 68 65 20 50 61 67 65 72 2e 6e 65 If the Pager.ne
12d79 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20 6e edSync flag is n
12d7a 6f 74 20 73 65 74 2c 20 74 68 65 6e 20 74 68 69 ot set, then thi
12d7b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 0a s function is a.
12d7c 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 ** no-op. Otherw
12d7d 69 73 65 2c 20 74 68 65 20 61 63 74 69 6f 6e 73 ise, the actions
12d7e 20 72 65 71 75 69 72 65 64 20 64 65 70 65 6e 64 required depend
12d7f 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d on the journal-
12d80 6d 6f 64 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 mode.** and the
12d81 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 device character
12d82 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 74 68 istics of the th
12d83 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 61 e file-system, a
12d84 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
12d85 20 20 20 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 * If the jour
12d86 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6e 20 69 nal file is an i
12d87 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
12d88 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74 69 6f 6e file, no action
12d89 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 62 65 20 need.** be
12d8a 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a taken..**.** *
12d8b 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 Otherwise, if t
12d8c 68 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e he device does n
12d8d 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 ot support the S
12d8e 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 AFE_APPEND prope
12d8f 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74 68 65 6e rty,.** then
12d90 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 the nRec field
12d91 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
12d92 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f 75 ntly written jou
12d93 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 20 rnal header.**
12d94 20 20 20 69 73 20 75 70 64 61 74 65 64 20 74 6f is updated to
12d95 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d contain the num
12d96 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 72 ber of journal r
12d97 65 63 6f 72 64 73 20 74 68 61 74 20 68 61 76 65 ecords that have
12d98 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20 77 72 69 .** been wri
12d99 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 tten following i
12d9a 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 t. If the pager
12d9b 69 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 is operating in
12d9c 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20 20 20 20 full-sync.**
12d9d 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 mode, then the
12d9e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
12d9f 73 79 6e 63 65 64 20 62 65 66 6f 72 65 20 74 68 synced before th
12da0 69 73 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 is field is upda
12da1 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 ted..**.** * I
12da2 66 20 74 68 65 20 64 65 76 69 63 65 20 64 6f 65 f the device doe
12da3 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 s not support th
12da4 65 20 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f e SEQUENTIAL pro
12da5 70 65 72 74 79 2c 20 74 68 65 6e 20 0a 2a 2a 20 perty, then .**
12da6 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 journal file
12da7 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a is synced..**.*
12da8 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75 64 6f 2d * Or, in pseudo-
12da9 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 code:.**.** if
12daa 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d 6f 72 79 ( NOT <in-memory
12dab 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 journal> ){.**
12dac 20 20 20 20 69 66 28 20 4e 4f 54 20 53 41 46 45 if( NOT SAFE
12dad 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a 20 20 20 _APPEND ){.**
12dae 20 20 20 20 69 66 28 20 3c 66 75 6c 6c 2d 73 79 if( <full-sy
12daf 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53 79 6e 63 nc mode> ) xSync
12db0 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 (<journal file>)
12db1 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75 70 64 61 ;.** <upda
12db2 74 65 20 6e 52 65 63 20 66 69 65 6c 64 3e 0a 2a te nRec field>.*
12db3 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20 20 20 20 * } .**
12db4 69 66 28 20 4e 4f 54 20 53 45 51 55 45 4e 54 49 if( NOT SEQUENTI
12db5 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 AL ) xSync(<jour
12db6 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 nal file>);.**
12db7 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 61 67 }.**.** The Pag
12db8 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 er.needSync flag
12db9 20 69 73 20 6e 65 76 65 72 20 62 65 20 73 65 74 is never be set
12dba 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 for temporary f
12dbb 69 6c 65 73 2c 20 6f 72 20 61 6e 79 0a 2a 2a 20 iles, or any.**
12dbc 66 69 6c 65 20 6f 70 65 72 61 74 69 6e 67 20 69 file operating i
12dbd 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 28 n no-sync mode (
12dbe 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 73 65 74 Pager.noSync set
12dbf 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a to non-zero)..*
12dc0 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 *.** If successf
12dc1 75 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ul, this routine
12dc2 20 63 6c 65 61 72 73 20 74 68 65 20 50 47 48 44 clears the PGHD
12dc3 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
12dc4 20 6f 66 20 65 76 65 72 79 20 0a 2a 2a 20 70 61 of every .** pa
12dc5 67 65 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c ge currently hel
12dc6 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 65 66 6f d in memory befo
12dc7 72 65 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c re returning SQL
12dc8 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f ITE_OK. If an IO
12dc9 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 65 6e 63 .** error is enc
12dca 6f 75 6e 74 65 72 65 64 2c 20 74 68 65 6e 20 74 ountered, then t
12dcb 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
12dcc 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
12dcd 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 the caller..*/.s
12dce 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a 6f tatic int syncJo
12dcf 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
12dd0 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 ger){. if( pPag
12dd1 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 7b 0a er->needSync ){.
12dd2 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
12dd3 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
12dd4 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
12dd5 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
12dd6 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
12dd7 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 MEMORY ){.
12dd8 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
12dd9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12dda 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
12ddb 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e ode */. con
12ddc 73 74 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c st int iDc = sql
12ddd 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 ite3OsDeviceChar
12dde 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 acteristics(pPag
12ddf 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 61 er->fd);. a
12de0 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
12de1 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 ager->jfd) );..
12de2 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63 if( 0==(iDc
12de3 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 &SQLITE_IOCAP_SA
12de4 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 20 FE_APPEND) ){.
12de5 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c /* This bl
12de6 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 61 ock deals with a
12de7 6e 20 6f 62 73 63 75 72 65 20 70 72 6f 62 6c 65 n obscure proble
12de8 6d 2e 20 49 66 20 74 68 65 20 6c 61 73 74 20 63 m. If the last c
12de9 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 onnection.
12dea 20 20 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20 ** that wrote
12deb 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 to this database
12dec 20 77 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69 was operating i
12ded 6e 20 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 n persistent-jou
12dee 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 rnal. **
12def 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a mode, then the j
12df0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 ournal file may
12df1 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63 at this point ac
12df2 74 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 tually be larger
12df3 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e . ** than
12df4 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
12df5 66 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 20 f bytes. If the
12df6 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68 next thing in th
12df7 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 e journal.
12df8 20 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 6e ** file happen
12df9 73 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61 s to be a journa
12dfa 6c 2d 68 65 61 64 65 72 20 28 77 72 69 74 74 65 l-header (writte
12dfb 6e 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 n as part of the
12dfc 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 . ** prev
12dfd 69 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 ious connections
12dfe 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61 transaction), a
12dff 6e 64 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f nd a crash or po
12e00 77 65 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20 wer-failure .
12e01 20 20 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 ** occurs a
12e02 66 74 65 72 20 6e 52 65 63 20 69 73 20 75 70 64 fter nRec is upd
12e03 61 74 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 ated but before
12e04 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 this connection
12e05 77 72 69 74 65 73 20 0a 20 20 20 20 20 20 20 20 writes .
12e06 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 ** anything else
12e07 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
12e08 66 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73 file (or commits
12e09 2f 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20 /rolls back its
12e0a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e . ** tran
12e0b 73 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53 saction), then S
12e0c 51 4c 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65 QLite may become
12e0d 20 63 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64 confused when d
12e0e 6f 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 oing the .
12e0f 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ** hot-journal
12e10 20 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 rollback follow
12e11 69 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74 ing recovery. It
12e12 20 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61 may roll back a
12e13 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 ll. ** of
12e14 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e this connection
12e15 73 20 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f s data, then pro
12e16 63 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 ceed to rolling
12e17 62 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20 back the old,.
12e18 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d ** out-of-
12e19 64 61 74 65 20 64 61 74 61 20 74 68 61 74 20 66 date data that f
12e1a 6f 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62 ollows it. Datab
12e1b 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a ase corruption..
12e1c 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
12e1d 20 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 ** To work ar
12e1e 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68 ound this, if th
12e1f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 e journal file d
12e20 6f 65 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f oes appear to co
12e21 6e 74 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a ntain. **
12e22 20 61 20 76 61 6c 69 64 20 68 65 61 64 65 72 20 a valid header
12e23 66 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e following Pager.
12e24 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e journalOff, then
12e25 20 77 72 69 74 65 20 61 20 30 78 30 30 0a 20 20 write a 0x00.
12e26 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f ** byte to
12e27 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 the start of it
12e28 20 74 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66 to prevent it f
12e29 72 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e rom being recogn
12e2a 69 7a 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a ized.. **
12e2b 0a 20 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 . ** Vari
12e2c 61 62 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 able iNextHdrOff
12e2d 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 set is set to th
12e2e 65 20 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 e offset at whic
12e2f 68 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a h this. *
12e30 2a 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65 * problematic he
12e31 61 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c ader will occur,
12e32 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 61 if it exists. a
12e33 4d 61 67 69 63 20 69 73 20 75 73 65 64 20 0a 20 Magic is used .
12e34 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74 ** as a t
12e35 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 emporary buffer
12e36 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 66 to inspect the f
12e37 69 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62 irst couple of b
12e38 79 74 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20 ytes of.
12e39 2a 2a 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c ** the potential
12e3a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e journal header.
12e3b 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
12e3c 20 20 20 20 69 36 34 20 69 4e 65 78 74 48 64 72 i64 iNextHdr
12e3d 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 Offset;.
12e3e 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b 0a 09 75 u8 aMagic[8];..u
12e3f 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 8 zHeader[sizeof
12e40 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b (aJournalMagic)+
12e41 34 5d 3b 0a 0a 09 6d 65 6d 63 70 79 28 7a 48 65 4];...memcpy(zHe
12e42 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 ader, aJournalMa
12e43 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 gic, sizeof(aJou
12e44 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 09 70 75 rnalMagic));..pu
12e45 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 t32bits(&zHeader
12e46 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
12e47 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65 72 2d Magic)], pPager-
12e48 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 20 >nRec);..
12e49 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 iNextHdrOffset
12e4a 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 = journalHdrOffs
12e4b 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 et(pPager);.
12e4c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12e4d 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a OsRead(pPager->j
12e4e 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 69 fd, aMagic, 8, i
12e4f 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a NextHdrOffset);.
12e50 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
12e51 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d SQLITE_OK && 0==
12e52 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 memcmp(aMagic, a
12e53 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 JournalMagic, 8)
12e54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 ){. st
12e55 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 7a 65 atic const u8 ze
12e56 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 robyte = 0;.
12e57 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12e58 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 e3OsWrite(pPager
12e59 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79 74 65 ->jfd, &zerobyte
12e5a 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 , 1, iNextHdrOff
12e5b 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a set);. }.
12e5c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
12e5d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
12e5e 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
12e5f 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
12e60 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
12e61 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
12e62 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
12e63 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f nRec value into
12e64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12e65 65 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a e header. If in.
12e66 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d ** full-
12e67 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 synchronous mode
12e68 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e , sync the journ
12e69 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 65 al first. This e
12e6a 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 20 nsures that.
12e6b 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20 ** all data
12e6c 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74 has really hit t
12e6d 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e he disk before n
12e6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20 74 Rec is updated t
12e6f 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a o mark. *
12e70 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 64 * it as a candid
12e71 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b ate for rollback
12e72 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
12e73 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 ** This is
12e74 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20 not required if
12e75 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d the persistent m
12e76 65 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68 edia supports th
12e77 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46 e. ** SAF
12e78 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 E_APPEND propert
12e79 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68 y. Because in th
12e7a 69 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f is case it is no
12e7b 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20 t possible .
12e7c 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61 ** for garba
12e7d 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 70 ge data to be ap
12e7e 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69 pended to the fi
12e7f 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65 le, the nRec fie
12e80 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 ld. ** is
12e81 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 populated with
12e82 30 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 20 0xFFFFFFFF when
12e83 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
12e84 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20 er is written.
12e85 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76 ** and nev
12e86 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 er needs to be u
12e87 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20 pdated..
12e88 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 */. if( p
12e89 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
12e8a 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 && 0==(iDc&SQLIT
12e8b 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
12e8c 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 AL) ){.
12e8d 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 PAGERTRACE(("SY
12e8e 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 NC journal of %d
12e8f 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
12e90 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 ger)));.
12e91 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e IOTRACE(("JSYN
12e92 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 C %p\n", pPager)
12e93 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ). rc =
12e94 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
12e95 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 Pager->jfd, pPag
12e96 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b er->sync_flags);
12e97 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
12e98 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
12e99 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
12e9a 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 }. IOTR
12e9b 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c ACE(("JHDR %p %l
12e9c 6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 ld\n", pPager, p
12e9d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
12e9e 72 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 r));. rc
12e9f 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
12ea0 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 (. pP
12ea1 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 ager->jfd, zHead
12ea2 65 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 61 64 er, sizeof(zHead
12ea3 65 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 er), pPager->jou
12ea4 72 6e 61 6c 48 64 72 0a 09 29 3b 0a 20 20 20 20 rnalHdr..);.
12ea5 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12ea6 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
12ea7 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 c;. }.
12ea8 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c if( 0==(iDc&SQL
12ea9 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
12eaa 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 TIAL) ){.
12eab 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59 PAGERTRACE(("SY
12eac 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64 NC journal of %d
12ead 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
12eae 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20 ger)));.
12eaf 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 IOTRACE(("JSYNC
12eb0 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
12eb1 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
12eb2 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 ite3OsSync(pPage
12eb3 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e r->jfd, pPager->
12eb4 73 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 20 20 sync_flags| .
12eb5 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e (pPager->
12eb6 73 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 4c 49 sync_flags==SQLI
12eb7 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 51 4c TE_SYNC_FULL?SQL
12eb8 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c ITE_SYNC_DATAONL
12eb9 59 3a 30 29 0a 20 20 20 20 20 20 20 20 29 3b 0a Y:0). );.
12eba 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
12ebb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 SQLITE_OK ) retu
12ebc 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
12ebd 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 }.. /* The
12ebe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 61 journal file wa
12ebf 73 20 6a 75 73 74 20 73 75 63 63 65 73 73 66 75 s just successfu
12ec0 6c 6c 79 20 73 79 6e 63 65 64 2e 20 53 65 74 20 lly synced. Set
12ec1 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 0a Pager.needSync .
12ec2 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 ** to zero a
12ec3 6e 64 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 nd clear the PGH
12ec4 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
12ec5 67 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 73 2e g on all pagess.
12ec6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 . */. pPag
12ec7 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 er->needSync = 0
12ec8 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f ;. pPager->jo
12ec9 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 31 urnalStarted = 1
12eca 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
12ecb 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 cheClearSyncFlag
12ecc 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 s(pPager->pPCach
12ecd 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 e);. }.. retur
12ece 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
12ecf 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 /*.** The argume
12ed0 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 nt is the first
12ed1 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 in a linked list
12ed2 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20 of dirty pages
12ed3 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 79 20 connected.** by
12ed4 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 74 79 the PgHdr.pDirty
12ed5 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 66 pointer. This f
12ed6 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 20 65 unction writes e
12ed7 61 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a ach one of the.*
12ed8 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 * in-memory page
12ed9 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 6f s in the list to
12eda 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
12edb 6c 65 2e 20 54 68 65 20 61 72 67 75 6d 65 6e 74 le. The argument
12edc 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2c may.** be NULL,
12edd 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61 6e representing an
12ede 20 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 6e 20 empty list. In
12edf 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 66 this case this f
12ee0 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 20 unction is.** a
12ee1 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 no-op..**.** The
12ee2 20 70 61 67 65 72 20 6d 75 73 74 20 68 6f 6c 64 pager must hold
12ee3 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 at least a RESE
12ee4 52 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e 20 74 RVED lock when t
12ee5 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 his function.**
12ee6 69 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 6f 72 is called. Befor
12ee7 65 20 77 72 69 74 69 6e 67 20 61 6e 79 74 68 69 e writing anythi
12ee8 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
12ee9 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 6c 6f se file, this lo
12eea 63 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 64 65 ck.** is upgrade
12eeb 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 d to an EXCLUSIV
12eec 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c E lock. If the l
12eed 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 ock cannot be ob
12eee 74 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 tained,.** SQLIT
12eef 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e E_BUSY is return
12ef0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 ed and no data i
12ef1 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 s written to the
12ef2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
12ef3 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 ** .** If the pa
12ef4 67 65 72 20 69 73 20 61 20 74 65 6d 70 2d 66 69 ger is a temp-fi
12ef5 6c 65 20 70 61 67 65 72 20 61 6e 64 20 74 68 65 le pager and the
12ef6 20 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 79 73 actual file-sys
12ef7 74 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 20 6e tem file.** is n
12ef8 6f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 74 20 ot yet open, it
12ef9 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6f is created and o
12efa 70 65 6e 65 64 20 62 65 66 6f 72 65 20 61 6e 79 pened before any
12efb 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 72 69 data is .** wri
12efc 74 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 tten out..**.**
12efd 4f 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 Once the lock ha
12efe 73 20 62 65 65 6e 20 75 70 67 72 61 64 65 64 20 s been upgraded
12eff 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 72 and, if necessar
12f00 79 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e y, the file open
12f01 65 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 ed,.** the pages
12f02 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 75 74 are written out
12f03 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
12f04 20 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 6f 72 file in list or
12f05 64 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a 2a 20 der. Writing.**
12f06 61 20 70 61 67 65 20 69 73 20 73 6b 69 70 70 65 a page is skippe
12f07 64 20 69 66 20 69 74 20 6d 65 65 74 73 20 65 69 d if it meets ei
12f08 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c ther of the foll
12f09 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 3a 0a owing criteria:.
12f0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 **.** * The pa
12f0b 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 ge number is gre
12f0c 61 74 65 72 20 74 68 61 6e 20 50 61 67 65 72 2e ater than Pager.
12f0d 64 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 20 20 dbSize, or.**
12f0e 2a 20 54 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 * The PGHDR_DONT
12f0f 5f 57 52 49 54 45 20 66 6c 61 67 20 69 73 20 73 _WRITE flag is s
12f10 65 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a et on the page..
12f11 2a 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 6e 67 **.** If writing
12f12 20 6f 75 74 20 61 20 70 61 67 65 20 63 61 75 73 out a page caus
12f13 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 es the database
12f14 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 50 61 file to grow, Pa
12f15 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 0a 2a ger.dbFileSize.*
12f16 2a 20 69 73 20 75 70 64 61 74 65 64 20 61 63 63 * is updated acc
12f17 6f 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 61 67 ordingly. If pag
12f18 65 20 31 20 69 73 20 77 72 69 74 74 65 6e 20 6f e 1 is written o
12f19 75 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c ut, then the val
12f1a 75 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 6e 20 ue cached.** in
12f1b 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 Pager.dbFileVers
12f1c 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20 74 6f [] is updated to
12f1d 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 20 76 match the new v
12f1e 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a alue stored in.*
12f1f 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 * the database f
12f20 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76 ile..**.** If ev
12f21 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 erything is succ
12f22 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f essful, SQLITE_O
12f23 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
12f24 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 2a f an IO error .*
12f25 2a 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 * occurs, an IO
12f26 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
12f27 74 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 20 74 turned. Or, if t
12f28 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 he EXCLUSIVE loc
12f29 6b 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 6f k cannot.** be o
12f2a 62 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 45 5f btained, SQLITE_
12f2b 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 BUSY is returned
12f2c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
12f2d 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 pager_write_page
12f2e 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c 69 73 list(PgHdr *pLis
12f2f 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 t){. Pager *pPa
12f30 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ger;
12f31 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
12f32 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ger object */.
12f33 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
12f34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f35 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
12f36 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 4e 45 ode */.. if( NE
12f37 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 29 20 VER(pList==0) )
12f38 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12f39 3b 0a 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 ;. pPager = pLi
12f3a 73 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f st->pPager;.. /
12f3b 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 * At this point
12f3c 74 68 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 there may be eit
12f3d 68 65 72 20 61 20 52 45 53 45 52 56 45 44 20 6f her a RESERVED o
12f3e 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b r EXCLUSIVE lock
12f3f 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 on the. ** dat
12f40 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 abase file. If t
12f41 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
12f42 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
12f43 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 k, the following
12f44 0a 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 20 . ** call is a
12f45 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a no-op.. **. **
12f46 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b Moving the lock
12f47 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74 from RESERVED t
12f48 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 74 75 o EXCLUSIVE actu
12f49 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f ally involves go
12f4a 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 ing. ** through
12f4b 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 an intermediate
12f4c 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20 state PENDING.
12f4d 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b A PENDING lock
12f4e 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20 prevents new.
12f4f 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20 ** readers from
12f50 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65 attaching to the
12f51 20 64 61 74 61 62 61 73 65 20 62 75 74 20 69 73 database but is
12f52 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f unsufficient fo
12f53 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69 r us to. ** wri
12f54 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66 te. The idea of
12f55 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 a PENDING lock
12f56 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65 is to prevent ne
12f57 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20 w readers from.
12f58 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68 ** coming in wh
12f59 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72 20 ile we wait for
12f5a 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 72 73 existing readers
12f5b 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a to clear.. **.
12f5c 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70 ** While the p
12f5d 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 52 ager is in the R
12f5e 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 ESERVED state, t
12f5f 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
12f60 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69 base file. ** i
12f61 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 s unchanged and
12f62 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 we can rollback
12f63 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 without having t
12f64 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20 o playback the.
12f65 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f ** journal into
12f66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
12f67 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e tabase file. On
12f68 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e ce we transition
12f69 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49 to. ** EXCLUSI
12f6a 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 VE, it means the
12f6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
12f6c 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20 as been changed
12f6d 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b and any rollback
12f6e 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69 . ** will requi
12f6f 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 re a journal pla
12f70 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 yback.. */. as
12f71 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
12f72 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 ate>=PAGER_RESER
12f73 56 45 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 VED );. rc = pa
12f74 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b ger_wait_on_lock
12f75 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 (pPager, EXCLUSI
12f76 56 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 VE_LOCK);.. /*
12f77 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 61 If the file is a
12f78 20 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e temp-file has n
12f79 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
12f7a 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e ed, open it now.
12f7b 20 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 It. ** is not
12f7c 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20 possible for rc
12f7d 74 6f 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e to be other than
12f7e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 SQLITE_OK if th
12f7f 69 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 69 is branch. ** i
12f80 73 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 65 s taken, as page
12f81 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 r_wait_on_lock()
12f82 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 is a no-op for
12f83 74 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f temp-files.. */
12f84 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 . if( !isOpen(p
12f85 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 Pager->fd) ){.
12f86 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12f87 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 72 63 ->tempFile && rc
12f88 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 ==SQLITE_OK );.
12f89 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 rc = pagerOpe
12f8a 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 ntemp(pPager, pP
12f8b 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 ager->fd, pPager
12f8c 2d 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 7d ->vfsFlags);. }
12f8d 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 .. while( rc==S
12f8e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73 QLITE_OK && pLis
12f8f 74 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 t ){. Pgno pg
12f90 6e 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f no = pList->pgno
12f91 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
12f92 72 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67 re are dirty pag
12f93 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 es in the page c
12f94 61 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e ache with page n
12f95 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 umbers greater.
12f96 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 ** than Pager
12f97 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 .dbSize, this me
12f98 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 ans sqlite3Pager
12f99 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 TruncateImage()
12f9a 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 was called to.
12f9b 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 ** make the fi
12f9c 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 le smaller (pres
12f9d 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 umably by auto-v
12f9e 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 acuum code). Do
12f9f 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a not write. **
12fa0 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 any such pages
12fa1 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 to the file..
12fa2 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c **. ** Also,
12fa3 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 do not write ou
12fa4 74 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 t any page that
12fa5 68 61 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f has the PGHDR_DO
12fa6 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20 NT_WRITE flag.
12fa7 20 20 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 ** set (set by
12fa8 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e sqlite3PagerDon
12fa9 74 57 72 69 74 65 28 29 29 2e 20 20 4e 6f 74 65 tWrite()). Note
12faa 20 74 68 61 74 20 69 66 20 63 6f 6d 70 69 6c 65 that if compile
12fab 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 53 51 d with. ** SQ
12fac 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 LITE_SECURE_DELE
12fad 54 45 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e TE the PGHDR_DON
12fae 54 5f 57 52 49 54 45 20 62 69 74 20 69 73 20 6e T_WRITE bit is n
12faf 65 76 65 72 20 73 65 74 20 61 6e 64 20 73 6f 0a ever set and so.
12fb0 20 20 20 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e ** the secon
12fb1 64 20 74 65 73 74 20 69 73 20 61 6c 77 61 79 73 d test is always
12fb2 20 74 72 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 true.. */.
12fb3 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 if( pgno<=pPag
12fb4 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d er->dbSize && 0=
12fb5 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 =(pList->flags&P
12fb6 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 GHDR_DONT_WRITE)
12fb7 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 ){. i64 of
12fb8 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a fset = (pgno-1)*
12fb9 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 (i64)pPager->pag
12fba 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 eSize; /* Offs
12fbb 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 et to write */.
12fbc 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 char *pData
12fbd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12fbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12fbf 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 /* Data to w
12fc0 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 rite */ ..
12fc1 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 /* Encode the
12fc2 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 database */.
12fc3 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 CODEC2(pPager
12fc4 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 , pList->pData,
12fc5 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 pgno, 6, return
12fc6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 SQLITE_NOMEM, pD
12fc7 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ata);.. /*
12fc8 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61 Write out the pa
12fc9 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 ge data. */.
12fca 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12fcb 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 Write(pPager->fd
12fcc 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d , pData, pPager-
12fcd 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 >pageSize, offse
12fce 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 t);.. /* If
12fcf 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74 page 1 was just
12fd0 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 written, update
12fd1 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
12fd2 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 s to match.
12fd3 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f ** the value no
12fd4 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 w stored in the
12fd5 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
12fd6 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a f writing this .
12fd7 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61 ** page ca
12fd8 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73 used the databas
12fd9 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 e file to grow,
12fda 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a update dbFileSiz
12fdb 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 e. . */.
12fdc 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 if( pgno==1 )
12fdd 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
12fde 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 (&pPager->dbFile
12fdf 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d Vers, &pData[24]
12fe0 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d , sizeof(pPager-
12fe1 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 >dbFileVers));.
12fe2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
12fe3 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 pgno>pPager->db
12fe4 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 FileSize ){.
12fe5 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 pPager->dbFi
12fe6 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 leSize = pgno;.
12fe7 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
12fe8 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63 6b Update any back
12fe9 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 up objects copyi
12fea 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ng the contents
12feb 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20 2a of this pager. *
12fec 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 /. sqlite3B
12fed 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 ackupUpdate(pPag
12fee 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e er->pBackup, pgn
12fef 6f 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70 o, (u8*)pList->p
12ff0 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 50 41 Data);.. PA
12ff1 47 45 52 54 52 41 43 45 28 28 22 53 54 4f 52 45 GERTRACE(("STORE
12ff2 20 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 %d page %d hash
12ff3 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 (%08x)\n",.
12ff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 PA
12ff5 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
12ff6 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 gno, pager_pageh
12ff7 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 ash(pList)));.
12ff8 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 IOTRACE(("PG
12ff9 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 OUT %p %d\n", pP
12ffa 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 ager, pgno));.
12ffb 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 PAGER_INCR(s
12ffc 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 qlite3_pager_wri
12ffd 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 tedb_count);.
12ffe 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 PAGER_INCR(pP
12fff 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 ager->nWrite);.
13000 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
13001 50 41 47 45 52 54 52 41 43 45 28 28 22 4e 4f 53 PAGERTRACE(("NOS
13002 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64 5c TORE %d page %d\
13003 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 n", PAGERID(pPag
13004 65 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 er), pgno));.
13005 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
13006 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
13007 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 73 68 pList->pageHash
13008 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
13009 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 h(pList);.#endif
1300a 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 4c 69 . pList = pLi
1300b 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a st->pDirty;. }.
1300c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1300d 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 ./*.** Append a
1300e 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 63 75 record of the cu
1300f 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 70 rrent state of p
13010 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 20 73 age pPg to the s
13011 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a 20 ub-journal. .**
13012 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 It is the caller
13013 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 s responsibility
13014 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71 75 to use subjRequ
13015 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 63 68 iresPage() to ch
13016 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 74 20 eck .** that it
13017 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 is really requir
13018 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e ed before callin
13019 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e g this function.
1301a 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 .**.** If succes
1301b 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 62 69 sful, set the bi
1301c 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 t corresponding
1301d 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 to pPg->pgno in
1301e 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a 20 66 the bitvecs.** f
1301f 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 or all open save
13020 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 72 65 points before re
13021 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 turning..**.** T
13022 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
13023 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 urns SQLITE_OK i
13024 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 f everything is
13025 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e 20 49 successful, an I
13026 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 O.** error code
13027 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 if the attempt t
13028 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 o write to the s
13029 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 ub-journal fails
1302a 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 45 5f , or .** SQLITE_
1302b 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f NOMEM if a mallo
1302c 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 73 65 c fails while se
1302d 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e 20 61 tting a bit in a
1302e 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 69 savepoint.** bi
1302f 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tvec..*/.static
13030 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 int subjournalPa
13031 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a ge(PgHdr *pPg){.
13032 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13033 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 E_OK;. Pager *p
13034 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
13035 67 65 72 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 ger;. if( isOpe
13036 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 n(pPager->sjfd)
13037 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61 ){. void *pDa
13038 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b ta = pPg->pData;
13039 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 . i64 offset
1303a 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 = pPager->nSubRe
1303b 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 c*(4+pPager->pag
1303c 65 53 69 7a 65 29 3b 0a 20 20 20 20 63 68 61 72 eSize);. char
1303d 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 43 *pData2;.. C
1303e 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 ODEC2(pPager, pD
1303f 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 ata, pPg->pgno,
13040 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 7, return SQLITE
13041 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b _NOMEM, pData2);
13042 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 . PAGERTRACE(
13043 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 ("STMT-JOURNAL %
13044 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 d page %d\n", PA
13045 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
13046 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 Pg->pgno));. .
13047 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49 assert( pageI
13048 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c nJournal(pPg) ||
13049 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 pPg->pgno>pPage
1304a 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b r->dbOrigSize );
1304b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 . rc = write3
1304c 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 73 6a 2bits(pPager->sj
1304d 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 67 2d fd, offset, pPg-
1304e 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 >pgno);. if(
1304f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13050 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
13051 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
13052 72 2d 3e 73 6a 66 64 2c 20 70 44 61 74 61 32 2c r->sjfd, pData2,
13053 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
13054 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 e, offset+4);.
13055 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 }. }. if( rc
13056 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13057 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 pPager->nSubR
13058 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 ec++;. assert
13059 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 ( pPager->nSavep
1305a 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 72 63 oint>0 );. rc
1305b 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e = addToSavepoin
1305c 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c tBitvecs(pPager,
1305d 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d pPg->pgno);. }
1305e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1305f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
13060 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
13061 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6c 61 by the pcache la
13062 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 73 20 yer when it has
13063 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 reached some.**
13064 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 soft memory limi
13065 74 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 t. The first arg
13066 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 ument is a point
13067 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 6f 62 er to a Pager ob
13068 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20 61 73 ject.** (cast as
13069 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65 20 70 a void*). The p
1306a 61 67 65 72 20 69 73 20 61 6c 77 61 79 73 20 27 ager is always '
1306b 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f 74 20 purgeable' (not
1306c 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 an in-memory.**
1306d 64 61 74 61 62 61 73 65 29 2e 20 54 68 65 20 73 database). The s
1306e 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
1306f 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
13070 20 61 20 70 61 67 65 20 74 68 61 74 20 69 73 20 a page that is
13071 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 64 69 .** currently di
13072 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f 20 6f rty but has no o
13073 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
13074 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67 65 0a ences. The page.
13075 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 73 73 ** is always ass
13076 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
13077 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 Pager object pa
13078 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
13079 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a t .** argument..
1307a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20 6f 66 **.** The job of
1307b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
1307c 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20 63 6c s to make pPg cl
1307d 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67 20 69 ean by writing i
1307e 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f ts contents.** o
1307f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
13080 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f 73 73 se file, if poss
13081 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79 20 69 ible. This may i
13082 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67 20 74 nvolve syncing t
13083 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
13084 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 le. .**.** If su
13085 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69 74 65 ccessful, sqlite
13086 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
13087 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 () is called on
13088 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 the page and.**
13089 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
1308a 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 ed. If an IO err
1308b 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
1308c 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 trying to make t
1308d 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e he.** page clean
1308e 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 , the IO error c
1308f 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
13090 20 49 66 20 74 68 65 20 70 61 67 65 20 63 61 6e If the page can
13091 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65 20 63 not be.** made c
13092 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 6f 74 lean for some ot
13093 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 74 20 her reason, but
13094 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c no error occurs,
13095 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a then SQLITE_OK.
13096 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 ** is returned b
13097 79 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d y sqlite3PcacheM
13098 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 6e 6f akeClean() is no
13099 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 t called..*/.sta
1309a 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 74 72 tic int pagerStr
1309b 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50 67 48 ess(void *p, PgH
1309c 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 dr *pPg){. Page
1309d 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 61 67 r *pPager = (Pag
1309e 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63 er *)p;. int rc
1309f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
130a0 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 assert( pPg->pP
130a1 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a ager==pPager );.
130a2 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 assert( pPg->f
130a3 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
130a4 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 64 6f );.. /* The do
130a5 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69 73 20 NotSync flag is
130a6 73 65 74 20 62 79 20 74 68 65 20 73 71 6c 69 74 set by the sqlit
130a7 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 66 e3PagerWrite() f
130a8 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20 69 74 unction while it
130a9 0a 20 20 2a 2a 20 69 73 20 6a 6f 75 72 6e 61 6c . ** is journal
130aa 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 20 74 77 ling a set of tw
130ab 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61 o or more databa
130ac 73 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 se pages that ar
130ad 65 20 73 74 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e e stored. ** on
130ae 20 74 68 65 20 73 61 6d 65 20 64 69 73 6b 20 73 the same disk s
130af 65 63 74 6f 72 2e 20 53 79 6e 63 69 6e 67 20 74 ector. Syncing t
130b0 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f he journal is no
130b1 74 20 61 6c 6c 6f 77 65 64 20 77 68 69 6c 65 0a t allowed while.
130b2 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 ** this is hap
130b3 70 65 6e 69 6e 67 20 61 73 20 69 74 20 69 73 20 pening as it is
130b4 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 important that a
130b5 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20 73 75 ll members of su
130b6 63 68 20 61 0a 20 20 2a 2a 20 73 65 74 20 6f 66 ch a. ** set of
130b7 20 70 61 67 65 73 20 61 72 65 20 73 79 6e 63 65 pages are synce
130b8 64 20 74 6f 20 64 69 73 6b 20 74 6f 67 65 74 68 d to disk togeth
130b9 65 72 2e 20 53 6f 2c 20 69 66 20 74 68 65 20 70 er. So, if the p
130ba 61 67 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f age this functio
130bb 6e 0a 20 20 2a 2a 20 69 73 20 74 72 79 69 6e 67 n. ** is trying
130bc 20 74 6f 20 6d 61 6b 65 20 63 6c 65 61 6e 20 77 to make clean w
130bd 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f ill require a jo
130be 75 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64 20 74 urnal sync and t
130bf 68 65 20 64 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a he doNotSync. *
130c0 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 72 * flag is set, r
130c1 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f eturn without do
130c2 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 54 68 ing anything. Th
130c3 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 e pcache layer w
130c4 69 6c 6c 0a 20 20 2a 2a 20 6a 75 73 74 20 68 61 ill. ** just ha
130c5 76 65 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 ve to go ahead a
130c6 6e 64 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 nd allocate a ne
130c7 77 20 70 61 67 65 20 62 75 66 66 65 72 20 69 6e w page buffer in
130c8 73 74 65 61 64 20 6f 66 0a 20 20 2a 2a 20 72 65 stead of. ** re
130c9 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 2a 2a 0a using pPg.. **.
130ca 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 ** Similarly,
130cb 69 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 if the pager has
130cc 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 already entered
130cd 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
130ce 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 , do not. ** tr
130cf 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 y to write the c
130d0 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 67 20 74 ontents of pPg t
130d1 6f 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 o disk.. */. i
130d2 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
130d3 3e 65 72 72 43 6f 64 65 29 0a 20 20 20 7c 7c 20 >errCode). ||
130d4 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 (pPager->doNotSy
130d5 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 67 73 nc && pPg->flags
130d6 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
130d7 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
130d8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
130d9 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 .. /* Sync the
130da 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 journal file if
130db 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 required. */. i
130dc 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 f( pPg->flags&PG
130dd 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b HDR_NEED_SYNC ){
130de 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f . rc = syncJo
130df 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
130e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
130e1 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
130e2 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 fullSync && .
130e3 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 !(pPager->jou
130e4 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
130e5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
130e6 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73 RY) &&. !(s
130e7 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
130e8 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
130e9 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 ager->fd)&SQLITE
130ea 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
130eb 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ND). ){.
130ec 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 pPager->nRec =
130ed 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 0;. rc = wr
130ee 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 iteJournalHdr(pP
130ef 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ager);. }. }
130f0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
130f1 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 ge number of thi
130f2 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72 s page is larger
130f3 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e than the curren
130f4 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 t size of. ** t
130f5 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 he database imag
130f6 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 e, it may need t
130f7 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 o be written to
130f8 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
130f9 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65 . ** This is be
130fa 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74 cause the call t
130fb 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 o pager_write_pa
130fc 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77 gelist() below w
130fd 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 ill not. ** act
130fe 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 ually write data
130ff 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 to the file in
13100 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a this case.. **.
13101 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 ** Consider th
13102 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 e following sequ
13103 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a ence of events:.
13104 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 **. ** BEGI
13105 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 N;. ** <jou
13106 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a rnal page X>. *
13107 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 * <modify pa
13108 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 ge X>. ** S
13109 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a AVEPOINT sp;. *
1310a 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 * <shrink
1310b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
1310c 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 Y pages>. **
1310d 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73 pagerStress
1310e 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 (page X). **
1310f 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 ROLLBACK TO sp
13110 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 ;. **. ** If (
13111 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 X>Y), then when
13112 70 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63 pagerStress is c
13113 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c alled page X wil
13114 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e l not be written
13115 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 . ** out to the
13116 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
13117 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 but will be drop
13118 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 ped from the cac
13119 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 he. Then,. ** f
1311a 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f ollowing the "RO
1311b 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74 LLBACK TO sp" st
1311c 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 atement, reading
1311d 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61 page X will rea
1311e 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d d. ** data from
1311f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13120 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 le. This will be
13121 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67 the copy of pag
13122 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 e X as it. ** w
13123 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e as when the tran
13124 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c saction started,
13125 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 not as it was w
13126 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 hen "SAVEPOINT s
13127 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63 p". ** was exec
13128 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 uted.. **. **
13129 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
1312a 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72 to write the cur
1312b 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61 rent data for pa
1312c 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 ge X into the .
1312d 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 ** sub-journal
1312e 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20 file now (if it
1312f 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 is not already t
13130 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69 here), so that i
13131 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 t will. ** be r
13132 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63 estored to its c
13133 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65 urrent value whe
13134 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 n the "ROLLBACK
13135 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 TO sp" is . **
13136 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 executed.. */.
13137 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20 20 20 if( NEVER(.
13138 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc==SQLITE_OK
13139 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 && pPg->pgno>pPa
1313a 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 73 ger->dbSize && s
1313b 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 ubjRequiresPage(
1313c 70 50 67 29 0a 20 20 29 20 29 7b 0a 20 20 20 20 pPg). ) ){.
1313d 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 rc = subjournalP
1313e 61 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 age(pPg);. }..
1313f 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f /* Write the co
13140 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 ntents of the pa
13141 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 ge out to the da
13142 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
13143 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13144 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e _OK ){. pPg->
13145 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 pDirty = 0;.
13146 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 rc = pager_write
13147 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a _pagelist(pPg);.
13148 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 }.. /* Mark t
13149 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e he page as clean
1314a 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 . */. if( rc==S
1314b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1314c 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 52 PAGERTRACE(("STR
1314d 45 53 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e ESS %d page %d\n
1314e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
1314f 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b r), pPg->pgno));
13150 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
13151 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 heMakeClean(pPg)
13152 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
13153 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
13154 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a er, rc);.}.../*.
13155 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 ** Allocate and
13156 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 initialize a new
13157 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e Pager object an
13158 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20 d put a pointer
13159 74 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 to it.** in *ppP
1315a 61 67 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 ager. The pager
1315b 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c should eventuall
1315c 79 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61 y be freed by pa
1315d 73 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 ssing it.** to s
1315e 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
1315f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 ()..**.** The zF
13160 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 ilename argument
13161 20 69 73 20 74 68 65 20 70 61 74 68 20 74 6f 20 is the path to
13162 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13163 65 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 e to open..** If
13164 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 zFilename is NU
13165 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d LL then a random
13166 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 ly-named tempora
13167 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 ry file is creat
13168 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 ed.** and used a
13169 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 s the file to be
1316a 20 63 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 cached. Tempora
1316b 72 79 20 66 69 6c 65 73 20 61 72 65 20 62 65 20 ry files are be
1316c 64 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d deleted.** autom
1316d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 atically when th
1316e 65 79 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 ey are closed. I
1316f 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 f zFilename is "
13170 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a :memory:" then .
13171 2a 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 ** all informati
13172 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61 on is held in ca
13173 63 68 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72 che. It is never
13174 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b written to disk
13175 2e 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 . .** This can b
13176 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d e used to implem
13177 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ent an in-memory
13178 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
13179 20 54 68 65 20 6e 45 78 74 72 61 20 70 61 72 61 The nExtra para
1317a 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 73 20 meter specifies
1317b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1317c 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c tes of space all
1317d 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 ocated.** along
1317e 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 72 with each page r
1317f 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73 eference. This s
13180 70 61 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c pace is availabl
13181 65 20 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a e to the user.**
13182 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 via the sqlite3
13183 50 61 67 65 72 47 65 74 45 78 74 72 61 28 29 20 PagerGetExtra()
13184 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 API..**.** The f
13185 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73 lags argument is
13186 20 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 used to specify
13187 20 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 properties that
13188 20 61 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f affect the.** o
13189 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 peration of the
1318a 70 61 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 pager. It should
1318b 20 62 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20 be passed some
1318c 62 69 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 bitwise combinat
1318d 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 ion.** of the PA
1318e 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c GER_OMIT_JOURNAL
1318f 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 and PAGER_NO_RE
13190 41 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a ADLOCK flags..**
13191 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73 .** The vfsFlags
13192 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 parameter is a
13193 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 bitmask to pass
13194 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 to the flags par
13195 61 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 ameter.** of the
13196 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 xOpen() method
13197 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 of the supplied
13198 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 VFS when opening
13199 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 files. .**.** I
1319a 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 f the pager obje
1319b 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ct is allocated
1319c 61 6e 64 20 74 68 65 20 73 70 65 63 69 66 69 65 and the specifie
1319d 64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a d file opened .*
1319e 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 * successfully,
1319f 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
131a0 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 urned and *ppPag
131a1 65 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 er set to point
131a2 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 to.** the new pa
131a3 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 ger object. If a
131a4 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
131a5 2a 70 70 50 61 67 65 72 20 69 73 20 73 65 74 20 *ppPager is set
131a6 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 to NULL.** and e
131a7 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e rror code return
131a8 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f ed. This functio
131a9 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c n may return SQL
131aa 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 ITE_NOMEM.** (sq
131ab 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 lite3Malloc() is
131ac 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 used to allocat
131ad 65 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 e memory), SQLIT
131ae 45 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a E_CANTOPEN or .*
131af 2a 20 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45 * various SQLITE
131b0 5f 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a _IO_XXX errors..
131b1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
131b2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
131b3 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 erOpen(. sqlite
131b4 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
131b5 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
131b6 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f l file system to
131b7 20 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 use */. Pager
131b8 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20 20 **ppPager,
131b9 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 /* OUT: Retur
131ba 6e 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 n the Pager stru
131bb 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 cture here */.
131bc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
131bd 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 ename, /* Name
131be 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
131bf 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f file to open */
131c0 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 . int nExtra,
131c1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
131c2 78 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e xtra bytes appen
131c3 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d d to each in-mem
131c4 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ory page */. in
131c5 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
131c6 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 /* flags
131c7 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 controlling this
131c8 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 file */. int v
131c9 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 20 20 fsFlags,
131ca 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 /* flags pas
131cb 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 sed through to s
131cc 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e qlite3_vfs.xOpen
131cd 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 () */. void (*x
131ce 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 2a 29 Reinit)(DbPage*)
131cf 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 /* Function to
131d0 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 reinitialize pag
131d1 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 es */.){. u8 *p
131d2 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 Ptr;. Pager *pP
131d3 61 67 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 ager = 0;
131d4 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
131d5 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 to allocate and
131d6 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 return */. int
131d7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
131d8 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
131d9 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d ode */. int tem
131da 70 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 pFile = 0;
131db 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65 /* True for te
131dc 6d 70 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 mp files (incl.
131dd 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 in-memory files)
131de 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 */. int memDb
131df 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f = 0; /
131e0 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
131e1 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 s an in-memory f
131e2 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 ile */. int rea
131e3 64 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 dOnly = 0;
131e4 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 /* True if thi
131e5 73 20 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 s is a read-only
131e6 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a file */. int j
131e7 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 ournalFileSize;
131e8 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 /* Bytes to
131e9 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63 allocate for eac
131ea 68 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a h journal fd */.
131eb 20 20 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d char *zPathnam
131ec 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 e = 0; /* Fu
131ed 6c 6c 20 70 61 74 68 20 74 6f 20 64 61 74 61 62 ll path to datab
131ee 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e ase file */. in
131ef 74 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b t nPathname = 0;
131f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
131f1 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61 of bytes in zPa
131f2 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 thname */. int
131f3 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c useJournal = (fl
131f4 61 67 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 ags & PAGER_OMIT
131f5 5f 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a _JOURNAL)==0; /*
131f6 20 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a False to omit j
131f7 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 ournal */. int
131f8 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c noReadlock = (fl
131f9 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 ags & PAGER_NO_R
131fa 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a EADLOCK)!=0; /*
131fb 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 True to omit re
131fc 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 ad-lock */. int
131fd 20 70 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 pcacheSize = sq
131fe 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 lite3PcacheSize(
131ff 29 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 ); /* Byte
13200 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f s to allocate fo
13201 72 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 r PCache */. u1
13202 36 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 6 szPageDflt = S
13203 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 QLITE_DEFAULT_PA
13204 47 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 GE_SIZE; /* Def
13205 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a ault page size *
13206 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f /.. /* Figure o
13207 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 ut how much spac
13208 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f e is required fo
13209 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 r each journal f
1320a 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 ile-handle. **
1320b 28 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f (there are two o
1320c 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e f them, the main
1320d 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 journal and the
1320e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 sub-journal). T
1320f 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 his. ** is the
13210 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 maximum space re
13211 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e quired for an in
13212 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 -memory journal
13213 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a file handle . *
13214 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 * and a regular
13215 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e journal file-han
13216 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 dle. Note that a
13217 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 "regular journa
13218 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d l-handle". ** m
13219 61 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20 ay be a wrapper
1321a 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 capable of cachi
1321b 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 ng the first por
1321c 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 tion of the jour
1321d 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e nal. ** file in
1321e 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 memory to imple
1321f 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d ment the atomic-
13220 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 write optimizati
13221 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f on (see . ** so
13222 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 urce file journa
13223 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 l.c).. */. if(
13224 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
13225 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 ize(pVfs)>sqlite
13226 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
13227 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c ) ){. journal
13228 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 FileSize = ROUND
13229 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 8(sqlite3Journal
1322a 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d Size(pVfs));. }
1322b 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 else{. journa
1322c 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e lFileSize = ROUN
1322d 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 D8(sqlite3MemJou
1322e 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d rnalSize());. }
1322f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f .. /* Set the o
13230 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 utput variable t
13231 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 o NULL in case a
13232 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 n error occurs.
13233 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 */. *ppPager =
13234 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 0;.. /* Compute
13235 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66 and store the f
13236 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 ull pathname in
13237 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 an allocated buf
13238 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a fer pointed. **
13239 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 to by zPathname
1323a 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 , length nPathna
1323b 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 me. Or, if this
1323c 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 is a temporary f
1323d 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 ile,. ** leave
1323e 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 both nPathname a
1323f 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 nd zPathname set
13240 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 to 0.. */. if
13241 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a ( zFilename && z
13242 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 Filename[0] ){.
13243 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 nPathname = p
13244 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
13245 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 1;. zPathname
13246 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
13247 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 (nPathname*2);.
13248 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 if( zPathname
13249 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
1324a 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1324b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1324c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
1324d 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 RYDB. if( str
1324e 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a cmp(zFilename,":
1324f 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a memory:")==0 ){.
13250 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b memDb = 1;
13251 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 . zPathname
13252 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c [0] = 0;. }el
13253 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a se.#endif. {.
13254 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b zPathname[
13255 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 0] = 0; /* Make
13256 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 sure initialized
13257 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 even if FullPat
13258 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f hname() fails */
13259 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1325a 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
1325b 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d e(pVfs, zFilenam
1325c 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 e, nPathname, zP
1325d 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a athname);. }.
1325e 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d . nPathname =
1325f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
13260 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 (zPathname);.
13261 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13262 4f 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b OK && nPathname+
13263 38 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 8>pVfs->mxPathna
13264 6d 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 me ){. /* T
13265 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61 his branch is ta
13266 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 ken when the jou
13267 72 6e 61 6c 20 70 61 74 68 20 72 65 71 75 69 72 rnal path requir
13268 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 ed by. ** t
13269 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e he database bein
1326a 67 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 g opened will be
1326b 20 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d more than pVfs-
1326c 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 >mxPathname.
1326d 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 ** bytes in le
1326e 6e 67 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 ngth. This means
1326f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 the database ca
13270 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a nnot be opened,.
13271 20 20 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 ** as it w
13272 69 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 ill not be possi
13273 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ble to open the
13274 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 journal file or
13275 65 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 even. ** ch
13276 65 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f eck for a hot-jo
13277 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 urnal before rea
13278 64 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ding.. */.
13279 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1327a 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d _CANTOPEN;. }
1327b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1327c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1327d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 sqlite3_free(zPa
1327e 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 thname);. r
1327f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
13280 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 }.. /* Alloca
13281 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 te memory for th
13282 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 e Pager structur
13283 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 e, PCache object
13284 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 , the. ** three
13285 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
13286 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 s, the database
13287 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 file name and th
13288 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 e journal . **
13289 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c file name. The l
1328a 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 ayout in memory
1328b 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 is as follows:.
1328c 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 **. ** Pag
1328d 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 er object
1328e 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73 69 (si
1328f 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 zeof(Pager) byte
13290 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 s). ** PCac
13291 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 he object
13292 20 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c (sql
13293 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 ite3PcacheSize()
13294 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
13295 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 Database file h
13296 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 andle
13297 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 (pVfs->szOsFile
13298 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 bytes). **
13299 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c Sub-journal fil
1329a 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 e handle
1329b 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a (journalFileSiz
1329c 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 e bytes). **
1329d 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 Main journal f
1329e 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 ile handle
1329f 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 (journalFileSi
132a0 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 ze bytes). **
132a1 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 Database file
132a2 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 name
132a3 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 (nPathname+1
132a4 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
132a5 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d Journal file nam
132a6 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
132a7 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 (nPathname+8+1 b
132a8 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 ytes). */. pPt
132a9 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 r = (u8 *)sqlite
132aa 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 3MallocZero(.
132ab 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a ROUND8(sizeof(*
132ac 70 50 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 pPager)) +
132ad 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 /* Pager structu
132ae 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 re */. ROUND8
132af 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 (pcacheSize) +
132b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 /* PCac
132b1 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 he object */.
132b2 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a ROUND8(pVfs->sz
132b3 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 OsFile) +
132b4 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 /* The main db f
132b5 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e ile */. journ
132b6 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b alFileSize * 2 +
132b7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
132b8 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c two journal fil
132b9 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 es */ . nPath
132ba 6e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20 name + 1 +
132bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 /* zFi
132bc 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 lename */. nP
132bd 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 athname + 8 + 1
132be 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
132bf 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b zJournal */. );
132c0 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
132c1 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
132c2 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 SQLITE_INT_TO_PT
132c3 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a R(journalFileSiz
132c4 65 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 e)) );. if( !pP
132c5 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 tr ){. sqlite
132c6 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 3_free(zPathname
132c7 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
132c8 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
132c9 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 pPager =
132ca 20 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 (Pager*)
132cb 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 (pPtr);. pPager
132cc 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 ->pPCache = (
132cd 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d PCache*)(pPtr +=
132ce 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a ROUND8(sizeof(*
132cf 70 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 pPager)));. pPa
132d0 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c ger->fd = (sql
132d1 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 ite3_file*)(pPtr
132d2 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 += ROUND8(pcach
132d3 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 eSize));. pPage
132d4 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 r->sjfd = (sqlit
132d5 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b e3_file*)(pPtr +
132d6 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 = ROUND8(pVfs->s
132d7 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 zOsFile));. pPa
132d8 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c ger->jfd = (sql
132d9 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 ite3_file*)(pPtr
132da 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 += journalFileS
132db 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e ize);. pPager->
132dc 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 zFilename = (
132dd 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a char*)(pPtr += j
132de 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b ournalFileSize);
132df 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
132e0 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
132e1 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
132e2 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 . /* Fill in th
132e3 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d e Pager.zFilenam
132e4 65 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 e and Pager.zJou
132e5 72 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 rnal buffers, if
132e6 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 required. */.
132e7 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b if( zPathname ){
132e8 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f . pPager->zJo
132e9 75 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a urnal = (char*
132ea 29 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e )(pPtr += nPathn
132eb 61 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 ame + 1);. me
132ec 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 mcpy(pPager->zFi
132ed 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d lename, zPathnam
132ee 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 e, nPathname);.
132ef 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 memcpy(pPager
132f0 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 ->zJournal, zPat
132f1 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 hname, nPathname
132f2 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 );. memcpy(&p
132f3 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b Pager->zJournal[
132f4 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f nPathname], "-jo
132f5 75 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 urnal", 8);.
132f6 69 66 28 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c if( pPager->zFil
132f7 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 20 70 50 ename[0]==0 ) pP
132f8 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 ager->zJournal[0
132f9 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 ] = 0;. sqlit
132fa 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d e3_free(zPathnam
132fb 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 e);. }. pPager
132fc 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 ->pVfs = pVfs;.
132fd 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 pPager->vfsFlag
132fe 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20 s = vfsFlags;..
132ff 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 61 67 /* Open the pag
13300 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 er file.. */.
13301 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 if( zFilename &&
13302 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 zFilename[0] &&
13303 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 69 !memDb ){. i
13304 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 nt fout = 0;
13305 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13306 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 72 65 74 /* VFS flags ret
13307 75 72 6e 65 64 20 62 79 20 78 4f 70 65 6e 28 29 urned by xOpen()
13308 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
13309 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c ite3OsOpen(pVfs,
1330a 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
1330b 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 me, pPager->fd,
1330c 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 vfsFlags, &fout)
1330d 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d ;. readOnly =
1330e 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 (fout&SQLITE_OP
1330f 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 EN_READONLY);..
13310 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
13311 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c e was successful
13312 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 ly opened for re
13313 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c ad/write access,
13314 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 . ** choose a
13315 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 default page si
13316 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68 61 ze in case we ha
13317 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 ve to create the
13318 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
13319 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61 75 file. The defau
1331a 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 lt page size is
1331b 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a the maximum of:.
1331c 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
1331d 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c + SQLITE_DEFAUL
1331e 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 T_PAGE_SIZE,.
1331f 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61 6c ** + The val
13320 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ue returned by s
13321 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 qlite3OsSectorSi
13322 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b ze(). ** +
13323 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 61 67 The largest pag
13324 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 6e 20 e size that can
13325 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 be written atomi
13326 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 cally.. */.
13327 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13328 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c 79 _OK && !readOnly
13329 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 65 63 ){. setSec
1332a 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b torSize(pPager);
1332b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 . assert(SQ
1332c 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 LITE_DEFAULT_PAG
1332d 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d E_SIZE<=SQLITE_M
1332e 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
1332f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 66 28 SIZE);. if(
13330 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 61 67 szPageDflt<pPag
13331 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 29 er->sectorSize )
13332 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 {. if( pP
13333 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
13334 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 >SQLITE_MAX_DEFA
13335 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b ULT_PAGE_SIZE ){
13336 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 . szPag
13337 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d eDflt = SQLITE_M
13338 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f AX_DEFAULT_PAGE_
13339 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 SIZE;. }e
1333a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 lse{. s
1333b 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 31 36 zPageDflt = (u16
1333c 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 )pPager->sectorS
1333d 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ize;. }.
1333e 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 }.#ifdef SQ
1333f 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d LITE_ENABLE_ATOM
13340 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20 7b IC_WRITE. {
13341 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 63 . int iDc
13342 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 = sqlite3OsDevi
13343 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
13344 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 s(pPager->fd);.
13345 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 int ii;.
13346 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 assert(SQ
13347 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
13348 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b C512==(512>>8));
13349 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1334a 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
1334b 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e MIC64K==(65536>>
1334c 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 8));. ass
1334d 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ert(SQLITE_MAX_D
1334e 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
1334f 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 <=65536);.
13350 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65 44 for(ii=szPageD
13351 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f flt; ii<=SQLITE_
13352 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 MAX_DEFAULT_PAGE
13353 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b _SIZE; ii=ii*2){
13354 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
13355 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 Dc&(SQLITE_IOCAP
13356 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29 _ATOMIC|(ii>>8))
13357 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
13358 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 69 3b szPageDflt = ii;
13359 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1335a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 }. }.#
1335b 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 endif. }. }e
1335c 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 lse{. /* If a
1335d 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
1335e 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 is requested, it
1335f 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 is not opened i
13360 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 mmediately..
13361 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 ** In this case
13362 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64 65 we accept the de
13363 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 fault page size
13364 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61 6c and delay actual
13365 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e ly. ** openin
13366 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69 6c g the file until
13367 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 the first call
13368 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 to OsWrite()..
13369 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 **. ** This
1336a 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 branch is also
1336b 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 run for an in-me
1336c 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20 41 mory database. A
1336d 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 n in-memory.
1336e 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 74 ** database is t
1336f 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 65 6d he same as a tem
13370 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 20 6e p-file that is n
13371 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f 75 74 ever written out
13372 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 to. ** disk
13373 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d and uses an in-m
13374 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a emory rollback j
13375 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a ournal.. */ .
13376 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 tempFile = 1
13377 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 ;. pPager->st
13378 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c ate = PAGER_EXCL
13379 55 53 49 56 45 3b 0a 20 20 20 20 72 65 61 64 4f USIVE;. readO
1337a 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 67 73 26 nly = (vfsFlags&
1337b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
1337c 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a ONLY);. }.. /*
1337d 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
1337e 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65 74 50 all to PagerSetP
1337f 61 67 65 73 69 7a 65 28 29 20 73 65 72 76 65 73 agesize() serves
13380 20 74 6f 20 73 65 74 20 74 68 65 20 76 61 6c 75 to set the valu
13381 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 67 65 72 e of . ** Pager
13382 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20 74 6f .pageSize and to
13383 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 50 61 allocate the Pa
13384 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 ger.pTmpSpace bu
13385 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ffer.. */. if(
13386 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13387 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
13388 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 ager->memDb==0 )
13389 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1338a 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
1338b 7a 65 28 70 50 61 67 65 72 2c 20 26 73 7a 50 61 ze(pPager, &szPa
1338c 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a 20 20 20 geDflt, -1);.
1338d 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 testcase( rc!=S
1338e 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a QLITE_OK );. }.
1338f 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f . /* If an erro
13390 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69 r occurred in ei
13391 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 ther of the bloc
13392 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74 ks above, free t
13393 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 he . ** Pager s
13394 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f tructure and clo
13395 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a se the file.. *
13396 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 /. if( rc!=SQLI
13397 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 TE_OK ){. ass
13398 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54 ert( !pPager->pT
13399 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73 mpSpace );. s
1339a 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 qlite3OsClose(pP
1339b 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 ager->fd);. s
1339c 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 qlite3_free(pPag
1339d 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 er);. return
1339e 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e rc;. }.. /* In
1339f 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61 itialize the PCa
133a0 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 che object. */.
133a1 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3c assert( nExtra<
133a2 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 74 72 61 1000 );. nExtra
133a3 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 72 61 = ROUND8(nExtra
133a4 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 );. sqlite3Pcac
133a5 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 66 6c heOpen(szPageDfl
133a6 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 6d 44 t, nExtra, !memD
133a7 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b,.
133a8 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f 70 61 !memDb?pa
133a9 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 76 6f gerStress:0, (vo
133aa 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 50 61 id *)pPager, pPa
133ab 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a ger->pPCache);..
133ac 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4f PAGERTRACE(("O
133ad 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 46 49 PEN %d %s\n", FI
133ae 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 67 65 LEHANDLEID(pPage
133af 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 2d 3e r->fd), pPager->
133b0 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 49 zFilename));. I
133b1 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 70 OTRACE(("OPEN %p
133b2 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %s\n", pPager,
133b3 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
133b4 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d 3e 75 e)).. pPager->u
133b5 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 seJournal = (u8)
133b6 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 useJournal;. pP
133b7 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b ager->noReadlock
133b8 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 = (noReadlock &
133b9 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30 & readOnly) ?1:0
133ba 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 ;. /* pPager->s
133bb 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a tmtOpen = 0; */.
133bc 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d /* pPager->stm
133bd 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 tInUse = 0; */.
133be 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 /* pPager->nRef
133bf 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 = 0; */. pPage
133c0 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
133c1 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a (u8)memDb;. /*
133c2 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a pPager->stmtSiz
133c3 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 e = 0; */. /* p
133c4 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 Pager->stmtJSize
133c5 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
133c6 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b ager->nPage = 0;
133c7 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 */. pPager->mx
133c8 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 Pgno = SQLITE_MA
133c9 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 X_PAGE_COUNT;.
133ca 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 /* pPager->state
133cb 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b = PAGER_UNLOCK;
133cc 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
133cd 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 ager->state == (
133ce 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52 tempFile ? PAGER
133cf 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47 _EXCLUSIVE : PAG
133d0 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 ER_UNLOCK) );.
133d1 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 /* pPager->errMa
133d2 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 sk = 0; */. pPa
133d3 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 ger->tempFile =
133d4 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20 (u8)tempFile;.
133d5 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 6c 65 assert( tempFile
133d6 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ==PAGER_LOCKINGM
133d7 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20 ODE_NORMAL .
133d8 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c || tempFil
133d9 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 e==PAGER_LOCKING
133da 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29 MODE_EXCLUSIVE )
133db 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
133dc 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
133dd 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20 CLUSIVE==1 );.
133de 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
133df 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 6d 70 eMode = (u8)temp
133e0 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 72 2d File; . pPager-
133e1 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
133e2 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 = pPager->tempF
133e3 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d ile;. pPager->m
133e4 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d 44 62 emDb = (u8)memDb
133e5 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 61 64 ;. pPager->read
133e6 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 64 4f Only = (u8)readO
133e7 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 nly;. /* pPager
133e8 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 ->needSync = 0;
133e9 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 73 65 */. assert( use
133ea 4a 6f 75 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65 Journal || pPage
133eb 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 r->tempFile );.
133ec 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
133ed 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 = pPager->tempFi
133ee 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75 le;. pPager->fu
133ef 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d llSync = pPager-
133f0 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 >noSync ?0:1;.
133f1 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 pPager->sync_fla
133f2 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs = SQLITE_SYNC
133f3 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 _NORMAL;. /* pP
133f4 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 ager->pFirst = 0
133f5 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
133f6 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20 3d ->pFirstSynced =
133f7 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
133f8 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a er->pLast = 0; *
133f9 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 /. pPager->nExt
133fa 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 72 61 ra = (u16)nExtra
133fb 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ;. pPager->jour
133fc 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 nalSizeLimit = S
133fd 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f QLITE_DEFAULT_JO
133fe 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 URNAL_SIZE_LIMIT
133ff 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
13400 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
13401 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 | tempFile );.
13402 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 setSectorSize(pP
13403 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 75 73 ager);. if( !us
13404 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 eJournal ){.
13405 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
13406 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 ode = PAGER_JOUR
13407 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d NALMODE_OFF;. }
13408 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62 20 29 else if( memDb )
13409 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
1340a 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 urnalMode = PAGE
1340b 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
1340c 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 MORY;. }. /* p
1340d 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 Pager->xBusyHand
1340e 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a ler = 0; */. /*
1340f 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61 pPager->pBusyHa
13410 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f ndlerArg = 0; */
13411 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e . pPager->xRein
13412 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a iter = xReinit;.
13413 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 /* memset(pPag
13414 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 er->aHash, 0, si
13415 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48 61 zeof(pPager->aHa
13416 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61 sh)); */. *ppPa
13417 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 ger = pPager;.
13418 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
13419 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ;.}..../*.** Thi
1341a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1341b 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e 73 lled after trans
1341c 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 itioning from PA
1341d 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a GER_UNLOCK to.**
1341e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 74 PAGER_SHARED st
1341f 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69 66 ate. It tests if
13420 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 there is a hot
13421 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 journal present
13422 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 in.** the file-s
13423 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67 69 ystem for the gi
13424 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f 74 ven pager. A hot
13425 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 journal is one
13426 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 that .** needs t
13427 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b o be played back
13428 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 . According to t
13429 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 his function, a
1342a 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 hot-journal.** f
1342b 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74 68 ile exists if th
1342c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 e following crit
1342d 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a eria are met:.**
1342e 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 .** * The jour
1342f 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 nal file exists
13430 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73 74 in the file syst
13431 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e em, and.** * N
13432 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 o process holds
13433 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 a RESERVED or gr
13434 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 eater lock on th
13435 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
13436 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 and.** * The
13437 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 74 database file it
13438 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72 20 self is greater
13439 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e 20 than 0 bytes in
1343a 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a size, and.** *
1343b 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 The first byte
1343c 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
1343d 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 69 ile exists and i
1343e 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a s not 0x00..**.*
1343f 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 * If the current
13440 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
13441 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30 20 abase file is 0
13442 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 but a journal fi
13443 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 le.** exists, th
13444 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61 at is probably a
13445 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 n old journal le
13446 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 ft over from a p
13447 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 rior.** database
13448 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e with the same n
13449 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ame. In this cas
1344a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
1344b 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 le is.** just de
1344c 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44 65 leted using OsDe
1344d 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20 69 lete, *pExists i
1344e 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 s set to 0 and S
1344f 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 QLITE_OK.** is r
13450 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
13451 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
13452 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74 68 not check if th
13453 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72 20 ere is a master
13454 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
13455 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f .** at the end o
13456 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 74 f the file. If t
13457 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68 61 here is, and tha
13458 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c t master journal
13459 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f file.** does no
1345a 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74 68 t exist, then th
1345b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
1345c 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 s not really hot
1345d 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 . In this.** cas
1345e 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 e this routine w
1345f 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61 6c ill return a fal
13460 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68 65 se-positive. The
13461 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
13462 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c ).** routine wil
13463 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74 20 l discover that
13464 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13465 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 is not really h
13466 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 ot and .** will
13467 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b not roll it back
13468 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f . .**.** If a ho
13469 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 t-journal file i
1346a 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 s found to exist
1346b 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 , *pExists is se
1346c 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 t to 1 and .** S
1346d 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
1346e 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 d. If no hot-jou
1346f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 rnal file is pre
13470 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20 69 sent, *pExists i
13471 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61 6e s.** set to 0 an
13472 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
13473 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
13474 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
13475 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 e trying.** to d
13476 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
13477 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f or not a hot-jo
13478 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 urnal file exist
13479 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 0a s, the IO error.
1347a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 ** code is retur
1347b 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75 ned and the valu
1347c 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69 73 e of *pExists is
1347d 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 undefined..*/.s
1347e 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f 74 tatic int hasHot
1347f 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
13480 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 Pager, int *pExi
13481 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f sts){. sqlite3_
13482 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 vfs * const pVfs
13483 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b = pPager->pVfs;
13484 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
13485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13486 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
13487 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b */. int exists;
13488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13489 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 /* True if a
1348a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
1348b 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 present */.. as
1348c 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 sert( pPager!=0
1348d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
1348e 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
1348f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f );. assert( isO
13490 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
13491 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 );. assert( !is
13492 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
13493 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
13494 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 Pager->state <=
13495 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a PAGER_SHARED );.
13496 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b . *pExists = 0;
13497 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f . rc = sqlite3O
13498 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50 sAccess(pVfs, pP
13499 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
1349a 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
1349b 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a ISTS, &exists);.
1349c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1349d 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b _OK && exists ){
1349e 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b . int locked;
1349f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
134a0 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f 6d 65 /* True if some
134a1 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 process holds a
134a2 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a RESERVED lock *
134a3 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65 20 63 /.. /* Race c
134a4 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a 20 20 ondition here:
134a5 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 Another process
134a6 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 might have been
134a7 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 20 20 holding the.
134a8 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44 20 ** the RESERVED
134a9 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20 61 20 lock and have a
134aa 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20 journal open at
134ab 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41 63 63 the sqlite3OsAcc
134ac 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 ess() . ** ca
134ad 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20 74 68 ll above, but th
134ae 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f en delete the jo
134af 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70 20 74 urnal and drop t
134b0 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20 he lock before.
134b1 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74 6f 20 ** we get to
134b2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 the following sq
134b3 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 lite3OsCheckRese
134b4 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e rvedLock() call.
134b5 20 20 49 66 20 74 68 61 74 0a 20 20 20 20 2a 2a If that. **
134b6 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 is the case, th
134b7 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 is routine might
134b8 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69 73 20 think there is
134b9 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68 a hot journal wh
134ba 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66 61 63 en. ** in fac
134bb 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e t there is none.
134bc 20 20 54 68 69 73 20 72 65 73 75 6c 74 73 20 69 This results i
134bd 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 n a false-positi
134be 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 ve which will.
134bf 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69 ** be dealt wi
134c0 74 68 20 62 79 20 74 68 65 20 70 6c 61 79 62 61 th by the playba
134c1 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54 69 63 ck routine. Tic
134c2 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20 20 2a ket #3883.. *
134c3 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
134c4 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
134c5 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 dLock(pPager->fd
134c6 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20 , &locked);.
134c7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
134c8 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a K && !locked ){.
134c9 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b int nPage;
134ca 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b .. /* Check
134cb 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
134cc 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
134cd 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20 6f If it consists o
134ce 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20 20 f 0 pages,.
134cf 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65 20 ** then delete
134d0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
134d1 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 . See the header
134d2 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66 comment above f
134d3 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 or . ** the
134d4 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e reasoning here.
134d5 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f 62 73 Delete the obs
134d6 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 olete journal fi
134d7 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 20 2a le under. *
134d8 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 * a RESERVED loc
134d9 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 65 20 k to avoid race
134da 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 conditions and t
134db 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e o avoid violatin
134dc 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 33 30 g. ** [H330
134dd 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20].. */.
134de 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
134df 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
134e0 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a Pager, &nPage);.
134e1 20 20 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 29 7b 0a 20 20 20 20 20 LITE_OK ){.
134e3 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 if( nPage==0
134e4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
134e5 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
134e6 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 alloc();.
134e7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 if( sqlite3Os
134e8 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c Lock(pPager->fd,
134e9 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3d RESERVED_LOCK)=
134ea 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
134eb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
134ec 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 3OsDelete(pVfs,
134ed 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
134ee 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
134ef 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 sqlite3OsUnloc
134f0 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 k(pPager->fd, SH
134f1 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 ARED_LOCK);.
134f2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
134f3 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
134f4 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
134f5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
134f6 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 /* The jour
134f7 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 nal file exists
134f8 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e and no other con
134f9 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20 72 65 nection has a re
134fa 73 65 72 76 65 64 0a 20 20 20 20 20 20 20 20 20 served.
134fb 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 6c ** or greater l
134fc 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
134fd 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 ase file. Now ch
134fe 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 eck that there i
134ff 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 s. ** a
13500 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d t least one non-
13501 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 zero bytes at th
13502 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a e start of the j
13503 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 ournal file..
13504 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 ** If the
13505 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65 20 63 re is, then we c
13506 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a 6f 75 onsider this jou
13507 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 rnal to be hot.
13508 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 If not, .
13509 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 ** it can be
1350a 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 ignored..
1350b 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
1350c 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f int f = SQLITE_O
1350d 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c PEN_READONLY|SQL
1350e 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
1350f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 URNAL;.
13510 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
13511 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
13512 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 ->zJournal, pPag
13513 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b er->jfd, f, &f);
13514 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
13515 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13516 20 20 20 20 20 20 20 20 20 20 20 20 75 38 20 66 u8 f
13517 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 irst = 0;.
13518 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
13519 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
1351a 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 >jfd, (void *)&f
1351b 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 irst, 1, 0);.
1351c 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d if( rc=
1351d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 =SQLITE_IOERR_SH
1351e 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 ORT_READ ){.
1351f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
13520 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
13521 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13522 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
13523 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
13524 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 . *pE
13525 78 69 73 74 73 20 3d 20 28 66 69 72 73 74 21 3d xists = (first!=
13526 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 0);. }e
13527 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 lse if( rc==SQLI
13528 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b 0a 20 TE_CANTOPEN ){.
13529 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
1352a 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 we cannot open
1352b 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
1352c 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64 rnal file in ord
1352d 65 72 20 74 6f 20 73 65 65 20 69 66 0a 20 20 20 er to see if.
1352e 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 20 ** its
1352f 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61 64 65 has a zero heade
13530 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 r, that might be
13531 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f 20 65 due to an I/O e
13532 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 20 20 20 rror, or.
13533 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74 ** it might
13534 20 62 65 20 64 75 65 20 74 6f 20 74 68 65 20 72 be due to the r
13535 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 ace condition de
13536 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 61 6e scribed above an
13537 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 d in.
13538 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 38 38 33 ** ticket #3883
13539 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 . Either way, a
1353a 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 6a ssume that the j
1353b 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 0a 20 ournal is hot..
1353c 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 ** Th
1353d 69 73 20 6d 69 67 68 74 20 62 65 20 61 20 66 61 is might be a fa
1353e 6c 73 65 20 70 6f 73 69 74 69 76 65 2e 20 20 42 lse positive. B
1353f 75 74 20 69 66 20 69 74 20 69 73 2c 20 74 68 65 ut if it is, the
13540 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 n the.
13541 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 20 6a ** automatic j
13542 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 20 ournal playback
13543 61 6e 64 20 72 65 63 6f 76 65 72 79 20 6d 65 63 and recovery mec
13544 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 65 61 6c hanism will deal
13545 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
13546 77 69 74 68 20 69 74 20 75 6e 64 65 72 20 61 6e with it under an
13547 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
13548 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 where we do not
13549 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 need to.
1354a 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 73 6f 20 ** worry so
1354b 6d 75 63 68 20 77 69 74 68 20 72 61 63 65 20 63 much with race c
1354c 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 20 20 20 onditions..
1354d 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1354e 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d *pExists =
1354f 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
13550 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
13551 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
13552 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
13553 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 }. }.. retur
13554 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
13555 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ead the content
13556 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f 75 74 for page pPg out
13557 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
13558 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f 20 0a file and into .
13559 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e 20 41 ** pPg->pData. A
1355a 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 72 20 shared lock or
1355b 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 65 20 greater must be
1355c 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 held on the data
1355d 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62 65 66 base.** file bef
1355e 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f ore this functio
1355f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a n is called..**.
13560 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69 73 20 ** If page 1 is
13561 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65 20 76 read, then the v
13562 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
13563 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 73 65 FileVers[] is se
13564 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 t to.** the valu
13565 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 e read from the
13566 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
13567 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 *.** If an IO er
13568 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e ror occurs, then
13569 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 69 73 the IO error is
1356a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
1356b 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 68 65 caller..** Othe
1356c 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b rwise, SQLITE_OK
1356d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
1356e 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 .static int read
1356f 44 62 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 DbPage(PgHdr *pP
13570 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 g){. Pager *pPa
13571 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 ger = pPg->pPage
13572 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 r; /* Pager obje
13573 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ct associated wi
13574 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f 0a 20 th page pPg */.
13575 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 Pgno pgno = pPg
13576 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 2f 2a ->pgno; /*
13577 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 Page number to
13578 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 read */. int rc
13579 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1357a 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
1357b 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 69 code */. i64 i
1357c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
1357d 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 /* Byte
1357e 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65 20 74 offset of file t
1357f 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 0a o read from */..
13580 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13581 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 ->state>=PAGER_S
13582 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 42 20 HARED && !MEMDB
13583 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f );. assert( isO
13584 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
13585 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );.. if( NEVER(
13586 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
13587 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 fd)) ){. asse
13588 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 rt( pPager->temp
13589 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 File );. mems
1358a 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 et(pPg->pData, 0
1358b 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
1358c 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ze);. return
1358d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1358e 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f iOffset = (pgno
1358f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d -1)*(i64)pPager-
13590 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 >pageSize;. rc
13591 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
13592 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d pPager->fd, pPg-
13593 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e >pData, pPager->
13594 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73 65 pageSize, iOffse
13595 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
13596 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
13597 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63 20 _READ ){. rc
13598 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d = SQLITE_OK;. }
13599 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 . if( pgno==1 )
1359a 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c 65 {. u8 *dbFile
1359b 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70 50 Vers = &((u8*)pP
1359c 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20 g->pData)[24];.
1359d 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
1359e 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 r->dbFileVers, d
1359f 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f bFileVers, sizeo
135a0 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
135a1 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f Vers));. }. CO
135a2 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50 67 DEC1(pPager, pPg
135a3 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 ->pData, pgno, 3
135a4 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f , rc = SQLITE_NO
135a5 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 MEM);.. PAGER_I
135a6 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 NCR(sqlite3_page
135a7 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b r_readdb_count);
135a8 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 . PAGER_INCR(pP
135a9 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 ager->nRead);.
135aa 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20 25 IOTRACE(("PGIN %
135ab 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
135ac 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 pgno));. PAGER
135ad 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25 64 TRACE(("FETCH %d
135ae 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 page %d hash(%0
135af 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 8x)\n",.
135b0 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
135b1 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
135b2 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
135b3 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 )));.. return r
135b4 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 c;.}../*.** This
135b5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
135b6 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 led to obtain a
135b7 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 shared lock on t
135b8 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
135b9 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 ..** It is illeg
135ba 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 al to call sqlit
135bb 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 29 e3PagerAcquire()
135bc 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 69 until after thi
135bd 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61 s function.** ha
135be 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 s been successfu
135bf 6c 6c 79 20 63 61 6c 6c 65 64 2e 20 49 66 20 61 lly called. If a
135c0 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 shared-lock is
135c1 61 6c 72 65 61 64 79 20 68 65 6c 64 20 77 68 65 already held whe
135c2 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 n.** this functi
135c3 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 on is called, it
135c4 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
135c5 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
135c6 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 operations are
135c7 61 6c 73 6f 20 70 65 72 66 6f 72 6d 65 64 20 62 also performed b
135c8 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
135c9 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 .**.** 1) If t
135ca 68 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72 he pager is curr
135cb 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52 5f 55 ently in PAGER_U
135cc 4e 4c 4f 43 4b 20 73 74 61 74 65 20 28 6e 6f 20 NLOCK state (no
135cd 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20 lock held.**
135ce 20 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 on the databas
135cf 65 20 66 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e e file), then an
135d0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
135d1 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 to obtain a.**
135d2 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b SHARED lock
135d3 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
135d4 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65 file. Immediate
135d5 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 6e 69 ly after obtaini
135d6 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 53 ng.** the S
135d7 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 HARED lock, the
135d8 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73 20 63 file-system is c
135d9 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68 6f 74 hecked for a hot
135da 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20 -journal,.**
135db 20 20 77 68 69 63 68 20 69 73 20 70 6c 61 79 65 which is playe
135dc 64 20 62 61 63 6b 20 69 66 20 70 72 65 73 65 6e d back if presen
135dd 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 t. Following any
135de 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a hot-journal .**
135df 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20 rollback,
135e0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
135e1 74 68 65 20 63 61 63 68 65 20 61 72 65 20 76 61 the cache are va
135e2 6c 69 64 61 74 65 64 20 62 79 20 63 68 65 63 6b lidated by check
135e3 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 ing.** the
135e4 27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27 'change-counter'
135e5 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 64 61 field of the da
135e6 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 tabase file head
135e7 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64 er and.** d
135e8 69 73 63 61 72 64 65 64 20 69 66 20 74 68 65 79 iscarded if they
135e9 20 61 72 65 20 66 6f 75 6e 64 20 74 6f 20 62 65 are found to be
135ea 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 invalid..**.**
135eb 20 20 32 29 20 49 66 20 74 68 65 20 70 61 67 65 2) If the page
135ec 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 r is running in
135ed 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20 exclusive-mode,
135ee 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 63 75 and there are cu
135ef 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 rrently.**
135f0 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 no outstanding r
135f1 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 eferences to any
135f2 20 70 61 67 65 73 2c 20 61 6e 64 20 69 73 20 69 pages, and is i
135f3 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
135f4 65 2c 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 e,.** then
135f5 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 an attempt is ma
135f6 64 65 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 de to clear the
135f7 65 72 72 6f 72 20 73 74 61 74 65 20 62 79 20 64 error state by d
135f8 69 73 63 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20 iscarding.**
135f9 20 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f the contents o
135fa 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 f the page cache
135fb 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63 and rolling bac
135fc 6b 20 61 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e k any open journ
135fd 61 6c 0a 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e al.** file.
135fe 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 .**.** If the op
135ff 65 72 61 74 69 6f 6e 20 64 65 73 63 72 69 62 65 eration describe
13600 64 20 62 79 20 28 32 29 20 61 62 6f 76 65 20 69 d by (2) above i
13601 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 64 2c s not attempted,
13602 20 61 6e 64 20 69 66 20 74 68 65 0a 2a 2a 20 70 and if the.** p
13603 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 72 ager is in an er
13604 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 ror state other
13605 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c than SQLITE_FULL
13606 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 when this is ca
13607 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 lled,.** the err
13608 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 or state error c
13609 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
1360a 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 It is permitted
1360b 20 74 6f 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 to read the.**
1360c 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 69 6e database when in
1360d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 SQLITE_FULL err
1360e 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 or state..**.**
1360f 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 76 Otherwise, if ev
13610 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 erything is succ
13611 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f essful, SQLITE_O
13612 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
13613 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72 f an.** IO error
13614 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6c 6f occurs while lo
13615 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 cking the databa
13616 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 se, checking for
13617 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a a hot-journal.*
13618 2a 20 66 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e * file or rollin
13619 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c g back a journal
1361a 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20 65 72 file, the IO er
1361b 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
1361c 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
1361d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1361e 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f te3PagerSharedLo
1361f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
13620 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
13621 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
13622 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
13623 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 n code */. int
13624 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 30 isErrorReset = 0
13625 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
13626 2a 20 54 72 75 65 20 69 66 20 72 65 63 6f 76 65 * True if recove
13627 72 69 6e 67 20 66 72 6f 6d 20 65 72 72 6f 72 20 ring from error
13628 73 74 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 state */.. /* T
13629 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f his routine is o
1362a 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 nly called from
1362b 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20 b-tree and only
1362c 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e when there are n
1362d 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 o. ** outstandi
1362e 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20 61 73 ng pages */. as
1362f 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
13630 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
13631 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 er->pPCache)==0
13632 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 4d );. if( NEVER(M
13633 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e EMDB && pPager->
13634 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65 74 75 errCode) ){ retu
13635 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
13636 64 65 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 de; }.. /* If t
13637 68 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20 his database is
13638 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 in an error-stat
13639 65 2c 20 6e 6f 77 20 69 73 20 61 20 63 68 61 6e e, now is a chan
1363a 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a ce to clear. **
1363b 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 73 63 the error. Disc
1363c 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ard the contents
1363d 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 of the pager-ca
1363e 63 68 65 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b che and rollback
1363f 0a 20 20 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f . ** any hot jo
13640 75 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c urnal in the fil
13641 65 2d 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 e-system.. */.
13642 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
13643 43 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20 Code ){. if(
13644 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
13645 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a fd) || pPager->z
13646 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 Journal ){.
13647 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20 isErrorReset =
13648 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 1;. }. pPa
13649 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 ger->errCode = S
1364a 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 QLITE_OK;. pa
1364b 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
1364c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
1364d 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
1364e 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 ER_UNLOCK || isE
1364f 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 rrorReset ){.
13650 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 sqlite3_vfs * c
13651 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 onst pVfs = pPag
13652 65 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e er->pVfs;. in
13653 74 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d t isHotJournal =
13654 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0;. assert(
13655 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 !MEMDB );. as
13656 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 63 61 sert( sqlite3Pca
13657 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
13658 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 er->pPCache)==0
13659 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
1365a 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b r->noReadlock ){
1365b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1365c 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 Pager->readOnly
1365d 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d );. pPager-
1365e 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 >state = PAGER_S
1365f 48 41 52 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65 HARED;. }else
13660 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
13661 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 er_wait_on_lock(
13662 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c pPager, SHARED_L
13663 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
13664 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13665 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
13666 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
13667 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
13668 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 return p
13669 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
1366a 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a r, rc);. }.
1366b 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
1366c 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
1366d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
1366e 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75 . /* If a jou
1366f 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 rnal file exists
13670 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e , and there is n
13671 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 o RESERVED lock
13672 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 on the. ** da
13673 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
13674 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 65 64 n it either need
13675 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 s to be played b
13676 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a ack or deleted..
13677 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
13678 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a isErrorReset ){.
13679 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1367a 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 ager->state <= P
1367b 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 AGER_SHARED );.
1367c 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f 74 rc = hasHot
1367d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 Journal(pPager,
1367e 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a &isHotJournal);.
1367f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
13680 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13681 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
13682 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13683 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 65 73 if( isErrorRes
13684 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e et || isHotJourn
13685 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 al ){. /* G
13686 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 et an EXCLUSIVE
13687 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
13688 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 74 68 base file. At th
13689 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 0a 20 is point it is.
1368a 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e ** importan
1368b 74 20 74 68 61 74 20 61 20 52 45 53 45 52 56 45 t that a RESERVE
1368c 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62 D lock is not ob
1368d 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 77 61 tained on the wa
1368e 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a y to the. *
1368f 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b * EXCLUSIVE lock
13690 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 61 6e . If it were, an
13691 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69 other process mi
13692 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 20 20 ght open the.
13693 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 ** database f
13694 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 65 20 ile, detect the
13695 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61 RESERVED lock, a
13696 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 nd conclude that
13697 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 the. ** da
13698 74 61 62 61 73 65 20 69 73 20 73 61 66 65 20 74 tabase is safe t
13699 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 68 69 o read while thi
1369a 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69 s process is sti
1369b 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a ll rolling the .
1369c 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 ** hot-jou
1369d 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20 rnal back..
1369e 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 ** . ** Be
1369f 63 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 6d cause the interm
136a0 65 64 69 61 74 65 20 52 45 53 45 52 56 45 44 20 ediate RESERVED
136a1 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 lock is not requ
136a2 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20 20 20 ested, any.
136a3 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 73 ** other proces
136a4 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 s attempting to
136a5 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 access the datab
136a6 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 67 65 ase file will ge
136a7 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74 t to . ** t
136a8 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 his point in the
136a9 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74 code and fail t
136aa 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f 77 6e o obtain its own
136ab 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 EXCLUSIVE lock
136ac 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 . ** on the
136ad 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
136ae 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
136af 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
136b0 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 <EXCLUSIVE_LOCK
136b1 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
136b2 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
136b3 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 ager->fd, EXCLUS
136b4 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 IVE_LOCK);.
136b5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
136b6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
136b7 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
136b8 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
136b9 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 goto f
136ba 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d ailed;. }
136bb 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
136bc 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 >state = PAGER_E
136bd 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 XCLUSIVE;.
136be 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 }. . /* Ope
136bf 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f n the journal fo
136c0 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 r read/write acc
136c1 65 73 73 2e 20 54 68 69 73 20 69 73 20 62 65 63 ess. This is bec
136c2 61 75 73 65 20 69 6e 20 0a 20 20 20 20 20 20 2a ause in . *
136c3 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 * exclusive-acce
136c4 73 73 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 ss mode the file
136c5 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c descriptor will
136c6 20 62 65 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e be kept open an
136c7 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 d. ** possi
136c8 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61 20 74 bly used for a t
136c9 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 ransaction later
136ca 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 on. On some sys
136cb 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 20 20 20 tems, the.
136cc 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 ** OsTruncate()
136cd 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63 call used in exc
136ce 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f lusive-access mo
136cf 64 65 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 de also requires
136d0 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64 . ** a read
136d1 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64 /write file hand
136d2 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 le.. */.
136d3 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 if( !isOpen(p
136d4 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
136d5 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a int res;.
136d6 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
136d7 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 ite3OsAccess(pVf
136d8 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e s,pPager->zJourn
136d9 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 53 53 al,SQLITE_ACCESS
136da 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b 0a 20 _EXISTS,&res);.
136db 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
136dc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
136dd 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29 7b if( res ){
136de 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
136df 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20 20 fout = 0;.
136e0 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53 int f = S
136e1 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
136e2 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
136e3 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 _MAIN_JOURNAL;.
136e4 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
136e5 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 t( !pPager->temp
136e6 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 File );.
136e7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
136e8 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 OsOpen(pVfs, pPa
136e9 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 ger->zJournal, p
136ea 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 Pager->jfd, f, &
136eb 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 20 20 fout);.
136ec 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
136ed 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 QLITE_OK || isOp
136ee 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
136ef 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
136f0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
136f1 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 45 5f && fout&SQLITE_
136f2 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b OPEN_READONLY ){
136f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
136f4 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f c = SQLITE_CANTO
136f5 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 PEN;.
136f6 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
136f7 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
136f8 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
136f9 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
136fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
136fb 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 the journal doe
136fc 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69 74 20 s not exist, it
136fd 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68 usually means th
136fe 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20 20 20 at some .
136ff 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f ** other co
13700 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64 nnection managed
13701 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 20 72 to get in and r
13702 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 66 6f oll it back befo
13703 72 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 re .
13704 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 ** this connecti
13705 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 65 20 on obtained the
13706 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 61 exclusive lock a
13707 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a 20 20 bove. Or, it .
13708 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 ** may
13709 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 70 mean that the p
1370a 61 67 65 72 20 77 61 73 20 69 6e 20 74 68 65 20 ager was in the
1370b 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e error-state when
1370c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 this.
1370d 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 ** function wa
1370e 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 65 s called and the
1370f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f journal file do
13710 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 2a es not exist. *
13711 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 /. rc
13712 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
13713 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
13714 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0);. }
13715 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13716 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
13717 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13718 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 goto faile
13719 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 d;. }..
1371a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61 /* TODO: Why a
1371b 72 65 20 74 68 65 73 65 20 63 6c 65 61 72 65 64 re these cleared
1371c 20 68 65 72 65 3f 20 49 73 20 69 74 20 6e 65 63 here? Is it nec
1371d 65 73 73 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20 essary? */.
1371e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1371f 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
13720 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13721 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 alOff = 0;.
13722 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
13723 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 er = 0;. pP
13724 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
13725 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a = 0;. . /*
13726 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20 64 65 Playback and de
13727 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c lete the journal
13728 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61 74 61 . Drop the data
13729 62 61 73 65 20 77 72 69 74 65 0a 20 20 20 20 20 base write.
1372a 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 ** lock and rea
1372b 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
1372c 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68 65 20 lock. Purge the
1372d 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20 20 20 cache before.
1372e 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61 ** playing ba
1372f 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e ck the hot-journ
13730 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f al so that we do
13731 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74 68 0a n't end up with.
13732 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f ** an inco
13733 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65 2e 0a nsistent cache..
13734 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
13735 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 f( isOpen(pPager
13736 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 ->jfd) ){.
13737 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 rc = pager_pla
13738 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 31 29 yback(pPager, 1)
13739 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1373a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1373b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 rc = pa
1373c 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 ger_error(pPager
1373d 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 , rc);.
1373e 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
1373f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13740 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
13741 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 Pager->state==PA
13742 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 GER_SHARED).
13743 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 || (pPage
13744 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
13745 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74 && pPager->stat
13746 65 3e 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a e>PAGER_SHARED).
13747 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a );. }..
13748 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
13749 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74 pBackup || sqlit
1374a 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e e3PcachePagecoun
1374b 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
1374c 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a e)>0 ){. /*
1374d 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b The shared-lock
1374e 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 has just been a
1374f 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 cquired on the d
13750 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 atabase file.
13751 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 ** and there
13752 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 are already page
13753 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 s in the cache (
13754 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a from a previous.
13755 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 ** read or
13756 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
13757 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 on). Check to s
13758 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ee if the databa
13759 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 se. ** has
1375a 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 been modified.
1375b 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
1375c 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 has changed, flu
1375d 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 sh the. **
1375e 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a cache.. **.
1375f 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73 ** Databas
13760 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 74 e changes is det
13761 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 ected by looking
13762 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 67 at 15 bytes beg
13763 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 inning. **
13764 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74 at offset 24 int
13765 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 o the file. The
13766 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 73 first 4 of thes
13767 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a 20 e 16 bytes are.
13768 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 ** a 32-bit
13769 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73 counter that is
1376a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 incremented wit
1376b 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20 h each change.
1376c 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 The. ** oth
1376d 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 er bytes change
1376e 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 randomly with ea
1376f 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 ch file change w
13770 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 hen. ** a c
13771 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a odec is in use..
13772 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 ** .
13773 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61 ** There is a va
13774 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 nishingly small
13775 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 68 chance that a ch
13776 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 ange will not be
13777 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63 . ** detec
13778 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65 ted. The chance
13779 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65 of an undetecte
1377a 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73 d change is so s
1377b 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20 mall that.
1377c 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67 ** it can be neg
1377d 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f lected.. */
1377e 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69 . char dbFi
1377f 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 leVers[sizeof(pP
13780 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
13781 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 )];. sqlite
13782 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
13783 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20 pPager, 0);..
13784 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 if( pPager->e
13785 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 rrCode ){.
13786 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 rc = pPager->e
13787 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 rrCode;.
13788 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
13789 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 }.. asse
1378a 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 rt( pPager->dbSi
1378b 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 zeValid );.
1378c 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
1378d 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ize>0 ){.
1378e 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52 IOTRACE(("CKVER
1378f 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 S %p %d\n", pPag
13790 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c er, sizeof(dbFil
13791 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20 eVers)));.
13792 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13793 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c Read(pPager->fd,
13794 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 &dbFileVers, si
13795 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 zeof(dbFileVers)
13796 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 69 , 24);. i
13797 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13798 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
13799 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
1379a 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1379b 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 {. memset
1379c 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 (dbFileVers, 0,
1379d 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 sizeof(dbFileVer
1379e 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 s));. }..
1379f 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 if( memcmp(p
137a0 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
137a1 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73 s, dbFileVers, s
137a2 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 izeof(dbFileVers
137a3 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ))!=0 ){.
137a4 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
137a5 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ger);. }.
137a6 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
137a7 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
137a8 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d eMode || pPager-
137a9 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 >state==PAGER_SH
137aa 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 61 ARED );. }.. fa
137ab 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21 3d iled:. if( rc!=
137ac 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
137ad 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b /* pager_unlock
137ae 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f () is a no-op fo
137af 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 r exclusive mode
137b0 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 and in-memory d
137b1 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20 atabases. */.
137b2 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 pager_unlock(pP
137b3 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ager);. }. ret
137b4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
137b5 20 49 66 20 74 68 65 20 72 65 66 65 72 65 6e 63 If the referenc
137b6 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 61 63 e count has reac
137b7 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61 hed zero, rollba
137b8 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a ck any active.**
137b9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
137ba 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 unlock the page
137bb 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c r..**.** Except,
137bc 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 in locking_mode
137bd 3d 45 58 43 4c 55 53 49 56 45 20 77 68 65 6e 20 =EXCLUSIVE when
137be 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 there is nothing
137bf 20 74 6f 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f to in.** the ro
137c0 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 llback journal,
137c1 74 68 65 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f the unlock is no
137c2 74 20 70 65 72 66 6f 72 6d 65 64 20 61 6e 64 20 t performed and
137c3 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68 there is.** noth
137c4 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c ing to rollback,
137c5 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 so this routine
137c6 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 is a no-op..*/
137c7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 .static void pag
137c8 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 erUnlockIfUnused
137c9 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
137ca 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 50 . if( (sqlite3P
137cb 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
137cc 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
137cd 30 29 0a 20 20 20 26 26 20 28 21 70 50 61 67 65 0). && (!pPage
137ce 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 r->exclusiveMode
137cf 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
137d0 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a nalOff>0) . ){.
137d1 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 pagerUnlockA
137d2 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 ndRollback(pPage
137d3 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a r);. }.}../*.**
137d4 20 41 63 71 75 69 72 65 20 61 20 72 65 66 65 72 Acquire a refer
137d5 65 6e 63 65 20 74 6f 20 70 61 67 65 20 6e 75 6d ence to page num
137d6 62 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 ber pgno in page
137d7 72 20 70 50 61 67 65 72 20 28 61 20 70 61 67 65 r pPager (a page
137d8 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 68 61 .** reference ha
137d9 73 20 74 79 70 65 20 44 62 50 61 67 65 2a 29 2e s type DbPage*).
137da 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 If the requeste
137db 64 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 0a d reference is .
137dc 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 ** successfully
137dd 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69 73 20 obtained, it is
137de 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 61 67 copied to *ppPag
137df 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 e and SQLITE_OK
137e0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
137e1 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 If the requested
137e2 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 page is already
137e3 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69 in the cache, i
137e4 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a t is returned. .
137e5 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 ** Otherwise, a
137e6 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 new page object
137e7 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
137e8 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 populated with
137e9 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f data.** read fro
137ea 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
137eb 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 ile. In some cas
137ec 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 20 6d es, the pcache m
137ed 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f odule may.** cho
137ee 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 ose not to alloc
137ef 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 6f ate a new page o
137f0 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 72 65 bject and may re
137f1 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a use an existing.
137f2 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 20 6e ** object with n
137f3 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 o outstanding re
137f4 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 ferences..**.**
137f5 54 68 65 20 65 78 74 72 61 20 64 61 74 61 20 61 The extra data a
137f6 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 61 67 ppended to a pag
137f7 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69 74 e is always init
137f8 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 ialized to zeros
137f9 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 the .** first t
137fa 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 6c 6f ime a page is lo
137fb 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 aded into memory
137fc 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 72 65 . If the page re
137fd 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 quested is .** a
137fe 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 lready in the ca
137ff 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 che when this fu
13800 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
13801 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 72 61 , then the extra
13802 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 66 74 .** data is left
13803 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20 as it was when
13804 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 the page object
13805 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a was last used..*
13806 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 *.** If the data
13807 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 73 6d base image is sm
13808 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 72 aller than the r
13809 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6f 72 equested page or
1380a 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 if a .** non-ze
1380b 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 ro value is pass
1380c 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 ed as the noCont
1380d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 61 6e ent parameter an
1380e 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 d the .** reques
1380f 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 ted page is not
13810 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69 already stored i
13811 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 n the cache, the
13812 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 n no .** actual
13813 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 72 73 disk read occurs
13814 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
13815 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 he memory image
13816 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 of the .** page
13817 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
13818 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a o all zeros. .**
13819 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 .** If noContent
1381a 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61 is true, it mea
1381b 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f ns that we do no
1381c 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65 t care about the
1381d 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 contents.** of
1381e 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 20 6f the page. This o
1381f 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 65 70 ccurs in two sep
13820 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f 73 3a erate scenarios:
13821 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e .**.** a) When
13822 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 65 2d reading a free-
13823 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 66 list leaf page f
13824 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
13825 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 , and.**.** b)
13826 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e When a savepoin
13827 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 t is being rolle
13828 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 d back and we ne
13829 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 ed to load.**
1382a 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 69 6e a new page in
1382b 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 to the cache to
1382c 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 74 68 populate with th
1382d 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 e data read.**
1382e 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 from the sav
1382f 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a epoint journal..
13830 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 **.** If noConte
13831 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e nt is true, then
13832 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e the data return
13833 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 ed is zeroed ins
13834 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 tead of.** being
13835 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
13836 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f atabase. Additio
13837 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 nally, the bits
13838 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
13839 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 to pgno in Page
1383a 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 r.pInJournal (bi
1383b 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c tvec of pages al
1383c 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f ready written to
1383d 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
1383e 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 file) and the Pa
1383f 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e gerSavepoint.pIn
13840 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 Savepoint bitvec
13841 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a s of any open.**
13842 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 savepoints are
13843 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 set. This means
13844 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6d if the page is m
13845 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 ade writable at
13846 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 any.** point in
13847 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e the future, usin
13848 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 g a call to sqli
13849 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2c te3PagerWrite(),
1384a 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a its contents.**
1384b 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 will not be jou
1384c 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 rnaled. This sav
1384d 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 es IO..**.** The
1384e 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 acquisition mig
1384f 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 ht fail for seve
13850 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e ral reasons. In
13851 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 all cases,.** a
13852 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 n appropriate er
13853 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
13854 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 rned and *ppPage
13855 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e is set to NULL.
13856 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 .**.** See also
13857 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b sqlite3PagerLook
13858 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 up(). Both this
13859 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f routine and Loo
1385a 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a kup() attempt.**
1385b 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 to find a page
1385c 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 in the in-memory
1385d 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 cache first. I
1385e 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
1385f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 t already.** in
13860 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 memory, this rou
13861 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 tine goes to dis
13862 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 k to read it in
13863 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 whereas Lookup()
13864 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 .** just returns
13865 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 0. This routin
13866 65 20 61 63 71 75 69 72 65 73 20 61 20 72 65 61 e acquires a rea
13867 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 d-lock the first
13868 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 time it.** has
13869 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 to go to disk, a
1386a 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c nd could also pl
1386b 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f ayback an old jo
1386c 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 urnal if necessa
1386d 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f ry..** Since Loo
1386e 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 kup() never goes
1386f 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 to disk, it nev
13870 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 er has to deal w
13871 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 ith locks.** or
13872 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a journal files..*
13873 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13874 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13875 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 rAcquire(. Page
13876 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
13877 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 /* The pager ope
13878 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
13879 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f e file */. Pgno
1387a 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
1387b 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 /* Page number t
1387c 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 o fetch */. DbP
1387d 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 age **ppPage,
1387e 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e /* Write a poin
1387f 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 ter to the page
13880 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f here */. int no
13881 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a Content /*
13882 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 Do not bother r
13883 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 eading content f
13884 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 rom disk if true
13885 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
13886 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a . PgHdr *pPg;..
13887 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 assert( assert
13888 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 _pager_state(pPa
13889 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ger) );. assert
1388a 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
1388b 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
1388c 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 . if( pgno==0 )
1388d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
1388e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1388f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
13890 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 he pager is in t
13891 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
13892 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
13893 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 immediately. .
13894 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 ** Otherwise, re
13895 71 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66 quest the page f
13896 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c rom the PCache l
13897 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 ayer. */. if( p
13898 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d Pager->errCode!=
13899 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 SQLITE_OK && pPa
1389a 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 ger->errCode!=SQ
1389b 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 LITE_FULL ){.
1389c 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 rc = pPager->er
1389d 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a rCode;. }else{.
1389e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1389f 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
138a0 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
138a1 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 o, 1, ppPage);.
138a2 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 }.. if( rc!=SQ
138a3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f LITE_OK ){. /
138a4 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c * Either the cal
138a5 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63 l to sqlite3Pcac
138a6 68 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e heFetch() return
138a7 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 ed an error or t
138a8 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 he. ** pager
138a9 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 was already in t
138aa 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 he error-state w
138ab 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
138ac 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 n was called..
138ad 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20 ** Set pPg to
138ae 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 0 and jump to th
138af 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 e exception hand
138b0 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 ler. */. pPg
138b1 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 = 0;. goto p
138b2 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 ager_acquire_err
138b3 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
138b4 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d (*ppPage)->pgno=
138b5 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 =pgno );. asser
138b6 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 t( (*ppPage)->pP
138b7 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 ager==pPager ||
138b8 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 (*ppPage)->pPage
138b9 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 r==0 );.. if( (
138ba 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 *ppPage)->pPager
138bb 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 ){. /* In th
138bc 69 73 20 63 61 73 65 20 74 68 65 20 70 63 61 63 is case the pcac
138bd 68 65 20 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 he already conta
138be 69 6e 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a ins an initializ
138bf 65 64 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a ed copy of. *
138c0 2a 20 74 68 65 20 70 61 67 65 2e 20 52 65 74 75 * the page. Retu
138c1 72 6e 20 77 69 74 68 6f 75 74 20 66 75 72 74 68 rn without furth
138c2 65 72 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 er ado. */.
138c3 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 assert( pgno<=PA
138c4 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 GER_MAX_PGNO &&
138c5 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 pgno!=PAGER_MJ_P
138c6 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 GNO(pPager) );.
138c7 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 PAGER_INCR(pP
138c8 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 ager->nHit);.
138c9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
138ca 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 K;.. }else{.
138cb 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 /* The pager ca
138cc 63 68 65 20 68 61 73 20 63 72 65 61 74 65 64 20 che has created
138cd 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 73 20 a new page. Its
138ce 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 74 6f content needs to
138cf 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 . ** be init
138d0 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20 ialized. */.
138d1 20 69 6e 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20 int nMax;..
138d2 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
138d3 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70 r->nMiss);. p
138d4 50 67 20 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 Pg = *ppPage;.
138d5 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 pPg->pPager =
138d6 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 pPager;.. /*
138d7 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 The maximum page
138d8 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31 2e number is 2^31.
138d9 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 Return SQLITE_C
138da 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67 65 ORRUPT if a page
138db 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 . ** number g
138dc 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 reater than this
138dd 2c 20 6f 72 20 74 68 65 20 75 6e 75 73 65 64 20 , or the unused
138de 6c 6f 63 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73 locking-page, is
138df 20 72 65 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20 requested. */.
138e0 20 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 if( pgno>PAGE
138e1 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 R_MAX_PGNO || pg
138e2 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e no==PAGER_MJ_PGN
138e3 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 O(pPager) ){.
138e4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
138e5 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
138e6 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 goto pager_ac
138e7 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d quire_err;. }
138e8 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 .. rc = sqlit
138e9 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
138ea 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b (pPager, &nMax);
138eb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
138ec 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
138ed 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 goto pager_acqui
138ee 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 re_err;. }..
138ef 20 20 20 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20 if( MEMDB ||
138f0 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c nMax<(int)pgno |
138f1 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 | noContent ){.
138f2 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 if( pgno>pP
138f3 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a ager->mxPgno ){.
138f4 09 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c .rc = SQLITE_FUL
138f5 4c 3b 0a 09 67 6f 74 6f 20 70 61 67 65 72 5f 61 L;..goto pager_a
138f6 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 cquire_err;.
138f7 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e 6f }. if( no
138f8 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 Content ){.
138f9 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f /* Failure to
138fa 20 73 65 74 20 74 68 65 20 62 69 74 73 20 69 6e set the bits in
138fb 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 the InJournal b
138fc 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 62 65 it-vectors is be
138fd 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a nign.. **
138fe 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 It merely means
138ff 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 20 64 that we might d
13900 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 o some extra wor
13901 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a k to journal a .
13902 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 ** page
13903 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 that does not ne
13904 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c ed to be journal
13905 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 ed. Nevertheles
13906 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 20 20 s, be sure .
13907 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 ** to test t
13908 68 65 20 63 61 73 65 20 77 68 65 72 65 20 61 20 he case where a
13909 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 malloc error occ
1390a 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 urs while trying
1390b 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 20 20 to set .
1390c 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 20 62 ** a bit in a b
1390d 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 it vector..
1390e 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 */. sq
1390f 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
13910 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
13911 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 if( pgno<=pPag
13912 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 er->dbOrigSize )
13913 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 53 54 {. TEST
13914 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c ONLY( rc = ) sql
13915 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 ite3BitvecSet(pP
13916 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
13917 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 , pgno);.
13918 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d testcase( rc=
13919 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
1391a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1391b 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 TESTONLY( rc
1391c 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 6f 69 = ) addToSavepoi
1391d 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72 ntBitvecs(pPager
1391e 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 , pgno);.
1391f 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
13920 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
13921 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e sqlite3En
13922 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
13923 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
13924 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 memset(pPg
13925 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 ->pData, 0, pPag
13926 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 er->pageSize);.
13927 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 }. IOT
13928 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 RACE(("ZERO %p %
13929 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 d\n", pPager, pg
1392a 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b no));. }else{
1392b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1392c 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 Pg->pPager==pPag
1392d 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d er );. rc =
1392e 20 72 65 61 64 44 62 50 61 67 65 28 70 50 67 29 readDbPage(pPg)
1392f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
13930 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13931 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f goto pager_
13932 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 acquire_err;.
13933 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 }. }.#ifde
13934 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 f SQLITE_CHECK_P
13935 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 AGES. pPg->pa
13936 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
13937 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65 agehash(pPg);.#e
13938 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ndif. }.. retu
13939 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 rn SQLITE_OK;..p
1393a 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 ager_acquire_err
1393b 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d :. assert( rc!=
1393c 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
1393d 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73 71 f( pPg ){. sq
1393e 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 lite3PcacheDrop(
1393f 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 pPg);. }. page
13940 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 rUnlockIfUnused(
13941 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50 pPager);.. *ppP
13942 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 age = 0;. retur
13943 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 n rc;.}../*.** A
13944 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69 66 cquire a page if
13945 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 it is already i
13946 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 n the in-memory
13947 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f cache. Do.** no
13948 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65 20 t read the page
13949 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 from disk. Retu
1394a 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1394b 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 the page,.** or
1394c 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 0 if the page is
1394d 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41 not in cache. A
1394e 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66 lso, return 0 if
1394f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 the .** pager i
13950 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 s in PAGER_UNLOC
13951 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 K state when thi
13952 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
13953 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 lled,.** or if t
13954 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 he pager is in a
13955 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 n error state ot
13956 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f her than SQLITE_
13957 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 FULL..**.** See
13958 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65 also sqlite3Page
13959 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 69 66 rGet(). The dif
1395a 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 ference between
1395b 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
1395c 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 and sqlite3Pager
1395d 47 65 74 28 29 20 69 73 20 74 68 61 74 20 5f 67 Get() is that _g
1395e 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 et() will go to
1395f 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61 the disk and rea
13960 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 d.** in the page
13961 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
13962 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63 not already in c
13963 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74 ache. This rout
13964 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e ine.** returns N
13965 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 65 20 ULL if the page
13966 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 is not in cache
13967 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f or if a disk I/O
13968 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 error .** has e
13969 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f ver happened..*/
1396a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1396b 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50 DbPage *sqlite3P
1396c 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 agerLookup(Pager
1396d 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 *pPager, Pgno p
1396e 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 gno){. PgHdr *p
1396f 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 Pg = 0;. assert
13970 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 ( pPager!=0 );.
13971 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 assert( pgno!=0
13972 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
13973 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21 3d 30 ager->pPCache!=0
13974 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
13975 61 67 65 72 2d 3e 73 74 61 74 65 20 3e 20 50 41 ager->state > PA
13976 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 GER_UNLOCK );.
13977 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 sqlite3PcacheFet
13978 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ch(pPager->pPCac
13979 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 he, pgno, 0, &pP
1397a 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 g);. return pPg
1397b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
1397c 73 65 20 61 20 70 61 67 65 20 72 65 66 65 72 65 se a page refere
1397d 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 nce..**.** If th
1397e 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
1397f 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
13980 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2c ge drop to zero,
13981 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67 then the.** pag
13982 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 e is added to th
13983 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57 68 65 e LRU list. Whe
13984 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 65 73 n all references
13985 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a 2a 2a to all pages.**
13986 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c 20 61 are released, a
13987 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
13988 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e and the lock on
13989 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
1398a 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a .** removed..*/.
1398b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1398c 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
1398d 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a 70 50 Unref(DbPage *pP
1398e 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 29 7b g){. if( pPg ){
1398f 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 . Pager *pPag
13990 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
13991 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 ;. sqlite3Pca
13992 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b cheRelease(pPg);
13993 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b . pagerUnlock
13994 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29 IfUnused(pPager)
13995 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
13996 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
13997 61 6c 20 66 69 6c 65 20 68 61 73 20 61 6c 72 65 al file has alre
13998 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c ady been opened,
13999 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
1399a 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 .** sub-journal
1399b 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f file is open too
1399c 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f . If the main jo
1399d 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65 urnal is not ope
1399e 6e 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 n,.** this funct
1399f 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
139a0 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 **.** SQLITE_OK
139a1 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 is returned if e
139a2 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 verything goes a
139a3 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e ccording to plan
139a4 2e 20 0a 2a 2a 20 41 6e 20 53 51 4c 49 54 45 5f . .** An SQLITE_
139a5 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 IOERR_XXX error
139a6 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
139a7 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 0a 2a if a call to .*
139a8 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 * sqlite3OsOpen(
139a9 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 ) fails..*/.stat
139aa 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f ic int openSubJo
139ab 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
139ac 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
139ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
139ae 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
139af 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e >jfd) && !isOpen
139b0 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 (pPager->sjfd) )
139b1 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 {. if( pPager
139b2 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
139b3 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
139b4 5f 4d 45 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65 _MEMORY || pPage
139b5 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 r->subjInMemory
139b6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
139b7 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 MemJournalOpen(p
139b8 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 Pager->sjfd);.
139b9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
139ba 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 65 6d c = pagerOpentem
139bb 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 p(pPager, pPager
139bc 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f ->sjfd, SQLITE_O
139bd 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b PEN_SUBJOURNAL);
139be 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
139bf 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
139c0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
139c1 73 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20 s called at the
139c2 73 74 61 72 74 20 6f 66 20 65 76 65 72 79 20 77 start of every w
139c3 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
139c4 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 ..** There must
139c5 61 6c 72 65 61 64 79 20 62 65 20 61 20 52 45 53 already be a RES
139c6 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 ERVED or EXCLUSI
139c7 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 VE lock on the d
139c8 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 atabase .** file
139c9 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
139ca 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
139cb 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 .** Open the jou
139cc 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 rnal file for pa
139cd 67 65 72 20 70 50 61 67 65 72 20 61 6e 64 20 77 ger pPager and w
139ce 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68 rite a journal h
139cf 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 eader.** to the
139d0 73 74 61 72 74 20 6f 66 20 69 74 2e 20 49 66 20 start of it. If
139d1 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 there are active
139d2 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65 savepoints, ope
139d3 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 n the sub-journa
139d4 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68 l.** as well. Th
139d5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
139d6 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 74 68 nly used when th
139d7 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
139d8 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e s being .** open
139d9 65 64 20 74 6f 20 77 72 69 74 65 20 61 20 72 6f ed to write a ro
139da 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61 llback log for a
139db 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 transaction. It
139dc 20 69 73 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a is not used .**
139dd 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 when opening a
139de 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot journal file
139df 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b to roll it back
139e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a ..**.** If the j
139e1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 ournal file is a
139e2 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 61 73 20 lready open (as
139e3 69 74 20 6d 61 79 20 62 65 20 69 6e 20 65 78 63 it may be in exc
139e4 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a lusive mode),.**
139e5 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
139e6 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 65 73 20 ion just writes
139e7 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 a journal header
139e8 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
139e9 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 the.** already
139ea 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a open file. .**.*
139eb 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 * Whether or not
139ec 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
139ed 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79 20 74 e is opened by t
139ee 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 his function, th
139ef 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f e.** Pager.pInJo
139f0 75 72 6e 61 6c 20 62 69 74 76 65 63 20 73 74 72 urnal bitvec str
139f1 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 ucture is alloca
139f2 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ted..**.** Retur
139f3 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 n SQLITE_OK if e
139f4 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
139f5 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72 77 69 cessful. Otherwi
139f6 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 se, return .** S
139f7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74 QLITE_NOMEM if t
139f8 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c he attempt to al
139f9 6c 6f 63 61 74 65 20 50 61 67 65 72 2e 70 49 6e locate Pager.pIn
139fa 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f Journal fails, o
139fb 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f r .** an IO erro
139fc 72 20 63 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e r code if openin
139fd 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 g or writing the
139fe 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 journal file fa
139ff 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ils..*/.static i
13a00 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f nt pager_open_jo
13a01 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 urnal(Pager *pPa
13a02 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
13a03 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
13a04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13a05 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
13a06 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 e */. sqlite3_v
13a07 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 fs * const pVfs
13a08 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 20 = pPager->pVfs;
13a09 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 /* Local cache
13a0a 20 6f 66 20 76 66 73 20 70 6f 69 6e 74 65 72 20 of vfs pointer
13a0b 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 */.. assert( pP
13a0c 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
13a0d 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 ER_RESERVED );.
13a0e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13a0f 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
13a10 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13a11 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
13a12 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13a13 4f 46 46 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OFF );. assert(
13a14 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
13a15 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f nal==0 );. . /
13a16 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20 * If already in
13a17 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c the error state,
13a18 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13a19 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 s a no-op. But
13a1a 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 on. ** the othe
13a1b 72 20 68 61 6e 64 2c 20 74 68 69 73 20 72 6f 75 r hand, this rou
13a1c 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 tine is never ca
13a1d 6c 6c 65 64 20 69 66 20 77 65 20 61 72 65 20 61 lled if we are a
13a1e 6c 72 65 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61 lready in. ** a
13a1f 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a n error state. *
13a20 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
13a21 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 ager->errCode) )
13a22 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
13a23 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 errCode;.. /* T
13a24 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c ODO: Is it reall
13a25 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 y possible to ge
13a26 74 20 68 65 72 65 20 77 69 74 68 20 64 62 53 69 t here with dbSi
13a27 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e zeValid==0? If n
13a28 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c ot,. ** the cal
13a29 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f l to PagerPageco
13a2a 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d unt() can be rem
13a2b 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 oved.. */. tes
13a2c 74 63 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64 tcase( pPager->d
13a2d 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b bSizeValid==0 );
13a2e 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 . sqlite3PagerP
13a2f 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
13a30 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 0);.. pPager->
13a31 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c pInJournal = sql
13a32 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
13a33 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
13a34 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
13a35 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b pInJournal==0 ){
13a36 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
13a37 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 TE_NOMEM;. }..
13a38 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 /* Open the jou
13a39 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 rnal file if it
13a3a 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f is not already o
13a3b 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 pen. */. if( !i
13a3c 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13a3d 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 d) ){. if( pP
13a3e 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
13a3f 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
13a40 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
13a41 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a sqlite3MemJ
13a42 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 ournalOpen(pPage
13a43 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c r->jfd);. }el
13a44 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 se{. const
13a45 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20 int flags =
13a46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13a47 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 VFS flags to op
13a48 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 en journal file
13a49 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 */. SQLIT
13a4a 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
13a4b 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 |SQLITE_OPEN_CRE
13a4c 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50 ATE|. (pP
13a4d 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f ager->tempFile ?
13a4e 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c . (SQL
13a4f 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
13a50 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 NCLOSE|SQLITE_OP
13a51 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 EN_TEMP_JOURNAL)
13a52 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c :. (SQL
13a53 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
13a54 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29 URNAL). )
13a55 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
13a56 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13a57 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ITE. rc = s
13a58 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 qlite3JournalOpe
13a59 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 56 66 n(. pVf
13a5a 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
13a5b 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
13a5c 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 , flags, jrnlBuf
13a5d 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a ferSize(pPager).
13a5e 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 );.#else.
13a5f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13a60 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
13a61 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 ager->zJournal,
13a62 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 pPager->jfd, fla
13a63 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 gs, 0);.#endif.
13a64 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
13a65 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc!=SQLITE_OK |
13a66 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d | isOpen(pPager-
13a67 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 >jfd) );. }...
13a68 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 /* Write the fi
13a69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
13a6a 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 er to the journa
13a6b 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 l file and open
13a6c 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f . ** the sub-jo
13a6d 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 urnal if necessa
13a6e 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 ry.. */. if( r
13a6f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13a70 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 /* TODO: Che
13a71 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 ck if all of the
13a72 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 se are really re
13a73 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 quired. */. p
13a74 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
13a75 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 e = pPager->dbSi
13a76 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ze;. pPager->
13a77 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
13a78 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13a79 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
13a7a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
13a7b 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13a7c 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
13a7d 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d pPager->setM
13a7e 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 aster = 0;. p
13a7f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
13a80 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 r = 0;. rc =
13a81 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
13a82 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 pPager);. }. i
13a83 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13a84 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 && pPager->nSav
13a85 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 epoint ){. rc
13a86 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 = openSubJourna
13a87 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a l(pPager);. }..
13a88 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13a89 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
13a8a 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
13a8b 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
13a8c 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d al);. pPager-
13a8d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b >pInJournal = 0;
13a8e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
13a8f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e ;.}../*.** Begin
13a90 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
13a91 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 tion on the spec
13a92 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65 ified pager obje
13a93 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 ct. If a .** wri
13a94 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 te-transaction h
13a95 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
13a96 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e opened, this fun
13a97 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
13a98 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 ..**.** If the e
13a99 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 xFlag argument i
13a9a 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 s false, then ac
13a9b 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 61 quire at least a
13a9c 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 RESERVED.** loc
13a9d 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
13a9e 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 e file. If exFla
13a9f 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
13aa0 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 acquire at least
13aa1 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 .** an EXCLUSIVE
13aa2 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61 lock. If such a
13aa3 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 lock is already
13aa4 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e held, no lockin
13aa5 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 g .** functions
13aa6 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a need be called..
13aa7 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 **.** If this is
13aa8 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 not a temporary
13aa9 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 or in-memory fi
13aaa 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 le and, the jour
13aab 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 nal file is .**
13aac 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 61 73 opened if it has
13aad 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64 not been alread
13aae 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 y. For a tempora
13aaf 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 ry file, the ope
13ab0 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 ning .** of the
13ab1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 journal file is
13ab2 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 deferred until t
13ab3 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 75 61 here is an actua
13ab4 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 l need to .** wr
13ab5 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ite to the journ
13ab6 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 al. TODO: Why ha
13ab7 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 ndle temporary f
13ab8 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 iles differently
13ab9 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a ?.**.** If the j
13aba 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f ournal file is o
13abb 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20 pened (or if it
13abc 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 is already open)
13abd 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 , then a.** jour
13abe 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 nal-header is wr
13abf 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61 itten to the sta
13ac0 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 rt of it..**.**
13ac1 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d If the subjInMem
13ac2 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ory argument is
13ac3 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 non-zero, then a
13ac4 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f ny sub-journal o
13ac5 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 pened.** within
13ac6 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
13ac7 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 will be opened
13ac8 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 as an in-memory
13ac9 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 file. This.** ha
13aca 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 s no effect if t
13acb 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
13acc 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 s already opened
13acd 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 (as it may be w
13ace 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 hen.** running i
13acf 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
13ad0 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e ) or if the tran
13ad1 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 saction does not
13ad2 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 require a.** su
13ad3 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 b-journal. If th
13ad4 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 e subjInMemory a
13ad5 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c rgument is zero,
13ad6 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 then any requir
13ad7 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 ed.** sub-journa
13ad8 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 l is implemented
13ad9 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 in-memory if pP
13ada 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 ager is an in-me
13adb 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a mory database, .
13adc 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 ** or using a te
13add 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 mporary file oth
13ade 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
13adf 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13ae0 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 lite3PagerBegin(
13ae1 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
13ae2 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 nt exFlag, int s
13ae3 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 ubjInMemory){.
13ae4 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
13ae5 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 OK;. assert( pP
13ae6 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 ager->state!=PAG
13ae7 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 ER_UNLOCK );. p
13ae8 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d Pager->subjInMem
13ae9 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e ory = (u8)subjIn
13aea 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 Memory;. if( pP
13aeb 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
13aec 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 ER_SHARED ){.
13aed 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13aee 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 >pInJournal==0 )
13aef 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d ;. assert( !M
13af0 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 72 2d EMDB && !pPager-
13af1 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 >tempFile );..
13af2 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 /* Obtain a RE
13af3 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 SERVED lock on t
13af4 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13af5 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 . If the exFlag
13af6 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a parameter. **
13af7 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 is true, then i
13af8 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 mmediately upgra
13af9 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 de this to an EX
13afa 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 CLUSIVE lock. Th
13afb 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 e. ** busy-ha
13afc 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 ndler callback c
13afd 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 an be used when
13afe 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 upgrading to the
13aff 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a EXCLUSIVE. *
13b00 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 * lock, but not
13b01 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 when obtaining t
13b02 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b he RESERVED lock
13b03 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
13b04 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 = sqlite3OsLock(
13b05 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 pPager->fd, RESE
13b06 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 RVED_LOCK);.
13b07 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
13b08 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
13b09 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
13b0a 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 _RESERVED;.
13b0b 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 if( exFlag ){.
13b0c 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
13b0d 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 r_wait_on_lock(p
13b0e 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 Pager, EXCLUSIVE
13b0f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a _LOCK);. }.
13b10 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
13b11 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f the required lo
13b12 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 73 73 cks were success
13b13 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 fully obtained,
13b14 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c open the journal
13b15 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 . ** file and
13b16 20 77 72 69 74 65 20 74 68 65 20 66 69 72 73 74 write the first
13b17 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
13b18 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
13b19 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13b1a 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a _OK && pPager->j
13b1b 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 ournalMode!=PAGE
13b1c 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
13b1d 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 F ){. rc =
13b1e 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
13b1f 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
13b20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 }. }else if( is
13b21 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
13b22 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ) && pPager->jou
13b23 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 rnalOff==0 ){.
13b24 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e /* This happen
13b25 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 s when the pager
13b26 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 was in exclusiv
13b27 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 e-access mode th
13b28 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 e last. ** ti
13b29 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 me a (read or wr
13b2a 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e ite) transaction
13b2b 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c was successfull
13b2c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 y concluded.
13b2d 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 ** by this conne
13b2e 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f ction. Instead o
13b2f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a f deleting the j
13b30 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 ournal file it w
13b31 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 as . ** kept
13b32 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 open and either
13b33 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f was truncated to
13b34 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 0 bytes or its
13b35 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a header was. *
13b36 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 * overwritten wi
13b37 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f th zeros.. */
13b38 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13b39 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a ger->nRec==0 );.
13b3a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
13b3b 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d er->dbOrigSize==
13b3c 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
13b3d 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
13b3e 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 nal==0 );. rc
13b3f 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f = pager_open_jo
13b40 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
13b41 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 }.. PAGERTRACE
13b42 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 (("TRANSACTION %
13b43 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
13b44 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 ager)));. asser
13b45 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 t( !isOpen(pPage
13b46 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 r->jfd) || pPage
13b47 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 r->journalOff>0
13b48 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b || rc!=SQLITE_OK
13b49 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 );. if( rc!=SQ
13b4a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 LITE_OK ){. a
13b4b 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
13b4c 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 dbModified );.
13b4d 20 20 2f 2a 20 49 67 6e 6f 72 65 20 61 6e 79 20 /* Ignore any
13b4e 49 4f 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 IO error that oc
13b4f 63 75 72 73 20 77 69 74 68 69 6e 20 70 61 67 65 curs within page
13b50 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
13b51 6e 28 29 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 n(). The. **
13b52 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 purpose of this
13b53 63 61 6c 6c 20 69 73 20 74 6f 20 72 65 73 65 74 call is to reset
13b54 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 the internal st
13b55 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 ate of the pager
13b56 0a 20 20 20 20 2a 2a 20 73 75 62 2d 73 79 73 74 . ** sub-syst
13b57 65 6d 2e 20 49 74 20 64 6f 65 73 6e 27 74 20 6d em. It doesn't m
13b58 61 74 74 65 72 20 69 66 20 74 68 65 20 6a 6f 75 atter if the jou
13b59 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 rnal-file is not
13b5a 20 70 72 6f 70 65 72 6c 79 0a 20 20 20 20 2a 2a properly. **
13b5b 20 66 69 6e 61 6c 69 7a 65 64 20 61 74 20 74 68 finalized at th
13b5c 69 73 20 70 6f 69 6e 74 20 28 73 69 6e 63 65 20 is point (since
13b5d 69 74 20 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 it is not a vali
13b5e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 d journal file a
13b5f 6e 79 77 61 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 nyway).. */.
13b60 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 pager_end_tra
13b61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
13b62 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 0);. }. retur
13b63 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
13b64 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64 61 74 ark a single dat
13b65 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61 a page as writea
13b66 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 ble. The page is
13b67 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
13b68 65 20 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e e .** main journ
13b69 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 al or sub-journa
13b6a 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e 20 49 l as required. I
13b6b 66 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 f the page is wr
13b6c 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e itten into.** on
13b6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
13b6e 73 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e s, the correspon
13b6f 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 20 ding bit is set
13b70 69 6e 20 74 68 65 20 0a 2a 2a 20 50 61 67 65 72 in the .** Pager
13b71 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 .pInJournal bitv
13b72 65 63 20 61 6e 64 20 74 68 65 20 50 61 67 65 72 ec and the Pager
13b73 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 Savepoint.pInSav
13b74 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 0a 2a epoint bitvecs.*
13b75 2a 20 6f 66 20 61 6e 79 20 6f 70 65 6e 20 73 61 * of any open sa
13b76 76 65 70 6f 69 6e 74 73 20 61 73 20 61 70 70 72 vepoints as appr
13b77 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 opriate..*/.stat
13b78 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 ic int pager_wri
13b79 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a te(PgHdr *pPg){.
13b7a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 void *pData =
13b7b 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61 pPg->pData;. Pa
13b7c 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
13b7d 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 g->pPager;. int
13b7e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13b7f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 .. /* This rout
13b80 69 6e 65 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 ine is not calle
13b81 64 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e 73 d unless a trans
13b82 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 action has alrea
13b83 64 79 20 62 65 65 6e 0a 20 20 2a 2a 20 73 74 61 dy been. ** sta
13b84 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 rted.. */. ass
13b85 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
13b86 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 te>=PAGER_RESERV
13b87 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 ED );.. /* If a
13b88 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e n error has been
13b89 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74 65 previously dete
13b8a 63 74 65 64 2c 20 77 65 20 73 68 6f 75 6c 64 20 cted, we should
13b8b 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 63 61 6c 6c not be. ** call
13b8c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
13b8d 2e 20 20 52 65 70 65 61 74 20 74 68 65 20 65 72 . Repeat the er
13b8e 72 6f 72 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 ror for robustne
13b8f 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e ss.. */. if( N
13b90 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 EVER(pPager->err
13b91 43 6f 64 65 29 20 29 20 20 72 65 74 75 72 6e 20 Code) ) return
13b92 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
13b93 0a 0a 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 .. /* Higher-le
13b94 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 vel routines nev
13b95 65 72 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e er call this fun
13b96 63 74 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73 ction if databas
13b97 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 e is not. ** wr
13b98 69 74 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65 itable. But che
13b99 63 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 ck anyway, just
13b9a 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 for robustness.
13b9b 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 */. if( NEVER(p
13b9c 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 Pager->readOnly)
13b9d 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
13b9e 5f 50 45 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74 _PERM;.. assert
13b9f 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 ( !pPager->setMa
13ba0 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b ster );.. CHECK
13ba1 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f _PAGE(pPg);.. /
13ba2 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 * Mark the page
13ba3 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 as dirty. If th
13ba4 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 e page has alrea
13ba5 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a dy been written.
13ba6 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 ** to the jour
13ba7 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 nal then we can
13ba8 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 return right awa
13ba9 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 y.. */. sqlite
13baa 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
13bab 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 (pPg);. if( pag
13bac 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
13bad 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 && !subjRequires
13bae 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 Page(pPg) ){.
13baf 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 pPager->dbModif
13bb0 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 ied = 1;. }else
13bb1 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 {.. /* If we
13bb2 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 get this far, it
13bb3 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
13bb4 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 page needs to be
13bb5 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 . ** written
13bb6 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 to the transacti
13bb7 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 on journal or th
13bb8 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 e ckeckpoint jou
13bb9 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 rnal. ** or b
13bba 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 oth.. **.
13bbb 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65 6c 20 ** Higher level
13bbc 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 routines should
13bbd 68 61 76 65 20 61 6c 72 65 61 64 79 20 73 74 61 have already sta
13bbe 72 74 65 64 20 61 20 74 72 61 6e 73 61 63 74 69 rted a transacti
13bbf 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 on,. ** which
13bc0 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61 76 65 means they have
13bc1 20 61 63 71 75 69 72 65 64 20 74 68 65 20 6e 65 acquired the ne
13bc2 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20 61 6e cessary locks an
13bc3 64 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20 d opened. **
13bc4 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
13bc5 61 6c 2e 20 20 44 6f 75 62 6c 65 2d 63 68 65 63 al. Double-chec
13bc6 6b 20 74 6f 20 6d 61 6b 65 73 20 73 75 72 65 20 k to makes sure
13bc7 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 this is the case
13bc8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
13bc9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 = sqlite3PagerBe
13bca 67 69 6e 28 70 50 61 67 65 72 2c 20 30 2c 20 70 gin(pPager, 0, p
13bcb 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d Pager->subjInMem
13bcc 6f 72 79 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 ory);. if( NE
13bcd 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f VER(rc!=SQLITE_O
13bce 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 K) ){. retu
13bcf 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
13bd0 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 if( !isOpen(pPa
13bd1 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 ger->jfd) && pPa
13bd2 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
13bd3 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d !=PAGER_JOURNALM
13bd4 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 ODE_OFF ){.
13bd5 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13bd6 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 >useJournal );.
13bd7 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
13bd8 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 open_journal(pPa
13bd9 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ger);. if(
13bda 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
13bdb 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
13bdc 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d . pPager->dbM
13bdd 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a odified = 1;. .
13bde 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 /* The trans
13bdf 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e action journal n
13be0 6f 77 20 65 78 69 73 74 73 20 61 6e 64 20 77 65 ow exists and we
13be1 20 68 61 76 65 20 61 20 52 45 53 45 52 56 45 44 have a RESERVED
13be2 20 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 or an. ** EX
13be3 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 CLUSIVE lock on
13be4 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
13be5 65 20 66 69 6c 65 2e 20 20 57 72 69 74 65 20 74 e file. Write t
13be6 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 he current page
13be7 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 72 to. ** the tr
13be8 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 ansaction journa
13be9 6c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 74 l if it is not t
13bea 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 here already..
13beb 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 61 */. if( !pa
13bec 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 geInJournal(pPg)
13bed 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
13bee 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
13bef 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d if( pPg->pgno<=
13bf0 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
13bf1 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33 ze ){. u3
13bf2 32 20 63 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20 2 cksum;.
13bf3 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a char *pData2;..
13bf4 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 73 68 /* We sh
13bf5 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69 74 65 ould never write
13bf6 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
13bf7 66 69 6c 65 20 74 68 65 20 70 61 67 65 20 74 68 file the page th
13bf8 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f at. ** co
13bf9 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61 62 ntains the datab
13bfa 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 ase locks. The
13bfb 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
13bfc 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 20 20 verifies.
13bfd 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 6f 20 ** that we do
13bfe 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 not. */.
13bff 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 67 6e assert( pPg->pgn
13c00 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f o!=PAGER_MJ_PGNO
13c01 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
13c02 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 CODEC2(pPage
13c03 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 r, pData, pPg->p
13c04 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 gno, 7, return S
13c05 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 QLITE_NOMEM, pDa
13c06 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 63 6b ta2);. ck
13c07 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 sum = pager_cksu
13c08 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70 m(pPager, (u8*)p
13c09 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 Data2);.
13c0a 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
13c0b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
13c0c 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13c0d 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 , pPg->pgno);.
13c0e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
13c0f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13c10 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13c11 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
13c12 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 >jfd, pData2, pP
13c13 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a ager->pageSize,.
13c14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
13c16 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13c17 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 + 4);.
13c18 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13c19 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 Off += pPager->p
13c1a 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 ageSize+4;.
13c1b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
13c1c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13c1d 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
13c1e 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
13c1f 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
13c20 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b ->journalOff, ck
13c21 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 sum);.
13c22 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13c23 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 ff += 4;.
13c24 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 }. IOTRA
13c25 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64 20 CE(("JOUT %p %d
13c26 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 %lld %d\n", pPag
13c27 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a er, pPg->pgno, .
13c28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13c29 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13c2a 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 Off, pPager->pag
13c2b 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20 20 eSize));.
13c2c 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 PAGER_INCR(sqli
13c2d 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a te3_pager_writej
13c2e 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 _count);.
13c2f 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a 4f PAGERTRACE(("JO
13c30 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 URNAL %d page %d
13c31 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61 73 needSync=%d has
13c32 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 h(%08x)\n",.
13c33 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 PAGERID
13c34 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 (pPager), pPg->p
13c35 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 gno, .
13c36 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 ((pPg->flags&
13c37 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
13c38 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61 67 ?1:0), pager_pag
13c39 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 ehash(pPg)));..
13c3a 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e 20 69 /* Even i
13c3b 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66 f an IO or diskf
13c3c 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 72 72 ull error occurr
13c3d 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c ed while journal
13c3e 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 20 20 ling the.
13c3f 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 ** page in the
13c40 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 65 74 block above, set
13c41 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 20 66 the need-sync f
13c42 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 67 65 lag for the page
13c43 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 .. ** Oth
13c44 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 68 65 erwise, when the
13c45 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
13c46 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 rolled back, the
13c47 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 20 20 logic in.
13c48 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e ** playback_on
13c49 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 74 68 e_page() will th
13c4a 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 ink that the pag
13c4b 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 e needs to be re
13c4c 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 20 2a stored. *
13c4d 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 * in the databas
13c4e 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 20 61 e file. And if a
13c4f 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 n IO error occur
13c50 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f s while doing so
13c51 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ,. ** the
13c52 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79 n corruption may
13c53 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20 follow..
13c54 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
13c55 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
13c56 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 ){. pPg
13c57 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
13c58 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
13c59 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 pPager->ne
13c5a 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
13c5b 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f }.. /
13c5c 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f * An error has o
13c5d 63 63 75 72 72 65 64 20 77 72 69 74 69 6e 67 20 ccurred writing
13c5e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
13c5f 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20 ile. The .
13c60 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
13c61 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 will be rolled
13c62 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65 back by the laye
13c63 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 r above..
13c64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
13c65 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13c66 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
13c67 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
13c68 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
13c69 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 >nRec++;.
13c6a 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13c6b 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 >pInJournal!=0 )
13c6c 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
13c6d 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
13c6e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
13c6f 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a al, pPg->pgno);.
13c70 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
13c71 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
13c72 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 EM );. as
13c73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
13c74 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 _OK || rc==SQLIT
13c75 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 E_NOMEM );.
13c76 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f 53 61 rc |= addToSa
13c77 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70 vepointBitvecs(p
13c78 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f Pager, pPg->pgno
13c79 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
13c7a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13c7b 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
13c7c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
13c7d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 EM );.
13c7e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
13c7f 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
13c80 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 {. if( !p
13c81 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 Pager->journalSt
13c82 61 72 74 65 64 20 26 26 20 21 70 50 61 67 65 72 arted && !pPager
13c83 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
13c84 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
13c85 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
13c86 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 YNC;. p
13c87 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
13c88 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 1;. }.
13c89 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 PAGERTRAC
13c8a 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 70 61 E(("APPEND %d pa
13c8b 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 ge %d needSync=%
13c8c 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
13c8d 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
13c8e 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f ager), pPg->pgno
13c8f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
13c90 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 ((pPg->flags&PG
13c91 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 HDR_NEED_SYNC)?1
13c92 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 :0)));. }.
13c93 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
13c94 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
13c95 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 journal is open
13c96 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 and the page is
13c97 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a not in it,. *
13c98 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 * then write the
13c99 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f current page to
13c9a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a the statement j
13c9b 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68 ournal. Note th
13c9c 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74 at. ** the st
13c9d 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
13c9e 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 20 66 format differs f
13c9f 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 rom the standard
13ca0 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a journal format.
13ca1 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 20 69 ** in that i
13ca2 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 65 63 t omits the chec
13ca3 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 68 65 ksums and the he
13ca4 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ader.. */.
13ca5 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 72 65 if( subjRequire
13ca6 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 sPage(pPg) ){.
13ca7 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 rc = subjour
13ca8 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20 nalPage(pPg);.
13ca9 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 }. }.. /* Up
13caa 64 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 date the databas
13cab 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 75 72 e size and retur
13cac 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 n.. */. assert
13cad 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
13cae 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b =PAGER_SHARED );
13caf 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 . if( pPager->d
13cb0 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 bSize<pPg->pgno
13cb1 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 ){. pPager->d
13cb2 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e bSize = pPg->pgn
13cb3 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 o;. }. return
13cb4 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 rc;.}../*.** Mar
13cb5 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 61 73 k a data page as
13cb6 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 69 73 writeable. This
13cb7 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 routine must be
13cb8 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a called before .
13cb9 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 ** making change
13cba 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 68 65 s to a page. The
13cbb 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 68 65 caller must che
13cbc 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ck the return va
13cbd 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 lue .** of this
13cbe 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 65 20 function and be
13cbf 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63 careful not to c
13cc0 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64 hange any page d
13cc1 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 ata unless .** t
13cc2 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
13cc3 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a rns SQLITE_OK..*
13cc4 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 *.** The differe
13cc5 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 nce between this
13cc6 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61 function and pa
13cc7 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 20 74 ger_write() is t
13cc8 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 hat this.** func
13cc9 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20 tion also deals
13cca 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c with the special
13ccb 20 63 61 73 65 20 77 68 65 72 65 20 32 20 6f 72 case where 2 or
13ccc 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a 20 66 more pages.** f
13ccd 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 it on a single d
13cce 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e 20 74 isk sector. In t
13ccf 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d his case all co-
13cd0 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 0a 2a resident pages.*
13cd1 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e * must have been
13cd2 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
13cd3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 journal file bef
13cd4 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
13cd5 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
13cd6 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f occurs, SQLITE_
13cd7 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 NOMEM or an IO e
13cd8 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
13cd9 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 70 72 urned.** as appr
13cda 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 77 69 opriate. Otherwi
13cdb 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a se, SQLITE_OK..*
13cdc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13cdd 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13cde 72 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70 rWrite(DbPage *p
13cdf 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 DbPage){. int r
13ce0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
13ce1 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 70 PgHdr *pPg = p
13ce2 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 DbPage;. Pager
13ce3 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
13ce4 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50 Pager;. Pgno nP
13ce5 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d 20 28 agePerSector = (
13ce6 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
13ce7 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ze/pPager->pageS
13ce8 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61 ize);.. if( nPa
13ce9 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 29 7b gePerSector>1 ){
13cea 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 . Pgno nPageC
13ceb 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f ount; /
13cec 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
13ced 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 f pages in datab
13cee 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 ase file */.
13cef 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 20 20 Pgno pg1;
13cf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
13cf1 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 st page of the s
13cf2 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 ector pPg is loc
13cf3 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 ated on. */.
13cf4 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 int nPage;
13cf5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
13cf6 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 61 ber of pages sta
13cf7 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 6f 20 rting at pg1 to
13cf8 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69 journal */. i
13cf9 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
13cfa 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
13cfb 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
13cfc 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 int needSync = 0
13cfd 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
13cfe 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 68 61 e if any page ha
13cff 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s PGHDR_NEED_SYN
13d00 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74 C */.. /* Set
13d01 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 the doNotSync f
13d02 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 20 69 lag to 1. This i
13d03 73 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e s because we can
13d04 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 not allow a jour
13d05 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 nal. ** heade
13d06 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 r to be written
13d07 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 67 65 between the page
13d08 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 s journaled by t
13d09 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 his function..
13d0a 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
13d0b 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 !MEMDB );. a
13d0c 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
13d0d 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 oNotSync==0 );.
13d0e 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 pPager->doNot
13d0f 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f Sync = 1;.. /
13d10 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 73 73 * This trick ass
13d11 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 20 74 umes that both t
13d12 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 he page-size and
13d13 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 sector-size are
13d14 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 . ** an integ
13d15 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 er power of 2. I
13d16 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 t sets variable
13d17 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 6e 74 pg1 to the ident
13d18 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 ifier. ** of
13d19 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f the first page o
13d1a 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 f the sector pPg
13d1b 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a is located on..
13d1c 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d */. pg1 =
13d1d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 ((pPg->pgno-1)
13d1e 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 63 74 & ~(nPagePerSect
13d1f 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 or-1)) + 1;..
13d20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
13d21 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 28 ecount(pPager, (
13d22 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e int *)&nPageCoun
13d23 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 67 2d t);. if( pPg-
13d24 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 >pgno>nPageCount
13d25 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 ){. nPage
13d26 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 = (pPg->pgno - p
13d27 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 g1)+1;. }else
13d28 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 65 50 if( (pg1+nPageP
13d29 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 erSector-1)>nPag
13d2a 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 eCount ){.
13d2b 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 nPage = nPageCou
13d2c 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 nt+1-pg1;. }e
13d2d 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 lse{. nPage
13d2e 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f = nPagePerSecto
13d2f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 r;. }. ass
13d30 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 ert(nPage>0);.
13d31 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d 70 50 assert(pg1<=pP
13d32 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 g->pgno);. as
13d33 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 65 29 sert((pg1+nPage)
13d34 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 >pPg->pgno);..
13d35 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e for(ii=0; ii<n
13d36 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 Page && rc==SQLI
13d37 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 TE_OK; ii++){.
13d38 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 Pgno pg = pg
13d39 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 48 64 1+ii;. PgHd
13d3a 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 20 20 r *pPage;.
13d3b 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e if( pg==pPg->pgn
13d3c 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 69 74 o || !sqlite3Bit
13d3d 76 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e vecTest(pPager->
13d3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 pInJournal, pg)
13d3f 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
13d40 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f g!=PAGER_MJ_PGNO
13d41 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 (pPager) ){.
13d42 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
13d43 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 e3PagerGet(pPage
13d44 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 3b 0a r, pg, &pPage);.
13d45 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
13d46 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13d47 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
13d48 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 61 67 pager_write(pPag
13d49 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
13d4a 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 if( pPage->flags
13d4b 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
13d4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
13d4d 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a needSync = 1;.
13d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 as
13d4f 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 sert(pPager->nee
13d50 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 dSync);.
13d51 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
13d52 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
13d53 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ref(pPage);.
13d54 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13d55 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
13d56 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 65 72 ( (pPage = pager
13d57 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 _lookup(pPager,
13d58 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 pg))!=0 ){.
13d59 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c if( pPage->fl
13d5a 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
13d5b 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 YNC ){.
13d5c 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 needSync = 1;.
13d5d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
13d5e 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
13d5f 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 ef(pPage);.
13d60 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
13d61 20 49 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 If the PGHDR_NE
13d62 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 20 ED_SYNC flag is
13d63 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 set for any of t
13d64 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 0a he nPage pages .
13d65 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 ** starting
13d66 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 74 20 at pg1, then it
13d67 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 74 20 needs to be set
13d68 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e for all of them.
13d69 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 Because. **
13d6a 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f writing to any o
13d6b 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 70 61 f these nPage pa
13d6c 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 20 74 ges may damage t
13d6d 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 he others, the.
13d6e 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 ** journal fi
13d6f 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 le must contain
13d70 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 73 20 sync()ed copies
13d71 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 of all of them.
13d72 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 ** before any
13d73 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 65 20 of them can be
13d74 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 written out to t
13d75 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13d76 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
13d77 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13d78 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 & needSync ){.
13d79 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d assert( !MEM
13d7a 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f DB && pPager->no
13d7b 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Sync==0 );.
13d7c 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 for(ii=0; ii<nP
13d7d 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 age; ii++){.
13d7e 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 PgHdr *pPage
13d7f 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 = pager_lookup(
13d80 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b pPager, pg1+ii);
13d81 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 . if( pPa
13d82 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ge ){.
13d83 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPage->flags |=
13d84 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b PGHDR_NEED_SYNC;
13d85 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
13d86 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
13d87 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ge);. }.
13d88 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
13d89 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 ert(pPager->need
13d8a 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 Sync);. }..
13d8b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13d8c 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 ->doNotSync==1 )
13d8d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f ;. pPager->do
13d8e 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d NotSync = 0;. }
13d8f 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 else{. rc = p
13d90 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 50 61 ager_write(pDbPa
13d91 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ge);. }. retur
13d92 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
13d93 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
13d94 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20 e page given in
13d95 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73 the argument was
13d96 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73 previously pass
13d97 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 ed.** to sqlite3
13d98 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20 49 PagerWrite(). I
13d99 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 n other words, r
13d9a 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 eturn TRUE if it
13d9b 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 is ok.** to cha
13d9c 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 nge the content
13d9d 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a of the page..*/.
13d9e 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 #ifndef NDEBUG.S
13d9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13da0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
13da1 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
13da2 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e *pPg){. return
13da3 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
13da4 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 R_DIRTY;.}.#endi
13da5 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
13da6 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a E_SECURE_DELETE.
13da7 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 /*.** A call to
13da8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c this routine tel
13da9 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 ls the pager tha
13daa 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 t it is not nece
13dab 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 ssary to.** writ
13dac 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f e the informatio
13dad 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 n on page pPg ba
13dae 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 ck to the disk,
13daf 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 even though.** t
13db0 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 hat page might b
13db1 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 e marked as dirt
13db2 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 y. This happens
13db3 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 , for example, w
13db4 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 hen.** the page
13db5 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 has been added a
13db6 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 s a leaf of the
13db7 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 freelist and so
13db8 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e its.** content n
13db9 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 o longer matters
13dba 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 ..**.** The over
13dbb 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c lying software l
13dbc 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 ayer calls this
13dbd 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c routine when all
13dbe 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 of the data.**
13dbf 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 on the given pag
13dc0 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65 e is unused. The
13dc1 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 pager marks the
13dc2 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 page as clean s
13dc3 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 o.** that it doe
13dc4 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 s not get writte
13dc5 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a n to disk..**.**
13dc6 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 Tests show that
13dc7 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 this optimizati
13dc8 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65 on can quadruple
13dc9 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 the speed of la
13dca 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f rge .** DELETE o
13dcb 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 perations..*/.SQ
13dcc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
13dcd 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f d sqlite3PagerDo
13dce 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 2a 70 ntWrite(PgHdr *p
13dcf 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 Pg){. Pager *pP
13dd0 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
13dd1 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e er;. if( (pPg->
13dd2 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
13dd3 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 Y) && pPager->nS
13dd4 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 avepoint==0 ){.
13dd5 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
13dd6 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 DONT_WRITE page
13dd7 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 %d of %d\n", pPg
13dd8 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 ->pgno, PAGERID(
13dd9 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49 pPager)));. I
13dda 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 OTRACE(("CLEAN %
13ddb 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
13ddc 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 pPg->pgno)).
13ddd 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 pPg->flags |= P
13dde 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b GHDR_DONT_WRITE;
13ddf 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
13de0 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 HECK_PAGES. p
13de1 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 Pg->pageHash = p
13de2 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
13de3 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d g);.#endif. }.}
13de4 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
13de5 6e 65 64 28 53 51 4c 49 54 45 5f 53 45 43 55 52 ned(SQLITE_SECUR
13de6 45 5f 44 45 4c 45 54 45 29 20 2a 2f 0a 0a 2f 2a E_DELETE) */../*
13de7 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
13de8 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e is called to in
13de9 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 crement the valu
13dea 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
13deb 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67 e file .** chang
13dec 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65 e-counter, store
13ded 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69 d as a 4-byte bi
13dee 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
13def 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a starting at .**
13df0 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 byte offset 24
13df1 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c of the pager fil
13df2 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
13df3 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 isDirectMode fla
13df4 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 g is zero, then
13df5 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 this is done by
13df6 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 calling .** sqli
13df7 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
13df8 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 on page 1, then
13df9 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f modifying the co
13dfa 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a ntents of the.**
13dfb 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 page data. In t
13dfc 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c his case the fil
13dfd 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 e will be update
13dfe 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 d when the curre
13dff 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
13e00 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a n is committed..
13e01 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 **.** The isDire
13e02 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20 ctMode flag may
13e03 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f only be non-zero
13e04 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 if the library
13e05 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 was compiled.**
13e06 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
13e07 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13e08 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ITE macro define
13e09 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c d. In this case,
13e0a 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 .** if isDirect
13e0b 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
13e0c 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13e0d 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 ile is updated d
13e0e 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 irectly.** by wr
13e0f 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 iting an updated
13e10 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 version of page
13e11 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 1 using a call
13e12 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 to the .** sqlit
13e13 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 e3OsWrite() func
13e14 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
13e15 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 int pager_incr_c
13e16 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 hangecounter(Pag
13e17 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
13e18 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 isDirectMode){.
13e19 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
13e1a 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 _OK;.. /* Decla
13e1b 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a re and initializ
13e1c 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 e constant integ
13e1d 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 er 'isDirect'. I
13e1e 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 f the. ** atomi
13e1f 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
13e20 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
13e21 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 in this build, t
13e22 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a hen isDirect. *
13e23 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 * is initialized
13e24 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 to the value pa
13e25 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 ssed as the isDi
13e26 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 rectMode paramet
13e27 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 er. ** to this
13e28 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 function. Otherw
13e29 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 ise, it is alway
13e2a 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 s set to zero..
13e2b 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 **. ** The ide
13e2c 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 a is that if the
13e2d 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
13e2e 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f timization is no
13e2f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 t. ** enabled a
13e30 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 t compile time,
13e31 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
13e32 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 omit the tests
13e33 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 of. ** 'isDirec
13e34 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c t' below, as wel
13e35 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 l as the block e
13e36 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 nclosed in the.
13e37 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 ** "if( isDirec
13e38 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a t )" condition..
13e39 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
13e3a 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
13e3b 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65 C_WRITE.# define
13e3c 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20 DIRECT_MODE 0.
13e3d 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63 assert( isDirec
13e3e 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e tMode==0 );. UN
13e3f 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
13e40 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 sDirectMode);.#e
13e41 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52 lse.# define DIR
13e42 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63 ECT_MODE isDirec
13e43 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20 tMode.#endif..
13e44 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13e45 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
13e46 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21 ERVED );. if( !
13e47 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
13e48 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 67 65 untDone && pPage
13e49 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20 r->dbSize>0 ){.
13e4a 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 64 72 PgHdr *pPgHdr
13e4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13e4c 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f /* Reference to
13e4d 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 20 75 page 1 */. u
13e4e 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 32 change_counte
13e4f 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
13e50 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 Initial value of
13e51 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
13e52 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 61 73 field */.. as
13e53 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 sert( !pPager->t
13e54 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f 70 65 empFile && isOpe
13e55 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b n(pPager->fd) );
13e56 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 .. /* Open pa
13e57 67 65 20 31 20 6f 66 20 74 68 65 20 66 69 6c 65 ge 1 of the file
13e58 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f for writing. */
13e59 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
13e5a 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
13e5b 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 , 1, &pPgHdr);.
13e5c 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64 assert( pPgHd
13e5d 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 r==0 || rc==SQLI
13e5e 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a TE_OK );.. /*
13e5f 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77 61 73 If page one was
13e60 20 66 65 74 63 68 65 64 20 73 75 63 63 65 73 73 fetched success
13e61 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 73 20 fully, and this
13e62 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 0a function is not.
13e63 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 ** operating
13e64 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 65 2c in direct-mode,
13e65 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77 72 69 make page 1 wri
13e66 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e 6f 74 table. When not
13e67 20 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 72 65 in . ** dire
13e68 63 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 31 20 ct mode, page 1
13e69 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20 69 is always held i
13e6a 6e 20 63 61 63 68 65 20 61 6e 64 20 68 65 6e 63 n cache and henc
13e6b 65 20 74 68 65 20 50 61 67 65 72 47 65 74 28 29 e the PagerGet()
13e6c 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 69 73 . ** above is
13e6d 20 61 6c 77 61 79 73 20 73 75 63 63 65 73 73 66 always successf
13e6e 75 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 20 41 ul - hence the A
13e6f 4c 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 51 4c LWAYS on rc==SQL
13e70 49 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 ITE_OK.. */.
13e71 20 20 20 69 66 28 20 21 44 49 52 45 43 54 5f 4d if( !DIRECT_M
13e72 4f 44 45 20 26 26 20 41 4c 57 41 59 53 28 72 63 ODE && ALWAYS(rc
13e73 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a ==SQLITE_OK) ){.
13e74 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
13e75 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 e3PagerWrite(pPg
13e76 48 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 Hdr);. }..
13e77 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13e78 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 OK ){. /* I
13e79 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c ncrement the val
13e7a 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e 64 ue just read and
13e7b 20 77 72 69 74 65 20 69 74 20 62 61 63 6b 20 74 write it back t
13e7c 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20 20 o byte 24. */.
13e7d 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 change_count
13e7e 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 er = sqlite3Get4
13e7f 62 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 byte((u8*)pPager
13e80 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 ->dbFileVers);.
13e81 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e change_coun
13e82 74 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70 75 74 ter++;. put
13e83 33 32 62 69 74 73 28 28 28 63 68 61 72 2a 29 70 32bits(((char*)p
13e84 50 67 48 64 72 2d 3e 70 44 61 74 61 29 2b 32 34 PgHdr->pData)+24
13e85 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 , change_counter
13e86 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 );.. /* If
13e87 72 75 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63 running in direc
13e88 74 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68 t mode, write th
13e89 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 e contents of pa
13e8a 67 65 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65 ge 1 to the file
13e8b 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44 . */. if( D
13e8c 49 52 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 IRECT_MODE ){.
13e8d 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 const void
13e8e 20 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 72 2d *zBuf = pPgHdr-
13e8f 3e 70 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 >pData;.
13e90 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13e91 64 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a dbFileSize>0 );.
13e92 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
13e93 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
13e94 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70 50 er->fd, zBuf, pP
13e95 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
13e96 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
13e97 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13e98 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 . pPage
13e99 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
13e9a 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 ne = 1;.
13e9b 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
13e9c 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 pPager->c
13e9d 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
13e9e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
13e9f 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 }.. /* Releas
13ea0 65 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 e the page refer
13ea1 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c ence. */. sql
13ea2 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
13ea3 50 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 PgHdr);. }. re
13ea4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
13ea5 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 * Sync the pager
13ea6 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 file to disk. T
13ea7 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 his is a no-op f
13ea8 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c or in-memory fil
13ea9 65 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 es.** or pages w
13eaa 69 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f ith the Pager.no
13eab 53 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a Sync flag set..*
13eac 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 *.** If successf
13ead 75 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e ul, or called on
13eae 20 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 69 a pager for whi
13eaf 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 ch it is a no-op
13eb0 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 , this.** functi
13eb1 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 on returns SQLIT
13eb2 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c E_OK. Otherwise,
13eb3 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
13eb4 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
13eb5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13eb6 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13eb7 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 rSync(Pager *pPa
13eb8 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 ger){. int rc;
13eb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13eba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13ebb 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
13ebc 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
13ebd 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
13ebe 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 >noSync ){. r
13ebf 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
13ec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
13ec1 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
13ec2 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 Pager->fd, pPage
13ec3 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
13ec4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13ec5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 .}../*.** Sync t
13ec6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13ec7 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 20 70 for the pager p
13ec8 50 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 20 70 Pager. zMaster p
13ec9 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 61 6d oints to the nam
13eca 65 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 65 72 e.** of a master
13ecb 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
13ecc 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 at should be wri
13ecd 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 69 6e tten into the in
13ece 64 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 dividual.** jour
13ecf 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 74 65 nal file. zMaste
13ed0 72 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c 20 77 r may be NULL, w
13ed1 68 69 63 68 20 69 73 20 69 6e 74 65 72 70 72 65 hich is interpre
13ed2 74 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 65 72 ted as no master
13ed3 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 20 73 .** journal (a s
13ed4 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 ingle database t
13ed5 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a ransaction)..**.
13ed6 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
13ed7 65 6e 73 75 72 65 73 20 74 68 61 74 3a 0a 2a 2a ensures that:.**
13ed8 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 .** * The data
13ed9 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 base file change
13eda 2d 63 6f 75 6e 74 65 72 20 69 73 20 75 70 64 61 -counter is upda
13edb 74 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 ted,.** * the
13edc 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 journal is synce
13edd 64 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 74 d (unless the at
13ede 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d omic-write optim
13edf 69 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 29 ization is used)
13ee0 2c 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 69 72 ,.** * all dir
13ee1 74 79 20 70 61 67 65 73 20 61 72 65 20 77 72 69 ty pages are wri
13ee2 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
13ee3 62 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 base file, .**
13ee4 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 * the database
13ee5 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 file is truncate
13ee6 64 20 28 69 66 20 72 65 71 75 69 72 65 64 29 2c d (if required),
13ee7 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 65 20 and.** * the
13ee8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73 79 database file sy
13ee9 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 nced. .**.** The
13eea 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 61 74 only thing that
13eeb 20 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d remains to comm
13eec 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 it the transacti
13eed 6f 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c 69 7a on is to finaliz
13eee 65 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c 20 74 e .** (delete, t
13eef 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 runcate or zero
13ef0 74 68 65 20 66 69 72 73 74 20 70 61 72 74 20 6f the first part o
13ef1 66 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 f) the journal f
13ef2 69 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 ile (or .** dele
13ef3 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f te the master jo
13ef4 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70 urnal file if sp
13ef5 65 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 ecified)..**.**
13ef6 4e 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61 Note that if zMa
13ef7 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 ster==NULL, this
13ef8 20 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72 does not overwr
13ef9 69 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76 ite a previous v
13efa 61 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74 alue.** passed t
13efb 6f 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65 o an sqlite3Page
13efc 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
13efd 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 ) call..**.** If
13efe 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d the final param
13eff 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 eter - noSync -
13f00 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
13f01 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13f02 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 itself.** is not
13f03 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63 61 6c synced. The cal
13f04 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 ler must call sq
13f05 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 29 lite3PagerSync()
13f06 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 directly to.**
13f07 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 sync the databas
13f08 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 63 61 e file before ca
13f09 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 61 73 lling CommitPhas
13f0a 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 74 65 eTwo() to delete
13f0b 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
13f0c 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 file in this cas
13f0d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
13f0e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
13f0f 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
13f10 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 One(. Pager *pP
13f11 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 ager,
13f12 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 /* Pager
13f13 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 object */. cons
13f14 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c t char *zMaster,
13f15 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
13f16 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 f not NULL, the
13f17 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
13f18 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 ame */. int noS
13f19 79 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 ync
13f1a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
13f1b 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 53 79 to omit the xSy
13f1c 6e 63 20 6f 6e 20 74 68 65 20 64 62 20 66 69 6c nc on the db fil
13f1d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
13f1e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
13f1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
13f20 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f urn code */.. /
13f21 2a 20 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65 * The dbOrigSize
13f22 20 69 73 20 6e 65 76 65 72 20 73 65 74 20 69 66 is never set if
13f23 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 journal_mode=OF
13f24 46 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 F */. assert( p
13f25 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
13f26 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
13f27 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 50 61 LMODE_OFF || pPa
13f28 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d ger->dbOrigSize=
13f29 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 =0 );.. /* If a
13f2a 20 70 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63 prior error occ
13f2b 75 72 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74 urred, this rout
13f2c 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 ine should not b
13f2d 65 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42 e called. ROLLB
13f2e 41 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 ACK. ** is the
13f2f 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 70 appropriate resp
13f30 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72 onse to an error
13f31 2c 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20 20 47 , not COMMIT. G
13f32 75 61 72 64 20 61 67 61 69 6e 73 74 0a 20 20 2a uard against. *
13f33 2a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 73 20 * coding errors
13f34 62 79 20 72 65 70 65 61 74 69 6e 67 20 74 68 65 by repeating the
13f35 20 70 72 69 6f 72 20 65 72 72 6f 72 2e 20 2a 2f prior error. */
13f36 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 . if( NEVER(pPa
13f37 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 ger->errCode) )
13f38 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 return pPager->e
13f39 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52 rrCode;.. PAGER
13f3a 54 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45 TRACE(("DATABASE
13f3b 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a SYNC: File=%s z
13f3c 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d Master=%s nSize=
13f3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50 %d\n", . pP
13f3e 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
13f3f 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
13f40 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 69 ->dbSize));.. i
13f41 66 28 20 4d 45 4d 44 42 20 26 26 20 70 50 61 67 f( MEMDB && pPag
13f42 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 er->dbModified )
13f43 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
13f44 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 is an in-memory
13f45 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65 73 db, or no pages
13f46 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 have been writt
13f47 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a 20 en to, or this.
13f48 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 ** function h
13f49 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
13f4a 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 6d 6f called, it is mo
13f4b 73 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 stly a no-op. H
13f4c 6f 77 65 76 65 72 2c 20 61 6e 79 0a 20 20 20 20 owever, any.
13f4d 2a 2a 20 62 61 63 6b 75 70 20 69 6e 20 70 72 6f ** backup in pro
13f4e 67 72 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 gress needs to b
13f4f 65 20 72 65 73 74 61 72 74 65 64 2e 0a 20 20 20 e restarted..
13f50 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
13f51 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 ackupRestart(pPa
13f52 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 ger->pBackup);.
13f53 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
13f54 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f r->state!=PAGER_
13f55 53 59 4e 43 45 44 20 26 26 20 70 50 61 67 65 72 SYNCED && pPager
13f56 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a ->dbModified ){.
13f57 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c . /* The foll
13f58 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64 61 owing block upda
13f59 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63 tes the change-c
13f5a 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79 20 ounter. Exactly
13f5b 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 64 6f how it. ** do
13f5c 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 73 20 es this depends
13f5d 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f on whether or no
13f5e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 t the atomic-upd
13f5f 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ate optimization
13f60 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e 61 62 . ** was enab
13f61 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 led at compile t
13f62 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 69 73 ime, and if this
13f63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 65 65 transaction mee
13f64 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 ts the . ** r
13f65 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69 61 20 untime criteria
13f66 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65 72 61 to use the opera
13f67 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a 20 20 tion: . **.
13f68 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 66 69 ** * The fi
13f69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le-system suppor
13f6a 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 ts the atomic-wr
13f6b 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 6f 72 ite property for
13f6c 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f . ** blo
13f6d 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 67 65 cks of size page
13f6e 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 -size, and .
13f6f 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d ** * This com
13f70 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 mit is not part
13f71 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 of a multi-file
13f72 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 transaction, and
13f73 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78 61 . ** * Exa
13f74 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68 61 ctly one page ha
13f75 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
13f76 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68 65 and store in the
13f77 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
13f78 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
13f79 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
13f7a 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 was not enabled
13f7b 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
13f7c 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a , then the. *
13f7d 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 * pager_incr_cha
13f7e 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 75 6e ngecounter() fun
13f7f 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
13f80 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 to update the ch
13f81 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e ange. ** coun
13f82 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 63 74 ter in 'indirect
13f83 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 20 6f -mode'. If the o
13f84 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 63 ptimization is c
13f85 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 0a 20 ompiled in but.
13f86 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 70 70 ** is not app
13f87 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69 73 20 licable to this
13f88 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 61 6c transaction, cal
13f89 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c l sqlite3Journal
13f8a 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a 2a 20 Create(). **
13f8b 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
13f8c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
13f8d 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 s actually been
13f8e 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 61 created, then ca
13f8f 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f ll. ** pager_
13f90 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13f91 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 er() to update t
13f92 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
13f93 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20 r in indirect.
13f94 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20 20 ** mode. .
13f95 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 **. ** Otherw
13f96 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70 74 69 ise, if the opti
13f97 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f 74 68 mization is both
13f98 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 70 70 enabled and app
13f99 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 licable,. **
13f9a 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 72 5f then call pager_
13f9b 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13f9c 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 er() to update t
13f9d 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 he change-counte
13f9e 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 69 72 r. ** in 'dir
13f9f 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 74 68 ect' mode. In th
13fa0 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 is case the jour
13fa1 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65 nal file will ne
13fa2 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 63 72 ver be. ** cr
13fa3 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74 eated for this t
13fa4 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 ransaction..
13fa5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
13fa6 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
13fa7 52 49 54 45 0a 20 20 20 20 50 67 48 64 72 20 2a RITE. PgHdr *
13fa8 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 74 28 pPg;. assert(
13fa9 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
13faa 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e jfd) || pPager->
13fab 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
13fac 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
13fad 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a FF );. if( !z
13fae 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 65 6e Master && isOpen
13faf 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 0a 20 (pPager->jfd) .
13fb0 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a && pPager->j
13fb1 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 ournalOff==jrnlB
13fb2 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 ufferSize(pPager
13fb3 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 ) . && pPage
13fb4 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 67 65 r->dbSize>=pPage
13fb5 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a 20 20 r->dbFileSize.
13fb6 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67 20 3d && (0==(pPg =
13fb7 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 sqlite3PcacheDi
13fb8 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e rtyList(pPager->
13fb9 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30 3d 3d pPCache)) || 0==
13fba 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 20 20 pPg->pDirty).
13fbb 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 ){. /* Upd
13fbc 61 74 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 ate the db file
13fbd 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 76 change counter v
13fbe 69 61 20 74 68 65 20 64 69 72 65 63 74 2d 77 72 ia the direct-wr
13fbf 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 ite method. The
13fc0 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 . ** follow
13fc1 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f ing call will mo
13fc2 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f dify the in-memo
13fc3 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ry representatio
13fc4 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20 n of page 1 .
13fc5 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 ** to include
13fc6 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61 the updated cha
13fc7 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20 nge counter and
13fc8 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20 then write page
13fc9 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 72 65 1 . ** dire
13fca 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61 ctly to the data
13fcb 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 61 75 base file. Becau
13fcc 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d 69 63 se of the atomic
13fcd 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 2a 2a -write . **
13fce 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 property of the
13fcf 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65 host file-syste
13fd0 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66 65 2e m, this is safe.
13fd1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
13fd2 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
13fd3 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
13fd4 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 7d 65 ager, 1);. }e
13fd5 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
13fd6 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 sqlite3JournalCr
13fd7 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 eate(pPager->jfd
13fd8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
13fd9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13fda 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
13fdb 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e _incr_changecoun
13fdc 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ter(pPager, 0);.
13fdd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 }. }.#e
13fde 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70 61 67 lse. rc = pag
13fdf 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
13fe0 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 unter(pPager, 0)
13fe1 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 ;.#endif. if(
13fe2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13fe3 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 goto commit_pha
13fe4 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 se_one_exit;..
13fe5 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61 /* If this tra
13fe6 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 nsaction has mad
13fe7 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 e the database s
13fe8 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c maller, then all
13fe9 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 62 65 pages. ** be
13fea 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62 79 ing discarded by
13feb 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 the truncation
13fec 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e 20 must be written
13fed 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 to the journal.
13fee 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 69 73 ** file. This
13fef 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
13ff0 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 in auto-vacuum
13ff1 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 mode.. **.
13ff2 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 ** Before readi
13ff3 6e 67 20 74 68 65 20 70 61 67 65 73 20 77 69 74 ng the pages wit
13ff4 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c h page numbers l
13ff5 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a arger than the .
13ff6 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76 ** current v
13ff7 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 alue of Pager.db
13ff8 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65 Size, set dbSize
13ff9 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c back to the val
13ffa 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 69 ue. ** that i
13ffb 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 73 74 t took at the st
13ffc 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 art of the trans
13ffd 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 action. Otherwis
13ffe 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 e, the. ** ca
13fff 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61 lls to sqlite3Pa
14000 67 65 72 47 65 74 28 29 20 72 65 74 75 72 6e 20 gerGet() return
14001 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69 6e 73 zeroed pages ins
14002 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a 2a 20 tead of . **
14003 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 72 6f reading data fro
14004 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
14005 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
14006 2a 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f ** When journal_
14007 6d 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20 64 62 mode==OFF the db
14008 4f 72 69 67 53 69 7a 65 20 69 73 20 61 6c 77 61 OrigSize is alwa
14009 79 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68 69 73 ys zero, so this
1400a 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 . ** block ne
1400b 76 65 72 20 72 75 6e 73 20 69 66 20 6a 6f 75 72 ver runs if jour
1400c 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 nal_mode=OFF..
1400d 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
1400e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
1400f 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 UUM. if( pPag
14010 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 er->dbSize<pPage
14011 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20 r->dbOrigSize .
14012 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 50 && ALWAYS(pP
14013 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
14014 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
14015 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20 29 7b MODE_OFF). ){
14016 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20 . Pgno i;
14017 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14018 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14019 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
1401a 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 63 iable */. c
1401b 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 70 20 onst Pgno iSkip
1401c 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 = PAGER_MJ_PGNO(
1401d 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 6e 64 pPager); /* Pend
1401e 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 2a 2f ing lock page */
1401f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e . const Pgn
14020 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 o dbSize = pPage
14021 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 20 20 r->dbSize;
14022 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6d 61 /* Database ima
14023 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 20 20 ge size */ .
14024 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
14025 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 = pPager->dbOri
14026 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 6f 72 gSize;. for
14027 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 69 3c ( i=dbSize+1; i<
14028 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 =pPager->dbOrigS
14029 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 ize; i++ ){.
1402a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
1402b 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 BitvecTest(pPage
1402c 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 r->pInJournal, i
1402d 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29 7b ) && i!=iSkip ){
1402e 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 64 72 . PgHdr
1402f 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 *pPage;
14030 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 /* Page to
14031 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 journal */.
14032 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
14033 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72 3PagerGet(pPager
14034 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 , i, &pPage);.
14035 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
14036 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
14037 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
14038 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 e_exit;.
14039 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1403a 67 65 72 57 72 69 74 65 28 70 50 61 67 65 29 3b gerWrite(pPage);
1403b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1403c 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
1403d 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
1403e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1403f 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
14040 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 hase_one_exit;.
14041 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
14042 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e . pPager->
14043 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a 65 3b dbSize = dbSize;
14044 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
14045 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
14046 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
14047 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 ame into the jou
14048 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 20 rnal file. If a
14049 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a 20 6a master . ** j
1404a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
1404b 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1404c 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 n written to the
1404d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a journal file, .
1404e 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 ** or if zMa
1404f 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f ster is NULL (no
14050 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 master journal)
14051 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c , then this call
14052 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 is a no-op..
14053 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 72 69 */. rc = wri
14054 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 teMasterJournal(
14055 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 pPager, zMaster)
14056 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
14057 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
14058 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
14059 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 exit;.. /* Sy
1405a 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 nc the journal f
1405b 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74 6f 6d ile. If the atom
1405c 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69 ic-update optimi
1405d 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 0a zation is being.
1405e 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 68 69 ** used, thi
1405f 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 s call will not
14060 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e create the journ
14061 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f al file or perfo
14062 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 72 65 rm any. ** re
14063 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 al IO.. */.
14064 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e rc = syncJourn
14065 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
14066 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
14067 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
14068 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
14069 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6c . /* Write al
1406a 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 74 6f l dirty pages to
1406b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1406c 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 le. */. rc =
1406d 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 pager_write_page
1406e 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63 61 63 list(sqlite3Pcac
1406f 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 heDirtyList(pPag
14070 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b 0a 20 er->pPCache));.
14071 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
14072 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 E_OK ){. as
14073 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
14074 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 _IOERR_BLOCKED )
14075 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6d ;. goto com
14076 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
14077 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 it;. }. sq
14078 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e lite3PcacheClean
14079 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 All(pPager->pPCa
1407a 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 che);.. /* If
1407b 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
1407c 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d k is not the sam
1407d 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 64 61 e size as the da
1407e 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a 20 20 tabase image,.
1407f 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 70 61 ** then use pa
14080 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74 6f 20 ger_truncate to
14081 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b 20 74 grow or shrink t
14082 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a 20 20 he file here..
14083 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 */. if( pPa
14084 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 50 61 ger->dbSize!=pPa
14085 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
14086 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e 4e ){. Pgno nN
14087 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ew = pPager->dbS
14088 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d 3e 64 ize - (pPager->d
14089 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f bSize==PAGER_MJ_
1408a 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b 0a 20 PGNO(pPager));.
1408b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1408c 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
1408d 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 R_EXCLUSIVE );.
1408e 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
1408f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c truncate(pPager,
14090 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66 nNew);. if
14091 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
14092 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
14093 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 ase_one_exit;.
14094 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 }.. /* Fina
14095 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 64 61 lly, sync the da
14096 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a tabase file. */.
14097 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d if( !pPager-
14098 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f 53 79 >noSync && !noSy
14099 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nc ){. rc =
1409a 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
1409b 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 Pager->fd, pPage
1409c 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a r->sync_flags);.
1409d 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 }. IOTRAC
1409e 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c 6e 22 E(("DBSYNC %p\n"
1409f 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 , pPager))..
140a0 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
140a1 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a 20 20 PAGER_SYNCED;.
140a2 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f }..commit_phase_
140a3 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65 74 75 one_exit:. retu
140a4 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
140a5 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 When this funct
140a6 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
140a7 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
140a8 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 has been comple
140a9 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 tely.** updated
140aa 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65 20 63 to reflect the c
140ab 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 hanges made by t
140ac 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
140ad 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 action and.** sy
140ae 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 nced to disk. Th
140af 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 e journal file s
140b0 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74 till exists in t
140b1 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a he file-system .
140b2 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 ** though, and i
140b3 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 f a failure occu
140b4 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 rs at this point
140b5 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 it will eventua
140b6 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 lly.** be used a
140b7 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 s a hot-journal
140b8 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 and the current
140b9 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c transaction roll
140ba 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 ed back..**.** T
140bb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e his function fin
140bc 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e alizes the journ
140bd 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 al file, either
140be 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a by deleting, .**
140bf 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 truncating or p
140c0 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 artially zeroing
140c1 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20 it, so that it
140c2 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a cannot be used .
140c3 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e ** for hot-journ
140c4 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 al rollback. Onc
140c5 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74 e this is done t
140c6 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
140c7 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 s.** irrevocably
140c8 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a committed..**.*
140c9 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
140ca 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f curs, an IO erro
140cb 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
140cc 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 ed and the pager
140cd 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 .** moves into t
140ce 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 he error state.
140cf 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 Otherwise, SQLIT
140d0 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
140d1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
140d2 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
140d3 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 agerCommitPhaseT
140d4 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 wo(Pager *pPager
140d5 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
140d6 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
140d7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
140d8 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f urn code */.. /
140d9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 * This routine s
140da 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c hould not be cal
140db 6c 65 64 20 69 66 20 61 20 70 72 69 6f 72 20 65 led if a prior e
140dc 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
140dd 64 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 28 d.. ** But if (
140de 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 due to a coding
140df 65 72 72 6f 72 20 65 6c 73 65 77 68 65 72 65 20 error elsewhere
140e0 69 6e 20 74 68 65 20 73 79 73 74 65 6d 29 20 69 in the system) i
140e1 74 20 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a 20 t does get. **
140e2 63 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 74 called, just ret
140e3 75 72 6e 20 74 68 65 20 73 61 6d 65 20 65 72 72 urn the same err
140e4 6f 72 20 63 6f 64 65 20 77 69 74 68 6f 75 74 20 or code without
140e5 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 doing anything.
140e6 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 */. if( NEVER(p
140e7 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 Pager->errCode)
140e8 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d ) return pPager-
140e9 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 >errCode;.. /*
140ea 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 This function sh
140eb 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c ould not be call
140ec 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 ed if the pager
140ed 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65 61 is not in at lea
140ee 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52 45 st. ** PAGER_RE
140ef 53 45 52 56 45 44 20 73 74 61 74 65 2e 20 41 6e SERVED state. An
140f0 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65 20 d indeed SQLite
140f1 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69 73 2e never does this.
140f2 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a 20 But it is. **
140f3 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74 68 69 nice to have thi
140f4 73 20 64 65 66 65 6e 73 69 76 65 20 74 65 73 74 s defensive test
140f5 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a 20 20 here anyway..
140f6 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 */. if( NEVER(p
140f7 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 Pager->state<PAG
140f8 45 52 5f 52 45 53 45 52 56 45 44 29 20 29 20 72 ER_RESERVED) ) r
140f9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
140fa 4f 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 OR;.. /* An opt
140fb 69 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 74 68 imization. If th
140fc 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6e e database was n
140fd 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 ot actually modi
140fe 66 69 65 64 20 64 75 72 69 6e 67 0a 20 20 2a 2a fied during. **
140ff 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
14100 6e 2c 20 74 68 65 20 70 61 67 65 72 20 69 73 20 n, the pager is
14101 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
14102 73 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 69 73 sive-mode and is
14103 0a 20 20 2a 2a 20 75 73 69 6e 67 20 70 65 72 73 . ** using pers
14104 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c istent journals,
14105 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
14106 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a ion is a no-op..
14107 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 **. ** The st
14108 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e art of the journ
14109 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c al file currentl
1410a 79 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e y contains a sin
1410b 67 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a gle journal . *
1410c 2a 20 68 65 61 64 65 72 20 77 69 74 68 20 74 68 * header with th
1410d 65 20 6e 52 65 63 20 66 69 65 6c 64 20 73 65 74 e nRec field set
1410e 20 74 6f 20 30 2e 20 49 66 20 73 75 63 68 20 61 to 0. If such a
1410f 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64 journal is used
14110 20 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a as. ** a hot-j
14111 6f 75 72 6e 61 6c 20 64 75 72 69 6e 67 20 68 6f ournal during ho
14112 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
14113 63 6b 2c 20 30 20 63 68 61 6e 67 65 73 20 77 69 ck, 0 changes wi
14114 6c 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a 2a 20 ll be made. **
14115 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
14116 66 69 6c 65 2e 20 53 6f 20 74 68 65 72 65 20 69 file. So there i
14117 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a 65 72 s no need to zer
14118 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 o the journal .
14119 20 2a 2a 20 68 65 61 64 65 72 2e 20 53 69 6e 63 ** header. Sinc
1411a 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 e the pager is i
1411b 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
1411c 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 , there is no ne
1411d 65 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f 70 20 ed. ** to drop
1411e 61 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68 65 72 any locks either
1411f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 .. */. if( pPa
14120 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d ger->dbModified=
14121 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78 =0 && pPager->ex
14122 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 clusiveMode .
14123 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
14124 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
14125 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
14126 54 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 65 72 T. ){. asser
14127 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e t( pPager->journ
14128 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 alOff==JOURNAL_H
14129 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b DR_SZ(pPager) );
1412a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1412b 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 TE_OK;. }.. PA
1412c 47 45 52 54 52 41 43 45 28 28 22 43 4f 4d 4d 49 GERTRACE(("COMMI
1412d 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 T %d\n", PAGERID
1412e 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 73 (pPager)));. as
1412f 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 sert( pPager->st
14130 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 ate==PAGER_SYNCE
14131 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70 D || MEMDB || !p
14132 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
14133 64 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65 d );. rc = page
14134 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
14135 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 n(pPager, pPager
14136 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 ->setMaster);.
14137 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 return pager_err
14138 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
14139 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 }../*.** Rollbac
1413a 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e 20 54 k all changes. T
1413b 68 65 20 64 61 74 61 62 61 73 65 20 66 61 6c 6c he database fall
1413c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f s back to PAGER_
1413d 53 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a SHARED mode..**.
1413e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
1413f 20 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 74 61 performs two ta
14140 73 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 sks:.**.** 1)
14141 49 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 It rolls back th
14142 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 e journal file,
14143 72 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20 64 61 restoring all da
14144 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
14145 0a 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65 6d 6f .** in-memo
14146 72 79 20 63 61 63 68 65 20 70 61 67 65 73 20 74 ry cache pages t
14147 6f 20 74 68 65 20 73 74 61 74 65 20 74 68 65 79 o the state they
14148 20 77 65 72 65 20 69 6e 20 77 68 65 6e 20 74 68 were in when th
14149 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
1414a 20 20 20 20 20 20 77 61 73 20 6f 70 65 6e 65 64 was opened
1414b 2c 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20 49 74 , and.** 2) It
1414c 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a finalizes the j
1414d 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 ournal file, so
1414e 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 75 that it is not u
1414f 73 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 sed for hot.**
14150 20 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 rollback at
14151 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 any point in the
14152 20 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 future..**.** s
14153 75 62 6a 65 63 74 20 74 6f 20 74 68 65 20 66 6f ubject to the fo
14154 6c 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66 69 63 llowing qualific
14155 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 ations:.**.** *
14156 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 If the journal f
14157 69 6c 65 20 69 73 20 6e 6f 74 20 79 65 74 20 6f ile is not yet o
14158 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 pen when this fu
14159 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1415a 2c 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e 6c 79 ,.** then only
1415b 20 28 32 29 20 69 73 20 70 65 72 66 6f 72 6d 65 (2) is performe
1415c 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 d. In this case
1415d 74 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f 75 72 there is no jour
1415e 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 74 6f nal file.** to
1415f 20 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a roll back..**.*
14160 2a 20 2a 20 49 66 20 69 6e 20 61 6e 20 65 72 72 * * If in an err
14161 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 or state other t
14162 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2c han SQLITE_FULL,
14163 20 74 68 65 6e 20 74 61 73 6b 20 28 31 29 20 69 then task (1) i
14164 73 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72 6d 65 s .** performe
14165 64 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c d. If successful
14166 2c 20 74 61 73 6b 20 28 32 29 2e 20 52 65 67 61 , task (2). Rega
14167 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75 rdless of the ou
14168 74 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20 65 69 tcome.** of ei
14169 74 68 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 ther, the error
1416a 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 state error code
1416b 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
1416c 74 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 the caller.**
1416d 28 69 2e 65 2e 20 65 69 74 68 65 72 20 53 51 4c (i.e. either SQL
1416e 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 53 51 4c ITE_IOERR or SQL
1416f 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a 2a ITE_CORRUPT)..**
14170 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 70 61 67 .** * If the pag
14171 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 52 er is in PAGER_R
14172 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 ESERVED state, t
14173 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e hen attempt (1).
14174 20 57 68 65 74 68 65 72 0a 2a 2a 20 20 20 6f 72 Whether.** or
14175 20 6e 6f 74 20 28 31 29 20 69 73 20 73 75 63 63 not (1) is succ
14176 75 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61 74 74 ussful, also att
14177 65 6d 70 74 20 28 32 29 2e 20 49 66 20 73 75 63 empt (2). If suc
14178 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a cessful, return.
14179 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 ** SQLITE_OK.
1417a 4f 74 68 65 72 77 69 73 65 2c 20 65 6e 74 65 72 Otherwise, enter
1417b 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
1417c 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 and return the
1417d 66 69 72 73 74 20 0a 2a 2a 20 20 20 65 72 72 6f first .** erro
1417e 72 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74 65 72 r code encounter
1417f 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 ed. .**.** In
14180 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 this case there
14181 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 is no chance tha
14182 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 t the database w
14183 61 73 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 0a as written to. .
14184 2a 2a 20 20 20 53 6f 20 69 73 20 73 61 66 65 20 ** So is safe
14185 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 to finalize the
14186 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 76 65 journal file eve
14187 6e 20 69 66 20 74 68 65 20 70 6c 61 79 62 61 63 n if the playbac
14188 6b 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61 74 69 k .** (operati
14189 6f 6e 20 31 29 20 66 61 69 6c 65 64 2e 20 48 6f on 1) failed. Ho
1418a 77 65 76 65 72 20 74 68 65 20 70 61 67 65 72 20 wever the pager
1418b 6d 75 73 74 20 65 6e 74 65 72 20 74 68 65 20 65 must enter the e
1418c 72 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20 20 20 rror state.**
1418d 61 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 as the contents
1418e 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 of the in-memory
1418f 20 63 61 63 68 65 20 61 72 65 20 6e 6f 77 20 73 cache are now s
14190 75 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 uspect..**.** *
14191 46 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e 20 50 Finally, if in P
14192 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 73 AGER_EXCLUSIVE s
14193 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d tate, then attem
14194 70 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 pt (1). Only.**
14195 20 20 61 74 74 65 6d 70 74 20 28 32 29 20 69 66 attempt (2) if
14196 20 28 31 29 20 69 73 20 73 75 63 63 65 73 73 66 (1) is successf
14197 75 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 ul. Return SQLIT
14198 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 E_OK if successf
14199 75 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72 77 69 ul,.** otherwi
1419a 73 65 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 se enter the err
1419b 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 or state and ret
1419c 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f urn the error co
1419d 64 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 de from the .**
1419e 20 20 66 61 69 6c 69 6e 67 20 6f 70 65 72 61 74 failing operat
1419f 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 ion..**.** In
141a0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 64 61 this case the da
141a1 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 79 20 tabase file may
141a2 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
141a3 6e 20 74 6f 2e 20 53 6f 20 69 66 20 74 68 65 0a n to. So if the.
141a4 2a 2a 20 20 20 70 6c 61 79 62 61 63 6b 20 6f 70 ** playback op
141a5 65 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f 74 20 eration did not
141a6 73 75 63 63 65 65 64 20 69 74 20 77 6f 75 6c 64 succeed it would
141a7 20 6e 6f 74 20 62 65 20 73 61 66 65 20 74 6f 20 not be safe to
141a8 66 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 finalize.** th
141a9 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
141aa 49 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6c It needs to be l
141ab 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d eft in the file-
141ac 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a system so that.*
141ad 2a 20 20 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 * some other p
141ae 72 6f 63 65 73 73 20 63 61 6e 20 75 73 65 20 69 rocess can use i
141af 74 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 t to restore the
141b0 20 64 61 74 61 62 61 73 65 20 73 74 61 74 65 20 database state
141b1 28 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 (by.** hot-jou
141b2 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a rnal rollback)..
141b3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
141b4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
141b5 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 erRollback(Pager
141b6 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
141b7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
141b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
141b9 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
141ba 20 2a 2f 0a 20 20 50 41 47 45 52 54 52 41 43 45 */. PAGERTRACE
141bb 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e (("ROLLBACK %d\n
141bc 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
141bd 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70 50 61 r)));. if( !pPa
141be 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 ger->dbModified
141bf 7c 7c 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 || !isOpen(pPage
141c0 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 72 r->jfd) ){. r
141c1 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 c = pager_end_tr
141c2 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 ansaction(pPager
141c3 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 , pPager->setMas
141c4 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ter);. }else if
141c5 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
141c6 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 e && pPager->err
141c7 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c Code!=SQLITE_FUL
141c8 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 L ){. if( pPa
141c9 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
141ca 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 R_EXCLUSIVE ){.
141cb 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 79 62 pager_playb
141cc 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a ack(pPager, 0);.
141cd 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70 }. rc = p
141ce 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
141cf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
141d0 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
141d1 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
141d2 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b {. int rc2;
141d3 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
141d4 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 r_playback(pPage
141d5 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32 r, 0);. rc2
141d6 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 = pager_end_tra
141d7 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c nsaction(pPager,
141d8 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
141d9 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 er);. if( r
141da 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
141db 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 rc = rc2
141dc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
141dd 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
141de 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
141df 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d Pager, 0);. }
141e0 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d 44 42 .. if( !MEMDB
141e1 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
141e2 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 ->dbSizeValid =
141e3 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0;. }.. /*
141e4 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
141e5 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c urs during a ROL
141e6 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f LBACK, we can no
141e7 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68 longer trust th
141e8 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 63 e pager. ** c
141e9 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 ache. So call pa
141ea 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 ger_error() on t
141eb 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 he way out to ma
141ec 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a 20 20 ke any error .
141ed 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 2e ** persistent.
141ee 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
141ef 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 pager_error(pPa
141f0 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 ger, rc);. }.
141f1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
141f2 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
141f3 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 if the database
141f4 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 72 file is opened r
141f5 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 75 72 ead-only. Retur
141f6 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 74 68 n FALSE.** if th
141f7 65 20 64 61 74 61 62 61 73 65 20 69 73 20 28 69 e database is (i
141f8 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74 61 62 n theory) writab
141f9 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
141fa 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 IVATE u8 sqlite3
141fb 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 PagerIsreadonly(
141fc 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
141fd 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
141fe 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a >readOnly;.}../*
141ff 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
14200 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e umber of referen
14201 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 72 ces to the pager
14202 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14203 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
14204 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 agerRefcount(Pag
14205 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 er *pPager){. r
14206 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 eturn sqlite3Pca
14207 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
14208 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a er->pPCache);.}.
14209 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1420a 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 e number of refe
1420b 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 70 rences to the sp
1420c 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a 2f ecified page..*/
1420d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1420e 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
1420f 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 PageRefcount(DbP
14210 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 age *pPage){. r
14211 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63 61 eturn sqlite3Pca
14212 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28 chePageRefcount(
14213 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 pPage);.}..#ifde
14214 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a f SQLITE_TEST./*
14215 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14216 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 is used for tes
14217 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 ting and analysi
14218 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 s only..*/.SQLIT
14219 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a 73 E_PRIVATE int *s
1421a 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 73 qlite3PagerStats
1421b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
1421c 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 5b . static int a[
1421d 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73 71 11];. a[0] = sq
1421e 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
1421f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
14220 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 che);. a[1] = s
14221 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 qlite3PcachePage
14222 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 count(pPager->pP
14223 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20 3d Cache);. a[2] =
14224 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 sqlite3PcacheGe
14225 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65 tCachesize(pPage
14226 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 r->pPCache);. a
14227 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 [3] = pPager->db
14228 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 6e 74 SizeValid ? (int
14229 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ) pPager->dbSize
1422a 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 : -1;. a[4] =
1422b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b 0a 20 pPager->state;.
1422c 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[5] = pPager->
1422d 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 errCode;. a[6]
1422e 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 3b 0a = pPager->nHit;.
1422f 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 72 2d a[7] = pPager-
14230 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d 20 3d >nMiss;. a[8] =
14231 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 0; /* Used to
14232 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 66 6c be pPager->nOvfl
14233 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 50 61 */. a[9] = pPa
14234 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 61 5b ger->nRead;. a[
14235 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 57 10] = pPager->nW
14236 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e 20 61 rite;. return a
14237 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
14238 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
14239 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d this is an in-m
1423a 65 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a 2f 0a emory pager..*/.
1423b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1423c 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 nt sqlite3PagerI
1423d 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a 70 50 sMemdb(Pager *pP
1423e 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
1423f 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 MEMDB;.}../*.**
14240 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 Check that there
14241 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 6e 53 are at least nS
14242 61 76 65 70 6f 69 6e 74 20 73 61 76 65 70 6f 69 avepoint savepoi
14243 6e 74 73 20 6f 70 65 6e 2e 20 49 66 20 74 68 65 nts open. If the
14244 72 65 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e re are.** curren
14245 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 6e 53 tly less than nS
14246 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 avepoints open,
14247 74 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 then open one or
14248 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 73 more savepoints
14249 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70 20 74 .** to make up t
1424a 68 65 20 64 69 66 66 65 72 65 6e 63 65 2e 20 49 he difference. I
1424b 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
1424c 73 61 76 65 70 6f 69 6e 74 73 20 69 73 20 61 6c savepoints is al
1424d 72 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c 20 74 ready.** equal t
1424e 6f 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20 74 68 o nSavepoint, th
1424f 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
14250 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
14251 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 ** If a memory a
14252 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
14253 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 SQLITE_NOMEM is
14254 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e returned. If an
14255 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 error .** occur
14256 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 20 s while opening
14257 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 the sub-journal
14258 66 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20 49 4f file, then an IO
14259 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a error code is.*
1425a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 * returned. Othe
1425b 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b rwise, SQLITE_OK
1425c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1425d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
1425e 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e agerOpenSavepoin
1425f 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c t(Pager *pPager,
14260 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 29 int nSavepoint)
14261 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
14262 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
14263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14264 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
14265 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 20 3d int nCurrent =
14266 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
14267 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 int; /* C
14268 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
14269 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a savepoints */..
1426a 20 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 if( nSavepoint
1426b 3e 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50 61 >nCurrent && pPa
1426c 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 ger->useJournal
1426d 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 ){. int ii;
1426e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1426f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14270 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 * Iterator varia
14271 62 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 ble */. Pager
14272 53 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b Savepoint *aNew;
14273 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14274 20 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e /* New Pager.
14275 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 aSavepoint array
14276 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 69 74 68 */.. /* Eith
14277 65 72 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 er there is no a
14278 63 74 69 76 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 ctive journal or
14279 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
1427a 20 69 73 20 6f 70 65 6e 20 6f 72 20 0a 20 20 20 is open or .
1427b 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ** the journal
1427c 69 73 20 61 6c 77 61 79 73 20 73 74 6f 72 65 64 is always stored
1427d 20 69 6e 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 in memory */.
1427e 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
1427f 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 ->nSavepoint==0
14280 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 || isOpen(pPager
14281 2d 3e 73 6a 66 64 29 20 7c 7c 0a 20 20 20 20 20 ->sjfd) ||.
14282 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
14283 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
14284 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
14285 4d 4f 52 59 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 MORY );.. /*
14286 47 72 6f 77 20 74 68 65 20 50 61 67 65 72 2e 61 Grow the Pager.a
14287 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20 Savepoint array
14288 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 2e using realloc().
14289 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e Return SQLITE_N
1428a 4f 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66 20 74 OMEM. ** if t
1428b 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 he allocation fa
1428c 69 6c 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ils. Otherwise,
1428d 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 70 6f 72 zero the new por
1428e 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 61 20 0a tion in case a .
1428f 20 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 ** malloc fa
14290 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 ilure occurs whi
14291 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 69 74 le populating it
14292 20 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29 in the for(...)
14293 20 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 20 20 loop below..
14294 20 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d 20 28 */. aNew = (
14295 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
14296 29 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 )sqlite3Realloc(
14297 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
14298 3e 61 53 61 76 65 70 6f 69 6e 74 2c 20 73 69 7a >aSavepoint, siz
14299 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 eof(PagerSavepoi
1429a 6e 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74 0a 20 nt)*nSavepoint.
1429b 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 61 );. if( !a
1429c 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 72 65 74 New ){. ret
1429d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1429e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 ;. }. mems
1429f 65 74 28 26 61 4e 65 77 5b 6e 43 75 72 72 65 6e et(&aNew[nCurren
142a0 74 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70 6f 69 t], 0, (nSavepoi
142a1 6e 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a 20 73 nt-nCurrent) * s
142a2 69 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 izeof(PagerSavep
142a3 6f 69 6e 74 29 29 3b 0a 20 20 20 20 70 50 61 67 oint));. pPag
142a4 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d er->aSavepoint =
142a5 20 61 4e 65 77 3b 0a 20 20 20 20 70 50 61 67 65 aNew;. pPage
142a6 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 r->nSavepoint =
142a7 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 nSavepoint;..
142a8 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 /* Populate the
142a9 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
142aa 73 74 72 75 63 74 75 72 65 73 20 6a 75 73 74 20 structures just
142ab 61 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 20 20 allocated. */.
142ac 20 20 66 6f 72 28 69 69 3d 6e 43 75 72 72 65 6e for(ii=nCurren
142ad 74 3b 20 69 69 3c 6e 53 61 76 65 70 6f 69 6e 74 t; ii<nSavepoint
142ae 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 ; ii++){. a
142af 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
142b0 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 bSizeValid );.
142b1 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 aNew[ii].nOr
142b2 69 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ig = pPager->dbS
142b3 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 69 ize;. if( i
142b4 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
142b5 64 29 20 26 26 20 41 4c 57 41 59 53 28 70 50 61 d) && ALWAYS(pPa
142b6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e ger->journalOff>
142b7 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4e 0) ){. aN
142b8 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d ew[ii].iOffset =
142b9 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
142ba 4f 66 66 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 Off;. }else
142bb 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 {. aNew[i
142bc 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a 4f 55 i].iOffset = JOU
142bd 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
142be 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 er);. }.
142bf 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 53 75 62 aNew[ii].iSub
142c0 52 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 Rec = pPager->nS
142c1 75 62 52 65 63 3b 0a 20 20 20 20 20 20 61 4e 65 ubRec;. aNe
142c2 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 w[ii].pInSavepoi
142c3 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 nt = sqlite3Bitv
142c4 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d ecCreate(pPager-
142c5 3e 64 62 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 >dbSize);.
142c6 69 66 28 20 21 61 4e 65 77 5b 69 69 5d 2e 70 49 if( !aNew[ii].pI
142c7 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 nSavepoint ){.
142c8 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
142c9 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
142ca 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
142cb 20 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f Open the sub-jo
142cc 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 urnal, if it is
142cd 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e not already open
142ce 65 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ed. */. rc =
142cf 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 openSubJournal(p
142d0 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65 Pager);. asse
142d1 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 rtTruncateConstr
142d2 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20 aint(pPager);.
142d3 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
142d4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
142d5 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
142d6 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 to rollback or
142d7 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 release (commit)
142d8 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a a savepoint..**
142d9 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 The savepoint t
142da 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c o release or rol
142db 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 lback need not b
142dc 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e e the most recen
142dd 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 tly .** created
142de 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a savepoint..**.**
142df 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73 Parameter op is
142e0 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53 always either S
142e1 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
142e2 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 K or SAVEPOINT_R
142e3 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 ELEASE..** If it
142e4 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 is SAVEPOINT_RE
142e5 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 LEASE, then rele
142e6 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20 ase and destroy
142e7 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69 the savepoint wi
142e8 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 th.** index iSav
142e9 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73 epoint. If it is
142ea 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
142eb 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 ACK, then rollba
142ec 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a ck all changes.*
142ed 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 * that have occu
142ee 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73 rred since the s
142ef 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 pecified savepoi
142f0 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a nt was created..
142f1 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f **.** The savepo
142f2 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 int to rollback
142f3 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64 or release is id
142f4 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 entified by para
142f5 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 meter .** iSavep
142f6 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 oint. A value of
142f7 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 0 means to oper
142f8 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 ate on the outer
142f9 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a most savepoint.*
142fa 2a 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65 * (the first cre
142fb 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f ated). A value o
142fc 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f f (Pager.nSavepo
142fd 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 int-1) means ope
142fe 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d rate.** on the m
142ff 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 ost recently cre
14300 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 ated savepoint.
14301 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 If iSavepoint is
14302 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
14303 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 (Pager.nSavepoi
14304 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73 nt-1), then this
14305 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
14306 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 o-op..**.** If a
14307 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 negative value
14308 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 is passed to thi
14309 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e s function, then
1430a 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
1430b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 transaction is r
1430c 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 olled back. This
1430d 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f is different to
1430e 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c calling .** sql
1430f 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
14310 6b 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73 k() because this
14311 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e function does n
14312 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 ot terminate.**
14313 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
14314 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 or unlock the da
14315 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20 tabase, it just
14316 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a restores the .**
14317 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
14318 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 database to its
14319 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e original state.
1431a 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 .**.** In any c
1431b 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 ase, all savepoi
1431c 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65 nts with an inde
1431d 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 x greater than i
1431e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 Savepoint .** ar
1431f 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20 e destroyed. If
14320 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 this is a releas
14321 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d e operation (op=
14322 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
14323 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 SE),.** then sav
14324 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e epoint iSavepoin
14325 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f t is also destro
14326 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 yed..**.** This
14327 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 function may ret
14328 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
14329 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c if a memory all
1432a 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a ocation fails,.*
1432b 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 * or an IO error
1432c 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65 code if an IO e
1432d 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
1432e 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 e rolling back a
1432f 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 .** savepoint.
14330 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 If no errors occ
14331 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 ur, SQLITE_OK is
14332 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 returned..*/ .S
14333 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14334 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
14335 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
14336 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
14337 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a nt iSavepoint){.
14338 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
14339 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 E_OK;.. assert(
1433a 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
1433b 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 ELEASE || op==SA
1433c 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
1433d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 );. assert( iS
1433e 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f avepoint>=0 || o
1433f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
14340 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 LBACK );.. if(
14341 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 iSavepoint<pPage
14342 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b r->nSavepoint ){
14343 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 . int ii;
14344 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
14345 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a tor variable */.
14346 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 int nNew;
14347 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14348 20 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 of remaining sa
14349 76 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 vepoints after t
1434a 68 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 his op. */..
1434b 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f /* Figure out ho
1434c 77 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 w many savepoint
1434d 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 s will still be
1434e 61 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 active after thi
1434f 73 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 s. ** operati
14350 6f 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 on. Store this v
14351 61 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 alue in nNew. Th
14352 65 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 en free resource
14353 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 s associated .
14354 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 ** with any sa
14355 76 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 vepoints that ar
14356 65 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 e destroyed by t
14357 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 his operation..
14358 20 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d */. nNew =
14359 20 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f iSavepoint + (o
1435a 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
1435b 4c 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 LBACK);. for(
1435c 69 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 ii=nNew; ii<pPag
1435d 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 er->nSavepoint;
1435e 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c ii++){. sql
1435f 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
14360 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 y(pPager->aSavep
14361 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 oint[ii].pInSave
14362 70 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 point);. }.
14363 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 pPager->nSavep
14364 6f 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 oint = nNew;..
14365 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
14366 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 a rollback opera
14367 74 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 tion, playback t
14368 68 65 20 73 70 65 63 69 66 69 65 64 20 73 61 76 he specified sav
14369 65 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 epoint.. ** I
1436a 66 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 f this is a temp
1436b 2d 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 -file, it is pos
1436c 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 6a sible that the j
1436d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a ournal file has.
1436e 20 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 ** not yet b
1436f 65 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 een opened. In t
14370 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 68 his case there h
14371 61 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e ave been no chan
14372 67 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 ges to. ** th
14373 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
14374 20 73 6f 20 74 68 65 20 70 6c 61 79 62 61 63 6b so the playback
14375 20 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 operation can b
14376 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a e skipped.. *
14377 2f 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 /. if( op==SA
14378 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
14379 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 && isOpen(pPage
1437a 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
1437b 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 PagerSavepoint
1437c 2a 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e *pSavepoint = (n
1437d 4e 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 New==0)?0:&pPage
1437e 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e r->aSavepoint[nN
1437f 65 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 ew-1];. rc
14380 3d 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 = pagerPlaybackS
14381 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c avepoint(pPager,
14382 20 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 pSavepoint);.
14383 20 20 20 20 61 73 73 65 72 74 28 72 63 21 3d 53 assert(rc!=S
14384 51 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 QLITE_DONE);.
14385 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 }. . /* If
14386 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73 this is a releas
14387 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 6d 6f e of the outermo
14388 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 st savepoint, tr
14389 75 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 uncate . ** t
1438a 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 he sub-journal t
1438b 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 o zero bytes in
1438c 73 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 size. */. if(
1438d 20 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d nNew==0 && op==
1438e 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
1438f 45 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 E && isOpen(pPag
14390 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 er->sjfd) ){.
14391 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
14392 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 QLITE_OK );.
14393 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
14394 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
14395 3e 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 >sjfd, 0);.
14396 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 pPager->nSubRec
14397 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
14398 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
14399 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1439a 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f full pathname o
1439b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
1439c 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile..*/.SQLITE_P
1439d 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
1439e 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 r *sqlite3PagerF
1439f 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 ilename(Pager *p
143a0 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
143a1 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
143a2 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 me;.}../*.** Ret
143a3 75 72 6e 20 74 68 65 20 56 46 53 20 73 74 72 75 urn the VFS stru
143a4 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 70 61 cture for the pa
143a5 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ger..*/.SQLITE_P
143a6 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c RIVATE const sql
143a7 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 ite3_vfs *sqlite
143a8 33 50 61 67 65 72 56 66 73 28 50 61 67 65 72 20 3PagerVfs(Pager
143a9 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
143aa 72 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b rn pPager->pVfs;
143ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
143ac 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 the file handle
143ad 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
143ae 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 e file associate
143af 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 70 61 d.** with the pa
143b0 67 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74 ger. This might
143b1 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 return NULL if
143b2 74 68 65 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 the file has.**
143b3 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 not yet been ope
143b4 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
143b5 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 66 RIVATE sqlite3_f
143b6 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 ile *sqlite3Page
143b7 72 46 69 6c 65 28 50 61 67 65 72 20 2a 70 50 61 rFile(Pager *pPa
143b8 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ger){. return p
143b9 50 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a Pager->fd;.}../*
143ba 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 .** Return the f
143bb 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 ull pathname of
143bc 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
143bd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
143be 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
143bf 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
143c0 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 nalname(Pager *p
143c1 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
143c2 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
143c3 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 l;.}../*.** Retu
143c4 72 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63 rn true if fsync
143c5 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73 () calls are dis
143c6 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70 abled for this p
143c7 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41 ager. Return FA
143c8 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 LSE.** if fsync(
143c9 29 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20 )s are executed
143ca 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c normally..*/.SQL
143cb 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
143cc 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 sqlite3PagerNosy
143cd 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
143ce 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
143cf 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 er->noSync;.}..#
143d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
143d1 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 _CODEC./*.** Set
143d2 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 65 or retrieve the
143d3 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73 20 codec for this
143d4 70 61 67 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 pager.*/.static
143d5 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
143d6 72 53 65 74 43 6f 64 65 63 28 0a 20 20 50 61 67 rSetCodec(. Pag
143d7 65 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 76 6f er *pPager,. vo
143d8 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f id *(*xCodec)(vo
143d9 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 id*,void*,Pgno,i
143da 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 nt),. void (*xC
143db 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f odecSizeChng)(vo
143dc 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 id*,int,int),.
143dd 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 void (*xCodecFre
143de 65 29 28 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 e)(void*),. voi
143df 64 20 2a 70 43 6f 64 65 63 0a 29 7b 0a 20 20 69 d *pCodec.){. i
143e0 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 f( pPager->xCode
143e1 63 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e cFree ) pPager->
143e2 78 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 xCodecFree(pPage
143e3 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 20 20 70 50 r->pCodec);. pP
143e4 61 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d 20 70 ager->xCodec = p
143e5 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3f 20 30 Pager->memDb ? 0
143e6 20 3a 20 78 43 6f 64 65 63 3b 0a 20 20 70 50 61 : xCodec;. pPa
143e7 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 ger->xCodecSizeC
143e8 68 6e 67 20 3d 20 78 43 6f 64 65 63 53 69 7a 65 hng = xCodecSize
143e9 43 68 6e 67 3b 0a 20 20 70 50 61 67 65 72 2d 3e Chng;. pPager->
143ea 78 43 6f 64 65 63 46 72 65 65 20 3d 20 78 43 6f xCodecFree = xCo
143eb 64 65 63 46 72 65 65 3b 0a 20 20 70 50 61 67 65 decFree;. pPage
143ec 72 2d 3e 70 43 6f 64 65 63 20 3d 20 70 43 6f 64 r->pCodec = pCod
143ed 65 63 3b 0a 20 20 70 61 67 65 72 52 65 70 6f 72 ec;. pagerRepor
143ee 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 7d tSize(pPager);.}
143ef 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 .static void *sq
143f0 6c 69 74 65 33 50 61 67 65 72 47 65 74 43 6f 64 lite3PagerGetCod
143f1 65 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ec(Pager *pPager
143f2 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 ){. return pPag
143f3 65 72 2d 3e 70 43 6f 64 65 63 3b 0a 7d 0a 23 65 er->pCodec;.}.#e
143f4 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
143f5 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
143f6 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 CUUM./*.** Move
143f7 74 68 65 20 70 61 67 65 20 70 50 67 20 74 6f 20 the page pPg to
143f8 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e location pgno in
143f9 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
143fa 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e There must be n
143fb 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 o references to
143fc 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 the page previou
143fd 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a sly located at.*
143fe 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20 77 65 * pgno (which we
143ff 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 call pPgOld) th
14400 6f 75 67 68 20 74 68 61 74 20 70 61 67 65 20 69 ough that page i
14401 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a s allowed to be.
14402 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20 49 66 ** in cache. If
14403 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69 6f the page previo
14404 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 20 usly located at
14405 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 pgno is not alre
14406 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f ady.** in the ro
14407 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 llback journal,
14408 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20 74 68 it is not put th
14409 65 72 65 20 62 79 20 62 79 20 74 68 69 73 20 72 ere by by this r
1440a 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 outine..**.** Re
1440b 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
1440c 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 6e 20 page pPg remain
1440d 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e 67 20 valid. Updating
1440e 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 any.** meta-data
1440f 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
14410 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74 61 20 pPg (i.e. data
14411 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 45 stored in the nE
14412 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c xtra bytes.** al
14413 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 located along wi
14414 74 68 20 74 68 65 20 70 61 67 65 29 20 69 73 20 th the page) is
14415 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 the responsibili
14416 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 ty of the caller
14417 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 ..**.** A transa
14418 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 ction must be ac
14419 74 69 76 65 20 77 68 65 6e 20 74 68 69 73 20 72 tive when this r
1441a 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1441b 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 0a . It used to be.
1441c 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68 61 74 ** required that
1441d 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 a statement tra
1441e 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 nsaction was not
1441f 20 61 63 74 69 76 65 2c 20 62 75 74 20 74 68 69 active, but thi
14420 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a s restriction.**
14421 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 has been remove
14422 64 20 28 43 52 45 41 54 45 20 49 4e 44 45 58 20 d (CREATE INDEX
14423 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 needs to move a
14424 70 61 67 65 20 77 68 65 6e 20 61 20 73 74 61 74 page when a stat
14425 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 ement.** transac
14426 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 29 2e tion is active).
14427 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f .**.** If the fo
14428 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 urth argument, i
14429 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d sCommit, is non-
1442a 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 zero, then this
1442b 70 61 67 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a page is being.**
1442c 20 6d 6f 76 65 64 20 61 73 20 70 61 72 74 20 6f moved as part o
1442d 66 20 61 20 64 61 74 61 62 61 73 65 20 72 65 6f f a database reo
1442e 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 rganization just
1442f 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e before the tran
14430 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 saction .** is b
14431 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 eing committed.
14432 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 69 74 In this case, it
14433 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
14434 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
14435 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 page .** pPg re
14436 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 fers to will not
14437 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 61 be written to a
14438 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 gain within this
14439 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
1443a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1443b 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c n may return SQL
1443c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 ITE_NOMEM or an
1443d 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 IO error code if
1443e 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 an error.** occ
1443f 75 72 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 urs. Otherwise,
14440 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 it returns SQLIT
14441 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
14442 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14443 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 te3PagerMovepage
14444 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
14445 44 62 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e DbPage *pPg, Pgn
14446 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f o pgno, int isCo
14447 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a mmit){. PgHdr *
14448 70 50 67 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 pPgOld;
14449 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
1444a 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 e being overwrit
1444b 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e ten. */. Pgno n
1444c 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b eedSyncPgno = 0;
1444d 20 20 20 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61 /* Old va
1444e 6c 75 65 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f lue of pPg->pgno
1444f 2c 20 69 66 20 73 79 6e 63 20 69 73 20 72 65 71 , if sync is req
14450 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 uired */. int r
14451 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
14452 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
14453 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f n code */. Pgno
14454 20 6f 72 69 67 50 67 6e 6f 3b 20 20 20 20 20 20 origPgno;
14455 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
14456 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 6e 75 original page nu
14457 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 mber */.. asser
14458 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 t( pPg->nRef>0 )
14459 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6f 72 64 65 72 ;.. /* In order
1445a 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 72 to be able to r
1445b 6f 6c 6c 62 61 63 6b 2c 20 61 6e 20 69 6e 2d 6d ollback, an in-m
1445c 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 6d emory database m
1445d 75 73 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a ust journal. **
1445e 20 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 the page we are
1445f 20 6d 6f 76 69 6e 67 20 66 72 6f 6d 2e 0a 20 20 moving from..
14460 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 */. if( MEMDB )
14461 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
14462 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67 e3PagerWrite(pPg
14463 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
14464 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
14465 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 /* If the page
14466 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 73 20 being moved is
14467 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 6e 6f dirty and has no
14468 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 79 20 t been saved by
14469 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a 2a 20 the latest. **
1446a 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 savepoint, then
1446b 73 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 save the current
1446c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1446d 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 0a page into the .
1446e 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c ** sub-journal
1446f 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 72 65 now. This is re
14470 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 quired to handle
14471 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
14472 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a 20 20 cenario:. **.
14473 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a ** BEGIN;. **
14474 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 <journal pa
14475 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 69 66 ge X, then modif
14476 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 3e 0a y it in memory>.
14477 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 ** SAVEPOI
14478 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 20 20 NT one;. **
14479 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 58 20 <Move page X
1447a 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e 0a 20 to location Y>.
1447b 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b ** ROLLBACK
1447c 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 TO one;. **.
1447d 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 65 72 ** If page X wer
1447e 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f e not written to
1447f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
14480 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c 64 20 here, it would
14481 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f 73 73 not. ** be poss
14482 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 65 20 ible to restore
14483 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 68 65 its contents whe
14484 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 n the "ROLLBACK
14485 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 74 61 TO one". ** sta
14486 74 65 6d 65 6e 74 20 77 65 72 65 20 69 73 20 70 tement were is p
14487 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2a 0a 20 rocessed.. **.
14488 20 2a 2a 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 ** subjournalPa
14489 67 65 28 29 20 6d 61 79 20 6e 65 65 64 20 74 6f ge() may need to
1448a 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 allocate space
1448b 74 6f 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 to store pPg->pg
1448c 6e 6f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f 6e 65 no into. ** one
1448d 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 or more savepoi
1448e 6e 74 20 62 69 74 76 65 63 73 2e 20 54 68 69 73 nt bitvecs. This
1448f 20 69 73 20 74 68 65 20 72 65 61 73 6f 6e 20 74 is the reason t
14490 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a his function. *
14491 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c * may return SQL
14492 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a ITE_NOMEM.. */.
14493 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 if( pPg->flags
14494 26 50 47 48 44 52 5f 44 49 52 54 59 0a 20 20 20 &PGHDR_DIRTY.
14495 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 && subjRequiresP
14496 61 67 65 28 70 50 67 29 0a 20 20 20 26 26 20 53 age(pPg). && S
14497 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 QLITE_OK!=(rc =
14498 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 subjournalPage(p
14499 50 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 Pg)). ){. re
1449a 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
1449b 50 41 47 45 52 54 52 41 43 45 28 28 22 4d 4f 56 PAGERTRACE(("MOV
1449c 45 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 E %d page %d (ne
1449d 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 edSync=%d) moves
1449e 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 to %d\n", .
1449f 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
144a0 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 ), pPg->pgno, (p
144a1 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
144a2 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 NEED_SYNC)?1:0,
144a3 70 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 41 43 pgno));. IOTRAC
144a4 45 28 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 E(("MOVE %p %d %
144a5 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 d\n", pPager, pP
144a6 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a g->pgno, pgno)).
144a7 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 . /* If the jou
144a8 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 rnal needs to be
144a9 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 sync()ed before
144aa 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 page pPg->pgno
144ab 63 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 69 74 can. ** be writ
144ac 74 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 70 50 ten to, store pP
144ad 67 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c g->pgno in local
144ae 20 76 61 72 69 61 62 6c 65 20 6e 65 65 64 53 79 variable needSy
144af 6e 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a ncPgno.. **. *
144b0 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d 6d 69 * If the isCommi
144b1 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 t flag is set, t
144b2 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
144b3 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 74 to remember that
144b4 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 . ** the journa
144b5 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 l needs to be sy
144b6 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 nc()ed before da
144b7 74 61 62 61 73 65 20 70 61 67 65 20 70 50 67 2d tabase page pPg-
144b8 3e 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 >pgno . ** can
144b9 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 54 be written to. T
144ba 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 6c he caller has al
144bb 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 6e ready promised n
144bc 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 69 ot to write to i
144bd 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 t.. */. if( (p
144be 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
144bf 4e 45 45 44 5f 53 59 4e 43 29 20 26 26 20 21 69 NEED_SYNC) && !i
144c0 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 6e sCommit ){. n
144c1 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 70 50 eedSyncPgno = pP
144c2 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 73 73 g->pgno;. ass
144c3 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e ert( pageInJourn
144c4 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e al(pPg) || pPg->
144c5 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f pgno>pPager->dbO
144c6 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 61 rigSize );. a
144c7 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 ssert( pPg->flag
144c8 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b s&PGHDR_DIRTY );
144c9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
144ca 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b ger->needSync );
144cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
144cc 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 73 e cache contains
144cd 20 61 20 70 61 67 65 20 77 69 74 68 20 70 61 67 a page with pag
144ce 65 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c 20 72 e-number pgno, r
144cf 65 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 66 72 emove it. ** fr
144d0 6f 6d 20 69 74 73 20 68 61 73 68 20 63 68 61 69 om its hash chai
144d1 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 65 20 n. Also, if the
144d2 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 77 PgHdr.needSync w
144d3 61 73 20 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a as set for . **
144d4 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 page pgno befor
144d5 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 e the 'move' ope
144d6 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 ration, it needs
144d7 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65 64 20 to be retained
144d8 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 . ** for the pa
144d9 67 65 20 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a ge moved there..
144da 20 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 */. pPg->flag
144db 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 s &= ~PGHDR_NEED
144dc 5f 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 _SYNC;. pPgOld
144dd 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 = pager_lookup(p
144de 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 Pager, pgno);.
144df 61 73 73 65 72 74 28 20 21 70 50 67 4f 6c 64 20 assert( !pPgOld
144e0 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d || pPgOld->nRef=
144e1 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 50 67 4f =1 );. if( pPgO
144e2 6c 64 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 ld ){. pPg->f
144e3 6c 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d lags |= (pPgOld-
144e4 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
144e5 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 D_SYNC);. if(
144e6 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 MEMDB ){.
144e7 2f 2a 20 44 6f 20 6e 6f 74 20 64 69 73 63 61 72 /* Do not discar
144e8 64 20 70 61 67 65 73 20 66 72 6f 6d 20 61 6e 20 d pages from an
144e9 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
144ea 73 65 20 73 69 6e 63 65 20 77 65 20 6d 69 67 68 se since we migh
144eb 74 0a 20 20 20 20 20 20 2a 2a 20 6e 65 65 64 20 t. ** need
144ec 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6c 61 74 65 to rollback late
144ed 72 2e 20 20 4a 75 73 74 20 6d 6f 76 65 20 74 68 r. Just move th
144ee 65 20 70 61 67 65 20 6f 75 74 20 6f 66 20 74 68 e page out of th
144ef 65 20 77 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20 e way. */.
144f0 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
144f1 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 dbSizeValid );.
144f2 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 sqlite3Pcac
144f3 68 65 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 70 heMove(pPgOld, p
144f4 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b 31 29 Pager->dbSize+1)
144f5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
144f6 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
144f7 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a 20 20 Drop(pPgOld);.
144f8 20 20 7d 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 }. }.. origP
144f9 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
144fa 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
144fb 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b Move(pPg, pgno);
144fc 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
144fd 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a MakeDirty(pPg);.
144fe 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
144ff 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 fied = 1;.. if(
14500 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b needSyncPgno ){
14501 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 . /* If needS
14502 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a yncPgno is non-z
14503 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f ero, then the jo
14504 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 urnal file needs
14505 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 to be . ** s
14506 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 61 ync()ed before a
14507 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74 74 ny data is writt
14508 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 en to database f
14509 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e ile page needSyn
1450a 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 cPgno.. ** Cu
1450b 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 rrently, no such
1450c 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e 20 page exists in
1450d 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 61 the page-cache a
1450e 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 nd the . ** "
1450f 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 is journaled" bi
14510 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 62 65 tvec flag has be
14511 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65 en set. This nee
14512 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65 ds to be remedie
14513 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 d by. ** load
14514 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 6e 74 ing the page int
14515 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 o the pager-cach
14516 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68 e and setting th
14517 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 e PgHdr.needSync
14518 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 . ** flag..
14519 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
1451a 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c the attempt to l
1451b 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 6e 74 oad the page int
1451c 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 o the page-cache
1451d 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 fails, (due.
1451e 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 ** to a malloc(
1451f 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29 ) or IO failure)
14520 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 74 20 , clear the bit
14521 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 in the pInJourna
14522 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 l[]. ** array
14523 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
14524 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64 the page is load
14525 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61 ed and written a
14526 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 gain in. ** t
14527 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c his transaction,
14528 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74 74 it may be writt
14529 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 en to the databa
1452a 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 se file before.
1452b 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 ** it is sync
1452c 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 ed into the jour
1452d 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77 nal file. This w
1452e 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75 ay, it may end u
1452f 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 p in. ** the
14530 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 journal file twi
14531 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 73 20 ce, but that is
14532 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 not a problem..
14533 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
14534 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
14535 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 () call may caus
14536 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f e the journal to
14537 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 sync. So make.
14538 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50 ** sure the P
14539 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c ager.needSync fl
1453a 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 ag is set too..
1453b 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 */. PgHdr
1453c 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 *pPgHdr;. ass
1453d 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 ert( pPager->nee
1453e 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 dSync );. rc
1453f 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
14540 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 t(pPager, needSy
14541 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 ncPgno, &pPgHdr)
14542 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
14543 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
14544 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e if( needSyncPgn
14545 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 o<=pPager->dbOri
14546 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 gSize ){.
14547 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14548 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b >pTmpSpace!=0 );
14549 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1454a 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 61 67 BitvecClear(pPag
1454b 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 er->pInJournal,
1454c 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50 needSyncPgno, pP
1454d 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 ager->pTmpSpace)
1454e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1454f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
14550 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 . pPager->nee
14551 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 dSync = 1;. a
14552 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
14553 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d oSync==0 && !MEM
14554 44 42 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 DB );. pPgHdr
14555 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
14556 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
14557 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
14558 65 44 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a eDirty(pPgHdr);.
14559 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
1455a 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 Unref(pPgHdr);.
1455b 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f }.. /*. ** Fo
1455c 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 r an in-memory d
1455d 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75 atabase, make su
1455e 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 re the original
1455f 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20 page continues.
14560 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e ** to exist, in
14561 20 63 61 73 65 20 74 68 65 20 74 72 61 6e 73 61 case the transa
14562 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72 ction needs to r
14563 6f 6c 6c 20 62 61 63 6b 2e 20 20 55 73 65 20 70 oll back. Use p
14564 50 67 4f 6c 64 0a 20 20 2a 2a 20 61 73 20 74 68 PgOld. ** as th
14565 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 e original page
14566 73 69 6e 63 65 20 69 74 20 68 61 73 20 61 6c 72 since it has alr
14567 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 eady been alloca
14568 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ted.. */. if(
14569 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 73 71 6c MEMDB ){. sql
1456a 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 ite3PcacheMove(p
1456b 50 67 4f 6c 64 2c 20 6f 72 69 67 50 67 6e 6f 29 PgOld, origPgno)
1456c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
1456d 65 72 55 6e 72 65 66 28 70 50 67 4f 6c 64 29 3b erUnref(pPgOld);
1456e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 . }.. return S
1456f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 QLITE_OK;.}.#end
14570 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
14571 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
14572 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 73 e data for the s
14573 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a pecified page..*
14574 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14575 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
14576 67 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67 gerGetData(DbPag
14577 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 e *pPg){. asser
14578 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 20 7c t( pPg->nRef>0 |
14579 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 6d | pPg->pPager->m
1457a 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 72 6e emDb );. return
1457b 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d 0a 0a pPg->pData;.}..
1457c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
1457d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 ointer to the Pa
1457e 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 65 73 ger.nExtra bytes
1457f 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 61 63 of "extra" spac
14580 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 e .** allocated
14581 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 73 along with the s
14582 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a pecified page..*
14583 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14584 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
14585 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61 gerGetExtra(DbPa
14586 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 ge *pPg){. retu
14587 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61 3b 0a rn pPg->pExtra;.
14588 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 }../*.** Get/set
14589 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 the locking-mod
1458a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 e for this pager
1458b 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 . Parameter eMod
1458c 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a 2a e must be one.**
1458d 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e of PAGER_LOCKIN
1458e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41 47 GMODE_QUERY, PAG
1458f 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e ER_LOCKINGMODE_N
14590 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41 47 ORMAL or .** PAG
14591 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 ER_LOCKINGMODE_E
14592 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68 65 XCLUSIVE. If the
14593 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
14594 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a 2a t _QUERY, then.*
14595 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f * the locking-mo
14596 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 de is set to the
14597 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 value specified
14598 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
14599 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65 69 rned value is ei
1459a 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ther PAGER_LOCKI
1459b 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 NGMODE_NORMAL or
1459c 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e .** PAGER_LOCKIN
1459d 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2c GMODE_EXCLUSIVE,
1459e 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 indicating the
1459f 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62 6c current (possibl
145a0 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c 6f y updated).** lo
145a1 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a 53 cking-mode..*/.S
145a2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
145a3 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f t sqlite3PagerLo
145a4 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 ckingMode(Pager
145a5 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f *pPager, int eMo
145a6 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 65 de){. assert( e
145a7 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b Mode==PAGER_LOCK
145a8 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20 INGMODE_QUERY.
145a9 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
145aa 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e de==PAGER_LOCKIN
145ab 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20 20 GMODE_NORMAL.
145ac 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 || eMod
145ad 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 e==PAGER_LOCKING
145ae 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29 MODE_EXCLUSIVE )
145af 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
145b0 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 R_LOCKINGMODE_QU
145b1 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65 72 ERY<0 );. asser
145b2 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 t( PAGER_LOCKING
145b3 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20 26 MODE_NORMAL>=0 &
145b4 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d & PAGER_LOCKINGM
145b5 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d 30 ODE_EXCLUSIVE>=0
145b6 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65 3e );. if( eMode>
145b7 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74 =0 && !pPager->t
145b8 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 70 empFile ){. p
145b9 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 Pager->exclusive
145ba 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 Mode = (u8)eMode
145bb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
145bc 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 6c int)pPager->excl
145bd 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a usiveMode;.}../*
145be 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 .** Get/set the
145bf 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72 journal-mode for
145c0 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 this pager. Par
145c1 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 ameter eMode mus
145c2 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a t be one of:.**.
145c3 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
145c4 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a 2a NALMODE_QUERY.**
145c5 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
145c6 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 LMODE_DELETE.**
145c7 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c PAGER_JOURNAL
145c8 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a 2a MODE_TRUNCATE.**
145c9 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
145ca 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a 2a LMODE_PERSIST.**
145cb 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 PAGER_JOURNA
145cc 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20 20 LMODE_OFF.**
145cd 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
145ce 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20 49 E_MEMORY.**.** I
145cf 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 f the parameter
145d0 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 is not _QUERY, t
145d1 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f hen the journal_
145d2 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 mode is set to t
145d3 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65 63 he.** value spec
145d4 69 66 69 65 64 20 69 66 20 74 68 65 20 63 68 61 ified if the cha
145d5 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 nge is allowed.
145d6 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 20 64 The change is d
145d7 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 6f 72 isallowed.** for
145d8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 the following r
145d9 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 easons:.**.**
145da 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 * An in-memory
145db 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f 6e 6c database can onl
145dc 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 72 6e y have its journ
145dd 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f 20 5f al_mode set to _
145de 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 20 5f OFF.** or _
145df 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 20 20 MEMORY..**.**
145e0 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 6d * The journal m
145e1 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 ode may not be c
145e2 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 20 74 hanged while a t
145e3 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 ransaction is ac
145e4 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tive..**.** The
145e5 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61 74 returned indicat
145e6 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 e the current (p
145e7 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 ossibly updated)
145e8 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a journal-mode..*
145e9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
145ea 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
145eb 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 rJournalMode(Pag
145ec 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
145ed 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 eMode){. assert
145ee 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a ( eMode==PAGER_J
145ef 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 OURNALMODE_QUERY
145f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
145f1 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
145f2 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a RNALMODE_DELETE.
145f3 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
145f4 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
145f5 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
145f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
145f7 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
145f8 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
145f9 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
145fa 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 eMode==PAGER_JOU
145fb 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 RNALMODE_OFF .
145fc 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f || eMo
145fd 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
145fe 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a LMODE_MEMORY );.
145ff 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f assert( PAGER_
14600 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 JOURNALMODE_QUER
14601 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f Y<0 );. if( eMo
14602 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 4d 45 de>=0. && (!ME
14603 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 MDB || eMode==PA
14604 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
14605 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 20 20 MEMORY .
14606 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d || eMode==
14607 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
14608 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 70 50 E_OFF). && !pP
14609 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
1460a 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 6e 28 . && (!isOpen(
1460b 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 pPager->jfd) ||
1460c 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 0==pPager->journ
1460d 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 20 20 alOff). ){.
1460e 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
1460f 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
14610 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
14611 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
14612 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
14613 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28 75 journalMode = (u
14614 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 8)eMode;. }. r
14615 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 eturn (int)pPage
14616 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a r->journalMode;.
14617 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 }../*.** Get/set
14618 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 74 20 the size-limit
14619 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 73 74 used for persist
1461a 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ent journal file
1461b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67 s..**.** Setting
1461c 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 74 20 the size limit
1461d 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f 20 6c to -1 means no l
1461e 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 65 64 imit is enforced
1461f 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 ..** An attempt
14620 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 20 73 to set a limit s
14621 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 20 69 maller than -1 i
14622 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 s a no-op..*/.SQ
14623 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
14624 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
14625 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 rnalSizeLimit(Pa
14626 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 36 34 ger *pPager, i64
14627 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28 20 iLimit){. if(
14628 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20 20 iLimit>=-1 ){.
14629 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
1462a 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c 69 lSizeLimit = iLi
1462b 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 mit;. }. retur
1462c 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 n pPager->journa
1462d 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f lSizeLimit;.}../
1462e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
1462f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50 61 inter to the pPa
14630 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 72 ger->pBackup var
14631 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b 75 iable. The backu
14632 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20 62 p module.** in b
14633 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 6e ackup.c maintain
14634 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 s the content of
14635 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e 20 this variable.
14636 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20 75 This module.** u
14637 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79 20 ses it opaquely
14638 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 as an argument t
14639 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 o sqlite3BackupR
1463a 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a 20 estart() and.**
1463b 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
1463c 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 ate() only..*/.S
1463d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
1463e 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 lite3_backup **s
1463f 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 qlite3PagerBacku
14640 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 61 67 pPtr(Pager *pPag
14641 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 70 er){. return &p
14642 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b 0a Pager->pBackup;.
14643 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
14644 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 ITE_OMIT_DISKIO
14645 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
14646 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72 *** End of pager
14647 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
14648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1464a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1464b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
1464c 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a tmutex.c *******
1464d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1464e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1464f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 **/./*.** 2007 A
14650 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20 54 ugust 27.**.** T
14651 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
14652 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
14653 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
14654 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
14655 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
14656 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
14657 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
14658 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
14659 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1465a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1465b 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1465c 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1465d 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1465e 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1465f 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
14660 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
14661 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
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 2a 2a 2a 2a 2a 2a 2a 2a ****************
14665 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14666 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
14667 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
14668 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d code used to im
14669 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 plement mutexes
1466a 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 on Btree objects
1466b 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 ..** This code r
1466c 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e eally belongs in
1466d 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 btree.c. But b
1466e 74 72 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e tree.c is gettin
1466f 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 g too.** big and
14670 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 we want to brea
14671 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 k it down some.
14672 20 54 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 This packaged s
14673 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 eemed like.** a
14674 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a good breakout..*
14675 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
14676 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 * Include btreeI
14677 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 nt.h in the midd
14678 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 le of btmutex.c
14679 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1467a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1467b 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 * Begin file btr
1467c 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a eeInt.h ********
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 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 /./*.** 2004 Apr
14680 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 il 6.**.** The a
14681 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
14682 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
14683 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
14684 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
14685 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
14686 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
14687 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
14688 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
14689 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
1468a 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
1468b 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
1468c 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
1468d 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
1468e 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
1468f 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
14690 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
14691 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
14692 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14693 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14694 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14695 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14696 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
14697 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 implements a ex
14698 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 ternal (disk-bas
14699 65 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 ed) database usi
1469a 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 46 6f ng BTrees..** Fo
1469b 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 69 73 r a detailed dis
1469c 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 65 65 cussion of BTree
1469d 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a 0a 2a s, refer to.**.*
1469e 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 2e 20 * Donald E.
1469f 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 20 4f Knuth, THE ART O
146a0 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f 47 52 F COMPUTER PROGR
146a1 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 20 33 AMMING, Volume 3
146a2 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 69 6e :.** "Sortin
146a3 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e 67 22 g And Searching"
146a4 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 30 2e , pages 473-480.
146a5 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 79 0a Addison-Wesley.
146a6 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 69 6e ** Publishin
146a7 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 64 69 g Company, Readi
146a8 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 74 74 ng, Massachusett
146a9 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 s..**.** The bas
146aa 69 63 20 69 64 65 61 20 69 73 20 74 68 61 74 20 ic idea is that
146ab 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 each page of the
146ac 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 4e file contains N
146ad 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 6e 74 database.** ent
146ae 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 6f 69 ries and N+1 poi
146af 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 67 65 nters to subpage
146b0 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d s..**.** -----
146b1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146b2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146b4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 -----------.**
146b5 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b 65 79 | Ptr(0) | Key
146b6 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c 20 4b (0) | Ptr(1) | K
146b7 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 4b 65 ey(1) | ... | Ke
146b8 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e 29 20 y(N-1) | Ptr(N)
146b9 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d |.** ---------
146ba 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146bb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146bc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
146bd 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c -------.**.** Al
146be 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e l of the keys on
146bf 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 50 the page that P
146c0 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 6f 20 tr(0) points to
146c1 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 73 73 have values less
146c2 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 29 2e .** than Key(0).
146c3 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 All of the key
146c4 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 31 29 s on page Ptr(1)
146c5 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 and its subpage
146c6 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 s have.** values
146c7 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 greater than Ke
146c8 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 74 68 y(0) and less th
146c9 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c 6c 20 an Key(1). All
146ca 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a 20 6f of the keys.** o
146cb 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 74 73 n Ptr(N) and its
146cc 20 73 75 62 70 61 67 65 73 20 68 61 76 65 20 76 subpages have v
146cd 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74 68 alues greater th
146ce 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 41 6e an Key(N-1). An
146cf 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e 0a 2a d.** so forth..*
146d0 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 20 70 *.** Finding a p
146d1 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 72 65 articular key re
146d2 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 20 4f quires reading O
146d3 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 20 66 (log(M)) pages f
146d4 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 73 6b rom the .** disk
146d5 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 65 20 where M is the
146d6 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 number of entrie
146d7 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a s in the tree..*
146d8 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 *.** In this imp
146d9 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 20 73 lementation, a s
146da 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e 20 68 ingle file can h
146db 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 old one or more
146dc 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 54 72 separate .** BTr
146dd 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 65 65 ees. Each BTree
146de 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 is identified b
146df 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 69 y the index of i
146e0 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 54 ts root page. T
146e1 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 he.** key and da
146e2 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 72 79 ta for any entry
146e3 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f are combined to
146e4 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 6c 6f form the "paylo
146e5 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 65 64 ad". A.** fixed
146e6 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f amount of paylo
146e7 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 69 65 ad can be carrie
146e8 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 74 68 d directly on th
146e9 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 e database.** pa
146ea 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 79 6c ge. If the payl
146eb 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 74 68 oad is larger th
146ec 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 61 6d an the preset am
146ed 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 6c 75 ount then surplu
146ee 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 20 73 s.** bytes are s
146ef 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 6c 6f tored on overflo
146f0 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 70 61 w pages. The pa
146f1 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 6e 74 yload for an ent
146f2 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 ry.** and the pr
146f3 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 72 20 eceding pointer
146f4 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 6f 20 are combined to
146f5 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e 20 20 form a "Cell".
146f6 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 68 61 Each .** page ha
146f7 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 s a small header
146f8 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 which contains
146f9 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 6e 74 the Ptr(N) point
146fa 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a 2a 20 er and other.**
146fb 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63 68 information such
146fc 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 as the size of
146fd 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a key and data..**
146fe 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 41 49 .** FORMAT DETAI
146ff 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c LS.**.** The fil
14700 65 20 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 e is divided int
14701 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 66 69 o pages. The fi
14702 72 73 74 20 70 61 67 65 20 69 73 20 63 61 6c 6c rst page is call
14703 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 74 68 ed page 1,.** th
14704 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 67 65 e second is page
14705 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2, and so forth
14706 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 65 72 . A page number
14707 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 61 74 of zero indicat
14708 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 20 70 es.** "no such p
14709 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 65 20 age". The page
1470a 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e 79 20 size can be any
1470b 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 power of 2 betwe
1470c 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 en 512 and 32768
1470d 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63 ..** Each page c
1470e 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 62 an be either a b
1470f 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 65 tree page, a fre
14710 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e 20 6f elist page, an o
14711 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c verflow.** page,
14712 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 or a pointer-ma
14713 70 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 p page..**.** Th
14714 65 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20 e first page is
14715 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 20 70 always a btree p
14716 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 age. The first
14717 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 68 65 100 bytes of the
14718 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 first.** page c
14719 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c ontain a special
1471a 20 68 65 61 64 65 72 20 28 74 68 65 20 22 66 69 header (the "fi
1471b 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 61 74 le header") that
1471c 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 66 describes the f
1471d 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d ile..** The form
1471e 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 at of the file h
1471f 65 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c eader is as foll
14720 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 ows:.**.** OFF
14721 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 44 45 SET SIZE DE
14722 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 SCRIPTION.**
14723 20 20 30 20 20 20 20 20 20 31 36 20 20 20 20 20 0 16
14724 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22 Header string: "
14725 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c SQLite format 3\
14726 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 000".** 16
14727 20 20 20 20 20 32 20 20 20 20 20 50 61 67 65 20 2 Page
14728 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 size in bytes.
14729 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 20 20 .** 18
1472a 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 1 File form
1472b 61 74 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e at write version
1472c 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 20 20 .** 19
1472d 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 1 File form
1472e 61 74 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a at read version.
1472f 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 20 20 ** 20
14730 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 20 75 1 Bytes of u
14731 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 20 74 nused space at t
14732 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 he end of each p
14733 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 age.** 21
14734 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d 1 Max em
14735 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 bedded payload f
14736 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 raction.** 2
14737 32 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 2 1 Mi
14738 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f n embedded paylo
14739 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 ad fraction.**
1473a 20 20 20 32 33 20 20 20 20 20 20 20 31 20 20 20 23 1
1473b 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f Min leaf paylo
1473c 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 ad fraction.**
1473d 20 20 20 32 34 20 20 20 20 20 20 20 34 20 20 20 24 4
1473e 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f File change co
1473f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 unter.** 28
14740 20 20 20 20 20 20 34 20 20 20 20 20 52 65 73 65 4 Rese
14741 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 rved for future
14742 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 use.** 32
14743 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 20 4 First
14744 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a freelist page.**
14745 20 20 20 20 20 33 36 20 20 20 20 20 20 20 34 20 36 4
14746 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 Number of fr
14747 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 6e 20 eelist pages in
14748 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 the file.**
14749 34 30 20 20 20 20 20 20 36 30 20 20 20 20 20 31 40 60 1
1474a 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 76 61 5 4-byte meta va
1474b 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f 20 68 lues passed to h
1474c 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a igher layers.**.
1474d 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 20 ** 40
1474e 34 20 20 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 4 Schema coo
1474f 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20 20 20 kie.** 44
14750 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 66 4 File f
14751 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20 ormat of schema
14752 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34 38 20 layer.** 48
14753 20 20 20 20 20 20 34 20 20 20 20 20 53 69 7a 65 4 Size
14754 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a of page cache.*
14755 2a 20 20 20 20 20 35 32 20 20 20 20 20 20 20 34 * 52 4
14756 20 20 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f Largest roo
14757 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63 t-page (auto/inc
14758 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20 20 20 r_vacuum).**
14759 20 35 36 20 20 20 20 20 20 20 34 20 20 20 20 20 56 4
1475a 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31 36 6c 1=UTF-8 2=UTF16l
1475b 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a 20 20 e 3=UTF16be.**
1475c 20 20 20 36 30 20 20 20 20 20 20 20 34 20 20 20 60 4
1475d 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e 0a 2a User version.*
1475e 2a 20 20 20 20 20 36 34 20 20 20 20 20 20 20 34 * 64 4
1475f 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c Incremental
14760 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 vacuum mode.**
14761 20 20 20 20 36 38 20 20 20 20 20 20 20 34 20 20 68 4
14762 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 unused.**
14763 20 37 32 20 20 20 20 20 20 20 34 20 20 20 20 20 72 4
14764 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 36 unused.** 76
14765 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 4 unu
14766 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 sed.**.** All of
14767 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
14768 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 ues are big-endi
14769 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 an (most signifi
1476a 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 cant byte first)
1476b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 ..**.** The file
1476c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1476d 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 is incremented w
1476e 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
1476f 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 is changed.** T
14770 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f his counter allo
14771 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ws other process
14772 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 es to know when
14773 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 the file has cha
14774 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 nged.** and thus
14775 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 when they need
14776 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 to flush their c
14777 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ache..**.** The
14778 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 max embedded pay
14779 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 load fraction is
1477a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 the amount of t
1477b 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a he total usable.
1477c 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 ** space in a pa
1477d 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 ge that can be c
1477e 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e onsumed by a sin
1477f 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 gle cell for sta
14780 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 ndard.** B-tree
14781 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 (non-LEAFDATA) t
14782 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 ables. A value
14783 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 of 255 means 100
14784 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a %. The default.
14785 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 ** is to limit t
14786 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 he maximum cell
14787 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 size so that at
14788 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 least 4 cells wi
14789 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 ll fit.** on one
1478a 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 page. Thus the
1478b 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 default max emb
1478c 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
1478d 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a action is 64..**
1478e 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f .** If the paylo
1478f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 ad for a cell is
14790 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
14791 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 max payload, th
14792 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c en extra.** payl
14793 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 oad is spilled t
14794 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
14795 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 . Once an overf
14796 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f low page is allo
14797 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e cated,.** as man
14798 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 y bytes as possi
14799 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e ble are moved in
1479a 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 to the overflow
1479b 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 pages without le
1479c 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c tting.** the cel
1479d 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f l size drop belo
1479e 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 w the min embedd
1479f 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
147a0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d ion..**.** The m
147a1 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 in leaf payload
147a2 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 fraction is like
147a3 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 the min embedde
147a4 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
147a5 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 on.** except tha
147a6 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 t it applies to
147a7 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 leaf nodes in a
147a8 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 LEAFDATA tree.
147a9 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 The maximum.** p
147aa 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 ayload fraction
147ab 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 for a LEAFDATA t
147ac 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 ree is always 10
147ad 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 0% (or 255) and
147ae 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 it.** not specif
147af 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 ied in the heade
147b0 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 r..**.** Each bt
147b1 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76 ree pages is div
147b2 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 ided into three
147b3 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 sections: The h
147b4 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 eader, the.** ce
147b5 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
147b6 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 , and the cell c
147b7 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 ontent area. Pa
147b8 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 ge 1 also has a
147b9 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 100-byte.** file
147ba 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 header that occ
147bb 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 urs before the p
147bc 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a age header..**.*
147bd 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
147be 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
147bf 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 | file header
147c0 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 | 100 bytes
147c1 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a . Page 1 only..
147c2 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
147c3 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 ---------|.**
147c4 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 | page header
147c5 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 | 8 bytes
147c6 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 for leaves. 12
147c7 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 bytes for interi
147c8 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 or nodes.**
147c9 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
147ca 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 --|.** | ce
147cb 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 ll pointer |
147cc 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20 | 2 bytes per
147cd 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 cell. Sorted or
147ce 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 der..** | a
147cf 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 rray |
147d0 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 | Grows downw
147d1 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 ard.** |
147d2 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 |
147d3 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d v.** |----
147d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a ------------|.**
147d5 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 | unalloca
147d6 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 ted |.**
147d7 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20 | space
147d8 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d |.** |---
147d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 -------------|
147da 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 ^ Grows upward
147db 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c s.** | cell
147dc 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c content | |
147dd 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 Arbitrary orde
147de 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 r interspersed w
147df 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a ith freeblocks..
147e0 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 ** | area
147e1 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 | |
147e2 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66 and free space f
147e3 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 ragments..**
147e4 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
147e5 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ---|.**.** The p
147e6 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b age headers look
147e7 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a s like this:.**.
147e8 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 ** OFFSET SI
147e9 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49 ZE DESCRIPTI
147ea 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 ON.** 0
147eb 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 1 Flags.
147ec 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 1: intkey, 2: z
147ed 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 erodata, 4: leaf
147ee 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a data, 8: leaf.**
147ef 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 1 2
147f0 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 byte offset
147f1 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 to the first fr
147f2 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 eeblock.**
147f3 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 3 2 n
147f4 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
147f5 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 n this page.**
147f6 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20 5 2
147f7 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 first byte of
147f8 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
147f9 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 t area.** 7
147fa 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 1 nu
147fb 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 mber of fragment
147fc 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a ed free bytes.**
147fd 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20 8 4
147fe 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 Right child
147ff 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c (the Ptr(N) val
14800 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e ue). Omitted on
14801 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 leaves..**.** T
14802 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 he flags define
14803 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 the format of th
14804 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 is btree page.
14805 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 The leaf flag me
14806 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 ans that.** this
14807 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 page has no chi
14808 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f ldren. The zero
14809 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 data flag means
1480a 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63 that this page c
1480b 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b arries.** only k
1480c 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e eys and no data.
1480d 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 The intkey fla
1480e 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 g means that the
1480f 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 key is a intege
14810 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 r.** which is st
14811 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 ored in the key
14812 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 size entry of th
14813 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 e cell header ra
14814 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 ther than in.**
14815 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 the payload area
14816 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c ..**.** The cell
14817 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 pointer array b
14818 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 egins on the fir
14819 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 st byte after th
1481a 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a e page header..*
1481b 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 * The cell point
1481c 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e er array contain
1481d 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 s zero or more 2
1481e 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 -byte numbers wh
1481f 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 ich are.** offse
14820 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 ts from the begi
14821 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 nning of the pag
14822 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f e to the cell co
14823 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c ntent in the cel
14824 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 l.** content are
14825 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 a. The cell poi
14826 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 nters occur in s
14827 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 orted order. Th
14828 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 e system strives
14829 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 .** to keep free
1482a 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 space after the
1482b 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 last cell point
1482c 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 er so that new c
1482d 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 ells can.** be e
1482e 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 asily added with
1482f 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 out having to de
14830 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 fragment the pag
14831 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f e..**.** Cell co
14832 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 ntent is stored
14833 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 at the very end
14834 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 of the page and
14835 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 grows toward the
14836 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 .** beginning of
14837 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
14838 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 Unused space wi
14839 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f thin the cell co
1483a 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f ntent area is co
1483b 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c llected into a l
1483c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a inked list of.**
1483d 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 freeblocks. Ea
1483e 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 ch freeblock is
1483f 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
14840 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 in size. The b
14841 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f yte offset.** to
14842 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 the first freeb
14843 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e lock is given in
14844 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 the header. Fr
14845 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 eeblocks occur i
14846 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 n.** increasing
14847 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 order. Because
14848 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 a freeblock must
14849 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 be at least 4 b
1484a 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a ytes in size,.**
1484b 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 any group of 3
1484c 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 or fewer unused
1484d 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c bytes in the cel
1484e 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 l content area c
1484f 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f annot.** exist o
14850 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 n the freeblock
14851 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 chain. A group
14852 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 of 3 or fewer fr
14853 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c ee bytes is call
14854 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 ed.** a fragment
14855 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d . The total num
14856 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
14857 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 all fragments is
14858 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e recorded..** in
14859 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
1485a 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a at offset 7..**
1485b 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 .** SIZE D
1485c 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 ESCRIPTION.**
1485d 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 2 Byte of
1485e 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 fset of the next
1485f 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 freeblock.**
14860 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69 2 Bytes i
14861 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b n this freeblock
14862 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 .**.** Cells are
14863 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e of variable len
14864 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 gth. Cells are
14865 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 stored in the ce
14866 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
14867 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 at.** the end of
14868 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e the page. Poin
14869 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c ters to the cell
1486a 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c s are in the cel
1486b 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a l pointer array.
1486c 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 ** that immediat
1486d 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 ely follows the
1486e 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 page header. Ce
1486f 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 lls is not neces
14870 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 sarily.** contig
14871 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 uous or in order
14872 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 , but cell point
14873 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f ers are contiguo
14874 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e us and in order.
14875 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 .**.** Cell cont
14876 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 ent makes use of
14877 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
14878 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 integers. A va
14879 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 riable.** length
1487a 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f integer is 1 to
1487b 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74 9 bytes where t
1487c 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 he lower 7 bits
1487d 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 of each .** byte
1487e 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 are used. The
1487f 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 integer consists
14880 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 of all bytes th
14881 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65 at have bit 8 se
14882 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 t and.** the fir
14883 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74 st byte with bit
14884 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 8 clear. The m
14885 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 ost significant
14886 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 byte of the inte
14887 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 ger.** appears f
14888 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c irst. A variabl
14889 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
1488a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 may not be more
1488b 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f than 9 bytes lo
1488c 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 ng..** As a spec
1488d 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 ial case, all 8
1488e 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 bytes of the 9th
1488f 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61 byte are used a
14890 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a s data. This.**
14891 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 allows a 64-bit
14892 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 integer to be e
14893 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 ncoded in 9 byte
14894 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 s..**.** 0x00
14895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14896 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
14897 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 x00000000.**
14898 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 0x7f
14899 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
1489a 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a s 0x0000007f.**
1489b 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20 0x81 0x00
1489c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
1489d 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 comes 0x0000008
1489e 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 0.** 0x82 0x0
1489f 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
148a0 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
148a1 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 00100.** 0x80
148a2 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 0x7f
148a3 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
148a4 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 x0000007f.**
148a5 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30 0x8a 0x91 0xd1 0
148a6 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 xac 0x78 become
148a7 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a s 0x12345678.**
148a8 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78 0x81 0x81 0x
148a9 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65 81 0x81 0x01 be
148aa 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 comes 0x1020408
148ab 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 1.**.** Variable
148ac 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 length integers
148ad 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f are used for ro
148ae 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 wids and to hold
148af 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
148b0 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 * bytes of key a
148b1 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 nd data in a btr
148b2 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 ee cell..**.** T
148b3 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
148b4 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 cell looks like
148b5 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
148b6 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
148b7 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
148b8 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
148b9 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 the left child.
148ba 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 Omitted if leaf
148bb 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 flag is set..**
148bc 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 var Numbe
148bd 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 r of bytes of da
148be 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 ta. Omitted if t
148bf 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 he zerodata flag
148c0 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 is set..**
148c1 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 var Number of
148c2 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f bytes of key. O
148c3 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 r the key itself
148c4 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 if intkey flag
148c5 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 is set..**
148c6 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a * Payload.**
148c7 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
148c8 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 t page of the ov
148c9 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f erflow chain. O
148ca 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 mitted if no ove
148cb 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 rflow.**.** Over
148cc 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 flow pages form
148cd 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 a linked list.
148ce 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74 Each page except
148cf 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d the last is com
148d0 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 pletely.** fille
148d1 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67 d with data (pag
148d2 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 esize - 4 bytes)
148d3 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 . The last page
148d4 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 can have as lit
148d5 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 tle.** as 1 byte
148d6 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 of data..**.**
148d7 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
148d8 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
148d9 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
148da 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f of next overflo
148db 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a w page.** *
148dc 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 Data.**.**
148dd 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 Freelist pages c
148de 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 ome in two subty
148df 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 pes: trunk pages
148e0 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e and leaf pages.
148e1 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 The.** file he
148e2 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 ader points to t
148e3 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 he first in a li
148e4 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 nked list of tru
148e5 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 nk page. Each t
148e6 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 runk.** page poi
148e7 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 nts to multiple
148e8 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 leaf pages. The
148e9 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 content of a le
148ea 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e af page is.** un
148eb 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 specified. A tr
148ec 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c unk page looks l
148ed 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
148ee 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
148ef 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
148f0 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
148f1 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 of next trunk p
148f2 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 age.** 4
148f3 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 Number of leaf
148f4 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 pointers on thi
148f5 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a s page.** *
148f6 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 zero or mor
148f7 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 e pages numbers
148f8 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f of leaves.*/.../
148f9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
148fa 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 value is the max
148fb 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 imum cell size a
148fc 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 ssuming a maximu
148fd 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 m page.** size g
148fe 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 ive above..*/.#d
148ff 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 efine MX_CELL_SI
14900 5a 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 ZE(pBt) (pBt->p
14901 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 ageSize-8)../* T
14902 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
14903 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 r of cells on a
14904 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 single page of t
14905 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
14906 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 is.** assumes a
14907 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a minimum cell siz
14908 65 20 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 e of 6 bytes (4
14909 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 bytes for the c
1490a 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c ell itself.** pl
1490b 75 73 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 us 2 bytes for t
1490c 68 65 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 he index to the
1490d 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 cell in the page
1490e 20 68 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a header). Such.
1490f 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 ** small cells w
14910 69 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 ill be rare, but
14911 20 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 they are possib
14912 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d le..*/.#define M
14913 58 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 X_CELL(pBt) ((pB
14914 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 t->pageSize-8)/6
14915 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 )../* Forward de
14916 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 clarations */.ty
14917 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
14918 50 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 Page MemPage;.ty
14919 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c pedef struct BtL
1491a 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a ock BtLock;../*.
1491b 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 ** This is a mag
1491c 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 ic string that a
1491d 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 ppears at the be
1491e 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 ginning of every
1491f 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 .** SQLite datab
14920 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ase in order to
14921 69 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c identify the fil
14922 65 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 e as a real data
14923 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 base..**.** You
14924 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 can change this
14925 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 value at compile
14926 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 -time by specify
14927 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 ing a.** -DSQLIT
14928 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e E_FILE_HEADER=".
14929 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 .." on the compi
1492a 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 ler command-line
1492b 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 . The.** header
1492c 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 must be exactly
1492d 20 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 16 bytes includ
1492e 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 ing the zero-ter
1492f 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 minator so.** th
14930 65 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 e string itself
14931 73 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 should be 15 cha
14932 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 racters long. I
14933 66 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 f you change.**
14934 74 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e the header, then
14935 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 your custom lib
14936 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 rary will not be
14937 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a able to read .*
14938 2a 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 * databases gene
14939 72 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61 rated by the sta
1493a 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 ndard tools and
1493b 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f the standard too
1493c 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 ls.** will not b
1493d 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 e able to read d
1493e 61 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 atabases created
1493f 20 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 by your custom
14940 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e library..*/.#ifn
14941 64 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f def SQLITE_FILE_
14942 48 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 HEADER /* 123456
14943 37 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 789 123456 */.#
14944 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 define SQLITE_F
14945 49 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 ILE_HEADER "SQLi
14946 74 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e te format 3".#en
14947 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 dif../*.** Page
14948 74 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 type flags. An
14949 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e ORed combination
1494a 20 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 of these flags
1494b 61 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a appear as the.**
1494c 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f first byte of o
1494d 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 n-disk image of
1494e 65 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65 every BTree page
1494f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 ..*/.#define PTF
14950 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a _INTKEY 0x01.
14951 23 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f #define PTF_ZERO
14952 44 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 DATA 0x02.#defi
14953 6e 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 ne PTF_LEAFDATA
14954 20 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 0x04.#define PT
14955 46 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 F_LEAF 0x08
14956 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 ../*.** As each
14957 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 page of the file
14958 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 is loaded into
14959 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 memory, an insta
1495a 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
1495b 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
1495c 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e e is appended an
1495d 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f d initialized to
1495e 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 zero. This str
1495f 75 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a ucture stores.**
14960 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
14961 75 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74 ut the page that
14962 20 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d is decoded from
14963 20 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 the raw file pa
14964 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 ge..**.** The pP
14965 61 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e arent field poin
14966 74 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 ts back to the p
14967 61 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 arent page. Thi
14968 73 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a s allows us to.*
14969 2a 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 * walk up the BT
1496a 72 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 ree from any lea
1496b 66 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 f to the root.
1496c 43 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b Care must be tak
1496d 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 en to.** unref()
1496e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
1496f 20 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 pointer when th
14970 69 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f is page is no lo
14971 6e 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e nger referenced.
14972 0a 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 .** The pageDest
14973 72 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 ructor() routine
14974 20 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 handles that ch
14975 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 ore..**.** Acces
14976 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 s to all fields
14977 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
14978 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 e is controlled
14979 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 by the mutex.**
1497a 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 stored in MemPag
1497b 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f e.pBt->mutex..*/
1497c 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 .struct MemPage
1497d 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 {. u8 isInit;
1497e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1497f 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 if previously i
14980 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 nitialized. MUST
14981 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 BE FIRST! */.
14982 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 u8 nOverflow;
14983 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14984 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 f overflow cell
14985 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b bodies in aCell[
14986 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 ] */. u8 intKey
14987 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
14988 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c rue if intkey fl
14989 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 ag is set */. u
1498a 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 8 leaf;
1498b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c /* True if l
1498c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 eaf flag is set
1498d 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b */. u8 hasData;
1498e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1498f 65 20 69 66 20 74 68 69 73 20 70 61 67 65 20 73 e if this page s
14990 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 tores data */.
14991 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 u8 hdrOffset;
14992 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 /* 100 for
14993 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 page 1. 0 other
14994 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 wise */. u8 chi
14995 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f ldPtrSize; /
14996 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 * 0 if leaf==1.
14997 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 4 if leaf==0 */
14998 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b . u16 maxLocal;
14999 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
1499a 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c of BtShared.maxL
1499b 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 ocal or BtShared
1499c 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 .maxLeaf */. u1
1499d 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 6 minLocal;
1499e 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 /* Copy of Bt
1499f 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 Shared.minLocal
149a0 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c or BtShared.minL
149a1 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c eaf */. u16 cel
149a2 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a lOffset; /*
149a3 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 Index in aData
149a4 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f of first cell po
149a5 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e inter */. u16 n
149a6 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 Free;
149a7 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 /* Number of fre
149a8 65 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 e bytes on the p
149a9 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 age */. u16 nCe
149aa 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ll; /*
149ab 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
149ac 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c on this page, l
149ad 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f ocal and ovfl */
149ae 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b . u16 maskPage;
149af 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 /* Mask
149b0 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 for page offset
149b1 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 */. struct _Ovf
149b2 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c lCell { /* Cel
149b3 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 ls that will not
149b4 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 fit on aData[]
149b5 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c */. u8 *pCell
149b6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ; /* Po
149b7 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f inters to the bo
149b8 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c dy of the overfl
149b9 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 ow cell */. u
149ba 31 36 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 16 idx;
149bb 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 /* Insert thi
149bc 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 s cell before id
149bd 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f x-th non-overflo
149be 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f w cell */. } aO
149bf 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 vfl[5];. BtShar
149c0 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f ed *pBt; /
149c1 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 * Pointer to BtS
149c2 68 61 72 65 64 20 74 68 61 74 20 74 68 69 73 20 hared that this
149c3 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 page is part of
149c4 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 */. u8 *aData;
149c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
149c6 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 nter to disk ima
149c7 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 ge of the page d
149c8 61 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 ata */. DbPage
149c9 2a 70 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a *pDbPage; /*
149ca 20 50 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 Pager page hand
149cb 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e le */. Pgno pgn
149cc 6f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 o; /*
149cd 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 Page number for
149ce 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a this page */.};.
149cf 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 ./*.** The in-me
149d0 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 mory image of a
149d1 64 69 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 disk page has th
149d2 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f e auxiliary info
149d3 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 rmation appended
149d4 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 .** to the end.
149d5 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 EXTRA_SIZE is t
149d6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
149d7 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 es of space need
149d8 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 ed to hold.** th
149d9 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 at extra informa
149da 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tion..*/.#define
149db 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 EXTRA_SIZE size
149dc 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a of(MemPage)../*.
149dd 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74 ** A linked list
149de 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
149df 67 20 73 74 72 75 63 74 75 72 65 73 20 69 73 20 g structures is
149e0 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 61 72 stored at BtShar
149e1 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 ed.pLock..** Loc
149e2 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 6f 72 ks are added (or
149e3 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52 upgraded from R
149e4 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 EAD_LOCK to WRIT
149e5 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63 E_LOCK) when a c
149e6 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65 ursor .** is ope
149e7 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ned on the table
149e8 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 with root page
149e9 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e BtShared.iTable.
149ea 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76 Locks are remov
149eb 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 ed.** from this
149ec 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e list when a tran
149ed 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 saction is commi
149ee 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 tted or rolled b
149ef 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 ack, or when.**
149f0 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69 a btree handle i
149f1 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 s closed..*/.str
149f2 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 uct BtLock {. B
149f3 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 tree *pBtree;
149f4 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61 /* Btree ha
149f5 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 ndle holding thi
149f6 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f s lock */. Pgno
149f7 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 iTable;
149f8 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
149f9 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 f table */. u8
149fa 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 eLock;
149fb 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 /* READ_LOCK
149fc 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f or WRITE_LOCK */
149fd 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 . BtLock *pNext
149fe 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 ; /* Next
149ff 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f in BtShared.pLo
14a00 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f ck list */.};../
14a01 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 * Candidate valu
14a02 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c es for BtLock.eL
14a03 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 ock */.#define R
14a04 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 EAD_LOCK 1.#
14a05 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43 define WRITE_LOC
14a06 4b 20 20 20 20 32 0a 0a 2f 2a 20 41 20 42 74 72 K 2../* A Btr
14a07 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 ee handle.**.**
14a08 41 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 A database conne
14a09 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 ction contains a
14a0a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
14a0b 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
14a0c 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 is object for ev
14a0d 65 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c ery database fil
14a0e 65 20 74 68 61 74 20 69 74 20 68 61 73 20 6f 70 e that it has op
14a0f 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 en. This struct
14a10 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 ure.** is opaque
14a11 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
14a12 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 connection. Th
14a13 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14a14 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 ction cannot.**
14a15 73 65 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c see the internal
14a16 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 s of this struct
14a17 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 ure and only dea
14a18 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 ls with pointers
14a19 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 to.** this stru
14a1a 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 cture..**.** For
14a1b 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66 some database f
14a1c 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75 iles, the same u
14a1d 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 nderlying databa
14a1e 73 65 20 63 61 63 68 65 20 6d 69 67 68 74 20 62 se cache might b
14a1f 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74 e .** shared bet
14a20 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f ween multiple co
14a21 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74 nnections. In t
14a22 68 61 74 20 63 61 73 65 2c 20 65 61 63 68 20 63 hat case, each c
14a23 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 onnection.** has
14a24 20 69 74 20 6f 77 6e 20 69 6e 73 74 61 6e 63 65 it own instance
14a25 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 2e of this object.
14a26 20 20 42 75 74 20 65 61 63 68 20 69 6e 73 74 61 But each insta
14a27 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
14a28 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 ct.** points to
14a29 74 68 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 the same BtShare
14a2a 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 d object. The d
14a2b 61 74 61 62 61 73 65 20 63 61 63 68 65 20 61 6e atabase cache an
14a2c 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 d the.** schema
14a2d 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
14a2e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
14a2f 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 e are all contai
14a30 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 ned within.** th
14a31 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 e BtShared objec
14a32 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 t..**.** All fie
14a33 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 lds in this stru
14a34 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 cture are access
14a35 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74 65 33 ed under sqlite3
14a36 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 .mutex..** The p
14a37 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c Bt pointer itsel
14a38 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 f may not be cha
14a39 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65 72 65 nged while there
14a3a 20 65 78 69 73 74 73 20 63 75 72 73 6f 72 73 20 exists cursors
14a3b 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66 65 72 .** in the refer
14a3c 65 6e 63 65 64 20 42 74 53 68 61 72 65 64 20 74 enced BtShared t
14a3d 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 hat point back t
14a3e 6f 20 74 68 69 73 20 42 74 72 65 65 20 73 69 6e o this Btree sin
14a3f 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 ce those.** curs
14a40 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f 20 67 ors have to do g
14a41 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73 20 42 o through this B
14a42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74 68 65 tree to find the
14a43 69 72 20 42 74 53 68 61 72 65 64 20 61 6e 64 0a ir BtShared and.
14a44 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20 64 6f ** they often do
14a45 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 so without hold
14a46 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 ing sqlite3.mute
14a47 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 x..*/.struct Btr
14a48 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a ee {. sqlite3 *
14a49 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 db; /* The
14a4a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14a4b 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 tion holding thi
14a4c 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 s btree */. BtS
14a4d 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 hared *pBt;
14a4e 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 /* Sharable cont
14a4f 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 ent of this btre
14a50 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e e */. u8 inTran
14a51 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41 s; /* TRA
14a52 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 NS_NONE, TRANS_R
14a53 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 EAD or TRANS_WRI
14a54 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 TE */. u8 shara
14a55 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 ble; /* Tr
14a56 75 65 20 69 66 20 77 65 20 63 61 6e 20 73 68 61 ue if we can sha
14a57 72 65 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74 re pBt with anot
14a58 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c her db */. u8 l
14a59 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f ocked; /
14a5a 2a 20 54 72 75 65 20 69 66 20 64 62 20 63 75 72 * True if db cur
14a5b 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74 20 6c rently has pBt l
14a5c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 ocked */. int w
14a5d 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a antToLock; /*
14a5e 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
14a5f 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 d calls to sqlit
14a60 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a e3BtreeEnter() *
14a61 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b /. int nBackup;
14a62 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14a63 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61 of backup opera
14a64 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68 tions reading th
14a65 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 is btree */. Bt
14a66 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 ree *pNext;
14a67 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 /* List of othe
14a68 72 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 r sharable Btree
14a69 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 s from the same
14a6a 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 db */. Btree *p
14a6b 50 72 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 Prev; /* Ba
14a6c 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 ck pointer of th
14a6d 65 20 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 23 e same list */.#
14a6e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14a6f 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
14a70 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 BtLock lock;
14a71 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 75 /* Object u
14a72 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70 61 67 65 sed to lock page
14a73 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 1 */.#endif.};.
14a74 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 ./*.** Btree.inT
14a75 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e rans may take on
14a76 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
14a77 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a ng values..**.**
14a78 20 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 If the shared-d
14a79 61 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 ata extension is
14a7a 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 enabled, there
14a7b 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 may be multiple
14a7c 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 users.** of the
14a7d 42 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e Btree structure.
14a7e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 At most one of
14a7f 74 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 these may open a
14a80 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
14a81 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e on,.** but any n
14a82 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 umber may have a
14a83 63 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 ctive read trans
14a84 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 actions..*/.#def
14a85 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 ine TRANS_NONE
14a86 30 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 0.#define TRANS_
14a87 52 45 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 READ 1.#define
14a88 54 52 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f TRANS_WRITE 2../
14a89 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
14a8a 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
14a8b 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e represents a sin
14a8c 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c gle database fil
14a8d 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 e..** .** A sing
14a8e 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
14a8f 20 63 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 can be in use a
14a90 73 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 s the same time
14a91 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 by two.** or mor
14a92 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14a93 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 ctions. When tw
14a94 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 o or more connec
14a95 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 tions are.** sha
14a96 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 ring the same da
14a97 74 61 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 tabase file, eac
14a98 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 h connection has
14a99 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 it own.** priva
14a9a 74 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 te Btree object
14a9b 66 6f 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 for the file and
14a9c 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 each of those B
14a9d 74 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 trees points.**
14a9e 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 to this one BtSh
14a9f 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 ared object. Bt
14aa0 53 68 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 Shared.nRef is t
14aa1 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
14aa2 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 connections curr
14aa3 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 ently sharing th
14aa4 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 is database file
14aa5 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 ..**.** Fields i
14aa6 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
14aa7 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
14aa8 64 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 der the BtShared
14aa9 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c .mutex.** mutex,
14aaa 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 except for nRef
14aab 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 and pNext which
14aac 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
14aad 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 der the.** globa
14aae 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 l SQLITE_MUTEX_S
14aaf 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 TATIC_MASTER mut
14ab0 65 78 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 ex. The pPager
14ab1 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 field.** may not
14ab2 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 be modified onc
14ab3 65 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c e it is initiall
14ab4 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 y set as long as
14ab5 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 nRef>0..** The
14ab6 70 53 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 pSchema field ma
14ab7 79 20 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e y be set once un
14ab8 64 65 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 der BtShared.mut
14ab9 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 ex and.** therea
14aba 66 74 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 fter is unchange
14abb 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 d as long as nRe
14abc 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e f>0..**.** isPen
14abd 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 ding:.**.** If
14abe 20 61 20 42 74 53 68 61 72 65 64 20 63 6c 69 65 a BtShared clie
14abf 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 nt fails to obta
14ac0 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 in a write-lock
14ac1 6f 6e 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a on a database.**
14ac2 20 20 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 table (becaus
14ac3 65 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f e there exists o
14ac4 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d ne or more read-
14ac5 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 74 61 62 locks on the tab
14ac6 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65 20 73 68 le),.** the sh
14ac7 61 72 65 64 2d 63 61 63 68 65 20 65 6e 74 65 72 ared-cache enter
14ac8 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 s 'pending-lock'
14ac9 20 73 74 61 74 65 20 61 6e 64 20 69 73 50 65 6e state and isPen
14aca 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20 73 65 74 ding is.** set
14acb 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 to true..**.**
14acc 20 20 54 68 65 20 73 68 61 72 65 64 2d 63 61 63 The shared-cac
14acd 68 65 20 6c 65 61 76 65 73 20 74 68 65 20 27 70 he leaves the 'p
14ace 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 ending lock' sta
14acf 74 65 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f te when either o
14ad0 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f f.** the follo
14ad1 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a wing occur:.**.*
14ad2 2a 20 20 20 20 20 31 29 20 54 68 65 20 63 75 72 * 1) The cur
14ad3 72 65 6e 74 20 77 72 69 74 65 72 20 28 42 74 53 rent writer (BtS
14ad4 68 61 72 65 64 2e 70 57 72 69 74 65 72 29 20 63 hared.pWriter) c
14ad5 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 74 72 61 oncludes its tra
14ad6 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 nsaction, OR.**
14ad7 20 20 20 20 32 29 20 54 68 65 20 6e 75 6d 62 65 2) The numbe
14ad8 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 r of locks held
14ad9 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 by other connect
14ada 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20 7a 65 ions drops to ze
14adb 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c ro..**.** whil
14adc 65 20 69 6e 20 74 68 65 20 27 70 65 6e 64 69 6e e in the 'pendin
14add 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 2c 20 6e g-lock' state, n
14ade 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 o connection may
14adf 20 73 74 61 72 74 20 61 20 6e 65 77 0a 2a 2a 20 start a new.**
14ae0 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a transaction..*
14ae1 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 65 61 74 *.** This feat
14ae2 75 72 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20 ure is included
14ae3 74 6f 20 68 65 6c 70 20 70 72 65 76 65 6e 74 20 to help prevent
14ae4 77 72 69 74 65 72 2d 73 74 61 72 76 61 74 69 6f writer-starvatio
14ae5 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53 n..*/.struct BtS
14ae6 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20 hared {. Pager
14ae7 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 *pPager;
14ae8 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 /* The page cach
14ae9 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
14aea 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 db; /*
14aeb 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 Database connect
14aec 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 ion currently us
14aed 69 6e 67 20 74 68 69 73 20 42 74 72 65 65 20 2a ing this Btree *
14aee 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
14aef 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c ursor; /* A l
14af0 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 ist of all open
14af1 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d cursors */. Mem
14af2 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20 Page *pPage1;
14af3 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 /* First page
14af4 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
14af5 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c */. u8 readOnl
14af6 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 y; /* T
14af7 72 75 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 rue if the under
14af8 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65 lying file is re
14af9 61 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 adonly */. u8 p
14afa 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20 ageSizeFixed;
14afb 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
14afc 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e page size can n
14afd 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e o longer be chan
14afe 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ged */.#ifndef S
14aff 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
14b00 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 ACUUM. u8 autoV
14b01 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a acuum; /*
14b02 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61 True if auto-va
14b03 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 cuum is enabled
14b04 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75 */. u8 incrVacu
14b05 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 um; /* Tr
14b06 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75 ue if incr-vacuu
14b07 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a m is enabled */.
14b08 23 65 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 #endif. u16 pag
14b09 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f eSize; /
14b0a 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f * Total number o
14b0b 66 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 f bytes on a pag
14b0c 65 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c e */. u16 usabl
14b0d 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 eSize; /*
14b0e 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 Number of usable
14b0f 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 bytes on each p
14b10 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 age */. u16 max
14b11 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f Local; /
14b12 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Maximum local
14b13 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c payload in non-L
14b14 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a EAFDATA tables *
14b15 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c /. u16 minLocal
14b16 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e ; /* Min
14b17 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f imum local paylo
14b18 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 ad in non-LEAFDA
14b19 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 TA tables */. u
14b1a 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 16 maxLeaf;
14b1b 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
14b1c 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e local payload in
14b1d 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c a LEAFDATA tabl
14b1e 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 65 e */. u16 minLe
14b1f 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 af; /*
14b20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 Minimum local pa
14b21 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 yload in a LEAFD
14b22 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 ATA table */. u
14b23 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 8 inTransaction;
14b24 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 /* Transact
14b25 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 ion state */. i
14b26 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b nt nTransaction;
14b27 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14b28 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 f open transacti
14b29 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 ons (read + writ
14b2a 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 e) */. void *pS
14b2b 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a chema; /*
14b2c 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 Pointer to spac
14b2d 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 e allocated by s
14b2e 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d qlite3BtreeSchem
14b2f 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a a() */. void (*
14b30 78 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 xFreeSchema)(voi
14b31 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 d*); /* Destruc
14b32 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 tor for BtShared
14b33 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 .pSchema */. sq
14b34 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
14b35 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 ex; /* Non-recur
14b36 73 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 sive mutex requi
14b37 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 red to access th
14b38 69 73 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 is struct */. B
14b39 69 74 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 itvec *pHasConte
14b3a 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 nt; /* Set of p
14b3b 61 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 ages moved to fr
14b3c 65 65 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 ee-list this tra
14b3d 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e nsaction */.#ifn
14b3e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14b3f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 SHARED_CACHE. i
14b40 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 nt nRef;
14b41 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14b42 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 f references to
14b43 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
14b44 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e /. BtShared *pN
14b45 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 ext; /* Nex
14b46 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 t on a list of s
14b47 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 harable BtShared
14b48 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 structs */. Bt
14b49 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 Lock *pLock;
14b4a 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c /* List of l
14b4b 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 ocks held on thi
14b4c 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 s shared-btree s
14b4d 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 truct */. Btree
14b4e 20 2a 70 57 72 69 74 65 72 3b 20 20 20 20 20 20 *pWriter;
14b4f 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68 20 63 /* Btree with c
14b50 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72 urrently open wr
14b51 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite transaction
14b52 2a 2f 0a 20 20 75 38 20 69 73 45 78 63 6c 75 73 */. u8 isExclus
14b53 69 76 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 ive; /* Tr
14b54 75 65 20 69 66 20 70 57 72 69 74 65 72 20 68 61 ue if pWriter ha
14b55 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c s an EXCLUSIVE l
14b56 6f 63 6b 20 6f 6e 20 74 68 65 20 64 62 20 2a 2f ock on the db */
14b57 0a 20 20 75 38 20 69 73 50 65 6e 64 69 6e 67 3b . u8 isPending;
14b58 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 /* If w
14b59 61 69 74 69 6e 67 20 66 6f 72 20 72 65 61 64 2d aiting for read-
14b5a 6c 6f 63 6b 73 20 74 6f 20 63 6c 65 61 72 20 2a locks to clear *
14b5b 2f 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 /.#endif. u8 *p
14b5c 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 TmpSpace;
14b5d 20 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 /* BtShared.pag
14b5e 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 eSize bytes of s
14b5f 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 pace for tmp use
14b60 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
14b61 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
14b62 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
14b63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 ture is used to
14b64 68 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e hold information
14b65 0a 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c .** about a cell
14b66 2e 20 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c . The parseCell
14b67 50 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 Ptr() function f
14b68 69 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 ills in this str
14b69 75 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 ucture.** based
14b6a 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 on information e
14b6b 78 74 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 xtract from the
14b6c 72 61 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a raw disk page..*
14b6d 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
14b6e 20 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e CellInfo CellIn
14b6f 66 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 fo;.struct CellI
14b70 6e 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c nfo {. u8 *pCel
14b71 6c 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 l; /* Pointe
14b72 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f r to the start o
14b73 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a f cell content *
14b74 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 /. i64 nKey;
14b75 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f /* The key fo
14b76 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c r INTKEY tables,
14b77 20 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 or number of by
14b78 74 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 tes in key */.
14b79 75 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f u32 nData; /
14b7a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
14b7b 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 s of data */. u
14b7c 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 32 nPayload; /*
14b7d 20 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 Total amount of
14b7e 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 payload */. u1
14b7f 36 20 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 6 nHeader; /*
14b80 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c Size of the cell
14b81 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 content header
14b82 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 in bytes */. u1
14b83 36 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 6 nLocal; /*
14b84 41 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 Amount of payloa
14b85 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a d held locally *
14b86 2f 0a 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f /. u16 iOverflo
14b87 77 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 w; /* Offset to
14b88 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 overflow page nu
14b89 6d 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e mber. Zero if n
14b8a 6f 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 o overflow */.
14b8b 75 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f u16 nSize; /
14b8c 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 * Size of the ce
14b8d 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 ll content on th
14b8e 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 e main b-tree pa
14b8f 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ge */.};../*.**
14b90 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 Maximum depth of
14b91 20 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 an SQLite B-Tre
14b92 65 20 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 e structure. Any
14b93 20 42 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 B-Tree deeper t
14b94 68 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c han.** this will
14b95 20 62 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 be declared cor
14b96 72 75 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 rupt. This value
14b97 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 is calculated b
14b98 61 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 ased on a.** max
14b99 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 imum database si
14b9a 7a 65 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 ze of 2^31 pages
14b9b 20 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 a minimum fanou
14b9c 74 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 t of 2 for a.**
14b9d 72 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 root-node and 3
14b9e 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e for all other in
14b9f 74 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a ternal nodes..**
14ba0 0a 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 .** If a tree th
14ba1 61 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 at appears to be
14ba2 20 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 taller than thi
14ba3 73 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 s is encountered
14ba4 2c 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d , it is.** assum
14ba5 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ed that the data
14ba6 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e base is corrupt.
14ba7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 .*/.#define BTCU
14ba8 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 RSOR_MAX_DEPTH 2
14ba9 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 0../*.** A curso
14baa 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 r is a pointer t
14bab 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 o a particular e
14bac 6e 74 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 ntry within a pa
14bad 72 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 rticular.** b-tr
14bae 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 ee within a data
14baf 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
14bb0 20 54 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 The entry is id
14bb1 65 6e 74 69 66 69 65 64 20 62 79 20 69 74 73 20 entified by its
14bb2 4d 65 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 MemPage and the
14bb3 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 index in.** MemP
14bb4 61 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 age.aCell[] of t
14bb5 68 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 he entry..**.**
14bb6 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 A single databas
14bb7 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 e file can share
14bb8 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 d by two more da
14bb9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14bba 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f ns,.** but curso
14bbb 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 rs cannot be sha
14bbc 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f red. Each curso
14bbd 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 r is associated
14bbe 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 with a.** partic
14bbf 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f ular database co
14bc0 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 nnection identif
14bc1 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 ied BtCursor.pBt
14bc2 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 ree.db..**.** Fi
14bc3 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 elds in this str
14bc4 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 ucture are acces
14bc5 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 sed under the Bt
14bc6 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 Shared.mutex.**
14bc7 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 found at self->p
14bc8 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 Bt->mutex. .*/.s
14bc9 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b truct BtCursor {
14bca 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
14bcb 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
14bcc 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 The Btree to whi
14bcd 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 ch this cursor b
14bce 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 elongs */. BtSh
14bcf 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
14bd0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 /* The BtS
14bd1 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f hared this curso
14bd2 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 r points to */.
14bd3 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 BtCursor *pNext
14bd4 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f , *pPrev; /* Fo
14bd5 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 rms a linked lis
14bd6 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 t of all cursors
14bd7 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 */. struct Key
14bd8 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
14bd9 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 /* Argument pass
14bda 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e ed to comparison
14bdb 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 function */. P
14bdc 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 gno pgnoRoot;
14bdd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
14bde 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 root page of thi
14bdf 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 s tree */. sqli
14be0 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 te3_int64 cached
14be1 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 Rowid; /* Next r
14be2 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d owid cache. 0 m
14be3 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a eans not valid *
14be4 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 /. CellInfo inf
14be5 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a o; /*
14be6 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 A parse of the
14be7 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e cell we are poin
14be8 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 ting at */. u8
14be9 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 wrFlag;
14bea 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
14beb 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 f writable */.
14bec 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 u8 atLast;
14bed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
14bee 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
14bef 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a the last entry *
14bf0 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 /. u8 validNKey
14bf1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
14bf2 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b True if info.nK
14bf3 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 ey is valid */.
14bf4 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 u8 eState;
14bf5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
14bf6 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f e of the CURSOR_
14bf7 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 XXX constants (s
14bf8 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 ee below) */. v
14bf9 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 oid *pKey;
14bfa 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 /* Saved key tha
14bfb 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c t was cursor's l
14bfc 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 ast known positi
14bfd 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 on */. i64 nKey
14bfe 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 ; /* Size
14bff 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 of pKey, or las
14c00 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f t integer key */
14c01 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b . int skipNext;
14c02 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73 /* Prev() is
14c03 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76 noop if negativ
14c04 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f e. Next() is noo
14c05 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f p if positive */
14c06 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14c07 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
14c08 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e u8 isIncrblobHan
14c09 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 dle; /* Tru
14c0a 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 e if this cursor
14c0b 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 is an incr. io
14c0c 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f handle */. Pgno
14c0d 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 *aOverflow;
14c0e 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f /* Cache o
14c0f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 f overflow page
14c10 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e locations */.#en
14c11 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b dif. i16 iPage;
14c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c13 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
14c14 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 ndex of current
14c15 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a page in apPage *
14c16 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 /. MemPage *apP
14c17 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 age[BTCURSOR_MAX
14c18 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 _DEPTH]; /* Pag
14c19 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 es from root to
14c1a 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a current page */.
14c1b 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 u16 aiIdx[BTCU
14c1c 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b RSOR_MAX_DEPTH];
14c1d 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
14c1e 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 nt index in apPa
14c1f 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ge[i] */.};../*.
14c20 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c ** Potential val
14c21 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 ues for BtCursor
14c22 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 .eState..**.** C
14c23 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 URSOR_VALID:.**
14c24 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 Cursor points
14c25 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 to a valid entry
14c26 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 . getPayload() e
14c27 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 tc. may be calle
14c28 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f d..**.** CURSOR_
14c29 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 INVALID:.** Cu
14c2a 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f rsor does not po
14c2b 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 int to a valid e
14c2c 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 ntry. This can h
14c2d 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 appen (for examp
14c2e 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 le) .** becaus
14c2f 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 e the table is e
14c30 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 mpty or because
14c31 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 BtreeCursorFirst
14c32 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a () has not been.
14c33 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a ** called..**.
14c34 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 ** CURSOR_REQUIR
14c35 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 ESEEK:.** The
14c36 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 table that this
14c37 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
14c38 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 d on still exist
14c39 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 s, but has been
14c3a 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 .** modified s
14c3b 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 ince the cursor
14c3c 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 was last used. T
14c3d 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 he cursor positi
14c3e 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 on is saved.**
14c3f 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 in variables Bt
14c40 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 Cursor.pKey and
14c41 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 BtCursor.nKey. W
14c42 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 hen a cursor is
14c43 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 in .** this st
14c44 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 ate, restoreCurs
14c45 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e orPosition() can
14c46 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 be called to at
14c47 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 tempt to.** se
14c48 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ek the cursor to
14c49 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 the saved posit
14c4a 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f ion..**.** CURSO
14c4b 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 R_FAULT:.** A
14c4c 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 unrecoverable er
14c4d 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f ror (an I/O erro
14c4e 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 r or a malloc fa
14c4f 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 ilure) has occur
14c50 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 red.** on a di
14c51 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 fferent connecti
14c52 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 on that shares t
14c53 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68 he BtShared cach
14c54 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 e with this.**
14c55 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 cursor. The er
14c56 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 ror has left the
14c57 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 cache in an inc
14c58 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e onsistent state.
14c59 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 .** Do nothing
14c5a 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 else with this
14c5b 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 cursor. Any att
14c5c 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 empt to use the
14c5d 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 cursor.** shou
14c5e 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 ld return the er
14c5f 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 ror code stored
14c60 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 in BtCursor.skip
14c61 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 .*/.#define CURS
14c62 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 OR_INVALID
14c63 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 0.#define C
14c64 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 URSOR_VALID
14c65 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
14c66 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 e CURSOR_REQUIRE
14c67 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 SEEK 2.#de
14c68 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c fine CURSOR_FAUL
14c69 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a T 3.
14c6a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 ./* .** The data
14c6b 62 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45 base page the PE
14c6c 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 NDING_BYTE occup
14c6d 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 ies. This page i
14c6e 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f s never used..*/
14c6f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e .# define PENDIN
14c70 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
14c71 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 PAGER_MJ_PGNO(p
14c72 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 Bt)../*.** These
14c73 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 macros define t
14c74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 he location of t
14c75 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
14c76 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 ntry for a .** d
14c77 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 atabase page. Th
14c78 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
14c79 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 to each is the
14c7a 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 number of usable
14c7b 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 .** bytes on eac
14c7c 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 h page of the da
14c7d 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 tabase (often 10
14c7e 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 24). The second
14c7f 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e is the.** page n
14c80 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 umber to look up
14c81 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 in the pointer
14c82 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 map..**.** PTRMA
14c83 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 P_PAGENO returns
14c84 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
14c85 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
14c86 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 pointer-map.**
14c87 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 page that stores
14c88 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f the required po
14c89 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 inter. PTRMAP_PT
14c8a 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a ROFFSET returns.
14c8b 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 ** the offset of
14c8c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d the requested m
14c8d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 ap entry..**.**
14c8e 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 If the pgno argu
14c8f 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 ment passed to P
14c90 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 TRMAP_PAGENO is
14c91 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
14c92 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f ge,.** then pgno
14c93 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f is returned. So
14c94 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 (pgno==PTRMAP_P
14c95 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f AGENO(pgsz, pgno
14c96 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 )) can be.** use
14c97 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e d to test if pgn
14c98 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d o is a pointer-m
14c99 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f ap page. PTRMAP_
14c9a 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 ISPAGE implement
14c9b 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a s.** this test..
14c9c 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 */.#define PTRMA
14c9d 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 P_PAGENO(pBt, pg
14c9e 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f no) ptrmapPageno
14c9f 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 (pBt, pgno).#def
14ca0 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 ine PTRMAP_PTROF
14ca1 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 FSET(pgptrmap, p
14ca2 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 gno) (5*(pgno-pg
14ca3 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 ptrmap-1)).#defi
14ca4 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 ne PTRMAP_ISPAGE
14ca5 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 (pBt, pgno) (PTR
14ca6 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 MAP_PAGENO((pBt)
14ca7 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 ,(pgno))==(pgno)
14ca8 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 )../*.** The poi
14ca9 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f nter map is a lo
14caa 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 okup table that
14cab 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 identifies the p
14cac 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a arent page for.*
14cad 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 * each child pag
14cae 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
14caf 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 e file. The par
14cb0 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 ent page is the
14cb1 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e page that.** con
14cb2 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
14cb3 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 to the child. E
14cb4 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
14cb5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
14cb6 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 ns.** 0 or 1 par
14cb7 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 ent pages. (In
14cb8 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 this context 'da
14cb9 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 tabase page' ref
14cba 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 ers.** to any pa
14cbb 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 ge that is not p
14cbc 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 art of the point
14cbd 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 er map itself.)
14cbe 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 Each pointer ma
14cbf 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 p.** entry consi
14cc0 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 sts of a single
14cc1 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 byte 'type' and
14cc2 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 a 4 byte parent
14cc3 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 page number..**
14cc4 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 The PTRMAP_XXX i
14cc5 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 dentifiers below
14cc6 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 are the valid t
14cc7 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ypes..**.** The
14cc8 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 purpose of the p
14cc9 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f ointer map is to
14cca 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 facility moving
14ccb 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a pages from one.
14ccc 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 ** position in t
14ccd 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 he file to anoth
14cce 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 er as part of au
14ccf 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 tovacuum. When
14cd0 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 a page.** is mov
14cd1 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 ed, the pointer
14cd2 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 in its parent mu
14cd3 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f st be updated to
14cd4 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a point to the.**
14cd5 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 new location.
14cd6 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
14cd7 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 is used to locat
14cd8 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
14cd9 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a e quickly..**.**
14cda 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
14cdb 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14cdc 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 age is a root-pa
14cdd 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d ge. The page-num
14cde 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 ber is not.**
14cdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
14ce0 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 sed in this case
14ce1 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 ..**.** PTRMAP_F
14ce2 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 REEPAGE: The dat
14ce3 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e abase page is an
14ce4 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 unused (free) p
14ce5 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 age. The page-nu
14ce6 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 mber .**
14ce7 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 is not
14ce8 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 used in this ca
14ce9 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 se..**.** PTRMAP
14cea 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 _OVERFLOW1: The
14ceb 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
14cec 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
14ced 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a in a list of .**
14cee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14cef 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 overflow page
14cf0 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 s. The page numb
14cf1 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 er identifies th
14cf2 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 e page that.**
14cf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14cf4 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 contains the ce
14cf5 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 ll with a pointe
14cf6 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c r to this overfl
14cf7 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 ow page..**.** P
14cf8 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a TRMAP_OVERFLOW2:
14cf9 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
14cfa 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 ge is the second
14cfb 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 or later page i
14cfc 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 n a list of.**
14cfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14cfe 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
14cff 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 The page-number
14d00 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
14d01 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 previous.**
14d02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
14d03 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c ge in the overfl
14d04 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a ow page list..**
14d05 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 .** PTRMAP_BTREE
14d06 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14d07 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f age is a non-roo
14d08 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 t btree page. Th
14d09 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a e page number.**
14d0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
14d0b 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 dentifies the pa
14d0c 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 rent page in the
14d0d 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 btree..*/.#defi
14d0e 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 ne PTRMAP_ROOTPA
14d0f 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 GE 1.#define PTR
14d10 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 MAP_FREEPAGE 2.#
14d11 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 define PTRMAP_OV
14d12 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e ERFLOW1 3.#defin
14d13 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f e PTRMAP_OVERFLO
14d14 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 W2 4.#define PTR
14d15 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 MAP_BTREE 5../*
14d16 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 A bunch of asser
14d17 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 t() statements t
14d18 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e o check the tran
14d19 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 saction state va
14d1a 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 riables.** of ha
14d1b 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 ndle p (type Btr
14d1c 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 ee*) are interna
14d1d 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a lly consistent..
14d1e 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 */.#define btree
14d1f 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 Integrity(p) \.
14d20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
14d21 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d >inTransaction!=
14d22 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d TRANS_NONE || p-
14d23 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 >pBt->nTransacti
14d24 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 on==0 ); \. ass
14d25 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 ert( p->pBt->inT
14d26 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 ransaction>=p->i
14d27 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a nTrans ); .../*.
14d28 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 ** The ISAUTOVAC
14d29 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 UUM macro is use
14d2a 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 d within balance
14d2b 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 _nonroot() to de
14d2c 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 termine.** if th
14d2d 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
14d2e 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 rts auto-vacuum
14d2f 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 or not. Because
14d30 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 it is used.** wi
14d31 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 thin an expressi
14d32 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 on that is an ar
14d33 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 gument to anothe
14d34 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c r macro .** (sql
14d35 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 iteMallocRaw), i
14d36 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
14d37 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 e to use conditi
14d38 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e onal compilation
14d39 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 ..** So, this ma
14d3a 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 cro is defined i
14d3b 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 nstead..*/.#ifnd
14d3c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
14d3d 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e UTOVACUUM.#defin
14d3e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 e ISAUTOVACUUM (
14d3f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
14d40 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 .#else.#define I
14d41 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 SAUTOVACUUM 0.#e
14d42 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ndif.../*.** Thi
14d43 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 s structure is p
14d44 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 assed around thr
14d45 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e ough all the san
14d46 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 ity checking rou
14d47 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 tines.** in orde
14d48 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 r to keep track
14d49 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 of some global s
14d4a 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tate information
14d4b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
14d4c 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 uct IntegrityCk
14d4d 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 IntegrityCk;.str
14d4e 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 uct IntegrityCk
14d4f 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
14d50 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 t; /* The tre
14d51 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 e being checked
14d52 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a out */. Pager *
14d53 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 pPager; /* Th
14d54 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 e associated pag
14d55 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 er. Also access
14d56 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 ible by pBt->pPa
14d57 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 ger */. Pgno nP
14d58 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 age; /* Nu
14d59 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
14d5a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f the database */
14d5b 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 . int *anRef;
14d5c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14d5d 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 f times each pag
14d5e 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 e is referenced
14d5f 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 */. int mxErr;
14d60 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 /* Stop a
14d61 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f ccumulating erro
14d62 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 rs when this rea
14d63 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 ches zero */. i
14d64 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 nt nErr;
14d65 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
14d66 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 ssages written t
14d67 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 o zErrMsg so far
14d68 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 */. int malloc
14d69 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d Failed; /* A mem
14d6a 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
14d6b 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
14d6c 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 d */. StrAccum
14d6d 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 errMsg; /* Accu
14d6e 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 mulate the error
14d6f 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65 message text he
14d70 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 re */.};../*.**
14d71 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 Read or write a
14d72 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 two- and four-by
14d73 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
14d74 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f teger values..*/
14d75 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 .#define get2byt
14d76 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c e(x) ((x)[0]<<
14d77 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 8 | (x)[1]).#def
14d78 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 ine put2byte(p,v
14d79 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 ) ((p)[0] = (u8)
14d7a 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d ((v)>>8), (p)[1]
14d7b 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 = (u8)(v)).#def
14d7c 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c ine get4byte sql
14d7d 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 ite3Get4byte.#de
14d7e 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 fine put4byte sq
14d7f 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f lite3Put4byte../
14d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
14d81 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68 nd of btreeInt.h
14d82 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
14d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
14d85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
14d86 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
14d87 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62 we left off in b
14d88 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a tmutex.c *******
14d89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 *************/.#
14d8a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14d8b 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
14d8c 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
14d8d 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 DSAFE../*.** Obt
14d8e 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 ain the BtShared
14d8f 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
14d90 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 d with B-Tree ha
14d91 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a ndle p. Also,.**
14d92 20 73 65 74 20 42 74 53 68 61 72 65 64 2e 64 62 set BtShared.db
14d93 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
14d94 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 handle associat
14d95 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 74 68 ed with p and th
14d96 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 e.** p->locked b
14d97 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a oolean to true..
14d98 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
14d99 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 ockBtreeMutex(Bt
14d9a 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
14d9b 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 t( p->locked==0
14d9c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14d9d 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
14d9e 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ld(p->pBt->mutex
14d9f 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
14da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14da1 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
14da2 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 );.. sqlite3_mu
14da3 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 tex_enter(p->pBt
14da4 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 ->mutex);. p->p
14da5 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
14da6 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b p->locked = 1;
14da7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 .}../*.** Releas
14da8 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d e the BtShared m
14da9 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 utex associated
14daa 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64 with B-Tree hand
14dab 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61 le p and.** clea
14dac 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20 r the p->locked
14dad 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 boolean..*/.stat
14dae 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 ic void unlockBt
14daf 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a reeMutex(Btree *
14db0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
14db1 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20 >locked==1 );.
14db2 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14db3 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 mutex_held(p->pB
14db4 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
14db5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
14db6 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
14db7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
14db8 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 ert( p->db==p->p
14db9 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c Bt->db );.. sql
14dba 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
14dbb 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
14dbc 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 . p->locked = 0
14dbd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 ;.}../*.** Enter
14dbe 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 a mutex on the
14dbf 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 given BTree obje
14dc0 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ct..**.** If the
14dc1 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 object is not s
14dc2 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f harable, then no
14dc3 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 20 72 mutex is ever r
14dc4 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 equired.** and t
14dc5 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
14dc6 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 no-op. The und
14dc7 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 erlying mutex is
14dc8 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a non-recursive..
14dc9 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 ** But we keep a
14dca 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
14dcb 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f in Btree.wantTo
14dcc 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 Lock so the beha
14dcd 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 vior.** of this
14dce 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 interface is rec
14dcf 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f ursive..**.** To
14dd0 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 avoid deadlocks
14dd1 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 , multiple Btree
14dd2 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 s are locked in
14dd3 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a the same order.*
14dd4 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 * by all databas
14dd5 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
14dd6 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 The p->pNext is
14dd7 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a a list of other.
14dd8 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 ** Btrees belong
14dd9 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ing to the same
14dda 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14ddb 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 ion as the p Btr
14ddc 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 ee.** which need
14ddd 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 to be locked af
14dde 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 ter p. If we ca
14ddf 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 nnot get a lock
14de0 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 on.** p, then fi
14de1 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f rst unlock all o
14de2 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 f the others on
14de3 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 p->pNext, then w
14de4 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c ait.** for the l
14de5 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 ock to become av
14de6 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 ailable on p, th
14de7 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 en relock all of
14de8 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 the.** subseque
14de9 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 20 64 nt Btrees that d
14dea 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f esire a lock..*/
14deb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14dec 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14ded 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 eEnter(Btree *p)
14dee 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 {. Btree *pLate
14def 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 r;.. /* Some ba
14df0 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b sic sanity check
14df1 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 ing on the Btree
14df2 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 . The list of B
14df3 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 trees. ** conne
14df4 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e cted by pNext an
14df5 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 d pPrev should b
14df6 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 e in sorted orde
14df7 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e r by. ** Btree.
14df8 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 pBt value. All e
14df9 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c lements of the l
14dfa 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e ist should belon
14dfb 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 g to. ** the sa
14dfc 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f me connection. O
14dfd 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 65 65 nly shared Btree
14dfe 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 s are on the lis
14dff 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 t. */. assert(
14e00 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 p->pNext==0 || p
14e01 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e ->pNext->pBt>p->
14e02 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 pBt );. assert(
14e03 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 p->pPrev==0 ||
14e04 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d p->pPrev->pBt<p-
14e05 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 >pBt );. assert
14e06 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c ( p->pNext==0 ||
14e07 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 p->pNext->db==p
14e08 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
14e09 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c ( p->pPrev==0 ||
14e0a 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 p->pPrev->db==p
14e0b 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
14e0c 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c ( p->sharable ||
14e0d 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 (p->pNext==0 &&
14e0e 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b p->pPrev==0) );
14e0f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 .. /* Check for
14e10 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 locking consist
14e11 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 ency */. assert
14e12 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 ( !p->locked ||
14e13 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
14e14 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
14e15 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 sharable || p->w
14e16 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a antToLock==0 );.
14e17 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 . /* We should
14e18 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c already hold a l
14e19 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
14e1a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
14e1b 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
14e1c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14e1d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
14e1e 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 . /* Unless the
14e1f 20 64 61 74 61 62 61 73 65 20 69 73 20 73 68 61 database is sha
14e20 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b rable and unlock
14e21 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 ed, then BtShare
14e22 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 d.db. ** should
14e23 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 74 20 already be set
14e24 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
14e25 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b assert( (p->lock
14e26 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 ed==0 && p->shar
14e27 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d able) || p->pBt-
14e28 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20 >db==p->db );..
14e29 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
14e2a 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d e ) return;. p-
14e2b 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 >wantToLock++;.
14e2c 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 if( p->locked )
14e2d 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 return;.. /* I
14e2e 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65 n most cases, we
14e2f 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 should be able
14e30 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c to acquire the l
14e31 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74 ock we. ** want
14e32 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
14e33 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74 to go throught t
14e34 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63 he ascending loc
14e35 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 k. ** procedure
14e36 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 that follows.
14e37 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74 Just be sure not
14e38 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a to block.. */.
14e39 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75 if( sqlite3_mu
14e3a 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e tex_try(p->pBt->
14e3b 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f mutex)==SQLITE_O
14e3c 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d K ){. p->pBt-
14e3d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 >db = p->db;.
14e3e 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a p->locked = 1;.
14e3f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
14e40 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64 . /* To avoid d
14e41 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72 eadlock, first r
14e42 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 elease all locks
14e43 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20 with a larger.
14e44 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 64 64 ** BtShared add
14e45 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75 ress. Then acqu
14e46 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 ire our lock. T
14e47 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20 hen reacquire.
14e48 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 74 53 ** the other BtS
14e49 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74 hared locks that
14e4a 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 we used to hold
14e4b 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20 in ascending.
14e4c 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 ** order.. */.
14e4d 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 for(pLater=p->p
14e4e 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c Next; pLater; pL
14e4f 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 ater=pLater->pNe
14e50 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 xt){. assert(
14e51 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c pLater->sharabl
14e52 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
14e53 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d pLater->pNext==
14e54 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65 0 || pLater->pNe
14e55 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e xt->pBt>pLater->
14e56 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14e57 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b t( !pLater->lock
14e58 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61 ed || pLater->wa
14e59 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
14e5a 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f if( pLater->lo
14e5b 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e cked ){. un
14e5c 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 lockBtreeMutex(p
14e5d 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Later);. }.
14e5e 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 }. lockBtreeMut
14e5f 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61 ex(p);. for(pLa
14e60 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c ter=p->pNext; pL
14e61 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
14e62 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
14e63 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e if( pLater->wan
14e64 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 tToLock ){.
14e65 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 lockBtreeMutex(
14e66 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pLater);. }.
14e67 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 }.}../*.** Exit
14e68 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d the recursive m
14e69 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e utex on a Btree.
14e6a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14e6b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14e6c 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 treeLeave(Btree
14e6d 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 *p){. if( p->sh
14e6e 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 arable ){. as
14e6f 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
14e70 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e ock>0 );. p->
14e71 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 wantToLock--;.
14e72 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c if( p->wantToL
14e73 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
14e74 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 unlockBtreeMutex
14e75 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
14e76 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
14e77 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
14e78 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72 ue if the BtShar
14e79 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 ed mutex is held
14e7a 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f on the btree, o
14e7b 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72 r if the.** B-Tr
14e7c 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64 ee is not marked
14e7d 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a as sharable..**
14e7e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14e7f 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 is used only fr
14e80 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 om within assert
14e81 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
14e82 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14e83 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
14e84 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 eHoldsMutex(Btre
14e85 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
14e86 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 p->sharable==0
14e87 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 || p->locked==0
14e88 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b || p->wantToLock
14e89 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
14e8a 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c p->sharable==0 |
14e8b 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c | p->locked==0 |
14e8c 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d | p->db==p->pBt-
14e8d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 >db );. assert(
14e8e 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 p->sharable==0
14e8f 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 || p->locked==0
14e90 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
14e91 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 _held(p->pBt->mu
14e92 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
14e93 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ( p->sharable==0
14e94 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 || p->locked==0
14e95 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
14e96 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14e97 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72 tex) );.. retur
14e98 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d n (p->sharable==
14e99 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b 0 || p->locked);
14e9a 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e .}.#endif...#ifn
14e9b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14e9c 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 INCRBLOB./*.** E
14e9d 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61 nter and leave a
14e9e 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 mutex on a Btre
14e9f 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72 e given a cursor
14ea0 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a owned by that.*
14ea1 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20 * Btree. These
14ea2 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65 entry points are
14ea3 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65 used by increme
14ea4 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e ntal I/O and can
14ea5 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 be.** omitted i
14ea6 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 f that module is
14ea7 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 not used..*/.SQ
14ea8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14ea9 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
14eaa 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 terCursor(BtCurs
14eab 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c or *pCur){. sql
14eac 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
14ead 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a Cur->pBtree);.}.
14eae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
14eaf 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
14eb0 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 LeaveCursor(BtCu
14eb1 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 rsor *pCur){. s
14eb2 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
14eb3 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a (pCur->pBtree);.
14eb4 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
14eb5 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
14eb6 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 */.../*.** Ente
14eb7 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 r the mutex on e
14eb8 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63 very Btree assoc
14eb9 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 iated with a dat
14eba 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
14ebb 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 ion. This is ne
14ebc 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c eded (for exampl
14ebd 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73 e) prior to pars
14ebe 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 ing.** a stateme
14ebf 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c nt since we will
14ec0 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 be comparing ta
14ec1 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e ble and column n
14ec2 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 ames.** against
14ec3 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 all schemas and
14ec4 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
14ec5 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69 hose schemas bei
14ec6 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 ng.** reset out
14ec7 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a from under us..*
14ec8 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 *.** There is a
14ec9 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 corresponding le
14eca 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 ave-all procedur
14ecb 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 es..**.** Enter
14ecc 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61 the mutexes in a
14ecd 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 ccending order b
14ece 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74 y BtShared point
14ecf 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f er address.** to
14ed0 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69 avoid the possi
14ed1 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f bility of deadlo
14ed2 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65 ck when two thre
14ed3 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 ads with.** two
14ed4 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69 or more btrees i
14ed5 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 n common both tr
14ed6 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 y to lock all th
14ed7 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 eir btrees.** at
14ed8 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e the same instan
14ed9 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
14eda 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14edb 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 3BtreeEnterAll(s
14edc 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
14edd 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 nt i;. Btree *p
14ede 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 , *pLater;. ass
14edf 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14ee0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
14ee1 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b x) );. for(i=0;
14ee2 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
14ee3 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 {. p = db->aD
14ee4 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 b[i].pBt;. as
14ee5 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e sert( !p || (p->
14ee6 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e locked==0 && p->
14ee7 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e sharable) || p->
14ee8 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 pBt->db==p->db )
14ee9 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 ;. if( p && p
14eea 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
14eeb 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14eec 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 k++;. if( !
14eed 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 p->locked ){.
14eee 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
14eef 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b wantToLock==1 );
14ef0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
14ef1 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 p->pPrev ) p = p
14ef2 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 ->pPrev;.
14ef3 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41 /* Reason for A
14ef4 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75 LWAYS: There mu
14ef5 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f st be at least o
14ef6 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65 n unlocked Btree
14ef7 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 in. ** t
14ef8 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72 he chain. Other
14ef9 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63 wise the !p->loc
14efa 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 20 77 ked test above w
14efb 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64 ould have failed
14efc 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c */. whil
14efd 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 e( p->locked &&
14efe 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29 ALWAYS(p->pNext)
14eff 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b ) p = p->pNext;
14f00 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 . for(pLa
14f01 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 ter = p->pNext;
14f02 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 pLater; pLater=p
14f03 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 Later->pNext){.
14f04 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 if( pLa
14f05 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 ter->locked ){.
14f06 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 unloc
14f07 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74 kBtreeMutex(pLat
14f08 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d er);. }
14f09 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
14f0a 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 while( p ){.
14f0b 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 lockBtr
14f0c 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14f0d 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 p = p->pNe
14f0e 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 xt;. }.
14f0f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
14f10 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
14f11 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14f12 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 eeLeaveAll(sqlit
14f13 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 e3 *db){. int i
14f14 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 ;. Btree *p;.
14f15 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14f16 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
14f17 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
14f18 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
14f19 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d ++){. p = db-
14f1a 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
14f1b 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 if( p && p->sha
14f1c 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 rable ){. a
14f1d 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f ssert( p->wantTo
14f1e 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 Lock>0 );.
14f1f 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b p->wantToLock--;
14f20 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61 . if( p->wa
14f21 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 ntToLock==0 ){.
14f22 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 unlockBtr
14f23 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14f24 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
14f25 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
14f26 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
14f27 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 e if the current
14f28 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 thread holds th
14f29 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14f2a 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 ction.** mutex a
14f2b 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 nd all required
14f2c 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 BtShared mutexes
14f2d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
14f2e 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 tine is used ins
14f2f 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
14f30 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f tements only..*/
14f31 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14f32 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14f33 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
14f34 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
14f35 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 int i;. if( !sq
14f36 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14f37 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 (db->mutex) ){.
14f38 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
14f39 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
14f3a 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
14f3b 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 Btree *p;. p
14f3c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14f3d 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
14f3e 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 p->sharable &&.
14f3f 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 (p->want
14f40 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 ToLock==0 || !sq
14f41 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14f42 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 (p->pBt->mutex))
14f43 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
14f44 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
14f45 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
14f46 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
14f47 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
14f48 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74 Btree pointer t
14f49 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72 o a BtreeMutexAr
14f4a 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20 ray. .** if the
14f4b 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73 pointer can poss
14f4c 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77 ibly be shared w
14f4d 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64 ith.** another d
14f4e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14f4f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f on..**.** The po
14f50 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20 inters are kept
14f51 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 in sorted order
14f52 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20 by pBtree->pBt.
14f53 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65 That.** way whe
14f54 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72 n we go to enter
14f55 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73 all the mutexes
14f56 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74 , we can enter t
14f57 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 hem.** in order
14f58 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 without every ha
14f59 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61 ving to backup a
14f5a 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74 nd retry and wit
14f5b 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 hout.** worrying
14f5c 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e about deadlock.
14f5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 .**.** The numbe
14f5e 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65 r of shared btre
14f5f 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 es will always b
14f60 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79 e small (usually
14f61 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61 0 or 1).** so a
14f62 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 n insertion sort
14f63 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20 is an adequate
14f64 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a algorithm here..
14f65 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14f66 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14f67 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
14f68 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 ert(BtreeMutexAr
14f69 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72 ray *pArray, Btr
14f6a 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69 ee *pBtree){. i
14f6b 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61 nt i, j;. BtSha
14f6c 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 red *pBt;. if(
14f6d 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74 pBtree==0 || pBt
14f6e 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ree->sharable==0
14f6f 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64 ) return;.#ifnd
14f70 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 ef NDEBUG. {.
14f71 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 for(i=0; i<pAr
14f72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b ray->nMutex; i++
14f73 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
14f74 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14f75 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 i]!=pBtree );.
14f76 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
14f77 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d assert( pArray-
14f78 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20 >nMutex>=0 );.
14f79 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e assert( pArray->
14f7a 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65 nMutex<ArraySize
14f7b 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29 (pArray->aBtree)
14f7c 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 -1 );. pBt = pB
14f7d 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 tree->pBt;. for
14f7e 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e (i=0; i<pArray->
14f7f 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 nMutex; i++){.
14f80 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14f81 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 ->aBtree[i]!=pBt
14f82 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ree );. if( p
14f83 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d Array->aBtree[i]
14f84 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 ->pBt>pBt ){.
14f85 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d for(j=pArray-
14f86 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d >nMutex; j>i; j-
14f87 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 -){. pArr
14f88 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 ay->aBtree[j] =
14f89 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a pArray->aBtree[j
14f8a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 -1];. }.
14f8b 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 pArray->aBtre
14f8c 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 e[i] = pBtree;.
14f8d 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 pArray->nMu
14f8e 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 tex++;. ret
14f8f 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
14f90 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14f91 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b pArray->nMutex++
14f92 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f ] = pBtree;.}../
14f93 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d *.** Enter the m
14f94 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 utex of every bt
14f95 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 ree in the array
14f96 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
14f97 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 is.** called at
14f98 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
14f99 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
14f9a 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 (). The mutexes
14f9b 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 are.** exited a
14f9c 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
14f9d 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a same function..
14f9e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14f9f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14fa0 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
14fa1 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 er(BtreeMutexArr
14fa2 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 ay *pArray){. i
14fa3 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
14fa4 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
14fa5 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 x; i++){. Btr
14fa6 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e ee *p = pArray->
14fa7 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f aBtree[i];. /
14fa8 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
14fa9 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
14faa 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 assert( i==0
14fab 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 || pArray->aBtr
14fac 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e ee[i-1]->pBt<p->
14fad 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14fae 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c t( !p->locked ||
14faf 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 p->wantToLock>0
14fb0 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 );.. /* We s
14fb1 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f hould already ho
14fb2 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ld a lock on the
14fb3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14fb4 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 tion */. asse
14fb5 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14fb6 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14fb7 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 tex) );.. /*
14fb8 54 68 65 20 42 74 72 65 65 20 69 73 20 73 68 61 The Btree is sha
14fb9 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e rable because on
14fba 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 ly sharable Btre
14fbb 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20 es are entered.
14fbc 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 ** into the a
14fbd 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 72 73 rray in the firs
14fbe 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20 t place. */.
14fbf 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14fc0 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 ble );.. p->w
14fc1 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 antToLock++;.
14fc2 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 if( !p->locked
14fc3 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 ){. lockBtr
14fc4 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14fc5 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c }. }.}../*.** L
14fc6 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f eave the mutex o
14fc7 66 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e f every btree in
14fc8 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 the group..*/.S
14fc9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14fca 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
14fcb 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 utexArrayLeave(B
14fcc 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a treeMutexArray *
14fcd 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 pArray){. int i
14fce 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
14fcf 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 Array->nMutex; i
14fd0 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a ++){. Btree *
14fd1 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 p = pArray->aBtr
14fd2 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f ee[i];. /* So
14fd3 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 me basic sanity
14fd4 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 checking */.
14fd5 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 assert( i==0 ||
14fd6 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
14fd7 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 -1]->pBt<p->pBt
14fd8 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
14fd9 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 ->locked );.
14fda 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
14fdb 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 oLock>0 );..
14fdc 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 /* We should alr
14fdd 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b eady hold a lock
14fde 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
14fdf 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
14fe0 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
14fe1 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14fe2 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
14fe3 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14fe4 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e k--;. if( p->
14fe5 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b wantToLock==0 ){
14fe6 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
14fe7 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14fe8 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 }. }.}..#else.S
14fe9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14fea 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
14feb 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a nter(Btree *p){.
14fec 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 p->pBt->db = p
14fed 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ->db;.}.SQLITE_P
14fee 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14fef 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
14ff0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
14ff1 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
14ff2 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
14ff3 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 +){. Btree *p
14ff4 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14ff5 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a t;. if( p ){.
14ff6 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 p->pBt->db
14ff7 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a = p->db;. }.
14ff8 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}.#endif /*
14ff9 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
14ffa 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f SAFE */.#endif /
14ffb 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f * ifndef SQLITE_
14ffc 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14ffd 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
14ffe 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d ***** End of btm
14fff 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utex.c *********
15000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15001 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15002 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
15003 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
15004 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a btree.c *******
15005 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15006 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
15007 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
15008 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 April 6.**.** T
15009 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1500a 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1500b 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1500c 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1500d 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1500e 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1500f 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
15010 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
15011 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
15012 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
15013 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
15014 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
15015 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
15016 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
15017 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
15018 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
15019 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
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 2a 2a 2a 2a 2a 2a ****************
1501d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1501e 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
1501f 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 file implements
15020 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b a external (disk
15021 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65 -based) database
15022 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a using BTrees..*
15023 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 * See the header
15024 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72 comment on "btr
15025 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 64 eeInt.h" for add
15026 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
15027 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e ion..** Includin
15028 67 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 g a description
15029 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 of file format a
1502a 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f nd an overview o
1502b 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a f operation..*/.
1502c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 ./*.** The heade
1502d 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 r string that ap
1502e 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 pears at the beg
1502f 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a inning of every.
15030 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 ** SQLite databa
15031 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f se..*/.static co
15032 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48 nst char zMagicH
15033 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45 eader[] = SQLITE
15034 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f _FILE_HEADER;../
15035 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c *.** Set this gl
15036 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f obal variable to
15037 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 1 to enable tra
15038 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54 cing using the T
15039 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a RACE.** macro..*
1503a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 /.#if 0.int sqli
1503b 74 65 33 42 74 72 65 65 54 72 61 63 65 3d 31 3b te3BtreeTrace=1;
1503c 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 /* True to ena
1503d 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 ble tracing */.#
1503e 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 define TRACE(X)
1503f 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65 if(sqlite3Btre
15040 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58 eTrace){printf X
15041 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b ;fflush(stdout);
15042 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 }.#else.# define
15043 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 TRACE(X).#endif
15044 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ....#ifndef SQLI
15045 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
15046 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 ACHE./*.** A lis
15047 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 t of BtShared ob
15048 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 jects that are e
15049 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 ligible for part
1504a 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 icipation.** in
1504b 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 shared cache. T
1504c 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 his variable has
1504d 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 file scope duri
1504e 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 ng normal builds
1504f 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 ,.** but the tes
15050 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 t harness needs
15051 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 to access it so
15052 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 we make it globa
15053 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 l for .** test b
15054 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 uilds..**.** Acc
15055 65 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69 ess to this vari
15056 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65 able is protecte
15057 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 d by SQLITE_MUTE
15058 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e X_STATIC_MASTER.
15059 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1505a 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
1505b 49 56 41 54 45 20 42 74 53 68 61 72 65 64 20 2a IVATE BtShared *
1505c 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 SQLITE_WSD sqlit
1505d 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
1505e 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 t = 0;.#else.sta
1505f 74 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51 tic BtShared *SQ
15060 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 LITE_WSD sqlite3
15061 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 SharedCacheList
15062 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 = 0;.#endif.#end
15063 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
15064 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a T_SHARED_CACHE *
15065 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
15066 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
15067 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 CHE./*.** Enable
15068 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 or disable the
15069 73 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64 shared pager and
1506a 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73 schema features
1506b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1506c 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65 tine has no effe
1506d 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64 ct on existing d
1506e 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1506f 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 ons..** The shar
15070 65 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 ed cache setting
15071 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75 effects only fu
15072 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a ture calls to.**
15073 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c sqlite3_open(),
15074 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
15075 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 ), or sqlite3_op
15076 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49 en_v2()..*/.SQLI
15077 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
15078 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 e3_enable_shared
15079 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61 62 6c _cache(int enabl
1507a 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f e){. sqlite3Glo
1507b 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 balConfig.shared
1507c 43 61 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 65 CacheEnabled = e
1507d 6e 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 nable;. return
1507e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e SQLITE_OK;.}.#en
1507f 64 69 66 0a 0a 0a 0a 23 69 66 64 65 66 20 53 51 dif....#ifdef SQ
15080 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
15081 5f 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a _CACHE. /*. **
15082 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 The functions q
15083 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
15084 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53 ableLock(), setS
15085 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15086 6f 63 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20 ock(),. ** and
15087 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 clearAllSharedCa
15088 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a cheTableLocks().
15089 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 ** manipulate
1508a 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 entries in the B
1508b 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 tShared.pLock li
1508c 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 nked list used t
1508d 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 o store. ** sha
1508e 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 red-cache table
1508f 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 level locks. If
15090 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 the library is c
15091 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
15092 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 . ** shared-cac
15093 68 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62 he feature disab
15094 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 led, then there
15095 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 is only ever one
15096 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 user. ** of ea
15097 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 ch BtShared stru
15098 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 cture and so thi
15099 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 s locking is not
1509a 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a necessary. . *
1509b 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 * So define the
1509c 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e lock related fun
1509d 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 ctions as no-ops
1509e 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 .. */. #define
1509f 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
150a0 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 eTableLock(a,b,c
150a1 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 ) SQLITE_OK. #d
150a2 65 66 69 6e 65 20 73 65 74 53 68 61 72 65 64 43 efine setSharedC
150a3 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c acheTableLock(a,
150a4 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 b,c) SQLITE_OK.
150a5 20 23 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c #define clearAl
150a6 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c lSharedCacheTabl
150a7 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 eLocks(a). #def
150a8 69 6e 65 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c ine downgradeAll
150a9 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
150aa 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69 Locks(a). #defi
150ab 6e 65 20 68 61 73 53 68 61 72 65 64 43 61 63 68 ne hasSharedCach
150ac 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 eTableLock(a,b,c
150ad 2c 64 29 20 31 0a 20 20 23 64 65 66 69 6e 65 20 ,d) 1. #define
150ae 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 hasReadConflicts
150af 28 61 2c 20 62 29 20 30 0a 23 65 6e 64 69 66 0a (a, b) 0.#endif.
150b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
150b1 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
150b2 45 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 E..#ifdef SQLITE
150b3 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 _DEBUG./*.**** T
150b4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
150b5 6f 6e 6c 79 20 75 73 65 64 20 61 73 20 70 61 72 only used as par
150b6 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29 t of an assert()
150b7 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2a 2a 0a statement. ***.
150b8 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 **.** Check to s
150b9 65 65 20 69 66 20 70 42 74 72 65 65 20 68 6f 6c ee if pBtree hol
150ba 64 73 20 74 68 65 20 72 65 71 75 69 72 65 64 20 ds the required
150bb 6c 6f 63 6b 73 20 74 6f 20 72 65 61 64 20 6f 72 locks to read or
150bc 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 0a 2a write to the .*
150bd 2a 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f * table with roo
150be 74 20 70 61 67 65 20 69 52 6f 6f 74 2e 20 20 20 t page iRoot.
150bf 52 65 74 75 72 6e 20 31 20 69 66 20 69 74 20 64 Return 1 if it d
150c0 6f 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 oes and 0 if not
150c1 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ..**.** For exam
150c2 70 6c 65 2c 20 77 68 65 6e 20 77 72 69 74 69 6e ple, when writin
150c3 67 20 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 g to a table wit
150c4 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f h root-page iRoo
150c5 74 20 76 69 61 20 0a 2a 2a 20 42 74 72 65 65 20 t via .** Btree
150c6 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 74 72 65 connection pBtre
150c7 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65 e:.**.** asse
150c8 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 rt( hasSharedCac
150c9 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 42 74 72 heTableLock(pBtr
150ca 65 65 2c 20 69 52 6f 6f 74 2c 20 30 2c 20 57 52 ee, iRoot, 0, WR
150cb 49 54 45 5f 4c 4f 43 4b 29 20 29 3b 0a 2a 2a 0a ITE_LOCK) );.**.
150cc 2a 2a 20 57 68 65 6e 20 77 72 69 74 69 6e 67 20 ** When writing
150cd 74 6f 20 61 6e 20 69 6e 64 65 78 20 74 68 61 74 to an index that
150ce 20 72 65 73 69 64 65 73 20 69 6e 20 61 20 73 68 resides in a sh
150cf 61 72 61 62 6c 65 20 64 61 74 61 62 61 73 65 2c arable database,
150d0 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 the .** caller
150d1 73 68 6f 75 6c 64 20 68 61 76 65 20 66 69 72 73 should have firs
150d2 74 20 6f 62 74 61 69 6e 65 64 20 61 20 6c 6f 63 t obtained a loc
150d3 6b 20 73 70 65 63 69 66 79 69 6e 67 20 74 68 65 k specifying the
150d4 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a root page of.**
150d5 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
150d6 6e 67 20 74 61 62 6c 65 2e 20 54 68 69 73 20 6d ng table. This m
150d7 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20 62 69 akes things a bi
150d8 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 t more complicat
150d9 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73 20 6d ed,.** as this m
150da 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65 61 63 odule treats eac
150db 68 20 74 61 62 6c 65 20 61 73 20 61 20 73 65 70 h table as a sep
150dc 61 72 61 74 65 20 73 74 72 75 63 74 75 72 65 2e arate structure.
150dd 20 54 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a To determine.**
150de 20 74 68 65 20 74 61 62 6c 65 20 63 6f 72 72 65 the table corre
150df 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
150e0 69 6e 64 65 78 20 62 65 69 6e 67 20 77 72 69 74 index being writ
150e1 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e ten, this.** fun
150e2 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65 61 ction has to sea
150e3 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 65 20 rch through the
150e4 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e database schema.
150e5 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f .**.** Instead o
150e6 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 f a lock on the
150e7 74 61 62 6c 65 2f 69 6e 64 65 78 20 72 6f 6f 74 table/index root
150e8 65 64 20 61 74 20 70 61 67 65 20 69 52 6f 6f 74 ed at page iRoot
150e9 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79 , the caller may
150ea 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 72 69 74 65 .** hold a write
150eb 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 -lock on the sch
150ec 65 6d 61 20 74 61 62 6c 65 20 28 72 6f 6f 74 20 ema table (root
150ed 70 61 67 65 20 31 29 2e 20 54 68 69 73 20 69 73 page 1). This is
150ee 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 65 70 74 61 also.** accepta
150ef 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ble..*/.static i
150f0 6e 74 20 68 61 73 53 68 61 72 65 64 43 61 63 68 nt hasSharedCach
150f1 65 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 eTableLock(. Bt
150f2 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 ree *pBtree,
150f3 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 /* Handle t
150f4 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20 6c 6f hat must hold lo
150f5 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 52 6f ck */. Pgno iRo
150f6 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ot, /
150f7 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 62 * Root page of b
150f8 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 -tree */. int i
150f9 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 sIndex,
150fa 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 52 6f /* True if iRo
150fb 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f ot is the root o
150fc 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 f an index b-tre
150fd 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4c 6f 63 6b e */. int eLock
150fe 54 79 70 65 20 20 20 20 20 20 20 20 20 20 2f 2a Type /*
150ff 20 52 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 74 Required lock t
15100 79 70 65 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f ype (READ_LOCK o
15101 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f r WRITE_LOCK) */
15102 0a 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 .){. Schema *pS
15103 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20 chema = (Schema
15104 2a 29 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 *)pBtree->pBt->p
15105 53 63 68 65 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 Schema;. Pgno i
15106 54 61 62 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 Tab = 0;. BtLoc
15107 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 k *pLock;.. /*
15108 49 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 If this database
15109 20 69 73 20 6e 6f 74 20 73 68 61 72 65 61 62 6c is not shareabl
1510a 65 2c 20 6f 72 20 69 66 20 74 68 65 20 63 6c 69 e, or if the cli
1510b 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 0a 20 ent is reading.
1510c 20 2a 2a 20 61 6e 64 20 68 61 73 20 74 68 65 20 ** and has the
1510d 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 read-uncommitted
1510e 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20 flag set, then
1510f 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 69 no lock is requi
15110 72 65 64 2e 20 0a 20 20 2a 2a 20 52 65 74 75 72 red. . ** Retur
15111 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61 74 65 n true immediate
15112 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 ly.. */. if( (
15113 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 pBtree->sharable
15114 3d 3d 30 29 0a 20 20 20 7c 7c 20 28 65 4c 6f 63 ==0). || (eLoc
15115 6b 54 79 70 65 3d 3d 52 45 41 44 5f 4c 4f 43 4b kType==READ_LOCK
15116 20 26 26 20 28 70 42 74 72 65 65 2d 3e 64 62 2d && (pBtree->db-
15117 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
15118 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 ReadUncommitted)
15119 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
1511a 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 n 1;. }.. /* I
1511b 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 f the client is
1511c 72 65 61 64 69 6e 67 20 20 6f 72 20 77 72 69 74 reading or writ
1511d 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 61 6e 64 ing an index and
1511e 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 0a 20 the schema is.
1511f 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20 ** not loaded,
15120 74 68 65 6e 20 69 74 20 69 73 20 74 6f 6f 20 64 then it is too d
15121 69 66 66 69 63 75 6c 74 20 74 6f 20 61 63 74 75 ifficult to actu
15122 61 6c 6c 79 20 63 68 65 63 6b 20 74 6f 20 73 65 ally check to se
15123 65 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f e if. ** the co
15124 72 72 65 63 74 20 6c 6f 63 6b 73 20 61 72 65 20 rrect locks are
15125 68 65 6c 64 2e 20 20 53 6f 20 64 6f 20 6e 6f 74 held. So do not
15126 20 62 6f 74 68 65 72 20 2d 20 6a 75 73 74 20 72 bother - just r
15127 65 74 75 72 6e 20 74 72 75 65 2e 0a 20 20 2a 2a eturn true.. **
15128 20 54 68 69 73 20 63 61 73 65 20 64 6f 65 73 20 This case does
15129 6e 6f 74 20 63 6f 6d 65 20 75 70 20 76 65 72 79 not come up very
1512a 20 6f 66 74 65 6e 20 61 6e 79 68 6f 77 2e 0a 20 often anyhow..
1512b 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e 64 65 */. if( isInde
1512c 78 20 26 26 20 28 21 70 53 63 68 65 6d 61 20 7c x && (!pSchema |
1512d 7c 20 28 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 | (pSchema->flag
1512e 73 26 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 s&DB_SchemaLoade
1512f 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 d)==0) ){. re
15130 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f turn 1;. }.. /
15131 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 * Figure out the
15132 20 72 6f 6f 74 2d 70 61 67 65 20 74 68 61 74 20 root-page that
15133 74 68 65 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 the lock should
15134 62 65 20 68 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 be held on. For
15135 74 61 62 6c 65 0a 20 20 2a 2a 20 62 2d 74 72 65 table. ** b-tre
15136 65 73 2c 20 74 68 69 73 20 69 73 20 6a 75 73 74 es, this is just
15137 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
15138 66 20 74 68 65 20 62 2d 74 72 65 65 20 62 65 69 f the b-tree bei
15139 6e 67 20 72 65 61 64 20 6f 72 0a 20 20 2a 2a 20 ng read or. **
1513a 77 72 69 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64 written. For ind
1513b 65 78 20 62 2d 74 72 65 65 73 2c 20 69 74 20 69 ex b-trees, it i
1513c 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 s the root page
1513d 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 of the associate
1513e 64 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a d. ** table. *
1513f 2f 0a 20 20 69 66 28 20 69 73 49 6e 64 65 78 20 /. if( isIndex
15140 29 7b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 ){. HashElem
15141 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 *p;. for(p=sq
15142 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70 liteHashFirst(&p
15143 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 Schema->idxHash)
15144 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 ; p; p=sqliteHas
15145 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 hNext(p)){.
15146 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 28 Index *pIdx = (
15147 49 6e 64 65 78 20 2a 29 73 71 6c 69 74 65 48 61 Index *)sqliteHa
15148 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 shData(p);.
15149 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d if( pIdx->tnum=
1514a 3d 28 69 6e 74 29 69 52 6f 6f 74 20 29 7b 0a 20 =(int)iRoot ){.
1514b 20 20 20 20 20 20 20 69 54 61 62 20 3d 20 70 49 iTab = pI
1514c 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 74 6e 75 6d dx->pTable->tnum
1514d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1514e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 54 61 }else{. iTa
1514f 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 20 7d 0a 0a b = iRoot;. }..
15150 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 /* Search for
15151 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 the required loc
15152 6b 2e 20 45 69 74 68 65 72 20 61 20 77 72 69 74 k. Either a writ
15153 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70 e-lock on root-p
15154 61 67 65 20 69 54 61 62 2c 20 61 20 0a 20 20 2a age iTab, a . *
15155 2a 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 * write-lock on
15156 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 the schema table
15157 2c 20 6f 72 20 28 69 66 20 74 68 65 20 63 6c 69 , or (if the cli
15158 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 29 20 ent is reading)
15159 61 0a 20 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b a. ** read-lock
1515a 20 6f 6e 20 69 54 61 62 20 77 69 6c 6c 20 73 75 on iTab will su
1515b 66 66 69 63 65 2e 20 52 65 74 75 72 6e 20 31 20 ffice. Return 1
1515c 69 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 if any of these
1515d 61 72 65 20 66 6f 75 6e 64 2e 20 20 2a 2f 0a 20 are found. */.
1515e 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 72 65 for(pLock=pBtre
1515f 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 e->pBt->pLock; p
15160 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 Lock; pLock=pLoc
15161 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 k->pNext){. i
15162 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 f( pLock->pBtree
15163 3d 3d 70 42 74 72 65 65 20 0a 20 20 20 20 20 26 ==pBtree . &
15164 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 & (pLock->iTable
15165 3d 3d 69 54 61 62 20 7c 7c 20 28 70 4c 6f 63 6b ==iTab || (pLock
15166 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c ->eLock==WRITE_L
15167 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 69 54 OCK && pLock->iT
15168 61 62 6c 65 3d 3d 31 29 29 0a 20 20 20 20 20 26 able==1)). &
15169 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d & pLock->eLock>=
1516a 65 4c 6f 63 6b 54 79 70 65 20 0a 20 20 20 20 29 eLockType . )
1516b 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 {. return 1
1516c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
1516d 2a 20 46 61 69 6c 65 64 20 74 6f 20 66 69 6e 64 * Failed to find
1516e 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f the required lo
1516f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 ck. */. return
15170 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 0;.}.#endif /* S
15171 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a QLITE_DEBUG */..
15172 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
15173 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 BUG./*.**** This
15174 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 function may be
15175 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 used as part of
15176 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
15177 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0a ents only. ****.
15178 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 **.** Return tru
15179 65 20 69 66 20 69 74 20 77 6f 75 6c 64 20 62 65 e if it would be
1517a 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 illegal for pBt
1517b 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 ree to write int
1517c 6f 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 6f o the.** table o
1517d 72 20 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 r index rooted a
1517e 74 20 69 52 6f 6f 74 20 62 65 63 61 75 73 65 20 t iRoot because
1517f 6f 74 68 65 72 20 73 68 61 72 65 64 20 63 6f 6e other shared con
15180 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 nections are.**
15181 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 20 72 simultaneously r
15182 65 61 64 69 6e 67 20 74 68 61 74 20 73 61 6d 65 eading that same
15183 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
15184 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c .**.** It is ill
15185 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 20 egal for pBtree
15186 74 6f 20 77 72 69 74 65 20 69 66 20 73 6f 6d 65 to write if some
15187 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 6a other Btree obj
15188 65 63 74 20 74 68 61 74 0a 2a 2a 20 73 68 61 72 ect that.** shar
15189 65 73 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 es the same BtSh
1518a 61 72 65 64 20 6f 62 6a 65 63 74 20 69 73 20 63 ared object is c
1518b 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67 urrently reading
1518c 20 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 or writing.** t
1518d 68 65 20 69 52 6f 6f 74 20 74 61 62 6c 65 2e 20 he iRoot table.
1518e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 Except, if the
1518f 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 6a 65 other Btree obje
15190 63 74 20 68 61 73 20 74 68 65 0a 2a 2a 20 72 65 ct has the.** re
15191 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 ad-uncommitted f
15192 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20 69 74 lag set, then it
15193 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 6f is OK for the o
15194 74 68 65 72 20 6f 62 6a 65 63 74 20 74 6f 0a 2a ther object to.*
15195 2a 20 68 61 76 65 20 61 20 72 65 61 64 20 63 75 * have a read cu
15196 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 rsor..**.** For
15197 65 78 61 6d 70 6c 65 2c 20 62 65 66 6f 72 65 20 example, before
15198 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 20 70 writing to any p
15199 61 72 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 art of the table
1519a 20 6f 72 20 69 6e 64 65 78 0a 2a 2a 20 72 6f 6f or index.** roo
1519b 74 65 64 20 61 74 20 70 61 67 65 20 69 52 6f 6f ted at page iRoo
1519c 74 2c 20 6f 6e 65 20 73 68 6f 75 6c 64 20 63 61 t, one should ca
1519d 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 ll:.**.** ass
1519e 65 72 74 28 20 21 68 61 73 52 65 61 64 43 6f 6e ert( !hasReadCon
1519f 66 6c 69 63 74 73 28 70 42 74 72 65 65 2c 20 69 flicts(pBtree, i
151a0 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 Root) );.*/.stat
151a1 69 63 20 69 6e 74 20 68 61 73 52 65 61 64 43 6f ic int hasReadCo
151a2 6e 66 6c 69 63 74 73 28 42 74 72 65 65 20 2a 70 nflicts(Btree *p
151a3 42 74 72 65 65 2c 20 50 67 6e 6f 20 69 52 6f 6f Btree, Pgno iRoo
151a4 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a t){. BtCursor *
151a5 70 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72 65 p;. for(p=pBtre
151a6 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b e->pBt->pCursor;
151a7 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
151a8 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f . if( p->pgno
151a9 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 0a 20 20 20 Root==iRoot .
151aa 20 20 26 26 20 70 2d 3e 70 42 74 72 65 65 21 3d && p->pBtree!=
151ab 70 42 74 72 65 65 0a 20 20 20 20 20 26 26 20 30 pBtree. && 0
151ac 3d 3d 28 70 2d 3e 70 42 74 72 65 65 2d 3e 64 62 ==(p->pBtree->db
151ad 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
151ae 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 _ReadUncommitted
151af 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 ). ){. r
151b0 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
151b1 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
151b2 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 23 69 .#endif /* #i
151b3 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
151b4 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 G */../*.** Quer
151b5 79 20 74 6f 20 73 65 65 20 69 66 20 42 74 72 65 y to see if Btre
151b6 65 20 68 61 6e 64 6c 65 20 70 20 6d 61 79 20 6f e handle p may o
151b7 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 btain a lock of
151b8 74 79 70 65 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28 type eLock .** (
151b9 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 READ_LOCK or WRI
151ba 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68 65 20 TE_LOCK) on the
151bb 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d table with root-
151bc 70 61 67 65 20 69 54 61 62 2e 20 52 65 74 75 72 page iTab. Retur
151bd 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 n.** SQLITE_OK i
151be 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 79 20 62 f the lock may b
151bf 65 20 6f 62 74 61 69 6e 65 64 20 28 62 79 20 63 e obtained (by c
151c0 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 74 53 68 61 alling.** setSha
151c1 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
151c2 6b 28 29 29 2c 20 6f 72 20 53 51 4c 49 54 45 5f k()), or SQLITE_
151c3 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 2e 0a 2a LOCKED if not..*
151c4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65 /.static int que
151c5 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
151c6 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c leLock(Btree *p,
151c7 20 50 67 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 Pgno iTab, u8 e
151c8 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 Lock){. BtShare
151c9 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
151ca 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 . BtLock *pIter
151cb 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
151cc 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
151cd 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
151ce 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f rt( eLock==READ_
151cf 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 LOCK || eLock==W
151d0 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 RITE_LOCK );. a
151d1 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 ssert( p->db!=0
151d2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 70 );. assert( !(p
151d3 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 ->db->flags&SQLI
151d4 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
151d5 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54 ed)||eLock==WRIT
151d6 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 3d 3d 31 20 E_LOCK||iTab==1
151d7 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 72 65 );. . /* If re
151d8 71 75 65 73 74 69 6e 67 20 61 20 77 72 69 74 65 questing a write
151d9 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 -lock, then the
151da 42 74 72 65 65 20 6d 75 73 74 20 68 61 76 65 20 Btree must have
151db 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 0a 20 20 an open write.
151dc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f ** transaction o
151dd 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 41 6e 64 n this file. And
151de 2c 20 6f 62 76 69 6f 75 73 6c 79 2c 20 66 6f 72 , obviously, for
151df 20 74 68 69 73 20 74 6f 20 62 65 20 73 6f 20 74 this to be so t
151e0 68 65 72 65 20 0a 20 20 2a 2a 20 6d 75 73 74 20 here . ** must
151e1 62 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 be an open write
151e2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 transaction on
151e3 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c 66 2e the file itself.
151e4 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
151e5 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
151e6 20 7c 7c 20 28 70 3d 3d 70 42 74 2d 3e 70 57 72 || (p==pBt->pWr
151e7 69 74 65 72 20 26 26 20 70 2d 3e 69 6e 54 72 61 iter && p->inTra
151e8 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 ns==TRANS_WRITE)
151e9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c );. assert( eL
151ea 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
151eb 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 | pBt->inTransac
151ec 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
151ed 45 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 E );. . /* Thi
151ee 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
151ef 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 o-op if the shar
151f0 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ed-cache is not
151f1 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 enabled */. if(
151f2 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b !p->sharable ){
151f3 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
151f4 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
151f5 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 If some other c
151f6 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c onnection is hol
151f7 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 ding an exclusiv
151f8 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a e lock, the. **
151f9 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 requested lock
151fa 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 may not be obtai
151fb 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ned.. */. if(
151fc 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 pBt->pWriter!=p
151fd 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 && pBt->isExclus
151fe 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ive ){. sqlit
151ff 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
15200 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
15201 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 pWriter->db);.
15202 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15203 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
15204 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 HE;. }.. for(p
15205 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Iter=pBt->pLock;
15206 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 pIter; pIter=pI
15207 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
15208 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f /* The conditio
15209 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 n (pIter->eLock!
1520a 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 =eLock) in the f
1520b 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 ollowing if(...)
1520c 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 . ** stateme
1520d 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 nt is a simplifi
1520e 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a cation of:. *
1520f 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 *. ** (eLoc
15210 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c k==WRITE_LOCK ||
15211 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 pIter->eLock==W
15212 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a RITE_LOCK). *
15213 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 *. ** since w
15214 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65 e know that if e
15215 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
15216 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 , then no other
15217 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a connection. *
15218 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49 * may hold a WRI
15219 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 TE_LOCK on any t
1521a 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c able in this fil
1521b 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63 e (since there c
1521c 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 an. ** only b
1521d 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65 e a single write
1521e 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 r).. */. a
1521f 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c ssert( pIter->eL
15220 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
15221 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
15222 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 WRITE_LOCK );.
15223 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
15224 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 =READ_LOCK || pI
15225 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c ter->pBtree==p |
15226 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
15227 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 READ_LOCK);.
15228 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 if( pIter->pBtre
15229 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 e!=p && pIter->i
1522a 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 Table==iTab && p
1522b 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f Iter->eLock!=eLo
1522c 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ck ){. sqli
1522d 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
1522e 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 cked(p->db, pIte
1522f 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a r->pBtree->db);.
15230 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d if( eLock=
15231 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 =WRITE_LOCK ){.
15232 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
15233 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 ==pBt->pWriter )
15234 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 ;. pBt->i
15235 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a 20 20 sPending = 1;.
15236 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
15237 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
15238 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
15239 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
1523a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
1523b 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
1523c 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
1523d 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1523e 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1523f 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64 _CACHE./*.** Add
15240 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 a lock on the t
15241 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
15242 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68 age iTable to th
15243 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75 e shared-btree u
15244 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20 sed.** by Btree
15245 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65 handle p. Parame
15246 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62 ter eLock must b
15247 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f e either READ_LO
15248 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f CK or .** WRITE_
15249 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 LOCK..**.** This
1524a 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
1524b 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
1524c 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65 .**.** (a) The
1524d 20 73 70 65 63 69 66 69 65 64 20 42 74 72 65 65 specified Btree
1524e 20 6f 62 6a 65 63 74 20 70 20 69 73 20 63 6f 6e object p is con
1524f 6e 65 63 74 65 64 20 74 6f 20 61 20 73 68 61 72 nected to a shar
15250 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 able.** da
15251 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74 68 tabase (one with
15252 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73 68 the BtShared.sh
15253 61 72 61 62 6c 65 20 66 6c 61 67 20 73 65 74 29 arable flag set)
15254 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28 62 , and.**.** (b
15255 29 20 4e 6f 20 6f 74 68 65 72 20 42 74 72 65 65 ) No other Btree
15256 20 6f 62 6a 65 63 74 73 20 68 6f 6c 64 20 61 20 objects hold a
15257 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69 lock that confli
15258 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 cts.** wit
15259 68 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 h the requested
1525a 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65 72 79 lock (i.e. query
1525b 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
1525c 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20 Lock() has.**
1525d 20 20 20 20 61 6c 72 65 61 64 79 20 62 65 65 6e already been
1525e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65 74 75 called and retu
1525f 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e rned SQLITE_OK).
15260 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
15261 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
15262 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 the lock is adde
15263 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 d successfully.
15264 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a SQLITE_NOMEM .**
15265 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
15266 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 a malloc attempt
15267 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 fails..*/.stati
15268 63 20 69 6e 74 20 73 65 74 53 68 61 72 65 64 43 c int setSharedC
15269 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 acheTableLock(Bt
1526a 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 ree *p, Pgno iTa
1526b 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a ble, u8 eLock){.
1526c 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
1526d 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f = p->pBt;. BtLo
1526e 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 ck *pLock = 0;.
1526f 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a BtLock *pIter;.
15270 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15271 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
15272 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
15273 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ( eLock==READ_LO
15274 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 CK || eLock==WRI
15275 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 TE_LOCK );. ass
15276 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b ert( p->db!=0 );
15277 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 .. /* A connect
15278 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 61 ion with the rea
15279 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c d-uncommitted fl
1527a 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 76 65 ag set will neve
1527b 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 r try to. ** ob
1527c 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b tain a read-lock
1527d 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63 using this func
1527e 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72 tion. The only r
1527f 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 ead-lock obtaine
15280 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e d. ** by a conn
15281 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75 ection in read-u
15282 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20 ncommitted mode
15283 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 is on the sqlite
15284 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20 74 61 _master . ** ta
15285 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f ble, and that lo
15286 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 ck is obtained i
15287 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e n BtreeBeginTran
15288 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 s(). */. asser
15289 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c t( 0==(p->db->fl
1528a 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
1528b 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65 ncommitted) || e
1528c 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
1528d 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 );.. /* This f
1528e 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f unction should o
1528f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e nly be called on
15290 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d 74 72 a sharable b-tr
15291 65 65 20 61 66 74 65 72 20 69 74 20 0a 20 20 2a ee after it . *
15292 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74 65 72 * has been deter
15293 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6f 74 mined that no ot
15294 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c 64 73 her b-tree holds
15295 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c a conflicting l
15296 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 ock. */. asser
15297 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 t( p->sharable )
15298 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 ;. assert( SQLI
15299 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 TE_OK==queryShar
1529a 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
1529b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 (p, iTable, eLoc
1529c 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 k) );.. /* Firs
1529d 74 20 73 65 61 72 63 68 20 74 68 65 20 6c 69 73 t search the lis
1529e 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e t for an existin
1529f 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 g lock on this t
152a0 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 able. */. for(p
152a1 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Iter=pBt->pLock;
152a2 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 pIter; pIter=pI
152a3 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
152a4 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54 61 62 if( pIter->iTab
152a5 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 49 le==iTable && pI
152a6 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 ter->pBtree==p )
152a7 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 {. pLock =
152a8 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62 72 65 pIter;. bre
152a9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ak;. }. }..
152aa 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f 76 65 /* If the above
152ab 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 20 search did not
152ac 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 find a BtLock st
152ad 72 75 63 74 20 61 73 73 6f 63 69 61 74 69 6e 67 ruct associating
152ae 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 Btree p. ** wi
152af 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c 65 2c th table iTable,
152b0 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e allocate one an
152b1 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 d link it into t
152b2 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 he list.. */.
152b3 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 if( !pLock ){.
152b4 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 pLock = (BtLoc
152b5 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f k *)sqlite3Mallo
152b6 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c cZero(sizeof(BtL
152b7 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28 20 21 ock));. if( !
152b8 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 pLock ){. r
152b9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
152ba 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c EM;. }. pL
152bb 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 ock->iTable = iT
152bc 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d able;. pLock-
152bd 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 >pBtree = p;.
152be 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 pLock->pNext =
152bf 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 pBt->pLock;.
152c0 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f pBt->pLock = pLo
152c1 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ck;. }.. /* Se
152c2 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f t the BtLock.eLo
152c3 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74 ck variable to t
152c4 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 he maximum of th
152c5 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 e current lock.
152c6 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 71 75 ** and the requ
152c7 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 ested lock. This
152c8 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72 69 74 means if a writ
152c9 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 e-lock was alrea
152ca 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 dy held. ** and
152cb 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 a read-lock req
152cc 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 uested, we don't
152cd 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 incorrectly dow
152ce 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e ngrade the lock.
152cf 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
152d0 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f WRITE_LOCK>READ_
152d1 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c LOCK );. if( eL
152d2 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b ock>pLock->eLock
152d3 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 ){. pLock->e
152d4 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 Lock = eLock;.
152d5 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
152d6 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 TE_OK;.}.#endif
152d7 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
152d8 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a SHARED_CACHE */.
152d9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
152da 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
152db 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 E./*.** Release
152dc 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 6c 6f all the table lo
152dd 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 cks (locks obtai
152de 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 74 6f ned via calls to
152df 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61 72 65 .** the setShare
152e0 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
152e1 29 20 70 72 6f 63 65 64 75 72 65 29 20 68 65 6c ) procedure) hel
152e2 64 20 62 79 20 42 74 72 65 65 20 6f 62 6a 65 63 d by Btree objec
152e3 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 t p..**.** This
152e4 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 function assumes
152e5 20 74 68 61 74 20 42 74 72 65 65 20 70 20 68 61 that Btree p ha
152e6 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f s an open read o
152e7 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e r write .** tran
152e8 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 saction. If it d
152e9 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 oes not, then th
152ea 65 20 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e e BtShared.isPen
152eb 64 69 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a ding variable.**
152ec 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 may be incorrec
152ed 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a tly cleared..*/.
152ee 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 static void clea
152ef 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 rAllSharedCacheT
152f0 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 ableLocks(Btree
152f1 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
152f2 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
152f3 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 BtLock **ppIter
152f4 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a = &pBt->pLock;.
152f5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
152f6 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
152f7 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 x(p) );. assert
152f8 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c ( p->sharable ||
152f9 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 0==*ppIter );.
152fa 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
152fb 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c ans>0 );.. whil
152fc 65 28 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 e( *ppIter ){.
152fd 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 BtLock *pLock
152fe 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 = *ppIter;. a
152ff 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 ssert( pBt->isEx
15300 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 clusive==0 || pB
15301 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 t->pWriter==pLoc
15302 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 k->pBtree );.
15303 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
15304 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e pBtree->inTrans>
15305 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b =pLock->eLock );
15306 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e . if( pLock->
15307 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 pBtree==p ){.
15308 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f *ppIter = pLo
15309 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ck->pNext;.
1530a 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
1530b 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f iTable!=1 || pLo
1530c 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a ck==&p->lock );.
1530d 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d if( pLock-
1530e 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 >iTable!=1 ){.
1530f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
15310 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 ee(pLock);.
15311 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
15312 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c ppIter = &pL
15313 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 ock->pNext;.
15314 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 }. }.. assert(
15315 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d pBt->isPending=
15316 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 =0 || pBt->pWrit
15317 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d er );. if( pBt-
15318 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 >pWriter==p ){.
15319 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 pBt->pWriter
1531a 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 = 0;. pBt->is
1531b 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 Exclusive = 0;.
1531c 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e pBt->isPendin
1531d 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 g = 0;. }else i
1531e 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 f( pBt->nTransac
1531f 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f tion==2 ){. /
15320 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
15321 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 42 is called when B
15322 74 72 65 65 20 70 20 69 73 20 63 6f 6e 63 6c 75 tree p is conclu
15323 64 69 6e 67 20 69 74 73 20 0a 20 20 20 20 2a 2a ding its . **
15324 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 transaction. If
15325 20 74 68 65 72 65 20 63 75 72 72 65 6e 74 6c 79 there currently
15326 20 65 78 69 73 74 73 20 61 20 77 72 69 74 65 72 exists a writer
15327 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f 74 0a 20 , and p is not.
15328 20 20 20 2a 2a 20 74 68 61 74 20 77 72 69 74 65 ** that write
15329 72 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 r, then the numb
1532a 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 er of locks held
1532b 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 by connections
1532c 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 other. ** tha
1532d 6e 20 74 68 65 20 77 72 69 74 65 72 20 6d 75 73 n the writer mus
1532e 74 20 62 65 20 61 62 6f 75 74 20 74 6f 20 64 72 t be about to dr
1532f 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 6e 20 74 op to zero. In t
15330 68 69 73 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 his case. **
15331 73 65 74 20 74 68 65 20 69 73 50 65 6e 64 69 6e set the isPendin
15332 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 g flag to 0..
15333 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 **. ** If th
15334 65 72 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 ere is not curre
15335 6e 74 6c 79 20 61 20 77 72 69 74 65 72 2c 20 74 ntly a writer, t
15336 68 65 6e 20 42 74 53 68 61 72 65 64 2e 69 73 50 hen BtShared.isP
15337 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20 20 20 20 ending must.
15338 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c 72 65 61 ** be zero alrea
15339 64 79 2e 20 53 6f 20 74 68 69 73 20 6e 65 78 74 dy. So this next
1533a 20 6c 69 6e 65 20 69 73 20 68 61 72 6d 6c 65 73 line is harmles
1533b 73 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e 0a s in that case..
1533c 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e */. pBt->
1533d 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 isPending = 0;.
1533e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
1533f 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67 65 function change
15340 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f 63 6b s all write-lock
15341 73 20 68 65 6c 64 20 62 79 20 42 74 72 65 65 20 s held by Btree
15342 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f 63 6b p into read-lock
15343 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
15344 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 d downgradeAllSh
15345 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
15346 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 cks(Btree *p){.
15347 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15348 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 70 p->pBt;. if( p
15349 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 Bt->pWriter==p )
1534a 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c {. BtLock *pL
1534b 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 57 ock;. pBt->pW
1534c 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 riter = 0;. p
1534d 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 Bt->isExclusive
1534e 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 = 0;. pBt->is
1534f 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 Pending = 0;.
15350 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 2d 3e for(pLock=pBt->
15351 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c pLock; pLock; pL
15352 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 ock=pLock->pNext
15353 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
15354 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 52 pLock->eLock==R
15355 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 EAD_LOCK || pLoc
15356 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 3b 0a k->pBtree==p );.
15357 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f pLock->eLo
15358 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a ck = READ_LOCK;.
15359 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6e }. }.}..#en
1535a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1535b 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 IT_SHARED_CACHE
1535c 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */..static void
1535d 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d 50 releasePage(MemP
1535e 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f 2a age *pPage); /*
1535f 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
15360 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 20 ce */../*.*****
15361 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
15362 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 20 61 used inside of a
15363 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a 2a 2a ssert() only ***
15364 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79 20 74 *.**.** Verify t
15365 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 hat the cursor h
15366 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 20 6f olds the mutex o
15367 6e 20 69 74 73 20 42 74 53 68 61 72 65 64 0a 2a n its BtShared.*
15368 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
15369 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 DEBUG.static int
1536a 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1536b 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a x(BtCursor *p){.
1536c 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1536d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 _mutex_held(p->p
1536e 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 Bt->mutex);.}.#e
1536f 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 ndif...#ifndef S
15370 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
15371 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 LOB./*.** Invali
15372 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f date the overflo
15373 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
15374 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75 e for cursor pCu
15375 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 r, if any..*/.st
15376 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 atic void invali
15377 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 dateOverflowCach
15378 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
15379 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ){. assert( cur
1537a 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1537b 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ur) );. sqlite3
1537c 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65 _free(pCur->aOve
1537d 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e rflow);. pCur->
1537e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d aOverflow = 0;.}
1537f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 ../*.** Invalida
15380 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 te the overflow
15381 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 page-list cache
15382 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 for all cursors
15383 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 opened.** on the
15384 20 73 68 61 72 65 64 20 62 74 72 65 65 20 73 74 shared btree st
15385 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f 0a ructure pBt..*/.
15386 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
15387 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f lidateAllOverflo
15388 77 43 61 63 68 65 28 42 74 53 68 61 72 65 64 20 wCache(BtShared
15389 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f *pBt){. BtCurso
1538a 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 r *p;. assert(
1538b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1538c 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
1538d 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 ;. for(p=pBt->p
1538e 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e Cursor; p; p=p->
1538f 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 61 pNext){. inva
15390 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 lidateOverflowCa
15391 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f che(p);. }.}../
15392 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
15393 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 65 66 on is called bef
15394 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 ore modifying th
15395 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 e contents of a
15396 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e 76 61 table.** to inva
15397 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 lidate any incrb
15398 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68 61 74 lob cursors that
15399 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 are open on the
1539a 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 20 6f .** row or one o
1539b 66 20 74 68 65 20 72 6f 77 73 20 62 65 69 6e 67 f the rows being
1539c 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a modified..**.**
1539d 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 If argument isC
1539e 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74 72 75 learTable is tru
1539f 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 e, then the enti
153a0 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 re contents of t
153a1 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 61 he.** table is a
153a2 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74 bout to be delet
153a3 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
153a4 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 invalidate all
153a5 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 72 73 incrblob.** curs
153a6 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 79 20 ors open on any
153a7 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65 20 74 row within the t
153a8 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
153a9 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a age pgnoRoot..**
153aa 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 .** Otherwise, i
153ab 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 f argument isCle
153ac 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c 73 65 arTable is false
153ad 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 77 , then the row w
153ae 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f ith.** rowid iRo
153af 77 20 69 73 20 62 65 69 6e 67 20 72 65 70 6c 61 w is being repla
153b0 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 ced or deleted.
153b1 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 In this case inv
153b2 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c 79 20 alidate.** only
153b3 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 20 63 those incrblob c
153b4 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 ursors open on t
153b5 68 61 74 20 73 70 65 63 69 66 69 63 20 72 6f 77 hat specific row
153b6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
153b7 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 invalidateIncrb
153b8 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 42 74 lobCursors(. Bt
153b9 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 ree *pBtree,
153ba 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
153bb 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 abase file to ch
153bc 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 52 6f eck */. i64 iRo
153bd 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w,
153be 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74 68 /* The rowid th
153bf 61 74 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e at might be chan
153c0 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 73 ging */. int is
153c1 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20 20 20 ClearTable
153c2 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c /* True if all
153c3 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e 67 20 rows are being
153c4 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 deleted */.){.
153c5 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 42 BtCursor *p;. B
153c6 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
153c7 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 61 73 Btree->pBt;. as
153c8 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
153c9 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 eeHoldsMutex(pBt
153ca 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d ree) );. for(p=
153cb 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
153cc 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
153cd 20 20 69 66 28 20 70 2d 3e 69 73 49 6e 63 72 62 if( p->isIncrb
153ce 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 28 69 73 lobHandle && (is
153cf 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 70 2d ClearTable || p-
153d0 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 >info.nKey==iRow
153d1 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 ) ){. p->eS
153d2 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
153d3 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 20 7d VALID;. }. }
153d4 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 53 .}..#else. /* S
153d5 74 75 62 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 tub functions wh
153d6 65 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73 20 6f en INCRBLOB is o
153d7 6d 69 74 74 65 64 20 2a 2f 0a 20 20 23 64 65 66 mitted */. #def
153d8 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 ine invalidateOv
153d9 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 erflowCache(x).
153da 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 #define invalid
153db 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 ateAllOverflowCa
153dc 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 che(x). #define
153dd 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 invalidateIncrb
153de 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 2c 7a lobCursors(x,y,z
153df 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ).#endif /* SQLI
153e0 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
153e1 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 */../*.** Set b
153e2 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 it pgno of the B
153e3 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
153e4 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 ent bitvec. This
153e5 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 is called .** w
153e6 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 hen a page that
153e7 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 previously conta
153e8 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 ined data become
153e9 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 s a free-list le
153ea 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a af .** page..**.
153eb 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e ** The BtShared.
153ec 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
153ed 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 ec exists to wor
153ee 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 k around an obsc
153ef 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 ure.** bug cause
153f0 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 d by the interac
153f1 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 tion of two usef
153f2 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 ul IO optimizati
153f3 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a ons surrounding.
153f4 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 ** free-list lea
153f5 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 f pages:.**.**
153f6 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 1) When all dat
153f7 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f a is deleted fro
153f8 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 m a page and the
153f9 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a page becomes.**
153fa 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 a free-lis
153fb 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 t leaf page, the
153fc 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 page is not wri
153fd 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
153fe 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 base.** (as
153ff 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
15400 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f pages contain no
15401 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 meaningful data
15402 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ). Sometimes.**
15403 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65 such a page
15404 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 is not even jou
15405 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 rnalled (as it w
15406 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 ill not be modif
15407 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 ied,.** why
15408 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c bother journall
15409 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 ing it?)..**.**
1540a 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2) When a free
1540b 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
1540c 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 is reused, its c
1540d 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 ontent is not re
1540e 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 ad.** from
1540f 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
15410 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
15411 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 ournal file (why
15412 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 should it.**
15413 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 be, if it is
15414 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 not at all meani
15415 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 ngful?)..**.** B
15416 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 y themselves, th
15417 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ese optimization
15418 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 s work fine and
15419 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a provide a handy.
1541a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 ** performance b
1541b 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c oost to bulk del
1541c 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 ete or insert op
1541d 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 erations. Howeve
1541e 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 r, if.** a page
1541f 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
15420 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 free-list and th
15421 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e en reused within
15422 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 the same.** tra
15423 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 nsaction, a prob
15424 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 lem comes up. If
15425 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
15426 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e journalled when
15427 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 .** it is moved
15428 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 to the free-list
15429 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 and it is also
1542a 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 not journalled w
1542b 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 hen it.** is ext
1542c 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 racted from the
1542d 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 free-list and re
1542e 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f used, then the o
1542f 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 riginal data.**
15430 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 may be lost. In
15431 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 the event of a r
15432 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 ollback, it may
15433 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a not be possible.
15434 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ** to restore th
15435 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
15436 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 s original confi
15437 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 guration..**.**
15438 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
15439 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
1543a 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e sContent bitvec.
1543b 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 Whenever a page
1543c 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f is .** moved to
1543d 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c become a free-l
1543e 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
1543f 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
15440 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 bit is.** set i
15441 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 n the bitvec. Wh
15442 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 enever a leaf pa
15443 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ge is extracted
15444 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
15445 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 st,.** optimizat
15446 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f ion 2 above is o
15447 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63 6f mitted if the co
15448 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 rresponding bit
15449 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 65 is already.** se
1544a 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 48 t in BtShared.pH
1544b 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 63 asContent. The c
1544c 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 ontents of the b
1544d 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 65 itvec are cleare
1544e 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 d.** at the end
1544f 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 63 of every transac
15450 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
15451 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 43 int btreeSetHasC
15452 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 ontent(BtShared
15453 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 *pBt, Pgno pgno)
15454 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
15455 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 ITE_OK;. if( !p
15456 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
15457 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 ){. int nPage
15458 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 6c 69 = 100;. sqli
15459 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
1545a 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 t(pBt->pPager, &
1545b 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a 20 49 nPage);. /* I
1545c 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 f sqlite3PagerPa
1545d 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c 73 20 gecount() fails
1545e 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 72 6d there is no harm
1545f 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20 20 because the.
15460 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 61 62 ** nPage variab
15461 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 le is unchanged
15462 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c 74 from its default
15463 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 2a 2f value of 100 */
15464 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43 6f . pBt->pHasCo
15465 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 ntent = sqlite3B
15466 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33 32 itvecCreate((u32
15467 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 )nPage);. if(
15468 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 !pBt->pHasConte
15469 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d nt ){. rc =
1546a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1546b 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 }. }. if( r
1546c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1546d 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 74 pgno<=sqlite3Bit
1546e 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 61 vecSize(pBt->pHa
1546f 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 20 sContent) ){.
15470 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 rc = sqlite3Bit
15471 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61 73 vecSet(pBt->pHas
15472 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a Content, pgno);.
15473 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
15474 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 .}../*.** Query
15475 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
15476 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 2e sContent vector.
15477 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
15478 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 tion is called w
15479 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 20 hen a free-list
1547a 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65 6d leaf page is rem
1547b 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a oved from the.**
1547c 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 72 free-list for r
1547d 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e 73 euse. It returns
1547e 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 false if it is
1547f 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76 65 safe to retrieve
15480 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 6f the.** page fro
15481 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 m the pager laye
15482 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d 63 r with the 'no-c
15483 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65 74 ontent' flag set
15484 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73 65 . True otherwise
15485 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15486 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 65 btreeGetHasConte
15487 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 nt(BtShared *pBt
15488 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
15489 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74 2d Bitvec *p = pBt-
1548a 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 20 >pHasContent;.
1548b 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 67 return (p && (pg
1548c 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65 63 no>sqlite3Bitvec
1548d 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 74 Size(p) || sqlit
1548e 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c 20 e3BitvecTest(p,
1548f 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pgno)));.}../*.*
15490 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f 79 * Clear (destroy
15491 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 ) the BtShared.p
15492 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 HasContent bitve
15493 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 c. This should b
15494 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 20 e.** invoked at
15495 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f the conclusion o
15496 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72 61 f each write-tra
15497 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 nsaction..*/.sta
15498 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43 6c tic void btreeCl
15499 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 earHasContent(Bt
1549a 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
1549b 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
1549c 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 6f troy(pBt->pHasCo
1549d 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e 70 ntent);. pBt->p
1549e 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a HasContent = 0;.
1549f 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 }../*.** Save th
154a0 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 e current cursor
154a1 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 position in the
154a2 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 variables BtCur
154a3 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 sor.nKey .** and
154a4 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e 20 BtCursor.pKey.
154a5 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 61 The cursor's sta
154a6 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 te is set to CUR
154a7 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 2e SOR_REQUIRESEEK.
154a8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 .**.** The calle
154a9 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 r must ensure th
154aa 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 at the cursor is
154ab 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 74 61 valid (has eSta
154ac 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
154ad 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 61 ).** prior to ca
154ae 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
154af 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 ne. .*/.static
154b0 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f int saveCursorPo
154b1 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 sition(BtCursor
154b2 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 *pCur){. int rc
154b3 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52 ;.. assert( CUR
154b4 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d SOR_VALID==pCur-
154b5 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73 >eState );. ass
154b6 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b ert( 0==pCur->pK
154b7 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
154b8 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
154b9 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20 (pCur) );.. rc
154ba 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
154bb 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75 ySize(pCur, &pCu
154bc 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65 r->nKey);. asse
154bd 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
154be 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 7a 65 K ); /* KeySize
154bf 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a () cannot fail *
154c0 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /.. /* If this
154c1 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 is an intKey tab
154c2 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 6f le, then the abo
154c3 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 ve call to Btree
154c4 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 73 KeySize(). ** s
154c5 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 65 tores the intege
154c6 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e 6e r key in pCur->n
154c7 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73 Key. In this cas
154c8 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 0a e this value is.
154c9 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 ** all that is
154ca 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 72 required. Other
154cb 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 73 wise, if pCur is
154cc 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 not open on an
154cd 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 6c intKey. ** tabl
154ce 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 73 e, then malloc s
154cf 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 6f pace for and sto
154d0 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b 65 re the pCur->nKe
154d1 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 0a y bytes of key .
154d2 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f 0a ** data.. */.
154d3 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d 3e 61 if( 0==pCur->a
154d4 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
154d5 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b ){. void *pK
154d6 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ey = sqlite3Mall
154d7 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e oc( (int)pCur->n
154d8 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 70 Key );. if( p
154d9 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Key ){. rc
154da 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 = sqlite3BtreeKe
154db 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 y(pCur, 0, (int)
154dc 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 pCur->nKey, pKey
154dd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
154de 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
154df 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 pCur->pKey
154e0 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 7d = pKey;. }
154e1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
154e2 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29 lite3_free(pKey)
154e3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
154e4 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
154e5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
154e6 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
154e7 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ( !pCur->apPage[
154e8 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70 0]->intKey || !p
154e9 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 Cur->pKey );..
154ea 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
154eb 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a K ){. int i;.
154ec 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d for(i=0; i<=
154ed 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b pCur->iPage; i++
154ee 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
154ef 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 Page(pCur->apPag
154f0 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 75 e[i]);. pCu
154f1 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 r->apPage[i] = 0
154f2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 ;. }. pCur
154f3 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 ->iPage = -1;.
154f4 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
154f5 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
154f6 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 EEK;. }.. inva
154f7 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 lidateOverflowCa
154f8 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 74 che(pCur);. ret
154f9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
154fa 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 Save the positi
154fb 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f ons of all curso
154fc 72 73 20 28 65 78 63 65 70 74 20 70 45 78 63 65 rs (except pExce
154fd 70 74 29 20 74 68 61 74 20 61 72 65 20 6f 70 65 pt) that are ope
154fe 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 61 62 6c n on.** the tabl
154ff 65 20 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 e with root-pag
15500 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c 79 e iRoot. Usually
15501 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 , this is called
15502 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75 72 just before cur
15503 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20 69 sor.** pExcept i
15504 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66 79 s used to modify
15505 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 72 65 the table (Btre
15506 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74 72 eDelete() or Btr
15507 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f 0a eeInsert())..*/.
15508 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 41 static int saveA
15509 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 llCursors(BtShar
1550a 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69 52 ed *pBt, Pgno iR
1550b 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 oot, BtCursor *p
1550c 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75 72 Except){. BtCur
1550d 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 sor *p;. assert
1550e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1550f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
15510 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
15511 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78 63 xcept==0 || pExc
15512 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b ept->pBt==pBt );
15513 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 . for(p=pBt->pC
15514 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 ursor; p; p=p->p
15515 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 Next){. if( p
15516 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30 3d !=pExcept && (0=
15517 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67 6e =iRoot || p->pgn
15518 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26 26 oRoot==iRoot) &&
15519 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53 74 . p->eSt
1551a 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
1551b 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 D ){. int r
1551c 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f c = saveCursorPo
1551d 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20 20 sition(p);.
1551e 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d if( SQLITE_OK!=
1551f 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 rc ){. re
15520 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
15521 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
15522 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
15523 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 ../*.** Clear th
15524 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 e current cursor
15525 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 position..*/.SQ
15526 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
15527 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c d sqlite3BtreeCl
15528 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 earCursor(BtCurs
15529 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
1552a 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1552b 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
1552c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
1552d 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43 75 ur->pKey);. pCu
1552e 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20 70 r->pKey = 0;. p
1552f 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
15530 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d 0a RSOR_INVALID;.}.
15531 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 76 ./*.** In this v
15532 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 65 4d ersion of BtreeM
15533 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 20 61 oveto, pKey is a
15534 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 72 65 packed index re
15535 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 73 20 cord.** such as
15536 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 is generated by
15537 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 the OP_MakeRecor
15538 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 61 63 d opcode. Unpac
15539 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 k the.** record
1553a 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 42 74 and then call Bt
1553b 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1553c 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 77 6f d() to do the wo
1553d 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e rk..*/.static in
1553e 74 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 0a 20 t btreeMoveto(.
1553f 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c BtCursor *pCur,
15540 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f /* Cursor o
15541 70 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 65 65 pen on the btree
15542 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 20 to be searched
15543 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
15544 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 63 6b *pKey, /* Pack
15545 65 64 20 6b 65 79 20 69 66 20 74 68 65 20 62 74 ed key if the bt
15546 72 65 65 20 69 73 20 61 6e 20 69 6e 64 65 78 20 ree is an index
15547 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c 20 20 */. i64 nKey,
15548 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 /* Inte
15549 67 65 72 20 6b 65 79 20 66 6f 72 20 74 61 62 6c ger key for tabl
1554a 65 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 4b 65 es. Size of pKe
1554b 79 20 66 6f 72 20 69 6e 64 69 63 65 73 20 2a 2f y for indices */
1554c 0a 20 20 69 6e 74 20 62 69 61 73 2c 20 20 20 20 . int bias,
1554d 20 20 20 20 20 20 20 2f 2a 20 42 69 61 73 20 73 /* Bias s
1554e 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 earch to the hig
1554f 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a h end */. int *
15550 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 2f pRes /
15551 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72 * Write search r
15552 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 esults here */.)
15553 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
15554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15555 2a 20 53 74 61 74 75 73 20 63 6f 64 65 20 2a 2f * Status code */
15556 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
15557 64 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 2f 2a d *pIdxKey; /*
15558 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 Unpacked index
15559 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20 61 53 key */. char aS
1555a 70 61 63 65 5b 31 35 30 5d 3b 20 20 20 20 20 20 pace[150];
1555b 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 /* Temp spac
1555c 65 20 66 6f 72 20 70 49 64 78 4b 65 79 20 2d 20 e for pIdxKey -
1555d 74 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c 6c 6f to avoid a mallo
1555e 63 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4b 65 79 c */.. if( pKey
1555f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
15560 6e 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e 74 29 nKey==(i64)(int)
15561 6e 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 64 78 nKey );. pIdx
15562 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Key = sqlite3Vdb
15563 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 70 43 eRecordUnpack(pC
15564 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 28 69 ur->pKeyInfo, (i
15565 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c 0a 20 nt)nKey, pKey,.
15566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15567 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15568 20 20 20 20 20 61 53 70 61 63 65 2c 20 73 69 7a aSpace, siz
15569 65 6f 66 28 61 53 70 61 63 65 29 29 3b 0a 20 20 eof(aSpace));.
1556a 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 if( pIdxKey==0
1556b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
1556c 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b _NOMEM;. }else{
1556d 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 30 . pIdxKey = 0
1556e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c ;. }. rc = sql
1556f 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
15570 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20 70 49 npacked(pCur, pI
15571 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 69 61 dxKey, nKey, bia
15572 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 28 20 s, pRes);. if(
15573 70 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 pKey ){. sqli
15574 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 te3VdbeDeleteUnp
15575 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64 78 ackedRecord(pIdx
15576 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Key);. }. retu
15577 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
15578 52 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 73 Restore the curs
15579 6f 72 20 74 6f 20 74 68 65 20 70 6f 73 69 74 69 or to the positi
1557a 6f 6e 20 69 74 20 77 61 73 20 69 6e 20 28 6f 72 on it was in (or
1557b 20 61 73 20 63 6c 6f 73 65 20 74 6f 20 61 73 20 as close to as
1557c 70 6f 73 73 69 62 6c 65 29 0a 2a 2a 20 77 68 65 possible).** whe
1557d 6e 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 n saveCursorPosi
1557e 74 69 6f 6e 28 29 20 77 61 73 20 63 61 6c 6c 65 tion() was calle
1557f 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 d. Note that thi
15580 73 20 63 61 6c 6c 20 64 65 6c 65 74 65 73 20 74 s call deletes t
15581 68 65 20 0a 2a 2a 20 73 61 76 65 64 20 70 6f 73 he .** saved pos
15582 69 74 69 6f 6e 20 69 6e 66 6f 20 73 74 6f 72 65 ition info store
15583 64 20 62 79 20 73 61 76 65 43 75 72 73 6f 72 50 d by saveCursorP
15584 6f 73 69 74 69 6f 6e 28 29 2c 20 73 6f 20 74 68 osition(), so th
15585 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 61 74 ere can be.** at
15586 20 6d 6f 73 74 20 6f 6e 65 20 65 66 66 65 63 74 most one effect
15587 69 76 65 20 72 65 73 74 6f 72 65 43 75 72 73 6f ive restoreCurso
15588 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6c 6c rPosition() call
15589 20 61 66 74 65 72 20 65 61 63 68 20 0a 2a 2a 20 after each .**
1558a 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 saveCursorPositi
1558b 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 on()..*/.static
1558c 69 6e 74 20 62 74 72 65 65 52 65 73 74 6f 72 65 int btreeRestore
1558d 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 CursorPosition(B
1558e 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
1558f 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
15590 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
15591 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
15592 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
15593 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 tate>=CURSOR_REQ
15594 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 UIRESEEK );. if
15595 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
15596 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a CURSOR_FAULT ){.
15597 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d return pCur-
15598 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 >skipNext;. }.
15599 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
1559a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
1559b 20 20 72 63 20 3d 20 62 74 72 65 65 4d 6f 76 65 rc = btreeMove
1559c 74 6f 28 70 43 75 72 2c 20 70 43 75 72 2d 3e 70 to(pCur, pCur->p
1559d 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c Key, pCur->nKey,
1559e 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b 69 70 4e 0, &pCur->skipN
1559f 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ext);. if( rc==
155a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
155a1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
155a2 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 70 ur->pKey);. p
155a3 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 Cur->pKey = 0;.
155a4 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
155a5 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
155a6 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 VALID || pCur->e
155a7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e State==CURSOR_IN
155a8 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20 20 72 VALID );. }. r
155a9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 64 65 eturn rc;.}..#de
155aa 66 69 6e 65 20 72 65 73 74 6f 72 65 43 75 72 73 fine restoreCurs
155ab 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 5c 0a orPosition(p) \.
155ac 20 20 28 70 2d 3e 65 53 74 61 74 65 3e 3d 43 55 (p->eState>=CU
155ad 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
155ae 20 3f 20 5c 0a 20 20 20 20 20 20 20 20 20 62 74 ? \. bt
155af 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 reeRestoreCursor
155b0 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0a Position(p) : \.
155b1 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
155b2 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 OK)../*.** Deter
155b3 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 mine whether or
155b4 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68 61 73 not a cursor has
155b5 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 moved from the
155b6 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a 20 77 position it.** w
155b7 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64 20 61 as last placed a
155b8 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61 6e 20 t. Cursors can
155b9 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20 72 6f move when the ro
155ba 77 20 74 68 65 79 20 61 72 65 20 70 6f 69 6e 74 w they are point
155bb 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64 65 6c ing.** at is del
155bc 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e eted out from un
155bd 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 der them..**.**
155be 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
155bf 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f urns an error co
155c0 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 de if something
155c1 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54 68 65 goes wrong. The
155c2 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70 48 61 .** integer *pHa
155c3 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f sMoved is set to
155c4 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75 72 73 one if the curs
155c5 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61 6e 64 or has moved and
155c6 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 0 if not..*/.SQ
155c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
155c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
155c9 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 sorHasMoved(BtCu
155ca 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
155cb 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20 20 69 *pHasMoved){. i
155cc 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 nt rc;.. rc = r
155cd 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
155ce 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 tion(pCur);. if
155cf 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 48 61 ( rc ){. *pHa
155d0 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 sMoved = 1;.
155d1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
155d2 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 if( pCur->eStat
155d3 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e!=CURSOR_VALID
155d4 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 || pCur->skipNex
155d5 74 21 3d 30 20 29 7b 0a 20 20 20 20 2a 70 48 61 t!=0 ){. *pHa
155d6 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 sMoved = 1;. }e
155d7 6c 73 65 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f lse{. *pHasMo
155d8 76 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 ved = 0;. }. r
155d9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
155da 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
155db 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
155dc 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 UM./*.** Given a
155dd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
155de 61 20 72 65 67 75 6c 61 72 20 64 61 74 61 62 61 a regular databa
155df 73 65 20 70 61 67 65 2c 20 72 65 74 75 72 6e 20 se page, return
155e0 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 the page.** numb
155e1 65 72 20 66 6f 72 20 74 68 65 20 70 6f 69 6e 74 er for the point
155e2 65 72 2d 6d 61 70 20 70 61 67 65 20 74 68 61 74 er-map page that
155e3 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e contains the en
155e4 74 72 79 20 66 6f 72 20 74 68 65 0a 2a 2a 20 69 try for the.** i
155e5 6e 70 75 74 20 70 61 67 65 20 6e 75 6d 62 65 72 nput page number
155e6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f ..*/.static Pgno
155e7 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 42 74 ptrmapPageno(Bt
155e8 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
155e9 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e o pgno){. int n
155ea 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b PagesPerMapPage;
155eb 0a 20 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c . Pgno iPtrMap,
155ec 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 ret;. assert(
155ed 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
155ee 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
155ef 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d 61 70 ;. nPagesPerMap
155f0 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 73 61 Page = (pBt->usa
155f1 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 bleSize/5)+1;.
155f2 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e 6f 2d iPtrMap = (pgno-
155f3 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 70 50 2)/nPagesPerMapP
155f4 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 69 50 age;. ret = (iP
155f5 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 72 4d trMap*nPagesPerM
155f6 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a 20 20 apPage) + 2; .
155f7 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 4e 47 if( ret==PENDING
155f8 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
155f9 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a 20 20 ){. ret++;.
155fa 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a }. return ret;.
155fb 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 }../*.** Write a
155fc 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 n entry into the
155fd 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a pointer map..**
155fe 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
155ff 20 75 70 64 61 74 65 73 20 74 68 65 20 70 6f 69 updates the poi
15600 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 nter map entry f
15601 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 or page number '
15602 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 74 20 key'.** so that
15603 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 65 20 it maps to type
15604 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 72 65 'eType' and pare
15605 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 27 nt page number '
15606 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pgno'..**.** If
15607 2a 70 52 43 20 69 73 20 69 6e 69 74 69 61 6c 6c *pRC is initiall
15608 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f 6e 2d y non-zero (non-
15609 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 65 6e 20 SQLITE_OK) then
1560a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a this routine is.
1560b 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 ** a no-op. If
1560c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
1560d 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
1560e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 77 error code is w
1560f 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a ritten.** into *
15610 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 pRC..*/.static v
15611 6f 69 64 20 70 74 72 6d 61 70 50 75 74 28 42 74 oid ptrmapPut(Bt
15612 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e Shared *pBt, Pgn
15613 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 65 2c o key, u8 eType,
15614 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c 20 69 6e Pgno parent, in
15615 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 50 61 67 t *pRC){. DbPag
15616 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 e *pDbPage; /*
15617 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
15618 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50 page */. u8 *pP
15619 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 trmap; /* T
1561a 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 he pointer map d
1561b 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 ata */. Pgno iP
1561c 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68 trmap; /* Th
1561d 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
1561e 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 ge number */. i
1561f 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 nt offset;
15620 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f /* Offset in po
15621 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a inter map page *
15622 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
15623 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
15624 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66 75 6e code from subfun
15625 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69 66 28 ctions */.. if(
15626 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a *pRC ) return;.
15627 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15628 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
15629 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f t->mutex) );. /
1562a 2a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 * The master-jou
1562b 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 rnal page number
1562c 20 6d 75 73 74 20 6e 65 76 65 72 20 62 65 20 75 must never be u
1562d 73 65 64 20 61 73 20 61 20 70 6f 69 6e 74 65 72 sed as a pointer
1562e 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 61 map page */. a
1562f 73 73 65 72 74 28 20 30 3d 3d 50 54 52 4d 41 50 ssert( 0==PTRMAP
15630 5f 49 53 50 41 47 45 28 70 42 74 2c 20 50 45 4e _ISPAGE(pBt, PEN
15631 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
15632 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 Bt)) );.. asser
15633 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 t( pBt->autoVacu
15634 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 3d um );. if( key=
15635 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d =0 ){. *pRC =
15636 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15637 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75 72 6e BKPT;. return
15638 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20 ;. }. iPtrmap
15639 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 = PTRMAP_PAGENO(
1563a 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 pBt, key);. rc
1563b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
1563c 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
1563d 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 Ptrmap, &pDbPage
1563e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1563f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 ITE_OK ){. *p
15640 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 RC = rc;. ret
15641 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 urn;. }. offse
15642 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 t = PTRMAP_PTROF
15643 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 FSET(iPtrmap, ke
15644 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 65 74 y);. if( offset
15645 3c 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d <0 ){. *pRC =
15646 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15647 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 70 BKPT;. goto p
15648 74 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20 7d 0a trmap_exit;. }.
15649 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 pPtrmap = (u8
1564a 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
1564b 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a tData(pDbPage);.
1564c 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 70 50 . if( eType!=pP
1564d 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 7c 7c trmap[offset] ||
1564e 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d get4byte(&pPtrm
1564f 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 3d 70 ap[offset+1])!=p
15650 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 52 41 arent ){. TRA
15651 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50 44 41 CE(("PTRMAP_UPDA
15652 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 29 5c TE: %d->(%d,%d)\
15653 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 2c 20 n", key, eType,
15654 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 2a 70 parent));. *p
15655 52 43 3d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 RC= rc = sqlite3
15656 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 PagerWrite(pDbPa
15657 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ge);. if( rc=
15658 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15659 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 pPtrmap[offs
1565a 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 et] = eType;.
1565b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 74 put4byte(&pPt
1565c 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 rmap[offset+1],
1565d 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 parent);. }.
1565e 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 78 69 74 3a }..ptrmap_exit:
1565f 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 . sqlite3PagerU
15660 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 7d nref(pDbPage);.}
15661 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 ../*.** Read an
15662 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70 entry from the p
15663 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a ointer map..**.*
15664 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
15665 65 74 72 69 65 76 65 73 20 74 68 65 20 70 6f 69 etrieves the poi
15666 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66 nter map entry f
15667 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77 or page 'key', w
15668 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 riting.** the ty
15669 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 pe and parent pa
1566a 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 ge number to *pE
1566b 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 Type and *pPgno
1566c 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a respectively..**
1566d 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 An error code i
1566e 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f s returned if so
1566f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f mething goes wro
15670 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51 ng, otherwise SQ
15671 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 LITE_OK..*/.stat
15672 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74 ic int ptrmapGet
15673 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 (BtShared *pBt,
15674 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 Pgno key, u8 *pE
15675 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e Type, Pgno *pPgn
15676 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
15677 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 bPage; /* The
15678 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
15679 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 */. int iPtrma
1567a 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e p; /* Poin
1567b 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64 ter map page ind
1567c 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 ex */. u8 *pPtr
1567d 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f map; /* Po
1567e 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 64 inter map page d
1567f 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 ata */. int off
15680 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f set; /* O
15681 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69 ffset of entry i
15682 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f n pointer map */
15683 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
15684 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15685 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
15686 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d tex) );.. iPtrm
15687 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 ap = PTRMAP_PAGE
15688 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 NO(pBt, key);.
15689 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1568a 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 rGet(pBt->pPager
1568b 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 , iPtrmap, &pDbP
1568c 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
1568d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1568e 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 rc;. }. pPtrma
1568f 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 p = (u8 *)sqlite
15690 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 3PagerGetData(pD
15691 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 bPage);.. offse
15692 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 t = PTRMAP_PTROF
15693 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 FSET(iPtrmap, ke
15694 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 y);. assert( pE
15695 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 Type!=0 );. *pE
15696 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f Type = pPtrmap[o
15697 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 ffset];. if( pP
15698 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 gno ) *pPgno = g
15699 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 et4byte(&pPtrmap
1569a 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 [offset+1]);..
1569b 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1569c 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 f(pDbPage);. if
1569d 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a ( *pEType<1 || *
1569e 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 75 72 pEType>5 ) retur
1569f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
156a0 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 _BKPT;. return
156a1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 SQLITE_OK;.}..#e
156a2 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 lse /* if define
156a3 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 d SQLITE_OMIT_AU
156a4 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64 TOVACUUM */. #d
156a5 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 28 efine ptrmapPut(
156a6 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a 20 20 23 64 w,x,y,z,rc). #d
156a7 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 74 28 efine ptrmapGet(
156a8 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f w,x,y,z) SQLITE_
156a9 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 OK. #define ptr
156aa 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 78 2c mapPutOvflPtr(x,
156ab 20 79 2c 20 72 63 29 0a 23 65 6e 64 69 66 0a 0a y, rc).#endif..
156ac 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 62 74 /*.** Given a bt
156ad 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 20 63 ree page and a c
156ae 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d 65 61 ell index (0 mea
156af 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 65 6c ns the first cel
156b0 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 l on.** the page
156b1 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 73 65 , 1 means the se
156b2 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 20 73 cond cell, and s
156b3 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 6e 20 o forth) return
156b4 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
156b5 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
156b6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
156b7 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c 79 20 tine works only
156b8 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 20 64 for pages that d
156b9 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 6f 76 o not contain ov
156ba 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f erflow cells..*/
156bb 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 65 6c .#define findCel
156bc 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 29 2d l(P,I) \. ((P)-
156bd 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d 3e 6d >aData + ((P)->m
156be 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 62 79 askPage & get2by
156bf 74 65 28 26 28 50 29 2d 3e 61 44 61 74 61 5b 28 te(&(P)->aData[(
156c0 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2b 32 P)->cellOffset+2
156c1 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 *(I)])))../*.**
156c2 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f 6d 70 This a more comp
156c3 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66 20 66 lex version of f
156c4 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74 20 77 indCell() that w
156c5 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 67 65 orks for.** page
156c6 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74 61 69 s that do contai
156c7 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 n overflow cells
156c8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 2a ..*/.static u8 *
156c9 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c findOverflowCell
156ca 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
156cb 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 69 int iCell){. i
156cc 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
156cd 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
156ce 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
156cf 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
156d0 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f =pPage->nOverflo
156d1 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b w-1; i>=0; i--){
156d2 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 . int k;.
156d3 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c struct _OvflCell
156d4 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70 4f 76 *pOvfl;. pOv
156d5 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61 4f 76 fl = &pPage->aOv
156d6 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d 20 70 fl[i];. k = p
156d7 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 20 69 Ovfl->idx;. i
156d8 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b 0a 20 f( k<=iCell ){.
156d9 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 65 6c if( k==iCel
156da 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 l ){. ret
156db 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 6c 6c urn pOvfl->pCell
156dc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
156dd 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d 0a 20 iCell--;. }.
156de 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 6e 64 }. return find
156df 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
156e0 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 l);.}../*.** Par
156e1 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 65 6e se a cell conten
156e2 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 6c 6c t block and fill
156e3 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e 66 6f in the CellInfo
156e4 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
156e5 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 76 65 re.** are two ve
156e6 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 66 rsions of this f
156e7 75 6e 63 74 69 6f 6e 2e 20 20 62 74 72 65 65 50 unction. btreeP
156e8 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b 65 73 arseCell() takes
156e9 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 a .** cell inde
156ea 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 x as the second
156eb 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 62 74 72 argument and btr
156ec 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 eeParseCellPtr()
156ed 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69 .** takes a poi
156ee 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79 nter to the body
156ef 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20 of the cell as
156f0 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d its second argum
156f1 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 ent..**.** Withi
156f2 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 n this file, the
156f3 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63 parseCell() mac
156f4 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 ro can be called
156f5 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62 instead of.** b
156f6 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
156f7 28 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65 20 63 (). Using some c
156f8 6f 6d 70 69 6c 65 72 73 2c 20 74 68 69 73 20 77 ompilers, this w
156f9 69 6c 6c 20 62 65 20 66 61 73 74 65 72 2e 0a 2a ill be faster..*
156fa 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 /.static void bt
156fb 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
156fc 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
156fd 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 e, /* Pa
156fe 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ge containing th
156ff 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a e cell */. u8 *
15700 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 pCell,
15701 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
15702 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 78 74 2e o the cell text.
15703 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a */. CellInfo *
15704 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a pInfo /*
15705 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 Fill in this st
15706 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 ructure */.){.
15707 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20 20 20 u16 n;
15708 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15709 72 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 20 r bytes in cell
1570a 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 2a content header *
1570b 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 /. u32 nPayload
1570c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1570d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1570e 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64 20 2a f cell payload *
1570f 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c /.. assert( sql
15710 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15711 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
15712 78 29 20 29 3b 0a 0a 20 20 70 49 6e 66 6f 2d 3e x) );.. pInfo->
15713 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 pCell = pCell;.
15714 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15715 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61 67 65 leaf==0 || pPage
15716 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 20 6e ->leaf==1 );. n
15717 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 = pPage->childP
15718 74 72 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 trSize;. assert
15719 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 2d 3e ( n==4-4*pPage->
1571a 6c 65 61 66 20 29 3b 0a 20 20 69 66 28 20 70 50 leaf );. if( pP
1571b 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
1571c 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 if( pPage->ha
1571d 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6e sData ){. n
1571e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 += getVarint32(
1571f 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c &pCell[n], nPayl
15720 6f 61 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b oad);. }else{
15721 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 . nPayload
15722 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e = 0;. }. n
15723 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28 26 70 += getVarint(&p
15724 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a 29 26 Cell[n], (u64*)&
15725 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a 20 20 pInfo->nKey);.
15726 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d pInfo->nData =
15727 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 65 6c nPayload;. }el
15728 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e se{. pInfo->n
15729 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 6e 20 Data = 0;. n
1572a 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 += getVarint32(&
1572b 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 6c 6f pCell[n], nPaylo
1572c 61 64 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e ad);. pInfo->
1572d 6e 4b 65 79 20 3d 20 6e 50 61 79 6c 6f 61 64 3b nKey = nPayload;
1572e 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 6e 50 . }. pInfo->nP
1572f 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c 6f 61 ayload = nPayloa
15730 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 65 61 d;. pInfo->nHea
15731 64 65 72 20 3d 20 6e 3b 0a 20 20 74 65 73 74 63 der = n;. testc
15732 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 ase( nPayload==p
15733 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 Page->maxLocal )
15734 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 ;. testcase( nP
15735 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d ayload==pPage->m
15736 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69 axLocal+1 );. i
15737 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 6c 6f f( likely(nPaylo
15738 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ad<=pPage->maxLo
15739 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 cal) ){. /* T
1573a 68 69 73 20 69 73 20 74 68 65 20 28 65 61 73 79 his is the (easy
1573b 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 ) common case wh
1573c 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 20 70 ere the entire p
1573d 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 20 20 ayload fits.
1573e 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ** on the local
1573f 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 66 6c page. No overfl
15740 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a ow is required..
15741 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e */. int n
15742 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 2f Size; /
15743 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 * Total size of
15744 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 cell content in
15745 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e 53 69 bytes */. nSi
15746 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 2b 20 ze = nPayload +
15747 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c n;. pInfo->nL
15748 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 61 79 ocal = (u16)nPay
15749 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d load;. pInfo-
1574a 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >iOverflow = 0;.
1574b 20 20 20 20 69 66 28 20 28 6e 53 69 7a 65 20 26 if( (nSize &
1574c 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ~3)==0 ){.
1574d 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 nSize = 4;
1574e 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 /* Minimum ce
1574f 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a ll size is 4 */.
15750 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d }. pInfo-
15751 3e 6e 53 69 7a 65 20 3d 20 28 75 31 36 29 6e 53 >nSize = (u16)nS
15752 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
15753 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 79 6c /* If the payl
15754 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 oad will not fit
15755 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e 20 74 completely on t
15756 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c 20 77 he local page, w
15757 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f e have. ** to
15758 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 63 68 decide how much
15759 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c to store locall
1575a 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 20 74 y and how much t
1575b 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 20 20 o spill onto.
1575c 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ** overflow pag
1575d 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 65 67 es. The strateg
1575e 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 y is to minimize
1575f 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 75 the amount of u
15760 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 70 61 nused. ** spa
15761 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ce on overflow p
15762 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 70 69 ages while keepi
15763 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ng the amount of
15764 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 0a 20 local storage.
15765 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e ** in between
15766 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 6d 61 minLocal and ma
15767 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a 0a 20 xLocal.. **.
15768 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a 20 20 ** Warning:
15769 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 61 79 changing the way
1576a 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 overflow payloa
1576b 64 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64 d is distributed
1576c 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 77 in any. ** w
1576d 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 ay will result i
1576e 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c n an incompatibl
1576f 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 20 e file format..
15770 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 69 */. int mi
15771 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 6e 69 nLocal; /* Mini
15772 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 mum amount of pa
15773 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c yload held local
15774 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61 ly */. int ma
15775 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 78 69 xLocal; /* Maxi
15776 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 mum amount of pa
15777 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c yload held local
15778 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 75 ly */. int su
15779 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 65 72 rplus; /* Over
1577a 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 76 61 flow payload ava
1577b 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 61 6c ilable for local
1577c 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 20 20 storage */..
1577d 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 minLocal = pPag
1577e 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 e->minLocal;.
1577f 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 61 67 maxLocal = pPag
15780 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 20 e->maxLocal;.
15781 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e 4c 6f surplus = minLo
15782 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 64 20 cal + (nPayload
15783 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 50 61 - minLocal)%(pPa
15784 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
15785 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65 ize - 4);. te
15786 73 74 63 61 73 65 28 20 73 75 72 70 6c 75 73 3d stcase( surplus=
15787 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 =maxLocal );.
15788 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70 6c testcase( surpl
15789 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 us==maxLocal+1 )
1578a 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c 75 ;. if( surplu
1578b 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b s <= maxLocal ){
1578c 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c . pInfo->nL
1578d 6f 63 61 6c 20 3d 20 28 75 31 36 29 73 75 72 70 ocal = (u16)surp
1578e 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a lus;. }else{.
1578f 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f pInfo->nLo
15790 63 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f cal = (u16)minLo
15791 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 cal;. }. p
15792 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 Info->iOverflow
15793 3d 20 28 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e = (u16)(pInfo->n
15794 4c 6f 63 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20 Local + n);.
15795 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 pInfo->nSize = p
15796 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 Info->iOverflow
15797 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 + 4;. }.}.#defi
15798 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 ne parseCell(pPa
15799 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f ge, iCell, pInfo
1579a 29 20 5c 0a 20 20 62 74 72 65 65 50 61 72 73 65 ) \. btreeParse
1579b 43 65 6c 6c 50 74 72 28 28 70 50 61 67 65 29 2c CellPtr((pPage),
1579c 20 66 69 6e 64 43 65 6c 6c 28 28 70 50 61 67 65 findCell((pPage
1579d 29 2c 20 28 69 43 65 6c 6c 29 29 2c 20 28 70 49 ), (iCell)), (pI
1579e 6e 66 6f 29 29 0a 73 74 61 74 69 63 20 76 6f 69 nfo)).static voi
1579f 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c d btreeParseCell
157a0 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 (. MemPage *pPa
157a1 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 ge, /* P
157a2 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 age containing t
157a3 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 he cell */. int
157a4 20 69 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 iCell,
157a5 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c /* The cell
157a6 20 69 6e 64 65 78 2e 20 20 46 69 72 73 74 20 63 index. First c
157a7 65 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 20 43 65 ell is 0 */. Ce
157a8 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 llInfo *pInfo
157a9 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e /* Fill in
157aa 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
157ab 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 65 43 65 6c */.){. parseCel
157ac 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 l(pPage, iCell,
157ad 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pInfo);.}../*.**
157ae 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 Compute the tot
157af 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
157b0 65 73 20 74 68 61 74 20 61 20 43 65 6c 6c 20 6e es that a Cell n
157b1 65 65 64 73 20 69 6e 20 74 68 65 20 63 65 6c 6c eeds in the cell
157b2 0a 2a 2a 20 64 61 74 61 20 61 72 65 61 20 6f 66 .** data area of
157b3 20 74 68 65 20 62 74 72 65 65 2d 70 61 67 65 2e the btree-page.
157b4 20 20 54 68 65 20 72 65 74 75 72 6e 20 6e 75 6d The return num
157b5 62 65 72 20 69 6e 63 6c 75 64 65 73 20 74 68 65 ber includes the
157b6 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 68 65 cell.** data he
157b7 61 64 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 ader and the loc
157b8 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 75 74 20 al payload, but
157b9 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 not any overflow
157ba 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 68 65 20 page or.** the
157bb 73 70 61 63 65 20 75 73 65 64 20 62 79 20 74 68 space used by th
157bc 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a e cell pointer..
157bd 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 */.static u16 ce
157be 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50 61 67 llSizePtr(MemPag
157bf 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 e *pPage, u8 *pC
157c0 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 49 74 65 ell){. u8 *pIte
157c1 72 20 3d 20 26 70 43 65 6c 6c 5b 70 50 61 67 65 r = &pCell[pPage
157c2 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 5d 3b ->childPtrSize];
157c3 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b 0a 0a 23 . u32 nSize;..#
157c4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
157c5 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 UG. /* The valu
157c6 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 e returned by th
157c7 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 is function shou
157c8 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 ld always be the
157c9 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a 20 74 68 same as. ** th
157ca 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 69 7a e (CellInfo.nSiz
157cb 65 29 20 76 61 6c 75 65 20 66 6f 75 6e 64 20 62 e) value found b
157cc 79 20 64 6f 69 6e 67 20 61 20 66 75 6c 6c 20 70 y doing a full p
157cd 61 72 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a arse of the. **
157ce 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c 49 54 45 cell. If SQLITE
157cf 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 _DEBUG is define
157d0 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 20 61 d, an assert() a
157d1 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 0a t the bottom of.
157d2 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 ** this functi
157d3 6f 6e 20 76 65 72 69 66 69 65 73 20 74 68 61 74 on verifies that
157d4 20 74 68 69 73 20 69 6e 76 61 72 69 61 6e 74 20 this invariant
157d5 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 65 64 2e is not violated.
157d6 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 64 */. CellInfo d
157d7 65 62 75 67 69 6e 66 6f 3b 0a 20 20 62 74 72 65 ebuginfo;. btre
157d8 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
157d9 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 64 65 62 age, pCell, &deb
157da 75 67 69 6e 66 6f 29 3b 0a 23 65 6e 64 69 66 0a uginfo);.#endif.
157db 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e . if( pPage->in
157dc 74 4b 65 79 20 29 7b 0a 20 20 20 20 75 38 20 2a tKey ){. u8 *
157dd 70 45 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 50 pEnd;. if( pP
157de 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a age->hasData ){.
157df 20 20 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 pIter += g
157e0 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 etVarint32(pIter
157e1 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 7d 65 , nSize);. }e
157e2 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 lse{. nSize
157e3 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 = 0;. }..
157e4 20 2f 2a 20 70 49 74 65 72 20 6e 6f 77 20 70 6f /* pIter now po
157e5 69 6e 74 73 20 61 74 20 74 68 65 20 36 34 2d 62 ints at the 64-b
157e6 69 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 76 it integer key v
157e7 61 6c 75 65 2c 20 61 20 76 61 72 69 61 62 6c 65 alue, a variable
157e8 20 6c 65 6e 67 74 68 20 0a 20 20 20 20 2a 2a 20 length . **
157e9 69 6e 74 65 67 65 72 2e 20 54 68 65 20 66 6f 6c integer. The fol
157ea 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 6f 76 lowing block mov
157eb 65 73 20 70 49 74 65 72 20 74 6f 20 70 6f 69 6e es pIter to poin
157ec 74 20 61 74 20 74 68 65 20 66 69 72 73 74 20 62 t at the first b
157ed 79 74 65 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 yte. ** past
157ee 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6b the end of the k
157ef 65 79 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 ey value. */.
157f0 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72 5b 39 pEnd = &pIter[9
157f1 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a ];. while( (*
157f2 70 49 74 65 72 2b 2b 29 26 30 78 38 30 20 26 26 pIter++)&0x80 &&
157f3 20 70 49 74 65 72 3c 70 45 6e 64 20 29 3b 0a 20 pIter<pEnd );.
157f4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 74 65 }else{. pIte
157f5 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 r += getVarint32
157f6 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a (pIter, nSize);.
157f7 20 20 7d 0a 0a 20 20 74 65 73 74 63 61 73 65 28 }.. testcase(
157f8 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d nSize==pPage->m
157f9 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 axLocal );. tes
157fa 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
157fb 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 age->maxLocal+1
157fc 29 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 3e 70 );. if( nSize>p
157fd 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 Page->maxLocal )
157fe 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 {. int minLoc
157ff 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c al = pPage->minL
15800 6f 63 61 6c 3b 0a 20 20 20 20 6e 53 69 7a 65 20 ocal;. nSize
15801 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53 = minLocal + (nS
15802 69 7a 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20 ize - minLocal)
15803 25 20 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 % (pPage->pBt->u
15804 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a sableSize - 4);.
15805 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 testcase( nS
15806 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c ize==pPage->maxL
15807 6f 63 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 ocal );. test
15808 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 case( nSize==pPa
15809 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 ge->maxLocal+1 )
1580a 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 7a 65 3e ;. if( nSize>
1580b 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 pPage->maxLocal
1580c 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d ){. nSize =
1580d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d minLocal;. }
1580e 0a 20 20 20 20 6e 53 69 7a 65 20 2b 3d 20 34 3b . nSize += 4;
1580f 0a 20 20 7d 0a 20 20 6e 53 69 7a 65 20 2b 3d 20 . }. nSize +=
15810 28 75 33 32 29 28 70 49 74 65 72 20 2d 20 70 43 (u32)(pIter - pC
15811 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 ell);.. /* The
15812 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 minimum size of
15813 61 6e 79 20 63 65 6c 6c 20 69 73 20 34 20 62 79 any cell is 4 by
15814 74 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 53 tes. */. if( nS
15815 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 6e 53 69 ize<4 ){. nSi
15816 7a 65 20 3d 20 34 3b 0a 20 20 7d 0a 0a 20 20 61 ze = 4;. }.. a
15817 73 73 65 72 74 28 20 6e 53 69 7a 65 3d 3d 64 65 ssert( nSize==de
15818 62 75 67 69 6e 66 6f 2e 6e 53 69 7a 65 20 29 3b buginfo.nSize );
15819 0a 20 20 72 65 74 75 72 6e 20 28 75 31 36 29 6e . return (u16)n
1581a 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 Size;.}..#ifdef
1581b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 20 SQLITE_DEBUG./*
1581c 54 68 69 73 20 76 61 72 69 61 74 69 6f 6e 20 6f This variation o
1581d 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 29 20 n cellSizePtr()
1581e 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f is used inside o
1581f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 f assert() state
15820 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 2a ments.** only. *
15821 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 6c /.static u16 cel
15822 6c 53 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 lSize(MemPage *p
15823 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 Page, int iCell)
15824 7b 0a 20 20 72 65 74 75 72 6e 20 63 65 6c 6c 53 {. return cellS
15825 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 66 69 izePtr(pPage, fi
15826 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 ndCell(pPage, iC
15827 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a ell));.}.#endif.
15828 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15829 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1582a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c /*.** If the cel
1582b 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66 l pCell, part of
1582c 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 page pPage cont
1582d 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a ains a pointer.*
1582e 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 * to an overflow
1582f 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e page, insert an
15830 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 entry into the
15831 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 pointer-map.** f
15832 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
15833 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 page..*/.static
15834 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 4f 76 void ptrmapPutOv
15835 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 flPtr(MemPage *p
15836 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 2c Page, u8 *pCell,
15837 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 43 65 int *pRC){. Ce
15838 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 69 llInfo info;. i
15839 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e f( *pRC ) return
1583a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c ;. assert( pCel
1583b 6c 21 3d 30 20 29 3b 0a 20 20 62 74 72 65 65 50 l!=0 );. btreeP
1583c 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
1583d 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 e, pCell, &info)
1583e 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 ;. assert( (inf
1583f 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e o.nData+(pPage->
15840 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b intKey?0:info.nK
15841 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c ey))==info.nPayl
15842 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 69 6e 66 oad );. if( inf
15843 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 o.iOverflow ){.
15844 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 Pgno ovfl = g
15845 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 et4byte(&pCell[i
15846 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b nfo.iOverflow]);
15847 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 . ptrmapPut(p
15848 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c Page->pBt, ovfl,
15849 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
1584a 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 1, pPage->pgno,
1584b 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 pRC);. }.}.#end
1584c 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61 if.../*.** Defra
1584d 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 20 67 gment the page g
1584e 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c 73 iven. All Cells
1584f 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 are moved to th
15850 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 e.** end of the
15851 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 65 page and all fre
15852 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c 6c 65 e space is colle
15853 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a 2a cted into one.**
15854 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74 68 61 big FreeBlk tha
15855 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 t occurs in betw
15856 65 65 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 een the header a
15857 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 nd cell.** point
15858 65 72 20 61 72 72 61 79 20 61 6e 64 20 74 68 65 er array and the
15859 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
1585a 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ea..*/.static in
1585b 74 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 t defragmentPage
1585c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
1585d 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
1585e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1585f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
15860 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 /. int pc;
15861 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15862 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 69 * Address of a i
15863 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e -th cell */. in
15864 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 t hdr;
15865 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
15866 65 74 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 et to the page h
15867 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 eader */. int s
15868 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
15869 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1586a 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 a cell */. int
1586b 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 usableSize;
1586c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1586d 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 r of usable byte
1586e 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 s on a page */.
1586f 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b int cellOffset;
15870 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
15871 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c ffset to the cel
15872 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 l pointer array
15873 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 */. int cbrk;
15874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15875 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 /* Offset to the
15876 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
15877 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c ea */. int nCel
15878 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
15879 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1587a 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 cells on the pag
1587b 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
1587c 63 68 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20 char *data;
1587d 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61 /* The page da
1587e 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ta */. unsigned
1587f 20 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20 char *temp;
15880 20 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20 /* Temp area
15881 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 for cell content
15882 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46 */. int iCellF
15883 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 irst;
15884 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 /* First allowa
15885 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a ble cell index *
15886 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 /. int iCellLas
15887 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t; /
15888 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 * Last possible
15889 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a cell index */...
1588a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1588b 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
1588c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
1588d 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1588e 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a Page->pBt!=0 );.
1588f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15890 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
15891 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 <= SQLITE_MAX_P
15892 41 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 AGE_SIZE );. as
15893 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
15894 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 erflow==0 );. a
15895 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15896 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
15897 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
15898 20 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 temp = sqlite3
15899 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 70 PagerTempSpace(p
1589a 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 Page->pBt->pPage
1589b 72 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 r);. data = pPa
1589c 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 ge->aData;. hdr
1589d 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
1589e 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 set;. cellOffse
1589f 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f t = pPage->cellO
158a0 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d ffset;. nCell =
158a1 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 pPage->nCell;.
158a2 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d assert( nCell==
158a3 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
158a4 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 dr+3]) );. usab
158a5 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e leSize = pPage->
158a6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
158a7 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62 79 . cbrk = get2by
158a8 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
158a9 3b 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 ;. memcpy(&temp
158aa 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 [cbrk], &data[cb
158ab 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 rk], usableSize
158ac 2d 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20 - cbrk);. cbrk
158ad 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 = usableSize;.
158ae 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c iCellFirst = cel
158af 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c lOffset + 2*nCel
158b0 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d l;. iCellLast =
158b1 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b usableSize - 4;
158b2 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 . for(i=0; i<nC
158b3 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 ell; i++){. u
158b4 38 20 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a 8 *pAddr; /*
158b5 20 54 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70 The i-th cell p
158b6 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41 ointer */. pA
158b7 64 64 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c ddr = &data[cell
158b8 4f 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20 Offset + i*2];.
158b9 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 pc = get2byte
158ba 28 70 41 64 64 72 29 3b 0a 20 20 20 20 74 65 73 (pAddr);. tes
158bb 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c tcase( pc==iCell
158bc 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 First );. tes
158bd 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c tcase( pc==iCell
158be 4c 61 73 74 20 29 3b 0a 23 69 66 20 21 64 65 66 Last );.#if !def
158bf 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
158c0 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c LE_OVERSIZE_CELL
158c1 5f 43 48 45 43 4b 29 0a 20 20 20 20 2f 2a 20 54 _CHECK). /* T
158c2 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 hese conditions
158c3 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 have already bee
158c4 6e 20 76 65 72 69 66 69 65 64 20 69 6e 20 62 74 n verified in bt
158c5 72 65 65 49 6e 69 74 50 61 67 65 28 29 0a 20 20 reeInitPage().
158c6 20 20 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 45 ** if SQLITE_E
158c7 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 NABLE_OVERSIZE_C
158c8 45 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64 65 66 ELL_CHECK is def
158c9 69 6e 65 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20 ined . */.
158ca 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 if( pc<iCellFir
158cb 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 st || pc>iCellLa
158cc 73 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 st ){. retu
158cd 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
158ce 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 T_BKPT;. }.#e
158cf 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 ndif. assert(
158d0 20 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 pc>=iCellFirst
158d1 26 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74 && pc<=iCellLast
158d2 20 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 );. size = c
158d3 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 ellSizePtr(pPage
158d4 2c 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 , &temp[pc]);.
158d5 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a cbrk -= size;.
158d6 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
158d7 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 TE_ENABLE_OVERSI
158d8 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 ZE_CELL_CHECK).
158d9 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c if( cbrk<iCel
158da 6c 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 lFirst ){.
158db 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
158dc 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
158dd 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 }.#else. if(
158de 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 cbrk<iCellFirst
158df 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c || pc+size>usabl
158e0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 eSize ){. r
158e1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
158e2 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
158e3 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 .#endif. asse
158e4 72 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 rt( cbrk+size<=u
158e5 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 sableSize && cbr
158e6 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b k>=iCellFirst );
158e7 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 63 . testcase( c
158e8 62 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 brk+size==usable
158e9 53 69 7a 65 20 29 3b 0a 20 20 20 20 74 65 73 74 Size );. test
158ea 63 61 73 65 28 20 70 63 2b 73 69 7a 65 3d 3d 75 case( pc+size==u
158eb 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 sableSize );.
158ec 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62 memcpy(&data[cb
158ed 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20 rk], &temp[pc],
158ee 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62 size);. put2b
158ef 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 yte(pAddr, cbrk)
158f0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
158f1 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 cbrk>=iCellFirst
158f2 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 );. put2byte(&
158f3 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 data[hdr+5], cbr
158f4 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 k);. data[hdr+1
158f5 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 ] = 0;. data[hd
158f6 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 r+2] = 0;. data
158f7 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d [hdr+7] = 0;. m
158f8 65 6d 73 65 74 28 26 64 61 74 61 5b 69 43 65 6c emset(&data[iCel
158f9 6c 46 69 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b lFirst], 0, cbrk
158fa 2d 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 -iCellFirst);.
158fb 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
158fc 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
158fd 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
158fe 29 3b 0a 20 20 69 66 28 20 63 62 72 6b 2d 69 43 );. if( cbrk-iC
158ff 65 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 65 2d ellFirst!=pPage-
15900 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 72 65 >nFree ){. re
15901 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15902 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 UPT_BKPT;. }.
15903 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15904 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
15905 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 ate nByte bytes
15906 6f 66 20 73 70 61 63 65 20 66 72 6f 6d 20 77 69 of space from wi
15907 74 68 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20 thin the B-Tree
15908 70 61 67 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 page passed.** a
15909 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
1590a 6d 65 6e 74 2e 20 57 72 69 74 65 20 69 6e 74 6f ment. Write into
1590b 20 2a 70 49 64 78 20 74 68 65 20 69 6e 64 65 78 *pIdx the index
1590c 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 into pPage->aDa
1590d 74 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 ta[].** of the f
1590e 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c irst byte of all
1590f 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 52 65 ocated space. Re
15910 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 turn either SQLI
15911 54 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e 20 65 TE_OK or.** an e
15912 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c rror code (usual
15913 6c 79 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 ly SQLITE_CORRUP
15914 54 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 T)..**.** The ca
15915 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 ller guarantees
15916 74 68 61 74 20 74 68 65 72 65 20 69 73 20 73 75 that there is su
15917 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 74 fficient space t
15918 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 61 6c o make the.** al
15919 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 location. This
1591a 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65 routine might ne
1591b 65 64 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 ed to defragment
1591c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69 in order to bri
1591d 6e 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70 ng.** all the sp
1591e 61 63 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f ace together, ho
1591f 77 65 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75 wever. This rou
15920 74 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20 tine will avoid
15921 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 72 using.** the fir
15922 73 74 20 74 77 6f 20 62 79 74 65 73 20 70 61 73 st two bytes pas
15923 74 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 t the cell point
15924 65 72 20 61 72 65 61 20 73 69 6e 63 65 20 70 72 er area since pr
15925 65 73 75 6d 61 62 6c 79 20 74 68 69 73 0a 2a 2a esumably this.**
15926 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 62 allocation is b
15927 65 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72 64 eing made in ord
15928 65 72 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e er to insert a n
15929 65 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20 77 ew cell, so we w
1592a 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e 64 20 ill.** also end
1592b 75 70 20 6e 65 65 64 69 6e 67 20 61 20 6e 65 77 up needing a new
1592c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a cell pointer..*
1592d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c /.static int all
1592e 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 ocateSpace(MemPa
1592f 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e ge *pPage, int n
15930 42 79 74 65 2c 20 69 6e 74 20 2a 70 49 64 78 29 Byte, int *pIdx)
15931 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 {. const int hd
15932 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
15933 66 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 fset; /* Loca
15934 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 l cache of pPage
15935 2d 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f 0a 20 ->hdrOffset */.
15936 20 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 u8 * const data
15937 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
15938 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 /* Local c
15939 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 ache of pPage->a
1593a 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 Data */. int nF
1593b 72 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 rag;
1593c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1593d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 * Number of frag
1593e 6d 65 6e 74 65 64 20 62 79 74 65 73 20 6f 6e 20 mented bytes on
1593f 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 74 pPage */. int t
15940 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
15941 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15942 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
15943 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
15944 65 61 20 2a 2f 0a 20 20 69 6e 74 20 67 61 70 3b ea */. int gap;
15945 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
15946 20 62 79 74 65 20 6f 66 20 67 61 70 20 62 65 74 byte of gap bet
15947 77 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 ween cell pointe
15948 72 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f 6e 74 rs and cell cont
15949 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b ent */. int rc;
1594a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 /* Inte
1594b 67 65 72 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ger return code
1594c 2a 2f 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 */. . assert(
1594d 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
1594e 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
1594f 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
15950 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 ert( pPage->pBt
15951 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15952 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15953 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
15954 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
15955 6e 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 nByte>=0 ); /*
15956 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a Minimum cell siz
15957 65 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 e is 4 */. asse
15958 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 rt( pPage->nFree
15959 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73 >=nByte );. ass
1595a 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
1595b 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 rflow==0 );. as
1595c 73 65 72 74 28 20 6e 42 79 74 65 3c 70 50 61 67 sert( nByte<pPag
1595d 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
1595e 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 72 61 67 ze-8 );.. nFrag
1595f 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a = data[hdr+7];.
15960 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15961 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 3d 20 68 >cellOffset == h
15962 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 dr + 12 - 4*pPag
15963 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 67 61 70 e->leaf );. gap
15964 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 = pPage->cellOf
15965 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e fset + 2*pPage->
15966 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 3d 20 67 nCell;. top = g
15967 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
15968 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67 61 70 r+5]);. if( gap
15969 3e 74 6f 70 20 29 20 72 65 74 75 72 6e 20 53 51 >top ) return SQ
1596a 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1596b 54 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 T;. testcase( g
1596c 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 ap+2==top );. t
1596d 65 73 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d estcase( gap+1==
1596e 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 top );. testcas
1596f 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0a 0a e( gap==top );..
15970 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 if( nFrag>=60
15971 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 ){. /* Always
15972 20 64 65 66 72 61 67 6d 65 6e 74 20 68 69 67 68 defragment high
15973 6c 79 20 66 72 61 67 6d 65 6e 74 65 64 20 70 61 ly fragmented pa
15974 67 65 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 ges */. rc =
15975 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70 defragmentPage(p
15976 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
15977 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
15978 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 top = get2byt
15979 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
1597a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61 70 . }else if( gap
1597b 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20 2f +2<=top ){. /
1597c 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 72 65 * Search the fre
1597d 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f elist looking fo
1597e 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 69 r a free slot bi
1597f 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69 g enough to sati
15980 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 sfy . ** the
15981 72 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c 6c request. The all
15982 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 ocation is made
15983 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 66 from the first f
15984 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 20 20 ree slot in .
15985 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 68 61 ** the list tha
15986 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 t is large enoug
15987 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 65 20 h to accomadate
15988 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 it.. */. i
15989 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 20 20 nt pc, addr;.
1598a 20 66 6f 72 28 61 64 64 72 3d 68 64 72 2b 31 3b for(addr=hdr+1;
1598b 20 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 (pc = get2byte(
1598c 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 3b &data[addr]))>0;
1598d 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 20 20 addr=pc){.
1598e 20 69 6e 74 20 73 69 7a 65 20 3d 20 67 65 74 32 int size = get2
1598f 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d byte(&data[pc+2]
15990 29 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f ); /* Size o
15991 66 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 f free slot */.
15992 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e if( size>=n
15993 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Byte ){.
15994 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e int x = size - n
15995 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65 Byte;. te
15996 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a stcase( x==4 );.
15997 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
15998 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 ( x==3 );.
15999 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 if( x<4 ){.
1599a 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 /* Remove
1599b 20 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 the slot from t
1599c 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 he free-list. Up
1599d 64 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 date the number
1599e 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 of. **
1599f 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 fragmented bytes
159a0 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 within the page
159a1 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d . */. m
159a2 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 emcpy(&data[addr
159a3 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 ], &data[pc], 2)
159a4 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 ;. data
159a5 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e [hdr+7] = (u8)(n
159a6 46 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20 Frag + x);.
159a7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
159a8 20 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 /* The slot
159a9 72 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 remains on the f
159aa 72 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 ree-list. Reduce
159ab 20 69 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63 its size to acc
159ac 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a ount. *
159ad 2a 20 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f * for the portio
159ae 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 6e 65 n used by the ne
159af 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f w allocation. */
159b0 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62 . put2b
159b1 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c yte(&data[pc+2],
159b2 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 x);. }.
159b3 20 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 *pIdx = p
159b4 63 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72 c + x;. r
159b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
159b6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
159b7 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 }.. /* Check t
159b8 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 o make sure ther
159b9 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 e is enough spac
159ba 65 20 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 e in the gap to
159bb 73 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65 satisfy. ** the
159bc 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 allocation. If
159bd 20 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 not, defragment
159be 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 .. */. testcas
159bf 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d e( gap+2+nByte==
159c0 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70 top );. if( gap
159c1 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a +2+nByte>top ){.
159c2 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d rc = defragm
159c3 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a entPage(pPage);.
159c4 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
159c5 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 urn rc;. top
159c6 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
159c7 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73 [hdr+5]);. as
159c8 73 65 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c sert( gap+nByte<
159c9 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 =top );. }...
159ca 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f /* Allocate memo
159cb 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 ry from the gap
159cc 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 in between the c
159cd 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
159ce 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 y. ** and the c
159cf 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
159d0 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74 . The btreeInit
159d1 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20 Page() call has
159d2 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c already. ** val
159d3 69 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c idated the freel
159d4 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74 ist. Given that
159d5 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 the freelist is
159d6 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20 valid, there.
159d7 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61 ** is no way tha
159d8 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e t the allocation
159d9 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20 can extend off
159da 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 the end of the p
159db 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73 age.. ** The as
159dc 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72 sert() below ver
159dd 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f ifies the previo
159de 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a us sentence.. *
159df 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 /. top -= nByte
159e0 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 ;. put2byte(&da
159e1 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b ta[hdr+5], top);
159e2 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e . assert( top+n
159e3 42 79 74 65 20 3c 3d 20 70 50 61 67 65 2d 3e 70 Byte <= pPage->p
159e4 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
159e5 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b ;. *pIdx = top;
159e6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
159e7 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 _OK;.}../*.** Re
159e8 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f turn a section o
159e9 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 f the pPage->aDa
159ea 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ta to the freeli
159eb 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 st..** The first
159ec 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 byte of the new
159ed 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 free block is p
159ee 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 Page->aDisk[star
159ef 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 t].** and the si
159f0 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 ze of the block
159f1 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e is "size" bytes.
159f2 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 .**.** Most of t
159f3 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 he effort here i
159f4 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f s involved in co
159f5 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 alesing adjacent
159f6 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 .** free blocks
159f7 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 into a single bi
159f8 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f g free block..*/
159f9 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 .static int free
159fa 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 Space(MemPage *p
159fb 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c Page, int start,
159fc 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e int size){. in
159fd 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 t addr, pbegin,
159fe 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 73 74 hdr;. int iLast
159ff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15a00 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 /* Larg
15a01 65 73 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65 est possible fre
15a02 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f eblock offset */
15a03 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
15a04 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e *data = pPage->
15a05 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 aData;.. assert
15a06 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 ( pPage->pBt!=0
15a07 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15a08 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
15a09 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
15a0a 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 age) );. assert
15a0b 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e ( start>=pPage->
15a0c 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67 hdrOffset+6+pPag
15a0d 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 e->childPtrSize
15a0e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74 );. assert( (st
15a0f 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 art + size)<=pPa
15a10 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
15a11 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ize );. assert(
15a12 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15a13 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15a14 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
15a15 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 rt( size>=0 );
15a16 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c /* Minimum cell
15a17 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 size is 4 */..#
15a18 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 ifdef SQLITE_SEC
15a19 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 URE_DELETE. /*
15a1a 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 Overwrite delete
15a1b 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 d information wi
15a1c 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 th zeros when th
15a1d 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 e SECURE_DELETE
15a1e 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 . ** option is
15a1f 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 enabled at compi
15a20 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d le-time */. mem
15a21 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d set(&data[start]
15a22 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 , 0, size);.#end
15a23 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 if.. /* Add the
15a24 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f space back into
15a25 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
15a26 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 of freeblocks.
15a27 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 Note that. **
15a28 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 even though the
15a29 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77 freeblock list w
15a2a 61 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74 as checked by bt
15a2b 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 20 reeInitPage(),.
15a2c 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 ** btreeInitPag
15a2d 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65 e() did not dete
15a2e 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63 ct overlapping c
15a2f 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 72 65 ells or. ** fre
15a30 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65 eblocks that ove
15a31 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20 rlapped cells.
15a32 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74 Nor does it det
15a33 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20 20 2a ect when the. *
15a34 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 * cell content a
15a35 72 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20 rea exceeds the
15a36 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67 value in the pag
15a37 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68 e header. If th
15a38 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 74 69 ese. ** situati
15a39 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e 20 ons arise, then
15a3a 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72 subsequent inser
15a3b 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69 67 t operations mig
15a3c 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a 2a 20 ht corrupt. **
15a3d 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 53 the freelist. S
15a3e 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 o we do need to
15a3f 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 75 70 check for corrup
15a40 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 6e 6e tion while scann
15a41 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 72 65 ing. ** the fre
15a42 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 68 64 elist.. */. hd
15a43 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
15a44 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68 fset;. addr = h
15a45 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 74 20 dr + 1;. iLast
15a46 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 = pPage->pBt->us
15a47 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 ableSize - 4;.
15a48 61 73 73 65 72 74 28 20 73 74 61 72 74 3c 3d 69 assert( start<=i
15a49 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c 65 28 Last );. while(
15a4a 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 (pbegin = get2b
15a4b 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 yte(&data[addr])
15a4c 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69 )<start && pbegi
15a4d 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 n>0 ){. if( p
15a4e 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 7b 0a begin<addr+4 ){.
15a4f 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15a50 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15a51 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 ;. }. addr
15a52 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20 = pbegin;. }.
15a53 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73 if( pbegin>iLas
15a54 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
15a55 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
15a56 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 KPT;. }. asser
15a57 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c t( pbegin>addr |
15a58 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 | pbegin==0 );.
15a59 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
15a5a 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 addr], start);.
15a5b 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
15a5c 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b start], pbegin);
15a5d 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
15a5e 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 a[start+2], size
15a5f 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
15a60 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 e = pPage->nFree
15a61 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20 + (u16)size;..
15a62 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a /* Coalesce adj
15a63 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b acent free block
15a64 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 68 64 s */. addr = hd
15a65 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 r + 1;. while(
15a66 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 (pbegin = get2by
15a67 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 te(&data[addr]))
15a68 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e >0 ){. int pn
15a69 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b 0a 20 ext, psize, x;.
15a6a 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 assert( pbegi
15a6b 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61 73 n>addr );. as
15a6c 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 sert( pbegin<=pP
15a6d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
15a6e 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e Size-4 );. pn
15a6f 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 ext = get2byte(&
15a70 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20 data[pbegin]);.
15a71 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62 psize = get2b
15a72 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
15a73 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62 +2]);. if( pb
15a74 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33 egin + psize + 3
15a75 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65 >= pnext && pne
15a76 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e xt>0 ){. in
15a77 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d t frag = pnext -
15a78 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b (pbegin+psize);
15a79 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67 . if( (frag
15a7a 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e <0) || (frag>(in
15a7b 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29 t)data[hdr+7]) )
15a7c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
15a7d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15a7e 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 BKPT;. }.
15a7f 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 data[hdr+7]
15a80 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 -= (u8)frag;.
15a81 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 x = get2byte(
15a82 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 &data[pnext]);.
15a83 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 put2byte(&d
15a84 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b ata[pbegin], x);
15a85 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 . x = pnext
15a86 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 + get2byte(&dat
15a87 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 a[pnext+2]) - pb
15a88 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 egin;. put2
15a89 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 byte(&data[pbegi
15a8a 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 n+2], x);. }e
15a8b 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 lse{. addr
15a8c 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a = pbegin;. }.
15a8d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
15a8e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
15a8f 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 ea begins with a
15a90 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f freeblock, remo
15a91 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 ve it. */. if(
15a92 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 data[hdr+1]==dat
15a93 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 a[hdr+5] && data
15a94 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 [hdr+2]==data[hd
15a95 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 r+6] ){. int
15a96 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 top;. pbegin
15a97 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
15a98 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 [hdr+1]);. me
15a99 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 mcpy(&data[hdr+1
15a9a 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d ], &data[pbegin]
15a9b 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 , 2);. top =
15a9c 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
15a9d 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 dr+5]) + get2byt
15a9e 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 e(&data[pbegin+2
15a9f 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 ]);. put2byte
15aa0 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 (&data[hdr+5], t
15aa1 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 op);. }. asser
15aa2 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
15aa3 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
15aa4 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
15aa5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15aa6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 ;.}../*.** Decod
15aa7 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 e the flags byte
15aa8 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 (the first byte
15aa9 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 of the header)
15aaa 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e for a page.** an
15aab 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 d initialize fie
15aac 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 lds of the MemPa
15aad 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 ge structure acc
15aae 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 ordingly..**.**
15aaf 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 Only the followi
15ab0 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 ng combinations
15ab1 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 are supported.
15ab2 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 Anything differe
15ab3 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 nt.** indicates
15ab4 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 a corrupt databa
15ab5 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 se files:.**.**
15ab6 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f PTF_ZERO
15ab7 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 DATA.**
15ab8 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 PTF_ZERODATA | P
15ab9 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 TF_LEAF.**
15aba 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 PTF_LEAFDATA
15abb 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 | PTF_INTKEY.**
15abc 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 PTF_LEAF
15abd 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 DATA | PTF_INTKE
15abe 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a Y | PTF_LEAF.*/.
15abf 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 static int decod
15ac0 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a eFlags(MemPage *
15ac1 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 pPage, int flagB
15ac2 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 yte){. BtShared
15ac3 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 *pBt; /* A
15ac4 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 copy of pPage->p
15ac5 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 Bt */.. assert(
15ac6 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
15ac7 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d t==(pPage->pgno=
15ac8 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b =1 ? 100 : 0) );
15ac9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15aca 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
15acb 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
15acc 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 );. pPage->lea
15acd 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 f = (u8)(flagByt
15ace 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 e>>3); assert(
15acf 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 PTF_LEAF == 1<<3
15ad0 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 );. flagByte &
15ad1 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 = ~PTF_LEAF;. p
15ad2 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
15ad3 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e ze = 4-4*pPage->
15ad4 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 leaf;. pBt = pP
15ad5 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 age->pBt;. if(
15ad6 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c flagByte==(PTF_L
15ad7 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e EAFDATA | PTF_IN
15ad8 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 TKEY) ){. pPa
15ad9 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a ge->intKey = 1;.
15ada 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 pPage->hasDa
15adb 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 ta = pPage->leaf
15adc 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 ;. pPage->max
15add 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 Local = pBt->max
15ade 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d Leaf;. pPage-
15adf 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >minLocal = pBt-
15ae0 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 >minLeaf;. }els
15ae1 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d e if( flagByte==
15ae2 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a PTF_ZERODATA ){.
15ae3 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 pPage->intKe
15ae4 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 y = 0;. pPage
15ae5 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 ->hasData = 0;.
15ae6 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 pPage->maxLoc
15ae7 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 al = pBt->maxLoc
15ae8 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d al;. pPage->m
15ae9 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d inLocal = pBt->m
15aea 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 inLocal;. }else
15aeb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
15aec 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15aed 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
15aee 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
15aef 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** Initialize th
15af0 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f e auxiliary info
15af1 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 rmation for a di
15af2 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 sk block..**.**
15af3 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
15af4 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 on success. If
15af5 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65 we see that the
15af6 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f page does.** no
15af7 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c t contain a well
15af8 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 -formed database
15af9 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75 page, then retu
15afa 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f rn .** SQLITE_CO
15afb 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 RRUPT. Note tha
15afc 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 t a return of SQ
15afd 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 LITE_OK does not
15afe 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 .** guarantee th
15aff 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 at the page is w
15b00 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 ell-formed. It
15b01 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a only shows that.
15b02 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 ** we failed to
15b03 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75 detect any corru
15b04 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ption..*/.static
15b05 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 61 int btreeInitPa
15b06 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
15b07 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 e){.. assert( p
15b08 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a Page->pBt!=0 );.
15b09 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15b0a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
15b0b 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
15b0c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
15b0d 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 ge->pgno==sqlite
15b0e 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
15b0f 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15b10 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
15b11 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 age == sqlite3Pa
15b12 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
15b13 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
15b14 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15b15 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 aData == sqlite3
15b16 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
15b17 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
15b18 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 . if( !pPage->i
15b19 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 sInit ){. u16
15b1a 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 pc;
15b1b 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 /* Address of a
15b1c 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e freeblock within
15b1d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 pPage->aData[]
15b1e 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 */. u8 hdr;
15b1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
15b20 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 set to beginning
15b21 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 of page header
15b22 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b */. u8 *data;
15b23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 /* Equ
15b24 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 al to pPage->aDa
15b25 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 ta */. BtShar
15b26 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 ed *pBt;
15b27 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 /* The main btre
15b28 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
15b29 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a u16 usableSiz
15b2a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 e; /* Amount
15b2b 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 of usable space
15b2c 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a on each page */.
15b2d 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 u16 cellOffs
15b2e 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 et; /* Offset
15b2f 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 from start of p
15b30 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c age to first cel
15b31 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 l pointer */.
15b32 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 u16 nFree;
15b33 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
15b34 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e unused bytes on
15b35 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
15b36 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 u16 top;
15b37 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
15b38 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
15b39 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 ntent area */.
15b3a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 int iCellFirst
15b3b 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c ; /* First al
15b3c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20 lowable cell or
15b3d 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 freeblock offset
15b3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c */. int iCel
15b3f 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 lLast; /* La
15b40 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c st possible cell
15b41 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 or freeblock of
15b42 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 74 fset */.. pBt
15b43 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 0a = pPage->pBt;..
15b44 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d hdr = pPage-
15b45 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 >hdrOffset;.
15b46 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 data = pPage->aD
15b47 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 63 ata;. if( dec
15b48 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 odeFlags(pPage,
15b49 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 74 data[hdr]) ) ret
15b4a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15b4b 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73 PT_BKPT;. ass
15b4c 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 ert( pBt->pageSi
15b4d 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e ze>=512 && pBt->
15b4e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 pageSize<=32768
15b4f 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 );. pPage->ma
15b50 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 skPage = pBt->pa
15b51 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20 geSize - 1;.
15b52 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
15b53 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c 65 = 0;. usable
15b54 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Size = pBt->usab
15b55 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 leSize;. pPag
15b56 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 e->cellOffset =
15b57 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72 cellOffset = hdr
15b58 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d + 12 - 4*pPage-
15b59 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 20 3d >leaf;. top =
15b5a 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15b5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 61 hdr+5]);. pPa
15b5c 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32 ge->nCell = get2
15b5d 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
15b5e 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ]);. if( pPag
15b5f 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c e->nCell>MX_CELL
15b60 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f (pBt) ){. /
15b61 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20 * To many cells
15b62 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67 for a single pag
15b63 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73 e. The page mus
15b64 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a t be corrupt */.
15b65 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15b66 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15b67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 ;. }. test
15b68 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65 case( pPage->nCe
15b69 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29 ll==MX_CELL(pBt)
15b6a 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d 61 );.. /* A ma
15b6b 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 lformed database
15b6c 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 73 page might caus
15b6d 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 73 e us to read pas
15b6e 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a t the end. **
15b6f 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61 of page when pa
15b70 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a rsing a cell. .
15b71 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
15b72 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
15b73 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b 73 k of code checks
15b74 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 66 early to see if
15b75 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 0a a cell extends.
15b76 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 ** past the
15b77 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f end of a page bo
15b78 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65 undary and cause
15b79 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 s SQLITE_CORRUPT
15b7a 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 72 to be . ** r
15b7b 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f eturned if it do
15b7c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 es.. */. i
15b7d 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c CellFirst = cell
15b7e 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 Offset + 2*pPage
15b7f 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 65 ->nCell;. iCe
15b80 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 llLast = usableS
15b81 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 66 ize - 4;.#if def
15b82 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
15b83 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c LE_OVERSIZE_CELL
15b84 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 20 _CHECK). {.
15b85 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
15b86 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
15b87 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 nto the cell poi
15b88 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 nter array */.
15b89 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20 int sz;
15b8a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
15b8b 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 20 a cell */..
15b8c 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
15b8d 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d 2d af ) iCellLast--
15b8e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
15b8f 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b i<pPage->nCell;
15b90 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 i++){. p
15b91 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 c = get2byte(&da
15b92 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 2a ta[cellOffset+i*
15b93 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 2]);. tes
15b94 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c tcase( pc==iCell
15b95 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 First );.
15b96 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 testcase( pc==i
15b97 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 CellLast );.
15b98 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c if( pc<iCell
15b99 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c First || pc>iCel
15b9a 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20 lLast ){.
15b9b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
15b9c 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
15b9d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15b9e 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 sz = cellSizePt
15b9f 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 r(pPage, &data[p
15ba0 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 c]);. tes
15ba1 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 73 tcase( pc+sz==us
15ba2 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 ableSize );.
15ba3 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 73 if( pc+sz>us
15ba4 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 ableSize ){.
15ba5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15ba6 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15ba7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
15ba8 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 }. if( !p
15ba9 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65 Page->leaf ) iCe
15baa 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d 20 llLast++;. }
15bab 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a .#endif.. /*
15bac 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 Compute the tot
15bad 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e al free space on
15bae 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 the page */.
15baf 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 pc = get2byte(&
15bb0 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 data[hdr+1]);.
15bb1 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68 nFree = data[h
15bb2 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 20 dr+7] + top;.
15bb3 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a while( pc>0 ){.
15bb4 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20 u16 next,
15bb5 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 size;. if(
15bb6 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c pc<iCellFirst ||
15bb7 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b pc>iCellLast ){
15bb8 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 . /* Star
15bb9 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20 t of free block
15bba 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 is off the page
15bbb 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
15bbc 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15bbd 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a _BKPT; . }.
15bbe 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 next = get
15bbf 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 2byte(&data[pc])
15bc0 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 ;. size = g
15bc1 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
15bc2 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 +2]);. if(
15bc3 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c (next>0 && next<
15bc4 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 70 =pc+size+3) || p
15bc5 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a c+size>usableSiz
15bc6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 e ){. /*
15bc7 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74 Free blocks must
15bc8 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 be in ascending
15bc9 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 20 order. And the
15bca 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 09 2a 2a last byte of..**
15bcb 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 20 the free-block
15bcc 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 20 must lie on the
15bcd 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 database page.
15bce 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 */. retur
15bcf 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15bd0 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a _BKPT; . }.
15bd1 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46 nFree = nF
15bd2 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 ree + size;.
15bd3 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 20 pc = next;.
15bd4 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 }.. /* At th
15bd5 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20 is point, nFree
15bd6 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d contains the sum
15bd7 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 74 of the offset t
15bd8 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 20 o the start.
15bd9 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 ** of the cell-c
15bda 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73 ontent area plus
15bdb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 the number of f
15bdc 72 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e ree bytes within
15bdd 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c . ** the cell
15bde 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49 -content area. I
15bdf 66 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65 f this is greate
15be0 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c r than the usabl
15be1 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f 66 e-size. ** of
15be2 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 the page, then
15be3 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 the page must be
15be4 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 corrupted. This
15be5 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 20 check also.
15be6 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 72 ** serves to ver
15be7 69 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 66 ify that the off
15be8 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74 set to the start
15be9 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e of the cell-con
15bea 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 61 tent. ** area
15beb 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 , according to t
15bec 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20 he page header,
15bed 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 lies within the
15bee 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 page.. */.
15bef 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 6c if( nFree>usabl
15bf0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 eSize ){. r
15bf1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15bf2 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 RUPT_BKPT; .
15bf3 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 }. pPage->nFr
15bf4 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 65 ee = (u16)(nFree
15bf5 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a - iCellFirst);.
15bf6 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 pPage->isIni
15bf7 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 t = 1;. }. ret
15bf8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
15bf9 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 ../*.** Set up a
15bfa 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 raw page so tha
15bfb 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 t it looks like
15bfc 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 a database page
15bfd 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e holding.** no en
15bfe 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tries..*/.static
15bff 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d void zeroPage(M
15c00 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 emPage *pPage, i
15c01 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 nt flags){. uns
15c02 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 igned char *data
15c03 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b = pPage->aData;
15c04 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
15c05 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 = pPage->pBt;.
15c06 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d u8 hdr = pPage-
15c07 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 >hdrOffset;. u1
15c08 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 6 first;.. asse
15c09 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
15c0a 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 Pagenumber(pPage
15c0b 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 ->pDbPage)==pPag
15c0c 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 e->pgno );. ass
15c0d 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
15c0e 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d rGetExtra(pPage-
15c0f 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f >pDbPage) == (vo
15c10 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 id*)pPage );. a
15c11 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15c12 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 gerGetData(pPage
15c13 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 ->pDbPage) == da
15c14 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ta );. assert(
15c15 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
15c16 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
15c17 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 DbPage) );. ass
15c18 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15c19 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
15c1a 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 ex) );.#ifdef SQ
15c1b 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 LITE_SECURE_DELE
15c1c 54 45 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 TE. memset(&dat
15c1d 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e a[hdr], 0, pBt->
15c1e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72 usableSize - hdr
15c1f 29 3b 0a 23 65 6e 64 69 66 0a 20 20 64 61 74 61 );.#endif. data
15c20 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c [hdr] = (char)fl
15c21 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 ags;. first = h
15c22 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 dr + 8 + 4*((fla
15c23 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 gs&PTF_LEAF)==0
15c24 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 ?1:0);. memset(
15c25 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c &data[hdr+1], 0,
15c26 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 4);. data[hdr+
15c27 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 7] = 0;. put2by
15c28 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c te(&data[hdr+5],
15c29 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
15c2a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
15c2b 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
15c2c 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 ize - first;. d
15c2d 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 ecodeFlags(pPage
15c2e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 , flags);. pPag
15c2f 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 e->hdrOffset = h
15c30 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c dr;. pPage->cel
15c31 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b lOffset = first;
15c32 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 . pPage->nOverf
15c33 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 low = 0;. asser
15c34 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 t( pBt->pageSize
15c35 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 >=512 && pBt->pa
15c36 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b geSize<=32768 );
15c37 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 . pPage->maskPa
15c38 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ge = pBt->pageSi
15c39 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d ze - 1;. pPage-
15c3a 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 >nCell = 0;. pP
15c3b 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b age->isInit = 1;
15c3c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 .}.../*.** Conve
15c3d 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 rt a DbPage obta
15c3e 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 ined from the pa
15c3f 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 ger into a MemPa
15c40 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 ge used by.** th
15c41 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a e btree layer..*
15c42 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 /.static MemPage
15c43 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 *btreePageFromD
15c44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 bPage(DbPage *pD
15c45 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f bPage, Pgno pgno
15c46 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 , BtShared *pBt)
15c47 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
15c48 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 ge = (MemPage*)s
15c49 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
15c4a 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 tra(pDbPage);.
15c4b 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 pPage->aData = s
15c4c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
15c4d 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ta(pDbPage);. p
15c4e 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 Page->pDbPage =
15c4f 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 pDbPage;. pPage
15c50 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 ->pBt = pBt;. p
15c51 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e Page->pgno = pgn
15c52 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f o;. pPage->hdrO
15c53 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 ffset = pPage->p
15c54 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 gno==1 ? 100 : 0
15c55 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 ;. return pPage
15c56 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 ; .}../*.** Get
15c57 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
15c58 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 pager. Initiali
15c59 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 ze the MemPage.p
15c5a 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 Bt and.** MemPag
15c5b 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 e.aData elements
15c5c 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a if needed..**.*
15c5d 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 * If the noConte
15c5e 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 nt flag is set,
15c5f 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 it means that we
15c60 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
15c61 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ut.** the conten
15c62 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 t of the page at
15c63 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 this time. So
15c64 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 do not go to the
15c65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 disk.** to fetc
15c66 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 h the content.
15c67 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 Just fill in the
15c68 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 content with ze
15c69 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 ros for now..**
15c6a 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 If in the future
15c6b 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 we call sqlite3
15c6c 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 PagerWrite() on
15c6d 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a this page, that.
15c6e 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 ** means we have
15c6f 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 started to be c
15c70 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 oncerned about c
15c71 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 ontent and the d
15c72 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 isk.** read shou
15c73 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 ld occur at that
15c74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 point..*/.stati
15c75 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61 c int btreeGetPa
15c76 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
15c77 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 pBt, /* Th
15c78 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e e btree */. Pgn
15c79 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
15c7a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
15c7b 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 he page to fetch
15c7c 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
15c7d 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 ppPage, /* Re
15c7e 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e turn the page in
15c7f 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 this parameter
15c80 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 */. int noConte
15c81 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 nt /* Do
15c82 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f not load page co
15c83 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f ntent if true */
15c84 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
15c85 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
15c86 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
15c87 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15c88 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15c89 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15c8a 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 rAcquire(pBt->pP
15c8b 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 ager, pgno, (DbP
15c8c 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 age**)&pDbPage,
15c8d 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 noContent);. if
15c8e 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
15c8f 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 ;. *ppPage = bt
15c90 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15c91 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
15c92 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 pBt);. return
15c93 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
15c94 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 .** Retrieve a p
15c95 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
15c96 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 er cache. If the
15c97 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
15c98 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 is not.** alread
15c99 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 y in the pager c
15c9a 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c ache return NULL
15c9b 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 . Initialize the
15c9c 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
15c9d 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
15c9e 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
15c9f 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
15ca0 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 MemPage *btreePa
15ca1 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 geLookup(BtShare
15ca2 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
15ca3 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
15ca4 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 bPage;. assert(
15ca5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15ca6 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15ca7 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 );. pDbPage = s
15ca8 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
15ca9 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 p(pBt->pPager, p
15caa 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 gno);. if( pDbP
15cab 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 age ){. retur
15cac 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 n btreePageFromD
15cad 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 bPage(pDbPage, p
15cae 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 gno, pBt);. }.
15caf 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
15cb0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
15cb1 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
15cb2 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 ase file in page
15cb3 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 s. If there is a
15cb4 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 ny kind of.** er
15cb5 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e ror, return ((un
15cb6 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a signed int)-1)..
15cb7 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 */.static Pgno p
15cb8 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 agerPagecount(Bt
15cb9 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
15cba 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a int nPage = -1;.
15cbb 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
15cbc 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 rt( pBt->pPage1
15cbd 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
15cbe 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
15cbf 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 pBt->pPager, &nP
15cc0 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 age);. assert(
15cc1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
15cc2 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 nPage==-1 );.
15cc3 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 return (Pgno)nPa
15cc4 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 ge;.}../*.** Get
15cc5 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
15cc6 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 pager and initi
15cc7 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 alize it. This
15cc8 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20 routine is just
15cc9 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 a.** convenience
15cca 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 wrapper around
15ccb 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 separate calls t
15ccc 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 o btreeGetPage()
15ccd 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e and .** btreeIn
15cce 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 itPage()..**.**
15ccf 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
15cd0 72 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c rs, then the val
15cd1 75 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 ue *ppPage is se
15cd2 74 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65 t to is undefine
15cd3 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d d. It.** may rem
15cd4 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f ain unchanged, o
15cd5 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20 r it may be set
15cd6 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61 to an invalid va
15cd7 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 lue..*/.static i
15cd8 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 nt getAndInitPag
15cd9 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
15cda 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Bt, /*
15cdb 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
15cdc 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
15cdd 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
15cde 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
15cdf 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 e to get */. Me
15ce0 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 mPage **ppPage
15ce1 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
15ce2 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 page pointer her
15ce3 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
15ce4 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 50 67 ;. TESTONLY( Pg
15ce5 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 70 61 67 no iLastPg = pag
15ce6 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
15ce7 3b 20 29 0a 20 20 61 73 73 65 72 74 28 20 73 71 ; ). assert( sq
15ce8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15ce9 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
15cea 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 . rc = btreeGet
15ceb 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 Page(pBt, pgno,
15cec 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 ppPage, 0);. if
15ced 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15cee 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
15cef 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67 eInitPage(*ppPag
15cf0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
15cf1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15cf2 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
15cf3 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 ppPage);. }.
15cf4 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
15cf5 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6e requested page n
15cf6 75 6d 62 65 72 20 77 61 73 20 65 69 74 68 65 72 umber was either
15cf7 20 30 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 0 or greater th
15cf8 61 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a an the page. **
15cf9 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c number of the l
15cfa 61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ast page in the
15cfb 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 66 database, this f
15cfc 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 unction should r
15cfd 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 eturn. ** SQLIT
15cfe 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 73 6f 6d E_CORRUPT or som
15cff 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 28 69 e other error (i
15d00 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 .e. SQLITE_FULL)
15d01 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69 . Check that thi
15d02 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 s. ** is the ca
15d03 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 se. */. assert
15d04 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 70 67 6e ( (pgno>0 && pgn
15d05 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c 7c 20 72 o<=iLastPg) || r
15d06 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
15d07 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f testcase( pgno
15d08 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==0 );. testcas
15d09 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 e( pgno==iLastPg
15d0a 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 );.. return rc
15d0b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
15d0c 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 se a MemPage. T
15d0d 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61 his should be ca
15d0e 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 lled once for ea
15d0f 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c ch prior.** call
15d10 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 to btreeGetPage
15d11 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
15d12 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d releasePage(Mem
15d13 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
15d14 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 if( pPage ){.
15d15 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15d16 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 nOverflow==0 ||
15d17 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
15d18 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e Refcount(pPage->
15d19 70 44 62 50 61 67 65 29 3e 31 20 29 3b 0a 20 20 pDbPage)>1 );.
15d1a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15d1b 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 >aData );. as
15d1c 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 sert( pPage->pBt
15d1d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15d1e 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
15d1f 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 xtra(pPage->pDbP
15d20 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 age) == (void*)p
15d21 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65 Page );. asse
15d22 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
15d23 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 GetData(pPage->p
15d24 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e DbPage)==pPage->
15d25 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 aData );. ass
15d26 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15d27 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
15d28 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15d29 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
15d2a 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ref(pPage->pDbPa
15d2b 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ge);. }.}../*.*
15d2c 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 * During a rollb
15d2d 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61 ack, when the pa
15d2e 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f ger reloads info
15d2f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 rmation into the
15d30 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 cache.** so tha
15d31 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72 t the cache is r
15d32 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f estored to its o
15d33 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74 riginal state at
15d34 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a the start of.**
15d35 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
15d36 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 , for each page
15d37 72 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f restored this ro
15d38 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
15d39 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
15d3a 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73 ine needs to res
15d3b 65 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74 et the extra dat
15d3c 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65 a section at the
15d3d 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 end of the.** p
15d3e 61 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74 age to agree wit
15d3f 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64 h the restored d
15d40 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ata..*/.static v
15d41 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44 oid pageReinit(D
15d42 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 bPage *pData){.
15d43 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
15d44 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 . pPage = (MemP
15d45 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 age *)sqlite3Pag
15d46 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 erGetExtra(pData
15d47 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15d48 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
15d49 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29 count(pData)>0 )
15d4a 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 ;. if( pPage->i
15d4b 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73 sInit ){. ass
15d4c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15d4d 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
15d4e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15d4f 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
15d50 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c = 0;. if( sql
15d51 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
15d52 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 31 20 29 count(pData)>1 )
15d53 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 {. /* pPage
15d54 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20 might not be a
15d55 62 74 72 65 65 20 70 61 67 65 3b 20 20 69 74 20 btree page; it
15d56 6d 69 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72 might be an over
15d57 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 flow page.
15d58 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67 ** or ptrmap pag
15d59 65 20 6f 72 20 61 20 66 72 65 65 20 70 61 67 65 e or a free page
15d5a 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 . In those case
15d5b 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 s, the following
15d5c 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 . ** call t
15d5d 6f 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 o btreeInitPage(
15d5e 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 ) will likely re
15d5f 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15d60 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75 UPT.. ** Bu
15d61 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e t no harm is don
15d62 65 20 62 79 20 74 68 69 73 2e 20 20 41 6e 64 20 e by this. And
15d63 69 74 20 69 73 20 76 65 72 79 20 69 6d 70 6f 72 it is very impor
15d64 74 61 6e 74 20 74 68 61 74 0a 20 20 20 20 20 20 tant that.
15d65 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 ** btreeInitPage
15d66 28 29 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 () be called on
15d67 65 76 65 72 79 20 62 74 72 65 65 20 70 61 67 65 every btree page
15d68 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 20 20 20 so we make.
15d69 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f ** the call fo
15d6a 72 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 r every page tha
15d6b 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72 t comes in for r
15d6c 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 e-initing. */.
15d6d 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 btreeInitPag
15d6e 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a e(pPage);. }.
15d6f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 }.}../*.** Inv
15d70 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e oke the busy han
15d71 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 dler for a btree
15d72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15d73 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 btreeInvokeBusyH
15d74 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 andler(void *pAr
15d75 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a g){. BtShared *
15d76 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a pBt = (BtShared*
15d77 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 )pArg;. assert(
15d78 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 pBt->db );. as
15d79 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15d7a 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 tex_held(pBt->db
15d7b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 ->mutex) );. re
15d7c 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f turn sqlite3Invo
15d7d 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70 keBusyHandler(&p
15d7e 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 Bt->db->busyHand
15d7f 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ler);.}../*.** O
15d80 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 pen a database f
15d81 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c ile..** .** zFil
15d82 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d ename is the nam
15d83 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
15d84 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c e file. If zFil
15d85 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a ename is NULL.**
15d86 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 a new database
15d87 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 with a random na
15d88 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 me is created.
15d89 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 This randomly na
15d8a 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 med.** database
15d8b 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c file will be del
15d8c 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 eted when sqlite
15d8d 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73 3BtreeClose() is
15d8e 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a called..** If z
15d8f 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 Filename is ":me
15d90 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 mory:" then an i
15d91 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
15d92 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 e is created.**
15d93 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 that is automati
15d94 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20 cally destroyed
15d95 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 when it is close
15d96 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
15d97 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 database is alre
15d98 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 ady opened in th
15d99 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
15d9a 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e connection.** an
15d9b 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72 d we are in shar
15d9c 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 ed cache mode, t
15d9d 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c hen the open wil
15d9e 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a l fail with an.*
15d9f 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 * SQLITE_CONSTRA
15da0 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63 INT error. We c
15da1 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 annot allow two
15da2 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64 or more BtShared
15da3 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 .** objects in t
15da4 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
15da5 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 connection sinc
15da6 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 e doing so will
15da7 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c lead.** to probl
15da8 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 ems with locking
15da9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
15daa 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
15dab 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 treeOpen(. cons
15dac 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
15dad 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 e, /* Name of t
15dae 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 he file containi
15daf 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 ng the BTree dat
15db0 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 abase */. sqlit
15db1 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
15db2 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 /* Associated
15db3 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
15db4 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 */. Btree **pp
15db5 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a Btree, /*
15db6 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 Pointer to new
15db7 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 Btree object wri
15db8 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 tten here */. i
15db9 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
15dba 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e /* Option
15dbb 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c s */. int vfsFl
15dbc 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
15dbd 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 * Flags passed t
15dbe 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 hrough to sqlite
15dbf 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
15dc0 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 .){. sqlite3_vf
15dc1 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
15dc2 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 /* The VFS
15dc3 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 to use for this
15dc4 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 btree */. BtSha
15dc5 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 red *pBt = 0;
15dc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 /* Sha
15dc7 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 red part of btre
15dc8 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
15dc9 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 Btree *p;
15dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15dcb 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 /* Handle to ret
15dcc 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 urn */. sqlite3
15dcd 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 _mutex *mutexOpe
15dce 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 n = 0; /* Preve
15dcf 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 nts a race condi
15dd0 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 tion. Ticket #35
15dd1 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 37 */. int rc =
15dd2 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
15dd3 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
15dd4 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 code from this
15dd5 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 function */. u8
15dd6 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 nReserve;
15dd7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15dd8 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 Byte of unused s
15dd9 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 pace on each pag
15dda 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
15ddb 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 char zDbHeader[1
15ddc 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 00]; /* Databas
15ddd 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 e header content
15dde 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 */.. /* Set th
15ddf 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d e variable isMem
15de0 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61 db to true for a
15de1 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
15de2 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 base, or . ** f
15de3 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d alse for a file-
15de4 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20 based database.
15de5 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f This symbol is o
15de6 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a nly required if.
15de7 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74 ** either of t
15de8 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f he shared-data o
15de9 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61 r autovacuum fea
15dea 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c tures are compil
15deb 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 ed . ** into th
15dec 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a e library.. */.
15ded 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
15dee 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
15def 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e CACHE) || !defin
15df0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ed(SQLITE_OMIT_A
15df1 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66 UTOVACUUM). #if
15df2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15df3 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e MEMORYDB. con
15df4 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d st int isMemdb =
15df5 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 0;. #else.
15df6 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 const int isMemd
15df7 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 b = zFilename &&
15df8 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 !strcmp(zFilena
15df9 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b me, ":memory:");
15dfa 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 . #endif.#endif
15dfb 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d .. assert( db!=
15dfc 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
15dfd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15dfe 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
15dff 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 . pVfs = db->pV
15e00 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 fs;. p = sqlite
15e01 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 3MallocZero(size
15e02 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 of(Btree));. if
15e03 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 ( !p ){. retu
15e04 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
15e05 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e . }. p->inTran
15e06 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
15e07 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 p->db = db;.#i
15e08 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15e09 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
15e0a 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 p->lock.pBtree
15e0b 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 = p;. p->lock.i
15e0c 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 Table = 1;.#endi
15e0d 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 f..#if !defined(
15e0e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15e0f 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
15e10 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15e11 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 T_DISKIO). /*.
15e12 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 ** If this Btre
15e13 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 e is a candidate
15e14 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 for shared cach
15e15 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 e, try to find a
15e16 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 n. ** existing
15e17 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
15e18 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 that we can shar
15e19 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 e with. */. if
15e1a 28 20 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 ( isMemdb==0 &&
15e1b 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 zFilename && zFi
15e1c 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 lename[0] ){.
15e1d 20 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20 if( vfsFlags &
15e1e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
15e1f 45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20 EDCACHE ){.
15e20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 int nFullPathna
15e21 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 me = pVfs->mxPat
15e22 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 hname+1;. c
15e23 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 har *zFullPathna
15e24 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c me = sqlite3Mall
15e25 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 oc(nFullPathname
15e26 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
15e27 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 _mutex *mutexSha
15e28 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 red;. p->sh
15e29 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 arable = 1;.
15e2a 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 if( !zFullPath
15e2b 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 name ){.
15e2c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
15e2d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
15e2e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15e2f 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
15e30 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
15e31 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d e(pVfs, zFilenam
15e32 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e, nFullPathname
15e33 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 , zFullPathname)
15e34 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65 ;. mutexOpe
15e35 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 n = sqlite3Mutex
15e36 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
15e37 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b EX_STATIC_OPEN);
15e38 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15e39 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
15e3a 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74 Open);. mut
15e3b 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 exShared = sqlit
15e3c 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
15e3d 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
15e3e 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 _MASTER);.
15e3f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
15e40 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 ter(mutexShared)
15e41 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d ;. for(pBt=
15e42 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15e43 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15e44 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70 cheList); pBt; p
15e45 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a Bt=pBt->pNext){.
15e46 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
15e47 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 pBt->nRef>0 );.
15e48 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74 if( 0==st
15e49 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61 rcmp(zFullPathna
15e4a 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 me, sqlite3Pager
15e4b 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50 Filename(pBt->pP
15e4c 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 ager)).
15e4d 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 && sqlit
15e4e 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e e3PagerVfs(pBt->
15e4f 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b pPager)==pVfs ){
15e50 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
15e51 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f Db;. fo
15e52 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b r(iDb=db->nDb-1;
15e53 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b iDb>=0; iDb--){
15e54 0a 20 20 20 20 20 20 20 20 20 20 20 20 42 74 72 . Btr
15e55 65 65 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20 ee *pExisting =
15e56 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
15e57 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
15e58 28 20 70 45 78 69 73 74 69 6e 67 20 26 26 20 70 ( pExisting && p
15e59 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 Existing->pBt==p
15e5a 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Bt ){.
15e5b 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15e5c 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 x_leave(mutexSha
15e5d 72 65 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 red);.
15e5e 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15e5f 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 x_leave(mutexOpe
15e60 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 n);.
15e61 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
15e62 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
15e63 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
15e64 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
15e65 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 retu
15e66 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 rn SQLITE_CONSTR
15e67 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 AINT;.
15e68 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
15e69 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74 p->pBt
15e6a 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20 = pBt;.
15e6b 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 pBt->nRef++;.
15e6c 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
15e6d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15e6e 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
15e6f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
15e70 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 xShared);.
15e71 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 sqlite3_free(zFu
15e72 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
15e73 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
15e74 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b _DEBUG. else{
15e75 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 . /* In deb
15e76 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b ug mode, we mark
15e77 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20 all persistent
15e78 64 61 74 61 62 61 73 65 73 20 61 73 20 73 68 61 databases as sha
15e79 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 rable. ** e
15e7a 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72 ven when they ar
15e7b 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65 e not. This exe
15e7c 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 rcises the locki
15e7d 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 ng code and.
15e7e 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 ** gives more
15e7f 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 opportunity for
15e80 61 73 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f asserts(sqlite3_
15e81 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 mutex_held()).
15e82 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
15e83 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e s to find lockin
15e84 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 g problems..
15e85 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 */. p->sh
15e86 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 arable = 1;.
15e87 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e }.#endif. }.#en
15e88 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 dif. if( pBt==0
15e89 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a ){. /*. *
15e8a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
15e8b 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72 asserts make sur
15e8c 65 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65 e that structure
15e8d 73 20 75 73 65 64 20 62 79 20 74 68 65 20 62 74 s used by the bt
15e8e 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 ree are. ** t
15e8f 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20 he right size.
15e90 54 68 69 73 20 69 73 20 74 6f 20 67 75 61 72 64 This is to guard
15e91 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68 against size ch
15e92 61 6e 67 65 73 20 74 68 61 74 20 72 65 73 75 6c anges that resul
15e93 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f t. ** when co
15e94 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 mpiling on a dif
15e95 66 65 72 65 6e 74 20 61 72 63 68 69 74 65 63 74 ferent architect
15e96 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ure.. */.
15e97 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69 assert( sizeof(i
15e98 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 64)==8 || sizeof
15e99 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 (i64)==4 );.
15e9a 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
15e9b 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 64)==8 || sizeof
15e9c 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 (u64)==4 );.
15e9d 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
15e9e 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 32)==4 );. as
15e9f 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36 sert( sizeof(u16
15ea0 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65 )==2 );. asse
15ea1 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 rt( sizeof(Pgno)
15ea2 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 ==4 );. . pB
15ea3 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f t = sqlite3Mallo
15ea4 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 cZero( sizeof(*p
15ea5 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 Bt) );. if( p
15ea6 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 Bt==0 ){. r
15ea7 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
15ea8 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 ;. goto btr
15ea9 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ee_open_out;.
15eaa 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
15eab 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 te3PagerOpen(pVf
15eac 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c s, &pBt->pPager,
15ead 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 zFilename,.
15eae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15eaf 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45 EXTRA_SIZE
15eb0 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 , flags, vfsFlag
15eb1 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a s, pageReinit);.
15eb2 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
15eb3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15eb4 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15eb5 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70 ReadFileheader(p
15eb6 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f Bt->pPager,sizeo
15eb7 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62 f(zDbHeader),zDb
15eb8 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 Header);. }.
15eb9 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
15eba 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
15ebb 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
15ebc 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 t;. }. pBt
15ebd 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 73 ->db = db;. s
15ebe 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 qlite3PagerSetBu
15ebf 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 syhandler(pBt->p
15ec0 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f Pager, btreeInvo
15ec1 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 keBusyHandler, p
15ec2 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 Bt);. p->pBt
15ec3 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 42 = pBt;. . pB
15ec4 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a t->pCursor = 0;.
15ec5 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 pBt->pPage1
15ec6 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 65 = 0;. pBt->re
15ec7 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33 adOnly = sqlite3
15ec8 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 PagerIsreadonly(
15ec9 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
15eca 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
15ecb 3d 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48 = get2byte(&zDbH
15ecc 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 20 eader[16]);.
15ecd 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a if( pBt->pageSiz
15ece 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61 e<512 || pBt->pa
15ecf 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 geSize>SQLITE_MA
15ed0 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20 X_PAGE_SIZE.
15ed1 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70 || ((pBt->p
15ed2 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e ageSize-1)&pBt->
15ed3 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a pageSize)!=0 ){.
15ed4 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 pBt->pageS
15ed5 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 ize = 0;.#ifndef
15ed6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
15ed7 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a OVACUUM. /*
15ed8 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61 If the magic na
15ed9 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 me ":memory:" wi
15eda 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d ll create an in-
15edb 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
15edc 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c then. ** l
15edd 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63 eave the autoVac
15ede 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64 uum mode at 0 (d
15edf 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 o not auto-vacuu
15ee0 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 m), even if.
15ee1 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 ** SQLITE_DEFA
15ee2 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 ULT_AUTOVACUUM i
15ee3 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f s true. On the o
15ee4 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20 ther hand, if.
15ee5 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d ** SQLITE_OM
15ee6 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 IT_MEMORYDB has
15ee7 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 been defined, th
15ee8 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 en ":memory:" is
15ee9 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a just a. **
15eea 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61 regular file-na
15eeb 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 me. In this case
15eec 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d the auto-vacuum
15eed 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20 applies as per
15eee 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f normal.. */
15eef 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 . if( zFile
15ef0 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 name && !isMemdb
15ef1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d ){. pBt-
15ef2 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53 >autoVacuum = (S
15ef3 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
15ef4 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30 TOVACUUM ? 1 : 0
15ef5 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e );. pBt->
15ef6 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51 incrVacuum = (SQ
15ef7 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
15ef8 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a OVACUUM==2 ? 1 :
15ef9 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 0);. }.#en
15efa 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72 dif. nReser
15efb 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ve = 0;. }els
15efc 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 e{. nReserv
15efd 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30 e = zDbHeader[20
15efe 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 ];. pBt->pa
15eff 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b geSizeFixed = 1;
15f00 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15f01 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
15f02 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 pBt->autoV
15f03 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 acuum = (get4byt
15f04 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 e(&zDbHeader[36
15f05 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 + 4*4])?1:0);.
15f06 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 pBt->incrVac
15f07 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 uum = (get4byte(
15f08 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 &zDbHeader[36 +
15f09 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 7*4])?1:0);.#end
15f0a 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 if. }. rc
15f0b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
15f0c 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
15f0d 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
15f0e 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 eSize, nReserve)
15f0f 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
15f10 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f oto btree_open_o
15f11 75 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 ut;. pBt->usa
15f12 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 bleSize = pBt->p
15f13 61 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 ageSize - nReser
15f14 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ve;. assert(
15f15 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 (pBt->pageSize &
15f16 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 7)==0 ); /* 8-
15f17 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f byte alignment o
15f18 66 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 f pageSize */.
15f19 20 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 .#if !defined(S
15f1a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15f1b 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 D_CACHE) && !def
15f1c 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
15f1d 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 _DISKIO). /*
15f1e 41 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 Add the new BtSh
15f1f 61 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 ared object to t
15f20 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 he linked list s
15f21 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 harable BtShared
15f22 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
15f23 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b ( p->sharable ){
15f24 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15f25 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 utex *mutexShare
15f26 64 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 d;. pBt->nR
15f27 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 ef = 1;. mu
15f28 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 texShared = sqli
15f29 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
15f2a 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
15f2b 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 C_MASTER);.
15f2c 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 if( SQLITE_THRE
15f2d 41 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65 ADSAFE && sqlite
15f2e 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
15f2f 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
15f30 20 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d pBt->mutex =
15f31 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
15f32 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
15f33 46 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 FAST);. i
15f34 66 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 f( pBt->mutex==0
15f35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
15f36 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
15f37 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d . db->m
15f38 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b allocFailed = 0;
15f39 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
15f3a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a btree_open_out;.
15f3b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15f3c 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
15f3d 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
15f3e 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 xShared);.
15f3f 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f pBt->pNext = GLO
15f40 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 BAL(BtShared*,sq
15f41 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 lite3SharedCache
15f42 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f List);. GLO
15f43 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 BAL(BtShared*,sq
15f44 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 lite3SharedCache
15f45 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 List) = pBt;.
15f46 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15f47 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 _leave(mutexShar
15f48 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ed);. }.#endi
15f49 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 f. }..#if !defi
15f4a 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15f4b 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 SHARED_CACHE) &&
15f4c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
15f4d 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 _OMIT_DISKIO).
15f4e 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 /* If the new Bt
15f4f 72 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 ree uses a shara
15f50 62 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 ble pBtShared, t
15f51 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 hen link the new
15f52 0a 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f . ** Btree into
15f53 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c the list of all
15f54 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 sharable Btrees
15f55 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f for the same co
15f56 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 nnection.. ** T
15f57 68 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 he list is kept
15f58 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 in ascending ord
15f59 65 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 er by pBt addres
15f5a 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d s.. */. if( p-
15f5b 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
15f5c 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 int i;. Btre
15f5d 65 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 e *pSib;. for
15f5e 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
15f5f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
15f60 20 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 (pSib = db->aDb
15f61 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 [i].pBt)!=0 && p
15f62 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b Sib->sharable ){
15f63 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
15f64 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 pSib->pPrev ){ p
15f65 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 Sib = pSib->pPre
15f66 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 v; }. if(
15f67 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42 p->pBt<pSib->pB
15f68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 t ){. p
15f69 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a ->pNext = pSib;.
15f6a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 p->pPr
15f6b 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ev = 0;.
15f6c 20 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20 pSib->pPrev =
15f6d 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 p;. }else
15f6e 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c {. whil
15f6f 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26 e( pSib->pNext &
15f70 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 & pSib->pNext->p
15f71 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20 Bt<p->pBt ){.
15f72 20 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20 pSib =
15f73 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pSib->pNext;.
15f74 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15f75 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 p->pNext = pS
15f76 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ib->pNext;.
15f77 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 p->pPrev =
15f78 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pSib;.
15f79 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a if( p->pNext ){.
15f7a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 p->p
15f7b 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b Next->pPrev = p;
15f7c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
15f7d 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 pSib->pNe
15f7e 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 xt = p;.
15f7f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
15f80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
15f81 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42 }.#endif. *ppB
15f82 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65 tree = p;..btree
15f83 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 _open_out:. if(
15f84 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15f85 7b 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 {. if( pBt &&
15f86 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a pBt->pPager ){.
15f87 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
15f88 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 erClose(pBt->pPa
15f89 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ger);. }.
15f8a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 sqlite3_free(pBt
15f8b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
15f8c 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42 ree(p);. *ppB
15f8d 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 tree = 0;. }.
15f8e 69 66 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b if( mutexOpen ){
15f8f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
15f90 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15f91 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 mutexOpen) );.
15f92 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15f93 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 leave(mutexOpen)
15f94 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
15f95 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 c;.}../*.** Decr
15f96 65 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 ement the BtShar
15f97 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e ed.nRef counter.
15f98 20 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 When it reache
15f99 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 s zero,.** remov
15f9a 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 e the BtShared s
15f9b 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 tructure from th
15f9c 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 e sharing list.
15f9d 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 Return.** true
15f9e 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e if the BtShared.
15f9f 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 nRef counter rea
15fa0 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 ches zero and re
15fa1 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 turn.** false if
15fa2 20 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 it is still pos
15fa3 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 itive..*/.static
15fa4 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 int removeFromS
15fa5 68 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 haringList(BtSha
15fa6 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 red *pBt){.#ifnd
15fa7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
15fa8 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71 HARED_CACHE. sq
15fa9 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 lite3_mutex *pMa
15faa 73 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64 ster;. BtShared
15fab 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 *pList;. int r
15fac 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 emoved = 0;.. a
15fad 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15fae 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 utex_notheld(pBt
15faf 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d ->mutex) );. pM
15fb0 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d aster = sqlite3M
15fb1 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
15fb2 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
15fb3 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 STER);. sqlite3
15fb4 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 _mutex_enter(pMa
15fb5 73 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 ster);. pBt->nR
15fb6 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d ef--;. if( pBt-
15fb7 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 >nRef<=0 ){.
15fb8 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 if( GLOBAL(BtSha
15fb9 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
15fba 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 edCacheList)==pB
15fbb 74 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 t ){. GLOBA
15fbc 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
15fbd 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
15fbe 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 st) = pBt->pNext
15fbf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
15fc0 20 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 pList = GLOBA
15fc1 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
15fc2 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
15fc3 73 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 st);. while
15fc4 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 ( ALWAYS(pList)
15fc5 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 && pList->pNext!
15fc6 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 =pBt ){.
15fc7 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 pList=pList->pNe
15fc8 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 xt;. }.
15fc9 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69 if( ALWAYS(pLi
15fca 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 st) ){. p
15fcb 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 List->pNext = pB
15fcc 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 t->pNext;.
15fcd 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
15fce 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
15fcf 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 E ){. sqlit
15fd0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 e3_mutex_free(pB
15fd1 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d t->mutex);. }
15fd2 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 . removed = 1
15fd3 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
15fd4 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 mutex_leave(pMas
15fd5 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ter);. return r
15fd6 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 emoved;.#else.
15fd7 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 return 1;.#endif
15fd8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 .}../*.** Make s
15fd9 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 ure pBt->pTmpSpa
15fda 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 ce points to an
15fdb 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a allocation of .*
15fdc 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 * MX_CELL_SIZE(p
15fdd 42 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 Bt) bytes..*/.st
15fde 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 atic void alloca
15fdf 74 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 teTempSpace(BtSh
15fe0 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 ared *pBt){. if
15fe1 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 ( !pBt->pTmpSpac
15fe2 65 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 e ){. pBt->pT
15fe3 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 mpSpace = sqlite
15fe4 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 3PageMalloc( pBt
15fe5 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 ->pageSize );.
15fe6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 }.}../*.** Free
15fe7 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 the pBt->pTmpSpa
15fe8 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f ce allocation.*/
15fe9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
15fea 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 eTempSpace(BtSha
15feb 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c red *pBt){. sql
15fec 69 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42 ite3PageFree( pB
15fed 74 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 t->pTmpSpace);.
15fee 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
15fef 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c = 0;.}../*.** Cl
15ff0 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 ose an open data
15ff1 62 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 base and invalid
15ff2 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e ate all cursors.
15ff3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15ff4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15ff5 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a reeClose(Btree *
15ff6 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a p){. BtShared *
15ff7 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
15ff8 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a BtCursor *pCur;.
15ff9 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 . /* Close all
15ffa 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 cursors opened v
15ffb 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 ia this handle.
15ffc 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 */. assert( sq
15ffd 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15ffe 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
15fff 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16000 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 72 Enter(p);. pCur
16001 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b = pBt->pCursor;
16002 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29 . while( pCur )
16003 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a {. BtCursor *
16004 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20 pTmp = pCur;.
16005 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e pCur = pCur->pN
16006 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d ext;. if( pTm
16007 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a p->pBtree==p ){.
16008 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
16009 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54 eeCloseCursor(pT
1600a 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a mp);. }. }..
1600b 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e /* Rollback an
1600c 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 y active transac
1600d 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68 tion and free th
1600e 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 e handle structu
1600f 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c re.. ** The cal
16010 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
16011 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 eRollback() drop
16012 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b s any table-lock
16013 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74 s held by. ** t
16014 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f his handle.. */
16015 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 . sqlite3BtreeR
16016 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71 ollback(p);. sq
16017 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
16018 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 p);.. /* If the
16019 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 re are still oth
1601a 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 er outstanding r
1601b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
1601c 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 shared-btree.
1601d 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 ** structure, re
1601e 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 turn now. The re
1601f 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 mainder of this
16020 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 procedure cleans
16021 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 . ** up the sh
16022 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f ared-btree.. */
16023 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 . assert( p->wa
16024 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 ntToLock==0 && p
16025 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 ->locked==0 );.
16026 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
16027 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 e || removeFromS
16028 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 haringList(pBt)
16029 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 ){. /* The pB
1602a 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f t is no longer o
1602b 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 n the sharing li
1602c 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 st, so we can ac
1602d 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 cess. ** it w
1602e 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
1602f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e hold the mutex.
16030 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
16031 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c lean out and del
16032 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 ete the BtShared
16033 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a object.. */.
16034 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 assert( !pBt
16035 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 ->pCursor );.
16036 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
16037 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b se(pBt->pPager);
16038 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 . if( pBt->xF
16039 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 reeSchema && pBt
1603a 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 ->pSchema ){.
1603b 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 pBt->xFreeSch
1603c 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ema(pBt->pSchema
1603d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
1603e 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 ite3_free(pBt->p
1603f 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65 Schema);. fre
16040 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
16041 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
16042 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 e(pBt);. }..#if
16043 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16044 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
16045 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
16046 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 oLock==0 );. as
16047 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d sert( p->locked=
16048 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 =0 );. if( p->p
16049 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d Prev ) p->pPrev-
1604a 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 >pNext = p->pNex
1604b 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 t;. if( p->pNex
1604c 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 t ) p->pNext->pP
1604d 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a rev = p->pPrev;.
1604e 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
1604f 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 3_free(p);. ret
16050 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
16051 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
16052 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 he limit on the
16053 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
16054 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63 allowed in the c
16055 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ache..**.** The
16056 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
16057 66 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73 f cache pages is
16058 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f set to the abso
16059 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 lute.** value of
1605a 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50 mxPage. If mxP
1605b 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c age is negative,
1605c 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a the pager will.
1605d 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63 ** operate async
1605e 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77 hronously - it w
1605f 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20 ill not stop to
16060 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74 do fsync()s.** t
16061 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73 o insure data is
16062 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
16063 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66 disk surface bef
16064 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e ore.** continuin
16065 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 g. Transactions
16066 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73 still work if s
16067 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 ynchronous is of
16068 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 f,.** and the da
16069 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 tabase cannot be
1606a 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68 corrupted if th
1606b 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 is program.** cr
1606c 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74 ashes. But if t
1606d 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 he operating sys
1606e 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74 tem crashes or t
1606f 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62 here is.** an ab
16070 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75 rupt power failu
16071 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e re when synchron
16072 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20 ous is off, the
16073 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c database.** coul
16074 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 d be left in an
16075 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64 inconsistent and
16076 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73 unrecoverable s
16077 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f tate..** Synchro
16078 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65 nous is on by de
16079 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73 fault so databas
1607a 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 e corruption is
1607b 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 not.** normally
1607c 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 a worry..*/.SQLI
1607d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1607e 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
1607f 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 cheSize(Btree *p
16080 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
16081 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
16082 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
16083 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
16084 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
16085 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
16086 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
16087 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
16088 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 Cachesize(pBt->p
16089 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a Pager, mxPage);.
1608a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
1608b 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1608c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1608d 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
1608e 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 way data is sync
1608f 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 ed to disk in or
16090 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 der to increase
16091 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 or decrease.** h
16092 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 ow well the data
16093 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d base resists dam
16094 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 age due to OS cr
16095 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a ashes and power.
16096 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 ** failures. Le
16097 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d vel 1 is the sam
16098 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 e as asynchronou
16099 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 s (no syncs() oc
1609a 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 cur and.** there
1609b 20 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 is a high proba
1609c 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 bility of damage
1609d 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 ) Level 2 is th
1609e 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 e default. Ther
1609f 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c e.** is a very l
160a0 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 ow but non-zero
160a1 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 probability of d
160a2 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 amage. Level 3
160a3 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 reduces the.** p
160a4 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 robability of da
160a5 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 mage to near zer
160a6 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 o but with a wri
160a7 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 te performance r
160a8 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 eduction..*/.#if
160a9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
160aa 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 _PAGER_PRAGMAS.S
160ab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
160ac 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
160ad 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 tSafetyLevel(Btr
160ae 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c ee *p, int level
160af 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b , int fullSync){
160b0 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
160b1 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 = p->pBt;. ass
160b2 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
160b3 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
160b4 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
160b5 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
160b6 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 . sqlite3PagerS
160b7 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 etSafetyLevel(pB
160b8 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c t->pPager, level
160b9 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 , fullSync);. s
160ba 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
160bb 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
160bc 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
160bd 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
160be 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 TRUE if the give
160bf 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74 n btree is set t
160c0 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 o safety level 1
160c1 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 . In other.** w
160c2 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 ords, return TRU
160c3 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f E if no sync() o
160c4 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 ccurs on the dis
160c5 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 k files..*/.SQLI
160c6 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
160c7 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 qlite3BtreeSyncD
160c8 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70 isabled(Btree *p
160c9 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
160ca 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 Bt = p->pBt;. i
160cb 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
160cc 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
160cd 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
160ce 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 ) ); . sqlite3
160cf 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
160d0 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20 assert( pBt &&
160d1 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20 pBt->pPager );.
160d2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
160d3 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 erNosync(pBt->pP
160d4 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 ager);. sqlite3
160d5 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
160d6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
160d7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
160d8 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
160d9 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e AGMAS) || !defin
160da 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
160db 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 ACUUM)./*.** Cha
160dc 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 nge the default
160dd 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 pages size and t
160de 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 he number of res
160df 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20 erved bytes per
160e0 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 page..** Or, if
160e1 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 61 the page size ha
160e2 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 s already been f
160e3 69 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c ixed, return SQL
160e4 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a ITE_READONLY .**
160e5 20 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e without changin
160e6 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a g anything..**.*
160e7 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 * The page size
160e8 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 must be a power
160e9 6f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 of 2 between 512
160ea 20 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 and 65536. If
160eb 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 the page.** size
160ec 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e supplied does n
160ed 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e ot meet this con
160ee 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65 straint then the
160ef 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f page size is no
160f0 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a t.** changed..**
160f1 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61 .** Page sizes a
160f2 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 re constrained t
160f3 6f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 o be a power of
160f4 74 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 two so that the
160f5 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 region.** of the
160f6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 database file u
160f7 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 sed for locking
160f8 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45 (beginning at PE
160f9 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 NDING_BYTE,.** t
160fa 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 he first byte pa
160fb 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64 st the 1GB bound
160fc 61 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 29 ary, 0x40000000)
160fd 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a needs to occur.
160fe 2a 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e ** at the beginn
160ff 69 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a ing of a page..*
16100 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 *.** If paramete
16101 72 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65 r nReserve is le
16102 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 ss than zero, th
16103 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 en the number of
16104 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74 reserved.** byt
16105 65 73 20 70 65 72 20 70 61 67 65 20 69 73 20 6c es per page is l
16106 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a eft unchanged..*
16107 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78 *.** If the iFix
16108 21 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67 !=0 then the pag
16109 65 53 69 7a 65 46 69 78 65 64 20 66 6c 61 67 20 eSizeFixed flag
1610a 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74 is set so that t
1610b 68 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 he page size.**
1610c 61 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d and autovacuum m
1610d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 ode can no longe
1610e 72 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f r be changed..*/
1610f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16110 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16111 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 SetPageSize(Btre
16112 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69 e *p, int pageSi
16113 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ze, int nReserve
16114 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 , int iFix){. i
16115 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
16116 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 K;. BtShared *p
16117 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 Bt = p->pBt;. a
16118 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e ssert( nReserve>
16119 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c =-1 && nReserve<
1611a 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 =255 );. sqlite
1611b 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1611c 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 if( pBt->pageS
1611d 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 izeFixed ){.
1611e 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1611f 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e e(p);. return
16120 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
16121 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 ;. }. if( nRes
16122 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 erve<0 ){. nR
16123 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 eserve = pBt->pa
16124 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 geSize - pBt->us
16125 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 ableSize;. }.
16126 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 assert( nReserve
16127 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c >=0 && nReserve<
16128 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 61 =255 );. if( pa
16129 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 geSize>=512 && p
1612a 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f ageSize<=SQLITE_
1612b 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 MAX_PAGE_SIZE &&
1612c 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65 53 . ((pageS
1612d 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 ize-1)&pageSize)
1612e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
1612f 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 t( (pageSize & 7
16130 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )==0 );. asse
16131 72 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31 rt( !pBt->pPage1
16132 20 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f && !pBt->pCurso
16133 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 r );. pBt->pa
16134 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 geSize = (u16)pa
16135 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 65 geSize;. free
16136 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
16137 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
16138 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
16139 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
1613a 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20 &pBt->pageSize,
1613b 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 74 nReserve);. pBt
1613c 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 ->usableSize = p
1613d 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 Bt->pageSize - (
1613e 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 u16)nReserve;.
1613f 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e if( iFix ) pBt->
16140 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 pageSizeFixed =
16141 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 1;. sqlite3Btre
16142 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
16143 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16144 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 Return the curr
16145 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 ently defined pa
16146 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 ge size.*/.SQLIT
16147 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16148 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
16149 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b eSize(Btree *p){
1614a 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 . return p->pBt
1614b 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f ->pageSize;.}../
1614c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1614d 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1614e 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 of space at the
1614f 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 end of every pag
16150 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e e that.** are in
16151 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 tentually left u
16152 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 nused. This is
16153 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 the "reserved" s
16154 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 pace that is.**
16155 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 sometimes used b
16156 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f y extensions..*/
16157 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16158 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16159 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 GetReserve(Btree
1615a 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 *p){. int n;.
1615b 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1615c 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e er(p);. n = p->
1615d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
1615e 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 p->pBt->usableSi
1615f 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 ze;. sqlite3Btr
16160 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
16161 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn n;.}../*.**
16162 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d Set the maximum
16163 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 page count for
16164 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 a database if mx
16165 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 Page is positive
16166 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 ..** No changes
16167 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 are made if mxPa
16168 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 ge is 0 or negat
16169 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 ive..** Regardle
1616a 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 ss of the value
1616b 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 of mxPage, retur
1616c 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 n the maximum pa
1616d 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c ge count..*/.SQL
1616e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1616f 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 sqlite3BtreeMaxP
16170 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a ageCount(Btree *
16171 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a p, int mxPage){.
16172 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
16173 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
16174 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 . n = sqlite3Pa
16175 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 gerMaxPageCount(
16176 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 p->pBt->pPager,
16177 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 mxPage);. sqlit
16178 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
16179 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 . return n;.}.#
1617a 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
1617b 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 d(SQLITE_OMIT_PA
1617c 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 GER_PRAGMAS) ||
1617d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1617e 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a OMIT_VACUUM) */.
1617f 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
16180 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 e 'auto-vacuum'
16181 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 property of the
16182 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 database. If the
16183 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 'autoVacuum'.**
16184 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
16185 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 n-zero, then aut
16186 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 o-vacuum mode is
16187 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 enabled. If zer
16188 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 o, it.** is disa
16189 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c bled. The defaul
1618a 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 t value for the
1618b 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 auto-vacuum prop
1618c 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 erty is .** dete
1618d 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 rmined by the SQ
1618e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
1618f 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a OVACUUM macro..*
16190 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16191 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16192 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 eSetAutoVacuum(B
16193 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 tree *p, int aut
16194 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 oVacuum){.#ifdef
16195 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
16196 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e OVACUUM. return
16197 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
16198 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 ;.#else. BtShar
16199 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1619a 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
1619b 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 ITE_OK;. u8 av
1619c 3d 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d = (u8)autoVacuum
1619d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
1619e 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 eEnter(p);. if(
1619f 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
161a0 78 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 xed && (av ?1:0)
161a1 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 !=pBt->autoVacuu
161a2 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 m ){. rc = SQ
161a3 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
161a4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d }else{. pBt-
161a5 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 >autoVacuum = av
161a6 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e ?1:0;. pBt->
161a7 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d incrVacuum = av=
161a8 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 =2 ?1:0;. }. s
161a9 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
161aa 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
161ab 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
161ac 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c * Return the val
161ad 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d ue of the 'auto-
161ae 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 vacuum' property
161af 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d . If auto-vacuum
161b0 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 is .** enabled
161b1 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 1 is returned. O
161b2 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 therwise 0..*/.S
161b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
161b4 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
161b5 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
161b6 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 e *p){.#ifdef SQ
161b7 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
161b8 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 CUUM. return BT
161b9 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e REE_AUTOVACUUM_N
161ba 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 ONE;.#else. int
161bb 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
161bc 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
161bd 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 c = (. (!p->p
161be 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f Bt->autoVacuum)?
161bf 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
161c0 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e _NONE:. (!p->
161c1 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 pBt->incrVacuum)
161c2 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 ?BTREE_AUTOVACUU
161c3 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 M_FULL:. BTRE
161c4 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 E_AUTOVACUUM_INC
161c5 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 R. );. sqlite3
161c6 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
161c7 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 return rc;.#end
161c8 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 if.}.../*.** Get
161c9 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
161ca 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 pPage1 of the da
161cb 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
161cc 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 is will.** also
161cd 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f acquire a readlo
161ce 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e ck on that file.
161cf 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
161d0 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
161d1 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 success. If the
161d2 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a file is not a.*
161d3 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 * well-formed da
161d4 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
161d5 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
161d6 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
161d7 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
161d8 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
161d9 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b database is lock
161da 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ed. SQLITE_NOME
161db 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 M.** is returned
161dc 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f if we run out o
161dd 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 f memory. .*/.st
161de 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 atic int lockBtr
161df 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ee(BtShared *pBt
161e0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d ){. int rc;. M
161e1 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a emPage *pPage1;.
161e2 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 int nPage;..
161e3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
161e4 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
161e5 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
161e6 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d rt( pBt->pPage1=
161e7 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c =0 );. rc = sql
161e8 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c ite3PagerSharedL
161e9 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29 ock(pBt->pPager)
161ea 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
161eb 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
161ec 63 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 c;. rc = btreeG
161ed 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 etPage(pBt, 1, &
161ee 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 pPage1, 0);. if
161ef 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
161f0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 ) return rc;..
161f1 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b /* Do some check
161f2 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 ing to help insu
161f3 72 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 6f re the file we o
161f4 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 0a pened really is.
161f5 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 74 ** a valid dat
161f6 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a abase file. . *
161f7 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 /. rc = sqlite3
161f8 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
161f9 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 Bt->pPager, &nPa
161fa 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 ge);. if( rc!=S
161fb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
161fc 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
161fd 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20 failed;. }else
161fe 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 if( nPage>0 ){.
161ff 20 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b int pageSize;
16200 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 . int usableS
16201 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 ize;. u8 *pag
16202 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 e1 = pPage1->aDa
16203 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ta;. rc = SQL
16204 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 ITE_NOTADB;.
16205 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 if( memcmp(page1
16206 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 , zMagicHeader,
16207 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 16)!=0 ){.
16208 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
16209 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
1620a 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e if( page1[18]>
1620b 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 1 ){. pBt->
1620c 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 readOnly = 1;.
1620d 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 }. if( page
1620e 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 1[19]>1 ){.
1620f 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 goto page1_init
16210 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a _failed;. }..
16211 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d /* The maxim
16212 75 6d 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 um embedded frac
16213 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 78 61 tion must be exa
16214 63 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 20 74 ctly 25%. And t
16215 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a he minimum. *
16216 2a 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 * embedded fract
16217 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 2e 35 ion must be 12.5
16218 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d % for both leaf-
16219 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 data and non-lea
1621a 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 f-data.. ** T
1621b 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 he original desi
1621c 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 gn allowed these
1621d 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 amounts to vary
1621e 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20 , but as of.
1621f 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 ** version 3.6.0
16220 2c 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65 , we require the
16221 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 m to be fixed..
16222 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 */. if( me
16223 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c mcmp(&page1[21],
16224 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c "\100\040\040",
16225 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 3)!=0 ){. g
16226 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 oto page1_init_f
16227 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 ailed;. }.
16228 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 pageSize = get2
16229 62 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29 byte(&page1[16])
1622a 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 ;. if( ((page
1622b 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 Size-1)&pageSize
1622c 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 )!=0 || pageSize
1622d 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20 20 <512 ||.
1622e 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 (SQLITE_MAX_PAGE
1622f 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20 70 _SIZE<32768 && p
16230 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
16231 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20 20 AX_PAGE_SIZE).
16232 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
16233 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
16234 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 d;. }. ass
16235 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 ert( (pageSize &
16236 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 7)==0 );. us
16237 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 ableSize = pageS
16238 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b ize - page1[20];
16239 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69 7a . if( pageSiz
1623a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e!=pBt->pageSize
1623b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 ){. /* Aft
1623c 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 66 er reading the f
1623d 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
1623e 20 64 61 74 61 62 61 73 65 20 61 73 73 75 6d 69 database assumi
1623f 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20 ng a page size.
16240 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 ** of BtSha
16241 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 red.pageSize, we
16242 20 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64 have discovered
16243 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 that the page-s
16244 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 ize is. **
16245 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a actually pageSiz
16246 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 e. Unlock the da
16247 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42 tabase, leave pB
16248 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20 t->pPage1 at.
16249 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 ** zero and r
1624a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
1624b 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c The caller will
1624c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
1624d 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 ion. ** aga
1624e 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 in with the corr
1624f 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 ect page-size..
16250 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 */. re
16251 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 leasePage(pPage1
16252 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73 );. pBt->us
16253 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 29 ableSize = (u16)
16254 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 usableSize;.
16255 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
16256 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b = (u16)pageSize;
16257 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53 . freeTempS
16258 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 pace(pBt);.
16259 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1625a 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 erSetPagesize(pB
1625b 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d t->pPager, &pBt-
1625c 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 >pageSize,.
1625d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1625e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
1625f 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a geSize-usableSiz
16260 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
16261 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 rc;. }. i
16262 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38 f( usableSize<48
16263 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
16264 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
16265 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 d;. }. pBt
16266 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 ->pageSize = (u1
16267 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 6)pageSize;.
16268 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
16269 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a = (u16)usableSiz
1626a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e;.#ifndef SQLIT
1626b 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1626c 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 M. pBt->autoV
1626d 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 acuum = (get4byt
1626e 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a e(&page1[36 + 4*
1626f 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 4])?1:0);. pB
16270 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
16271 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 (get4byte(&page1
16272 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 [36 + 7*4])?1:0)
16273 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 ;.#endif. }..
16274 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 /* maxLocal is t
16275 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e he maximum amoun
16276 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 t of payload to
16277 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f store locally fo
16278 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 r. ** a cell.
16279 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 Make sure it is
1627a 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 small enough so
1627b 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 that at least mi
1627c 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c nFanout. ** cel
1627d 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 ls can will fit
1627e 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 on one page. We
1627f 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 assume a 10-byt
16280 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 e page header..
16281 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 ** Besides the
16282 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c payload, the cel
16283 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 l must store:.
16284 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f ** 2-byte po
16285 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c inter to the cel
16286 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 l. ** 4-byt
16287 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a e child pointer.
16288 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 ** 9-byte
16289 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 nKey value. **
1628a 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 4-byte nData
1628b 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 value. **
1628c 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 4-byte overflow
1628d 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a page pointer. *
1628e 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 * So a cell cons
1628f 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 ists of a 2-byte
16290 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65 poiner, a heade
16291 72 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 r which is as mu
16292 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 ch as. ** 17 by
16293 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e tes long, 0 to N
16294 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 bytes of payloa
16295 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e d, and an option
16296 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c al 4 byte overfl
16297 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 ow. ** page poi
16298 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 nter.. */. pBt
16299 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 ->maxLocal = (pB
1629a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
1629b 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20 )*64/255 - 23;.
1629c 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d pBt->minLocal =
1629d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a (pBt->usableSiz
1629e 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 e-12)*32/255 - 2
1629f 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 3;. pBt->maxLea
162a0 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 f = pBt->usableS
162a1 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d ize - 35;. pBt-
162a2 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d >minLeaf = (pBt-
162a3 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a >usableSize-12)*
162a4 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61 32/255 - 23;. a
162a5 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c ssert( pBt->maxL
162a6 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 eaf + 23 <= MX_C
162a7 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b ELL_SIZE(pBt) );
162a8 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d . pBt->pPage1 =
162a9 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72 pPage1;. retur
162aa 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 n SQLITE_OK;..pa
162ab 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a ge1_init_failed:
162ac 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
162ad 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 Page1);. pBt->p
162ae 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 Page1 = 0;. ret
162af 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
162b0 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
162b1 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 outstanding cur
162b2 73 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 sors and we are
162b3 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c not in the middl
162b4 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 e.** of a transa
162b5 63 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 ction but there
162b6 69 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f is a read lock o
162b7 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 n the database,
162b8 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 then.** this rou
162b9 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 tine unrefs the
162ba 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 first page of th
162bb 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
162bc 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 which .** has th
162bd 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 e effect of rele
162be 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c asing the read l
162bf 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ock..**.** If th
162c0 65 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63 ere is a transac
162c1 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 tion in progress
162c2 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
162c3 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
162c4 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b atic void unlock
162c5 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74 BtreeIfUnused(Bt
162c6 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
162c7 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
162c8 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
162c9 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
162ca 72 74 28 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 rt( pBt->pCursor
162cb 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 ==0 || pBt->inTr
162cc 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f ansaction>TRANS_
162cd 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 42 NONE );. if( pB
162ce 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
162cf 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 ==TRANS_NONE &&
162d0 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 pBt->pPage1!=0 )
162d1 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 {. assert( pB
162d2 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
162d3 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
162d4 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
162d5 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
162d6 29 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 )==1 );. asse
162d7 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d rt( pBt->pPage1-
162d8 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 72 65 >aData );. re
162d9 6c 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70 leasePage(pBt->p
162da 50 61 67 65 31 29 3b 0a 20 20 20 20 70 42 74 2d Page1);. pBt-
162db 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d >pPage1 = 0;. }
162dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 .}../*.** If pBt
162dd 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d points to an em
162de 70 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 6f pty file then co
162df 6e 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 79 nvert that empty
162e0 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 file.** into a
162e1 6e 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 61 new empty databa
162e2 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 se by initializi
162e3 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ng the first pag
162e4 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 e of.** the data
162e5 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
162e6 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 int newDatabase(
162e7 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
162e8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a MemPage *pP1;.
162e9 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
162ea 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b *data;. int rc;
162eb 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 . int nPage;..
162ec 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
162ed 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
162ee 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
162ef 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 = sqlite3PagerPa
162f0 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 gecount(pBt->pPa
162f1 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 ger, &nPage);.
162f2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
162f3 4b 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a K || nPage>0 ){.
162f4 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
162f5 20 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e }. pP1 = pBt->
162f6 70 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 pPage1;. assert
162f7 28 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 ( pP1!=0 );. da
162f8 74 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b ta = pP1->aData;
162f9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
162fa 61 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 agerWrite(pP1->p
162fb 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
162fc 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
162fd 20 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d memcpy(data, zM
162fe 61 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 agicHeader, size
162ff 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 of(zMagicHeader)
16300 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a );. assert( siz
16301 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 eof(zMagicHeader
16302 29 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 )==16 );. put2b
16303 79 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 yte(&data[16], p
16304 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
16305 20 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 data[18] = 1;.
16306 20 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 data[19] = 1;.
16307 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 assert( pBt->us
16308 61 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 ableSize<=pBt->p
16309 61 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e ageSize && pBt->
1630a 75 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d usableSize+255>=
1630b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a pBt->pageSize);.
1630c 20 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 data[20] = (u8
1630d 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 )(pBt->pageSize
1630e 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a - pBt->usableSiz
1630f 65 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d e);. data[21] =
16310 20 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 64;. data[22]
16311 3d 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d = 32;. data[23]
16312 20 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 = 32;. memset(
16313 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 &data[24], 0, 10
16314 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 0-24);. zeroPag
16315 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 e(pP1, PTF_INTKE
16316 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c Y|PTF_LEAF|PTF_L
16317 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 EAFDATA );. pBt
16318 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 ->pageSizeFixed
16319 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c = 1;.#ifndef SQL
1631a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
1631b 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 UUM. assert( pB
1631c 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 t->autoVacuum==1
1631d 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 || pBt->autoVac
1631e 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 uum==0 );. asse
1631f 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 rt( pBt->incrVac
16320 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 uum==1 || pBt->i
16321 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a ncrVacuum==0 );.
16322 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 put4byte(&data
16323 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d [36 + 4*4], pBt-
16324 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 >autoVacuum);.
16325 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 put4byte(&data[3
16326 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6 + 7*4], pBt->i
16327 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 ncrVacuum);.#end
16328 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 if. return SQLI
16329 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1632a 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 Attempt to start
1632b 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 a new transacti
1632c 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e on. A write-tran
1632d 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 saction.** is st
1632e 61 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 arted if the sec
1632f 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ond argument is
16330 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 nonzero, otherwi
16331 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 se a read-.** tr
16332 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 ansaction. If t
16333 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
16334 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 nt is 2 or more
16335 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a and exclusive.**
16336 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
16337 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 started, meaning
16338 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 that no other p
16339 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 rocess is allowe
1633a 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 d.** to access t
1633b 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 he database. A
1633c 70 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e preexisting tran
1633d 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 saction may not
1633e 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 be.** upgraded t
1633f 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 o exclusive by c
16340 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
16341 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d ine a second tim
16342 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 e - the.** exclu
16343 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 sivity flag only
16344 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 works for a new
16345 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
16346 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e .** A write-tran
16347 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 saction must be
16348 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 started before a
16349 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a ttempting any .*
1634a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 * changes to the
1634b 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 database. None
1634c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
1634d 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 g routines .** w
1634e 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 ill work unless
1634f 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
16350 20 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a started first:.
16351 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 **.** sqlit
16352 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 e3BtreeCreateTab
16353 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c le().** sql
16354 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 ite3BtreeCreateI
16355 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 ndex().** s
16356 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
16357 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 Table().**
16358 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 sqlite3BtreeDrop
16359 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 Table().**
1635a 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 sqlite3BtreeInse
1635b 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c rt().** sql
1635c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 ite3BtreeDelete(
1635d 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
1635e 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
1635f 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 ().**.** If an i
16360 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 nitial attempt t
16361 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f o acquire the lo
16362 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 ck fails because
16363 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 of lock content
16364 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 ion.** and the d
16365 61 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 atabase was prev
16366 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c iously unlocked,
16367 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 then invoke the
16368 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a busy handler.**
16369 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 if there is one
1636a 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 . But if there
1636b 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 was previously a
1636c 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e read-lock, do n
1636d 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 ot.** invoke the
1636e 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 busy handler -
1636f 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 just return SQLI
16370 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 TE_BUSY. SQLITE
16371 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 _BUSY is .** ret
16372 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 urned when there
16373 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 is already a re
16374 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 ad-lock in order
16375 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 to avoid a dead
16376 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 lock..**.** Supp
16377 6f 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 ose there are tw
16378 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e o processes A an
16379 64 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 d B. A has a re
1637a 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 ad lock and B ha
1637b 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 s.** a reserved
1637c 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 lock. B tries t
1637d 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 o promote to exc
1637e 6c 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c lusive but is bl
1637f 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a ocked because.**
16380 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 of A's read loc
16381 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 k. A tries to p
16382 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 romote to reserv
16383 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 ed but is blocke
16384 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f d by B..** One o
16385 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 r the other of t
16386 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 he two processes
16387 20 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f must give way o
16388 72 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a r there can be.*
16389 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 * no progress.
1638a 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c By returning SQL
1638b 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 ITE_BUSY and not
1638c 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 invoking the bu
1638d 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 sy callback.** w
1638e 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 hen A already ha
1638f 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 s a read lock, w
16390 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f e encourage A to
16391 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 give up and let
16392 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a B.** proceed..*
16393 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16394 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16395 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 eBeginTrans(Btre
16396 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 e *p, int wrflag
16397 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 ){. sqlite3 *pB
16398 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 lock = 0;. BtSh
16399 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
1639a 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 Bt;. int rc = S
1639b 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c QLITE_OK;.. sql
1639c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1639d 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 );. btreeIntegr
1639e 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 ity(p);.. /* If
1639f 20 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c the btree is al
163a0 72 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 ready in a write
163a1 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 -transaction, or
163a2 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 it. ** is alre
163a3 61 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 ady in a read-tr
163a4 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 ansaction and a
163a5 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e read-transaction
163a6 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 . ** is request
163a7 65 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f ed, this is a no
163a8 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 -op.. */. if(
163a9 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
163aa 53 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 S_WRITE || (p->i
163ab 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 nTrans==TRANS_RE
163ac 41 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 AD && !wrflag) )
163ad 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 {. goto trans
163ae 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f _begun;. }.. /
163af 2a 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 * Write transact
163b0 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 ions are not pos
163b1 73 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d sible on a read-
163b2 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f only database */
163b3 0a 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 . if( pBt->read
163b4 4f 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29 Only && wrflag )
163b5 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
163b6 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 E_READONLY;.
163b7 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e goto trans_begun
163b8 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 ;. }..#ifndef S
163b9 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
163ba 44 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 20 D_CACHE. /* If
163bb 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 another database
163bc 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 handle has alre
163bd 61 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 ady opened a wri
163be 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a te transaction .
163bf 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 ** on this sha
163c0 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 red-btree struct
163c1 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 ure and a second
163c2 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
163c3 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 on is. ** reque
163c4 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c sted, return SQL
163c5 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f ITE_LOCKED.. */
163c6 0a 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 26 . if( (wrflag &
163c7 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 & pBt->inTransac
163c8 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
163c9 45 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 65 6e E) || pBt->isPen
163ca 64 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 6c 6f ding ){. pBlo
163cb 63 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65 ck = pBt->pWrite
163cc 72 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69 r->db;. }else i
163cd 66 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20 f( wrflag>1 ){.
163ce 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 BtLock *pIter
163cf 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d ;. for(pIter=
163d0 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 pBt->pLock; pIte
163d1 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e r; pIter=pIter->
163d2 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 pNext){. if
163d3 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 ( pIter->pBtree!
163d4 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 =p ){. pB
163d5 6c 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 lock = pIter->pB
163d6 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 tree->db;.
163d7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
163d8 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
163d9 20 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 pBlock ){. s
163da 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
163db 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 Blocked(p->db, p
163dc 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d Block);. rc =
163dd 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 SQLITE_LOCKED_S
163de 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 HAREDCACHE;.
163df 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e goto trans_begun
163e0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
163e1 2f 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 /* Any read-only
163e2 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 or read-write t
163e3 72 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69 ransaction impli
163e4 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f es a read-lock o
163e5 6e 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20 n . ** page 1.
163e6 53 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 So if some other
163e7 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c shared-cache cl
163e8 69 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73 ient already has
163e9 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 a write-lock .
163ea 20 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 ** on page 1, t
163eb 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 he transaction c
163ec 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e annot be opened.
163ed 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 */. rc = query
163ee 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
163ef 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 Lock(p, MASTER_R
163f0 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b OOT, READ_LOCK);
163f1 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
163f2 21 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e !=rc ) goto tran
163f3 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f 20 7b s_begun;.. do {
163f4 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 . /* Call loc
163f5 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 kBtree() until e
163f6 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 ither pBt->pPage
163f7 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 1 is populated o
163f8 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 r. ** lockBtr
163f9 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d ee() returns som
163fa 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 ething other tha
163fb 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 n SQLITE_OK. loc
163fc 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 kBtree(). **
163fd 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 may return SQLIT
163fe 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 E_OK but leave p
163ff 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 Bt->pPage1 set t
16400 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 o 0 if after.
16401 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 ** reading page
16402 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 1 it discovers
16403 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 that the page-si
16404 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
16405 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 se . ** file
16406 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 is not pBt->page
16407 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 Size. In this ca
16408 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 se lockBtree() w
16409 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a ill update. *
1640a 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 * pBt->pageSize
1640b 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 to the page-size
1640c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 of the file on
1640d 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 disk.. */.
1640e 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 while( pBt->pPa
1640f 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 ge1==0 && SQLITE
16410 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 _OK==(rc = lockB
16411 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 tree(pBt)) );..
16412 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
16413 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 E_OK && wrflag )
16414 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d {. if( pBt-
16415 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 >readOnly ){.
16416 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
16417 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 _READONLY;.
16418 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
16419 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1641a 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 rBegin(pBt->pPag
1641b 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 er,wrflag>1,sqli
1641c 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 te3TempInMemory(
1641d 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 p->db));.
1641e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1641f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
16420 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 rc = newDatabase
16421 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d (pBt);. }
16422 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
16423 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 . if( rc!=SQ
16424 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16425 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
16426 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d used(pBt);. }
16427 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 . }while( rc==S
16428 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 QLITE_BUSY && pB
16429 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1642a 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a ==TRANS_NONE &&.
1642b 20 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 btreeI
1642c 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
1642d 28 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 (pBt) );.. if(
1642e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1642f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 . if( p->inTr
16430 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ans==TRANS_NONE
16431 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 ){. pBt->nT
16432 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 ransaction++;.#i
16433 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16434 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
16435 20 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 if( p->shar
16436 61 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 74 28 able ){..assert(
16437 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d p->lock.pBtree=
16438 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 =p && p->lock.iT
16439 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 able==1 );.
1643a 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b p->lock.eLock
1643b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 = READ_LOCK;.
1643c 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e p->lock.pN
1643d 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b ext = pBt->pLock
1643e 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 ;. pBt->p
1643f 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b Lock = &p->lock;
16440 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
16441 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 }. p->inT
16442 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 rans = (wrflag?T
16443 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 RANS_WRITE:TRANS
16444 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 _READ);. if(
16445 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e p->inTrans>pBt->
16446 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b inTransaction ){
16447 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 . pBt->inTr
16448 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 ansaction = p->i
16449 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 nTrans;. }.#i
1644a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1644b 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
1644c 20 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b if( wrflag ){
1644d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
1644e 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a pBt->pWriter );.
1644f 20 20 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 pBt->pWrit
16450 65 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 er = p;. pB
16451 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d t->isExclusive =
16452 20 28 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b (u8)(wrflag>1);
16453 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
16454 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a }...trans_begun:
16455 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16456 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 E_OK && wrflag )
16457 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 {. /* This ca
16458 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 ll makes sure th
16459 61 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73 at the pager has
1645a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d the correct num
1645b 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 ber of. ** op
1645c 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 en savepoints. I
1645d 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 f the second par
1645e 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65 ameter is greate
1645f 72 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 r than 0 and.
16460 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 ** the sub-jour
16461 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 nal is not alrea
16462 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 dy open, then it
16463 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 will be opened
16464 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 here.. */.
16465 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
16466 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 erOpenSavepoint(
16467 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e pBt->pPager, p->
16468 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b db->nSavepoint);
16469 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 . }.. btreeInt
1646a 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c egrity(p);. sql
1646b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1646c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1646d 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1646e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1646f 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 M../*.** Set the
16470 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
16471 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 ries for all chi
16472 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 ldren of page pP
16473 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a age. Also, if.**
16474 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 pPage contains
16475 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 cells that point
16476 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
16477 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e es, set the poin
16478 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 ter.** map entri
16479 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 es for the overf
1647a 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c low pages as wel
1647b 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
1647c 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 setChildPtrmaps
1647d 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 (MemPage *pPage)
1647e 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
1647f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16480 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 /* Counte
16481 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 r variable */.
16482 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 int nCell;
16483 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16484 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
16485 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 cells in page pP
16486 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b age */. int rc;
16487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16488 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
16489 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
1648a 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
1648b 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 pPage->pBt;. u8
1648c 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 isInitOrig = pP
1648d 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 age->isInit;. P
1648e 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 gno pgno = pPage
1648f 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 ->pgno;.. asser
16490 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
16491 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
16492 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 ->mutex) );. rc
16493 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 = btreeInitPage
16494 28 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 (pPage);. if( r
16495 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16496 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 goto set_chi
16497 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a ld_ptrmaps_out;.
16498 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 }. nCell = pP
16499 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 age->nCell;.. f
1649a 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
1649b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 i++){. u8 *p
1649c 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
1649d 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 pPage, i);..
1649e 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 ptrmapPutOvflPtr
1649f 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 (pPage, pCell, &
164a0 72 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 rc);.. if( !p
164a1 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
164a2 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 Pgno childPg
164a3 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 no = get4byte(pC
164a4 65 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d ell);. ptrm
164a5 61 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 apPut(pBt, child
164a6 50 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 Pgno, PTRMAP_BTR
164a7 45 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a EE, pgno, &rc);.
164a8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
164a9 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
164aa 0a 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 . Pgno childP
164ab 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 gno = get4byte(&
164ac 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
164ad 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
164ae 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 );. ptrmapPut
164af 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c (pBt, childPgno,
164b0 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 PTRMAP_BTREE, p
164b1 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a gno, &rc);. }..
164b2 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 set_child_ptrmap
164b3 73 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e s_out:. pPage->
164b4 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f isInit = isInitO
164b5 72 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 rig;. return rc
164b6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 ;.}../*.** Somew
164b7 68 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73 here on pPage is
164b8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 a pointer to pa
164b9 67 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 ge iFrom. Modif
164ba 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 y this pointer s
164bb 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 o.** that it poi
164bc 6e 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 nts to iTo. Para
164bd 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 meter eType desc
164be 72 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f ribes the type o
164bf 66 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 f pointer to.**
164c0 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 be modified, as
164c1 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
164c2 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 PTRMAP_BTREE:
164c3 20 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 pPage is a btr
164c4 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 ee-page. The poi
164c5 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 nter points at a
164c6 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 child .**
164c7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
164c8 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a e of pPage..**.*
164c9 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f * PTRMAP_OVERFLO
164ca 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 W1: pPage is a b
164cb 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 tree-page. The p
164cc 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 ointer points at
164cd 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 an overflow.**
164ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
164cf 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 page pointed t
164d0 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 o by one of the
164d1 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a cells on pPage..
164d2 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 **.** PTRMAP_OVE
164d3 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 RFLOW2: pPage is
164d4 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 an overflow-pag
164d5 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 e. The pointer p
164d6 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 oints at the nex
164d7 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
164d8 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 overflow
164d9 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 page in the list
164da 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
164db 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 modifyPagePointe
164dc 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 r(MemPage *pPage
164dd 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 , Pgno iFrom, Pg
164de 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 no iTo, u8 eType
164df 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
164e0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
164e1 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
164e2 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
164e3 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
164e4 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
164e5 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 DbPage) );. if(
164e6 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
164e7 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 VERFLOW2 ){.
164e8 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 /* The pointer i
164e9 73 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 s always the fir
164ea 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 st 4 bytes of th
164eb 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 e page in this c
164ec 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 ase. */. if(
164ed 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d get4byte(pPage-
164ee 3e 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 >aData)!=iFrom )
164ef 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
164f0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
164f1 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 PT;. }. pu
164f2 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 t4byte(pPage->aD
164f3 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c ata, iTo);. }el
164f4 73 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 se{. u8 isIni
164f5 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 tOrig = pPage->i
164f6 73 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 sInit;. int i
164f7 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b ;. int nCell;
164f8 0a 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 .. btreeInitP
164f9 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
164fa 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e nCell = pPage->n
164fb 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 Cell;.. for(i
164fc 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b =0; i<nCell; i++
164fd 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 ){. u8 *pCe
164fe 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
164ff 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 age, i);. i
16500 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
16501 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 _OVERFLOW1 ){.
16502 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 CellInfo i
16503 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72 nfo;. btr
16504 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
16505 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
16506 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 fo);. if(
16507 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 info.iOverflow
16508 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
16509 20 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 iFrom==get4byte
1650a 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
1650b 65 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 erflow]) ){.
1650c 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 put4byte
1650d 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 (&pCell[info.iOv
1650e 65 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 erflow], iTo);.
1650f 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
16510 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
16511 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
16512 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 lse{. if(
16513 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 get4byte(pCell)
16514 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 ==iFrom ){.
16515 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 put4byte(pC
16516 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 ell, iTo);.
16517 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
16518 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
16519 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 }. . if( i
1651a 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 ==nCell ){.
1651b 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d if( eType!=PTRM
1651c 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 AP_BTREE || .
1651d 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 get4byte(
1651e 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
1651f 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
16520 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 ])!=iFrom ){.
16521 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
16522 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
16523 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
16524 75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e ut4byte(&pPage->
16525 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
16526 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b Offset+8], iTo);
16527 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 . }.. pPag
16528 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e e->isInit = isIn
16529 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 itOrig;. }. re
1652a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1652b 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 }.../*.** Move t
1652c 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 he open database
1652d 20 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f page pDbPage to
1652e 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 location iFreeP
1652f 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 age in the .** d
16530 61 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 atabase. The pDb
16531 50 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 Page reference r
16532 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a emains valid..**
16533 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 .** The isCommit
16534 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20 flag indicates
16535 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f that there is no
16536 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 need to remembe
16537 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f r that.** the jo
16538 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 urnal needs to b
16539 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 e sync()ed befor
1653a 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
1653b 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a pDbPage->pgno .*
1653c 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e * can be written
1653d 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 to. The caller
1653e 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d has already prom
1653f 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 ised not to writ
16540 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 e to that.** pag
16541 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
16542 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 relocatePage(.
16543 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
16544 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 /* Btr
16545 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ee */. MemPage
16546 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20 *pDbPage,
16547 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f /* Open page to
16548 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 move */. u8 eT
16549 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ype,
1654a 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d /* Pointer m
1654b 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20 ap 'type' entry
1654c 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 for pDbPage */.
1654d 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 Pgno iPtrPage,
1654e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1654f 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e nter map 'page-n
16550 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 o' entry for pDb
16551 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 Page */. Pgno i
16552 46 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20 FreePage,
16553 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 /* The locati
16554 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 on to move pDbPa
16555 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 ge to */. int i
16556 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 sCommit
16557 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 /* isCommit
16558 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73 flag passed to s
16559 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
1655a 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 age */.){. MemP
1655b 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20 age *pPtrPage;
1655c 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 /* The page tha
1655d 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 t contains a poi
1655e 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20 nter to pDbPage
1655f 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 */. Pgno iDbPag
16560 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e e = pDbPage->pgn
16561 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 o;. Pager *pPag
16562 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 er = pBt->pPager
16563 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 ;. int rc;.. a
16564 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54 ssert( eType==PT
16565 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c RMAP_OVERFLOW2 |
16566 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
16567 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 OVERFLOW1 || .
16568 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 eType==PTRMA
16569 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 P_BTREE || eType
1656a 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 ==PTRMAP_ROOTPAG
1656b 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 E );. assert( s
1656c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1656d 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
1656e 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61 . assert( pDbPa
1656f 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a ge->pBt==pBt );.
16570 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 . /* Move page
16571 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73 iDbPage from its
16572 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f current locatio
16573 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 n to page number
16574 20 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 iFreePage */.
16575 54 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55 TRACE(("AUTOVACU
16576 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f UM: Moving %d to
16577 20 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70 free page %d (p
16578 74 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20 tr page %d type
16579 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 %d)\n", . i
1657a 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 DbPage, iFreePag
1657b 65 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79 e, iPtrPage, eTy
1657c 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c pe));. rc = sql
1657d 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 ite3PagerMovepag
1657e 65 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67 e(pPager, pDbPag
1657f 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65 e->pDbPage, iFre
16580 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 ePage, isCommit)
16581 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
16582 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
16583 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 urn rc;. }. pD
16584 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 bPage->pgno = iF
16585 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 reePage;.. /* I
16586 66 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20 f pDbPage was a
16587 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e btree-page, then
16588 20 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69 it may have chi
16589 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 ld pages and/or
1658a 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 cells. ** that
1658b 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f point to overflo
1658c 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69 w pages. The poi
1658d 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 nter map entries
1658e 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 for all these.
1658f 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74 ** pages need t
16590 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 o be changed..
16591 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61 **. ** If pDbPa
16592 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f ge is an overflo
16593 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 w page, then the
16594 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d first 4 bytes m
16595 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 ay store a. **
16596 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62 pointer to a sub
16597 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 sequent overflow
16598 20 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69 page. If this i
16599 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e s the case, then
1659a 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 . ** the pointe
1659b 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62 r map needs to b
1659c 65 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68 e updated for th
1659d 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 e subsequent ove
1659e 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f rflow page.. */
1659f 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 . if( eType==PT
165a0 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 RMAP_BTREE || eT
165a1 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 ype==PTRMAP_ROOT
165a2 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d PAGE ){. rc =
165a3 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 setChildPtrmaps
165a4 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
165a5 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
165a6 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
165a7 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c rc;. }. }el
165a8 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 se{. Pgno nex
165a9 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 tOvfl = get4byte
165aa 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 (pDbPage->aData)
165ab 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 ;. if( nextOv
165ac 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 fl!=0 ){. p
165ad 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 trmapPut(pBt, ne
165ae 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f xtOvfl, PTRMAP_O
165af 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 VERFLOW2, iFreeP
165b0 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 age, &rc);.
165b1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
165b2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
165b3 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
165b4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
165b5 20 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73 Fix the databas
165b6 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 e pointer on pag
165b7 65 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20 e iPtrPage that
165b8 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61 pointed at iDbPa
165b9 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 ge so. ** that
165ba 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72 it points at iFr
165bb 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 eePage. Also fix
165bc 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
165bd 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 entry for. **
165be 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 iPtrPage.. */.
165bf 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d if( eType!=PTRM
165c0 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 AP_ROOTPAGE ){.
165c1 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
165c2 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 Page(pBt, iPtrPa
165c3 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 ge, &pPtrPage, 0
165c4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
165c5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
165c6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
165c7 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
165c8 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
165c9 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 trPage->pDbPage)
165ca 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
165cb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
165cc 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 releasePage(pPt
165cd 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 rPage);. re
165ce 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
165cf 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 rc = modifyPa
165d0 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 gePointer(pPtrPa
165d1 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 ge, iDbPage, iFr
165d2 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a eePage, eType);.
165d3 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
165d4 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 pPtrPage);. i
165d5 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
165d6 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ){. ptrmap
165d7 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 Put(pBt, iFreePa
165d8 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 ge, eType, iPtrP
165d9 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d age, &rc);. }
165da 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
165db 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 ;.}../* Forward
165dc 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 declaration requ
165dd 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 ired by incrVacu
165de 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 umStep(). */.sta
165df 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 tic int allocate
165e0 42 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 BtreePage(BtShar
165e1 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a ed *, MemPage **
165e2 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 , Pgno *, Pgno,
165e3 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 u8);../*.** Perf
165e4 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 orm a single ste
165e5 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e p of an incremen
165e6 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 tal-vacuum. If s
165e7 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 uccessful,.** re
165e8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 turn SQLITE_OK.
165e9 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 If there is no w
165ea 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 ork to do (and t
165eb 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 herefore no.** p
165ec 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 oint in calling
165ed 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 this function ag
165ee 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c ain), return SQL
165ef 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 ITE_DONE..**.**
165f0 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c More specificly,
165f1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
165f2 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 ttempts to re-or
165f3 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 ganize the .** d
165f4 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 atabase so that
165f5 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 the last page of
165f6 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e the file curren
165f7 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 tly in use.** is
165f8 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 no longer in us
165f9 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
165fa 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 nFin parameter i
165fb 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 s non-zero, this
165fc 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
165fd 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 s.** that the ca
165fe 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 ller will keep c
165ff 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 alling incrVacuu
16600 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a mStep() until.**
16601 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 it returns SQLI
16602 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 TE_DONE or an er
16603 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 ror, and that nF
16604 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d in is the.** num
16605 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 ber of pages the
16606 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
16607 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 ill contain afte
16608 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 r this .** proce
16609 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 ss is complete.
1660a 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f If nFin is zero
1660b 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 , it is assumed
1660c 74 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 that.** incrVacu
1660d 75 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65 umStep() will be
1660e 20 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 called a finite
1660f 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 amount of times
16610 0a 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72 .** which may or
16611 20 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 may not empty t
16612 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20 he freelist. A
16613 66 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a full autovacuum.
16614 2a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 ** has nFin>0.
16615 41 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65 6d A "PRAGMA increm
16616 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 ental_vacuum" ha
16617 73 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 s nFin==0..*/.st
16618 61 74 69 63 20 69 6e 74 20 69 6e 63 72 56 61 63 atic int incrVac
16619 75 75 6d 53 74 65 70 28 42 74 53 68 61 72 65 64 uumStep(BtShared
1661a 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e *pBt, Pgno nFin
1661b 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b , Pgno iLastPg){
1661c 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 . Pgno nFreeLis
1661d 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
1661e 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
1661f 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 still on the fre
16620 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 e-list */.. ass
16621 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
16622 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
16623 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
16624 20 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b iLastPg>nFin );
16625 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f .. if( !PTRMAP_
16626 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 ISPAGE(pBt, iLas
16627 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 tPg) && iLastPg!
16628 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
16629 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69 GE(pBt) ){. i
1662a 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54 nt rc;. u8 eT
1662b 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 ype;. Pgno iP
1662c 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 trPage;.. nFr
1662d 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 eeList = get4byt
1662e 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
1662f 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 aData[36]);.
16630 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 if( nFreeList==0
16631 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
16632 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
16633 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 }.. rc = pt
16634 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 rmapGet(pBt, iLa
16635 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 stPg, &eType, &i
16636 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 PtrPage);. if
16637 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
16638 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
16639 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rc;. }. if
1663a 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
1663b 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 ROOTPAGE ){.
1663c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1663d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1663e 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 }.. if( eTy
1663f 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 pe==PTRMAP_FREEP
16640 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 AGE ){. if(
16641 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 nFin==0 ){.
16642 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 /* Remove th
16643 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 e page from the
16644 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e files free-list.
16645 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 This is not req
16646 75 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a uired. **
16647 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d if nFin is non-
16648 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 zero. In that ca
16649 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 se, the free-lis
1664a 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 t will be.
1664b 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 ** truncated t
1664c 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 o zero after thi
1664d 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
1664e 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 ns, so it doesn'
1664f 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 t . ** ma
16650 74 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c tter if it still
16651 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 contains some g
16652 61 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a arbage entries..
16653 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
16654 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b Pgno iFreePg;
16655 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 . MemPage
16656 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 *pFreePg;.
16657 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
16658 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
16659 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 pFreePg, &iFreeP
1665a 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a g, iLastPg, 1);.
1665b 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1665c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1665d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1665e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1665f 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 assert( iFre
16660 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a ePg==iLastPg );.
16661 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
16662 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 age(pFreePg);.
16663 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 }. } else
16664 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 {. Pgno iF
16665 72 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 reePg;
16666 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 /* Index of f
16667 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 ree page to move
16668 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 pLastPg to */.
16669 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c MemPage *pL
1666a 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 astPg;.. rc
1666b 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
1666c 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 pBt, iLastPg, &p
1666d 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 LastPg, 0);.
1666e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1666f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
16670 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
16671 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e }.. /* If n
16672 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 Fin is zero, thi
16673 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 s loop runs exac
16674 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 tly once and pag
16675 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 e pLastPg.
16676 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 ** is swapped wi
16677 74 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 th the first fre
16678 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 e page pulled of
16679 66 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e f the free list.
1667a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1667b 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 ** On the other
1667c 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 hand, if nFin is
1667d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 greater than ze
1667e 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 ro, then keep.
1667f 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 ** looping u
16680 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 ntil a free-page
16681 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 located within
16682 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 the first nFin p
16683 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 ages. ** of
16684 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 the file is fou
16685 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 nd.. */.
16686 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 do {.
16687 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 MemPage *pFreePg
16688 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 ;. rc = a
16689 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
1668a 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 (pBt, &pFreePg,
1668b 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b &iFreePg, 0, 0);
1668c 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
1668d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1668e 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1668f 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 age(pLastPg);.
16690 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
16691 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
16692 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
16693 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 (pFreePg);.
16694 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 }while( nFin!=0
16695 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e && iFreePg>nFin
16696 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
16697 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 ( iFreePg<iLastP
16698 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 g );. .
16699 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1669a 67 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 gerWrite(pLastPg
1669b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1669c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1669d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
1669e 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 c = relocatePage
1669f 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 (pBt, pLastPg, e
166a0 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Type, iPtrPage,
166a1 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 iFreePg, nFin!=0
166a2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
166a3 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 releasePage(pLa
166a4 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 stPg);. if(
166a5 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
166a6 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
166a7 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
166a8 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 }. }.. if( nF
166a9 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 in==0 ){. iLa
166aa 73 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c stPg--;. whil
166ab 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 e( iLastPg==PEND
166ac 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
166ad 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 t)||PTRMAP_ISPAG
166ae 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 E(pBt, iLastPg)
166af 29 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 ){. if( PTR
166b0 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
166b1 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 iLastPg) ){.
166b2 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 MemPage *pPg
166b3 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 ;. int rc
166b4 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
166b5 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 pBt, iLastPg, &p
166b6 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 Pg, 0);.
166b7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
166b8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
166b9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
166ba 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d }. rc =
166bb 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
166bc 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 te(pPg->pDbPage)
166bd 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
166be 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 ePage(pPg);.
166bf 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
166c0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
166c1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
166c2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
166c3 20 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b iLastPg--;
166c4 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
166c5 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 e3PagerTruncateI
166c6 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 mage(pBt->pPager
166c7 2c 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 7d 0a , iLastPg);. }.
166c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
166c9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 OK;.}../*.** A w
166ca 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
166cb 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 must be opened
166cc 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 before calling t
166cd 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
166ce 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 It performs a s
166cf 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f ingle unit of wo
166d0 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e rk towards an in
166d1 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
166d2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 ..**.** If the i
166d3 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
166d4 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66 m is finished af
166d5 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f ter this functio
166d6 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 n has run,.** SQ
166d7 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 LITE_DONE is ret
166d8 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20 urned. If it is
166d9 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75 not finished, bu
166da 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 t no error occur
166db 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f red,.** SQLITE_O
166dc 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f K is returned. O
166dd 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69 therwise an SQLi
166de 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a te error code. .
166df 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
166e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
166e1 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 eeIncrVacuum(Btr
166e2 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 ee *p){. int rc
166e3 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
166e4 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 t = p->pBt;.. s
166e5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
166e6 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
166e7 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
166e8 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 n==TRANS_WRITE &
166e9 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 & p->inTrans==TR
166ea 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 ANS_WRITE );. i
166eb 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 f( !pBt->autoVac
166ec 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 uum ){. rc =
166ed 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
166ee 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 else{. invali
166ef 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 dateAllOverflowC
166f0 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 ache(pBt);. r
166f1 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 c = incrVacuumSt
166f2 65 70 28 70 42 74 2c 20 30 2c 20 70 61 67 65 72 ep(pBt, 0, pager
166f3 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b Pagecount(pBt));
166f4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
166f5 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
166f6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
166f7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
166f8 69 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 is called prior
166f9 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 to sqlite3PagerC
166fa 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 ommit when a tra
166fb 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 nsaction.** is c
166fc 6f 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 ommited for an a
166fd 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 uto-vacuum datab
166fe 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 ase..**.** If SQ
166ff 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
16700 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 ned, then *pnTru
16701 6e 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 nc is set to the
16702 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
16703 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
16704 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 file should be
16705 74 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 truncated to dur
16706 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 ing the commit p
16707 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e rocess. .** i.e.
16708 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
16709 73 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a s been reorganiz
1670a 65 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 ed so that only
1670b 74 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 the first *pnTru
1670c 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 nc.** pages are
1670d 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 in use..*/.stati
1670e 63 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d c int autoVacuum
1670f 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 Commit(BtShared
16710 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 *pBt){. int rc
16711 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 = SQLITE_OK;. P
16712 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
16713 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 Bt->pPager;. VV
16714 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 A_ONLY( int nRef
16715 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 = sqlite3PagerR
16716 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 efcount(pPager)
16717 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 );.. assert( sq
16718 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
16719 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
1671a 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f invalidateAllO
1671b 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 verflowCache(pBt
1671c 29 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d );. assert(pBt-
1671d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 >autoVacuum);.
1671e 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 if( !pBt->incrVa
1671f 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f cuum ){. Pgno
16720 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f nFin; /
16721 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
16722 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 s in database af
16723 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e ter autovacuumin
16724 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 g */. Pgno nF
16725 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e ree; /* N
16726 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
16727 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 n the freelist i
16728 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 nitially */.
16729 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20 Pgno nPtrmap;
1672a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1672b 50 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f 20 PtrMap pages to
1672c 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 be freed */.
1672d 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 Pgno iFree;
1672e 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 /* The next p
1672f 61 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 age to be freed
16730 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 */. int nEntr
16731 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d y; /* Num
16732 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f ber of entries o
16733 6e 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67 n one ptrmap pag
16734 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f e */. Pgno nO
16735 72 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 rig; /* D
16736 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 atabase size bef
16737 6f 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a ore freeing */..
16738 20 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 nOrig = page
16739 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b rPagecount(pBt);
1673a 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f . if( PTRMAP_
1673b 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 ISPAGE(pBt, nOri
1673c 67 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e g) || nOrig==PEN
1673d 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1673e 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 Bt) ){. /*
1673f 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 It is not possib
16740 6c 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 le to create a d
16741 61 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 atabase for whic
16742 68 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 h the final page
16743 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 . ** is eit
16744 68 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 her a pointer-ma
16745 70 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 p page or the pe
16746 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e nding-byte page.
16747 20 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a If one. **
16748 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
16749 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 this indicates
1674a 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 corruption..
1674b 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 */. retur
1674c 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1674d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 _BKPT;. }..
1674e 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 nFree = get4by
1674f 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
16750 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 >aData[36]);.
16751 20 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75 nEntry = pBt->u
16752 73 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 20 sableSize/5;.
16753 20 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 nPtrmap = (nFre
16754 65 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 e-nOrig+PTRMAP_P
16755 41 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 AGENO(pBt, nOrig
16756 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 72 79 )+nEntry)/nEntry
16757 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 ;. nFin = nOr
16758 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 ig - nFree - nPt
16759 72 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f rmap;. if( nO
1675a 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 rig>PENDING_BYTE
1675b 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 _PAGE(pBt) && nF
1675c 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f in<PENDING_BYTE_
1675d 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
1675e 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d nFin--;. }
1675f 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d . while( PTRM
16760 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e AP_ISPAGE(pBt, n
16761 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 Fin) || nFin==PE
16762 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
16763 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 pBt) ){. nF
16764 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 in--;. }.
16765 69 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 if( nFin>nOrig )
16766 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
16767 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 ORRUPT_BKPT;..
16768 20 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 for(iFree=nOri
16769 67 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 g; iFree>nFin &&
1676a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc==SQLITE_OK;
1676b 69 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 iFree--){.
1676c 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 rc = incrVacuumS
1676d 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 tep(pBt, nFin, i
1676e 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Free);. }.
1676f 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 if( (rc==SQLITE
16770 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c _DONE || rc==SQL
16771 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 ITE_OK) && nFree
16772 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d >0 ){. rc =
16773 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
16774 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16775 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 gerWrite(pBt->pP
16776 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a age1->pDbPage);.
16777 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
16778 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
16779 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 ta[32], 0);.
1677a 20 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d put4byte(&pBt-
1677b 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
1677c 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6], 0);. sq
1677d 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
1677e 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 teImage(pBt->pPa
1677f 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 ger, nFin);.
16780 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 }. if( rc!=SQ
16781 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16782 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
16783 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 lback(pPager);.
16784 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 }. }.. asse
16785 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 rt( nRef==sqlite
16786 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
16787 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 Pager) );. retu
16788 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 65 20 rn rc;.}..#else
16789 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /* ifndef SQLITE
1678a 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1678b 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 65 74 */.# define set
1678c 43 68 69 6c 64 50 74 72 6d 61 70 73 28 78 29 20 ChildPtrmaps(x)
1678d 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 SQLITE_OK.#endif
1678e 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
1678f 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 tine does the fi
16790 72 73 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 rst phase of a t
16791 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e wo-phase commit.
16792 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a This routine.*
16793 2a 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 * causes a rollb
16794 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 ack journal to b
16795 65 20 63 72 65 61 74 65 64 20 28 69 66 20 69 74 e created (if it
16796 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 does not alread
16797 79 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 y exist).** and
16798 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 populated with e
16799 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f nough informatio
1679a 6e 20 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 n so that if a p
1679b 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 ower loss occurs
1679c 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1679d 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 can be restored
1679e 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c to its original
1679f 20 73 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e state by playin
167a0 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f g back.** the jo
167a1 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 urnal. Then the
167a2 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
167a3 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 journal are flu
167a4 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 shed out to.** t
167a5 68 65 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 he disk. After
167a6 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 the journal is s
167a7 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 afely on oxide,
167a8 74 68 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 the changes to t
167a9 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 he.** database a
167aa 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 re written into
167ab 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
167ac 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f e and flushed to
167ad 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 oxide..** At th
167ae 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 e end of this ca
167af 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b ll, the rollback
167b0 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 journal still e
167b1 78 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 xists on the.**
167b2 64 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 disk and we are
167b3 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c still holding al
167b4 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 l locks, so the
167b5 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 transaction has
167b6 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 not.** committed
167b7 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 . See sqlite3Bt
167b8 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 reeCommitPhaseTw
167b9 6f 28 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f o() for the seco
167ba 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a nd phase of the.
167bb 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 ** commit proces
167bc 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 s..**.** This ca
167bd 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 ll is a no-op if
167be 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 no write-transa
167bf 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 ction is current
167c0 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 ly active on pBt
167c1 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ..**.** Otherwis
167c2 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 e, sync the data
167c3 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 base file for th
167c4 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 e btree pBt. zMa
167c5 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a ster points to.*
167c6 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 * the name of a
167c7 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
167c8 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ile that should
167c9 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 be written into
167ca 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 the.** individua
167cb 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 l journal file,
167cc 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 or is NULL, indi
167cd 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 cating no master
167ce 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a journal file .*
167cf 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 * (single databa
167d0 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e se transaction).
167d1 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 .**.** When this
167d2 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 is called, the
167d3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 master journal s
167d4 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 hould already ha
167d5 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 ve been.** creat
167d6 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 ed, populated wi
167d7 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 th this journal
167d8 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 pointer and sync
167d9 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a ed to disk..**.*
167da 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 * Once this is r
167db 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 outine has retur
167dc 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 ned, the only th
167dd 69 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 ing required to
167de 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 commit.** the wr
167df 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
167e0 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 for this databas
167e1 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c e file is to del
167e2 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e ete the journal.
167e3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
167e4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
167e5 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e reeCommitPhaseOn
167e6 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 e(Btree *p, cons
167e7 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 t char *zMaster)
167e8 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
167e9 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d ITE_OK;. if( p-
167ea 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
167eb 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 WRITE ){. BtS
167ec 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
167ed 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 pBt;. sqlite3
167ee 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 BtreeEnter(p);.#
167ef 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
167f0 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
167f1 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
167f2 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 acuum ){. r
167f3 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f c = autoVacuumCo
167f4 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 mmit(pBt);.
167f5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
167f6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 OK ){. sq
167f7 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
167f8 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
167f9 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
167fa 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
167fb 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
167fc 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
167fd 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 pBt->pPager, zMa
167fe 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 ster, 0);. sq
167ff 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
16800 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p);. }. return
16801 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
16802 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
16803 61 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20 alled from both
16804 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
16805 54 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52 Two() and BtreeR
16806 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20 ollback().** at
16807 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f the conclusion o
16808 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e f a transaction.
16809 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1680a 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 btreeEndTransact
1680b 69 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ion(Btree *p){.
1680c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
1680d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
1680e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
1680f 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
16810 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 . btreeClearHas
16811 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 Content(pBt);.
16812 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 if( p->inTrans>T
16813 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 2d 3e RANS_NONE && p->
16814 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
16815 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 t>1 ){. /* If
16816 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 there are other
16817 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e active statemen
16818 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 ts that belong t
16819 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 0a o this database.
1681a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c 20 64 ** handle, d
1681b 6f 77 6e 67 72 61 64 65 20 74 6f 20 61 20 72 65 owngrade to a re
1681c 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 ad-only transact
1681d 69 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 20 73 ion. The other s
1681e 74 61 74 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a tatements. **
1681f 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 may still be re
16820 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 64 ading from the d
16821 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 20 atabase. */.
16822 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 downgradeAllSha
16823 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
16824 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e ks(p);. p->in
16825 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45 Trans = TRANS_RE
16826 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 AD;. }else{.
16827 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c /* If the handl
16828 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f e had any kind o
16829 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 f transaction op
1682a 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 en, decrement th
1682b 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 e . ** transa
1682c 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74 ction count of t
1682d 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e he shared btree.
1682e 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 If the transact
1682f 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a ion count . *
16830 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74 * reaches 0, set
16831 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 the shared stat
16832 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e e to TRANS_NONE.
16833 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65 The unlockBtree
16834 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a IfUnused(). *
16835 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c * call below wil
16836 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 l unlock the pag
16837 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 er. */. if(
16838 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e p->inTrans!=TRAN
16839 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 S_NONE ){.
1683a 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 clearAllSharedCa
1683b 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 cheTableLocks(p)
1683c 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 ;. pBt->nTr
1683d 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 ansaction--;.
1683e 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e if( 0==pBt->n
1683f 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 Transaction ){.
16840 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 pBt->inTr
16841 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
16842 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a S_NONE;. }.
16843 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 }.. /* Se
16844 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 t the current tr
16845 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 ansaction state
16846 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e to TRANS_NONE an
16847 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20 d unlock the .
16848 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68 ** pager if th
16849 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 is call closed t
1684a 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 he only read or
1684b 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
1684c 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e n. */. p->in
1684d 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
1684e 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 NE;. unlockBt
1684f 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
16850 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e ;. }.. btreeIn
16851 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f tegrity(p);.}../
16852 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 *.** Commit the
16853 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 transaction curr
16854 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 ently in progres
16855 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
16856 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 utine implements
16857 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 the second phas
16858 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 e of a 2-phase c
16859 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73 ommit. The.** s
1685a 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1685b 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 tPhaseOne() rout
1685c 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
1685d 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f st phase and sho
1685e 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 uld.** be invoke
1685f 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 d prior to calli
16860 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
16861 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 The sqlite3Btr
16862 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
16863 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69 ().** routine di
16864 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f d all the work o
16865 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d f writing inform
16866 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 ation out to dis
16867 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74 k and flushing t
16868 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73 he.** contents s
16869 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 o that they are
1686a 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65 written onto the
1686b 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20 disk platter.
1686c 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 All this.** rout
1686d 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73 ine has to do is
1686e 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63 delete or trunc
1686f 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 ate or zero the
16870 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a header in the.**
16871 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
16872 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75 urnal (which cau
16873 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 ses the transact
16874 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61 ion to commit) a
16875 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 nd.** drop locks
16876 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c ..**.** This wil
16877 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 l release the wr
16878 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite lock on the
16879 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
1687a 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 If there.** are
1687b 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 no active cursor
1687c 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 s, it also relea
1687d 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ses the read loc
1687e 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 k..*/.SQLITE_PRI
1687f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16880 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
16881 54 77 6f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 Two(Btree *p){.
16882 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
16883 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 p->pBt;.. sqli
16884 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
16885 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 ;. btreeIntegri
16886 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ty(p);.. /* If
16887 74 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 the handle has a
16888 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
16889 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 on open, commit
1688a 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
1688b 73 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 s . ** transact
1688c 69 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 ion and set the
1688d 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 shared state to
1688e 54 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f TRANS_READ.. */
1688f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
16890 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
16891 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
16892 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
16893 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
16894 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 ANS_WRITE );.
16895 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 assert( pBt->nT
16896 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a ransaction>0 );.
16897 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16898 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
16899 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29 Two(pBt->pPager)
1689a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1689b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1689c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1689d 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 ve(p);. ret
1689e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1689f 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
168a0 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 tion = TRANS_REA
168a1 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 D;. }.. btreeE
168a2 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 ndTransaction(p)
168a3 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
168a4 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
168a5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
168a6 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 ./*.** Do both p
168a7 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 hases of a commi
168a8 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
168a9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
168aa 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 BtreeCommit(Btre
168ab 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
168ac 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
168ad 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
168ae 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
168af 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 itPhaseOne(p, 0)
168b0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
168b1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
168b2 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
168b3 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b mmitPhaseTwo(p);
168b4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
168b5 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
168b6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 eturn rc;.}..#if
168b7 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
168b8 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
168b9 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72 ber of write-cur
168ba 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 sors open on thi
168bb 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69 s handle. This i
168bc 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20 s for use.** in
168bd 61 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73 assert() express
168be 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f ions, so it is o
168bf 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 nly compiled if
168c0 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a NDEBUG is not.**
168c1 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
168c2 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
168c3 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
168c4 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 , a write-cursor
168c5 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74 is any cursor t
168c6 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c hat.** is capabl
168c7 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20 e of writing to
168c8 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68 the databse. Th
168c9 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72 at means the cur
168ca 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 sor was.** origi
168cb 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 nally opened for
168cc 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65 writing and the
168cd 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20 cursor has not
168ce 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62 be disabled.** b
168cf 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61 y having its sta
168d0 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55 te changed to CU
168d1 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 RSOR_FAULT..*/.s
168d2 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57 tatic int countW
168d3 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68 riteCursors(BtSh
168d4 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 ared *pBt){. Bt
168d5 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
168d6 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72 int r = 0;. for
168d7 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73 (pCur=pBt->pCurs
168d8 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70 or; pCur; pCur=p
168d9 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Cur->pNext){.
168da 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 if( pCur->wrFla
168db 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 g && pCur->eStat
168dc 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e!=CURSOR_FAULT
168dd 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 ) r++; . }. re
168de 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 turn r;.}.#endif
168df 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
168e0 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 tine sets the st
168e1 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 ate to CURSOR_FA
168e2 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f ULT and the erro
168e3 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 r.** code to err
168e4 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 Code for every c
168e5 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 ursor on BtShare
168e6 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a d that pBtree.**
168e7 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a references..**.
168e8 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 ** Every cursor
168e9 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c is tripped, incl
168ea 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 uding cursors th
168eb 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 at belong.** to
168ec 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
168ed 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 onnections that
168ee 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 happen to be sha
168ef 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 ring.** the cach
168f0 65 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a e with pBtree..*
168f1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
168f2 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 e gets called wh
168f3 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 en a rollback oc
168f4 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 curs..** All cur
168f5 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 sors using the s
168f6 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 ame cache must b
168f7 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 e tripped.** to
168f8 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f prevent them fro
168f9 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 m trying to use
168fa 74 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a the btree after.
168fb 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e ** the rollback.
168fc 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d The rollback m
168fd 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 ay have deleted
168fe 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 tables.** or mov
168ff 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 ed root pages, s
16900 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 o it is not suff
16901 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 icient to.** sav
16902 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
16903 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 he cursor. The
16904 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a cursor must be.*
16905 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a * invalidated..*
16906 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16907 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
16908 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 eeTripAllCursors
16909 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 (Btree *pBtree,
1690a 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 int errCode){.
1690b 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73 BtCursor *p;. s
1690c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1690d 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28 (pBtree);. for(
1690e 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 p=pBtree->pBt->p
1690f 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e Cursor; p; p=p->
16910 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 pNext){. int
16911 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 i;. sqlite3Bt
16912 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 reeClearCursor(p
16913 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65 );. p->eState
16914 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b = CURSOR_FAULT;
16915 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 74 . p->skipNext
16916 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 = errCode;.
16917 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 for(i=0; i<=p->i
16918 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Page; i++){.
16919 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d releasePage(p-
1691a 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 >apPage[i]);.
1691b 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20 p->apPage[i]
1691c 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
1691d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1691e 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f ve(pBtree);.}../
1691f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 *.** Rollback th
16920 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e e transaction in
16921 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20 progress. All
16922 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a cursors will be.
16923 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20 ** invalided by
16924 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 this operation.
16925 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
16926 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 use a cursor.**
16927 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 that was open at
16928 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
16929 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e f this operation
1692a 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 will result.**
1692b 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a in an error..**.
1692c 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c ** This will rel
1692d 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c ease the write l
1692e 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
1692f 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 ase file. If th
16930 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 ere.** are no ac
16931 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 tive cursors, it
16932 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 also releases t
16933 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f he read lock..*/
16934 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16935 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16936 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a Rollback(Btree *
16937 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 p){. int rc;.
16938 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
16939 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 p->pBt;. MemPag
1693a 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 e *pPage1;.. sq
1693b 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1693c 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 p);. rc = saveA
1693d 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 llCursors(pBt, 0
1693e 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 , 0);.#ifndef SQ
1693f 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
16940 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 _CACHE. if( rc!
16941 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16942 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 /* This is a h
16943 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f orrible situatio
16944 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c n. An IO or mall
16945 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 oc() error occur
16946 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a red whilst. *
16947 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 * trying to save
16948 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e cursor position
16949 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e s. If this is an
1694a 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 automatic rollb
1694b 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 ack (as. ** t
1694c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 he result of a c
1694d 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f onstraint, mallo
1694e 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 c() failure or I
1694f 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 O error) then .
16950 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 ** the cache
16951 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c may be internall
16952 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 y inconsistent (
16953 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 not contain vali
16954 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 d trees) so.
16955 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d ** we cannot sim
16956 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 ply return the e
16957 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c rror to the call
16958 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f er. Instead, abo
16959 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 rt . ** all q
1695a 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 ueries that may
1695b 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 be using any of
1695c 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 the cursors that
1695d 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e failed to save.
1695e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
1695f 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 te3BtreeTripAllC
16960 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 ursors(p, rc);.
16961 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 }.#endif. btre
16962 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a eIntegrity(p);..
16963 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
16964 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b ==TRANS_WRITE ){
16965 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 . int rc2;..
16966 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 assert( TRANS
16967 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 _WRITE==pBt->inT
16968 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 ransaction );.
16969 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 rc2 = sqlite3P
1696a 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 agerRollback(pBt
1696b 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
1696c 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f f( rc2!=SQLITE_O
1696d 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
1696e 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc2;. }..
1696f 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 /* The rollback
16970 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 may have destroy
16971 65 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 ed the pPage1->a
16972 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a Data value. So.
16973 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 72 65 ** call btre
16974 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61 eGetPage() on pa
16975 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61 ge 1 again to ma
16976 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70 ke. ** sure p
16977 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20 Page1->aData is
16978 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a set correctly. *
16979 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 65 47 /. if( btreeG
1697a 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 etPage(pBt, 1, &
1697b 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 pPage1, 0)==SQLI
1697c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
1697d 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
1697e 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 1);. }. as
1697f 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 sert( countWrite
16980 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 Cursors(pBt)==0
16981 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 );. pBt->inTr
16982 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
16983 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 S_READ;. }.. b
16984 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 treeEndTransacti
16985 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 on(p);. sqlite3
16986 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
16987 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
16988 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 *.** Start a sta
16989 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
1698a 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 ction. The subtr
1698b 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61 ansaction can ca
1698c 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 n be rolled.** b
1698d 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c ack independentl
1698e 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 y of the main tr
1698f 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d ansaction. You m
16990 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e ust start a tran
16991 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f saction .** befo
16992 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 re starting a su
16993 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 btransaction. Th
16994 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e e subtransaction
16995 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 is ended automa
16996 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 tically .** if t
16997 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 he main transact
16998 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 ion commits or r
16999 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a olls back..**.**
1699a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 Statement subtr
1699b 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 ansactions are u
1699c 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 sed around indiv
1699d 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d idual SQL statem
1699e 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 ents.** that are
1699f 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
169a0 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d n a BEGIN...COMM
169a1 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 IT block. If a
169a2 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 constraint.** er
169a3 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 ror occurs withi
169a4 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c n the statement,
169a5 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 the effect of t
169a6 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e hat one statemen
169a7 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c t.** can be roll
169a8 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 ed back without
169a9 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 having to rollba
169aa 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 ck the entire tr
169ab 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
169ac 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 A statement sub
169ad 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 -transaction is
169ae 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 implemented as a
169af 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 n anonymous save
169b0 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 point. The.** va
169b1 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 lue passed as th
169b2 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
169b3 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 er is the total
169b4 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f number of savepo
169b5 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 ints,.** includi
169b6 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 ng the new anony
169b7 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 mous savepoint,
169b8 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 open on the B-Tr
169b9 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 ee. i.e. if ther
169ba 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 e.** are no acti
169bb 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e ve savepoints an
169bc 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 d no other state
169bd 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e ment-transaction
169be 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 s open,.** iStat
169bf 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 ement is 1. This
169c0 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 anonymous savep
169c1 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 oint can be rele
169c2 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 ased or rolled b
169c3 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ack.** using the
169c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 sqlite3BtreeSav
169c5 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f epoint() functio
169c6 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
169c7 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
169c8 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 BtreeBeginStmt(B
169c9 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 tree *p, int iSt
169ca 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 atement){. int
169cb 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a rc;. BtShared *
169cc 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
169cd 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
169ce 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
169cf 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
169d0 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 S_WRITE );. ass
169d1 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e ert( pBt->readOn
169d2 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ly==0 );. asser
169d3 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20 t( iStatement>0
169d4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 );. assert( iSt
169d5 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e atement>p->db->n
169d6 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69 Savepoint );. i
169d7 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72 f( NEVER(p->inTr
169d8 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans!=TRANS_WRITE
169d9 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c || pBt->readOnl
169da 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 y) ){. rc = S
169db 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a QLITE_INTERNAL;.
169dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
169dd 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
169de 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
169df 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41 RITE );. /* A
169e0 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 t the pager leve
169e1 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 l, a statement t
169e2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 ransaction is a
169e3 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 savepoint with.
169e4 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 ** an index g
169e5 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 reater than all
169e6 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 savepoints creat
169e7 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 ed explicitly us
169e8 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73 ing. ** SQL s
169e9 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 tatements. It is
169ea 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e illegal to open
169eb 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c , release or rol
169ec 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a lback any. **
169ed 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 such savepoints
169ee 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 while the state
169ef 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
169f0 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 savepoint is ac
169f1 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tive.. */.
169f2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
169f3 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 erOpenSavepoint(
169f4 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74 pBt->pPager, iSt
169f5 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 atement);. }.
169f6 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
169f7 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
169f8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
169f9 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
169fa 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
169fb 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 20 , op, is always
169fc 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
169fd 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49 CK.** or SAVEPOI
169fe 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 73 NT_RELEASE. This
169ff 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 72 function either
16a00 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c releases or rol
16a01 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73 ls back the.** s
16a02 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 66 avepoint identif
16a03 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
16a04 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 70 iSavepoint, dep
16a05 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 ending on the va
16a06 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a lue .** of op..*
16a07 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 *.** Normally, i
16a08 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 Savepoint is gre
16a09 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
16a0a 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 al to zero. Howe
16a0b 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a ver, if op is.**
16a0c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
16a0d 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 70 ACK, then iSavep
16a0e 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 65 oint may also be
16a0f 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 73 -1. In this cas
16a10 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e e the .** conten
16a11 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 ts of the entire
16a12 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 transaction are
16a13 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 rolled back. Th
16a14 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a is is different.
16a15 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c ** from a normal
16a16 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c transaction rol
16a17 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 lback, as no loc
16a18 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 ks are released
16a19 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 and the.** trans
16a1a 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f action remains o
16a1b 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pen..*/.SQLITE_P
16a1c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16a1d 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 e3BtreeSavepoint
16a1e 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f (Btree *p, int o
16a1f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e p, int iSavepoin
16a20 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
16a21 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
16a22 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d p && p->inTrans=
16a23 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a =TRANS_WRITE ){.
16a24 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 BtShared *pB
16a25 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 t = p->pBt;.
16a26 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 assert( op==SAVE
16a27 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c POINT_RELEASE ||
16a28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
16a29 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 20 61 OLLBACK );. a
16a2a 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e ssert( iSavepoin
16a2b 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 70 6f t>=0 || (iSavepo
16a2c 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 int==-1 && op==S
16a2d 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
16a2e 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 K) );. sqlite
16a2f 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
16a30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16a31 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70 PagerSavepoint(p
16a32 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20 Bt->pPager, op,
16a33 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 iSavepoint);.
16a34 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16a35 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
16a36 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 newDatabase(pBt
16a37 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
16a38 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
16a39 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
16a3a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 rc;.}../*.** Cre
16a3b 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 ate a new cursor
16a3c 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 for the BTree w
16a3d 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 hose root is on
16a3e 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 the page.** iTab
16a3f 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e le. If a read-on
16a40 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 ly cursor is req
16a41 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73 uested, it is as
16a42 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 sumed that.** th
16a43 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79 e caller already
16a44 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20 has at least a
16a45 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 read-only transa
16a46 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e ction open.** on
16a47 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c the database al
16a48 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74 ready. If a writ
16a49 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 e-cursor is requ
16a4a 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 ested, then.** t
16a4b 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73 he caller is ass
16a4c 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20 umed to have an
16a4d 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
16a4e 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 action..**.** If
16a4f 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e wrFlag==0, then
16a50 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 the cursor can
16a51 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 only be used for
16a52 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 reading..** If
16a53 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 wrFlag==1, then
16a54 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 the cursor can b
16a55 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 e used for readi
16a56 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 ng or for.** wri
16a57 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f ting if other co
16a58 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 nditions for wri
16a59 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 ting are also me
16a5a 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 t. These.** are
16a5b 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 the conditions
16a5c 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 that must be met
16a5d 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 in order for wr
16a5e 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 iting to.** be a
16a5f 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a llowed:.**.** 1:
16a60 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 The cursor mus
16a61 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e t have been open
16a62 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d ed with wrFlag==
16a63 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 1.**.** 2: Othe
16a64 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
16a65 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 ctions that shar
16a66 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 e the same pager
16a67 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 cache.** bu
16a68 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 t which are not
16a69 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f in the READ_UNCO
16a6a 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 MMITTED state ma
16a6b 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 y not have.**
16a6c 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 cursors open w
16a6d 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e ith wrFlag==0 on
16a6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e the same table.
16a6f 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 Otherwise.**
16a70 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d the changes m
16a71 61 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74 ade by this writ
16a72 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 e cursor would b
16a73 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 e visible to.**
16a74 20 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72 the read cur
16a75 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 sors in the othe
16a76 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
16a77 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 ction..**.** 3:
16a78 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 The database mu
16a79 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 st be writable (
16a7a 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 not on read-only
16a7b 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a media).**.** 4:
16a7c 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 There must be
16a7d 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 an active transa
16a7e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 ction..**.** No
16a7f 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 checking is done
16a80 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
16a81 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 at page iTable r
16a82 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 eally is the.**
16a83 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 root page of a b
16a84 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 -tree. If it is
16a85 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 not, then the c
16a86 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a ursor acquired.*
16a87 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 * will not work
16a88 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a correctly..**.**
16a89 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
16a8a 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 hat the sqlite3B
16a8b 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 treeCursorZero()
16a8c 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
16a8d 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 .** on pCur to i
16a8e 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
16a8f 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 mory space prior
16a90 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 to invoking thi
16a91 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 s routine..*/.st
16a92 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 75 atic int btreeCu
16a93 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 rsor(. Btree *p
16a94 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16a95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16a96 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 * The btree */.
16a97 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 int iTable,
16a98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a99 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 /* Root
16a9a 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f page of table to
16a9b 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 open */. int w
16a9c 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 rFlag,
16a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a9e 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e /* 1 to write.
16a9f 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 0 read-only */.
16aa0 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f struct KeyInfo
16aa1 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 *pKeyInfo,
16aa2 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
16aa3 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 t arg to compari
16aa4 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a son function */.
16aa5 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 BtCursor *pCur
16aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16aa7 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 /* Spac
16aa8 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 e for new cursor
16aa9 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 */.){. BtShare
16aaa 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
16aab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16aac 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 65 /* Shared b-tree
16aad 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 handle */.. as
16aae 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
16aaf 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 eeHoldsMutex(p)
16ab0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 );. assert( wrF
16ab1 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 lag==0 || wrFlag
16ab2 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 ==1 );.. /* The
16ab3 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 following asser
16ab4 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65 72 t statements ver
16ab5 69 66 79 20 74 68 61 74 20 69 66 20 74 68 69 73 ify that if this
16ab6 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 20 0a is a sharable .
16ab7 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61 ** b-tree data
16ab8 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 base, the connec
16ab9 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 tion is holding
16aba 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62 the required tab
16abb 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 le locks, . **
16abc 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 and that no othe
16abd 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 r connection has
16abe 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 any open cursor
16abf 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 that conflicts
16ac0 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 with . ** this
16ac1 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 lock. */. asse
16ac2 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 rt( hasSharedCac
16ac3 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 heTableLock(p, i
16ac4 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 Table, pKeyInfo!
16ac5 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 3b =0, wrFlag+1) );
16ac6 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 . assert( wrFla
16ac7 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 64 g==0 || !hasRead
16ac8 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 61 Conflicts(p, iTa
16ac9 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 ble) );.. /* As
16aca 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61 sert that the ca
16acb 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64 20 ller has opened
16acc 74 68 65 20 72 65 71 75 69 72 65 64 20 74 72 61 the required tra
16acd 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 nsaction. */. a
16ace 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
16acf 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a s>TRANS_NONE );.
16ad0 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 assert( wrFlag
16ad1 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e ==0 || p->inTran
16ad2 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
16ad3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d ;. assert( pBt-
16ad4 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 2d 3e >pPage1 && pBt->
16ad5 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b pPage1->aData );
16ad6 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77 72 .. if( NEVER(wr
16ad7 46 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 65 61 Flag && pBt->rea
16ad8 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 65 dOnly) ){. re
16ad9 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 turn SQLITE_READ
16ada 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ONLY;. }. if(
16adb 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 61 67 iTable==1 && pag
16adc 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
16add 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
16ade 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a n SQLITE_EMPTY;.
16adf 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 68 }.. /* Now th
16ae0 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 6f at no other erro
16ae1 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 69 rs can occur, fi
16ae2 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e 20 nish filling in
16ae3 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 2a the BtCursor. *
16ae4 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 * variables and
16ae5 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 link the cursor
16ae6 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 into the BtShare
16ae7 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 70 43 d list. */. pC
16ae8 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 ur->pgnoRoot = (
16ae9 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 70 Pgno)iTable;. p
16aea 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b Cur->iPage = -1;
16aeb 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 . pCur->pKeyInf
16aec 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 o = pKeyInfo;.
16aed 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 pCur->pBtree = p
16aee 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 ;. pCur->pBt =
16aef 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 pBt;. pCur->wrF
16af0 6c 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 67 lag = (u8)wrFlag
16af1 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 ;. pCur->pNext
16af2 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a = pBt->pCursor;.
16af3 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 if( pCur->pNex
16af4 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 t ){. pCur->p
16af5 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 Next->pPrev = pC
16af6 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 ur;. }. pBt->p
16af7 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 Cursor = pCur;.
16af8 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
16af9 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
16afa 20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f pCur->cachedRo
16afb 77 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 wid = 0;. retur
16afc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 n SQLITE_OK;.}.S
16afd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16afe 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
16aff 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 rsor(. Btree *p
16b00 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b02 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 /* The btree
16b03 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */. int iTable
16b04 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
16b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b06 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
16b07 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 f table to open
16b08 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c */. int wrFlag,
16b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b0b 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 /* 1 to write.
16b0c 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 0 read-only */.
16b0d 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
16b0e 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 *pKeyInfo,
16b0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16b10 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f First arg to xCo
16b11 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 mpare() */. BtC
16b12 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 ursor *pCur
16b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b14 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
16b15 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 e new cursor her
16b16 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
16b17 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16b18 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d Enter(p);. rc =
16b19 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 btreeCursor(p,
16b1a 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 iTable, wrFlag,
16b1b 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b pKeyInfo, pCur);
16b1c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
16b1d 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
16b1e 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
16b1f 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f eturn the size o
16b20 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a f a BtCursor obj
16b21 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a ect in bytes..**
16b22 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
16b23 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f ces is needed so
16b24 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 that users of c
16b25 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c ursors can preal
16b26 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 locate.** suffic
16b27 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 ient storage to
16b28 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 hold a cursor.
16b29 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a The BtCursor obj
16b2a 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a ect is opaque.**
16b2b 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 to users so the
16b2c 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 y cannot do the
16b2d 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c sizeof() themsel
16b2e 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 ves - they must
16b2f 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 call.** this rou
16b30 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tine..*/.SQLITE_
16b31 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16b32 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 te3BtreeCursorSi
16b33 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 ze(void){. retu
16b34 72 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 rn ROUND8(sizeof
16b35 28 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a (BtCursor));.}..
16b36 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
16b37 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 69 6c memory that wil
16b38 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 l be converted i
16b39 6e 74 6f 20 61 20 42 74 43 75 72 73 6f 72 20 6f nto a BtCursor o
16b3a 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 bject..**.** The
16b3b 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 simple approach
16b3c 20 68 65 72 65 20 77 6f 75 6c 64 20 62 65 20 74 here would be t
16b3d 6f 20 6d 65 6d 73 65 74 28 29 20 74 68 65 20 65 o memset() the e
16b3e 6e 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 ntire object.**
16b3f 74 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 74 to zero. But it
16b40 20 74 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 turns out that
16b41 74 68 65 20 61 70 50 61 67 65 5b 5d 20 61 6e 64 the apPage[] and
16b42 20 61 69 49 64 78 5b 5d 20 61 72 72 61 79 73 0a aiIdx[] arrays.
16b43 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 ** do not need t
16b44 6f 20 62 65 20 7a 65 72 6f 65 64 20 61 6e 64 20 o be zeroed and
16b45 74 68 65 79 20 61 72 65 20 6c 61 72 67 65 2c 20 they are large,
16b46 73 6f 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 so we can save a
16b47 20 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74 lot.** of run-t
16b48 69 6d 65 20 62 79 20 73 6b 69 70 70 69 6e 67 20 ime by skipping
16b49 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 the initializati
16b4a 6f 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c 65 6d on of those elem
16b4b 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
16b4c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
16b4d 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a ite3BtreeCursorZ
16b4e 65 72 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 29 ero(BtCursor *p)
16b4f 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c {. memset(p, 0,
16b50 20 6f 66 66 73 65 74 6f 66 28 42 74 43 75 72 73 offsetof(BtCurs
16b51 6f 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a or, iPage));.}..
16b52 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61 /*.** Set the ca
16b53 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65 ched rowid value
16b54 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f 72 of every cursor
16b55 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 in the same dat
16b56 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 73 abase file.** as
16b57 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67 pCur and having
16b58 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70 the same root p
16b59 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43 age number as pC
16b5a 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 ur. The value i
16b5b 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77 s.** set to iRow
16b5c 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 70 id..**.** Only p
16b5d 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 61 ositive rowid va
16b5e 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 lues are conside
16b5f 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 68 red valid for th
16b60 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 is cache..** The
16b61 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 61 cache is initia
16b62 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 69 lized to zero, i
16b63 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 76 ndicating an inv
16b64 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 41 alid cache..** A
16b65 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 6b btree will work
16b66 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f 20 fine with zero
16b67 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 or negative rowi
16b68 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 6e ds. We just can
16b69 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 72 not.** cache zer
16b6a 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f o or negative ro
16b6b 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 6e wids, which mean
16b6c 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 73 s tables that us
16b6d 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 e zero or.** neg
16b6e 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 67 ative rowids mig
16b6f 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 ht run a little
16b70 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e 20 slower. But in
16b71 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a 2a practice, zero.*
16b72 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f * or negative ro
16b73 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75 6e wids are very un
16b74 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 73 common so this s
16b75 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 70 hould not be a p
16b76 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 roblem..*/.SQLIT
16b77 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
16b78 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
16b79 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 chedRowid(BtCurs
16b7a 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 65 or *pCur, sqlite
16b7b 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 3_int64 iRowid){
16b7c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a . BtCursor *p;.
16b7d 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 42 for(p=pCur->pB
16b7e 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
16b7f 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
16b80 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d if( p->pgnoRoot=
16b81 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 =pCur->pgnoRoot
16b82 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 ) p->cachedRowid
16b83 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a 20 = iRowid;. }.
16b84 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 assert( pCur->c
16b85 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77 achedRowid==iRow
16b86 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 id );.}../*.** R
16b87 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 64 eturn the cached
16b88 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 67 rowid for the g
16b89 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 20 iven cursor. A
16b8a 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f negative or zero
16b8b 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 .** return value
16b8c 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
16b8d 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 the rowid cache
16b8e 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 73 is invalid and s
16b8f 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e 6f hould be.** igno
16b90 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f 77 red. If the row
16b91 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 76 id cache has nev
16b92 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 73 er before been s
16b93 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a 65 et, then a.** ze
16b94 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a ro is returned..
16b95 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16b96 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 E sqlite3_int64
16b97 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 sqlite3BtreeGetC
16b98 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
16b99 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 sor *pCur){. re
16b9a 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 65 turn pCur->cache
16b9b 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a dRowid;.}../*.**
16b9c 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e Close a cursor.
16b9d 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 The read lock
16b9e 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
16b9f 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 file is released
16ba0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 .** when the las
16ba1 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 t cursor is clos
16ba2 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
16ba3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
16ba4 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 3BtreeCloseCurso
16ba5 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 r(BtCursor *pCur
16ba6 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 ){. Btree *pBtr
16ba7 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 ee = pCur->pBtre
16ba8 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 e;. if( pBtree
16ba9 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
16baa 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
16bab 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20 = pCur->pBt;.
16bac 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
16bad 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 er(pBtree);.
16bae 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
16baf 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 rCursor(pCur);.
16bb0 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72 if( pCur->pPr
16bb1 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 ev ){. pCur
16bb2 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d ->pPrev->pNext =
16bb3 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 pCur->pNext;.
16bb4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
16bb5 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 Bt->pCursor = pC
16bb6 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d ur->pNext;. }
16bb7 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 . if( pCur->p
16bb8 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 Next ){. pC
16bb9 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 ur->pNext->pPrev
16bba 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a = pCur->pPrev;.
16bbb 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
16bbc 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 0; i<=pCur->iPag
16bbd 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 e; i++){. r
16bbe 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d eleasePage(pCur-
16bbf 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 >apPage[i]);.
16bc0 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 }. unlockBtr
16bc1 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b eeIfUnused(pBt);
16bc2 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f . invalidateO
16bc3 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 verflowCache(pCu
16bc4 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 r);. /* sqlit
16bc5 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a e3_free(pCur); *
16bc6 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 /. sqlite3Btr
16bc7 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b eeLeave(pBtree);
16bc8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
16bc9 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16bca 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
16bcb 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 BtCursor* given
16bcc 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 in the argument
16bcd 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 has a valid.** B
16bce 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 tCursor.info str
16bcf 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 69 ucture. If it i
16bd0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61 s not already va
16bd1 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72 lid, call.** btr
16bd2 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f eeParseCell() to
16bd3 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a fill it in..**.
16bd4 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f ** BtCursor.info
16bd5 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 is a cache of t
16bd6 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 he information i
16bd7 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 n the current ce
16bd8 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 ll..** Using thi
16bd9 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 s cache reduces
16bda 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 the number of ca
16bdb 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 lls to btreePars
16bdc 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 eCell()..**.** 2
16bdd 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72 007-06-25: Ther
16bde 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f e is a bug in so
16bdf 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d me versions of M
16be0 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20 74 SVC that cause t
16be1 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74 he.** compiler t
16be2 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 74 o crash when get
16be3 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d CellInfo() is im
16be4 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d plemented as a m
16be5 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65 acro..** But the
16be6 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 61 re is a measurea
16be7 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e 74 ble speed advant
16be8 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65 age to using the
16be9 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a macro on gcc.**
16bea 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70 (when less comp
16beb 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f iler optimizatio
16bec 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d ns like -Os or -
16bed 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 20 O0 are used and
16bee 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 the.** compiler
16bef 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 72 is not doing agr
16bf0 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 2e essive inlining.
16bf1 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 72 ) So we use a r
16bf2 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 eal function.**
16bf3 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 6d for MSVC and a m
16bf4 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 68 acro for everyth
16bf5 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b 65 ing else. Ticke
16bf6 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 6e t #2457..*/.#ifn
16bf7 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 61 def NDEBUG. sta
16bf8 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 43 tic void assertC
16bf9 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 ellInfo(BtCursor
16bfa 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 6c *pCur){. Cel
16bfb 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 lInfo info;.
16bfc 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
16bfd 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 6d ->iPage;. mem
16bfe 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 69 set(&info, 0, si
16bff 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 20 zeof(info));.
16c00 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
16c01 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
16c02 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 ge], pCur->aiIdx
16c03 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b [iPage], &info);
16c04 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d . assert( mem
16c05 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 cmp(&info, &pCur
16c06 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 ->info, sizeof(i
16c07 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a nfo))==0 );. }.
16c08 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 #else. #define
16c09 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 assertCellInfo(x
16c0a 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 ).#endif.#ifdef
16c0b 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 _MSC_VER. /* Us
16c0c 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f e a real functio
16c0d 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 n in MSVC to wor
16c0e 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e k around bugs in
16c0f 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 that compiler.
16c10 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 */. static void
16c11 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 getCellInfo(BtC
16c12 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
16c13 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f if( pCur->info
16c14 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 .nSize==0 ){.
16c15 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 int iPage = p
16c16 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 Cur->iPage;.
16c17 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
16c18 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 (pCur->apPage[iP
16c19 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 age],pCur->aiIdx
16c1a 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 [iPage],&pCur->i
16c1b 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 nfo);. pCur
16c1c 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b ->validNKey = 1;
16c1d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
16c1e 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f assertCellInfo
16c1f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 (pCur);. }.
16c20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f }.#else /* if no
16c21 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 t _MSC_VER */.
16c22 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 /* Use a macro i
16c23 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 n all other comp
16c24 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 ilers so that th
16c25 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e e function is in
16c26 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 lined */.#define
16c27 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 getCellInfo(pCu
16c28 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r)
16c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c2b 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 \. if(
16c2c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
16c2d 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 ==0 ){
16c2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c30 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 \. i
16c31 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d nt iPage = pCur-
16c32 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 >iPage;
16c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c35 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
16c36 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 btreeParseCell(p
16c37 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 Cur->apPage[iPag
16c38 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 e],pCur->aiIdx[i
16c39 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 Page],&pCur->inf
16c3a 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e o); \. pCur->
16c3b 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20 validNKey = 1;
16c3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c3f 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 \. }else{
16c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c44 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65 \. asse
16c45 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 rtCellInfo(pCur)
16c46 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16c47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c49 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e \. }.#en
16c4a 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20 dif /* _MSC_VER
16c4b 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 */..#ifndef NDEB
16c4c 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 UG /* The next
16c4d 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e 6c routine used onl
16c4e 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 y within assert(
16c4f 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a ) statements */.
16c50 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
16c51 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 42 e if the given B
16c52 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 tCursor is valid
16c53 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 73 6f . A valid curso
16c54 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 r is one.** that
16c55 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
16c56 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 inting to a row
16c57 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29 in a (non-empty)
16c58 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 table..** This
16c59 69 73 20 61 20 76 65 72 69 66 69 63 61 74 69 6f is a verificatio
16c5a 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 n routine is use
16c5b 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 d only within as
16c5c 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
16c5d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
16c5e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16c5f 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c BtreeCursorIsVal
16c60 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 id(BtCursor *pCu
16c61 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 r){. return pCu
16c62 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 r && pCur->eStat
16c63 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b e==CURSOR_VALID;
16c64 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 .}.#endif /* NDE
16c65 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 BUG */../*.** Se
16c66 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 t *pSize to the
16c67 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 size of the buff
16c68 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c er needed to hol
16c69 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a d the value of.*
16c6a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68 * the key for th
16c6b 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e e current entry.
16c6c 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 If the cursor
16c6d 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a is not pointing.
16c6e 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e ** to a valid en
16c6f 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73 try, *pSize is s
16c70 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 et to 0. .**.**
16c71 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 68 For a table with
16c72 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67 the INTKEY flag
16c73 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69 set, this routi
16c74 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b ne returns the k
16c75 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f ey.** itself, no
16c76 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
16c77 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79 bytes in the key
16c78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
16c79 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e er must position
16c7a 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 69 6f the cursor prio
16c7b 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 r to invoking th
16c7c 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 0a is routine..** .
16c7d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
16c7e 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74 cannot fail. It
16c7f 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
16c80 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f 0a SQLITE_OK. .*/.
16c81 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16c82 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b nt sqlite3BtreeK
16c83 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 eySize(BtCursor
16c84 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 7a *pCur, i64 *pSiz
16c85 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 e){. assert( cu
16c86 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16c87 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
16c88 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16c89 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c CURSOR_INVALID |
16c8a 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d | pCur->eState==
16c8b 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
16c8c 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 if( pCur->eSta
16c8d 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te!=CURSOR_VALID
16c8e 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d ){. *pSize =
16c8f 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
16c90 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 getCellInfo(pCu
16c91 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d r);. *pSize =
16c92 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 pCur->info.nKey
16c93 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
16c94 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
16c95 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f ** Set *pSize to
16c96 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
16c97 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 ytes of data in
16c98 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a the entry the.**
16c99 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c cursor currentl
16c9a 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a 0a y points to..**.
16c9b 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 ** The caller mu
16c9c 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 st guarantee tha
16c9d 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 t the cursor is
16c9e 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e 6f pointing to a no
16c9f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 20 n-NULL.** valid
16ca0 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 72 entry. In other
16ca1 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c 6c words, the call
16ca2 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d 75 ing procedure mu
16ca3 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a 20 st guarantee.**
16ca4 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
16ca5 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61 74 has Cursor.eStat
16ca6 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e e==CURSOR_VALID.
16ca7 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 .**.** Failure i
16ca8 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 s not possible.
16ca9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
16caa 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 51 lways returns SQ
16cab 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 6d LITE_OK..** It m
16cac 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 65 6c ight just as wel
16cad 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 72 65 l be a procedure
16cae 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 64 (returning void
16caf 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e 75 ) but we continu
16cb0 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 e.** to return a
16cb1 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 n integer result
16cb2 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f 72 code for histor
16cb3 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a 2f ical reasons..*/
16cb4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16cb5 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16cb6 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f DataSize(BtCurso
16cb7 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 r *pCur, u32 *pS
16cb8 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ize){. assert(
16cb9 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
16cba 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
16cbb 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16cbc 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16cbd 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 ;. getCellInfo(
16cbe 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 20 pCur);. *pSize
16cbf 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 = pCur->info.nDa
16cc0 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ta;. return SQL
16cc1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
16cc2 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 Given the page
16cc3 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 number of an ove
16cc4 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 rflow page in th
16cc5 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72 61 e database (para
16cc6 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 meter.** ovfl),
16cc7 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 this function fi
16cc8 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d nds the page num
16cc9 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 ber of the next
16cca 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 page in the .**
16ccb 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f linked list of o
16ccc 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 verflow pages. I
16ccd 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 f possible, it u
16cce 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 ses the auto-vac
16ccf 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d uum.** pointer-m
16cd0 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 ap data instead
16cd1 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 of reading the c
16cd2 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f ontent of page o
16cd3 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a vfl to do so. .*
16cd4 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 *.** If an error
16cd5 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 occurs an SQLit
16cd6 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
16cd7 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 returned. Otherw
16cd8 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ise:.**.** The p
16cd9 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
16cda 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 e next overflow
16cdb 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b page in the link
16cdc 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77 ed list is .** w
16cdd 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f ritten to *pPgno
16cde 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 Next. If page ov
16cdf 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 70 fl is the last p
16ce0 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65 age in its linke
16ce1 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67 d .** list, *pPg
16ce2 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f noNext is set to
16ce3 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 zero. .**.** If
16ce4 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e ppPage is not N
16ce5 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72 ULL, and a refer
16ce6 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 ence to the MemP
16ce7 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 age object corre
16ce8 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 sponding.** to p
16ce9 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c age number pOvfl
16cea 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 was obtained, t
16ceb 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 hen *ppPage is s
16cec 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 et to point to t
16ced 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 hat.** reference
16cee 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 . It is the resp
16cef 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 onsibility of th
16cf0 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c e caller to call
16cf1 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a releasePage().*
16cf2 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20 * on *ppPage to
16cf3 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65 6e free the referen
16cf4 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65 ce. In no refere
16cf5 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64 nce was obtained
16cf6 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 (because.** the
16cf7 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73 pointer-map was
16cf8 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 used to obtain
16cf9 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70 the value for *p
16cfa 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a PgnoNext), then.
16cfb 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 ** *ppPage is se
16cfc 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 t to zero..*/.st
16cfd 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 atic int getOver
16cfe 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 flowPage(. BtSh
16cff 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
16d00 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
16d01 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
16d02 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 . Pgno ovfl,
16d03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16d04 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66 /* Current overf
16d05 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 low page number
16d06 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 */. MemPage **p
16d07 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 pPage,
16d08 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 /* OUT: MemPag
16d09 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 e handle (may be
16d0a 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f NULL) */. Pgno
16d0b 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 *pPgnoNext
16d0c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
16d0d 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 Next overflow p
16d0e 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b age number */.){
16d0f 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 . Pgno next = 0
16d10 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
16d11 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 ge = 0;. int rc
16d12 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
16d13 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16d14 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
16d15 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
16d16 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a ert(pPgnoNext);.
16d17 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
16d18 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
16d19 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 /* Try to find
16d1a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
16d1b 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c n the overflow l
16d1c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 ist using the.
16d1d 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f ** autovacuum po
16d1e 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e inter-map pages.
16d1f 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 Guess that the
16d20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 next page in .
16d21 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 ** the overflow
16d22 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d list is page num
16d23 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 ber (ovfl+1). If
16d24 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e that guess turn
16d25 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 s . ** out to b
16d26 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 e wrong, fall ba
16d27 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 ck to loading th
16d28 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a e data of page .
16d29 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c ** number ovfl
16d2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
16d2b 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 e next page numb
16d2c 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 er.. */. if( p
16d2d 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
16d2e 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b {. Pgno pgno;
16d2f 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 . Pgno iGuess
16d30 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 = ovfl+1;. u
16d31 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 8 eType;.. wh
16d32 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 ile( PTRMAP_ISPA
16d33 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 GE(pBt, iGuess)
16d34 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 || iGuess==PENDI
16d35 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
16d36 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 ) ){. iGues
16d37 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 s++;. }..
16d38 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 if( iGuess<=page
16d39 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 rPagecount(pBt)
16d3a 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 ){. rc = pt
16d3b 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 rmapGet(pBt, iGu
16d3c 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 ess, &eType, &pg
16d3d 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 no);. if( r
16d3e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
16d3f 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
16d40 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d ERFLOW2 && pgno=
16d41 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 =ovfl ){.
16d42 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a next = iGuess;.
16d43 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
16d44 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 ITE_DONE;.
16d45 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 }. }. }.#end
16d46 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65 if.. assert( ne
16d47 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c xt==0 || rc==SQL
16d48 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 ITE_DONE );. if
16d49 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16d4a 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
16d4b 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 eGetPage(pBt, ov
16d4c 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a fl, &pPage, 0);.
16d4d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
16d4e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 SQLITE_OK || pPa
16d4f 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 ge==0 );. if(
16d50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
16d51 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 {. next = g
16d52 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 et4byte(pPage->a
16d53 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Data);. }. }
16d54 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d .. *pPgnoNext =
16d55 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70 50 next;. if( ppP
16d56 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 61 age ){. *ppPa
16d57 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 65 ge = pPage;. }e
16d58 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 lse{. release
16d59 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d Page(pPage);. }
16d5a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 . return (rc==S
16d5b 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c QLITE_DONE ? SQL
16d5c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a ITE_OK : rc);.}.
16d5d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 ./*.** Copy data
16d5e 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 from a buffer t
16d5f 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f o a page, or fro
16d60 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 m a page to a bu
16d61 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 ffer..**.** pPay
16d62 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65 load is a pointe
16d63 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64 r to data stored
16d64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 on database pag
16d65 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66 e pDbPage..** If
16d66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73 argument eOp is
16d67 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79 false, then nBy
16d68 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 te bytes of data
16d69 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 are copied.** f
16d6a 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20 rom pPayload to
16d6b 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 the buffer point
16d6c 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 49 ed at by pBuf. I
16d6d 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a f eOp is true,.*
16d6e 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61 * then sqlite3Pa
16d6f 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 gerWrite() is ca
16d70 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20 lled on pDbPage
16d71 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a and nByte bytes.
16d72 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 ** of data are c
16d73 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 opied from the b
16d74 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 50 uffer pBuf to pP
16d75 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 ayload..**.** SQ
16d76 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
16d77 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 ned on success,
16d78 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 otherwise an err
16d79 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 or code..*/.stat
16d7a 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f ic int copyPaylo
16d7b 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79 ad(. void *pPay
16d7c 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 load,
16d7d 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 /* Pointer to pa
16d7e 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 ge data */. voi
16d7f 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 d *pBuf,
16d80 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
16d81 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20 r to buffer */.
16d82 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 int nByte,
16d83 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
16d84 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f mber of bytes to
16d85 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65 copy */. int e
16d86 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 Op,
16d87 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 /* 0 -> cop
16d88 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d y from page, 1 -
16d89 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a > copy to page *
16d8a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 /. DbPage *pDbP
16d8b 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a age /*
16d8c 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 Page containing
16d8d 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a pPayload */.){.
16d8e 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20 if( eOp ){.
16d8f 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 /* Copy data fr
16d90 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 67 om buffer to pag
16d91 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 61 e (a write opera
16d92 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 tion) */. int
16d93 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
16d94 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 erWrite(pDbPage)
16d95 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
16d96 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16d97 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16d98 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 }. memcpy(pPa
16d99 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79 yload, pBuf, nBy
16d9a 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 te);. }else{.
16d9b 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 /* Copy data f
16d9c 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66 rom page to buff
16d9d 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 61 er (a read opera
16d9e 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d tion) */. mem
16d9f 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f cpy(pBuf, pPaylo
16da0 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a ad, nByte);. }.
16da1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16da2 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 OK;.}../*.** Thi
16da3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
16da4 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 ed to read or ov
16da5 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 erwrite payload
16da6 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 information.** f
16da7 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 or the entry tha
16da8 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73 6f t the pCur curso
16da9 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
16daa 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20 . If the eOp.**
16dab 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 parameter is 0,
16dac 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f this is a read o
16dad 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63 peration (data c
16dae 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 opied into.** bu
16daf 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 69 ffer pBuf). If i
16db0 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 t is non-zero, a
16db1 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f 70 write (data cop
16db2 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 ied from.** buff
16db3 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 er pBuf)..**.**
16db4 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 A total of "amt"
16db5 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64 20 bytes are read
16db6 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e or written begin
16db7 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 ning at "offset"
16db8 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 ..** Data is rea
16db9 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 d to or from the
16dba 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a buffer pBuf..**
16dbb 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 .** The content
16dbc 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 being read or wr
16dbd 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 itten might appe
16dbe 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 ar on the main p
16dbf 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 age.** or be sca
16dc0 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 ttered out on mu
16dc1 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 ltiple overflow
16dc2 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pages..**.** If
16dc3 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49 the BtCursor.isI
16dc4 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c ncrblobHandle fl
16dc5 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74 ag is set, and t
16dc6 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75 he current.** cu
16dc7 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20 rsor entry uses
16dc8 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 one or more over
16dc9 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73 flow pages, this
16dca 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c function.** all
16dcb 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f 72 ocates space for
16dcc 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c and lazily popl
16dcd 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c uates the overfl
16dce 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a ow page-list .**
16dcf 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42 74 cache array (Bt
16dd0 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 Cursor.aOverflow
16dd1 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 ). Subsequent ca
16dd2 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20 lls use this.**
16dd3 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 cache to make se
16dd4 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 eking to the sup
16dd5 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 plied offset mor
16dd6 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a e efficient..**.
16dd7 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 ** Once an overf
16dd8 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
16dd9 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c che has been all
16dda 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62 ocated, it may b
16ddb 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 e.** invalidated
16ddc 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 if some other c
16ddd 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 ursor writes to
16dde 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 the same table,
16ddf 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 or if.** the cur
16de0 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 sor is moved to
16de1 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e a different row.
16de2 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 Additionally, i
16de3 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a n auto-vacuum.**
16de4 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f mode, the follo
16de5 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 wing events may
16de6 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 invalidate an ov
16de7 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 erflow page-list
16de8 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 cache..**.**
16de9 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c * An incremental
16dea 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 vacuum,.** *
16deb 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f A commit in auto
16dec 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d _vacuum="full" m
16ded 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 ode,.** * Crea
16dee 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 ting a table (ma
16def 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 y require moving
16df0 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
16df1 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e e)..*/.static in
16df2 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 t accessPayload(
16df3 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
16df4 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f r, /* Curso
16df5 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e r pointing to en
16df6 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d try to read from
16df7 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 */. u32 offset
16df8 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 , /* Be
16df9 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 gin reading this
16dfa 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 far into payloa
16dfb 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20 d */. u32 amt,
16dfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
16dfd 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 ead this many by
16dfe 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 tes */. unsigne
16dff 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a d char *pBuf, /*
16e00 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65 73 Write the bytes
16e01 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 into this buffe
16e02 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20 r */ . int eOp
16e03 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16e04 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f zero to read. no
16e05 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e n-zero to write.
16e06 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 */.){. unsigne
16e07 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 d char *aPayload
16e08 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
16e09 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b ITE_OK;. u32 nK
16e0a 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d ey;. int iIdx =
16e0b 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 0;. MemPage *p
16e0c 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
16e0d 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16e0e 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 ; /* Btree page
16e0f 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 of current entry
16e10 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
16e11 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b pBt = pCur->pBt;
16e12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16e13 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 /* Btree this
16e14 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 cursor belongs t
16e15 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 o */.. assert(
16e16 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 pPage );. asser
16e17 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16e18 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16e19 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16e1a 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
16e1b 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c ge]<pPage->nCell
16e1c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 );. assert( cu
16e1d 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16e1e 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 Cur) );.. getCe
16e1f 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 llInfo(pCur);.
16e20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d aPayload = pCur-
16e21 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 >info.pCell + pC
16e22 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 ur->info.nHeader
16e23 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 ;. nKey = (pPag
16e24 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 e->intKey ? 0 :
16e25 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e (int)pCur->info.
16e26 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e 45 nKey);.. if( NE
16e27 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e VER(offset+amt >
16e28 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f nKey+pCur->info
16e29 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 26 .nData) . || &
16e2a 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 aPayload[pCur->i
16e2b 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 nfo.nLocal] > &p
16e2c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d Page->aData[pBt-
16e2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 >usableSize]. )
16e2e 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 {. /* Trying
16e2f 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 to read or write
16e30 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
16e31 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 the data is an
16e32 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 error */. ret
16e33 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
16e34 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 PT_BKPT;. }..
16e35 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 /* Check if data
16e36 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 must be read/wr
16e37 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 itten to/from th
16e38 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73 e btree page its
16e39 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 elf. */. if( of
16e3a 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e fset<pCur->info.
16e3b 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e nLocal ){. in
16e3c 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 t a = amt;. i
16e3d 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 f( a+offset>pCur
16e3e 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b ->info.nLocal ){
16e3f 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d . a = pCur-
16e40 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f >info.nLocal - o
16e41 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 ffset;. }.
16e42 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 rc = copyPayloa
16e43 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 d(&aPayload[offs
16e44 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f et], pBuf, a, eO
16e45 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 p, pPage->pDbPag
16e46 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d e);. offset =
16e47 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 0;. pBuf +=
16e48 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b a;. amt -= a;
16e49 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 . }else{. of
16e4a 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e fset -= pCur->in
16e4b 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a fo.nLocal;. }..
16e4c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16e4d 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a _OK && amt>0 ){.
16e4e 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 const u32 ov
16e4f 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 flSize = pBt->us
16e50 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f ableSize - 4; /
16e51 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 * Bytes content
16e52 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f per ovfl page */
16e53 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 . Pgno nextPa
16e54 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 ge;.. nextPag
16e55 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 e = get4byte(&aP
16e56 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 ayload[pCur->inf
16e57 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 o.nLocal]);..#if
16e58 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16e59 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a _INCRBLOB. /*
16e5a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c If the isIncrbl
16e5b 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 obHandle flag is
16e5c 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43 set and the BtC
16e5d 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b ursor.aOverflow[
16e5e 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 ]. ** has not
16e5f 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c been allocated,
16e60 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 allocate it now
16e61 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73 . The array is s
16e62 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f ized at. ** o
16e63 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 ne entry for eac
16e64 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 h overflow page
16e65 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 in the overflow
16e66 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a chain. The. *
16e67 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 * page number of
16e68 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 the first overf
16e69 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 low page is stor
16e6a 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b ed in aOverflow[
16e6b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 0],. ** etc.
16e6c 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 A value of 0 in
16e6d 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 the aOverflow[]
16e6e 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 array means "not
16e6f 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 yet known".
16e70 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73 ** (the cache is
16e71 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 lazily populate
16e72 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 d).. */. i
16e73 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 f( pCur->isIncrb
16e74 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43 lobHandle && !pC
16e75 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b ur->aOverflow ){
16e76 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c . int nOvfl
16e77 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e = (pCur->info.n
16e78 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e Payload-pCur->in
16e79 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 fo.nLocal+ovflSi
16e7a 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a ze-1)/ovflSize;.
16e7b 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 pCur->aOve
16e7c 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 rflow = (Pgno *)
16e7d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
16e7e 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e o(sizeof(Pgno)*n
16e7f 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a 20 Ovfl);. /*
16e80 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73 20 nOvfl is always
16e81 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69 74 positive. If it
16e82 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 63 were zero, fetc
16e83 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 68 hPayload would h
16e84 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 65 ave. ** bee
16e85 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f n used instead o
16e86 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 f this routine.
16e87 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 */. if( ALW
16e88 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 70 AYS(nOvfl) && !p
16e89 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 Cur->aOverflow )
16e8a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
16e8b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
16e8c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
16e8d 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c /* If the overfl
16e8e 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 ow page-list cac
16e8f 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f he has been allo
16e90 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20 cated and the.
16e91 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 ** entry for t
16e92 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72 65 he first require
16e93 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 d overflow page
16e94 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20 is valid, skip.
16e95 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 ** directly t
16e96 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 o it.. */.
16e97 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 if( pCur->aOver
16e98 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f flow && pCur->aO
16e99 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f verflow[offset/o
16e9a 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 20 vflSize] ){.
16e9b 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 iIdx = (offset
16e9c 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 /ovflSize);.
16e9d 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 nextPage = pCu
16e9e 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 r->aOverflow[iId
16e9f 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 x];. offset
16ea0 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53 = (offset%ovflS
16ea1 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ize);. }.#end
16ea2 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 if.. for( ; r
16ea3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
16ea4 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61 67 amt>0 && nextPag
16ea5 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 e; iIdx++){..#if
16ea6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16ea7 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 _INCRBLOB.
16ea8 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 /* If required,
16ea9 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 populate the ove
16eaa 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
16eab 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 cache. */.
16eac 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 if( pCur->aOverf
16ead 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 61 low ){. a
16eae 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 ssert(!pCur->aOv
16eaf 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 erflow[iIdx] ||
16eb0 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
16eb1 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 iIdx]==nextPage)
16eb2 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e ;. pCur->
16eb3 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 aOverflow[iIdx]
16eb4 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20 = nextPage;.
16eb5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
16eb6 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 if( offset>=ov
16eb7 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 flSize ){.
16eb8 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 /* The only re
16eb9 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 ason to read thi
16eba 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 s page is to obt
16ebb 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 ain the page.
16ebc 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 ** number f
16ebd 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 or the next page
16ebe 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 in the overflow
16ebf 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 chain. The page
16ec0 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 . ** data
16ec1 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
16ec2 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 . So first try t
16ec3 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 o lookup the ove
16ec4 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a rflow. **
16ec5 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 page-list cache
16ec6 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 , if any, then f
16ec7 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 all back to the
16ec8 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 getOverflowPage(
16ec9 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e ). ** fun
16eca 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a ction.. *
16ecb 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
16ecc 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
16ecd 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d if( pCur-
16ece 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 >aOverflow && pC
16ecf 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
16ed0 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 dx+1] ){.
16ed1 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 nextPage = pC
16ed2 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
16ed3 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d dx+1];. }
16ed4 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 else .#endif.
16ed5 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 rc = get
16ed6 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 OverflowPage(pBt
16ed7 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 , nextPage, 0, &
16ed8 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 nextPage);.
16ed9 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 offset -= ovf
16eda 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c lSize;. }el
16edb 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e se{. /* N
16edc 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 73 eed to read this
16edd 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 page properly.
16ede 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 It contains some
16edf 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 of the.
16ee0 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61 ** range of data
16ee1 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 72 that is being r
16ee2 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 ead (eOp==0) or
16ee3 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 written (eOp!=0)
16ee4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
16ee5 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 DbPage *pDb
16ee6 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69 6e Page;. in
16ee7 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 20 t a = amt;.
16ee8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
16ee9 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 agerGet(pBt->pPa
16eea 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 ger, nextPage, &
16eeb 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
16eec 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16eed 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
16eee 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69 aPayload = sqli
16eef 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
16ef0 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
16ef1 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 nextPage = g
16ef2 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61 64 et4byte(aPayload
16ef3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
16ef4 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 a + offset > ov
16ef5 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 flSize ){.
16ef6 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 a = ovflSi
16ef7 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 ze - offset;.
16ef8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
16ef9 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c rc = copyPayl
16efa 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 oad(&aPayload[of
16efb 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 fset+4], pBuf, a
16efc 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b , eOp, pDbPage);
16efd 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
16efe 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 e3PagerUnref(pDb
16eff 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
16f00 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 offset = 0;.
16f01 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b amt -= a;
16f02 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66 20 . pBuf
16f03 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a += a;. }.
16f04 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
16f05 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }.. if( rc==SQL
16f06 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 ITE_OK && amt>0
16f07 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16f08 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
16f09 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 T;. }. return
16f0a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 rc;.}../*.** Rea
16f0b 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 d part of the ke
16f0c 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
16f0d 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 h cursor pCur.
16f0e 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 Exactly.** "amt"
16f0f 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 bytes will be t
16f10 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 ransfered into p
16f11 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e Buf[]. The tran
16f12 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 sfer.** begins a
16f13 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a t "offset"..**.*
16f14 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 * The caller mus
16f15 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 t ensure that pC
16f16 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 ur is pointing t
16f17 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a o a valid row.**
16f18 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a in the table..*
16f19 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
16f1a 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
16f1b 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 or an error cod
16f1c 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f e if anything go
16f1d 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e es.** wrong. An
16f1e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
16f1f 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d ed if "offset+am
16f20 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 t" is larger tha
16f21 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 n.** the availab
16f22 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 le payload..*/.S
16f23 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16f24 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 t sqlite3BtreeKe
16f25 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 y(BtCursor *pCur
16f26 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 , u32 offset, u3
16f27 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 2 amt, void *pBu
16f28 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 f){. assert( cu
16f29 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16f2a 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
16f2b 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16f2c 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
16f2d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16f2e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 iPage>=0 && pCur
16f2f 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16f30 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 Page] );. asser
16f31 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
16f32 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 Cur->iPage]<pCur
16f33 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16f34 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a Page]->nCell );.
16f35 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 return accessP
16f36 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 ayload(pCur, off
16f37 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 set, amt, (unsig
16f38 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 ned char*)pBuf,
16f39 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 0);.}../*.** Rea
16f3a 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 d part of the da
16f3b 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ta associated wi
16f3c 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 th cursor pCur.
16f3d 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 Exactly.** "amt
16f3e 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 " bytes will be
16f3f 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 transfered into
16f40 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 pBuf[]. The tra
16f41 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 nsfer.** begins
16f42 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a at "offset"..**.
16f43 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
16f44 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f _OK on success o
16f45 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 r an error code
16f46 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 if anything goes
16f47 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 .** wrong. An e
16f48 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
16f49 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 if "offset+amt"
16f4a 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a is larger than.
16f4b 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 ** the available
16f4c 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c payload..*/.SQL
16f4d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16f4e 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
16f4f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
16f50 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 u32 offset, u32
16f51 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 amt, void *pBuf
16f52 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 ){. int rc;..#i
16f53 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16f54 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 T_INCRBLOB. if
16f55 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16f56 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
16f57 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
16f58 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 ITE_ABORT;. }.#
16f59 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 endif.. assert(
16f5a 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
16f5b 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 x(pCur) );. rc
16f5c 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
16f5d 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 osition(pCur);.
16f5e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16f5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OK ){. assert
16f60 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16f61 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
16f62 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
16f63 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 ->iPage>=0 && pC
16f64 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
16f65 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 >iPage] );. a
16f66 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
16f67 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
16f68 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16f69 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
16f6a 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 );. rc = acc
16f6b 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c essPayload(pCur,
16f6c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 offset, amt, pB
16f6d 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 uf, 0);. }. re
16f6e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
16f6f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
16f70 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e er to payload in
16f71 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
16f72 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 he entry that th
16f73 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f e .** pCur curso
16f74 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
16f75 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 . The pointer i
16f76 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
16f77 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 ng of.** the key
16f78 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 if skipKey==0 a
16f79 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 nd it points to
16f7a 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
16f7b 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 data if.** skip
16f7c 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d Key==1. The num
16f7d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
16f7e 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 available key/da
16f7f 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a ta is written.**
16f80 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 into *pAmt. If
16f81 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 *pAmt==0, then
16f82 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
16f83 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a ed will not be.*
16f84 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 * a valid pointe
16f85 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f r..**.** This ro
16f86 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 utine is an opti
16f87 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 mization. It is
16f88 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 common for the
16f89 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e entire key.** an
16f8a 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e d data to fit on
16f8b 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 the local page
16f8c 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f and for there to
16f8d 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a be no overflow.
16f8e 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 ** pages. When
16f8f 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 that is so, this
16f90 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 routine can be
16f91 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 used to access t
16f92 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 he.** key and da
16f93 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e ta without makin
16f94 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 g a copy. If th
16f95 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 e key and/or dat
16f96 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f a spills.** onto
16f97 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c overflow pages,
16f98 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c then accessPayl
16f99 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 oad() must be us
16f9a 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 ed to reassemble
16f9b 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 .** the key/data
16f9c 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 and copy it int
16f9d 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 o a preallocated
16f9e 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 buffer..**.** T
16f9f 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 he pointer retur
16fa0 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ned by this rout
16fa1 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 ine looks direct
16fa2 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 ly into the cach
16fa3 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 ed.** page of th
16fa4 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
16fa5 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e data might chan
16fa6 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e ge or move the n
16fa7 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 ext time.** any
16fa8 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 btree routine is
16fa9 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 called..*/.stat
16faa 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
16fab 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 d char *fetchPay
16fac 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 load(. BtCursor
16fad 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 *pCur, /*
16fae 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 Cursor pointing
16faf 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 to entry to read
16fb0 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a from */. int *
16fb1 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 pAmt,
16fb2 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d /* Write the num
16fb3 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 ber of available
16fb4 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 bytes here */.
16fb5 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 int skipKey
16fb6 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 /* read be
16fb7 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 ginning at data
16fb8 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 if this is true
16fb9 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 */.){. unsigned
16fba 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b char *aPayload;
16fbb 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
16fbc 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 e;. u32 nKey;.
16fbd 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 u32 nLocal;..
16fbe 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 assert( pCur!=0
16fbf 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d && pCur->iPage>=
16fc0 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 0 && pCur->apPag
16fc1 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b e[pCur->iPage]);
16fc2 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16fc3 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16fc4 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
16fc5 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
16fc6 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 tex(pCur) );. p
16fc7 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
16fc8 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16fc9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16fca 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
16fcb 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c age]<pPage->nCel
16fcc 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 l );. if( NEVER
16fcd 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a (pCur->info.nSiz
16fce 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 e==0) ){. btr
16fcf 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 eeParseCell(pCur
16fd0 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16fd1 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 Page], pCur->aiI
16fd2 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c dx[pCur->iPage],
16fd3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
16fd4 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 &pCur->info)
16fd5 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 ;. }. aPayload
16fd6 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 = pCur->info.pC
16fd7 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 ell;. aPayload
16fd8 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 += pCur->info.nH
16fd9 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 eader;. if( pPa
16fda 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
16fdb 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 nKey = 0;. }e
16fdc 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 lse{. nKey =
16fdd 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e (int)pCur->info.
16fde 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 nKey;. }. if(
16fdf 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 skipKey ){. a
16fe0 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b Payload += nKey;
16fe1 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 . nLocal = pC
16fe2 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 ur->info.nLocal
16fe3 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b - nKey;. }else{
16fe4 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 . nLocal = pC
16fe5 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b ur->info.nLocal;
16fe6 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f . assert( nLo
16fe7 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d cal<=nKey );. }
16fe8 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 . *pAmt = nLoca
16fe9 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 l;. return aPay
16fea 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 load;.}.../*.**
16feb 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 For the entry th
16fec 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 at cursor pCur i
16fed 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 s point to, retu
16fee 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 rn as.** many by
16fef 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f tes of the key o
16ff0 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61 76 r data as are av
16ff1 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c ailable on the l
16ff2 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 ocal.** b-tree p
16ff3 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 age. Write the
16ff4 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 number of availa
16ff5 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a ble bytes into *
16ff6 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 pAmt..**.** The
16ff7 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 pointer returned
16ff8 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 is ephemeral.
16ff9 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 The key/data may
16ffa 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 move.** or be d
16ffb 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 estroyed on the
16ffc 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 next call to any
16ffd 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a Btree routine,.
16ffe 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c ** including cal
16fff 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 ls from other th
17000 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 reads against th
17001 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a e same cache..**
17002 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 Hence, a mutex
17003 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 on the BtShared
17004 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 should be held p
17005 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a rior to calling.
17006 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ** this routine.
17007 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
17008 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f tines is used to
17009 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 get quick acces
1700a 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 s to key and dat
1700b 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d a.** in the comm
1700c 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f on case where no
1700d 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
1700e 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c are used..*/.SQL
1700f 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
17010 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 t void *sqlite3B
17011 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 treeKeyFetch(BtC
17012 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
17013 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 *pAmt){. const
17014 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 void *p = 0;.
17015 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17016 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
17017 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
17018 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
17019 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
1701a 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 x(pCur) );. if(
1701b 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 ALWAYS(pCur->eS
1701c 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
1701d 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 ID) ){. p = (
1701e 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 const void*)fetc
1701f 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 hPayload(pCur, p
17020 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 Amt, 0);. }. r
17021 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 eturn p;.}.SQLIT
17022 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
17023 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 void *sqlite3Btr
17024 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43 75 eeDataFetch(BtCu
17025 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
17026 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 *pAmt){. const
17027 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 void *p = 0;. a
17028 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
17029 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e utex_held(pCur->
1702a 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 pBtree->db->mute
1702b 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
1702c 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1702d 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 (pCur) );. if(
1702e 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 ALWAYS(pCur->eSt
1702f 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
17030 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 D) ){. p = (c
17031 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 onst void*)fetch
17032 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 Payload(pCur, pA
17033 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 mt, 1);. }. re
17034 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn p;.}.../*.*
17035 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
17036 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 r down to a new
17037 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 child page. The
17038 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e newPgno argumen
17039 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 t is the.** page
1703a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 number of the c
1703b 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 hild page to mov
1703c 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 e to..**.** This
1703d 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1703e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 s SQLITE_CORRUPT
1703f 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 if the page-hea
17040 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 der flags field
17041 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 of.** the new ch
17042 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f ild page does no
17043 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 t match the flag
17044 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 s field of the p
17045 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 arent (i.e..** i
17046 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 f an intkey page
17047 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 appears to be t
17048 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e he parent of a n
17049 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 on-intkey page,
1704a 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 or.** vice-versa
1704b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
1704c 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 moveToChild(BtC
1704d 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 ursor *pCur, u32
1704e 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 newPgno){. int
1704f 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 rc;. int i = p
17050 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 Cur->iPage;. Me
17051 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b mPage *pNewPage;
17052 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
17053 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 = pCur->pBt;..
17054 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
17055 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
17056 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
17057 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
17058 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
17059 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 ert( pCur->iPage
1705a 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 <BTCURSOR_MAX_DE
1705b 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 PTH );. if( pCu
1705c 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 r->iPage>=(BTCUR
1705d 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 SOR_MAX_DEPTH-1)
1705e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1705f 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
17060 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 PT;. }. rc = g
17061 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 etAndInitPage(pB
17062 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 t, newPgno, &pNe
17063 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 wPage);. if( rc
17064 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
17065 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 pCur->apPage[i+1
17066 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 ] = pNewPage;.
17067 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d pCur->aiIdx[i+1]
17068 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 = 0;. pCur->iP
17069 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e age++;.. pCur->
1706a 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a info.nSize = 0;.
1706b 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
1706c 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 y = 0;. if( pNe
1706d 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c wPage->nCell<1 |
1706e 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b | pNewPage->intK
1706f 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 ey!=pCur->apPage
17070 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 [i]->intKey ){.
17071 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17072 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
17073 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
17074 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
17075 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 f NDEBUG./*.** P
17076 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 age pParent is a
17077 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d n internal (non-
17078 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65 2e leaf) tree page.
17079 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a This function .
1707a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 20 ** asserts that
1707b 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 page number iChi
1707c 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 ld is the left-c
1707d 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 hild if the iIdx
1707e 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 'th.** cell in p
1707f 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c age pParent. Or,
17080 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75 61 if iIdx is equa
17081 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e l to the total n
17082 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c umber of.** cell
17083 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 s in pParent, th
17084 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 at page number i
17085 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 Child is the rig
17086 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 ht-child of.** t
17087 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 he page..*/.stat
17088 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 ic void assertPa
17089 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 rentIndex(MemPag
1708a 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 e *pParent, int
1708b 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c iIdx, Pgno iChil
1708c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 d){. assert( iI
1708d 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 dx<=pParent->nCe
1708e 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 ll );. if( iIdx
1708f 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c ==pParent->nCell
17090 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
17091 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e get4byte(&pParen
17092 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 t->aData[pParent
17093 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d ->hdrOffset+8])=
17094 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c =iChild );. }el
17095 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
17096 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c get4byte(findCel
17097 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 l(pParent, iIdx)
17098 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d )==iChild );. }
17099 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 .}.#else.# defi
1709a 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 ne assertParentI
1709b 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e ndex(x,y,z) .#en
1709c 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 dif../*.** Move
1709d 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f the cursor up to
1709e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
1709f 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 ..**.** pCur->id
170a0 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 x is set to the
170a1 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 cell index that
170a2 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 contains the poi
170a3 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 nter.** to the p
170a4 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e age we are comin
170a5 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 g from. If we a
170a6 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 re coming from t
170a7 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 he.** right-most
170a8 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e child page then
170a9 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 pCur->idx is se
170aa 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 t to one more th
170ab 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 an.** the larges
170ac 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f t cell index..*/
170ad 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 .static void mov
170ae 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 eToParent(BtCurs
170af 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
170b0 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
170b1 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
170b2 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
170b3 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
170b4 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 LID );. assert(
170b5 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 pCur->iPage>0 )
170b6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
170b7 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
170b8 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 Page] );. asser
170b9 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 tParentIndex(.
170ba 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 pCur->apPage[p
170bb 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a Cur->iPage-1], .
170bc 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
170bd 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 pCur->iPage-1],
170be 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 . pCur->apPag
170bf 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
170c0 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 pgno. );. rele
170c1 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
170c2 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
170c3 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 ]);. pCur->iPag
170c4 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 e--;. pCur->inf
170c5 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
170c6 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
170c7 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 0;.}../*.** Mov
170c8 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
170c9 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f point to the roo
170ca 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d t page of its b-
170cb 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a tree structure..
170cc 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 **.** If the tab
170cd 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61 6c le has a virtual
170ce 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e root page, then
170cf 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d the cursor is m
170d0 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a oved to point.**
170d1 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 to the virtual
170d2 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65 61 root page instea
170d3 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c 20 d of the actual
170d4 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61 62 root page. A tab
170d5 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 74 le has a.** virt
170d6 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 68 ual root page wh
170d7 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f en the actual ro
170d8 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 ot page contains
170d9 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 20 no cells and a
170da 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c 64 .** single child
170db 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e 20 page. This can
170dc 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74 68 only happen with
170dd 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 the table roote
170de 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a 0a d at page 1..**.
170df 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 ** If the b-tree
170e0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65 6d structure is em
170e1 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72 20 pty, the cursor
170e2 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 state is set to
170e3 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c .** CURSOR_INVAL
170e4 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 ID. Otherwise, t
170e5 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 65 74 he cursor is set
170e6 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
170e7 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 6c first.** cell l
170e8 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f ocated on the ro
170e9 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20 72 ot (or virtual r
170ea 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74 68 oot) page and th
170eb 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a 2a e cursor state.*
170ec 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 * is set to CURS
170ed 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 OR_VALID..**.**
170ee 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e If this function
170ef 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 returns success
170f0 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65 fully, it may be
170f1 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
170f2 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72 e.** page-header
170f3 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20 flags indicate
170f4 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 61 that the [virtua
170f5 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 l] root-page is
170f6 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a the expected .**
170f7 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20 kind of b-tree
170f8 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68 page (i.e. if wh
170f9 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63 en opening the c
170fa 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72 ursor the caller
170fb 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63 did not.** spec
170fc 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 ify a KeyInfo st
170fd 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 ructure the flag
170fe 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f s byte is set to
170ff 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a 0x05 or 0x0D,.*
17100 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 * indicating a t
17101 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 able b-tree, or
17102 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 if the caller di
17103 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 d specify a KeyI
17104 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 nfo .** structur
17105 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 e the flags byte
17106 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 is set to 0x02
17107 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 or 0x0A, indicat
17108 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 ing an index.**
17109 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74 b-tree)..*/.stat
1710a 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f ic int moveToRoo
1710b 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 t(BtCursor *pCur
1710c 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 ){. MemPage *pR
1710d 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 oot;. int rc =
1710e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 SQLITE_OK;. Btr
1710f 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 ee *p = pCur->pB
17110 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 tree;. BtShared
17111 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
17112 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
17113 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
17114 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 ) );. assert( C
17115 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 URSOR_INVALID <
17116 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
17117 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 EK );. assert(
17118 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c CURSOR_VALID <
17119 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
1711a 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 EEK );. assert(
1711b 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 CURSOR_FAULT
1711c 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 > CURSOR_REQUIRE
1711d 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 SEEK );. if( pC
1711e 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
1711f 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
17120 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e {. if( pCur->
17121 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 eState==CURSOR_F
17122 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73 AULT ){. as
17123 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 sert( pCur->skip
17124 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 Next!=SQLITE_OK
17125 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
17126 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a pCur->skipNext;.
17127 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
17128 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 3BtreeClearCurso
17129 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 r(pCur);. }..
1712a 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e if( pCur->iPage>
1712b 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b =0 ){. int i;
1712c 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c . for(i=1; i<
1712d 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b =pCur->iPage; i+
1712e 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 +){. releas
1712f 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
17130 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 ge[i]);. }.
17131 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 pCur->iPage =
17132 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 0;. }else{.
17133 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 rc = getAndInitP
17134 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 age(pBt, pCur->p
17135 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e gnoRoot, &pCur->
17136 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 apPage[0]);.
17137 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
17138 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d K ){. pCur-
17139 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
1713a 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 _INVALID;.
1713b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1713c 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 . pCur->iPage
1713d 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 = 0;.. /* If
1713e 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 pCur->pKeyInfo
1713f 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 is not NULL, the
17140 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 n the caller tha
17141 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75 t opened this cu
17142 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 rsor. ** expe
17143 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 cted to open it
17144 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 on an index b-tr
17145 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ee. Otherwise, i
17146 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 f pKeyInfo is.
17147 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 ** NULL, the c
17148 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61 20 aller expects a
17149 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 table b-tree. If
1714a 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 this is not the
1714b 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 case,. ** re
1714c 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 turn an SQLITE_C
1714d 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20 2a ORRUPT error. *
1714e 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 /. assert( pC
1714f 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
17150 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 ntKey==1 || pCur
17151 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
17152 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 Key==0 );. if
17153 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 ( (pCur->pKeyInf
17154 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 o==0)!=pCur->apP
17155 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 age[0]->intKey )
17156 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
17157 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
17158 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 PT;. }. }..
17159 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 /* Assert that
1715a 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 the root page is
1715b 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 of the correct
1715c 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74 20 type. This must
1715d 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 be the. ** case
1715e 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 as the call to
1715f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 this function th
17160 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72 6f at loaded the ro
17161 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 72 0a ot-page (either.
17162 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f ** this call o
17163 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e 76 r a previous inv
17164 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 ocation) would h
17165 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 ave detected cor
17166 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 ruption . ** if
17167 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 the assumption
17168 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 were not true, a
17169 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 nd it is not pos
1716a 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66 6c sible for the fl
1716b 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 ags . ** byte t
1716c 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 o have been modi
1716d 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 73 20 fied while this
1716e 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e cursor is holdin
1716f 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 g a reference.
17170 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20 ** to the page.
17171 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 */. pRoot = pC
17172 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 ur->apPage[0];.
17173 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e assert( pRoot->
17174 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f pgno==pCur->pgno
17175 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 Root );. assert
17176 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 ( pRoot->isInit
17177 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e && (pCur->pKeyIn
17178 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 fo==0)==pRoot->i
17179 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 ntKey );.. pCur
1717a 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a ->aiIdx[0] = 0;.
1717b 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
1717c 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
1717d 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 atLast = 0;. pC
1717e 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
1717f 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 0;.. if( pRoot-
17180 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 >nCell==0 && !pR
17181 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 oot->leaf ){.
17182 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 Pgno subpage;.
17183 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 if( pRoot->pg
17184 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53 no!=1 ) return S
17185 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
17186 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 PT;. subpage
17187 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f = get4byte(&pRoo
17188 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e t->aData[pRoot->
17189 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
1718a 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
1718b 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a = CURSOR_VALID;.
1718c 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
1718d 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61 hild(pCur, subpa
1718e 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ge);. }else{.
1718f 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
17190 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e ((pRoot->nCell>
17191 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0)?CURSOR_VALID:
17192 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b CURSOR_INVALID);
17193 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
17194 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
17195 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 the cursor down
17196 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 to the left-most
17197 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 leaf entry bene
17198 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 ath the.** entry
17199 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 to which it is
1719a 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
1719b 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 ng..**.** The le
1719c 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 ft-most leaf is
1719d 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 the one with the
1719e 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 smallest key -
1719f 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 the first.** in
171a0 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e ascending order.
171a1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
171a2 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 oveToLeftmost(Bt
171a3 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
171a4 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e Pgno pgno;. in
171a5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
171a6 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
171a7 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 ge;.. assert( c
171a8 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
171a9 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
171aa 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
171ab 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
171ac 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 . while( rc==SQ
171ad 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 LITE_OK && !(pPa
171ae 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
171af 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d e[pCur->iPage])-
171b0 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73 >leaf ){. ass
171b1 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
171b2 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 [pCur->iPage]<pP
171b3 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
171b4 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 pgno = get4byt
171b5 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 e(findCell(pPage
171b6 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 , pCur->aiIdx[pC
171b7 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 ur->iPage]));.
171b8 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
171b9 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a ld(pCur, pgno);.
171ba 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
171bb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 .}../*.** Move t
171bc 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 he cursor down t
171bd 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 o the right-most
171be 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 leaf entry bene
171bf 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 ath the.** page
171c0 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 to which it is c
171c1 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
171c2 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64 g. Notice the d
171c3 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 ifference.** bet
171c4 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d ween moveToLeftm
171c5 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f ost() and moveTo
171c6 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f Rightmost(). mo
171c7 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a veToLeftmost().*
171c8 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 * finds the left
171c9 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 -most entry bene
171ca 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 ath the *entry*
171cb 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 whereas moveToRi
171cc 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e ghtmost().** fin
171cd 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 ds the right-mos
171ce 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 t entry beneath
171cf 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a the *page*..**.*
171d0 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 * The right-most
171d1 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e entry is the on
171d2 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 e with the large
171d3 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 st key - the las
171d4 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 t.** key in asce
171d5 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a nding order..*/.
171d6 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 static int moveT
171d7 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72 oRightmost(BtCur
171d8 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 sor *pCur){. Pg
171d9 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 no pgno;. int r
171da 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
171db 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
171dc 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
171dd 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
171de 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
171df 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
171e0 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
171e1 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 ;. while( rc==S
171e2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 QLITE_OK && !(pP
171e3 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
171e4 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
171e5 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 ->leaf ){. pg
171e6 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
171e7 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
171e8 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
171e9 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 ;. pCur->aiId
171ea 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
171eb 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 pPage->nCell;.
171ec 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
171ed 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b ild(pCur, pgno);
171ee 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
171ef 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
171f0 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
171f1 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 ->iPage] = pPage
171f2 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 ->nCell-1;. p
171f3 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
171f4 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 = 0;. pCur->v
171f5 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 alidNKey = 0;.
171f6 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
171f7 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 ../* Move the cu
171f8 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 rsor to the firs
171f9 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 t entry in the t
171fa 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 able. Return SQ
171fb 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 LITE_OK.** on su
171fc 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 ccess. Set *pRe
171fd 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 s to 0 if the cu
171fe 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f rsor actually po
171ff 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e ints to somethin
17200 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 g.** or set *pRe
17201 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 s to 1 if the ta
17202 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f ble is empty..*/
17203 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
17204 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
17205 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a First(BtCursor *
17206 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 pCur, int *pRes)
17207 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 {. int rc;.. a
17208 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
17209 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
1720a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1720b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1720c 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e ur->pBtree->db->
1720d 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
1720e 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
1720f 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
17210 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
17211 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
17212 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 CURSOR_INVALID )
17213 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
17214 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
17215 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
17216 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 ==0 );. *pR
17217 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 es = 1;. rc
17218 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
17219 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
1721a 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
1721b 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
1721c 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 ->nCell>0 );.
1721d 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
1721e 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c rc = moveToL
1721f 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 eftmost(pCur);.
17220 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
17221 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 n rc;.}../* Move
17222 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 the cursor to t
17223 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
17224 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 the table. Ret
17225 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a urn SQLITE_OK.**
17226 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 on success. Se
17227 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 t *pRes to 0 if
17228 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 the cursor actua
17229 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f lly points to so
1722a 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 mething.** or se
1722b 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 t *pRes to 1 if
1722c 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 the table is emp
1722d 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ty..*/.SQLITE_PR
1722e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1722f 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 3BtreeLast(BtCur
17230 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
17231 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b pRes){. int rc;
17232 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 . . assert( cur
17233 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
17234 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
17235 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
17236 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 eld(pCur->pBtree
17237 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
17238 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 . /* If the cur
17239 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 6e sor already poin
1723a 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 ts to the last e
1723b 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 61 20 ntry, this is a
1723c 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 no-op. */. if(
1723d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 CURSOR_VALID==pC
1723e 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70 43 ur->eState && pC
1723f 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 ur->atLast ){.#i
17240 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
17241 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c G. /* This bl
17242 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61 73 ock serves to as
17243 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20 sert() that the
17244 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f cursor really do
17245 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a es point . **
17246 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 to the last ent
17247 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 ry in the b-tree
17248 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b . */. int ii;
17249 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 . for(ii=0; i
1724a 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 i<pCur->iPage; i
1724b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 i++){. asse
1724c 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
1724d 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 ii]==pCur->apPag
1724e 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a e[ii]->nCell );.
1724f 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
17250 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 ( pCur->aiIdx[pC
17251 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 ur->iPage]==pCur
17252 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
17253 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 Page]->nCell-1 )
17254 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
17255 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
17256 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b >iPage]->leaf );
17257 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 .#endif. retu
17258 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
17259 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f }.. rc = moveTo
1725a 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 Root(pCur);. if
1725b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1725c 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f ){. if( CURSO
1725d 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d R_INVALID==pCur-
1725e 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 >eState ){.
1725f 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
17260 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
17261 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a e]->nCell==0 );.
17262 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b *pRes = 1;
17263 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
17264 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
17265 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
17266 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 ALID );. *p
17267 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 Res = 0;. r
17268 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d c = moveToRightm
17269 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 ost(pCur);.
1726a 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 pCur->atLast =
1726b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 rc==SQLITE_OK ?1
1726c 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 :0;. }. }.
1726d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1726e 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
1726f 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e so that it poin
17270 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e ts to an entry n
17271 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 ear the key .**
17272 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 64 specified by pId
17273 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 xKey or intKey.
17274 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 Return a succe
17275 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 ss code..**.** F
17276 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 or INTKEY tables
17277 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 , the intKey par
17278 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 ameter is used.
17279 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 pIdxKey .** mus
1727a 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 t be NULL. For
1727b 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 index tables, pI
1727c 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e dxKey is used an
1727d 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 d intKey.** is i
1727e 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 gnored..**.** If
1727f 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 an exact match
17280 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 is not found, th
17281 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 en the cursor is
17282 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 always.** left
17283 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 pointing at a le
17284 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 6f af page which wo
17285 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 uld hold the ent
17286 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 ry if it.** were
17287 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 present. The c
17288 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e ursor might poin
17289 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 t to an entry th
1728a 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f at comes.** befo
1728b 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 re or after the
1728c 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e key..**.** An in
1728d 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65 6e teger is written
1728e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 into *pRes whic
1728f 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 h is the result
17290 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 of.** comparing
17291 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 the key with the
17292 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 entry to which
17293 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a the cursor is .*
17294 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 * pointing. The
17295 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 meaning of the
17296 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 integer written
17297 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 into.** *pRes is
17298 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
17299 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 ** *pRes<0
1729a 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
1729b 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1729c 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
1729d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1729e 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 is smaller
1729f 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 than intKey/pIdx
172a0 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 Key or if the ta
172a1 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 ble is empty.**
172a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
172a3 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 and the cursor
172a4 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 is therefore lef
172a5 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 t point to nothi
172a6 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 ng..**.** *p
172a7 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 Res==0 The c
172a8 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f ursor is left po
172a9 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 inting at an ent
172aa 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 ry that.**
172ab 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63 exac
172ac 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b tly matches intK
172ad 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a ey/pIdxKey..**.*
172ae 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 * *pRes>0
172af 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 The cursor is
172b0 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 left pointing a
172b1 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a t an entry that.
172b2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
172b3 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 is larger th
172b4 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 an intKey/pIdxKe
172b5 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f y..**.*/.SQLITE_
172b6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
172b7 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
172b8 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 packed(. BtCurs
172b9 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 or *pCur,
172ba 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 /* The cursor
172bb 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a to be moved */.
172bc 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
172bd 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e *pIdxKey, /* Un
172be 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
172bf 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 */. i64 intKey
172c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
172c1 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 * The table key
172c2 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 */. int biasRig
172c3 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ht, /*
172c4 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 If true, bias t
172c5 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 he search to the
172c6 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 high end */. i
172c7 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 nt *pRes
172c8 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
172c9 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 search results
172ca 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
172cb 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
172cc 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
172cd 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
172ce 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
172cf 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 x_held(pCur->pBt
172d0 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 ree->db->mutex)
172d1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 );. assert( pRe
172d2 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 s );. assert( (
172d3 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 pIdxKey==0)==(pC
172d4 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 ur->pKeyInfo==0)
172d5 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
172d6 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 cursor is alrea
172d7 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 dy positioned at
172d8 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 the point we ar
172d9 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f e trying. ** to
172da 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a move to, then j
172db 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f ust return witho
172dc 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 ut doing any wor
172dd 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d k */. if( pCur-
172de 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
172df 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 VALID && pCur->v
172e0 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 alidNKey . &&
172e1 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
172e2 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 >intKey . ){.
172e3 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f if( pCur->info
172e4 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b .nKey==intKey ){
172e5 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 . *pRes = 0
172e6 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
172e7 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
172e8 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 if( pCur->at
172e9 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e Last && pCur->in
172ea 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 fo.nKey<intKey )
172eb 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 {. *pRes =
172ec 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e -1;. return
172ed 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
172ee 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f }. }.. rc = mo
172ef 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a veToRoot(pCur);.
172f0 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
172f1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
172f2 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
172f3 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
172f4 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e] );. assert(
172f5 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
172f6 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 r->iPage]->isIni
172f7 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 t );. assert( p
172f8 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
172f9 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e ->iPage]->nCell>
172fa 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 0 || pCur->eStat
172fb 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 e==CURSOR_INVALI
172fc 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d D );. if( pCur-
172fd 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
172fe 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a INVALID ){. *
172ff 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 pRes = -1;. a
17300 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
17301 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
17302 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 ->nCell==0 );.
17303 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17304 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 OK;. }. assert
17305 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 ( pCur->apPage[0
17306 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 ]->intKey || pId
17307 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b xKey );. for(;;
17308 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 ){. int lwr,
17309 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 upr;. Pgno ch
1730a 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 ldPg;. MemPag
1730b 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d e *pPage = pCur-
1730c 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1730d 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 3b age];. int c;
1730e 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e .. /* pPage->
1730f 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 72 nCell must be gr
17310 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e eater than zero.
17311 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
17312 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a root-page. **
17313 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75 6c the cursor woul
17314 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56 41 d have been INVA
17315 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74 68 LID above and th
17316 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0a is for(;;) loop.
17317 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20 ** not run.
17318 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 If this is not t
17319 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68 he root-page, th
1731a 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 69 en the moveToChi
1731b 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 20 ld() routine.
1731c 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61 ** would have a
1731d 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 64 20 lready detected
1731e 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53 db corruption. S
1731f 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20 imilarly, pPage
17320 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74 must. ** be t
17321 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69 he right kind (i
17322 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f ndex or table) o
17323 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f f b-tree page. O
17324 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a 20 therwise. **
17325 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 a moveToChild()
17326 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 or moveToRoot()
17327 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20 call would have
17328 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 74 detected corrupt
17329 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 ion. */. ass
1732a 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ert( pPage->nCel
1732b 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 l>0 );. asser
1732c 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 t( pPage->intKey
1732d 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 29 ==(pIdxKey==0) )
1732e 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 ;. lwr = 0;.
1732f 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e upr = pPage->
17330 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 nCell-1;. if(
17331 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 20 biasRight ){.
17332 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
17333 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 pCur->iPage] = (
17334 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 6c u16)upr;. }el
17335 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e se{. pCur->
17336 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
17337 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 2b e] = (u16)((upr+
17338 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 lwr)/2);. }.
17339 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 for(;;){.
1733a 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 int idx = pCur
1733b 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
1733c 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 6f age]; /* Index o
1733d 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 f current cell i
1733e 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 20 n pPage */.
1733f 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 u8 *pCell;
17340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17341 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
17342 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 to current cell
17343 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 in pPage */..
17344 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 pCur->info.nS
17345 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 ize = 0;. p
17346 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
17347 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 50 pPage, idx) + pP
17348 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
17349 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 e;. if( pPa
1734a 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
1734b 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b i64 nCellK
1734c 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ey;. if(
1734d 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
1734e 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 {. u32
1734f 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 20 dummy;.
17350 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 72 pCell += getVar
17351 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 6d int32(pCell, dum
17352 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 my);. }.
17353 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 getVarint
17354 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e (pCell, (u64*)&n
17355 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 CellKey);.
17356 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d 3d if( nCellKey==
17357 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
17358 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20 c = 0;.
17359 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 }else if( nCe
1735a 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a llKey<intKey ){.
1735b 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31 c = -1
1735c 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
1735d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
1735e 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b t( nCellKey>intK
1735f 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ey );.
17360 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20 c = +1;.
17361 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e }. pCur->
17362 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 validNKey = 1;.
17363 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 pCur->inf
17364 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 o.nKey = nCellKe
17365 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a y;. }else{.
17366 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d /* The m
17367 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 aximum supported
17368 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 32 page-size is 32
17369 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 20 768 bytes. This
1736a 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 means that.
1736b 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 ** the maximu
1736c 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f m number of reco
1736d 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 rd bytes stored
1736e 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 on an index B-Tr
1736f 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 ee. ** pa
17370 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 31 ge is at most 81
17371 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 20 98 bytes, which
17372 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 may be stored as
17373 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 a 2-byte.
17374 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 ** varint. Thi
17375 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 s information is
17376 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 used to attempt
17377 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e to avoid parsin
17378 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 g . ** th
17379 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 e entire cell by
1737a 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 checking for th
1737b 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74 68 e cases where th
1737c 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 e record is .
1737d 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 ** stored e
1737e 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 ntirely within t
1737f 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 he b-tree page b
17380 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 y inspecting the
17381 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 first .
17382 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 ** 2 bytes of th
17383 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 e cell..
17384 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e */. int n
17385 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d 3b Cell = pCell[0];
17386 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28 6e . if( !(n
17387 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 20 Cell & 0x80) &&
17388 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 nCell<=pPage->ma
17389 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 xLocal ){.
1738a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e /* This bran
1738b 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 ch runs if the r
1738c 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 ecord-size field
1738d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 of the cell is
1738e 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 a. ** s
1738f 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e ingle byte varin
17390 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 t and the record
17391 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f fits entirely o
17392 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 n the main.
17393 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 ** b-tree p
17394 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 age. */.
17395 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 c = sqlite3Vd
17396 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
17397 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 nCell, (void*)&p
17398 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 79 Cell[1], pIdxKey
17399 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
1739a 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20 if( !(pCell[1]
1739b 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 20 & 0x80) .
1739c 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28 && (nCell = (
1739d 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29 (nCell&0x7f)<<7)
1739e 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50 + pCell[1])<=pP
1739f 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20 age->maxLocal.
173a0 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 ){.
173a1 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 /* The record
173a2 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 -size field is a
173a3 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 2 byte varint a
173a4 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a 20 nd the record .
173a5 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 73 ** fits
173a6 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 entirely on the
173a7 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 main b-tree pag
173a8 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 e. */.
173a9 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 c = sqlite3Vdbe
173aa 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 RecordCompare(nC
173ab 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 ell, (void*)&pCe
173ac 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b ll[2], pIdxKey);
173ad 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
173ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
173af 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 record flows ov
173b0 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d er onto one or m
173b1 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ore overflow pag
173b2 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 es. In.
173b3 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 ** this case th
173b4 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 e whole cell nee
173b5 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c ds to be parsed,
173b6 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 a buffer alloca
173b7 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ted. **
173b8 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f and accessPaylo
173b9 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 ad() used to ret
173ba 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 rieve the record
173bb 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 into the.
173bc 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 ** buffer be
173bd 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 fore VdbeRecordC
173be 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 ompare() can be
173bf 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 called. */.
173c0 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c void *pCell
173c1 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 Key;. u
173c2 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 8 * const pCellB
173c3 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 ody = pCell - pP
173c4 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
173c5 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 e;. btr
173c6 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
173c7 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c Page, pCellBody,
173c8 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 &pCur->info);.
173c9 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d nCell =
173ca 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f (int)pCur->info
173cb 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 .nKey;.
173cc 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 pCellKey = sqli
173cd 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c te3Malloc( nCell
173ce 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 );. if
173cf 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b ( pCellKey==0 ){
173d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
173d1 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
173d2 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
173d3 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a moveto_finish;.
173d4 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
173d5 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 rc = acces
173d6 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 sPayload(pCur, 0
173d7 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e , nCell, (unsign
173d8 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 ed char*)pCellKe
173d9 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 y, 0);.
173da 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
173db 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
173dc 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 ree(pCellKey);.
173dd 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
173de 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 moveto_finish;.
173df 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
173e0 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 c = sqlite3
173e1 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 VdbeRecordCompar
173e2 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 e(nCell, pCellKe
173e3 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 y, pIdxKey);.
173e4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
173e5 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 ree(pCellKey);.
173e6 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
173e7 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 . if( c==0
173e8 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
173e9 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 Page->intKey &&
173ea 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
173eb 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 lwr =
173ec 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 idx;. u
173ed 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 pr = lwr - 1;.
173ee 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
173ef 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
173f0 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
173f1 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 0;. rc
173f2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
173f3 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 goto move
173f4 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 to_finish;.
173f5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
173f6 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 if( c<0 ){.
173f7 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b lwr = idx+
173f8 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
173f9 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 upr = id
173fa 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 x-1;. }.
173fb 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 if( lwr>upr )
173fc 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b {. break;
173fd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
173fe 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
173ff 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 >iPage] = (u16)(
17400 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 (lwr+upr)/2);.
17401 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
17402 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 lwr==upr+1 );.
17403 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17404 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 >isInit );. i
17405 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 f( pPage->leaf )
17406 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d {. chldPg =
17407 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
17408 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 ( lwr>=pPage->nC
17409 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c ell ){. chl
1740a 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 dPg = get4byte(&
1740b 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
1740c 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
1740d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
1740e 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 chldPg = get
1740f 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 4byte(findCell(p
17410 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 Page, lwr));.
17411 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 }. if( chldP
17412 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 g==0 ){. as
17413 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
17414 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
17415 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
17416 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 ->iPage]->nCell
17417 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d );. *pRes =
17418 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 c;. rc = S
17419 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1741a 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 goto moveto_fini
1741b 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 sh;. }. pC
1741c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
1741d 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 iPage] = (u16)lw
1741e 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 r;. pCur->inf
1741f 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 o.nSize = 0;.
17420 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
17421 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d = 0;. rc = m
17422 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c oveToChild(pCur,
17423 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 chldPg);. if
17424 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 ( rc ) goto move
17425 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d to_finish;. }.m
17426 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 oveto_finish:.
17427 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
17428 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
17429 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 if the cursor i
1742a 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 s not pointing a
1742b 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 t an entry of th
1742c 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 e table..**.** T
1742d 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 RUE will be retu
1742e 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c rned after a cal
1742f 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
17430 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a eNext() moves.**
17431 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 past the last e
17432 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c ntry in the tabl
17433 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 e or sqlite3Btre
17434 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 ePrev() moves pa
17435 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 st.** the first
17436 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 entry. TRUE is
17437 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 also returned if
17438 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
17439 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pty..*/.SQLITE_P
1743a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1743b 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 e3BtreeEof(BtCur
1743c 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a sor *pCur){. /*
1743d 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 TODO: What if t
1743e 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 he cursor is in
1743f 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
17440 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 EK but all table
17441 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 entries. ** ha
17442 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f ve been deleted?
17443 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e This API will n
17444 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f eed to change to
17445 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
17446 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 code. ** as we
17447 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 ll as the boolea
17448 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a n result value..
17449 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 */. return (C
1744a 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 URSOR_VALID!=pCu
1744b 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f r->eState);.}../
1744c 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 *.** Advance the
1744d 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e cursor to the n
1744e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ext entry in the
1744f 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a database. If.*
17450 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 * successful the
17451 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 n set *pRes=0.
17452 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a If the cursor.**
17453 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 was already poi
17454 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 nting to the las
17455 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 t entry in the d
17456 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a atabase before.*
17457 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * this routine w
17458 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 as called, then
17459 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a set *pRes=1..*/.
1745a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1745b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e nt sqlite3BtreeN
1745c 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ext(BtCursor *pC
1745d 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a ur, int *pRes){.
1745e 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 int rc;. int
1745f 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a idx;. MemPage *
17460 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 pPage;.. assert
17461 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
17462 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
17463 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
17464 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
17465 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
17466 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
17467 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 n rc;. }. asse
17468 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 rt( pRes!=0 );.
17469 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 if( CURSOR_INVA
1746a 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 LID==pCur->eStat
1746b 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d e ){. *pRes =
1746c 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 1;. return S
1746d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
1746e 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 if( pCur->skipNe
1746f 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 xt>0 ){. pCur
17470 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a ->skipNext = 0;.
17471 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
17472 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17473 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d _OK;. }. pCur-
17474 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a >skipNext = 0;..
17475 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e pPage = pCur->
17476 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17477 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 ge];. idx = ++p
17478 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
17479 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 >iPage];. asser
1747a 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 t( pPage->isInit
1747b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 );. assert( id
1747c 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x<=pPage->nCell
1747d 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f );.. pCur->info
1747e 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 .nSize = 0;. pC
1747f 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
17480 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 0;. if( idx>=pP
17481 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 age->nCell ){.
17482 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
17483 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d af ){. rc =
17484 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
17485 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 r, get4byte(&pPa
17486 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
17487 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b >hdrOffset+8]));
17488 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
17489 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1748a 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 rc = moveToLeft
1748b 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
1748c 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
1748d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1748e 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 }. do{.
1748f 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
17490 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
17491 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 *pRes = 1;.
17492 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
17493 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
17494 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
17495 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
17496 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f }. moveTo
17497 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 Parent(pCur);.
17498 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 pPage = pCur
17499 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1749a 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c Page];. }whil
1749b 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 e( pCur->aiIdx[p
1749c 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 Cur->iPage]>=pPa
1749d 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 ge->nCell );.
1749e 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 *pRes = 0;.
1749f 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
174a0 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 y ){. rc =
174a1 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
174a2 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 (pCur, pRes);.
174a3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
174a4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
174a5 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
174a6 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 rc;. }. *pRes
174a7 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 = 0;. if( pPage
174a8 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65 ->leaf ){. re
174a9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
174aa 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 }. rc = moveT
174ab 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b oLeftmost(pCur);
174ac 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
174ad 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65 ../*.** Step the
174ae 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 cursor to the b
174af 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 ack to the previ
174b0 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ous entry in the
174b1 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a database. If.*
174b2 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 * successful the
174b3 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 n set *pRes=0.
174b4 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a If the cursor.**
174b5 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 was already poi
174b6 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72 nting to the fir
174b7 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
174b8 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a database before.
174b9 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
174ba 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e was called, then
174bb 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f set *pRes=1..*/
174bc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
174bd 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
174be 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f Previous(BtCurso
174bf 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 r *pCur, int *pR
174c0 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 es){. int rc;.
174c1 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
174c2 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
174c3 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
174c4 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 r) );. rc = res
174c5 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
174c6 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 on(pCur);. if(
174c7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
174c8 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
174c9 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 }. pCur->atLa
174ca 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 55 st = 0;. if( CU
174cb 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 RSOR_INVALID==pC
174cc 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 ur->eState ){.
174cd 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 *pRes = 1;.
174ce 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
174cf 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 K;. }. if( pCu
174d0 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b r->skipNext<0 ){
174d1 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e . pCur->skipN
174d2 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 ext = 0;. *pR
174d3 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 es = 0;. retu
174d4 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
174d5 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 }. pCur->skipNe
174d6 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 xt = 0;.. pPage
174d7 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
174d8 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
174d9 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
174da 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 sInit );. if( !
174db 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
174dc 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 int idx = pCu
174dd 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
174de 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 Page];. rc =
174df 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
174e0 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 , get4byte(findC
174e1 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 ell(pPage, idx))
174e2 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
174e3 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
174e4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
174e5 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 moveToRightmost
174e6 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b (pCur);. }else{
174e7 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 . while( pCur
174e8 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
174e9 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 age]==0 ){.
174ea 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
174eb 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
174ec 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 Cur->eState = CU
174ed 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 RSOR_INVALID;.
174ee 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b *pRes = 1;
174ef 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
174f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
174f1 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 }. moveToP
174f2 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 arent(pCur);.
174f3 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 }. pCur->inf
174f4 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 o.nSize = 0;.
174f5 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
174f6 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 2d = 0;.. pCur-
174f7 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
174f8 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65 ge]--;. pPage
174f9 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
174fa 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
174fb 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
174fc 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c Key && !pPage->l
174fd 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 eaf ){. rc
174fe 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 = sqlite3BtreePr
174ff 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 evious(pCur, pRe
17500 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 s);. }else{.
17501 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
17502 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 _OK;. }. }.
17503 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 65 *pRes = 0;. re
17504 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
17505 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
17506 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 page from the d
17507 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
17508 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 .** The new page
17509 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 is marked as di
1750a 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 rty. (In other
1750b 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 words, sqlite3Pa
1750c 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 gerWrite().** ha
1750d 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 s already been c
1750e 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 alled on the new
1750f 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 page.) The new
17510 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a page has also.*
17511 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 * been reference
17512 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e d and the callin
17513 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 g routine is res
17514 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c ponsible for cal
17515 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 ling.** sqlite3P
17516 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 agerUnref() on t
17517 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e he new page when
17518 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a it is done..**.
17519 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
1751a 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 returned on succ
1751b 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 ess. Any other
1751c 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 return value ind
1751d 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 icates.** an err
1751e 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64 or. *ppPage and
1751f 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65 *pPgno are unde
17520 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65 fined in the eve
17521 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a nt of an error..
17522 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 ** Do not invoke
17523 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
17524 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20 ef() on *ppPage
17525 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 if an error is r
17526 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
17527 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 f the "nearby" p
17528 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 arameter is not
17529 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c 0, then a (feebl
1752a 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64 e) effort is mad
1752b 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 e to .** locate
1752c 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20 a page close to
1752d 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 the page number
1752e 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 20 "nearby". This
1752f 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 can be used in a
17530 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 n.** attempt to
17531 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 67 keep related pag
17532 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68 es close to each
17533 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 61 other in the da
17534 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 tabase file,.**
17535 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61 which in turn ca
17536 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 n make database
17537 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a access faster..*
17538 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61 *.** If the "exa
17539 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 ct" parameter is
1753a 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20 not 0, and the
1753b 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72 page-number near
1753c 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e by exists .** an
1753d 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 ywhere on the fr
1753e 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74 ee-list, then it
1753f 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 74 is guarenteed t
17540 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 o be returned. T
17541 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 his.** is only u
17542 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75 sed by auto-vacu
17543 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 65 um databases whe
17544 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e n allocating a n
17545 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 ew table..*/.sta
17546 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 tic int allocate
17547 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 BtreePage(. BtS
17548 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d hared *pBt, . M
17549 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c emPage **ppPage,
1754a 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c . Pgno *pPgno,
1754b 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c . Pgno nearby,
1754c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20 . u8 exact.){.
1754d 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 MemPage *pPage1
1754e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 ;. int rc;. u3
1754f 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 2 n; /* Numb
17550 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 er of pages on t
17551 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 he freelist */.
17552 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e u32 k; /* N
17553 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20 umber of leaves
17554 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20 on the trunk of
17555 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a the freelist */.
17556 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e MemPage *pTrun
17557 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 k = 0;. MemPage
17558 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 *pPrevTrunk = 0
17559 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b ;. Pgno mxPage;
1755a 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 /* Total si
1755b 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
1755c 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 se file */.. as
1755d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1755e 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
1755f 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 tex) );. pPage1
17560 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a = pBt->pPage1;.
17561 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65 72 mxPage = pager
17562 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
17563 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 n = get4byte(&
17564 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
17565 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 ]);. testcase(
17566 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 n==mxPage-1 );.
17567 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 if( n>=mxPage )
17568 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
17569 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1756a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 ;. }. if( n>0
1756b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 ){. /* There
1756c 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 are pages on the
1756d 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 freelist. Reus
1756e 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 e one of those p
1756f 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e ages. */. Pgn
17570 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 o iTrunk;. u8
17571 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b searchList = 0;
17572 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d /* If the free-
17573 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 list must be sea
17574 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 rched for 'nearb
17575 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f y' */. . /
17576 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 * If the 'exact'
17577 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 parameter was t
17578 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 rue and a query
17579 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d of the pointer-m
1757a 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 ap. ** shows
1757b 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e that the page 'n
1757c 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 earby' is somewh
1757d 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d ere on the free-
1757e 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a list, then. *
1757f 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 * the entire-lis
17580 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 t will be search
17581 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 ed for that page
17582 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 .. */.#ifndef
17583 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
17584 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
17585 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c exact && nearby<
17586 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 =mxPage ){.
17587 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 u8 eType;.
17588 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e assert( nearby>
17589 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
1758a 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 t( pBt->autoVacu
1758b 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d um );. rc =
1758c 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
1758d 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 nearby, &eType,
1758e 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
1758f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
17590 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
17591 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
17592 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 {. search
17593 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 List = 1;.
17594 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d }. *pPgno =
17595 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 nearby;. }.#
17596 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 endif.. /* De
17597 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 crement the free
17598 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 -list count by 1
17599 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 . Set iTrunk to
1759a 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
1759b 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 . ** first fr
1759c 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 ee-list trunk pa
1759d 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 ge. iPrevTrunk i
1759e 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 s initially 1..
1759f 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
175a0 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
175a1 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 (pPage1->pDbPage
175a2 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
175a3 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 return rc;. p
175a4 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d ut4byte(&pPage1-
175a5 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 >aData[36], n-1)
175a6 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f ;.. /* The co
175a7 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c de within this l
175a8 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 oop is run only
175a9 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65 61 once if the 'sea
175aa 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c rchList' variabl
175ab 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 e. ** is not
175ac 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c true. Otherwise,
175ad 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f it runs once fo
175ae 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 r each trunk-pag
175af 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 e on the. **
175b0 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 free-list until
175b1 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 the page 'nearby
175b2 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 ' is located..
175b3 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 */. do {.
175b4 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d pPrevTrunk =
175b5 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 pTrunk;. i
175b6 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b f( pPrevTrunk ){
175b7 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 . iTrunk
175b8 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72 65 = get4byte(&pPre
175b9 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d vTrunk->aData[0]
175ba 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
175bb 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d iTrunk =
175bc 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
175bd 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 1->aData[32]);.
175be 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73 }. tes
175bf 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d tcase( iTrunk==m
175c0 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 69 xPage );. i
175c1 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65 f( iTrunk>mxPage
175c2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
175c3 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
175c4 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 BKPT;. }els
175c5 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
175c6 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 btreeGetPage(pBt
175c7 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e , iTrunk, &pTrun
175c8 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 k, 0);. }.
175c9 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
175ca 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 pTrunk =
175cb 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 0;. goto
175cc 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
175cd 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 e;. }..
175ce 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 k = get4byte(&
175cf 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d pTrunk->aData[4]
175d0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d );. if( k==
175d1 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 0 && !searchList
175d2 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 ){. /* T
175d3 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 he trunk has no
175d4 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c leaves and the l
175d5 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 ist is not being
175d6 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 searched. .
175d7 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 ** So extrac
175d8 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 t the trunk page
175d9 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 itself and use
175da 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 it as the newly
175db 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f . ** allo
175dc 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 cated page */.
175dd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
175de 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 revTrunk==0 );.
175df 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
175e0 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 te3PagerWrite(pT
175e1 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
175e2 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
175e3 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f {. goto
175e4 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
175e5 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ge;. }.
175e6 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 *pPgno = i
175e7 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d Trunk;. m
175e8 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 emcpy(&pPage1->a
175e9 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e Data[32], &pTrun
175ea 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b k->aData[0], 4);
175eb 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 . *ppPage
175ec 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 = pTrunk;.
175ed 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 pTrunk = 0;.
175ee 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 TRACE(("A
175ef 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e LLOCATE: %d trun
175f0 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 k - %d free page
175f1 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e s left\n", *pPgn
175f2 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 o, n-1));.
175f3 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 32 }else if( k>(u32
175f4 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a )(pBt->usableSiz
175f5 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 20 e/4 - 2) ){.
175f6 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 /* Value of
175f7 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 k is out of rang
175f8 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 e. Database cor
175f9 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 ruption */.
175fa 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
175fb 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
175fc 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c goto end_al
175fd 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 locate_page;.#if
175fe 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
175ff 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
17600 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 }else if( sear
17601 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 chList && nearby
17602 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 ==iTrunk ){.
17603 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 /* The list
17604 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 is being searche
17605 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b d and this trunk
17606 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 page is the pag
17607 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 e. ** to
17608 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 allocate, regard
17609 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 less of whether
1760a 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 it has leaves..
1760b 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
1760c 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f assert( *pPgno
1760d 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 ==iTrunk );.
1760e 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 *ppPage = pT
1760f 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 runk;. se
17610 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 archList = 0;.
17611 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
17612 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 e3PagerWrite(pTr
17613 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 unk->pDbPage);.
17614 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b if( rc ){
17615 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
17616 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
17617 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
17618 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b if( k==0 ){
17619 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 . if( !
1761a 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 pPrevTrunk ){.
1761b 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
1761c 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
1761d 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 32], &pTrunk->aD
1761e 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 ata[0], 4);.
1761f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
17620 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
17621 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
17622 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e ta[0], &pTrunk->
17623 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
17624 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17625 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
17626 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 /* The trunk
17627 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 page is required
17628 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 by the caller b
17629 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a ut it contains .
1762a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 ** poi
1762b 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 nters to free-li
1762c 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 st leaves. The f
1762d 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 irst leaf become
1762e 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 s a trunk.
1762f 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 ** page in t
17630 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 his case..
17631 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
17632 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 MemPage *pNewTr
17633 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 unk;. P
17634 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 gno iNewTrunk =
17635 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b get4byte(&pTrunk
17636 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 ->aData[8]);.
17637 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 if( iNewT
17638 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a runk>mxPage ){ .
17639 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
1763a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1763b 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 BKPT;.
1763c 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 goto end_alloc
1763d 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 ate_page;.
1763e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1763f 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 testcase( iNewTr
17640 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 unk==mxPage );.
17641 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 rc = bt
17642 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
17643 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 iNewTrunk, &pNew
17644 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 Trunk, 0);.
17645 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
17646 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17647 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
17648 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 llocate_page;.
17649 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1764a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1764b 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 PagerWrite(pNewT
1764c 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
1764d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
1764e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1764f 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 relea
17650 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b sePage(pNewTrunk
17651 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 );. g
17652 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
17653 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 _page;.
17654 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d }. mem
17655 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e cpy(&pNewTrunk->
17656 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e aData[0], &pTrun
17657 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b k->aData[0], 4);
17658 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 . put4b
17659 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e yte(&pNewTrunk->
1765a 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a aData[4], k-1);.
1765b 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
1765c 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 (&pNewTrunk->aDa
1765d 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e ta[8], &pTrunk->
1765e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 aData[12], (k-1)
1765f 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 *4);. r
17660 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 eleasePage(pNewT
17661 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 runk);.
17662 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b if( !pPrevTrunk
17663 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
17664 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
17665 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
17666 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
17667 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
17668 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 put4byte(&pPage1
17669 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 ->aData[32], iNe
1766a 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 wTrunk);.
1766b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1766c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1766d 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 72 e3PagerWrite(pPr
1766e 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 evTrunk->pDbPage
1766f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 );. i
17670 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
17671 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
17672 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
17673 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
17674 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 put4byt
17675 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 e(&pPrevTrunk->a
17676 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 Data[0], iNewTru
17677 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d nk);. }
17678 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
17679 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 pTrunk = 0;.
1767a 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 TRACE(("A
1767b 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e LLOCATE: %d trun
1767c 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 k - %d free page
1767d 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e s left\n", *pPgn
1767e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 o, n-1));.#endif
1767f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
17680 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 k>0 ){.
17681 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 /* Extract a lea
17682 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b f from the trunk
17683 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 */. u32
17684 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 closest;.
17685 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 Pgno iPage;.
17686 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
17687 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 ar *aData = pTru
17688 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 nk->aData;.
17689 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1768a 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b agerWrite(pTrunk
1768b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
1768c 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
1768d 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
1768e 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
1768f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17690 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 if( nearby>0 )
17691 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 {. u32
17692 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 i;. int
17693 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 dist;.
17694 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 closest = 0;.
17695 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 dist = g
17696 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 et4byte(&aData[8
17697 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 ]) - nearby;.
17698 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74 3c if( dist<
17699 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 0 ) dist = -dist
1769a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 ;. for(
1769b 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a i=1; i<k; i++){.
1769c 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
1769d 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 d2 = get4byte(&a
1769e 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e Data[8+i*4]) - n
1769f 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 earby;.
176a0 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 if( d2<0 ) d2
176a1 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 = -d2;.
176a2 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 if( d2<dist
176a3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
176a4 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 closest = i;.
176a5 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 dist
176a6 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 = d2;.
176a7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
176a8 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
176a9 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 closes
176aa 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d t = 0;. }
176ab 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20 .. iPage
176ac 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 = get4byte(&aDat
176ad 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b a[8+closest*4]);
176ae 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
176af 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 e( iPage==mxPage
176b0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
176b1 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a iPage>mxPage ){.
176b2 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
176b3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
176b4 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
176b5 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
176b6 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a page;. }.
176b7 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
176b8 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 ( iPage==mxPage
176b9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 );. if( !
176ba 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 searchList || iP
176bb 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 age==nearby ){.
176bc 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43 int noC
176bd 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 ontent;.
176be 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 *pPgno = iPage
176bf 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 43 ;. TRAC
176c0 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 E(("ALLOCATE: %d
176c1 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20 was leaf %d of
176c2 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a %d on trunk %d".
176c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176c4 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65 ": %d more free
176c5 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 pages\n",.
176c6 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 *pPg
176c7 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b no, closest+1, k
176c8 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 , pTrunk->pgno,
176c9 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 n-1));.
176ca 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 if( closest<k-1
176cb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
176cc 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b memcpy(&aData[8+
176cd 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 closest*4], &aDa
176ce 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 ta[4+k*4], 4);.
176cf 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
176d0 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 61 put4byte(&a
176d1 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 Data[4], k-1);.
176d2 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 assert(
176d3 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 sqlite3PagerIsw
176d4 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b 2d riteable(pTrunk-
176d5 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 >pDbPage) );.
176d6 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 noContent
176d7 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 43 = !btreeGetHasC
176d8 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67 ontent(pBt, *pPg
176d9 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 no);. r
176da 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
176db 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 (pBt, *pPgno, pp
176dc 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 Page, noContent)
176dd 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
176de 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
176df 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
176e0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
176e1 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 ite((*ppPage)->p
176e2 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
176e3 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
176e4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
176e5 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
176e6 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 age(*ppPage);.
176e7 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
176e8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
176e9 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 searchList = 0
176ea 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
176eb 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 }. releas
176ec 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b ePage(pPrevTrunk
176ed 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 );. pPrevTr
176ee 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 unk = 0;. }wh
176ef 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 ile( searchList
176f0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
176f1 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 /* There are no
176f2 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 pages on the fre
176f3 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 elist, so create
176f4 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 a new page at t
176f5 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 he. ** end of
176f6 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 the file */.
176f7 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 int nPage = pag
176f8 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
176f9 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e ;. *pPgno = n
176fa 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 Page + 1;.. i
176fb 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 f( *pPgno==PENDI
176fc 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
176fd 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 ) ){. (*pPg
176fe 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 no)++;. }..#i
176ff 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
17700 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
17701 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
17702 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 cuum && PTRMAP_I
17703 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e SPAGE(pBt, *pPgn
17704 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 o) ){. /* I
17705 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 f *pPgno refers
17706 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 to a pointer-map
17707 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 page, allocate
17708 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 two new pages.
17709 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e ** at the en
1770a 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e d of the file in
1770b 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 stead of one. Th
1770c 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 e first allocate
1770d 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 d page. **
1770e 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f becomes a new po
1770f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 inter-map page,
17710 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 the second is us
17711 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ed by the caller
17712 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
17713 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 MemPage *pPg =
17714 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 0;. TRACE((
17715 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 "ALLOCATE: %d fr
17716 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 om end of file (
17717 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
17718 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a )\n", *pPgno));.
17719 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 assert( *p
1771a 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
1771b 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
1771c 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
1771d 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 GetPage(pBt, *pP
1771e 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 gno, &pPg, 0);.
1771f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
17720 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
17721 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
17722 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 gerWrite(pPg->pD
17723 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
17724 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 releasePage(pPg)
17725 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17726 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
17727 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e rc;. (*pPgn
17728 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 o)++;. if(
17729 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f *pPgno==PENDING_
1772a 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
1772b 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a { (*pPgno)++; }.
1772c 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1772d 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f assert( *pPgno
1772e 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
1772f 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 AGE(pBt) );.
17730 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
17731 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 e(pBt, *pPgno, p
17732 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 pPage, 0);. i
17733 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
17734 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 c;. rc = sqli
17735 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a te3PagerWrite((*
17736 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 ppPage)->pDbPage
17737 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
17738 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17739 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 releasePage(*p
1773a 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 pPage);. }.
1773b 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 TRACE(("ALLOCA
1773c 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 TE: %d from end
1773d 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 of file\n", *pPg
1773e 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 no));. }.. ass
1773f 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e ert( *pPgno!=PEN
17740 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
17741 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f Bt) );..end_allo
17742 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c cate_page:. rel
17743 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 easePage(pTrunk)
17744 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 ;. releasePage(
17745 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 pPrevTrunk);. i
17746 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17747 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
17748 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
17749 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e ount((*ppPage)->
1774a 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 pDbPage)>1 ){.
1774b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1774c 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 *ppPage);.
1774d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1774e 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
1774f 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d }. (*ppPage)-
17750 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d >isInit = 0;. }
17751 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 67 else{. *ppPag
17752 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
17753 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
17754 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
17755 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 s used to add pa
17756 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 ge iPage to the
17757 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 database file fr
17758 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 ee-list. .** It
17759 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
1775a 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
1775b 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f already a part o
1775c 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e f the free-list.
1775d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
1775e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
1775f 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
17760 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
17761 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 is optional..**
17762 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 If the caller ha
17763 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 ppens to have a
17764 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d pointer to the M
17765 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a emPage object .*
17766 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
17767 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61 to page iPage ha
17768 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 ndy, it may pass
17769 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e it as the secon
1776a 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 d value. .** Oth
1776b 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 erwise, it may p
1776c 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 ass NULL..**.**
1776d 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 If a pointer to
1776e 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 a MemPage object
1776f 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
17770 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
17771 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 t,.** its refere
17772 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 nce count is not
17773 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 altered by this
17774 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
17775 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 atic int freePag
17776 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 e2(BtShared *pBt
17777 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 , MemPage *pMemP
17778 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 age, Pgno iPage)
17779 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 {. MemPage *pTr
1777a 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 unk = 0;
1777b 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d /* Free-
1777c 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 list trunk page
1777d 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b */. Pgno iTrunk
1777e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1777f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
17780 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d number of free-
17781 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 list trunk page
17782 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 */ . MemPage *p
17783 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 Page1 = pBt->pPa
17784 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 ge1; /* Loc
17785 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 al reference to
17786 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 page 1 */. MemP
17787 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 age *pPage;
17788 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17789 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 /* Page being fr
1778a 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c eed. May be NULL
1778b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 . */. int rc;
1778c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1778d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1778e 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 turn Code */. i
1778f 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 nt nFree;
17790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17791 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 /* Initial nu
17792 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e mber of pages on
17793 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 free-list */..
17794 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17795 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
17796 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
17797 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a ert( iPage>1 );.
17798 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 assert( !pMemP
17799 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d age || pMemPage-
1779a 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a >pgno==iPage );.
1779b 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 . if( pMemPage
1779c 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 ){. pPage = p
1779d 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c MemPage;. sql
1779e 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61 ite3PagerRef(pPa
1779f 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ge->pDbPage);.
177a0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 }else{. pPage
177a1 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b = btreePageLook
177a2 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a up(pBt, iPage);.
177a3 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d }.. /* Increm
177a4 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67 ent the free pag
177a5 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 e count on pPage
177a6 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 1 */. rc = sqli
177a7 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
177a8 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a age1->pDbPage);.
177a9 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
177aa 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
177ab 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 nFree = get4byte
177ac 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
177ad 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 36]);. put4byte
177ae 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
177af 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 36], nFree+1);..
177b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 #ifdef SQLITE_SE
177b1 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a CURE_DELETE. /*
177b2 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 If the SQLITE_S
177b3 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d ECURE_DELETE com
177b4 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
177b5 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 is enabled, the
177b6 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 n. ** always fu
177b7 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 lly overwrite de
177b8 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f leted informatio
177b9 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 n with zeros..
177ba 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 65 */. if( (!pPage
177bb 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 65 47 && (rc = btreeG
177bc 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 etPage(pBt, iPag
177bd 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29 0a e, &pPage, 0))).
177be 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 20 ||
177bf 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 (rc = sqlite3Pa
177c0 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e gerWrite(pPage->
177c1 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a 20 pDbPage)). ){.
177c2 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 goto freepage
177c3 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 _out;. }. mems
177c4 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c et(pPage->aData,
177c5 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 0, pPage->pBt->
177c6 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 pageSize);.#endi
177c7 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 f.. /* If the d
177c8 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 atabase supports
177c9 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 auto-vacuum, wr
177ca 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 ite an entry in
177cb 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a the pointer-map.
177cc 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 ** to indicate
177cd 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 that the page i
177ce 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 s free.. */. i
177cf 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 f( ISAUTOVACUUM
177d0 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 ){. ptrmapPut
177d1 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 (pBt, iPage, PTR
177d2 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c MAP_FREEPAGE, 0,
177d3 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 &rc);. if( r
177d4 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 c ) goto freepag
177d5 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a e_out;. }.. /*
177d6 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 Now manipulate
177d7 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 the actual datab
177d8 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 ase free-list st
177d9 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 ructure. There a
177da 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 re two. ** poss
177db 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 ibilities. If th
177dc 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 e free-list is c
177dd 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 urrently empty,
177de 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74 0a or if the first.
177df 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 ** trunk page
177e0 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 in the free-list
177e1 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 is full, then t
177e2 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 his page will be
177e3 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 come a. ** new
177e4 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 free-list trunk
177e5 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c page. Otherwise,
177e6 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 it will become
177e7 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 a leaf of the.
177e8 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 ** first trunk p
177e9 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 age in the curre
177ea 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 nt free-list. Th
177eb 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 is block tests i
177ec 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 f it. ** is pos
177ed 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 sible to add the
177ee 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 page as a new f
177ef 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 ree-list leaf..
177f0 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 */. if( nFree!
177f1 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c =0 ){. u32 nL
177f2 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 eaf;
177f3 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
177f4 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 umber of leaf ce
177f5 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 lls on trunk pag
177f6 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b e */.. iTrunk
177f7 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
177f8 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b ge1->aData[32]);
177f9 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
177fa 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 etPage(pBt, iTru
177fb 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b nk, &pTrunk, 0);
177fc 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
177fd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
177fe 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 goto freepage_ou
177ff 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c t;. }.. nL
17800 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 eaf = get4byte(&
17801 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d pTrunk->aData[4]
17802 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
17803 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 Bt->usableSize>3
17804 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 2 );. if( nLe
17805 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 af > (u32)pBt->u
17806 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 sableSize/4 - 2
17807 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
17808 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
17809 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 T;. goto fr
1780a 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 eepage_out;.
1780b 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 }. if( nLeaf
1780c 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 < (u32)pBt->usab
1780d 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a leSize/4 - 8 ){.
1780e 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 /* In this
1780f 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 72 case there is r
17810 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b oom on the trunk
17811 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 page to insert
17812 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a the page. *
17813 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 * being freed as
17814 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 a new leaf..
17815 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e **. ** N
17816 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75 ote that the tru
17817 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 nk page is not r
17818 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c eally full until
17819 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 it contains.
1781a 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 ** usableSize
1781b 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 /4 - 2 entries,
1781c 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 not usableSize/4
1781d 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 - 8 entries as
1781e 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a we have. **
1781f 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 coded. But due
17820 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 to a coding err
17821 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f or in versions o
17822 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 f SQLite prior t
17823 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 o. ** 3.6.0
17824 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 , databases with
17825 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 freelist trunk
17826 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f pages holding mo
17827 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a re than. **
17828 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
17829 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 8 entries will b
1782a 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f e reported as co
1782b 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 rrupt. In order
1782c 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 . ** to mai
1782d 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 ntain backwards
1782e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 compatibility wi
1782f 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e th older version
17830 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 s of SQLite,.
17831 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f ** we will co
17832 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 ntinue to restri
17833 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ct the number of
17834 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 entries to usab
17835 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 leSize/4 - 8.
17836 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 ** for now.
17837 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e At some point in
17838 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 the future (onc
17839 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 e everyone has u
1783a 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a pgraded. **
1783b 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 to 3.6.0 or lat
1783c 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f er) we should co
1783d 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 nsider fixing th
1783e 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 e conditional ab
1783f 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 ove. ** to
17840 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 read "usableSize
17841 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 /4-2" instead of
17842 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 "usableSize/4-8
17843 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ".. */.
17844 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
17845 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d gerWrite(pTrunk-
17846 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
17847 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
17848 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 OK ){. pu
17849 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
1784a 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b aData[4], nLeaf+
1784b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 1);. put4
1784c 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 byte(&pTrunk->aD
1784d 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 ata[8+nLeaf*4],
1784e 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 iPage);.#ifndef
1784f 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 SQLITE_SECURE_DE
17850 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 66 28 LETE. if(
17851 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 pPage ){.
17852 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
17853 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d DontWrite(pPage-
17854 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
17855 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
17856 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 53 65 rc = btreeSe
17857 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c tHasContent(pBt,
17858 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d iPage);. }
17859 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 46 . TRACE(("F
1785a 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 REE-PAGE: %d lea
1785b 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 f on trunk page
1785c 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e %d\n",pPage->pgn
1785d 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 o,pTrunk->pgno))
1785e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 ;. goto fre
1785f 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d epage_out;. }
17860 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f . }.. /* If co
17861 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 ntrol flows to t
17862 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 his point, then
17863 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 69 it was not possi
17864 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a 20 ble to add the.
17865 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65 69 ** the page bei
17866 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6c 65 ng freed as a le
17867 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 af page of the f
17868 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 irst trunk in th
17869 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a e free-list.. *
1786a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61 75 * Possibly becau
1786b 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 se the free-list
1786c 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f is empty, or po
1786d 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 ssibly because t
1786e 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 he . ** first t
1786f 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 runk in the free
17870 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 45 -list is full. E
17871 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 70 ither way, the p
17872 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 0a age being freed.
17873 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 ** will become
17874 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 20 74 the new first t
17875 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 runk page in the
17876 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f free-list.. */
17877 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20 . if( pPage==0
17878 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 && SQLITE_OK!=(r
17879 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
1787a 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 (pBt, iPage, &pP
1787b 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 age, 0)) ){.
1787c 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 goto freepage_ou
1787d 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 t;. }. rc = sq
1787e 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1787f 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
17880 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
17881 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f E_OK ){. goto
17882 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
17883 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 }. put4byte(pP
17884 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 age->aData, iTru
17885 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 nk);. put4byte(
17886 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d &pPage->aData[4]
17887 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 , 0);. put4byte
17888 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
17889 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 32], iPage);. T
1788a 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 RACE(("FREE-PAGE
1788b 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 : %d new trunk p
1788c 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 age replacing %d
1788d 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f \n", pPage->pgno
1788e 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 , iTrunk));..fre
1788f 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 epage_out:. if(
17890 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 pPage ){. pP
17891 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b age->isInit = 0;
17892 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 . }. releasePa
17893 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c ge(pPage);. rel
17894 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 easePage(pTrunk)
17895 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
17896 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
17897 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 ePage(MemPage *p
17898 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b Page, int *pRC){
17899 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 . if( (*pRC)==S
1789a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1789b 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 65 32 *pRC = freePage2
1789c 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 61 (pPage->pBt, pPa
1789d 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 ge, pPage->pgno)
1789e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 ;. }.}../*.** F
1789f 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 ree any overflow
178a0 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 pages associate
178a1 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e d with the given
178a2 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 Cell..*/.static
178a3 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d int clearCell(M
178a4 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
178a5 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 nsigned char *pC
178a6 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 ell){. BtShared
178a7 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 *pBt = pPage->p
178a8 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 Bt;. CellInfo i
178a9 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c nfo;. Pgno ovfl
178aa 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Pgno;. int rc;.
178ab 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75 int nOvfl;. u
178ac 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 16 ovflPageSize;
178ad 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
178ae 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
178af 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
178b0 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 ) );. btreePars
178b1 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 eCellPtr(pPage,
178b2 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 pCell, &info);.
178b3 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 if( info.iOverf
178b4 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 low==0 ){. re
178b5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
178b6 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 /* No overflow
178b7 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69 pages. Return wi
178b8 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 thout doing anyt
178b9 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 hing */. }. ov
178ba 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 flPgno = get4byt
178bb 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f e(&pCell[info.iO
178bc 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 verflow]);. ass
178bd 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 ert( pBt->usable
178be 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76 Size > 4 );. ov
178bf 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 flPageSize = pBt
178c0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 ->usableSize - 4
178c1 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 ;. nOvfl = (inf
178c2 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 o.nPayload - inf
178c3 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 o.nLocal + ovflP
178c4 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 ageSize - 1)/ovf
178c5 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 lPageSize;. ass
178c6 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 ert( ovflPgno==0
178c7 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 || nOvfl>0 );.
178c8 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 while( nOvfl--
178c9 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 ){. Pgno iNex
178ca 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 t = 0;. MemPa
178cb 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 ge *pOvfl = 0;.
178cc 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c if( ovflPgno<
178cd 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 2 || ovflPgno>pa
178ce 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
178cf 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 ) ){. /* 0
178d0 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 is not a legal p
178d1 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 age number and p
178d2 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 age 1 cannot be
178d3 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 an . ** ove
178d4 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 rflow page. Ther
178d5 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e efore if ovflPgn
178d6 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 o<2 or past the
178d7 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 end of the .
178d8 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 ** file the da
178d9 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 tabase must be c
178da 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 orrupt. */.
178db 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
178dc 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
178dd 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c }. if( nOvfl
178de 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 ){. rc = g
178df 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 etOverflowPage(p
178e0 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 Bt, ovflPgno, &p
178e1 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 Ovfl, &iNext);.
178e2 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
178e3 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
178e4 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 rc = freePage
178e5 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 2(pBt, pOvfl, ov
178e6 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 flPgno);. if(
178e7 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 pOvfl ){.
178e8 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
178e9 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 f(pOvfl->pDbPage
178ea 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
178eb 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
178ec 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 . ovflPgno =
178ed 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 iNext;. }. ret
178ee 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
178ef 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 ../*.** Create t
178f0 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 he byte sequence
178f1 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 used to represe
178f2 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 nt a cell on pag
178f3 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 e pPage.** and w
178f4 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73 rite that byte s
178f5 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 equence into pCe
178f6 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 ll[]. Overflow
178f7 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c pages are.** all
178f8 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 ocated and fille
178f9 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 d in as necessar
178fa 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 y. The calling
178fb 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 procedure.** is
178fc 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
178fd 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 making sure suff
178fe 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 icient space has
178ff 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a been allocated.
17900 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a ** for pCell[]..
17901 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
17902 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e pCell does not n
17903 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f ecessary need to
17904 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 point to the pP
17905 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 age->aData.** ar
17906 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 ea. pCell might
17907 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 point to some t
17908 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 emporary storage
17909 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c . The cell will
1790a 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 .** be construct
1790b 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f ed in this tempo
1790c 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 rary area then c
1790d 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 opied into pPage
1790e 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 ->aData.** later
1790f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
17910 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 fillInCell(. Me
17911 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 mPage *pPage,
17912 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17913 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f The page that co
17914 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 ntains the cell
17915 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
17916 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 ar *pCell,
17917 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 /* Complete
17918 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c text of the cell
17919 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1791a 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 *pKey, i64 nKey
1791b 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 , /* The key
1791c 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1791d 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 *pData,int nData
1791e 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 , /* The data
1791f 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 */. int nZero,
17920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17921 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 /* Extra zer
17922 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e o bytes to appen
17923 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 d to pData */.
17924 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 int *pnSize
17925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17926 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a * Write cell siz
17927 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 e here */.){. i
17928 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 nt nPayload;. c
17929 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 onst u8 *pSrc;.
1792a 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 int nSrc, n, rc
1792b 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 ;. int spaceLef
1792c 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f t;. MemPage *pO
1792d 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 vfl = 0;. MemPa
1792e 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d ge *pToRelease =
1792f 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 0;. unsigned c
17930 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 har *pPrior;. u
17931 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 nsigned char *pP
17932 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 ayload;. BtShar
17933 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d ed *pBt = pPage-
17934 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e >pBt;. Pgno pgn
17935 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 oOvfl = 0;. int
17936 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c nHeader;. Cell
17937 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 Info info;.. as
17938 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17939 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1793a 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a pBt->mutex) );..
1793b 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f /* pPage is no
1793c 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 t necessarily wr
1793d 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 iteable since pC
1793e 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 ell might be aux
1793f 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 iliary. ** buff
17940 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 er space that is
17941 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 separate from t
17942 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 he pPage buffer
17943 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 area */. assert
17944 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 ( pCell<pPage->a
17945 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 Data || pCell>=&
17946 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 pPage->aData[pBt
17947 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 ->pageSize].
17948 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 || sqlit
17949 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
1794a 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
1794b 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c e) );.. /* Fill
1794c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 in the header.
1794d 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 */. nHeader = 0
1794e 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e ;. if( !pPage->
1794f 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 leaf ){. nHea
17950 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 der += 4;. }.
17951 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
17952 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 ta ){. nHeade
17953 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 r += putVarint(&
17954 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 pCell[nHeader],
17955 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 nData+nZero);.
17956 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 }else{. nData
17957 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 = nZero = 0;.
17958 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 }. nHeader += p
17959 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b utVarint(&pCell[
1795a 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a nHeader], *(u64*
1795b 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 )&nKey);. btree
1795c 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 ParseCellPtr(pPa
1795d 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f ge, pCell, &info
1795e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 );. assert( inf
1795f 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 o.nHeader==nHead
17960 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 er );. assert(
17961 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 info.nKey==nKey
17962 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 );. assert( inf
17963 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e o.nData==(u32)(n
17964 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 Data+nZero) );.
17965 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 . /* Fill in t
17966 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 he payload */.
17967 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 nPayload = nData
17968 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 + nZero;. if(
17969 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
1796a 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 . pSrc = pDat
1796b 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 a;. nSrc = nD
1796c 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d ata;. nData =
1796d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 0;. }else{ .
1796e 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 if( NEVER(nKey
1796f 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 >0x7fffffff || p
17970 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 Key==0) ){.
17971 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
17972 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
17973 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 }. nPayload
17974 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 += (int)nKey;.
17975 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 pSrc = pKey;.
17976 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e nSrc = (int)n
17977 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 Key;. }. *pnSi
17978 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b ze = info.nSize;
17979 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 . spaceLeft = i
1797a 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 nfo.nLocal;. pP
1797b 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b ayload = &pCell[
1797c 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 nHeader];. pPri
1797d 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f or = &pCell[info
1797e 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 .iOverflow];..
1797f 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e while( nPayload>
17980 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 0 ){. if( spa
17981 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 ceLeft==0 ){.#if
17982 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
17983 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
17984 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 Pgno pgnoPtrma
17985 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a p = pgnoOvfl; /*
17986 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 Overflow page p
17987 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 ointer-map entry
17988 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 page */. i
17989 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
1798a 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f um ){. do
1798b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f {. pgno
1798c 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 Ovfl++;.
1798d 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 } while( .
1798e 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 PTRMAP_ISPAG
1798f 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 E(pBt, pgnoOvfl)
17990 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 || pgnoOvfl==PE
17991 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
17992 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b pBt) . );
17993 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
17994 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 rc = alloc
17995 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
17996 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f , &pOvfl, &pgnoO
17997 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 vfl, pgnoOvfl, 0
17998 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
17999 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1799a 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 M. /* If th
1799b 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
1799c 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c rts auto-vacuum,
1799d 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
1799e 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 or subsequent.
1799f 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 ** overflow
179a0 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c page is being al
179a1 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 located, add an
179a2 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 entry to the poi
179a3 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a nter-map. *
179a4 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 * for that page
179a5 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 now. . **.
179a6 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 ** If this
179a7 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 is the first ove
179a8 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e rflow page, then
179a9 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c write a partial
179aa 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a entry . **
179ab 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d to the pointer-
179ac 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 map. If we write
179ad 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 nothing to this
179ae 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f pointer-map slo
179af 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e t,. ** then
179b0 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 the optimistic
179b1 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 overflow chain p
179b2 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 rocessing in cle
179b3 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a arCell(). *
179b4 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 * may misinterpr
179b5 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c et the uninitial
179b6 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 ised values and
179b7 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 delete the.
179b8 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 ** wrong pages
179b9 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
179ba 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
179bb 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
179bc 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c acuum && rc==SQL
179bd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
179be 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 u8 eType = (pg
179bf 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f noPtrmap?PTRMAP_
179c0 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 OVERFLOW2:PTRMAP
179c1 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 _OVERFLOW1);.
179c2 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
179c3 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 Bt, pgnoOvfl, eT
179c4 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c ype, pgnoPtrmap,
179c5 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 &rc);. i
179c6 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
179c7 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
179c8 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d Ovfl);. }
179c9 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
179ca 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
179cb 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
179cc 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b age(pToRelease);
179cd 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
179ce 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 rc;. }..
179cf 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 /* If pToRele
179d0 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 ase is not zero
179d1 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e than pPrior poin
179d2 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 ts into the data
179d3 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f area. ** o
179d4 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d f pToRelease. M
179d5 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 ake sure pToRele
179d6 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 ase is still wri
179d7 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 teable. */.
179d8 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 assert( pToRele
179d9 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ase==0 || sqlite
179da 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
179db 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 e(pToRelease->pD
179dc 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 bPage) );..
179dd 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 /* If pPrior is
179de 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
179df 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c a area of pPage,
179e0 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 then make sure
179e1 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 pPage. ** i
179e2 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
179e3 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 e */. asser
179e4 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d t( pPrior<pPage-
179e5 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 >aData || pPrior
179e6 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b >=&pPage->aData[
179e7 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 pBt->pageSize].
179e8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 || sq
179e9 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
179ea 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
179eb 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 Page) );..
179ec 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c put4byte(pPrior,
179ed 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 pgnoOvfl);.
179ee 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
179ef 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 oRelease);.
179f0 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f pToRelease = pO
179f1 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f vfl;. pPrio
179f2 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 r = pOvfl->aData
179f3 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
179f4 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 (pPrior, 0);.
179f5 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 pPayload = &p
179f6 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a Ovfl->aData[4];.
179f7 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 spaceLeft
179f8 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
179f9 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 e - 4;. }.
179fa 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 n = nPayload;.
179fb 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 if( n>spaceLe
179fc 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 ft ) n = spaceLe
179fd 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 ft;.. /* If p
179fe 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 ToRelease is not
179ff 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c zero than pPayl
17a00 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 oad points into
17a01 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 the data area.
17a02 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 ** of pToRelea
17a03 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 se. Make sure p
17a04 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 ToRelease is sti
17a05 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f ll writeable. */
17a06 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f . assert( pTo
17a07 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 Release==0 || sq
17a08 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
17a09 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 eable(pToRelease
17a0a 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
17a0b 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 /* If pPayloa
17a0c 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 d is part of the
17a0d 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 data area of pP
17a0e 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 age, then make s
17a0f 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a ure pPage. **
17a10 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 is still writea
17a11 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 ble */. asser
17a12 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 t( pPayload<pPag
17a13 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 e->aData || pPay
17a14 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 load>=&pPage->aD
17a15 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a ata[pBt->pageSiz
17a16 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c e]. |
17a17 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 | sqlite3PagerIs
17a18 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
17a19 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 >pDbPage) );..
17a1a 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a if( nSrc>0 ){.
17a1b 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 if( n>nSrc
17a1c 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 ) n = nSrc;.
17a1d 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 assert( pSrc
17a1e 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 );. memcpy(
17a1f 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 pPayload, pSrc,
17a20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 n);. }else{.
17a21 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 memset(pPay
17a22 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 load, 0, n);.
17a23 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 }. nPayload
17a24 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f -= n;. pPaylo
17a25 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 ad += n;. pSr
17a26 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 c += n;. nSrc
17a27 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 -= n;. space
17a28 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 Left -= n;. i
17a29 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 f( nSrc==0 ){.
17a2a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 nSrc = nData
17a2b 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 ;. pSrc = p
17a2c 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Data;. }. }.
17a2d 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
17a2e 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 oRelease);. ret
17a2f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
17a30 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 ../*.** Remove t
17a31 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f he i-th cell fro
17a32 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 m pPage. This r
17a33 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 outine effects p
17a34 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 Page only..** Th
17a35 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 e cell content i
17a36 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 s not freed or d
17a37 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 eallocated. It
17a38 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a is assumed that.
17a39 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 ** the cell cont
17a3a 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 ent has been cop
17a3b 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c ied someplace el
17a3c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e se. This routin
17a3d 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 e just.** remove
17a3e 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 s the reference
17a3f 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d to the cell from
17a40 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 pPage..**.** "s
17a41 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e z" must be the n
17a42 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
17a43 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 n the cell..*/.s
17a44 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 tatic void dropC
17a45 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ell(MemPage *pPa
17a46 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 ge, int idx, int
17a47 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a sz, int *pRC){.
17a48 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
17a49 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
17a4a 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 r */. int pc;
17a4b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
17a4c 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 to cell content
17a4d 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 of cell being d
17a4e 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a eleted */. u8 *
17a4f 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 data; /* p
17a50 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 Page->aData */.
17a51 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 u8 *ptr;
17a52 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 /* Used to move
17a53 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 bytes around wi
17a54 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 thin data[] */.
17a55 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
17a56 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 /* The return c
17a57 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 ode */. int hdr
17a58 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 ; /* Begi
17a59 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 nning of the hea
17a5a 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 der. 0 most pag
17a5b 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 es. 100 page 1
17a5c 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 */.. if( *pRC )
17a5d 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 return;.. asse
17a5e 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 rt( idx>=0 && id
17a5f 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 x<pPage->nCell )
17a60 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d ;. assert( sz==
17a61 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 cellSize(pPage,
17a62 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 idx) );. assert
17a63 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
17a64 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
17a65 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
17a66 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
17a67 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
17a68 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
17a69 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e data = pPage->
17a6a 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 aData;. ptr = &
17a6b 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c data[pPage->cell
17a6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d 3b Offset + 2*idx];
17a6d 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 . pc = get2byte
17a6e 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70 (ptr);. hdr = p
17a6f 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
17a70 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d . testcase( pc=
17a71 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b =get2byte(&data[
17a72 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73 hdr+5]) );. tes
17a73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 tcase( pc+sz==pP
17a74 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
17a75 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63 Size );. if( pc
17a76 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 < get2byte(&dat
17a77 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b a[hdr+5]) || pc+
17a78 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d sz > pPage->pBt-
17a79 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 >usableSize ){.
17a7a 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 *pRC = SQLITE
17a7b 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
17a7c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
17a7d 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 rc = freeSpace(
17a7e 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a pPage, pc, sz);.
17a7f 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
17a80 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 *pRC = rc;. r
17a81 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 eturn;. }. for
17a82 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 (i=idx+1; i<pPag
17a83 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 e->nCell; i++, p
17a84 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b tr+=2){. ptr[
17a85 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 0] = ptr[2];.
17a86 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d ptr[1] = ptr[3]
17a87 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e ;. }. pPage->n
17a88 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 Cell--;. put2by
17a89 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c te(&data[hdr+3],
17a8a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a pPage->nCell);.
17a8b 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b pPage->nFree +
17a8c 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e = 2;.}../*.** In
17a8d 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 sert a new cell
17a8e 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c on pPage at cell
17a8f 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 index "i". pCe
17a90 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 ll points to the
17a91 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 .** content of t
17a92 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 he cell..**.** I
17a93 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 f the cell conte
17a94 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 nt will fit on t
17a95 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 he page, then pu
17a96 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 t it there. If
17a97 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 it.** will not f
17a98 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 it, then make a
17a99 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c copy of the cell
17a9a 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 content into pT
17a9b 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 emp if.** pTemp
17a9c 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 is not null. Re
17a9d 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d gardless of pTem
17a9e 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 p, allocate a ne
17a9f 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 w entry.** in pP
17aa0 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 age->aOvfl[] and
17aa1 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 make it point t
17aa2 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
17aa3 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e nt (either.** in
17aa4 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 pTemp or the or
17aa5 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e iginal pCell) an
17aa6 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 d also record it
17aa7 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c s index. .** All
17aa8 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e ocating a new en
17aa9 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 try in pPage->aC
17aaa 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 ell[] implies th
17aab 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f at .** pPage->nO
17aac 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 verflow is incre
17aad 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 mented..**.** If
17aae 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 nSkip is non-ze
17aaf 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 ro, then do not
17ab0 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e copy the first n
17ab1 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 Skip bytes of th
17ab2 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 e.** cell. The c
17ab3 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 aller will overw
17ab4 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 rite them after
17ab5 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 this function re
17ab6 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b turns. If.** nSk
17ab7 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 ip is non-zero,
17ab8 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e then pCell may n
17ab9 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 ot point to an i
17aba 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f nvalid memory lo
17abb 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 cation .** (but
17abc 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 pCell+nSkip is a
17abd 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f lways valid)..*/
17abe 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73 .static void ins
17abf 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 ertCell(. MemPa
17ac0 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 ge *pPage, /*
17ac1 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 Page into which
17ac2 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a we are copying *
17ac3 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 /. int i,
17ac4 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c /* New cel
17ac5 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d l becomes the i-
17ac6 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 th cell of the p
17ac7 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 age */. u8 *pCe
17ac8 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ll, /* Co
17ac9 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 ntent of the new
17aca 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 cell */. int s
17acb 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 z, /*
17acc 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 Bytes of content
17acd 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 in pCell */. u
17ace 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 8 *pTemp,
17acf 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 /* Temp storage
17ad0 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c space for pCell
17ad1 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 , if needed */.
17ad2 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 Pgno iChild,
17ad3 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 /* If non-zer
17ad4 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73 74 o, replace first
17ad5 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 4 bytes with th
17ad6 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e is value */. in
17ad7 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 20 t *pRC
17ad8 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 /* Read and writ
17ad9 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 e return code fr
17ada 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 om here */.){.
17adb 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 int idx;
17adc 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 /* Where to wr
17add 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e ite new cell con
17ade 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a tent in data[] *
17adf 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 /. int j;
17ae0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
17ae1 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 unter */. int e
17ae2 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 nd; /*
17ae3 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 First byte past
17ae4 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f the last cell po
17ae5 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 inter in data[]
17ae6 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 */. int ins;
17ae7 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
17ae8 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 in data[] where
17ae9 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 new cell pointer
17aea 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a is inserted */.
17aeb 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 int cellOffset
17aec 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f ; /* Address o
17aed 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 f first cell poi
17aee 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a nter in data[] *
17aef 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 /. u8 *data;
17af0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e /* The con
17af1 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c tent of the whol
17af2 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a e page */. u8 *
17af3 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ptr; /*
17af4 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 Used for moving
17af5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f information aro
17af6 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f und in data[] */
17af7 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d 20 .. int nSkip =
17af8 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 29 (iChild ? 4 : 0)
17af9 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 ;.. if( *pRC )
17afa 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 return;.. asser
17afb 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 50 t( i>=0 && i<=pP
17afc 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 age->nCell+pPage
17afd 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 ->nOverflow );.
17afe 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
17aff 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 nCell<=MX_CELL(p
17b00 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 Page->pBt) && MX
17b01 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 _CELL(pPage->pBt
17b02 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73 )<=5460 );. ass
17b03 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
17b04 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65 rflow<=ArraySize
17b05 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 29 (pPage->aOvfl) )
17b06 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17b07 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
17b08 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
17b09 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 63 65 ) );. /* The ce
17b0a 6c 6c 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c ll should normal
17b0b 6c 79 20 62 65 20 73 69 7a 65 64 20 63 6f 72 72 ly be sized corr
17b0c 65 63 74 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c ectly. However,
17b0d 20 77 68 65 6e 20 6d 6f 76 69 6e 67 20 61 0a 20 when moving a.
17b0e 20 2a 2a 20 6d 61 6c 66 6f 72 6d 65 64 20 63 65 ** malformed ce
17b0f 6c 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 20 70 ll from a leaf p
17b10 61 67 65 20 74 6f 20 61 6e 20 69 6e 74 65 72 69 age to an interi
17b11 6f 72 20 70 61 67 65 2c 20 69 66 20 74 68 65 20 or page, if the
17b12 63 65 6c 6c 20 73 69 7a 65 0a 20 20 2a 2a 20 77 cell size. ** w
17b13 61 6e 74 65 64 20 74 6f 20 62 65 20 6c 65 73 73 anted to be less
17b14 20 74 68 61 6e 20 34 20 62 75 74 20 67 6f 74 20 than 4 but got
17b15 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20 rounded up to 4
17b16 6f 6e 20 74 68 65 20 6c 65 61 66 2c 20 74 68 65 on the leaf, the
17b17 6e 20 73 69 7a 65 0a 20 20 2a 2a 20 6d 69 67 68 n size. ** migh
17b18 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 38 t be less than 8
17b19 20 28 6c 65 61 66 2d 73 69 7a 65 20 2b 20 70 6f (leaf-size + po
17b1a 69 6e 74 65 72 29 20 6f 6e 20 74 68 65 20 69 6e inter) on the in
17b1b 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 48 65 terior node. He
17b1c 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 74 65 72 nce. ** the ter
17b1d 6d 20 61 66 74 65 72 20 74 68 65 20 7c 7c 20 69 m after the || i
17b1e 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
17b1f 61 73 73 65 72 74 28 29 2e 20 2a 2f 0a 20 20 61 assert(). */. a
17b20 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 ssert( sz==cellS
17b21 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 izePtr(pPage, pC
17b22 65 6c 6c 29 20 7c 7c 20 28 73 7a 3d 3d 38 20 26 ell) || (sz==8 &
17b23 26 20 69 43 68 69 6c 64 3e 30 29 20 29 3b 0a 20 & iChild>0) );.
17b24 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 if( pPage->nOve
17b25 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 rflow || sz+2>pP
17b26 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 age->nFree ){.
17b27 20 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 if( pTemp ){.
17b28 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d memcpy(pTem
17b29 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e p+nSkip, pCell+n
17b2a 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b Skip, sz-nSkip);
17b2b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 . pCell = p
17b2c 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Temp;. }.
17b2d 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 if( iChild ){.
17b2e 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 put4byte(pCe
17b2f 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 ll, iChild);.
17b30 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 }. j = pPage
17b31 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 ->nOverflow++;.
17b32 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e assert( j<(in
17b33 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d t)(sizeof(pPage-
17b34 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 >aOvfl)/sizeof(p
17b35 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 Page->aOvfl[0]))
17b36 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 );. pPage->a
17b37 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 Ovfl[j].pCell =
17b38 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 pCell;. pPage
17b39 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d ->aOvfl[j].idx =
17b3a 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65 (u16)i;. }else
17b3b 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 {. int rc = s
17b3c 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
17b3d 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
17b3e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
17b3f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17b40 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 *pRC = rc;.
17b41 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a return;. }.
17b42 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
17b43 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
17b44 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
17b45 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 ge) );. data
17b46 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
17b47 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d cellOffset =
17b48 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 pPage->cellOffs
17b49 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65 et;. end = ce
17b4a 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 llOffset + 2*pPa
17b4b 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 ge->nCell;. i
17b4c 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 ns = cellOffset
17b4d 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20 + 2*i;. rc =
17b4e 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 allocateSpace(pP
17b4f 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a age, sz, &idx);.
17b50 20 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70 if( rc ){ *p
17b51 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b RC = rc; return;
17b52 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c }. /* The al
17b53 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 6f locateSpace() ro
17b54 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73 utine guarantees
17b55 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
17b56 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20 wo properties.
17b57 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72 ** if it retur
17b58 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20 ns success */.
17b59 20 20 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d assert( idx >=
17b5a 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73 end+2 );. as
17b5b 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 sert( idx+sz <=
17b5c 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
17b5d 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50 leSize );. pP
17b5e 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 age->nCell++;.
17b5f 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d pPage->nFree -
17b60 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 3b = (u16)(2 + sz);
17b61 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 . memcpy(&dat
17b62 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 a[idx+nSkip], pC
17b63 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 ell+nSkip, sz-nS
17b64 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 20 69 43 kip);. if( iC
17b65 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 hild ){. pu
17b66 74 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78 t4byte(&data[idx
17b67 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 ], iChild);.
17b68 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2c }. for(j=end,
17b69 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a ptr=&data[j]; j
17b6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d >ins; j-=2, ptr-
17b6b 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 =2){. ptr[0
17b6c 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 ] = ptr[-2];.
17b6d 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b ptr[1] = ptr[
17b6e 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 -1];. }. p
17b6f 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e ut2byte(&data[in
17b70 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75 s], idx);. pu
17b71 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61 t2byte(&data[pPa
17b72 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d ge->hdrOffset+3]
17b73 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b , pPage->nCell);
17b74 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
17b75 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
17b76 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 if( pPage->p
17b77 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
17b78 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 {. /* The c
17b79 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 ell may contain
17b7a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
17b7b 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 overflow page. I
17b7c 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 f so, write.
17b7d 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 ** the entry f
17b7e 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 or the overflow
17b7f 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f page into the po
17b80 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 inter map..
17b81 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70 */. ptrmap
17b82 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 PutOvflPtr(pPage
17b83 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20 , pCell, pRC);.
17b84 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
17b85 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c }../*.** Add a l
17b86 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 ist of cells to
17b87 61 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 a page. The pag
17b88 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 e should be init
17b89 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 ially empty..**
17b8a 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 The cells are gu
17b8b 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 aranteed to fit
17b8c 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a on the page..*/.
17b8d 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 static void asse
17b8e 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 mblePage(. MemP
17b8f 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a age *pPage, /*
17b90 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 The page to be
17b91 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 assemblied */.
17b92 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 int nCell,
17b93 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 /* The number
17b94 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 of cells to add
17b95 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a to this page */.
17b96 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 u8 **apCell,
17b97 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 /* Pointers
17b98 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a to cell bodies *
17b99 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 /. u16 *aSize
17b9a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f /* Sizes o
17b9b 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 f the cells */.)
17b9c 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
17b9d 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
17b9e 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 unter */. u8 *p
17b9f 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 Cellptr; /*
17ba0 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 Address of next
17ba1 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a cell pointer */.
17ba2 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 int cellbody;
17ba3 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f /* Address o
17ba4 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 f next cell body
17ba5 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 */. u8 * const
17ba6 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
17ba7 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
17ba8 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
17ba9 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a data for pPage *
17baa 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 /. const int hd
17bab 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
17bac 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
17bad 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 /* Offset of hea
17bae 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a der on pPage */.
17baf 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 const int nUsa
17bb0 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 ble = pPage->pBt
17bb1 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a ->usableSize; /*
17bb2 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 Usable size of
17bb3 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 page */.. asser
17bb4 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 t( pPage->nOverf
17bb5 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 low==0 );. asse
17bb6 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
17bb7 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 x_held(pPage->pB
17bb8 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
17bb9 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 ssert( nCell>=0
17bba 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c && nCell<=MX_CEL
17bbb 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 L(pPage->pBt) &&
17bbc 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e MX_CELL(pPage->
17bbd 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 pBt)<=5460 );.
17bbe 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
17bbf 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
17bc0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
17bc1 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 );.. /* Check t
17bc2 68 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 hat the page has
17bc3 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 just been zeroe
17bc4 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 d by zeroPage()
17bc5 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
17bc6 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a ge->nCell==0 );.
17bc7 20 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 assert( get2by
17bc8 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
17bc9 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 ==nUsable );..
17bca 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 pCellptr = &data
17bcb 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 [pPage->cellOffs
17bcc 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 et + nCell*2];.
17bcd 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 cellbody = nUsa
17bce 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 ble;. for(i=nCe
17bcf 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 ll-1; i>=0; i--)
17bd0 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d {. pCellptr -
17bd1 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 = 2;. cellbod
17bd2 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 y -= aSize[i];.
17bd3 20 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c put2byte(pCel
17bd4 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b lptr, cellbody);
17bd5 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 . memcpy(&dat
17bd6 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 a[cellbody], apC
17bd7 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d ell[i], aSize[i]
17bd8 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 );. }. put2byt
17bd9 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 e(&data[hdr+3],
17bda 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 nCell);. put2by
17bdb 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c te(&data[hdr+5],
17bdc 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 cellbody);. pP
17bdd 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e age->nFree -= (n
17bde 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 Cell*2 + nUsable
17bdf 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 - cellbody);.
17be0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 pPage->nCell = (
17be1 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a u16)nCell;.}../*
17be2 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
17be3 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 g parameters det
17be4 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 ermine how many
17be5 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 adjacent pages g
17be6 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 et involved.** i
17be7 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 n a balancing op
17be8 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 eration. NN is
17be9 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 the number of ne
17bea 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 ighbors on eithe
17beb 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 r side.** of the
17bec 20 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 page that parti
17bed 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 cipate in the ba
17bee 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f lancing operatio
17bef 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a n. NB is the.**
17bf0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
17bf1 20 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 pages that part
17bf2 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 icipate, includi
17bf3 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 ng the target pa
17bf4 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 ge and.** NN nei
17bf5 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 ghbors on either
17bf6 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 side..**.** The
17bf7 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f minimum value o
17bf8 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f f NN is 1 (of co
17bf9 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 urse). Increasi
17bfa 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a ng NN above 1.**
17bfb 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 (to 2 or 3) giv
17bfc 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 es a modest impr
17bfd 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 ovement in SELEC
17bfe 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 T and DELETE per
17bff 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 formance.** in e
17c00 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 xchange for a la
17c01 72 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e rger degradation
17c02 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 in INSERT and U
17c03 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 PDATE performanc
17c04 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 e..** The value
17c05 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f of NN appears to
17c06 20 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 give the best r
17c07 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a esults overall..
17c08 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 */.#define NN 1
17c09 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17c0a 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f umber of neighbo
17c0b 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 rs on either sid
17c0c 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 e of pPage */.#d
17c0d 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 efine NB (NN*2+1
17c0e 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 ) /* Total
17c0f 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 pages involved i
17c10 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f n the balance */
17c11 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
17c12 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 E_OMIT_QUICKBALA
17c13 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 NCE./*.** This v
17c14 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 ersion of balanc
17c15 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 e() handles the
17c16 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 common special c
17c17 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e ase where.** a n
17c18 65 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e ew entry is bein
17c19 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 g inserted on th
17c1a 65 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d e extreme right-
17c1b 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 end of the.** tr
17c1c 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 ee, in other wor
17c1d 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 ds, when the new
17c1e 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f entry will beco
17c1f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a me the largest.*
17c20 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 * entry in the t
17c21 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 ree..**.** Inste
17c22 61 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 ad of trying to
17c23 62 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 balance the 3 ri
17c24 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 ght-most leaf pa
17c25 67 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a ges, just add.**
17c26 20 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 a new page to t
17c27 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 he right-hand si
17c28 64 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f de and put the o
17c29 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a ne new entry in.
17c2a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 ** that page. T
17c2b 68 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 his leaves the r
17c2c 69 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 ight side of the
17c2d 20 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a tree somewhat.*
17c2e 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 * unbalanced. B
17c2f 75 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 ut odds are that
17c30 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 we will be inse
17c31 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 rting new entrie
17c32 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 s.** at the end
17c33 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 soon afterwards
17c34 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d so the nearly em
17c35 70 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 pty page will qu
17c36 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 ickly.** fill up
17c37 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a . On average..*
17c38 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 *.** pPage is th
17c39 65 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 e leaf page whic
17c3a 68 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d h is the right-m
17c3b 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ost page in the
17c3c 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 tree..** pParent
17c3d 20 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 is its parent.
17c3e 20 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 pPage must have
17c3f 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c a single overfl
17c40 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 ow entry.** whic
17c41 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 h is also the ri
17c42 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f ght-most entry o
17c43 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a n the page..**.*
17c44 2a 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 * The pSpace buf
17c45 66 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 fer is used to s
17c46 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 tore a temporary
17c47 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 copy of the div
17c48 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 ider.** cell tha
17c49 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 t will be insert
17c4a 65 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e ed into pParent.
17c4b 20 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e Such a cell con
17c4c 73 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 sists of a 4.**
17c4d 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 byte page number
17c4e 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 followed by a v
17c4f 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 ariable length i
17c50 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 nteger. In other
17c51 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f .** words, at mo
17c52 73 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e st 13 bytes. Hen
17c53 63 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 ce the pSpace bu
17c54 66 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a ffer must be at.
17c55 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 ** least 13 byte
17c56 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 s in size..*/.st
17c57 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 atic int balance
17c58 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a _quick(MemPage *
17c59 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 pParent, MemPage
17c5a 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 *pPage, u8 *pSp
17c5b 61 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 ace){. BtShared
17c5c 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 *const pBt = pP
17c5d 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 age->pBt; /*
17c5e 42 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 B-Tree Database
17c5f 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e */. MemPage *pN
17c60 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ew;
17c61 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 /* New
17c62 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 ly allocated pag
17c63 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
17c64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
17c66 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 eturn Code */.
17c67 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 Pgno pgnoNew;
17c68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c69 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
17c6a 62 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a ber of pNew */..
17c6b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17c6c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
17c6d 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
17c6e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
17c6f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
17c70 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 able(pParent->pD
17c71 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
17c72 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
17c73 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 69 66 flow==1 );.. if
17c74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d ( pPage->nCell<=
17c75 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
17c76 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
17c77 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 . /* Allocate a
17c78 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 20 new page. This
17c79 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 page will become
17c7a 20 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c 69 the right-sibli
17c7b 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 67 ng of . ** pPag
17c7c 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 65 e. Make the pare
17c7d 6e 74 20 70 61 67 65 20 77 72 69 74 61 62 6c 65 nt page writable
17c7e 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 , so that the ne
17c7f 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 w divider cell.
17c80 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 72 ** may be inser
17c81 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 65 ted. If both the
17c82 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 se operations ar
17c83 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 72 e successful, pr
17c84 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 oceed.. */. rc
17c85 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
17c86 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c Page(pBt, &pNew,
17c87 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 &pgnoNew, 0, 0)
17c88 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
17c89 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 75 ITE_OK ){.. u
17c8a 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 63 8 *pOut = &pSpac
17c8b 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43 e[4];. u8 *pC
17c8c 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 ell = pPage->aOv
17c8d 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 fl[0].pCell;.
17c8e 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65 u16 szCell = ce
17c8f 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
17c90 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20 pCell);. u8
17c91 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73 *pStop;.. ass
17c92 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
17c93 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 rIswriteable(pNe
17c94 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 w->pDbPage) );.
17c95 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
17c96 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46 ->aData[0]==(PTF
17c97 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 _INTKEY|PTF_LEAF
17c98 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29 DATA|PTF_LEAF) )
17c99 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 ;. zeroPage(p
17c9a 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c New, PTF_INTKEY|
17c9b 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 PTF_LEAFDATA|PTF
17c9c 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65 _LEAF);. asse
17c9d 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31 mblePage(pNew, 1
17c9e 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c , &pCell, &szCel
17c9f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 l);.. /* If t
17ca0 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 his is an auto-v
17ca1 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 acuum database,
17ca2 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 update the point
17ca3 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 er map. ** wi
17ca4 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 th entries for t
17ca5 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 he new page, and
17ca6 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f any pointer fro
17ca7 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 m the . ** ce
17ca8 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 ll on the page t
17ca9 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 o an overflow pa
17caa 67 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 ge. If either of
17cab 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 these. ** op
17cac 65 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 erations fails,
17cad 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 the return code
17cae 69 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 is set, but the
17caf 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 contents. **
17cb0 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 of the parent pa
17cb1 67 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e ge are still man
17cb2 69 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 ipulated by thh
17cb3 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 code below..
17cb4 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 ** That is Ok, a
17cb5 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 t this point the
17cb6 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 parent page is
17cb7 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 guaranteed to.
17cb8 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 ** be marked a
17cb9 73 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 s dirty. Returni
17cba 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 ng an error code
17cbb 20 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 will cause a.
17cbc 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 ** rollback, u
17cbd 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 ndoing any chang
17cbe 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 es made to the p
17cbf 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 arent page..
17cc0 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 */. if( ISAUT
17cc1 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 OVACUUM ){.
17cc2 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
17cc3 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f pgnoNew, PTRMAP_
17cc4 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e BTREE, pParent->
17cc5 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 pgno, &rc);.
17cc6 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 if( szCell>pNe
17cc7 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 w->minLocal ){.
17cc8 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 ptrmapPut
17cc9 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 OvflPtr(pNew, pC
17cca 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 ell, &rc);.
17ccb 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
17ccc 2f 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69 /* Create a divi
17ccd 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 der cell to inse
17cce 72 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e rt into pParent.
17ccf 20 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c The divider cel
17cd0 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 l. ** consist
17cd1 73 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 s of a 4-byte pa
17cd2 67 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 ge number (the p
17cd3 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 age number of pP
17cd4 61 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 age) and. **
17cd5 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 a variable lengt
17cd6 68 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 h key value (whi
17cd7 63 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 ch must be the s
17cd8 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 ame value as the
17cd9 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 . ** largest
17cda 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 key on pPage)..
17cdb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 **. ** To
17cdc 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 find the largest
17cdd 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 key value on pP
17cde 61 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 age, first find
17cdf 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a the right-most .
17ce0 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 ** cell on p
17ce1 50 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 Page. The first
17ce2 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 two fields of th
17ce3 69 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 is cell are the
17ce4 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c . ** record-l
17ce5 65 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c ength (a variabl
17ce6 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e length integer
17ce7 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 at most 32-bits
17ce8 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a in size). **
17ce9 20 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c and the key val
17cea 75 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c ue (a variable l
17ceb 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d ength integer, m
17cec 61 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 ay have any valu
17ced 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 e).. ** The f
17cee 69 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c irst of the whil
17cef 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c e(...) loops bel
17cf0 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 ow skips over th
17cf1 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a e record-length.
17cf2 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 ** field. Th
17cf3 65 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e e second while(.
17cf4 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 ..) loop copies
17cf5 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 the key value fr
17cf6 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 om the. ** ce
17cf7 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f ll on pPage into
17cf8 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 the pSpace buff
17cf9 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 er.. */. p
17cfa 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
17cfb 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 pPage, pPage->nC
17cfc 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f ell-1);. pSto
17cfd 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 p = &pCell[9];.
17cfe 20 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 while( (*(pCe
17cff 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 ll++)&0x80) && p
17d00 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 Cell<pStop );.
17d01 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c pStop = &pCell
17d02 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 [9];. while(
17d03 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 ((*(pOut++) = *(
17d04 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 pCell++))&0x80)
17d05 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 && pCell<pStop )
17d06 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 ;.. /* Insert
17d07 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 the new divider
17d08 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 cell into pPare
17d09 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 nt. */. inser
17d0a 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 tCell(pParent, p
17d0b 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 Parent->nCell, p
17d0c 53 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 Space, (int)(pOu
17d0d 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 t-pSpace),.
17d0e 20 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 0, pPa
17d0f 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a ge->pgno, &rc);.
17d10 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 . /* Set the
17d11 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e right-child poin
17d12 74 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 ter of pParent t
17d13 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e o point to the n
17d14 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 ew page. */.
17d15 70 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e put4byte(&pParen
17d16 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 t->aData[pParent
17d17 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 ->hdrOffset+8],
17d18 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 pgnoNew);. .
17d19 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 /* Release the
17d1a 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
17d1b 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 new page. */.
17d1c 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e releasePage(pN
17d1d 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ew);. }.. retu
17d1e 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 rn rc;.}.#endif
17d1f 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 /* SQLITE_OMIT_Q
17d20 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a UICKBALANCE */..
17d21 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 #if 0./*.** This
17d22 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e function does n
17d23 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e ot contribute an
17d24 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 ything to the op
17d25 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 eration of SQLit
17d26 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 e..** it is some
17d27 74 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 times activated
17d28 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c temporarily whil
17d29 65 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 e debugging code
17d2a 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a responsible .**
17d2b 20 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 for setting poi
17d2c 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
17d2d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
17d2e 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 ptrmapCheckPages
17d2f 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 (MemPage **apPag
17d30 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 e, int nPage){.
17d31 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 int i, j;. for
17d32 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 (i=0; i<nPage; i
17d33 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b ++){. Pgno n;
17d34 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d . u8 e;. M
17d35 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
17d36 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 apPage[i];. B
17d37 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
17d38 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 Page->pBt;. a
17d39 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 ssert( pPage->is
17d3a 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 Init );.. for
17d3b 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e (j=0; j<pPage->n
17d3c 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 Cell; j++){.
17d3d 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
17d3e 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 . u8 *z;.
17d3f 20 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 . z = fi
17d40 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 ndCell(pPage, j)
17d41 3b 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 ;. btreePar
17d42 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
17d43 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 z, &info);.
17d44 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 if( info.iOver
17d45 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 flow ){.
17d46 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 Pgno ovfl = get4
17d47 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 byte(&z[info.iOv
17d48 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 erflow]);.
17d49 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c ptrmapGet(pBt,
17d4a 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a ovfl, &e, &n);.
17d4b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
17d4c 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 n==pPage->pgno &
17d4d 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 & e==PTRMAP_OVER
17d4e 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d FLOW1 );. }
17d4f 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 . if( !pPag
17d50 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
17d51 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 Pgno child =
17d52 67 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 get4byte(z);.
17d53 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 ptrmapGet(p
17d54 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 Bt, child, &e, &
17d55 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 n);. asse
17d56 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 rt( n==pPage->pg
17d57 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f no && e==PTRMAP_
17d58 42 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d BTREE );. }
17d59 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
17d5a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
17d5b 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 Pgno child
17d5c 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
17d5d 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
17d5e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
17d5f 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 ptrmapGet(p
17d60 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 Bt, child, &e, &
17d61 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 n);. assert
17d62 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f ( n==pPage->pgno
17d63 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 && e==PTRMAP_BT
17d64 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d REE );. }. }
17d65 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
17d66 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
17d67 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
17d68 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 ed to copy the c
17d69 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 ontents of the b
17d6a 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 -tree node store
17d6b 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 d .** on page pF
17d6c 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e rom to page pTo.
17d6d 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 If page pFrom w
17d6e 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 as not a leaf pa
17d6f 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 ge, then.** the
17d70 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
17d71 69 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 ies for each chi
17d72 6c 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61 ld page are upda
17d73 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a ted so that the.
17d74 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 ** parent page s
17d75 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 tored in the poi
17d76 6e 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 nter map is page
17d77 20 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 pTo. If pFrom c
17d78 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 ontained.** any
17d79 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 cells with overf
17d7a 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 low page pointer
17d7b 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 s, then the corr
17d7c 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 esponding pointe
17d7d 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 r.** map entries
17d7e 20 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 are also update
17d7f 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 d so that the pa
17d80 72 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 rent page is pag
17d81 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 e pTo..**.** If
17d82 70 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 pFrom is current
17d83 6c 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 ly carrying any
17d84 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 overflow cells (
17d85 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a entries in the.*
17d86 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 6c 5b * MemPage.aOvfl[
17d87 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 ] array), they a
17d88 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f re not copied to
17d89 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 pTo. .**.** Bef
17d8a 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 ore returning, p
17d8b 61 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 age pTo is reini
17d8c 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 tialized using b
17d8d 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a treeInitPage()..
17d8e 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 **.** The perfor
17d8f 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 mance of this fu
17d90 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 nction is not cr
17d91 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e itical. It is on
17d92 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 ly used by .** t
17d93 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c he balance_shall
17d94 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e ower() and balan
17d95 63 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 ce_deeper() proc
17d96 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 edures, neither
17d97 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 of.** which are
17d98 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 called often und
17d99 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d er normal circum
17d9a 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 stances..*/.stat
17d9b 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 ic void copyNode
17d9c 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 Content(MemPage
17d9d 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 *pFrom, MemPage
17d9e 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b *pTo, int *pRC){
17d9f 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 . if( (*pRC)==S
17da0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17da1 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 BtShared * const
17da2 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 pBt = pFrom->pB
17da3 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 t;. u8 * cons
17da4 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d t aFrom = pFrom-
17da5 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a >aData;. u8 *
17da6 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f const aTo = pTo
17da7 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 ->aData;. int
17da8 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 const iFromHdr
17da9 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 = pFrom->hdrOffs
17daa 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 et;. int cons
17dab 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f t iToHdr = ((pTo
17dac 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 ->pgno==1) ? 100
17dad 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 53 54 4f : 0);. TESTO
17dae 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20 20 20 NLY(int rc;).
17daf 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 20 0a 20 int iData;. .
17db0 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 . assert( pF
17db1 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 rom->isInit );.
17db2 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d assert( pFrom
17db3 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64 72 20 ->nFree>=iToHdr
17db4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67 );. assert( g
17db5 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 et2byte(&aFrom[i
17db6 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70 42 74 FromHdr+5])<=pBt
17db7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a ->usableSize );.
17db8 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 . /* Copy t
17db9 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 63 he b-tree node c
17dba 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61 67 65 ontent from page
17dbb 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 pFrom to page p
17dbc 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61 74 61 To. */. iData
17dbd 20 3d 20 67 65 74 32 62 79 74 65 28 26 61 46 72 = get2byte(&aFr
17dbe 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3b om[iFromHdr+5]);
17dbf 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f . memcpy(&aTo
17dc0 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 6f 6d 5b [iData], &aFrom[
17dc1 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 73 61 iData], pBt->usa
17dc2 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 3b 0a bleSize-iData);.
17dc3 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 6f 5b memcpy(&aTo[
17dc4 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f 6d 5b iToHdr], &aFrom[
17dc5 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 6f 6d iFromHdr], pFrom
17dc6 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 ->cellOffset + 2
17dc7 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 3b 0a *pFrom->nCell);.
17dc8 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e 69 74 . /* Reinit
17dc9 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54 6f 20 ialize page pTo
17dca 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 so that the cont
17dcb 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 ents of the MemP
17dcc 61 67 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 age structure.
17dcd 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 65 20 6e ** match the n
17dce 65 77 20 64 61 74 61 2e 20 54 68 65 20 69 6e 69 ew data. The ini
17dcf 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 70 tialization of p
17dd0 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61 69 6c To "cannot" fail
17dd1 2c 20 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 , as the. **
17dd2 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d data copied from
17dd3 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77 6e 20 pFrom is known
17dd4 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 20 2a 2f to be valid. */
17dd5 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e 69 74 . pTo->isInit
17dd6 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54 4f 4e = 0;. TESTON
17dd7 4c 59 28 72 63 20 3d 20 29 20 62 74 72 65 65 49 LY(rc = ) btreeI
17dd8 6e 69 74 50 61 67 65 28 70 54 6f 29 3b 0a 20 20 nitPage(pTo);.
17dd9 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
17dda 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a 20 20 LITE_OK );. .
17ddb 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
17ddc 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 an auto-vacuum d
17ddd 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20 atabase, update
17dde 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
17ddf 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 66 entries. ** f
17de0 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 20 6f 72 or any b-tree or
17de1 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
17de2 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 63 6f 6e that pTo now con
17de3 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 tains the pointe
17de4 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 rs to.. */.
17de5 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
17de6 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 UM ){. *pRC
17de7 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 = setChildPtrma
17de8 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 ps(pTo);. }.
17de9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 }.}../*.** This
17dea 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 74 72 routine redistr
17deb 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20 ibutes cells on
17dec 74 68 65 20 69 50 61 72 65 6e 74 49 64 78 27 74 the iParentIdx't
17ded 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 h child of pPare
17dee 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 74 65 72 nt.** (hereafter
17def 20 22 74 68 65 20 70 61 67 65 22 29 20 61 6e 64 "the page") and
17df0 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69 6e 67 up to 2 sibling
17df1 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61 s so that all pa
17df2 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74 ges have about t
17df3 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f 75 6e he.** same amoun
17df4 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e t of free space.
17df5 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e 67 6c Usually a singl
17df6 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65 69 74 e sibling on eit
17df7 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 0a her side of the.
17df8 2a 2a 20 70 61 67 65 20 61 72 65 20 75 73 65 64 ** page are used
17df9 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e in the balancin
17dfa 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 20 73 g, though both s
17dfb 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 63 6f iblings might co
17dfc 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 me from one.** s
17dfd 69 64 65 20 69 66 20 74 68 65 20 70 61 67 65 20 ide if the page
17dfe 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 72 20 is the first or
17dff 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 20 69 74 last child of it
17e00 73 20 70 61 72 65 6e 74 2e 20 49 66 20 74 68 65 s parent. If the
17e01 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 20 66 65 page .** has fe
17e02 77 65 72 20 74 68 61 6e 20 32 20 73 69 62 6c 69 wer than 2 sibli
17e03 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e 67 20 77 ngs (something w
17e04 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20 68 61 hich can only ha
17e05 70 70 65 6e 20 69 66 20 74 68 65 20 70 61 67 65 ppen if the page
17e06 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20 70 61 .** is a root pa
17e07 67 65 20 6f 72 20 61 20 63 68 69 6c 64 20 6f 66 ge or a child of
17e08 20 61 20 72 6f 6f 74 20 70 61 67 65 29 20 74 68 a root page) th
17e09 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 en all available
17e0a 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70 61 72 siblings.** par
17e0b 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 ticipate in the
17e0c 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a balancing..**.**
17e0d 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 The number of s
17e0e 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 65 20 70 iblings of the p
17e0f 61 67 65 20 6d 69 67 68 74 20 62 65 20 69 6e 63 age might be inc
17e10 72 65 61 73 65 64 20 6f 72 20 64 65 63 72 65 61 reased or decrea
17e11 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f sed by .** one o
17e12 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f r two in an effo
17e13 72 74 20 74 6f 20 6b 65 65 70 20 70 61 67 65 73 rt to keep pages
17e14 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 nearly full but
17e15 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 not over full.
17e16 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
17e17 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
17e18 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f ne is called, so
17e19 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 me of the cells
17e1a 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6d on the page.** m
17e1b 69 67 68 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c ight not actuall
17e1c 79 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 4d y be stored in M
17e1d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20 emPage.aData[].
17e1e 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a This can happen.
17e1f 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69 ** if the page i
17e20 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68 69 73 s overfull. This
17e21 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 routine ensures
17e22 20 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20 that all cells
17e23 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74 6f 20 allocated.** to
17e24 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73 the page and its
17e25 20 73 69 62 6c 69 6e 67 73 20 66 69 74 20 69 6e siblings fit in
17e26 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 to MemPage.aData
17e27 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e [] before return
17e28 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 ing..**.** In th
17e29 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 e course of bala
17e2a 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 ncing the page a
17e2b 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 2c nd its siblings,
17e2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a 2a 2a cells may be.**
17e2d 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 6f inserted into o
17e2e 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 r removed from t
17e2f 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 28 he parent page (
17e30 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e 67 20 pParent). Doing
17e31 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73 65 20 so.** may cause
17e32 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
17e33 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 to become overfu
17e34 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e ll or underfull.
17e35 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 61 70 70 If this.** happ
17e36 65 6e 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 ens, it is the r
17e37 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 esponsibility of
17e38 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 69 the caller to i
17e39 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 72 65 63 nvoke the correc
17e3a 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 20 72 t.** balancing r
17e3b 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 20 74 68 outine to fix th
17e3c 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 65 65 20 is problem (see
17e3d 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20 72 6f the balance() ro
17e3e 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 utine). .**.** I
17e3f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 f this routine f
17e40 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65 61 ails for any rea
17e41 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 6c 65 son, it might le
17e42 61 76 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ave the database
17e43 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 70 74 .** in a corrupt
17e44 65 64 20 73 74 61 74 65 2e 20 53 6f 20 69 66 20 ed state. So if
17e45 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 this routine fai
17e46 6c 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 ls, the database
17e47 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 72 6f should.** be ro
17e48 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a lled back..**.**
17e49 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
17e4a 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
17e4b 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61 63 65 tion, aOvflSpace
17e4c 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 , is a pointer t
17e4d 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 20 62 69 o a.** buffer bi
17e4e 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 g enough to hold
17e4f 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 20 77 68 one page. If wh
17e50 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 63 65 ile inserting ce
17e51 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72 lls into the par
17e52 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70 50 61 ent.** page (pPa
17e53 72 65 6e 74 29 20 74 68 65 20 70 61 72 65 6e 74 rent) the parent
17e54 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 page becomes ov
17e55 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 62 75 66 erfull, this buf
17e56 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 fer is.** used t
17e57 6f 20 73 74 6f 72 65 20 74 68 65 20 70 61 72 65 o store the pare
17e58 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 nt's overflow ce
17e59 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 74 68 69 lls. Because thi
17e5a 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72 s function inser
17e5b 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20 ts.** a maximum
17e5c 6f 66 20 66 6f 75 72 20 64 69 76 69 64 65 72 20 of four divider
17e5d 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 cells into the p
17e5e 61 72 65 6e 74 20 70 61 67 65 2c 20 61 6e 64 20 arent page, and
17e5f 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73 the maximum.** s
17e60 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 73 74 ize of a cell st
17e61 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e 20 69 ored within an i
17e62 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20 nternal node is
17e63 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68 61 6e always less than
17e64 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65 20 70 1/4.** of the p
17e65 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 61 4f age-size, the aO
17e66 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 66 65 vflSpace[] buffe
17e67 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 r is guaranteed
17e68 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a 20 65 to be large.** e
17e69 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20 6f 76 nough for all ov
17e6a 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a erflow cells..**
17e6b 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70 61 63 .** If aOvflSpac
17e6c 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6e 75 e is set to a nu
17e6d 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 ll pointer, this
17e6e 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
17e6f 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d s .** SQLITE_NOM
17e70 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e EM..*/.static in
17e71 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f t balance_nonroo
17e72 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 t(. MemPage *pP
17e73 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 arent,
17e74 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 /* Parent p
17e75 61 67 65 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 age of siblings
17e76 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a being balanced *
17e77 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 49 /. int iParentI
17e78 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 dx,
17e79 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
17e7a 22 74 68 65 20 70 61 67 65 22 20 69 6e 20 70 50 "the page" in pP
17e7b 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20 2a 61 arent */. u8 *a
17e7c 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 20 20 20 OvflSpace,
17e7d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 61 /* pa
17e7e 67 65 2d 73 69 7a 65 20 62 79 74 65 73 20 6f 66 ge-size bytes of
17e7f 20 73 70 61 63 65 20 66 6f 72 20 70 61 72 65 6e space for paren
17e80 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e 74 20 t ovfl */. int
17e81 69 73 52 6f 6f 74 20 20 20 20 20 20 20 20 20 20 isRoot
17e82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
17e83 72 75 65 20 69 66 20 70 50 61 72 65 6e 74 20 69 rue if pParent i
17e84 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f s a root-page */
17e85 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a .){. BtShared *
17e86 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 pBt;
17e87 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 /* The whole
17e88 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e database */. in
17e89 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 t nCell = 0;
17e8a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
17e8b 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e mber of cells in
17e8c 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 apCell[] */. i
17e8d 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 nt nMaxCells = 0
17e8e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 ; /* A
17e8f 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
17e90 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c apCell, szCell,
17e91 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 aFrom. */. int
17e92 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 nNew = 0;
17e93 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
17e94 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
17e95 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 apNew[] */. int
17e96 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 nOld;
17e97 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
17e98 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
17e99 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 apOld[] */. int
17e9a 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 i, j, k;
17e9b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
17e9c 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 p counters */.
17e9d 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 20 20 int nxDiv;
17e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e9f 4e 65 78 74 20 64 69 76 69 64 65 72 20 73 6c 6f Next divider slo
17ea0 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 t in pParent->aC
17ea1 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 ell[] */. int r
17ea2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
17ea3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
17ea4 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
17ea5 75 31 36 20 6c 65 61 66 43 6f 72 72 65 63 74 69 u16 leafCorrecti
17ea6 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 on; /*
17ea7 34 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 4 if pPage is a
17ea8 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 leaf. 0 if not
17ea9 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 61 74 */. int leafDat
17eaa 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
17eab 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61 /* True if pPa
17eac 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f 66 20 ge is a leaf of
17ead 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 a LEAFDATA tree
17eae 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 */. int usableS
17eaf 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 pace;
17eb0 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 70 50 /* Bytes in pP
17eb1 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65 20 68 age beyond the h
17eb2 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 eader */. int p
17eb3 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 ageFlags;
17eb4 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
17eb5 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 of pPage->aData
17eb6 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 [0] */. int sub
17eb7 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 20 20 total;
17eb8 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 /* Subtota
17eb9 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 63 65 l of bytes in ce
17eba 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 lls on one page
17ebb 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 65 31 */. int iSpace1
17ebc 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
17ebd 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 /* First unuse
17ebe 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 63 65 d byte of aSpace
17ebf 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 76 1[] */. int iOv
17ec0 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 20 20 20 flSpace = 0;
17ec1 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 /* First u
17ec2 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 4f nused byte of aO
17ec3 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a 20 20 vflSpace[] */.
17ec4 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20 int szScratch;
17ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17ec6 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63 68 20 Size of scratch
17ec7 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74 65 64 memory requested
17ec8 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 */. MemPage *a
17ec9 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20 pOld[NB];
17eca 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 /* pPage and
17ecb 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e up to two siblin
17ecc 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 gs */. MemPage
17ecd 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 *apCopy[NB];
17ece 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 /* Private
17ecf 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b copies of apOld[
17ed0 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d ] pages */. Mem
17ed1 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 Page *apNew[NB+2
17ed2 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 ]; /* pPa
17ed3 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 ge and up to NB
17ed4 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 siblings after b
17ed5 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 75 38 alancing */. u8
17ed6 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 *pRight;
17ed7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
17ed8 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74 cation in parent
17ed9 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c 69 6e of right-siblin
17eda 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 g pointer */. u
17edb 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d 3b 20 8 *apDiv[NB-1];
17edc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
17edd 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 ivider cells in
17ede 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 pParent */. int
17edf 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 cntNew[NB+2];
17ee0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
17ee1 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 ex in aCell[] of
17ee2 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 cell after i-th
17ee3 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 page */. int s
17ee4 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 zNew[NB+2];
17ee5 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 /* Combi
17ee6 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c ned size of cell
17ee7 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 s place on i-th
17ee8 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 page */. u8 **a
17ee9 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 pCell = 0;
17eea 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 /* All ce
17eeb 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 lls begin balanc
17eec 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 ed */. u16 *szC
17eed 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ell;
17eee 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 /* Local si
17eef 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 ze of all cells
17ef0 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 in apCell[] */.
17ef1 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 20 20 u8 *aSpace1;
17ef2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17ef3 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70 69 65 Space for copie
17ef4 73 20 6f 66 20 64 69 76 69 64 65 72 73 20 63 65 s of dividers ce
17ef5 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 lls */. Pgno pg
17ef6 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
17ef7 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 76 61 /* Temp va
17ef8 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70 61 67 r to store a pag
17ef9 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 0a e number in */..
17efa 20 20 70 42 74 20 3d 20 70 50 61 72 65 6e 74 2d pBt = pParent-
17efb 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 >pBt;. assert(
17efc 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17efd 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
17efe 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17eff 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
17f00 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 ble(pParent->pDb
17f01 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a Page) );..#if 0.
17f02 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 TRACE(("BALANC
17f03 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20 25 64 E: begin page %d
17f04 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c child of %d\n",
17f05 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 pPage->pgno, pP
17f06 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23 arent->pgno));.#
17f07 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74 endif.. /* At t
17f08 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72 65 6e his point pParen
17f09 74 20 6d 61 79 20 68 61 76 65 20 61 74 20 6d 6f t may have at mo
17f0a 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20 st one overflow
17f0b 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a cell. And if. *
17f0c 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 * this overflow
17f0d 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e 74 2c cell is present,
17f0e 20 69 74 20 6d 75 73 74 20 62 65 20 74 68 65 20 it must be the
17f0f 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20 cell with . **
17f10 69 6e 64 65 78 20 69 50 61 72 65 6e 74 49 64 78 index iParentIdx
17f11 2e 20 54 68 69 73 20 73 63 65 6e 61 72 69 6f 20 . This scenario
17f12 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 65 6e comes about when
17f13 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 this function.
17f14 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 28 69 ** is called (i
17f15 6e 64 69 72 65 63 74 6c 79 29 20 66 72 6f 6d 20 ndirectly) from
17f16 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 sqlite3BtreeDele
17f17 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 te().. */. ass
17f18 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f ert( pParent->nO
17f19 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 verflow==0 || pP
17f1a 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 arent->nOverflow
17f1b 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==1 );. assert(
17f1c 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 pParent->nOverf
17f1d 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e low==0 || pParen
17f1e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d t->aOvfl[0].idx=
17f1f 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b 0a 0a =iParentIdx );..
17f20 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70 61 63 if( !aOvflSpac
17f21 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
17f22 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
17f23 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 }.. /* Find the
17f24 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 sibling pages t
17f25 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 o balance. Also
17f26 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c 6c 73 locate the cells
17f27 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 20 2a in pParent . *
17f28 2a 20 74 68 61 74 20 64 69 76 69 64 65 20 74 68 * that divide th
17f29 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61 e siblings. An a
17f2a 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
17f2b 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e o find NN siblin
17f2c 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 68 gs on . ** eith
17f2d 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 er side of pPage
17f2e 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 . More siblings
17f2f 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f are taken from o
17f30 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72 ne side, however
17f31 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65 , . ** if there
17f32 20 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 are fewer than
17f33 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 NN siblings on t
17f34 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e 20 49 he other side. I
17f35 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 f pParent. ** h
17f36 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 20 63 as NB or fewer c
17f37 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c hildren then all
17f38 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 children of pPa
17f39 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e 2e 20 rent are taken.
17f3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 . **. ** This
17f3b 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f 70 73 loop also drops
17f3c 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
17f3d 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 ls from the pare
17f3e 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a 20 20 nt page. This.
17f3f 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65 6d 61 ** way, the rema
17f40 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66 75 6e inder of the fun
17f41 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 68 ction does not h
17f42 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 ave to deal with
17f43 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 66 6c any. ** overfl
17f44 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 ow cells in the
17f45 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73 69 6e parent page, sin
17f46 63 65 20 69 66 20 61 6e 79 20 65 78 69 73 74 65 ce if any existe
17f47 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 2a 2a d they will. **
17f48 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 have already be
17f49 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f en removed.. */
17f4a 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74 2d 3e . i = pParent->
17f4b 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 61 72 nOverflow + pPar
17f4c 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66 ent->nCell;. if
17f4d 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e 78 44 ( i<2 ){. nxD
17f4e 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f 6c 64 iv = 0;. nOld
17f4f 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b = i+1;. }else{
17f50 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b 0a 20 . nOld = 3;.
17f51 20 20 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 if( iParentId
17f52 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 x==0 ){
17f53 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 6e . n
17f54 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 xDiv = 0;. }e
17f55 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e 74 49 lse if( iParentI
17f56 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e dx==i ){. n
17f57 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20 20 20 xDiv = i-2;.
17f58 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 78 44 }else{. nxD
17f59 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64 78 2d iv = iParentIdx-
17f5a 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 1;. }. i =
17f5b 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 69 2;. }. if( (i
17f5c 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e +nxDiv-pParent->
17f5d 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 61 72 nOverflow)==pPar
17f5e 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 ent->nCell ){.
17f5f 20 20 70 52 69 67 68 74 20 3d 20 26 70 50 61 72 pRight = &pPar
17f60 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
17f61 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
17f62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
17f63 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65 6c 6c Right = findCell
17f64 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 (pParent, i+nxDi
17f65 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 v-pParent->nOver
17f66 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 67 6e flow);. }. pgn
17f67 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 52 69 o = get4byte(pRi
17f68 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 ght);. while( 1
17f69 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 ){. rc = get
17f6a 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c AndInitPage(pBt,
17f6b 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d pgno, &apOld[i]
17f6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
17f6d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61 70 . memset(ap
17f6e 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 Old, 0, (i+1)*si
17f6f 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b zeof(MemPage*));
17f70 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 . goto bala
17f71 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
17f72 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 }. nMaxCells
17f73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e += 1+apOld[i]->
17f74 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e nCell+apOld[i]->
17f75 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 nOverflow;. i
17f76 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72 f( (i--)==0 ) br
17f77 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b eak;.. if( i+
17f78 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e nxDiv==pParent->
17f79 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26 26 20 aOvfl[0].idx &&
17f7a 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
17f7b 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 70 44 69 ow ){. apDi
17f7c 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e v[i] = pParent->
17f7d 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a aOvfl[0].pCell;.
17f7e 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 pgno = get
17f7f 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 4byte(apDiv[i]);
17f80 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 . szNew[i]
17f81 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 = cellSizePtr(pP
17f82 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 arent, apDiv[i])
17f83 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d ;. pParent-
17f84 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >nOverflow = 0;.
17f85 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
17f86 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 apDiv[i] = find
17f87 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b Cell(pParent, i+
17f88 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e nxDiv-pParent->n
17f89 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 20 20 Overflow);.
17f8a 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 pgno = get4byte
17f8b 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 (apDiv[i]);.
17f8c 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c szNew[i] = cel
17f8d 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 lSizePtr(pParent
17f8e 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 20 , apDiv[i]);..
17f8f 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 /* Drop the
17f90 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 70 61 cell from the pa
17f91 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44 69 76 rent page. apDiv
17f92 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 [i] still points
17f93 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 to. ** the
17f94 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 cell within the
17f95 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 74 68 parent, even th
17f96 6f 75 67 68 20 69 74 20 68 61 73 20 62 65 65 6e ough it has been
17f97 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 20 20 dropped..
17f98 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66 65 20 ** This is safe
17f99 62 65 63 61 75 73 65 20 64 72 6f 70 70 69 6e 67 because dropping
17f9a 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 65 a cell only ove
17f9b 72 77 72 69 74 65 73 20 74 68 65 20 66 69 72 73 rwrites the firs
17f9c 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 t. ** four
17f9d 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61 6e 64 bytes of it, and
17f9e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 this function d
17f9f 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 oes not need the
17fa0 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 first. **
17fa1 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 74 68 four bytes of th
17fa2 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 e divider cell.
17fa3 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 So the pointer i
17fa4 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a 20 20 s safe to use.
17fa5 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f 6e 2e ** later on.
17fa6 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 . **.
17fa7 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51 4c 69 ** Unless SQLi
17fa8 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 te is compiled i
17fa9 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 n secure-delete
17faa 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 mode. In this ca
17fab 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 se,. ** the
17fac 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f 75 74 dropCell() rout
17fad 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 ine will overwri
17fae 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 te the entire ce
17faf 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 0a ll with zeroes..
17fb0 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 ** In this
17fb1 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 72 69 case, temporari
17fb2 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65 6c 6c ly copy the cell
17fb3 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 6c 53 into the aOvflS
17fb4 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a 2a 20 pace[]. **
17fb5 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c 6c 20 buffer. It will
17fb6 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20 61 67 be copied out ag
17fb7 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 ain as soon as t
17fb8 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75 66 66 he aSpace[] buff
17fb9 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 er. ** is a
17fba 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a 23 69 llocated. */.#i
17fbb 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 fdef SQLITE_SECU
17fbc 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 RE_DELETE.
17fbd 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 70 61 memcpy(&aOvflSpa
17fbe 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 ce[apDiv[i]-pPar
17fbf 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61 70 44 ent->aData], apD
17fc0 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 iv[i], szNew[i])
17fc1 3b 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d ;. apDiv[i]
17fc2 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 = &aOvflSpace[a
17fc3 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d pDiv[i]-pParent-
17fc4 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69 66 0a >aData];.#endif.
17fc5 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 dropCell(p
17fc6 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d Parent, i+nxDiv-
17fc7 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
17fc8 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20 26 72 ow, szNew[i], &r
17fc9 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 c);. }. }..
17fca 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c /* Make nMaxCel
17fcb 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 ls a multiple of
17fcc 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 4 in order to p
17fcd 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20 reserve 8-byte.
17fce 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f ** alignment */
17fcf 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 . nMaxCells = (
17fd0 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e nMaxCells + 3)&~
17fd1 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 3;.. /*. ** Al
17fd2 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
17fd3 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 memory structur
17fd4 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 70 42 es. */. k = pB
17fd5 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 52 4f t->pageSize + RO
17fd6 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 UND8(sizeof(MemP
17fd7 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 72 61 74 age));. szScrat
17fd8 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 ch =. nMax
17fd9 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a Cells*sizeof(u8*
17fda 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
17fdb 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c /* apCel
17fdc 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 l */. + nMax
17fdd 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 Cells*sizeof(u16
17fde 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 )
17fdf 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c /* szCel
17fe0 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d l */. + pBt-
17fe1 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20 >pageSize
17fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17fe3 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63 /* aSpac
17fe4 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b 2a 6e e1 */. + k*n
17fe5 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 Old;
17fe6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17fe7 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
17fe8 20 63 6f 70 69 65 73 20 28 61 70 43 6f 70 79 29 copies (apCopy)
17fe9 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 */. apCell = s
17fea 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
17feb 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20 29 loc( szScratch )
17fec 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d ; . if( apCell=
17fed 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
17fee 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
17fef 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
17ff0 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 eanup;. }. szC
17ff1 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 ell = (u16*)&apC
17ff2 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a ell[nMaxCells];.
17ff3 20 20 61 53 70 61 63 65 31 20 3d 20 28 75 38 2a aSpace1 = (u8*
17ff4 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c )&szCell[nMaxCel
17ff5 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 45 ls];. assert( E
17ff6 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
17ff7 45 4e 54 28 61 53 70 61 63 65 31 29 20 29 3b 0a ENT(aSpace1) );.
17ff8 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 . /*. ** Load
17ff9 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 pointers to all
17ffa 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69 6e 67 cells on sibling
17ffb 20 70 61 67 65 73 20 61 6e 64 20 74 68 65 20 64 pages and the d
17ffc 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a ivider cells. *
17ffd 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63 61 6c * into the local
17ffe 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61 79 2e apCell[] array.
17fff 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 20 6f 66 Make copies of
18000 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
18001 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73 70 61 ls. ** into spa
18002 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ce obtained from
18003 20 61 53 70 61 63 65 31 5b 5d 20 61 6e 64 20 72 aSpace1[] and r
18004 65 6d 6f 76 65 20 74 68 65 20 74 68 65 20 64 69 emove the the di
18005 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 20 2a 2a vider Cells. **
18006 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e 0a 20 from pParent..
18007 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 **. ** If the
18008 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6f 6e 20 siblings are on
18009 6c 65 61 66 20 70 61 67 65 73 2c 20 74 68 65 6e leaf pages, then
1800a 20 74 68 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 the child point
1800b 65 72 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 ers of the. **
1800c 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 61 72 divider cells ar
1800d 65 20 73 74 72 69 70 70 65 64 20 66 72 6f 6d 20 e stripped from
1800e 74 68 65 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 the cells before
1800f 20 74 68 65 79 20 61 72 65 20 63 6f 70 69 65 64 they are copied
18010 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63 . ** into aSpac
18011 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73 20 77 e1[]. In this w
18012 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e ay, all cells in
18013 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69 apCell[] are wi
18014 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69 6c 64 thout. ** child
18015 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 66 20 73 pointers. If s
18016 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 iblings are not
18017 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c leaves, then all
18018 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 cell in. ** ap
18019 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63 Cell[] include c
1801a 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 hild pointers.
1801b 45 69 74 68 65 72 20 77 61 79 2c 20 61 6c 6c 20 Either way, all
1801c 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b cells in apCell[
1801d 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65 ]. ** are alike
1801e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 .. **. ** leaf
1801f 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34 20 69 Correction: 4 i
18020 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 f pPage is a lea
18021 66 2e 20 20 30 20 69 66 20 70 50 61 67 65 20 69 f. 0 if pPage i
18022 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a 20 20 s not a leaf..
18023 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 44 61 74 ** leafDat
18024 61 3a 20 20 31 20 69 66 20 70 50 61 67 65 20 68 a: 1 if pPage h
18025 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 20 61 6e olds key+data an
18026 64 20 70 50 61 72 65 6e 74 20 68 6f 6c 64 73 20 d pParent holds
18027 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a only keys.. */.
18028 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e leafCorrection
18029 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c 65 61 = apOld[0]->lea
1802a 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 f*4;. leafData
1802b 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61 73 44 = apOld[0]->hasD
1802c 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 ata;. for(i=0;
1802d 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 i<nOld; i++){.
1802e 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20 20 20 int limit;.
1802f 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72 65 20 . /* Before
18030 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 65 doing anything e
18031 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 6f 70 79 lse, take a copy
18032 20 6f 66 20 74 68 65 20 69 27 74 68 20 6f 72 69 of the i'th ori
18033 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a 20 20 ginal sibling.
18034 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20 6f 66 ** The rest of
18035 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
18036 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66 72 6f ill use data fro
18037 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72 61 74 m the copies rat
18038 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 her. ** that
18039 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
1803a 65 73 20 73 69 6e 63 65 20 74 68 65 20 6f 72 69 es since the ori
1803b 67 69 6e 61 6c 20 70 61 67 65 73 20 77 69 6c 6c ginal pages will
1803c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 20 20 2a be in the. *
1803d 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69 * process of bei
1803e 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 ng overwritten.
1803f 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 */. MemPage
18040 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 *pOld = apCopy[i
18041 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 26 61 ] = (MemPage*)&a
18042 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 Space1[pBt->page
18043 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20 20 20 Size + k*i];.
18044 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70 memcpy(pOld, ap
18045 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d Old[i], sizeof(M
18046 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 70 4f emPage));. pO
18047 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f 69 ld->aData = (voi
18048 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20 20 20 d*)&pOld[1];.
18049 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e 61 44 memcpy(pOld->aD
1804a 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 ata, apOld[i]->a
1804b 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 Data, pBt->pageS
1804c 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d 69 74 ize);.. limit
1804d 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 = pOld->nCell+p
1804e 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a Old->nOverflow;.
1804f 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c for(j=0; j<l
18050 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 imit; j++){.
18051 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c assert( nCell<
18052 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
18053 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d apCell[nCell]
18054 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 = findOverflowC
18055 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 ell(pOld, j);.
18056 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c szCell[nCell
18057 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 ] = cellSizePtr(
18058 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 pOld, apCell[nCe
18059 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c ll]);. nCel
1805a 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 l++;. }. i
1805b 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 f( i<nOld-1 && !
1805c 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 20 20 20 leafData){.
1805d 20 75 31 36 20 73 7a 20 3d 20 28 75 31 36 29 73 u16 sz = (u16)s
1805e 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 zNew[i];. u
1805f 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 8 *pTemp;.
18060 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d assert( nCell<nM
18061 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 axCells );.
18062 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d szCell[nCell] =
18063 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 sz;. pTemp
18064 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 = &aSpace1[iSpa
18065 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 ce1];. iSpa
18066 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 ce1 += sz;.
18067 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 assert( sz<=pBt
18068 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a ->pageSize/4 );.
18069 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 53 assert( iS
1806a 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61 67 65 pace1<=pBt->page
1806b 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 6d 65 Size );. me
1806c 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70 44 69 mcpy(pTemp, apDi
1806d 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 v[i], sz);.
1806e 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d apCell[nCell] =
1806f 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 pTemp+leafCorre
18070 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 73 73 ction;. ass
18071 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 ert( leafCorrect
18072 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f ion==0 || leafCo
18073 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 rrection==4 );.
18074 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c szCell[nCel
18075 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c l] = szCell[nCel
18076 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 63 74 l] - leafCorrect
18077 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 ion;. if( !
18078 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 pOld->leaf ){.
18079 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 assert( le
1807a 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 afCorrection==0
1807b 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
1807c 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 t( pOld->hdrOffs
1807d 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 et==0 );.
1807e 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f /* The right po
1807f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 inter of the chi
18080 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 ld page pOld bec
18081 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 omes the left.
18082 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 ** pointer
18083 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 of the divider
18084 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 cell */.
18085 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 memcpy(apCell[nC
18086 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 ell], &pOld->aDa
18087 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 ta[8], 4);.
18088 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
18089 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 assert( leafCorr
1808a 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 ection==4 );.
1808b 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b if( szCell[
1808c 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 nCell]<4 ){.
1808d 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 /* Do not
1808e 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c 73 20 allow any cells
1808f 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 20 62 smaller than 4 b
18090 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 ytes. */.
18091 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d szCell[nCell]
18092 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 4;. }.
18093 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 }. nC
18094 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ell++;. }. }
18095 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 75 .. /*. ** Figu
18096 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d 62 65 re out the numbe
18097 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 64 65 r of pages neede
18098 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e 43 d to hold all nC
18099 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a 20 ell cells.. **
1809a 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d 62 65 Store this numbe
1809b 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 6f 20 r in "k". Also
1809c 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b 5d 20 compute szNew[]
1809d 77 68 69 63 68 20 69 73 20 74 68 65 20 74 6f 74 which is the tot
1809e 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 al. ** size of
1809f 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 all cells on the
180a0 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 20 63 i-th page and c
180a1 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 ntNew[] which is
180a2 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 the index. **
180a3 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 74 in apCell[] of t
180a4 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 69 76 he cell that div
180a5 69 64 65 73 20 70 61 67 65 20 69 20 66 72 6f 6d ides page i from
180a6 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 20 2a page i+1. . *
180a7 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f 75 * cntNew[k] shou
180a8 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c 2e 0a ld equal nCell..
180a9 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 73 **. ** Values
180aa 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 69 computed by thi
180ab 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 20 s block:. **.
180ac 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b 3a 20 ** k:
180ad 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 The total number
180ae 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 67 65 of sibling page
180af 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 77 5b s. ** szNew[
180b0 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 64 20 i]: Spaced used
180b1 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c on the i-th sibl
180b2 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 20 ing page.. **
180b3 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65 cntNew[i]: Inde
180b4 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e x in apCell[] an
180b5 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74 d szCell[] for t
180b6 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f he first cell to
180b7 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 . **
180b8 20 20 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 the right of
180b9 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 the i-th sibling
180ba 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 61 62 page.. ** usab
180bb 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 72 20 leSpace: Number
180bc 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 of bytes of spac
180bd 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65 e available on e
180be 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 20 2a ach sibling.. *
180bf 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 6c 65 * . */. usable
180c0 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 73 61 Space = pBt->usa
180c1 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b 20 6c bleSize - 12 + l
180c2 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 eafCorrection;.
180c3 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d 6b 3d for(subtotal=k=
180c4 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b i=0; i<nCell; i+
180c5 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 +){. assert(
180c6 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 i<nMaxCells );.
180c7 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d 20 73 subtotal += s
180c8 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 20 zCell[i] + 2;.
180c9 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c 20 3e if( subtotal >
180ca 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 7b 0a usableSpace ){.
180cb 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d szNew[k] =
180cc 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a 43 65 subtotal - szCe
180cd 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 6e 74 ll[i];. cnt
180ce 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 20 New[k] = i;.
180cf 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 20 29 if( leafData )
180d0 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 73 { i--; }. s
180d1 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 ubtotal = 0;.
180d2 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 k++;. if
180d3 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 20 3d ( k>NB+1 ){ rc =
180d4 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b SQLITE_CORRUPT;
180d5 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
180d6 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d 0a 20 eanup; }. }.
180d7 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 }. szNew[k] =
180d8 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e subtotal;. cntN
180d9 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20 ew[k] = nCell;.
180da 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a k++;.. /*. **
180db 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d The packing com
180dc 70 75 74 65 64 20 62 79 20 74 68 65 20 70 72 65 puted by the pre
180dd 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 20 62 vious block is b
180de 69 61 73 65 64 20 74 6f 77 61 72 64 20 74 68 65 iased toward the
180df 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f siblings. ** o
180e0 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 2e n the left side.
180e1 20 20 54 68 65 20 6c 65 66 74 20 73 69 62 6c 69 The left sibli
180e2 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e ngs are always n
180e3 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c early full, whil
180e4 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 68 74 e the. ** right
180e5 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69 -most sibling mi
180e6 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20 65 6d ght be nearly em
180e7 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b pty. This block
180e8 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d 70 74 of code attempt
180e9 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74 s. ** to adjust
180ea 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f 66 20 the packing of
180eb 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 74 20 siblings to get
180ec 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e 63 65 a better balance
180ed 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
180ee 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d adjustment is m
180ef 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 ore than an opti
180f0 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 mization. The p
180f1 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 acking above mig
180f2 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 ht. ** be so ou
180f3 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 73 20 t of balance as
180f4 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 to be illegal.
180f5 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
180f6 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a right-most. **
180f7 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 sibling might b
180f8 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 e completely emp
180f9 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 73 74 ty. This adjust
180fa 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 74 69 ment is not opti
180fb 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 onal.. */. for
180fc 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d (i=k-1; i>0; i--
180fd 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 69 67 ){. int szRig
180fe 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20 ht = szNew[i];
180ff 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 /* Size of sibli
18100 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 ng on the right
18101 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c 65 66 */. int szLef
18102 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20 t = szNew[i-1];
18103 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 /* Size of sibli
18104 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a ng on the left *
18105 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 20 20 /. int r;
18106 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
18107 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 ex of right-most
18108 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69 cell in left si
18109 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 bling */. int
1810a 20 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
1810b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72 /* Index of fir
1810c 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c st cell to the l
1810d 65 66 74 20 6f 66 20 72 69 67 68 74 20 73 69 62 eft of right sib
1810e 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d ling */.. r =
1810f 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 cntNew[i-1] - 1
18110 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 ;. d = r + 1
18111 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 - leafData;.
18112 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 43 65 assert( d<nMaxCe
18113 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 lls );. asser
18114 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 t( r<nMaxCells )
18115 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 7a 52 ;. while( szR
18116 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 ight==0 || szRig
18117 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d ht+szCell[d]+2<=
18118 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 szLeft-(szCell[r
18119 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 73 7a ]+2) ){. sz
1811a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b Right += szCell[
1811b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a d] + 2;. sz
1811c 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 Left -= szCell[r
1811d 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 ] + 2;. cnt
1811e 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 New[i-1]--;.
1811f 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 r = cntNew[i-1
18120 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 20 3d ] - 1;. d =
18121 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 r + 1 - leafDat
18122 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e a;. }. szN
18123 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b ew[i] = szRight;
18124 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 . szNew[i-1]
18125 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 = szLeft;. }..
18126 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 66 6f /* Either we fo
18127 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 und one or more
18128 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d cells (cntnew[0]
18129 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20 69 73 )>0) or pPage is
1812a 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 . ** a virtual
1812b 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 76 69 root page. A vi
1812c 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 rtual root page
1812d 69 73 20 77 68 65 6e 20 74 68 65 20 72 65 61 6c is when the real
1812e 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20 root. ** page
1812f 69 73 20 70 61 67 65 20 31 20 61 6e 64 20 77 65 is page 1 and we
18130 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 63 68 are the only ch
18131 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61 67 65 ild of that page
18132 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
18133 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 cntNew[0]>0 ||
18134 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d (pParent->pgno==
18135 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 1 && pParent->nC
18136 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 54 52 ell==0) );.. TR
18137 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f ACE(("BALANCE: o
18138 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 22 2c ld: %d %d %d ",
18139 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 . apOld[0]->p
1813a 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d gno, . nOld>=
1813b 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e 70 67 2 ? apOld[1]->pg
1813c 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 no : 0,. nOld
1813d 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d 2d 3e >=3 ? apOld[2]->
1813e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b 0a 0a pgno : 0. ));..
1813f 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 /*. ** Alloca
18140 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 te k new pages.
18141 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67 65 73 Reuse old pages
18142 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c 65 2e where possible.
18143 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 70 4f 6c . */. if( apOl
18144 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b d[0]->pgno<=1 ){
18145 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
18146 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 67 6f _CORRUPT;. go
18147 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e to balance_clean
18148 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65 46 6c up;. }. pageFl
18149 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e ags = apOld[0]->
1814a 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 aData[0];. for(
1814b 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a i=0; i<k; i++){.
1814c 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 MemPage *pNe
1814d 77 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c w;. if( i<nOl
1814e 64 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 d ){. pNew
1814f 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f = apNew[i] = apO
18150 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4f ld[i];. apO
18151 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 ld[i] = 0;.
18152 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
18153 65 72 57 72 69 74 65 28 70 4e 65 77 2d 3e 70 44 erWrite(pNew->pD
18154 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 6e 4e bPage);. nN
18155 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 ew++;. if(
18156 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 rc ) goto balanc
18157 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d e_cleanup;. }
18158 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
18159 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 20 20 20 rt( i>0 );.
1815a 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
1815b 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e reePage(pBt, &pN
1815c 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e 6f 2c ew, &pgno, pgno,
1815d 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
1815e 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 c ) goto balance
1815f 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 _cleanup;.
18160 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 77 3b apNew[i] = pNew;
18161 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 0a . nNew++;..
18162 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 /* Set the
18163 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
18164 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20 73 ry for the new s
18165 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a ibling page. */.
18166 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54 4f if( ISAUTO
18167 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
18168 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
18169 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 pNew->pgno, PTR
1816a 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 MAP_BTREE, pPare
1816b 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a nt->pgno, &rc);.
1816c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1816d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1816e 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 goto bala
1816f 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
18170 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
18171 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
18172 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 67 65 ree any old page
18173 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 20 s that were not
18174 72 65 75 73 65 64 20 61 73 20 6e 65 77 20 70 61 reused as new pa
18175 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c ges.. */. whil
18176 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 e( i<nOld ){.
18177 20 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b freePage(apOld[
18178 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 i], &rc);. if
18179 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 ( rc ) goto bala
1817a 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
1817b 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f releasePage(apO
1817c 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 4f 6c ld[i]);. apOl
1817d 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69 2b d[i] = 0;. i+
1817e 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a +;. }.. /*. *
1817f 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20 70 61 * Put the new pa
18180 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 ges in accending
18181 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20 68 65 order. This he
18182 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 lps to. ** keep
18183 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
18184 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64 disk file in ord
18185 65 72 20 73 6f 20 74 68 61 74 20 61 20 73 63 61 er so that a sca
18186 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 n. ** of the ta
18187 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 72 20 ble is a linear
18188 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 scan through the
18189 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 20 2a file. That. *
1818a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 73 20 * in turn helps
1818b 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
1818c 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 72 20 stem to deliver
1818d 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 pages. ** from
1818e 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 72 61 the disk more ra
1818f 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a pidly.. **. **
18190 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 65 72 An O(n^2) inser
18191 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f 72 69 tion sort algori
18192 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62 75 74 thm is used, but
18193 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 73 since. ** n is
18194 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e never more than
18195 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 6f 6e NB (a small con
18196 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73 68 6f stant), that sho
18197 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 uld. ** not be
18198 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a 0a a problem.. **.
18199 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 2c ** When NB==3,
1819a 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 6d 69 this one optimi
1819b 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 68 65 zation makes the
1819c 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 61 database. ** a
1819d 62 6f 75 74 20 32 35 25 20 66 61 73 74 65 72 20 bout 25% faster
1819e 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 72 74 for large insert
1819f 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 69 6f ions and deletio
181a0 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 ns.. */. for(i
181a1 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 7b =0; i<k-1; i++){
181a2 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 3d 20 . int minV =
181a3 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a apNew[i]->pgno;.
181a4 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 int minI = i
181a5 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b ;. for(j=i+1;
181a6 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 j<k; j++){.
181a7 20 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e if( apNew[j]->
181a8 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d pgno<(unsigned)m
181a9 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d inV ){. m
181aa 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 inI = j;.
181ab 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b 6a 5d minV = apNew[j]
181ac 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a ->pgno;. }.
181ad 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 }. if( mi
181ae 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e nI>i ){. in
181af 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 t t;. MemPa
181b0 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 ge *pT;. t
181b1 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f = apNew[i]->pgno
181b2 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 70 4e ;. pT = apN
181b3 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e ew[i];. apN
181b4 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 ew[i] = apNew[mi
181b5 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 nI];. apNew
181b6 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 [minI] = pT;.
181b7 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 28 28 }. }. TRACE((
181b8 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25 64 28 "new: %d(%d) %d(
181b9 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64 %d) %d(%d) %d(%d
181ba 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 ) %d(%d)\n",.
181bb 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c apNew[0]->pgno,
181bc 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e szNew[0],. n
181bd 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31 New>=2 ? apNew[1
181be 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 ]->pgno : 0, nNe
181bf 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 w>=2 ? szNew[1]
181c0 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 : 0,. nNew>=3
181c1 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e ? apNew[2]->pgn
181c2 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f o : 0, nNew>=3 ?
181c3 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 szNew[2] : 0,.
181c4 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e nNew>=4 ? apN
181c5 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c ew[3]->pgno : 0,
181c6 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77 nNew>=4 ? szNew
181c7 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 [3] : 0,. nNe
181c8 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d w>=5 ? apNew[4]-
181c9 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e >pgno : 0, nNew>
181ca 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 =5 ? szNew[4] :
181cb 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 0));.. assert(
181cc 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
181cd 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
181ce 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 70 >pDbPage) );. p
181cf 75 74 34 62 79 74 65 28 70 52 69 67 68 74 2c 20 ut4byte(pRight,
181d0 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 apNew[nNew-1]->p
181d1 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a gno);.. /*. **
181d2 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69 62 75 Evenly distribu
181d3 74 65 20 74 68 65 20 64 61 74 61 20 69 6e 20 61 te the data in a
181d4 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 pCell[] across t
181d5 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 he new pages..
181d6 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 64 65 ** Insert divide
181d7 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 r cells into pPa
181d8 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 61 72 rent as necessar
181d9 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b y.. */. j = 0;
181da 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e . for(i=0; i<nN
181db 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a ew; i++){. /*
181dc 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 Assemble the ne
181dd 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 w sibling page.
181de 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a */. MemPage *
181df 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b pNew = apNew[i];
181e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e . assert( j<n
181e1 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 MaxCells );.
181e2 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 zeroPage(pNew, p
181e3 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 20 61 ageFlags);. a
181e4 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 ssemblePage(pNew
181e5 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26 , cntNew[i]-j, &
181e6 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65 apCell[j], &szCe
181e7 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65 ll[j]);. asse
181e8 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e rt( pNew->nCell>
181e9 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26 0 || (nNew==1 &&
181ea 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29 cntNew[0]==0) )
181eb 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e ;. assert( pN
181ec 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ew->nOverflow==0
181ed 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 );.. j = cnt
181ee 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 New[i];.. /*
181ef 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 If the sibling p
181f0 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 age assembled ab
181f1 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68 65 20 ove was not the
181f2 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 right-most sibli
181f3 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 ng,. ** inser
181f4 74 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c t a divider cell
181f5 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 into the parent
181f6 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
181f7 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e 65 77 assert( i<nNew
181f8 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29 -1 || j==nCell )
181f9 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c ;. if( j<nCel
181fa 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 l ){. u8 *p
181fb 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a Cell;. u8 *
181fc 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 pTemp;. int
181fd 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 sz;.. asse
181fe 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 rt( j<nMaxCells
181ff 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
18200 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 apCell[j];.
18201 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d sz = szCell[j]
18202 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f + leafCorrectio
18203 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d n;. pTemp =
18204 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76 &aOvflSpace[iOv
18205 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 flSpace];.
18206 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 if( !pNew->leaf
18207 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ){. memcp
18208 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 y(&pNew->aData[8
18209 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 ], pCell, 4);.
1820a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 }else if( le
1820b 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 afData ){.
1820c 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65 65 /* If the tree
1820d 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61 20 is a leaf-data
1820e 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73 69 tree, and the si
1820f 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76 65 blings are leave
18210 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 s, . ** t
18211 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hen there is no
18212 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 divider cell in
18213 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 apCell[]. Instea
18214 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20 0a d, the divider .
18215 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 ** cell
18216 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20 consists of the
18217 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 integer key for
18218 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 the right-most c
18219 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20 20 ell of .
1821a 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 ** the sibling-p
1821b 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61 62 age assembled ab
1821c 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 ove only..
1821d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65 6c */. Cel
1821e 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 lInfo info;.
1821f 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 j--;.
18220 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
18221 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b tr(pNew, apCell[
18222 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 j], &info);.
18223 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d pCell = pTem
18224 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 3d 20 p;. sz =
18225 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28 26 70 4 + putVarint(&p
18226 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b Cell[4], info.nK
18227 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 ey);. pTe
18228 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 mp = 0;. }e
18229 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 65 lse{. pCe
1822a 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 ll -= 4;.
1822b 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 73 65 /* Obscure case
1822c 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 for non-leaf-da
1822d 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 68 65 ta trees: If the
1822e 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 cell at pCell w
1822f 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 as. ** pr
18230 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 eviously stored
18231 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 on a leaf node,
18232 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 65 64 and its reported
18233 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 20 20 size was 4.
18234 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 ** bytes, th
18235 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 61 6c en it may actual
18236 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 ly be smaller th
18237 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 an this .
18238 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65 50 61 ** (see btreePa
18239 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 rseCellPtr(), 4
1823a 62 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e bytes is the min
1823b 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 imum size of.
1823c 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c ** any cell
1823d 29 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 ). But it is imp
1823e 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 ortant to pass t
1823f 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 he correct size
18240 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 to . ** i
18241 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 nsertCell(), so
18242 72 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c reparse the cell
18243 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a now.. **
18244 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 . ** Note
18245 20 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e that this can n
18246 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61 ever happen in a
18247 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69 n SQLite data fi
18248 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 le, as all.
18249 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 ** cells are
1824a 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
1824b 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e . It only happen
1824c 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65 s in b-trees use
1824d 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 d. ** to
1824e 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45 evaluate "IN (SE
1824f 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 LECT ...)" and s
18250 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a imilar clauses..
18251 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
18252 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d if( szCell[j]
18253 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==4 ){.
18254 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72 assert(leafCorr
18255 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 ection==4);.
18256 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 sz = cellS
18257 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 izePtr(pParent,
18258 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 pCell);.
18259 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
1825a 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a iOvflSpace += sz
1825b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1825c 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a sz<=pBt->pageSiz
1825d 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 e/4 );. ass
1825e 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63 65 3c ert( iOvflSpace<
1825f 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 =pBt->pageSize )
18260 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 74 43 65 ;. insertCe
18261 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 ll(pParent, nxDi
18262 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 v, pCell, sz, pT
18263 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c emp, pNew->pgno,
18264 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 &rc);. if(
18265 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
18266 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
18267 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 73 73 eanup;. ass
18268 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
18269 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
1826a 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 rent->pDbPage) )
1826b 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 ;.. j++;.
1826c 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 nxDiv++;.
1826d 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
1826e 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 j==nCell );. a
1826f 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b ssert( nOld>0 );
18270 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3e . assert( nNew>
18271 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61 67 65 0 );. if( (page
18272 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 41 46 Flags & PTF_LEAF
18273 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a )==0 ){. u8 *
18274 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 zChild = &apCopy
18275 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b [nOld-1]->aData[
18276 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 8];. memcpy(&
18277 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 apNew[nNew-1]->a
18278 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c Data[8], zChild,
18279 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 4);. }.. if(
1827a 69 73 52 6f 6f 74 20 26 26 20 70 50 61 72 65 6e isRoot && pParen
1827b 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70 t->nCell==0 && p
1827c 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 Parent->hdrOffse
1827d 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 t<=apNew[0]->nFr
1827e 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 ee ){. /* The
1827f 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 root page of th
18280 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 63 6f 6e e b-tree now con
18281 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 tains no cells.
18282 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69 6e 67 The only sibling
18283 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 20 . ** page is
18284 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
18285 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2e 20 43 of the parent. C
18286 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 opy the contents
18287 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 of the. ** c
18288 68 69 6c 64 20 70 61 67 65 20 69 6e 74 6f 20 74 hild page into t
18289 68 65 20 70 61 72 65 6e 74 2c 20 64 65 63 72 65 he parent, decre
1828a 61 73 69 6e 67 20 74 68 65 20 6f 76 65 72 61 6c asing the overal
1828b 6c 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 0a l height of the.
1828c 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 73 74 ** b-tree st
1828d 72 75 63 74 75 72 65 20 62 79 20 6f 6e 65 2e 20 ructure by one.
1828e 54 68 69 73 20 69 73 20 64 65 73 63 72 69 62 65 This is describe
1828f 64 20 61 73 20 74 68 65 20 22 62 61 6c 61 6e 63 d as the "balanc
18290 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20 20 20 e-shallower".
18291 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69 74 68 ** sub-algorith
18292 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75 6d 65 m in some docume
18293 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a ntation.. **.
18294 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 ** If this i
18295 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
18296 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 database, the c
18297 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43 all to copyNodeC
18298 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 20 2a 2a ontent() . **
18299 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65 sets all pointe
1829a 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f r-map entries co
1829b 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 rresponding to d
1829c 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 70 61 atabase image pa
1829d 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f 72 20 ges . ** for
1829e 77 68 69 63 68 20 74 68 65 20 70 6f 69 6e 74 65 which the pointe
1829f 72 20 69 73 20 73 74 6f 72 65 64 20 77 69 74 68 r is stored with
182a0 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 in the content b
182a1 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20 20 20 eing copied..
182a2 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 **. ** The s
182a3 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62 65 6c econd assert bel
182a4 6f 77 20 76 65 72 69 66 69 65 73 20 74 68 61 74 ow verifies that
182a5 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 the child page
182a6 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65 64 0a is defragmented.
182a7 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73 74 20 ** (it must
182a8 62 65 2c 20 61 73 20 69 74 20 77 61 73 20 6a 75 be, as it was ju
182a9 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74 65 64 st reconstructed
182aa 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c 65 50 using assembleP
182ab 61 67 65 28 29 29 2e 20 54 68 69 73 0a 20 20 20 age()). This.
182ac 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 ** is important
182ad 20 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 if the parent p
182ae 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 age happens to b
182af 65 20 70 61 67 65 20 31 20 6f 66 20 74 68 65 20 e page 1 of the
182b0 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 database. **
182b1 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 61 image. */. a
182b2 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31 20 29 ssert( nNew==1 )
182b3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 70 ;. assert( ap
182b4 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 3d 3d New[0]->nFree ==
182b5 20 0a 20 20 20 20 20 20 20 20 28 67 65 74 32 62 . (get2b
182b6 79 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d 3e 61 yte(&apNew[0]->a
182b7 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77 5b 30 Data[5])-apNew[0
182b8 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d 61 70 ]->cellOffset-ap
182b9 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 New[0]->nCell*2)
182ba 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 63 6f 70 . );. cop
182bb 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61 70 4e yNodeContent(apN
182bc 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74 2c 20 ew[0], pParent,
182bd 26 72 63 29 3b 0a 20 20 20 20 66 72 65 65 50 61 &rc);. freePa
182be 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26 72 63 ge(apNew[0], &rc
182bf 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 49 );. }else if( I
182c0 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 SAUTOVACUUM ){.
182c1 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20 70 6f /* Fix the po
182c2 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 inter-map entrie
182c3 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 20 63 65 s for all the ce
182c4 6c 6c 73 20 74 68 61 74 20 77 65 72 65 20 73 68 lls that were sh
182c5 69 66 74 65 64 20 61 72 6f 75 6e 64 2e 20 0a 20 ifted around. .
182c6 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72 65 20 ** There are
182c7 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e several differen
182c8 74 20 74 79 70 65 73 20 6f 66 20 70 6f 69 6e 74 t types of point
182c9 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 74 er-map entries t
182ca 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 hat need to.
182cb 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 ** be dealt with
182cc 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
182cd 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 . Some of these
182ce 68 61 76 65 20 62 65 65 6e 20 73 65 74 20 61 6c have been set al
182cf 72 65 61 64 79 2c 20 62 75 74 0a 20 20 20 20 2a ready, but. *
182d0 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e * many have not.
182d1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 The following i
182d2 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20 20 20 s a summary:.
182d3 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 29 20 **. ** 1)
182d4 54 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f The entries asso
182d5 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 20 ciated with new
182d6 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 74 68 sibling pages th
182d7 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 20 20 20 at were not.
182d8 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e 67 73 ** siblings
182d9 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
182da 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 ion was called.
182db 54 68 65 73 65 20 68 61 76 65 20 61 6c 72 65 61 These have alrea
182dc 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 dy. ** b
182dd 65 65 6e 20 73 65 74 2e 20 57 65 20 64 6f 6e 27 een set. We don'
182de 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 t need to worry
182df 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c 69 6e about old siblin
182e0 67 73 20 74 68 61 74 20 77 65 72 65 0a 20 20 20 gs that were.
182e1 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64 20 74 ** moved t
182e2 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 o the free-list
182e3 2d 20 74 68 65 20 66 72 65 65 50 61 67 65 28 29 - the freePage()
182e4 20 63 6f 64 65 20 68 61 73 20 74 61 6b 65 6e 20 code has taken
182e5 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 care. **
182e6 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20 20 2a of those.. *
182e7 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 54 68 *. ** 2) Th
182e8 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e e pointer-map en
182e9 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 tries associated
182ea 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 with the first
182eb 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a 2a 20 overflow. **
182ec 20 20 20 20 20 70 61 67 65 20 69 6e 20 61 6e 79 page in any
182ed 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 73 overflow chains
182ee 20 75 73 65 64 20 62 79 20 6e 65 77 20 64 69 76 used by new div
182ef 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 68 65 73 ider cells. Thes
182f0 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 68 e . ** h
182f1 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 61 64 79 ave also already
182f2 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 61 72 65 been taken care
182f3 20 6f 66 20 62 79 20 74 68 65 20 69 6e 73 65 72 of by the inser
182f4 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a 20 20 tCell() code..
182f5 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 33 29 **. ** 3)
182f6 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 If the sibling
182f7 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 pages are not le
182f8 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63 aves, then the c
182f9 68 69 6c 64 20 70 61 67 65 73 20 6f 66 0a 20 20 hild pages of.
182fa 20 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c 73 20 ** cells
182fb 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 73 69 stored on the si
182fc 62 6c 69 6e 67 20 70 61 67 65 73 20 6d 61 79 20 bling pages may
182fd 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 need to be updat
182fe 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ed.. **. *
182ff 2a 20 20 20 34 29 20 49 66 20 74 68 65 20 73 69 * 4) If the si
18300 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 bling pages are
18301 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74 not internal int
18302 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20 key nodes, then
18303 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 any. **
18304 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 75 overflow pages u
18305 73 65 64 20 62 79 20 74 68 65 73 65 20 63 65 6c sed by these cel
18306 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 ls may need to b
18307 65 20 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a e updated. **
18308 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61 6c 20 (internal
18309 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76 intkey nodes nev
1830a 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 er contain point
1830b 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 ers to overflow
1830c 70 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 pages).. **.
1830d 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20 74 68 ** 5) If th
1830e 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 e sibling pages
1830f 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 are not leaves,
18310 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 then the pointer
18311 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 -map. **
18312 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 entries for the
18313 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 61 67 right-child pag
18314 65 73 20 6f 66 20 65 61 63 68 20 73 69 62 6c 69 es of each sibli
18315 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 ng may need.
18316 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 20 75 70 ** to be up
18317 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 dated.. **.
18318 20 20 2a 2a 20 43 61 73 65 73 20 31 20 61 6e 64 ** Cases 1 and
18319 20 32 20 61 72 65 20 64 65 61 6c 74 20 77 69 74 2 are dealt wit
1831a 68 20 61 62 6f 76 65 20 62 79 20 6f 74 68 65 72 h above by other
1831b 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 78 74 0a code. The next.
1831c 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64 65 61 ** block dea
1831d 6c 73 20 77 69 74 68 20 63 61 73 65 73 20 33 20 ls with cases 3
1831e 61 6e 64 20 34 20 61 6e 64 20 74 68 65 20 6f 6e and 4 and the on
1831f 65 20 61 66 74 65 72 20 74 68 61 74 2c 20 63 61 e after that, ca
18320 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 20 20 20 se 5. Since.
18321 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 70 6f 69 ** setting a poi
18322 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 69 nter map entry i
18323 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79 20 65 s a relatively e
18324 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61 74 69 xpensive operati
18325 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 on, this. **
18326 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73 20 70 code only sets p
18327 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 ointer map entri
18328 65 73 20 66 6f 72 20 63 68 69 6c 64 20 6f 72 20 es for child or
18329 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74 overflow pages t
1832a 68 61 74 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 hat have. **
1832b 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64 20 62 actually moved b
1832c 65 74 77 65 65 6e 20 70 61 67 65 73 2e 20 20 2a etween pages. *
1832d 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 /. MemPage *p
1832e 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d 3b 0a New = apNew[0];.
1832f 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c MemPage *pOl
18330 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b 0a 20 d = apCopy[0];.
18331 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77 int nOverflow
18332 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c = pOld->nOverfl
18333 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e 65 78 ow;. int iNex
18334 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 tOld = pOld->nCe
18335 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0a ll + nOverflow;.
18336 20 20 20 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f int iOverflo
18337 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f w = (nOverflow ?
18338 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e pOld->aOvfl[0].
18339 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20 20 6a idx : -1);. j
1833a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1833b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1833c 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c /* Current 'ol
1833d 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 d' sibling page
1833e 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20 20 20 */. k = 0;
1833f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
18341 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62 6c 69 rent 'new' sibli
18342 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 66 ng page */. f
18343 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b or(i=0; i<nCell;
18344 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 i++){. int
18345 20 69 73 44 69 76 69 64 65 72 20 3d 20 30 3b 0a isDivider = 0;.
18346 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 3d 3d while( i==
18347 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20 20 20 iNextOld ){.
18348 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 /* Cell i is
18349 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 the cell immedi
1834a 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 ately following
1834b 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e the last cell on
1834c 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 old. **
1834d 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6a 2e 20 sibling page j.
1834e 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 If the siblings
1834f 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 are not leaf pag
18350 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 es of an.
18351 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 ** intkey b-tre
18352 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 20 77 e, then cell i w
18353 61 73 20 61 20 64 69 76 69 64 65 72 20 63 65 6c as a divider cel
18354 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f l. */. pO
18355 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a 5d ld = apCopy[++j]
18356 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 4f ;. iNextO
18357 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 ld = i + !leafDa
18358 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c ta + pOld->nCell
18359 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c + pOld->nOverfl
1835a 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ow;. if(
1835b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 pOld->nOverflow
1835c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 76 ){. nOv
1835d 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e erflow = pOld->n
1835e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 Overflow;.
1835f 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 iOverflow =
18360 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b 20 i + !leafData +
18361 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 pOld->aOvfl[0].i
18362 64 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 dx;. }.
18363 20 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 isDivider
18364 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 = !leafData; .
18365 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 }.. as
18366 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 sert(nOverflow>0
18367 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 || iOverflow<i
18368 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
18369 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 nOverflow<2 || p
1836a 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 Old->aOvfl[0].id
1836b 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 x==pOld->aOvfl[1
1836c 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 ].idx-1);.
1836d 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 assert(nOverflow
1836e 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 <3 || pOld->aOvf
1836f 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e l[1].idx==pOld->
18370 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b aOvfl[2].idx-1);
18371 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 69 4f . if( i==iO
18372 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
18373 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 31 isDivider = 1
18374 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 2d ;. if( (-
18375 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 29 7b -nOverflow)>0 ){
18376 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76 65 72 . iOver
18377 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 20 20 flow++;.
18378 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }. }..
18379 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 5b 6b if( i==cntNew[k
1837a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ] ){. /*
1837b 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 65 Cell i is the ce
1837c 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 ll immediately f
1837d 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 ollowing the las
1837e 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a 20 20 t cell on new.
1837f 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 ** sibling
18380 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68 65 20 page k. If the
18381 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 siblings are not
18382 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 61 leaf pages of a
18383 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 n. ** int
18384 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e key b-tree, then
18385 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64 69 76 cell i is a div
18386 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 ider cell. */.
18387 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 pNew = ap
18388 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 20 20 New[++k];.
18389 20 20 69 66 28 20 21 6c 65 61 66 44 61 74 61 20 if( !leafData
1838a 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
1838b 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
1838c 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 ( j<nOld );.
1838d 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77 assert( k<nNew
1838e 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 );.. /* If
1838f 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6f 72 the cell was or
18390 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 65 72 iginally divider
18391 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 6e 6f cell (and is no
18392 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 20 20 t now) or.
18393 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 ** an overflow c
18394 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 20 63 ell, or if the c
18395 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 64 20 ell was located
18396 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 on a different s
18397 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 ibling. **
18398 70 61 67 65 20 62 65 66 6f 72 65 20 74 68 65 20 page before the
18399 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 6e 20 balancing, then
1839a 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
1839b 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 entries associat
1839c 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 ed. ** with
1839d 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 6f 76 any child or ov
1839e 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e 65 65 erflow pages nee
1839f 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e d to be updated.
183a0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 */. if( i
183a1 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f 6c 64 sDivider || pOld
183a2 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 ->pgno!=pNew->pg
183a3 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 no ){. if
183a4 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 69 6f ( !leafCorrectio
183a5 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 n ){. p
183a6 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 trmapPut(pBt, ge
183a7 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b 69 5d t4byte(apCell[i]
183a8 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c ), PTRMAP_BTREE,
183a9 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 pNew->pgno, &rc
183aa 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
183ab 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b if( szCell[
183ac 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 i]>pNew->minLoca
183ad 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 l ){. p
183ae 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
183af 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c pNew, apCell[i],
183b0 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d &rc);. }
183b1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
183b2 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72 if( !leafCor
183b3 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 rection ){.
183b4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 for(i=0; i<nNew
183b5 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
183b6 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34 62 79 u32 key = get4by
183b7 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e 61 44 te(&apNew[i]->aD
183b8 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 ata[8]);.
183b9 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
183ba 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 key, PTRMAP_BTRE
183bb 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e E, apNew[i]->pgn
183bc 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 7d o, &rc);. }
183bd 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 . }..#if 0.
183be 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 70 43 /* The ptrmapC
183bf 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f 6e 74 heckPages() cont
183c0 61 69 6e 73 20 61 73 73 65 72 74 28 29 20 73 74 ains assert() st
183c1 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 76 65 atements that ve
183c2 72 69 66 79 20 74 68 61 74 0a 20 20 20 20 2a 2a rify that. **
183c3 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d 61 70 all pointer map
183c4 20 70 61 67 65 73 20 61 72 65 20 73 65 74 20 63 pages are set c
183c5 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73 20 69 orrectly. This i
183c6 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c 65 20 s helpful while
183c7 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67 69 6e . ** debuggin
183c8 67 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c g. This is usual
183c9 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65 63 61 ly disabled beca
183ca 75 73 65 20 61 20 63 6f 72 72 75 70 74 20 64 61 use a corrupt da
183cb 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20 20 2a tabase may. *
183cc 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73 65 72 * cause an asser
183cd 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 74 6f t() statement to
183ce 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 fail. */. p
183cf 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 trmapCheckPages(
183d0 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a 20 20 apNew, nNew);.
183d1 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 ptrmapCheckPag
183d2 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31 29 3b es(&pParent, 1);
183d3 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 .#endif. }.. a
183d4 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e ssert( pParent->
183d5 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 41 43 isInit );. TRAC
183d6 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e E(("BALANCE: fin
183d7 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 ished: old=%d ne
183d8 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 w=%d cells=%d\n"
183d9 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f 6c 64 ,. nOld
183da 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b , nNew, nCell));
183db 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 .. /*. ** Clea
183dc 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 75 72 nup before retur
183dd 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e ning.. */.balan
183de 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 ce_cleanup:. sq
183df 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 lite3ScratchFree
183e0 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 (apCell);. for(
183e1 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b i=0; i<nOld; i++
183e2 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 ){. releasePa
183e3 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 ge(apOld[i]);.
183e4 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e }. for(i=0; i<n
183e5 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 New; i++){. r
183e6 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e 65 77 eleasePage(apNew
183e7 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 [i]);. }.. ret
183e8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
183e9 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
183ea 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 is called when t
183eb 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
183ec 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 a b-tree structu
183ed 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c re is.** overful
183ee 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f l (has one or mo
183ef 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 re overflow page
183f0 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 s)..**.** A new
183f1 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 61 6c child page is al
183f2 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 located and the
183f3 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
183f4 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 current root.**
183f5 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 page, including
183f6 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 overflow cells,
183f7 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 are copied into
183f8 74 68 65 20 63 68 69 6c 64 2e 20 54 68 65 20 72 the child. The r
183f9 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 oot.** page is t
183fa 68 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20 hen overwritten
183fb 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d to make it an em
183fc 70 74 79 20 70 61 67 65 20 77 69 74 68 20 74 68 pty page with th
183fd 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a e right-child .*
183fe 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 * pointer pointi
183ff 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 ng to the new pa
18400 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 ge..**.** Before
18401 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 returning, all
18402 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
18403 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ies correspondin
18404 67 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 g to pages .** t
18405 68 61 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c hat the new chil
18406 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 d-page now conta
18407 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ins pointers to
18408 61 72 65 20 75 70 64 61 74 65 64 2e 20 54 68 65 are updated. The
18409 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73 .** entry corres
1840a 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e ponding to the n
1840b 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 ew right-child p
1840c 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f ointer of the ro
1840d 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c ot.** page is al
1840e 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a so updated..**.*
1840f 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c * If successful,
18410 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 *ppChild is set
18411 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 to contain a re
18412 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 ference to the c
18413 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e hild .** page an
18414 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 d SQLITE_OK is r
18415 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 eturned. In this
18416 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 case the caller
18417 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 is required.**
18418 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 to call releaseP
18419 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c age() on *ppChil
1841a 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 d exactly once.
1841b 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
1841c 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 rs,.** an error
1841d 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
1841e 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 69 73 and *ppChild is
1841f 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 set to 0..*/.st
18420 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 atic int balance
18421 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 67 65 20 _deeper(MemPage
18422 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 *pRoot, MemPage
18423 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e **ppChild){. in
18424 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
18425 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18426 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f Return value fro
18427 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 m subprocedures
18428 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 */. MemPage *pC
18429 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 hild = 0;
1842a 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1842b 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 o a new child pa
1842c 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e ge */. Pgno pgn
1842d 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 oChild = 0;
1842e 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
1842f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 umber of the new
18430 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 child page */.
18431 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
18432 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 pRoot->pBt;
18433 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a /* The BTree */.
18434 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 . assert( pRoot
18435 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b ->nOverflow>0 );
18436 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
18437 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
18438 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 t->mutex) );..
18439 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 /* Make pRoot, t
1843a 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
1843b 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74 the b-tree, writ
1843c 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 able. Allocate a
1843d 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 new . ** page
1843e 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 that will become
1843f 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63 the new right-c
18440 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20 43 hild of pPage. C
18441 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 opy the contents
18442 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 . ** of the nod
18443 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f e stored on pRoo
18444 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 t into the new c
18445 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a hild page.. */.
18446 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
18447 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e gerWrite(pRoot->
18448 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 pDbPage);. if(
18449 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1844a 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 . rc = alloca
1844b 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
1844c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 &pChild,&pgnoChi
1844d 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 ld,pRoot->pgno,0
1844e 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 );. copyNodeC
1844f 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 ontent(pRoot, pC
18450 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 hild, &rc);.
18451 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
18452 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ){. ptrmap
18453 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 Put(pBt, pgnoChi
18454 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 ld, PTRMAP_BTREE
18455 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 , pRoot->pgno, &
18456 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rc);. }. }.
18457 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a if( rc ){. *
18458 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 ppChild = 0;.
18459 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 releasePage(pCh
1845a 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ild);. return
1845b 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
1845c 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
1845d 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 69 6c swriteable(pChil
1845e 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 d->pDbPage) );.
1845f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18460 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
18461 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
18462 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
18463 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f hild->nCell==pRo
18464 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 ot->nCell );..
18465 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a TRACE(("BALANCE:
18466 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 69 6e copy root %d in
18467 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d to %d\n", pRoot-
18468 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 >pgno, pChild->p
18469 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 gno));.. /* Cop
1846a 79 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 y the overflow c
1846b 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 ells from pRoot
1846c 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d to pChild */. m
1846d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f emcpy(pChild->aO
1846e 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f 76 66 vfl, pRoot->aOvf
1846f 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 l, pRoot->nOverf
18470 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f 6f 74 low*sizeof(pRoot
18471 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20 20 ->aOvfl[0]));.
18472 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f pChild->nOverflo
18473 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 w = pRoot->nOver
18474 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f flow;.. /* Zero
18475 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
18476 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69 6e 73 pRoot. Then ins
18477 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 73 20 74 tall pChild as t
18478 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 2e 20 he right-child.
18479 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52 */. zeroPage(pR
1847a 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 oot, pChild->aDa
1847b 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41 ta[0] & ~PTF_LEA
1847c 46 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 F);. put4byte(&
1847d 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f pRoot->aData[pRo
1847e 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ot->hdrOffset+8]
1847f 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 0a 20 , pgnoChild);..
18480 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 68 69 *ppChild = pChi
18481 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ld;. return SQL
18482 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
18483 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 70 The page that p
18484 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f Cur currently po
18485 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 73 74 ints to has just
18486 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 69 been modified i
18487 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e 20 54 n.** some way. T
18488 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 67 his function fig
18489 75 72 65 73 20 6f 75 74 20 69 66 20 74 68 69 73 ures out if this
1848a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 65 modification me
1848b 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 65 20 ans the.** tree
1848c 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c 61 needs to be bala
1848d 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73 6f 20 nced, and if so
1848e 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72 6f 70 calls the approp
1848f 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67 20 riate balancing
18490 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42 61 6c .** routine. Bal
18491 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 ancing routines
18492 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 61 6c are:.**.** bal
18493 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a 2a 20 ance_quick().**
18494 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 balance_deeper
18495 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f ().** balance_
18496 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73 74 61 nonroot().*/.sta
18497 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 tic int balance(
18498 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
18499 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1849a 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 69 TE_OK;. const i
1849b 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e nt nMin = pCur->
1849c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
1849d 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 20 61 42 * 2 / 3;. u8 aB
1849e 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 alanceQuickSpace
1849f 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46 72 65 [13];. u8 *pFre
184a0 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54 4f 4e e = 0;.. TESTON
184a1 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f LY( int balance_
184a2 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d 20 30 quick_called = 0
184a3 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 );. TESTONLY(
184a4 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 int balance_deep
184a5 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b er_called = 0 );
184a6 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 .. do {. int
184a7 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 iPage = pCur->i
184a8 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 Page;. MemPag
184a9 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d e *pPage = pCur-
184aa 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 3b 0a >apPage[iPage];.
184ab 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 3d 3d . if( iPage==
184ac 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
184ad 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
184ae 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
184af 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
184b0 68 65 20 62 2d 74 72 65 65 20 69 73 20 6f 76 65 he b-tree is ove
184b1 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 rfull. In this c
184b2 61 73 65 20 63 61 6c 6c 20 74 68 65 0a 20 20 20 ase call the.
184b3 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f ** balance_
184b4 64 65 65 70 65 72 28 29 20 66 75 6e 63 74 69 6f deeper() functio
184b5 6e 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 n to create a ne
184b6 77 20 63 68 69 6c 64 20 66 6f 72 20 74 68 65 20 w child for the
184b7 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 root-page.
184b8 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20 74 68 ** and copy th
184b9 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e e current conten
184ba 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 ts of the root-p
184bb 61 67 65 20 74 6f 20 69 74 2e 20 54 68 65 0a 20 age to it. The.
184bc 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 ** next i
184bd 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 teration of the
184be 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c do-loop will bal
184bf 61 6e 63 65 20 74 68 65 20 63 68 69 6c 64 20 70 ance the child p
184c0 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 20 age.. */
184c1 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
184c2 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 (balance_deeper
184c3 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b _called++)==0 );
184c4 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 . rc = ba
184c5 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70 50 61 lance_deeper(pPa
184c6 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 ge, &pCur->apPag
184c7 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 e[1]);. i
184c8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
184c9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 ){. pC
184ca 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b 0a 20 ur->iPage = 1;.
184cb 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 pCur->a
184cc 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 iIdx[0] = 0;.
184cd 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 pCur->aiI
184ce 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20 dx[1] = 0;.
184cf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 assert( pCu
184d0 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e 6e 4f r->apPage[1]->nO
184d1 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20 20 20 verflow );.
184d2 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
184d3 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b {. break;
184d4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
184d5 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f se if( pPage->nO
184d6 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 verflow==0 && pP
184d7 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e age->nFree<=nMin
184d8 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ){. break;
184d9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
184da 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73 MemPage * cons
184db 74 20 70 50 61 72 65 6e 74 20 3d 20 70 43 75 72 t pParent = pCur
184dc 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 2d 31 ->apPage[iPage-1
184dd 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e ];. int con
184de 73 74 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e st iIdx = pCur->
184df 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0a aiIdx[iPage-1];.
184e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
184e1 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
184e2 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b arent->pDbPage);
184e3 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
184e4 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 6e QLITE_OK ){.#ifn
184e5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
184e6 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20 20 20 QUICKBALANCE.
184e7 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
184e8 68 61 73 44 61 74 61 0a 20 20 20 20 20 20 20 20 hasData.
184e9 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 && pPage->nOver
184ea 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20 20 20 flow==1.
184eb 20 26 26 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c && pPage->aOvfl
184ec 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e [0].idx==pPage->
184ed 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 26 nCell. &
184ee 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 & pParent->pgno!
184ef 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 =1. && p
184f0 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 69 Parent->nCell==i
184f1 49 64 78 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 Idx. ){.
184f2 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c /* Call
184f3 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 balance_quick()
184f4 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 to create a new
184f5 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 50 61 67 sibling of pPag
184f6 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 20 20 20 e on which.
184f7 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65 ** to store
184f8 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 the overflow ce
184f9 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 ll. balance_quic
184fa 6b 28 29 20 69 6e 73 65 72 74 73 20 61 20 6e 65 k() inserts a ne
184fb 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 w cell.
184fc 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 ** into pParent
184fd 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 75 73 , which may caus
184fe 65 20 70 50 61 72 65 6e 74 20 6f 76 65 72 66 6c e pParent overfl
184ff 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 ow. If this.
18500 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 ** happens
18501 2c 20 74 68 65 20 6e 65 78 74 20 69 6e 74 65 72 , the next inter
18502 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d ation of the do-
18503 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 loop will balanc
18504 65 20 70 50 61 72 65 6e 74 20 0a 20 20 20 20 20 e pParent .
18505 20 20 20 20 20 2a 2a 20 75 73 65 20 65 69 74 68 ** use eith
18506 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f er balance_nonro
18507 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f ot() or balance_
18508 64 65 65 70 65 72 28 29 2e 20 55 6e 74 69 6c 20 deeper(). Until
18509 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a this. *
1850a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 6f * happens, the o
1850b 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 verflow cell is
1850c 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 61 42 stored in the aB
1850d 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 alanceQuickSpace
1850e 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 []. **
1850f 62 75 66 66 65 72 2e 20 0a 20 20 20 20 20 20 20 buffer. .
18510 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 **.
18511 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f ** The purpose o
18512 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
18513 61 73 73 65 72 74 28 29 20 69 73 20 74 6f 20 63 assert() is to c
18514 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79 20 61 heck that only a
18515 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69 . ** si
18516 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c ngle call to bal
18517 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 73 20 ance_quick() is
18518 6d 61 64 65 20 66 6f 72 20 65 61 63 68 20 63 61 made for each ca
18519 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 20 20 20 ll to this.
1851a 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e ** function
1851b 2e 20 49 66 20 74 68 69 73 20 77 65 72 65 20 6e . If this were n
1851c 6f 74 20 76 65 72 69 66 69 65 64 2c 20 61 20 73 ot verified, a s
1851d 75 62 74 6c 65 20 62 75 67 20 69 6e 76 6f 6c 76 ubtle bug involv
1851e 69 6e 67 20 72 65 75 73 65 0a 20 20 20 20 20 20 ing reuse.
1851f 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 61 42 ** of the aB
18520 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 alanceQuickSpace
18521 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b 20 69 [] might sneak i
18522 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a n.. */.
18523 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
18524 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b ( (balance_quick
18525 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b _called++)==0 );
18526 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
18527 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 50 balance_quick(pP
18528 61 72 65 6e 74 2c 20 70 50 61 67 65 2c 20 61 42 arent, pPage, aB
18529 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 63 65 alanceQuickSpace
1852a 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
1852b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
1852c 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
1852d 6e 20 74 68 69 73 20 63 61 73 65 2c 20 63 61 6c n this case, cal
1852e 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f l balance_nonroo
1852f 74 28 29 20 74 6f 20 72 65 64 69 73 74 72 69 62 t() to redistrib
18530 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 ute cells.
18531 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e 20 70 ** between p
18532 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 32 Page and up to 2
18533 20 6f 66 20 69 74 73 20 73 69 62 6c 69 6e 67 20 of its sibling
18534 70 61 67 65 73 2e 20 54 68 69 73 20 69 6e 76 6f pages. This invo
18535 6c 76 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a lves. *
18536 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 * modifying the
18537 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 61 72 contents of pPar
18538 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 ent, which may c
18539 61 75 73 65 20 70 50 61 72 65 6e 74 20 74 6f 0a ause pParent to.
1853a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 ** bec
1853b 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 ome overfull or
1853c 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e underfull. The n
1853d 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 ext iteration of
1853e 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20 20 20 the do-loop.
1853f 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 ** will b
18540 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 65 6e alance the paren
18541 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72 65 63 t page to correc
18542 74 20 74 68 69 73 2e 0a 20 20 20 20 20 20 20 20 t this..
18543 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 20 20 ** .
18544 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 ** If the parent
18545 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 page becomes ov
18546 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76 65 72 erfull, the over
18547 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63 65 6c flow cell or cel
18548 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ls. **
18549 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 are stored in th
1854a 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 e pSpace buffer
1854b 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65 64 69 allocated immedi
1854c 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a 20 20 ately below. .
1854d 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 75 62 ** A sub
1854e 73 65 71 75 65 6e 74 20 69 74 65 72 61 74 69 6f sequent iteratio
1854f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 n of the do-loop
18550 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 74 68 20 will deal with
18551 74 68 69 73 20 62 79 0a 20 20 20 20 20 20 20 20 this by.
18552 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62 61 6c ** calling bal
18553 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 28 ance_nonroot() (
18554 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 balance_deeper()
18555 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 66 may be called f
18556 69 72 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 irst,.
18557 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 6e 27 ** but it doesn'
18558 74 20 64 65 61 6c 20 77 69 74 68 20 6f 76 65 72 t deal with over
18559 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a 75 73 flow cells - jus
1855a 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 74 6f 20 t moves them to
1855b 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 64 a. ** d
1855c 69 66 66 65 72 65 6e 74 20 70 61 67 65 29 2e 20 ifferent page).
1855d 4f 6e 63 65 20 74 68 69 73 20 73 75 62 73 65 71 Once this subseq
1855e 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62 61 6c uent call to bal
1855f 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 0a ance_nonroot() .
18560 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73 ** has
18561 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74 20 69 completed, it i
18562 73 20 73 61 66 65 20 74 6f 20 72 65 6c 65 61 73 s safe to releas
18563 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 e the pSpace buf
18564 66 65 72 20 75 73 65 64 20 62 79 0a 20 20 20 20 fer used by.
18565 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 ** the pre
18566 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73 20 74 vious call, as t
18567 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c he overflow cell
18568 20 64 61 74 61 20 77 69 6c 6c 20 68 61 76 65 20 data will have
18569 62 65 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 been .
1856a 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 68 65 72 ** copied either
1856b 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f into the body o
1856c 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 f a database pag
1856d 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6e 65 e or into the ne
1856e 77 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 w. ** p
1856f 53 70 61 63 65 20 62 75 66 66 65 72 20 70 61 73 Space buffer pas
18570 73 65 64 20 74 6f 20 74 68 65 20 6c 61 74 74 65 sed to the latte
18571 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 r call to balanc
18572 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20 20 20 e_nonroot()..
18573 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
18574 20 20 20 20 75 38 20 2a 70 53 70 61 63 65 20 3d u8 *pSpace =
18575 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c sqlite3PageMall
18576 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61 oc(pCur->pBt->pa
18577 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 geSize);.
18578 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f rc = balance_
18579 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c nonroot(pParent,
1857a 20 69 49 64 78 2c 20 70 53 70 61 63 65 2c 20 69 iIdx, pSpace, i
1857b 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20 20 20 Page==1);.
1857c 20 20 20 20 69 66 28 20 70 46 72 65 65 20 29 7b if( pFree ){
1857d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
1857e 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f 74 20 If pFree is not
1857f 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 NULL, it points
18580 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75 to the pSpace bu
18581 66 66 65 72 20 75 73 65 64 20 0a 20 20 20 20 20 ffer used .
18582 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61 20 70 ** by a p
18583 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 revious call to
18584 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 balance_nonroot(
18585 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 ). Its contents
18586 61 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 are.
18587 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 65 69 ** now stored ei
18588 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 61 74 ther on real dat
18589 61 62 61 73 65 20 70 61 67 65 73 20 6f 72 20 77 abase pages or w
1858a 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20 20 20 ithin the .
1858b 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 70 53 ** new pS
1858c 70 61 63 65 20 62 75 66 66 65 72 2c 20 73 6f 20 pace buffer, so
1858d 69 74 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 it may be safely
1858e 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a 2f 0a freed here. */.
1858f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
18590 74 65 33 50 61 67 65 46 72 65 65 28 70 46 72 65 te3PageFree(pFre
18591 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a e);. }.
18592 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
18593 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 e pSpace buffer
18594 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 61 66 will be freed af
18595 74 65 72 20 74 68 65 20 6e 65 78 74 20 63 61 6c ter the next cal
18596 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a l to. *
18597 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f * balance_nonroo
18598 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 65 66 t(), or just bef
18599 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f ore this functio
1859a 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69 63 68 n returns, which
1859b 65 76 65 72 0a 20 20 20 20 20 20 20 20 20 20 2a ever. *
1859c 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 20 2a * comes first. *
1859d 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 65 /. pFre
1859e 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 20 20 e = pSpace;.
1859f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 }. }..
185a0 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 pPage->nOve
185a1 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20 rflow = 0;..
185a2 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 74 /* The next it
185a3 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 eration of the d
185a4 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 73 20 o-loop balances
185a5 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
185a6 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 */. releas
185a7 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
185a8 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 2d pCur->iPage-
185a9 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c -;. }. }whil
185aa 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b e( rc==SQLITE_OK
185ab 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 65 65 );.. if( pFree
185ac 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ){. sqlite3P
185ad 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3b 0a ageFree(pFree);.
185ae 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
185af 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 .}.../*.** Inser
185b0 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 t a new record i
185b1 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20 20 nto the BTree.
185b2 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65 6e The key is given
185b3 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a by (pKey,nKey).
185b4 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 ** and the data
185b5 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44 61 is given by (pDa
185b6 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65 20 ta,nData). The
185b7 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20 6f cursor is used o
185b8 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 nly to.** define
185b9 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65 20 what table the
185ba 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 record should be
185bb 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 inserted into.
185bc 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 The cursor.** i
185bd 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
185be 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 at a random loca
185bf 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 tion..**.** For
185c0 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c an INTKEY table,
185c1 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 only the nKey v
185c2 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20 alue of the key
185c3 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20 69 is used. pKey i
185c4 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 s.** ignored. F
185c5 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74 61 or a ZERODATA ta
185c6 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20 61 ble, the pData a
185c7 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f 74 nd nData are bot
185c8 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a h ignored..**.**
185c9 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 75 If the seekResu
185ca 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 lt parameter is
185cb 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 non-zero, then a
185cc 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
185cd 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 6e 70 to.** MovetoUnp
185ce 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65 6b 20 acked() to seek
185cf 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f 20 28 cursor pCur to (
185d0 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 73 20 pKey, nKey) has
185d1 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 already.** been
185d2 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 6b 52 performed. seekR
185d3 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 65 61 esult is the sea
185d4 72 63 68 20 72 65 73 75 6c 74 20 72 65 74 75 72 rch result retur
185d5 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76 65 0a ned (a negative.
185d6 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 43 75 ** number if pCu
185d7 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 r points at an e
185d8 6e 74 72 79 20 74 68 61 74 20 69 73 20 73 6d 61 ntry that is sma
185d9 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 79 2c ller than (pKey,
185da 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 61 20 nKey), or.** a
185db 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 69 positive value i
185dc 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 f pCur points at
185dd 20 61 6e 20 65 74 72 79 20 74 68 61 74 20 69 73 an etry that is
185de 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a larger than .**
185df 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 2e 20 (pKey, nKey)).
185e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 .**.** If the se
185e1 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 ekResult paramet
185e2 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 er is non-zero,
185e3 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 then the caller
185e4 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a guarantees that.
185e5 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 ** cursor pCur i
185e6 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 s pointing at th
185e7 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70 79 20 e existing copy
185e8 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20 69 73 of a row that is
185e9 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 to be.** overwr
185ea 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65 20 73 itten. If the s
185eb 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 eekResult parame
185ec 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 63 ter is 0, then c
185ed 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79 0a 2a ursor pCur may.*
185ee 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20 65 * point to any e
185ef 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 65 6e ntry or to no en
185f0 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 20 73 try at all and s
185f1 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
185f2 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 has to seek.** t
185f3 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f 72 65 he cursor before
185f4 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63 61 6e the new key can
185f5 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f be inserted..*/
185f6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
185f7 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
185f8 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75 72 73 Insert(. BtCurs
185f9 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 or *pCur,
185fa 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 /* Inse
185fb 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 rt data into the
185fc 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 table of this c
185fd 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 ursor */. const
185fe 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 void *pKey, i64
185ff 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 nKey, /* The
18600 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 77 20 key of the new
18601 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 record */. cons
18602 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 t void *pData, i
18603 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 nt nData, /* Th
18604 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 6e 65 e data of the ne
18605 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e w record */. in
18606 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 t nZero,
18607 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18608 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 Number of extra
18609 30 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 0 bytes to appen
1860a 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 d to data */. i
1860b 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20 nt appendBias,
1860c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1860d 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 True if this is
1860e 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e likely an appen
1860f 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 d */. int seekR
18610 65 73 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 esult
18611 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
18612 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 6f 55 of prior MovetoU
18613 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c 20 2a npacked() call *
18614 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
18615 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 int loc = seekR
18616 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 esult;
18617 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 64 65 /* -1: before de
18618 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20 sired location
18619 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 20 69 +1: after */. i
1861a 6e 74 20 73 7a 4e 65 77 20 3d 20 30 3b 0a 20 20 nt szNew = 0;.
1861b 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 int idx;. MemPa
1861c 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 ge *pPage;. Btr
1861d 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 ee *p = pCur->pB
1861e 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 tree;. BtShared
1861f 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
18620 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
18621 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 *oldCell;. unsi
18622 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 gned char *newCe
18623 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 ll = 0;.. if( p
18624 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
18625 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 SOR_FAULT ){.
18626 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 assert( pCur->s
18627 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f kipNext!=SQLITE_
18628 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e OK );. return
18629 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
1862a 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 . }.. assert(
1862b 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
1862c 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
1862d 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 rt( pCur->wrFlag
1862e 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 && pBt->inTrans
1862f 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
18630 49 54 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 ITE && !pBt->rea
18631 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 dOnly );. asser
18632 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 t( hasSharedCach
18633 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 eTableLock(p, pC
18634 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 ur->pgnoRoot, pC
18635 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c ur->pKeyInfo!=0,
18636 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 2) );.. /* Ass
18637 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c ert that the cal
18638 6c 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e ler has been con
18639 73 69 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 sistent. If this
1863a 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e cursor was open
1863b 65 64 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e ed. ** expectin
1863c 67 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 g an index b-tre
1863d 65 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c e, then the call
1863e 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 er should be ins
1863f 65 72 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a erting blob. **
18640 20 6b 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 keys with no as
18641 73 6f 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 sociated data. I
18642 66 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 f the cursor was
18643 20 6f 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e opened expectin
18644 67 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 g an. ** intkey
18645 20 74 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c table, the call
18646 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 er should be ins
18647 65 72 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b erting integer k
18648 65 79 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 eys with a. **
18649 62 6c 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 blob of associat
1864a 65 64 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 ed data. */. a
1864b 73 73 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 ssert( (pKey==0)
1864c 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 ==(pCur->pKeyInf
1864d 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 o==0) );.. /* I
1864e 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 f this is an ins
1864f 65 72 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 ert into a table
18650 20 62 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 b-tree, invalid
18651 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 ate any incrblob
18652 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f . ** cursors o
18653 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 pen on the row b
18654 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 eing replaced (a
18655 73 73 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 ssuming this is
18656 61 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f a replace. ** o
18657 70 65 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 peration - if it
18658 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c is not, the fol
18659 6c 6f 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f lowing is a no-o
1865a 70 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 p). */. if( pC
1865b 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 ur->pKeyInfo==0
1865c 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 ){. invalidat
1865d 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 eIncrblobCursors
1865e 28 70 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 (p, nKey, 0);.
1865f 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 }.. /* Save the
18660 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e positions of an
18661 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 y other cursors
18662 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 open on this tab
18663 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e le.. **. ** In
18664 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 some cases, the
18665 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f call to btreeMo
18666 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 veto() below is
18667 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a a no-op. For. *
18668 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 * example, when
18669 69 6e 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 inserting data i
1866a 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 nto a table with
1866b 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 auto-generated
1866c 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 integer. ** key
1866d 73 2c 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 s, the VDBE laye
1866e 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 r invokes sqlite
1866f 33 42 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 3BtreeLast() to
18670 66 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a figure out the .
18671 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 ** integer key
18672 20 74 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e to use. It then
18673 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 calls this func
18674 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 tion to actually
18675 20 69 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a insert the . *
18676 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 * data into the
18677 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 intkey B-Tree. I
18678 6e 20 74 68 69 73 20 63 61 73 65 20 62 74 72 65 n this case btre
18679 65 4d 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e eMoveto() recogn
1867a 69 7a 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 izes. ** that t
1867b 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 he cursor is alr
1867c 65 61 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 eady where it ne
1867d 65 64 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 eds to be and re
1867e 74 75 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 turns without.
1867f 2a 2a 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 ** doing any wor
18680 6b 2e 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 k. To avoid thwa
18681 72 74 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 rting these opti
18682 6d 69 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 mizations, it is
18683 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 important. **
18684 6e 6f 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 not to clear the
18685 20 63 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 cursor here..
18686 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c */. rc = saveAl
18687 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 lCursors(pBt, pC
18688 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 ur->pgnoRoot, pC
18689 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 ur);. if( rc )
1868a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 return rc;. if(
1868b 20 21 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 !loc ){. rc
1868c 3d 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 = btreeMoveto(pC
1868d 75 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 ur, pKey, nKey,
1868e 61 70 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 appendBias, &loc
1868f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
18690 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
18691 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
18692 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
18693 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 LID || (pCur->eS
18694 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 tate==CURSOR_INV
18695 41 4c 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a ALID && loc) );.
18696 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
18697 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18698 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 age];. assert(
18699 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c pPage->intKey ||
1869a 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 nKey>=0 );. as
1869b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 sert( pPage->lea
1869c 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 f || !pPage->int
1869d 4b 65 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 Key );.. TRACE(
1869e 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d ("INSERT: table=
1869f 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 %d nkey=%lld nda
186a0 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 ta=%d page=%d %s
186a1 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 \n",. p
186a2 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e Cur->pgnoRoot, n
186a3 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 Key, nData, pPag
186a4 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 e->pgno,.
186a5 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 loc==0 ? "ove
186a6 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 rwrite" : "new e
186a7 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 ntry"));. asser
186a8 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 t( pPage->isInit
186a9 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 );. allocateTe
186aa 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 mpSpace(pBt);.
186ab 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 newCell = pBt->p
186ac 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 TmpSpace;. if(
186ad 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 newCell==0 ) ret
186ae 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
186af 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 ;. rc = fillInC
186b0 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 ell(pPage, newCe
186b1 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 ll, pKey, nKey,
186b2 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a pData, nData, nZ
186b3 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 ero, &szNew);.
186b4 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e if( rc ) goto en
186b5 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 d_insert;. asse
186b6 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 rt( szNew==cellS
186b7 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 izePtr(pPage, ne
186b8 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 wCell) );. asse
186b9 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 rt( szNew<=MX_CE
186ba 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a LL_SIZE(pBt) );.
186bb 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 idx = pCur->ai
186bc 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
186bd 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 ;. if( loc==0 )
186be 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b {. u16 szOld;
186bf 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
186c0 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b <pPage->nCell );
186c1 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
186c2 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 3PagerWrite(pPag
186c3 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
186c4 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
186c5 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 goto end_insert
186c6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 ;. }. oldC
186c7 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
186c8 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 Page, idx);.
186c9 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
186ca 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ){. memcpy
186cb 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c (newCell, oldCel
186cc 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 l, 4);. }.
186cd 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a szOld = cellSiz
186ce 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 ePtr(pPage, oldC
186cf 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 ell);. rc = c
186d0 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 learCell(pPage,
186d1 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 oldCell);. dr
186d2 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 opCell(pPage, id
186d3 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a x, szOld, &rc);.
186d4 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
186d5 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 o end_insert;.
186d6 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 }else if( loc<0
186d7 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e && pPage->nCell>
186d8 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
186d9 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a pPage->leaf );.
186da 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 idx = ++pCur
186db 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
186dc 61 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 age];. }else{.
186dd 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
186de 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 ->leaf );. }.
186df 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 insertCell(pPage
186e0 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 , idx, newCell,
186e1 73 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 szNew, 0, 0, &rc
186e2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 );. assert( rc!
186e3 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 =SQLITE_OK || pP
186e4 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 age->nCell>0 ||
186e5 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
186e6 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e >0 );.. /* If n
186e7 6f 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 o error has occu
186e8 72 65 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 red and pPage ha
186e9 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 s an overflow ce
186ea 6c 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 ll, call balance
186eb 28 29 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 () . ** to redi
186ec 73 74 72 69 62 75 74 65 20 74 68 65 20 63 65 6c stribute the cel
186ed 6c 73 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 ls within the tr
186ee 65 65 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 ee. Since balanc
186ef 65 28 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a e() may move. *
186f0 2a 20 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 * the cursor, ze
186f1 72 6f 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e ro the BtCursor.
186f2 69 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 info.nSize and B
186f3 74 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 tCursor.validNKe
186f4 79 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 y. ** variables
186f5 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 .. **. ** Prev
186f6 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 ious versions of
186f7 20 53 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d SQLite called m
186f8 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d oveToRoot() to m
186f9 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 ove the cursor.
186fa 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 ** back to the
186fb 72 6f 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c root page as bal
186fc 61 6e 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 ance() used to i
186fd 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f nvalidate the co
186fe 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 ntents. ** of B
186ff 74 43 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d tCursor.apPage[]
18700 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 and BtCursor.ai
18701 49 64 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f Idx[]. Instead o
18702 66 20 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 f doing that,.
18703 2a 2a 20 73 65 74 20 74 68 65 20 63 75 72 73 6f ** set the curso
18704 72 20 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 r state to "inva
18705 6c 69 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 lid". This makes
18706 20 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f common insert o
18707 70 65 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 perations. ** s
18708 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a lightly faster..
18709 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 **. ** There
1870a 69 73 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 is a subtle but
1870b 69 6d 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 important optimi
1870c 7a 61 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e zation here too.
1870d 20 57 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a When inserting.
1870e 20 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 ** multiple re
1870f 63 6f 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e cords into an in
18710 74 6b 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e tkey b-tree usin
18711 67 20 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f g a single curso
18712 72 20 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 r (as can. ** h
18713 61 70 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 appen while proc
18714 65 73 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 essing an "INSER
18715 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 T INTO ... SELEC
18716 54 22 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 T" statement), i
18717 74 0a 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 t. ** is advant
18718 61 67 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 ageous to leave
18719 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 the cursor point
1871a 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 ing to the last
1871b 65 6e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 entry in. ** th
1871c 65 20 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 e b-tree if poss
1871d 69 62 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 ible. If the cur
1871e 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e sor is left poin
1871f 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 ting to the last
18720 0a 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 . ** entry in t
18721 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 he table, and th
18722 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 e next row inser
18723 74 65 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 ted has an integ
18724 65 72 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 er key. ** larg
18725 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 er than the larg
18726 65 73 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 est existing key
18727 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 , it is possible
18728 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 to insert the.
18729 20 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 ** row without
1872a 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 seeking the curs
1872b 6f 72 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 or. This can be
1872c 61 20 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 a big performanc
1872d 65 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 e boost.. */.
1872e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
1872f 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 = 0;. pCur->va
18730 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 lidNKey = 0;. i
18731 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
18732 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 && pPage->nOver
18733 66 6c 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d flow ){. rc =
18734 20 62 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a balance(pCur);.
18735 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b . /* Must mak
18736 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 e sure nOverflow
18737 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 is reset to zer
18738 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 o even if the ba
18739 6c 61 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 lance(). ** f
1873a 61 69 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 ails. Internal d
1873b 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f ata structure co
1873c 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 rruption will re
1873d 73 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 sult otherwise.
1873e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 . ** Also, se
1873f 74 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 t the cursor sta
18740 74 65 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 te to invalid. T
18741 68 69 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 his stops saveCu
18742 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 rsorPosition().
18743 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e ** from tryin
18744 67 20 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 g to save the cu
18745 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f rrent position o
18746 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a f the cursor. *
18747 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 /. pCur->apPa
18748 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
18749 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a >nOverflow = 0;.
1874a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
1874b 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
1874c 44 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 D;. }. assert(
1874d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1874e 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 ur->iPage]->nOve
1874f 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 rflow==0 );..end
18750 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 _insert:. retur
18751 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 n rc;.}../*.** D
18752 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 elete the entry
18753 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
18754 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 is pointing to.
18755 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 The cursor.** i
18756 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
18757 61 74 20 61 20 61 72 62 69 74 72 61 72 79 20 6c at a arbitrary l
18758 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ocation..*/.SQLI
18759 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1875a 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
1875b 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
1875c 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 ){. Btree *p =
1875d 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 pCur->pBtree;.
1875e 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
1875f 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 p->pBt;
18760 20 20 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 . int rc;
18761 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18762 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18763 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
18764 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
18765 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18766 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f /* Page to
18767 20 64 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f delete cell fro
18768 6d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 m */. unsigned
18769 63 68 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 char *pCell;
1876a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1876b 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 ointer to cell t
1876c 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e o delete */. in
1876d 74 20 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 t iCellIdx;
1876e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1876f 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 /* Index of c
18770 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f ell to delete */
18771 0a 20 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 . int iCellDept
18772 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
18773 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 /* Depth
18774 20 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e of node contain
18775 69 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 ing pCell */ ..
18776 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
18777 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
18778 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 );. assert( pBt
18779 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
1877a 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a =TRANS_WRITE );.
1877b 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
1877c 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 readOnly );. as
1877d 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c sert( pCur->wrFl
1877e 61 67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ag );. assert(
1877f 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 hasSharedCacheTa
18780 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d bleLock(p, pCur-
18781 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d >pgnoRoot, pCur-
18782 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 >pKeyInfo!=0, 2)
18783 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 );. assert( !h
18784 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 asReadConflicts(
18785 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f p, pCur->pgnoRoo
18786 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 t) );.. if( NEV
18787 45 52 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 ER(pCur->aiIdx[p
18788 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 Cur->iPage]>=pCu
18789 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
1878a 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a iPage]->nCell) .
1878b 20 20 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 || NEVER(pCur
1878c 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
1878d 5f 56 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 _VALID). ){.
1878e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1878f 52 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 RROR; /* Someth
18790 69 6e 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 ing has gone awr
18791 79 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 y. */. }.. /*
18792 49 66 20 74 68 69 73 20 69 73 20 61 20 64 65 6c If this is a del
18793 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f ete operation to
18794 20 72 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 remove a row fr
18795 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 om a table b-tre
18796 65 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 e,. ** invalida
18797 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 te any incrblob
18798 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 cursors open on
18799 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 the row being de
1879a 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 leted. */. if(
1879b 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d pCur->pKeyInfo=
1879c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 =0 ){. invali
1879d 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
1879e 6f 72 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 ors(p, pCur->inf
1879f 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a o.nKey, 0);. }.
187a0 0a 20 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 . iCellDepth =
187a1 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 pCur->iPage;. i
187a2 43 65 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e CellIdx = pCur->
187a3 61 69 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 aiIdx[iCellDepth
187a4 5d 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 ];. pPage = pCu
187a5 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 r->apPage[iCellD
187a6 65 70 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d epth];. pCell =
187a7 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
187a8 20 69 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f iCellIdx);.. /
187a9 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 63 6f * If the page co
187aa 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 ntaining the ent
187ab 72 79 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 ry to delete is
187ac 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c not a leaf page,
187ad 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 move. ** the c
187ae 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 ursor to the lar
187af 67 65 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 gest entry in th
187b0 65 20 74 72 65 65 20 74 68 61 74 20 69 73 20 73 e tree that is s
187b1 6d 61 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a maller than. **
187b2 20 74 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 the entry being
187b3 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 deleted. This c
187b4 65 6c 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 ell will replace
187b5 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 the cell being
187b6 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f deleted. ** fro
187b7 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e m the internal n
187b8 6f 64 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f ode. The 'previo
187b9 75 73 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 us' entry is use
187ba 64 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 d for this inste
187bb 61 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 ad. ** of the '
187bc 6e 65 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 next' entry, as
187bd 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 the previous ent
187be 72 79 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 ry is always a p
187bf 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 art of the. **
187c0 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 sub-tree headed
187c1 62 79 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 by the child pag
187c2 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 e of the cell be
187c3 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 ing deleted. Thi
187c4 73 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c s makes. ** bal
187c5 61 6e 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 ancing the tree
187c6 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 following the de
187c7 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 lete operation e
187c8 61 73 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 asier. */. if(
187c9 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
187ca 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 . int notUsed
187cb 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
187cc 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
187cd 70 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b pCur, ¬Used);
187ce 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
187cf 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
187d0 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 /* Save the posi
187d1 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 tions of any oth
187d2 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 er cursors open
187d3 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 on this table be
187d4 66 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 fore. ** making
187d5 20 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f any modificatio
187d6 6e 73 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 ns. Make the pag
187d7 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
187d8 20 65 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 entry to be .
187d9 2a 2a 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 ** deleted writa
187da 62 6c 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 ble. Then free a
187db 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ny overflow page
187dc 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
187dd 68 20 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 h the . ** entr
187de 79 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 y and finally re
187df 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 move the cell it
187e0 73 65 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e self from within
187e1 20 74 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a the page. . *
187e2 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c /. rc = saveAll
187e3 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 Cursors(pBt, pCu
187e4 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 r->pgnoRoot, pCu
187e5 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 r);. if( rc ) r
187e6 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
187e7 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
187e8 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
187e9 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 e);. if( rc ) r
187ea 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
187eb 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 clearCell(pPage
187ec 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 , pCell);. drop
187ed 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c Cell(pPage, iCel
187ee 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 lIdx, cellSizePt
187ef 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c r(pPage, pCell),
187f0 20 26 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 &rc);. if( rc
187f1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 ) return rc;..
187f2 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 /* If the cell d
187f3 65 6c 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c eleted was not l
187f4 6f 63 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 ocated on a leaf
187f5 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 page, then the
187f6 63 75 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 cursor. ** is c
187f7 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
187f8 67 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 g to the largest
187f9 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 entry in the su
187fa 62 2d 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 b-tree headed.
187fb 2a 2a 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d ** by the child-
187fc 70 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c page of the cell
187fd 20 74 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 that was just d
187fe 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 eleted from an i
187ff 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 nternal. ** nod
18800 65 2e 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d e. The cell from
18801 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e the leaf node n
18802 65 65 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 eeds to be moved
18803 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c to the internal
18804 0a 20 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 . ** node to re
18805 70 6c 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 place the delete
18806 64 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 d cell. */. if
18807 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
18808 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 {. MemPage *p
18809 4c 65 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 Leaf = pCur->apP
1880a 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
1880b 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b ;. int nCell;
1880c 0a 20 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 . Pgno n = pC
1880d 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c ur->apPage[iCell
1880e 44 65 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a Depth+1]->pgno;.
1880f 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
18810 72 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 r *pTmp;.. pC
18811 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
18812 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 Leaf, pLeaf->nCe
18813 6c 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c ll-1);. nCell
18814 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
18815 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 Leaf, pCell);.
18816 20 20 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c assert( MX_CEL
18817 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 L_SIZE(pBt)>=nCe
18818 6c 6c 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 ll );.. alloc
18819 61 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 ateTempSpace(pBt
1881a 29 3b 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 );. pTmp = pB
1881b 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 t->pTmpSpace;..
1881c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
1881d 61 67 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d agerWrite(pLeaf-
1881e 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
1881f 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c nsertCell(pPage,
18820 20 69 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c iCellIdx, pCell
18821 2d 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d -4, nCell+4, pTm
18822 70 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 p, n, &rc);.
18823 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 dropCell(pLeaf,
18824 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 pLeaf->nCell-1,
18825 6e 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 nCell, &rc);.
18826 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
18827 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 rc;. }.. /* B
18828 61 6c 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e alance the tree.
18829 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 If the entry de
1882a 6c 65 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 leted was locate
1882b 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 d on a leaf page
1882c 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 ,. ** then the
1882d 63 75 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 cursor still poi
1882e 6e 74 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 nts to that page
1882f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
18830 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 he first. ** ca
18831 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 ll to balance()
18832 72 65 70 61 69 72 73 20 74 68 65 20 74 72 65 65 repairs the tree
18833 2c 20 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e , and the if(...
18834 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 ) condition is.
18835 20 2a 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a ** never true..
18836 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 **. ** Otherw
18837 69 73 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 ise, if the entr
18838 79 20 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e y deleted was on
18839 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 an internal nod
1883a 65 20 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a e page, then. *
1883b 2a 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 * pCur is pointi
1883c 6e 67 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 ng to the leaf p
1883d 61 67 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 age from which a
1883e 20 63 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 cell was remove
1883f 64 20 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 d to. ** replac
18840 65 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 e the cell delet
18841 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 ed from the inte
18842 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 rnal node. This
18843 69 73 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a is slightly. **
18844 20 74 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c tricky as the l
18845 65 61 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 eaf node may be
18846 75 6e 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 underfull, and t
18847 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 he internal node
18848 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 may. ** be eit
18849 68 65 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 her under or ove
1884a 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 rfull. In this c
1884b 61 73 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 ase run the bala
1884c 6e 63 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a ncing algorithm.
1884d 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 ** on the leaf
1884e 20 6e 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 node first. If
1884f 74 68 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 the balance proc
18850 65 65 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 eeds far enough
18851 75 70 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 up the. ** tree
18852 20 74 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 that we can be
18853 73 75 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 sure that any pr
18854 6f 62 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 oblem in the int
18855 65 72 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 ernal node has.
18856 20 2a 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 ** been correct
18857 65 64 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 ed, so be it. Ot
18858 68 65 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 herwise, after b
18859 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 alancing the lea
1885a 66 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c f node,. ** wal
1885b 6b 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 k the cursor up
1885c 74 68 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 the tree to the
1885d 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e internal node an
1885e 64 20 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 d balance it as
1885f 0a 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a . ** well. */.
18860 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 rc = balance(p
18861 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d Cur);. if( rc==
18862 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 SQLITE_OK && pCu
18863 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 r->iPage>iCellDe
18864 70 74 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 pth ){. while
18865 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 ( pCur->iPage>iC
18866 65 6c 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 ellDepth ){.
18867 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 releasePage(pC
18868 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
18869 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 >iPage--]);.
1886a 7d 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e }. rc = balan
1886b 63 65 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 ce(pCur);. }..
1886c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1886d 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f OK ){. moveTo
1886e 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a Root(pCur);. }.
1886f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
18870 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
18871 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 ew BTree table.
18872 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 Write into *piT
18873 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a able the page.**
18874 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 number for the
18875 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 root page of the
18876 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a new table..**.*
18877 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 * The type of ty
18878 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 pe is determined
18879 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 by the flags pa
1887a 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 rameter. Only t
1887b 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 he.** following
1887c 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 values of flags
1887d 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e are currently in
1887e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c use. Other val
1887f 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 ues for.** flags
18880 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a might not work:
18881 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 .**.** BTREE
18882 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 _INTKEY|BTREE_LE
18883 41 46 44 41 54 41 20 20 20 20 20 55 73 65 64 20 AFDATA Used
18884 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 for SQL tables w
18885 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a ith rowid keys.*
18886 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f * BTREE_ZERO
18887 44 41 54 41 20 20 20 20 20 20 20 20 20 20 20 20 DATA
18888 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 Used for S
18889 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 QL indices.*/.st
1888a 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 atic int btreeCr
1888b 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 eateTable(Btree
1888c 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 *p, int *piTable
1888d 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
1888e 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
1888f 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 p->pBt;. MemPag
18890 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f e *pRoot;. Pgno
18891 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 pgnoRoot;. int
18892 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
18893 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
18894 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 sMutex(p) );. a
18895 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
18896 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
18897 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
18898 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e rt( !pBt->readOn
18899 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 ly );..#ifdef SQ
1889a 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1889b 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f CUUM. rc = allo
1889c 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
1889d 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f t, &pRoot, &pgno
1889e 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 Root, 1, 0);. i
1889f 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 f( rc ){. ret
188a0 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 urn rc;. }.#els
188a1 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 e. if( pBt->aut
188a2 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 oVacuum ){. P
188a3 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 gno pgnoMove;
188a4 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 /* Move a pag
188a5 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 e here to make r
188a6 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 oom for the root
188a7 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d -page */. Mem
188a8 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b Page *pPageMove;
188a9 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 /* The page to
188aa 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 move to. */..
188ab 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e /* Creating a n
188ac 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f ew table may pro
188ad 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f bably require mo
188ae 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 ving an existing
188af 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a database. **
188b0 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f to make room fo
188b1 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 r the new tables
188b2 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 root page. In c
188b3 61 73 65 20 74 68 69 73 20 70 61 67 65 20 74 75 ase this page tu
188b4 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 rns. ** out t
188b5 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 o be an overflow
188b6 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c page, delete al
188b7 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d l overflow page-
188b8 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a map caches. *
188b9 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 * held by open c
188ba 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 ursors.. */.
188bb 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c invalidateAll
188bc 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 OverflowCache(pB
188bd 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 t);.. /* Read
188be 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 the value of me
188bf 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 ta[3] from the d
188c0 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 atabase to deter
188c1 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 mine where the.
188c2 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 ** root page
188c3 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 of the new table
188c4 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 should go. meta
188c5 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 [3] is the large
188c6 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
188c7 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 ** created so f
188c8 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 ar, so the new r
188c9 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 oot-page is (met
188ca 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a a[3]+1).. */.
188cb 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
188cc 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 GetMeta(p, BTREE
188cd 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 _LARGEST_ROOT_PA
188ce 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a GE, &pgnoRoot);.
188cf 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a pgnoRoot++;.
188d0 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 . /* The new
188d1 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f root-page may no
188d2 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f t be allocated o
188d3 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 n a pointer-map
188d4 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 page, or the.
188d5 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ** PENDING_BYTE
188d6 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 page.. */.
188d7 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f while( pgnoRoo
188d8 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f t==PTRMAP_PAGENO
188d9 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 (pBt, pgnoRoot)
188da 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 ||. pgnoR
188db 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 oot==PENDING_BYT
188dc 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
188dd 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b pgnoRoot++;
188de 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
188df 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 t( pgnoRoot>=3 )
188e0 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 ;.. /* Alloca
188e1 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 te a page. The p
188e2 61 67 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 age that current
188e3 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 70 67 ly resides at pg
188e4 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 noRoot will.
188e5 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 ** be moved to t
188e6 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 he allocated pag
188e7 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c e (unless the al
188e8 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 61 70 located page hap
188e9 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 pens. ** to r
188ea 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f eside at pgnoRoo
188eb 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 t).. */. r
188ec 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 c = allocateBtre
188ed 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 ePage(pBt, &pPag
188ee 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 eMove, &pgnoMove
188ef 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a , pgnoRoot, 1);.
188f0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
188f1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
188f2 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
188f3 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 . if( pgnoMov
188f4 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 e!=pgnoRoot ){.
188f5 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 /* pgnoRoot
188f6 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68 61 is the page tha
188f7 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 t will be used f
188f8 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 or the root-page
188f9 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 of. ** the
188fa 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 new table (assu
188fb 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 ming an error di
188fc 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 d not occur). Bu
188fd 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 t we were.
188fe 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e ** allocated pgn
188ff 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 oMove. If requir
18900 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 ed (i.e. if it w
18901 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 as not allocated
18902 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 . ** by ext
18903 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 ending the file)
18904 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 , the current pa
18905 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 ge at position p
18906 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a gnoMove. **
18907 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 is already jour
18908 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a naled.. */.
18909 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20 3d u8 eType =
1890a 20 30 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 0;. Pgno i
1890b 50 74 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 PtrPage = 0;..
1890c 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1890d 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 pPageMove);..
1890e 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 /* Move the p
1890f 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 age currently at
18910 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e pgnoRoot to pgn
18911 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 oMove. */.
18912 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
18913 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c e(pBt, pgnoRoot,
18914 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 &pRoot, 0);.
18915 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
18916 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
18917 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18918 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 }. rc = pt
18919 72 6d 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e rmapGet(pBt, pgn
1891a 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 oRoot, &eType, &
1891b 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 iPtrPage);.
1891c 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
1891d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 AP_ROOTPAGE || e
1891e 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 Type==PTRMAP_FRE
1891f 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 EPAGE ){.
18920 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
18921 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
18922 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
18923 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
18924 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
18925 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 e(pRoot);.
18926 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
18927 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 }. asser
18928 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 t( eType!=PTRMAP
18929 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 _ROOTPAGE );.
1892a 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 assert( eType
1892b 21 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 !=PTRMAP_FREEPAG
1892c 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 E );. rc =
1892d 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 relocatePage(pBt
1892e 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 , pRoot, eType,
1892f 69 50 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f iPtrPage, pgnoMo
18930 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 ve, 0);. re
18931 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
18932 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 ;.. /* Obta
18933 69 6e 20 74 68 65 20 70 61 67 65 20 61 74 20 70 in the page at p
18934 67 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 gnoRoot */.
18935 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18936 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
18937 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
18938 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 . rc = btre
18939 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 eGetPage(pBt, pg
1893a 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 noRoot, &pRoot,
1893b 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
1893c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1893d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
1893e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1893f 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
18940 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 rWrite(pRoot->pD
18941 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 bPage);. if
18942 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
18943 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 ){. relea
18944 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 sePage(pRoot);.
18945 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
18946 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
18947 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 lse{. pRoot
18948 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 = pPageMove;.
18949 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 } .. /* Upd
1894a 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d ate the pointer-
1894b 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 map and meta-dat
1894c 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 a with the new r
1894d 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e oot-page number.
1894e 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 */. ptrmapPu
1894f 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c t(pBt, pgnoRoot,
18950 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
18951 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 , 0, &rc);. i
18952 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
18953 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 eleasePage(pRoot
18954 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
18955 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 rc;. }. rc
18956 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 = sqlite3BtreeU
18957 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 pdateMeta(p, 4,
18958 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 pgnoRoot);. i
18959 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 f( rc ){. r
1895a 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 eleasePage(pRoot
1895b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1895c 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c rc;. }.. }el
1895d 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c se{. rc = all
1895e 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
1895f 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e Bt, &pRoot, &pgn
18960 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 oRoot, 1, 0);.
18961 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
18962 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 n rc;. }.#endif
18963 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
18964 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
18965 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 le(pRoot->pDbPag
18966 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 e) );. zeroPage
18967 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 (pRoot, flags |
18968 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c PTF_LEAF);. sql
18969 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
1896a 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a Root->pDbPage);.
1896b 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e *piTable = (in
1896c 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 t)pgnoRoot;. re
1896d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1896e 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
1896f 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
18970 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 eCreateTable(Btr
18971 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 ee *p, int *piTa
18972 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b ble, int flags){
18973 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c . int rc;. sql
18974 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
18975 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 );. rc = btreeC
18976 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 reateTable(p, pi
18977 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 Table, flags);.
18978 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18979 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
1897a 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 rc;.}../*.** Era
1897b 73 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 se the given dat
1897c 61 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 abase page and a
1897d 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e ll its children.
1897e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 Return.** the
1897f 70 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 page to the free
18980 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
18981 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 int clearDatabas
18982 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 ePage(. BtShare
18983 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 d *pBt,
18984 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 /* The BTree t
18985 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
18986 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f table */. Pgno
18987 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 pgno,
18988 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
18989 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a ber to clear */.
1898a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c int freePageFl
1898b 61 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 65 ag, /* De
1898c 61 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 allocate page if
1898d 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a true */. int *
1898e 70 6e 43 68 61 6e 67 65 20 20 20 20 20 20 20 20 pnChange
1898f 20 20 20 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65 /* Add numbe
18990 72 20 6f 66 20 43 65 6c 6c 73 20 66 72 65 65 64 r of Cells freed
18991 20 74 6f 20 74 68 69 73 20 63 6f 75 6e 74 65 72 to this counter
18992 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 */.){. MemPage
18993 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 *pPage;. int r
18994 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 c;. unsigned ch
18995 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 ar *pCell;. int
18996 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 i;.. assert( s
18997 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
18998 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
18999 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 . if( pgno>page
1899a 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 rPagecount(pBt)
1899b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
1899c 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
1899d 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 T;. }.. rc = g
1899e 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 etAndInitPage(pB
1899f 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 t, pgno, &pPage)
189a0 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
189a1 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d urn rc;. for(i=
189a2 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 0; i<pPage->nCel
189a3 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 l; i++){. pCe
189a4 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 ll = findCell(pP
189a5 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 age, i);. if(
189a6 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
189a7 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 . rc = clea
189a8 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42 rDatabasePage(pB
189a9 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 65 6c t, get4byte(pCel
189aa 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 l), 1, pnChange)
189ab 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
189ac 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 goto cleardatab
189ad 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 asepage_out;.
189ae 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 }. rc = clea
189af 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 rCell(pPage, pCe
189b0 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ll);. if( rc
189b1 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 ) goto cleardata
189b2 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 basepage_out;.
189b3 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e }. if( !pPage->
189b4 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d leaf ){. rc =
189b5 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 clearDatabasePa
189b6 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 ge(pBt, get4byte
189b7 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 (&pPage->aData[8
189b8 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 ]), 1, pnChange)
189b9 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
189ba 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
189bb 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c epage_out;. }el
189bc 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20 se if( pnChange
189bd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
189be 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a Page->intKey );.
189bf 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d *pnChange +=
189c0 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 pPage->nCell;.
189c1 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 }. if( freePag
189c2 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 65 eFlag ){. fre
189c3 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 ePage(pPage, &rc
189c4 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 );. }else if( (
189c5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
189c6 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 rWrite(pPage->pD
189c7 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 bPage))==0 ){.
189c8 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 zeroPage(pPage
189c9 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 , pPage->aData[0
189ca 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 ] | PTF_LEAF);.
189cb 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 }..cleardatabas
189cc 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c epage_out:. rel
189cd 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
189ce 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
189cf 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c ./*.** Delete al
189d0 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 l information fr
189d1 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c om a single tabl
189d2 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
189d3 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a e. iTable is.**
189d4 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
189d5 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 of the root of
189d6 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65 the table. Afte
189d7 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 r this routine r
189d8 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 eturns,.** the r
189d9 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 74 oot page is empt
189da 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69 y, but still exi
189db 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 sts..**.** This
189dc 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 routine will fai
189dd 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f l with SQLITE_LO
189de 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72 CKED if there ar
189df 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 e any open.** re
189e0 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 ad cursors on th
189e1 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 e table. Open w
189e2 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 rite cursors are
189e3 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a moved to the.**
189e4 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 root of the tab
189e5 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 le..**.** If pnC
189e6 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c hange is not NUL
189e7 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54 L, then table iT
189e8 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20 able must be an
189e9 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 intkey table. Th
189ea 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c e.** integer val
189eb 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ue pointed to by
189ec 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63 pnChange is inc
189ed 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 remented by the
189ee 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 number of.** ent
189ef 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c ries in the tabl
189f0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
189f1 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
189f2 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 BtreeClearTable(
189f3 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 Btree *p, int iT
189f4 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 able, int *pnCha
189f5 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a nge){. int rc;.
189f6 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
189f7 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 = p->pBt;. sqli
189f8 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
189f9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
189fa 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
189fb 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 ITE );.. /* Inv
189fc 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 alidate all incr
189fd 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 blob cursors ope
189fe 6e 20 6f 6e 20 74 61 62 6c 65 20 69 54 61 62 6c n on table iTabl
189ff 65 20 28 61 73 73 75 6d 69 6e 67 20 69 54 61 62 e (assuming iTab
18a00 6c 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 72 le. ** is the r
18a01 6f 6f 74 20 6f 66 20 61 20 74 61 62 6c 65 20 62 oot of a table b
18a02 2d 74 72 65 65 20 2d 20 69 66 20 69 74 20 69 73 -tree - if it is
18a03 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 not, the follow
18a04 69 6e 67 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a ing call is. **
18a05 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 a no-op). */.
18a06 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 invalidateIncrb
18a07 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 30 2c lobCursors(p, 0,
18a08 20 31 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 1);.. rc = sav
18a09 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
18a0a 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 (Pgno)iTable, 0
18a0b 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f );. if( SQLITE_
18a0c 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 OK==rc ){. rc
18a0d 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 = clearDatabase
18a0e 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 Page(pBt, (Pgno)
18a0f 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 iTable, 0, pnCha
18a10 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 nge);. }. sqli
18a11 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
18a12 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
18a13 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c ../*.** Erase al
18a14 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e l information in
18a15 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 64 64 a table and add
18a16 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18a17 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 table to.** the
18a18 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78 63 65 freelist. Exce
18a19 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 pt, the root of
18a1a 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20 74 61 the principle ta
18a1b 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a ble (the one on.
18a1c 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20 6e 65 ** page 1) is ne
18a1d 76 65 72 20 61 64 64 65 64 20 74 6f 20 74 68 65 ver added to the
18a1e 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a freelist..**.**
18a1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
18a20 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 4c ll fail with SQL
18a21 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 ITE_LOCKED if th
18a22 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 6e ere are any open
18a23 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 .** cursors on t
18a24 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 he table..**.**
18a25 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 If AUTOVACUUM is
18a26 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 68 65 enabled and the
18a27 20 70 61 67 65 20 61 74 20 69 54 61 62 6c 65 20 page at iTable
18a28 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a is not the last.
18a29 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 ** root page in
18a2a 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
18a2b 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 e, then the last
18a2c 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 root page .** i
18a2d 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
18a2e 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 ile is moved int
18a2f 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 o the slot forme
18a30 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 0a rly occupied by.
18a31 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 74 68 ** iTable and th
18a32 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 at last slot for
18a33 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 merly occupied b
18a34 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 y the last root
18a35 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 page.** is added
18a36 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 to the freelist
18a37 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54 61 62 instead of iTab
18a38 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 61 79 le. In this say
18a39 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 , all.** root pa
18a3a 67 65 73 20 61 72 65 20 6b 65 70 74 20 61 74 20 ges are kept at
18a3b 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
18a3c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
18a3d 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 le, which.** is
18a3e 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 41 55 necessary for AU
18a3f 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b TOVACUUM to work
18a40 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 right. *piMove
18a41 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 d is set to the
18a42 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 .** page number
18a43 74 68 61 74 20 75 73 65 64 20 74 6f 20 62 65 20 that used to be
18a44 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 the last root pa
18a45 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 62 ge in the file b
18a46 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 efore.** the mov
18a47 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 67 e. If no page g
18a48 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f ets moved, *piMo
18a49 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e ved is set to 0.
18a4a 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 6f 6f .** The last roo
18a4b 74 20 70 61 67 65 20 69 73 20 72 65 63 6f 72 64 t page is record
18a4c 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e ed in meta[3] an
18a4d 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a d the value of.*
18a4e 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 70 64 * meta[3] is upd
18a4f 61 74 65 64 20 62 79 20 74 68 69 73 20 70 72 6f ated by this pro
18a50 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 cedure..*/.stati
18a51 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f 70 54 c int btreeDropT
18a52 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 able(Btree *p, P
18a53 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 gno iTable, int
18a54 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 *piMoved){. int
18a55 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a rc;. MemPage *
18a56 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 pPage = 0;. BtS
18a57 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
18a58 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
18a59 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
18a5a 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 sMutex(p) );. a
18a5b 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
18a5c 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
18a5d 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 6c ;.. /* It is il
18a5e 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 legal to drop a
18a5f 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63 75 72 table if any cur
18a60 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e sors are open on
18a61 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 the. ** databa
18a62 73 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 se. This is beca
18a63 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 use in auto-vacu
18a64 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61 63 6b um mode the back
18a65 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 end may. ** nee
18a66 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 d to move anothe
18a67 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 r root-page to f
18a68 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74 20 62 ill a gap left b
18a69 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a 20 20 y the deleted.
18a6a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 ** root page. If
18a6b 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 an open cursor
18a6c 77 61 73 20 75 73 69 6e 67 20 74 68 69 73 20 70 was using this p
18a6d 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f age a problem wo
18a6e 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e uld . ** occur.
18a6f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
18a70 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 20 error is caught
18a71 6c 6f 6e 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 long before cont
18a72 72 6f 6c 20 72 65 61 63 68 65 73 20 74 68 69 73 rol reaches this
18a73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 point.. */. i
18a74 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 f( NEVER(pBt->pC
18a75 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 ursor) ){. sq
18a76 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
18a77 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 locked(p->db, pB
18a78 74 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 t->pCursor->pBtr
18a79 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 ee->db);. ret
18a7a 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 urn SQLITE_LOCKE
18a7b 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 D_SHAREDCACHE;.
18a7c 20 7d 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 65 }.. rc = btree
18a7d 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67 GetPage(pBt, (Pg
18a7e 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67 no)iTable, &pPag
18a7f 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 e, 0);. if( rc
18a80 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 ) return rc;. r
18a81 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
18a82 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54 ClearTable(p, iT
18a83 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 able, 0);. if(
18a84 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 rc ){. releas
18a85 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
18a86 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
18a87 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 .. *piMoved = 0
18a88 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e ;.. if( iTable>
18a89 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 1 ){.#ifdef SQLI
18a8a 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18a8b 55 4d 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 UM. freePage(
18a8c 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 pPage, &rc);.
18a8d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
18a8e 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 ge);.#else. i
18a8f 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
18a90 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f um ){. Pgno
18a91 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 maxRootPgno;.
18a92 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
18a93 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 GetMeta(p, BTREE
18a94 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 _LARGEST_ROOT_PA
18a95 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f GE, &maxRootPgno
18a96 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 69 54 );.. if( iT
18a97 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e able==maxRootPgn
18a98 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 o ){. /*
18a99 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 If the table bei
18a9a 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74 68 ng dropped is th
18a9b 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 e table with the
18a9c 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 largest root-pa
18a9d 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 ge. ** nu
18a9e 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 mber in the data
18a9f 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72 6f base, put the ro
18aa0 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20 66 ot page on the f
18aa1 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 ree list. .
18aa2 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 */. fr
18aa3 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 eePage(pPage, &r
18aa4 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 c);. rele
18aa5 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
18aa6 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
18aa7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
18aa8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
18aa9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
18aaa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
18aab 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 /* The table be
18aac 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65 73 ing dropped does
18aad 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c 61 not have the la
18aae 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a rgest root-page.
18aaf 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ** numbe
18ab0 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 r in the databas
18ab1 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 e. So move the p
18ab2 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69 6e age that does in
18ab3 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 to the .
18ab4 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 ** gap left by t
18ab5 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d he deleted root-
18ab6 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f page.. */
18ab7 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 . MemPage
18ab8 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 *pMove;.
18ab9 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
18aba 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ge);. rc
18abb 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
18abc 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c Bt, maxRootPgno,
18abd 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 &pMove, 0);.
18abe 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18abf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18ac0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18ac1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
18ac2 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 rc = relocatePa
18ac3 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 ge(pBt, pMove, P
18ac4 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 TRMAP_ROOTPAGE,
18ac5 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 0, iTable, 0);.
18ac6 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
18ac7 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 ge(pMove);.
18ac8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
18ac9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
18aca 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
18acb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
18acc 4d 6f 76 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 Move = 0;.
18acd 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
18ace 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 age(pBt, maxRoot
18acf 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 Pgno, &pMove, 0)
18ad0 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 61 ;. freePa
18ad1 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a ge(pMove, &rc);.
18ad2 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
18ad3 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 age(pMove);.
18ad4 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
18ad5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
18ad6 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
18ad7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
18ad8 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f *piMoved = maxRo
18ad9 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a otPgno;. }.
18ada 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 . /* Set th
18adb 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d e new 'max-root-
18adc 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e 20 74 page' value in t
18add 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64 he database head
18ade 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a er. This. *
18adf 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 61 6c * is the old val
18ae0 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 ue less one, les
18ae1 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 s one more if th
18ae2 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 at happens to.
18ae3 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 ** be a root
18ae4 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 -page number, le
18ae5 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 ss one again if
18ae6 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 20 20 that is the.
18ae7 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 ** PENDING_BYT
18ae8 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f E_PAGE.. */
18ae9 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 . maxRootPg
18aea 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c no--;. whil
18aeb 65 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d e( maxRootPgno==
18aec 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
18aed 45 28 70 42 74 29 0a 20 20 20 20 20 20 20 20 20 E(pBt).
18aee 20 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 || PTRMAP_IS
18aef 50 41 47 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f PAGE(pBt, maxRoo
18af0 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 tPgno) ){.
18af1 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b maxRootPgno--;
18af2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
18af3 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 ssert( maxRootPg
18af4 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 no!=PENDING_BYTE
18af5 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 _PAGE(pBt) );..
18af6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
18af7 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
18af8 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 (p, 4, maxRootPg
18af9 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a no);. }else{.
18afa 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 70 freePage(p
18afb 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 Page, &rc);.
18afc 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
18afd 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 age);. }.#end
18afe 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 if. }else{.
18aff 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42 74 72 /* If sqlite3Btr
18b00 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20 eeDropTable was
18b01 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 called on page 1
18b02 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 72 65 .. ** This re
18b03 61 6c 6c 79 20 6e 65 76 65 72 20 73 68 6f 75 6c ally never shoul
18b04 64 20 68 61 70 70 65 6e 20 65 78 63 65 70 74 20 d happen except
18b05 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 20 20 20 in a corrupt.
18b06 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20 ** database. .
18b07 20 20 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 */. zeroPa
18b08 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49 4e ge(pPage, PTF_IN
18b09 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b TKEY|PTF_LEAF );
18b0a 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
18b0b 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 (pPage);. }. r
18b0c 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 eturn rc; .}.SQ
18b0d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18b0e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f sqlite3BtreeDro
18b0f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c pTable(Btree *p,
18b10 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 int iTable, int
18b11 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e *piMoved){. in
18b12 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 t rc;. sqlite3B
18b13 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
18b14 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54 61 rc = btreeDropTa
18b15 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 ble(p, iTable, p
18b16 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 iMoved);. sqlit
18b17 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
18b18 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18b19 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
18b1a 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 ction may only b
18b1b 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 e called if the
18b1c 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f b-tree connectio
18b1d 6e 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 n already.** has
18b1e 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 a read or write
18b1f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 transaction ope
18b20 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 n on the databas
18b21 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 e..**.** Read th
18b22 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 e meta-informati
18b23 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61 on out of a data
18b24 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 base file. Meta
18b25 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 [0].** is the nu
18b26 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 mber of free pag
18b27 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 es currently in
18b28 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d the database. M
18b29 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 eta[1].** throug
18b2a 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61 h meta[15] are a
18b2b 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 vailable for use
18b2c 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 by higher layer
18b2d 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 s. Meta[0].** i
18b2e 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 s read-only, the
18b2f 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 61 64 others are read
18b30 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 /write..** .** T
18b31 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 he schema layer
18b32 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c numbers meta val
18b33 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e ues differently.
18b34 20 20 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a At the schema.
18b35 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68 ** layer (and th
18b36 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 e SetCookie and
18b37 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 ReadCookie opcod
18b38 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f es) the number o
18b39 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20 f.** free pages
18b3a 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 is not visible.
18b3b 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 So Cookie[0] is
18b3c 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74 the same as Met
18b3d 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f a[1]..*/.SQLITE_
18b3e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
18b3f 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
18b40 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 (Btree *p, int i
18b41 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 7b dx, u32 *pMeta){
18b42 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
18b43 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 = p->pBt;.. sq
18b44 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
18b45 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p);. assert( p-
18b46 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e >inTrans>TRANS_N
18b47 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ONE );. assert(
18b48 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 SQLITE_OK==quer
18b49 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
18b4a 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f eLock(p, MASTER_
18b4b 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 ROOT, READ_LOCK)
18b4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
18b4d 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 61 t->pPage1 );. a
18b4e 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 ssert( idx>=0 &&
18b4f 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a idx<=15 );.. *
18b50 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 74 65 pMeta = get4byte
18b51 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
18b52 44 61 74 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d Data[36 + idx*4]
18b53 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f );.. /* If auto
18b54 2d 76 61 63 75 75 6d 20 69 73 20 64 69 73 61 62 -vacuum is disab
18b55 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c led in this buil
18b56 64 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 6e d and this is an
18b57 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a auto-vacuum. *
18b58 2a 20 64 61 74 61 62 61 73 65 2c 20 6d 61 72 6b * database, mark
18b59 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 the database as
18b5a 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a read-only. */.
18b5b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
18b5c 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18b5d 69 66 28 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c if( idx==BTREE_L
18b5e 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 ARGEST_ROOT_PAGE
18b5f 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 && *pMeta>0 ) p
18b60 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 Bt->readOnly = 1
18b61 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 ;.#endif.. sqli
18b62 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
18b63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
18b64 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f meta-informatio
18b65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 n back into the
18b66 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 5b database. Meta[
18b67 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 0] is.** read-on
18b68 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 ly and may not b
18b69 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 e written..*/.SQ
18b6a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18b6b 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
18b6c 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a 70 ateMeta(Btree *p
18b6d 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 69 , int idx, u32 i
18b6e 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 65 Meta){. BtShare
18b6f 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
18b70 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
18b71 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b *pP1;. int rc;
18b72 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d . assert( idx>=
18b73 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 1 && idx<=15 );.
18b74 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
18b75 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
18b76 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
18b77 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
18b78 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 ssert( pBt->pPag
18b79 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d e1!=0 );. pP1 =
18b7a 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 pBt->pPage1->aD
18b7b 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ata;. rc = sqli
18b7c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 te3PagerWrite(pB
18b7d 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 t->pPage1->pDbPa
18b7e 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ge);. if( rc==S
18b7f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18b80 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 36 put4byte(&pP1[36
18b81 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 + idx*4], iMeta
18b82 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18b83 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18b84 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d 42 M. if( idx==B
18b85 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d TREE_INCR_VACUUM
18b86 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
18b87 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ( pBt->autoVacuu
18b88 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b m || iMeta==0 );
18b89 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
18b8a 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 Meta==0 || iMeta
18b8b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 42 74 ==1 );. pBt
18b8c 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 ->incrVacuum = (
18b8d 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a u8)iMeta;. }.
18b8e 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c #endif. }. sql
18b8f 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18b90 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
18b91 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
18b92 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e E_OMIT_BTREECOUN
18b93 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 T./*.** The firs
18b94 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 t argument, pCur
18b95 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 , is a cursor op
18b96 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 ened on some b-t
18b97 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a ree. Count the.*
18b98 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * number of entr
18b99 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 72 65 ies in the b-tre
18b9a 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 e and write the
18b9b 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 result to *pnEnt
18b9c 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 ry..**.** SQLITE
18b9d 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
18b9e 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e if the operation
18b9f 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 is successfully
18ba0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f executed. .** O
18ba1 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 therwise, if an
18ba2 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
18ba3 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f ered (i.e. an IO
18ba4 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 62 61 error or databa
18ba5 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e se.** corruption
18ba6 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f ) an SQLite erro
18ba7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
18ba8 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
18ba9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18baa 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75 3BtreeCount(BtCu
18bab 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 rsor *pCur, i64
18bac 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 *pnEntry){. i64
18bad 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 nEntry = 0;
18bae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18baf 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
18bb0 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 turn in *pnEntry
18bb1 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
18bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18bb3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
18bb4 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 turn code */. r
18bb5 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 c = moveToRoot(p
18bb6 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 Cur);.. /* Unle
18bb7 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ss an error occu
18bb8 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e rs, the followin
18bb9 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 g loop runs one
18bba 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 iteration for ea
18bbb 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 ch. ** page in
18bbc 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 75 63 the B-Tree struc
18bbd 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 ture (not includ
18bbe 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ing overflow pag
18bbf 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 es). . */. whi
18bc0 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
18bc1 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 K ){. int iId
18bc2 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x;
18bc3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
18bc4 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f ndex of child no
18bc5 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a de in parent */.
18bc6 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 MemPage *pPa
18bc7 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
18bc8 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
18bc9 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d t page of the b-
18bca 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 tree */.. /*
18bcb 49 66 20 74 68 69 73 20 69 73 20 61 20 6c 65 61 If this is a lea
18bcc 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 74 72 f page or the tr
18bcd 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 ee is not an int
18bce 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 6e 20 -key tree, then
18bcf 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 61 67 . ** this pag
18bd0 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 e contains count
18bd1 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 6e able entries. In
18bd2 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 crement the entr
18bd3 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a y counter. **
18bd4 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 accordingly..
18bd5 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 3d */. pPage =
18bd6 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
18bd7 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
18bd8 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
18bd9 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 || !pPage->intKe
18bda 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 y ){. nEntr
18bdb 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c y += pPage->nCel
18bdc 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a l;. }.. /*
18bdd 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 pPage is a leaf
18bde 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 node. This loop
18bdf 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 20 63 navigates the c
18be0 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 ursor so that it
18be1 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 . ** points
18be2 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e 74 to the first int
18be3 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 erior cell that
18be4 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
18be5 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a parent of. *
18be6 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 * the next page
18be7 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74 in the tree that
18be8 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 has not yet bee
18be9 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 0a 20 n visited. The.
18bea 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 ** pCur->aiId
18beb 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 x[pCur->iPage] v
18bec 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20 74 alue is set to t
18bed 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
18bee 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 parent cell.
18bef 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 ** of the page,
18bf0 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 or to the number
18bf1 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 of cells in the
18bf2 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65 78 page if the nex
18bf3 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f t page. ** to
18bf4 20 76 69 73 69 74 20 69 73 20 74 68 65 20 72 69 visit is the ri
18bf5 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 ght-child of its
18bf6 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a parent.. **.
18bf7 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 ** If all pa
18bf8 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 ges in the tree
18bf9 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65 have been visite
18bfa 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 d, return SQLITE
18bfb 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 2a _OK to the. *
18bfc 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f * caller.. */
18bfd 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
18bfe 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 64 6f leaf ){. do
18bff 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 {. if( p
18c00 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b Cur->iPage==0 ){
18c01 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c . /* Al
18c02 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 62 l pages of the b
18c03 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 -tree have been
18c04 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 6e 20 visited. Return
18c05 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f successfully. */
18c06 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e 45 6e . *pnEn
18c07 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 try = nEntry;.
18c08 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
18c09 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
18c0a 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 }. move
18c0b 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a ToParent(pCur);.
18c0c 20 20 20 20 20 20 7d 77 68 69 6c 65 20 28 20 70 }while ( p
18c0d 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
18c0e 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 >iPage]>=pCur->a
18c0f 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
18c10 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 e]->nCell );..
18c11 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
18c12 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a pCur->iPage]++;.
18c13 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 pPage = pC
18c14 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
18c15 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a >iPage];. }..
18c16 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 /* Descend t
18c17 6f 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 o the child node
18c18 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 of the cell tha
18c19 74 20 74 68 65 20 63 75 72 73 6f 72 20 63 75 72 t the cursor cur
18c1a 72 65 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 rently . ** p
18c1b 6f 69 6e 74 73 20 61 74 2e 20 54 68 69 73 20 69 oints at. This i
18c1c 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c s the right-chil
18c1d 64 20 69 66 20 28 69 49 64 78 3d 3d 70 50 61 67 d if (iIdx==pPag
18c1e 65 2d 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a e->nCell).. *
18c1f 2f 0a 20 20 20 20 69 49 64 78 20 3d 20 70 43 75 /. iIdx = pCu
18c20 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
18c21 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 Page];. if( i
18c22 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c Idx==pPage->nCel
18c23 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 l ){. rc =
18c24 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
18c25 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 , get4byte(&pPag
18c26 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
18c27 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a hdrOffset+8]));.
18c28 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18c29 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
18c2a 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 d(pCur, get4byte
18c2b 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c (findCell(pPage,
18c2c 20 69 49 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a iIdx)));. }.
18c2d 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 }.. /* An err
18c2e 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e or has occurred.
18c2f 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
18c30 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 code. */. retu
18c31 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a rn rc;.}.#endif.
18c32 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18c33 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 61 74 e pager associat
18c34 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 2e ed with a BTree.
18c35 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 This routine i
18c36 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 s used for.** te
18c37 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 sting and debugg
18c38 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c ing only..*/.SQL
18c39 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 67 65 ITE_PRIVATE Page
18c3a 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 r *sqlite3BtreeP
18c3b 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a ager(Btree *p){.
18c3c 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d return p->pBt-
18c3d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e >pPager;.}..#ifn
18c3e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18c3f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a INTEGRITY_CHECK.
18c40 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d /*.** Append a m
18c41 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 65 72 essage to the er
18c42 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 ror message stri
18c43 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ng..*/.static vo
18c44 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 id checkAppendMs
18c45 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b g(. IntegrityCk
18c46 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 *pCheck,. char
18c47 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 *zMsg1,. const
18c48 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a char *zFormat,.
18c49 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 ....){. va_li
18c4a 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 43 st ap;. if( !pC
18c4b 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 heck->mxErr ) re
18c4c 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e turn;. pCheck->
18c4d 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 mxErr--;. pChec
18c4e 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f k->nErr++;. va_
18c4f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 start(ap, zForma
18c50 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b t);. if( pCheck
18c51 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 ->errMsg.nChar )
18c52 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 {. sqlite3Str
18c53 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 68 AccumAppend(&pCh
18c54 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e eck->errMsg, "\n
18c55 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ", 1);. }. if(
18c56 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 71 zMsg1 ){. sq
18c57 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
18c58 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 end(&pCheck->err
18c59 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b Msg, zMsg1, -1);
18c5a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 . }. sqlite3VX
18c5b 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e Printf(&pCheck->
18c5c 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d errMsg, 1, zForm
18c5d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
18c5e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 43 68 d(ap);. if( pCh
18c5f 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c eck->errMsg.mall
18c60 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
18c61 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 pCheck->mallocFa
18c62 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a iled = 1;. }.}.
18c63 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
18c64 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
18c65 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 CHECK */..#ifnde
18c66 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
18c67 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
18c68 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68 65 .** Add 1 to the
18c69 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
18c6a 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65 2e for page iPage.
18c6b 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 If this is the
18c6c 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 second.** refer
18c6d 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 ence to the page
18c6e 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d , add an error m
18c6f 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63 6b essage to pCheck
18c70 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 ->zErrMsg..** Re
18c71 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 turn 1 if there
18c72 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 are 2 ore more r
18c73 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
18c74 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a 2a page and 0 if.*
18c75 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 * if this is the
18c76 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63 65 first reference
18c77 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a to the page..**
18c78 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 .** Also check t
18c79 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75 6d hat the page num
18c7a 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 ber is in bounds
18c7b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
18c7c 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72 69 checkRef(Integri
18c7d 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 tyCk *pCheck, Pg
18c7e 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 20 2a no iPage, char *
18c7f 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 zContext){. if(
18c80 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 iPage==0 ) retu
18c81 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61 67 rn 1;. if( iPag
18c82 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 e>pCheck->nPage
18c83 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 ){. checkAppe
18c84 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
18c85 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 ontext, "invalid
18c86 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64 22 page number %d"
18c87 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 , iPage);. re
18c88 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 turn 1;. }. if
18c89 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b ( pCheck->anRef[
18c8a 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 iPage]==1 ){.
18c8b 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18c8c 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 pCheck, zContext
18c8d 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63 65 , "2nd reference
18c8e 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69 50 to page %d", iP
18c8f 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e age);. return
18c90 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
18c91 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 (pCheck->anRef
18c92 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a [iPage]++)>1;.}.
18c93 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18c94 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18c95 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 /*.** Check that
18c96 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 the entry in th
18c97 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f e pointer-map fo
18c98 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 r page iChild ma
18c99 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 ps to .** page i
18c9a 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 Parent, pointer
18c9b 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 type ptrType. If
18c9c 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 not, append an
18c9d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a error message.**
18c9e 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 to pCheck..*/.s
18c9f 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b tatic void check
18ca0 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 Ptrmap(. Integr
18ca1 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 ityCk *pCheck,
18ca2 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 /* Integrity ch
18ca3 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 eck context */.
18ca4 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 Pgno iChild,
18ca5 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 /* Child
18ca6 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
18ca7 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 u8 eType,
18ca8 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 /* Expe
18ca9 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 cted pointer map
18caa 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 type */. Pgno
18cab 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 iParent,
18cac 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f /* Expected po
18cad 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 inter map parent
18cae 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
18caf 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 char *zContext
18cb0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 /* Cont
18cb1 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 ext description
18cb2 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 (used for error
18cb3 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 msg) */.){. int
18cb4 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 rc;. u8 ePtrma
18cb5 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 pType;. Pgno iP
18cb6 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 trmapParent;..
18cb7 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 rc = ptrmapGet(p
18cb8 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 Check->pBt, iChi
18cb9 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 ld, &ePtrmapType
18cba 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 , &iPtrmapParent
18cbb 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
18cbc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 ITE_OK ){. if
18cbd 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d ( rc==SQLITE_NOM
18cbe 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 EM || rc==SQLITE
18cbf 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 _IOERR_NOMEM ) p
18cc0 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 Check->mallocFai
18cc1 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 led = 1;. che
18cc2 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18cc3 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 ck, zContext, "F
18cc4 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 ailed to read pt
18cc5 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 rmap key=%d", iC
18cc6 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 hild);. retur
18cc7 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 n;. }.. if( eP
18cc8 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 trmapType!=eType
18cc9 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e || iPtrmapParen
18cca 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 t!=iParent ){.
18ccb 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18ccc 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
18ccd 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 t, . "Bad p
18cce 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 tr map entry key
18ccf 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 =%d expected=(%d
18cd0 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 ,%d) got=(%d,%d)
18cd1 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 ", . iChild
18cd2 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 , eType, iParent
18cd3 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 , ePtrmapType, i
18cd4 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 PtrmapParent);.
18cd5 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a }.}.#endif../*.
18cd6 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 ** Check the int
18cd7 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 egrity of the fr
18cd8 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 eelist or of an
18cd9 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 overflow page li
18cda 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 st..** Verify th
18cdb 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 at the number of
18cdc 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 pages on the li
18cdd 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 st is N..*/.stat
18cde 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 ic void checkLis
18cdf 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b t(. IntegrityCk
18ce0 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e *pCheck, /* In
18ce1 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 tegrity checking
18ce2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
18ce3 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 t isFreeList,
18ce4 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
18ce5 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c a freelist. Fal
18ce6 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 se for overflow
18ce7 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 page list */. i
18ce8 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 nt iPage,
18ce9 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
18cea 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 ber for first pa
18ceb 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a ge in the list *
18cec 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 /. int N,
18ced 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
18cee 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 ected number of
18cef 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 pages in the lis
18cf0 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f t */. char *zCo
18cf1 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 ntext /*
18cf2 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f Context for erro
18cf3 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b r messages */.){
18cf4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
18cf5 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 expected = N;.
18cf6 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 int iFirst = iPa
18cf7 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d ge;. while( N--
18cf8 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e > 0 && pCheck->
18cf9 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 mxErr ){. DbP
18cfa 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a age *pOvflPage;.
18cfb 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 unsigned cha
18cfc 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 r *pOvflData;.
18cfd 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b if( iPage<1 ){
18cfe 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 . checkAppe
18cff 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
18d00 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18d01 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 "%d of %d pages
18d02 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 missing from ov
18d03 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 erflow list star
18d04 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 ting at %d",.
18d05 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 N+1, expe
18d06 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 cted, iFirst);.
18d07 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
18d08 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 }. if( checkR
18d09 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 ef(pCheck, iPage
18d0a 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 , zContext) ) br
18d0b 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c eak;. if( sql
18d0c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 43 68 ite3PagerGet(pCh
18d0d 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 eck->pPager, (Pg
18d0e 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c no)iPage, &pOvfl
18d0f 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 Page) ){. c
18d10 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18d11 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18d12 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 "failed to get p
18d13 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b age %d", iPage);
18d14 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
18d15 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 }. pOvflDat
18d16 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 a = (unsigned ch
18d17 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 ar *)sqlite3Page
18d18 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 rGetData(pOvflPa
18d19 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 ge);. if( isF
18d1a 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 reeList ){.
18d1b 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 int n = get4byt
18d1c 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 e(&pOvflData[4])
18d1d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
18d1e 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
18d1f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 . if( pChec
18d20 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 k->pBt->autoVacu
18d21 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 um ){. ch
18d22 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b eckPtrmap(pCheck
18d23 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f , iPage, PTRMAP_
18d24 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f FREEPAGE, 0, zCo
18d25 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a ntext);. }.
18d26 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 #endif. if(
18d27 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e n>pCheck->pBt->
18d28 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 usableSize/4-2 )
18d29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 {. checkA
18d2a 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18d2b 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 zContext,.
18d2c 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 74 20 "freelist
18d2d 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 leaf count too b
18d2e 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 ig on page %d",
18d2f 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 iPage);.
18d30 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 N--;. }else
18d31 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d {. for(i=
18d32 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 0; i<n; i++){.
18d33 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 Pgno iFr
18d34 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 eePage = get4byt
18d35 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 e(&pOvflData[8+i
18d36 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 *4]);.#ifndef SQ
18d37 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
18d38 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 20 69 CUUM. i
18d39 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e f( pCheck->pBt->
18d3a 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 autoVacuum ){.
18d3b 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 checkP
18d3c 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 46 trmap(pCheck, iF
18d3d 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 50 5f reePage, PTRMAP_
18d3e 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f FREEPAGE, 0, zCo
18d3f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 ntext);.
18d40 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
18d41 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 70 43 checkRef(pC
18d42 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65 2c heck, iFreePage,
18d43 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 zContext);.
18d44 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 }. N
18d45 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 -= n;. }.
18d46 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
18d47 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18d48 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 UM. else{.
18d49 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 /* If this da
18d4a 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
18d4b 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 auto-vacuum and
18d4c 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 68 65 iPage is not the
18d4d 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 last. ** p
18d4e 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 65 72 age in this over
18d4f 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 6b flow list, check
18d50 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 that the pointe
18d51 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a r-map entry for.
18d52 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c ** the fol
18d53 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 74 63 lowing page matc
18d54 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 20 20 hes iPage..
18d55 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 */. if( pC
18d56 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 heck->pBt->autoV
18d57 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a acuum && N>0 ){.
18d58 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 74 34 i = get4
18d59 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 3b byte(pOvflData);
18d5a 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 . checkPt
18d5b 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 rmap(pCheck, i,
18d5c 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
18d5d 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 , iPage, zContex
18d5e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
18d5f 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 50 61 }.#endif. iPa
18d60 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f ge = get4byte(pO
18d61 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 71 vflData);. sq
18d62 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
18d63 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a pOvflPage);. }.
18d64 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
18d65 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 TE_OMIT_INTEGRIT
18d66 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e Y_CHECK */..#ifn
18d67 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18d68 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a INTEGRITY_CHECK.
18d69 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 /*.** Do various
18d6a 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 20 6f sanity checks o
18d6b 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 n a single page
18d6c 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 74 75 of a tree. Retu
18d6d 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 64 rn.** the tree d
18d6e 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 epth. Root page
18d6f 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61 72 s return 0. Par
18d70 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 ents of root pag
18d71 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 es.** return 1,
18d72 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a and so forth..**
18d73 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 63 6b .** These check
18d74 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a s are done:.**.*
18d75 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 * 1. Make
18d76 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c 73 20 sure that cells
18d77 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 and freeblocks d
18d78 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a o not overlap.**
18d79 20 20 20 20 20 20 20 20 20 20 62 75 74 20 63 6f but co
18d7a 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 mbine to complet
18d7b 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 70 61 ely cover the pa
18d7c 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 ge..** NO 2.
18d7d 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 6b Make sure cell k
18d7e 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 eys are in order
18d7f 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 ..** NO 3. Ma
18d80 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 ke sure no key i
18d81 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
18d82 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f qual to zLowerBo
18d83 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 und..** NO 4.
18d84 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 Make sure no ke
18d85 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 y is greater tha
18d86 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 n or equal to zU
18d87 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 pperBound..**
18d88 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 68 65 5. Check the
18d89 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 6f 76 integrity of ov
18d8a 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a erflow pages..**
18d8b 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 72 73 6. Recurs
18d8c 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b ively call check
18d8d 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 TreePage on all
18d8e 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 children..**
18d8f 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 68 61 7. Verify tha
18d90 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 61 t the depth of a
18d91 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 20 74 ll children is t
18d92 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 he same..**
18d93 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 8. Make sure t
18d94 68 69 73 20 70 61 67 65 20 69 73 20 61 74 20 6c his page is at l
18d95 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 east 33% full or
18d96 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 else it is.**
18d97 20 20 20 20 20 20 20 20 74 68 65 20 72 6f 6f 74 the root
18d98 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f of the tree..*/
18d99 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 .static int chec
18d9a 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 6e 74 kTreePage(. Int
18d9b 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b egrityCk *pCheck
18d9c 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f , /* Context fo
18d9d 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 65 r the sanity che
18d9e 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 ck */. int iPag
18d9f 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e, /*
18da0 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
18da1 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 65 63 the page to chec
18da2 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 k */. char *zPa
18da3 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 rentContext /*
18da4 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a Parent context *
18da5 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a /.){. MemPage *
18da6 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 pPage;. int i,
18da7 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 70 rc, depth, d2, p
18da8 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 gno, cnt;. int
18da9 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a hdr, cellStart;.
18daa 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 int nCell;. u
18dab 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 8 *data;. BtSha
18dac 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 red *pBt;. int
18dad 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 usableSize;. ch
18dae 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d ar zContext[100]
18daf 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 3d 20 ;. char *hit =
18db0 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 0;.. sqlite3_sn
18db1 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 printf(sizeof(zC
18db2 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 ontext), zContex
18db3 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 2c 20 t, "Page %d: ",
18db4 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 iPage);.. /* Ch
18db5 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 eck that the pag
18db6 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 e exists. */.
18db7 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 pBt = pCheck->pB
18db8 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 t;. usableSize
18db9 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
18dba 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3d 3d e;. if( iPage==
18dbb 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
18dbc 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 if( checkRef(pCh
18dbd 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 eck, iPage, zPar
18dbe 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 72 65 entContext) ) re
18dbf 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 turn 0;. if( (r
18dc0 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
18dc1 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 (pBt, (Pgno)iPag
18dc2 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 3d e, &pPage, 0))!=
18dc3 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 0 ){. checkAp
18dc4 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
18dc5 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 zContext,.
18dc6 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 "unable to get
18dc7 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f 72 20 the page. error
18dc8 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 code=%d", rc);.
18dc9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
18dca 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d .. /* Clear Mem
18dcb 50 61 67 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d Page.isInit to m
18dcc 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 72 ake sure the cor
18dcd 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 69 6f ruption detectio
18dce 6e 20 63 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 n code in. ** b
18dcf 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 69 treeInitPage() i
18dd0 73 20 65 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a s executed. */.
18dd1 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
18dd2 3d 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d = 0;. if( (rc =
18dd3 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 btreeInitPage(p
18dd4 50 61 67 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 Page))!=0 ){.
18dd5 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
18dd6 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 ITE_CORRUPT );
18dd7 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 /* The only poss
18dd8 69 62 6c 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 ible error from
18dd9 49 6e 69 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 InitPage */.
18dda 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18ddb 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
18ddc 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
18ddd 20 20 20 20 20 22 62 74 72 65 65 49 6e 69 74 50 "btreeInitP
18dde 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65 72 age() returns er
18ddf 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72 63 ror code %d", rc
18de0 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 );. releasePa
18de1 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 ge(pPage);. r
18de2 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 eturn 0;. }..
18de3 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c /* Check out all
18de4 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f the cells.. */
18de5 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 . depth = 0;.
18de6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 for(i=0; i<pPage
18de7 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 63 ->nCell && pChec
18de8 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a k->mxErr; i++){.
18de9 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 u8 *pCell;.
18dea 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 u32 sz;. C
18deb 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 ellInfo info;..
18dec 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c /* Check payl
18ded 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 oad overflow pag
18dee 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 es. */. sq
18def 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
18df0 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c izeof(zContext),
18df1 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 zContext,.
18df2 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65 65 "On tree
18df3 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25 64 page %d cell %d
18df4 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a : ", iPage, i);.
18df5 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
18df6 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 Cell(pPage,i);.
18df7 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
18df8 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c lPtr(pPage, pCel
18df9 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 l, &info);. s
18dfa 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a z = info.nData;.
18dfb 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
18dfc 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 intKey ) sz += (
18dfd 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 int)info.nKey;.
18dfe 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 assert( sz==i
18dff 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a nfo.nPayload );.
18e00 20 20 20 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f if( (sz>info
18e01 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 .nLocal) . &
18e02 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 & (&pCell[info.i
18e03 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 Overflow]<=&pPag
18e04 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 e->aData[pBt->us
18e05 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 ableSize]). )
18e06 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 {. int nPag
18e07 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e e = (sz - info.n
18e08 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 Local + usableSi
18e09 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 ze - 5)/(usableS
18e0a 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 ize - 4);.
18e0b 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 Pgno pgnoOvfl =
18e0c 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b get4byte(&pCell[
18e0d 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 info.iOverflow])
18e0e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
18e0f 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
18e10 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
18e11 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 autoVacuum ){.
18e12 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18e13 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 p(pCheck, pgnoOv
18e14 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 fl, PTRMAP_OVERF
18e15 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f LOW1, iPage, zCo
18e16 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a ntext);. }.
18e17 23 65 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 #endif. che
18e18 63 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 ckList(pCheck, 0
18e19 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 , pgnoOvfl, nPag
18e1a 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 e, zContext);.
18e1b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 }.. /* Chec
18e1c 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 k sanity of left
18e1d 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 child page..
18e1e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 */. if( !pPa
18e1f 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
18e20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 pgno = get4byt
18e21 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 e(pCell);.#ifnde
18e22 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
18e23 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 TOVACUUM. i
18e24 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 f( pBt->autoVacu
18e25 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 um ){. ch
18e26 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b eckPtrmap(pCheck
18e27 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 , pgno, PTRMAP_B
18e28 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f TREE, iPage, zCo
18e29 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a ntext);. }.
18e2a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 #endif. d2
18e2b 3d 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 = checkTreePage(
18e2c 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 pCheck, pgno, zC
18e2d 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 ontext);. i
18e2e 66 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 f( i>0 && d2!=de
18e2f 70 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 pth ){. c
18e30 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18e31 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18e32 22 43 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 "Child page dept
18e33 68 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 h differs");.
18e34 20 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 }. depth
18e35 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d = d2;. }. }
18e36 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c . if( !pPage->l
18e37 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 eaf ){. pgno
18e38 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
18e39 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
18e3a 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 hdrOffset+8]);.
18e3b 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
18e3c 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 ntf(sizeof(zCont
18e3d 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 ext), zContext,
18e3e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
18e3f 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 "On page %
18e40 64 20 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 d at right child
18e41 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 : ", iPage);.#if
18e42 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18e43 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
18e44 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
18e45 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 uum ){. che
18e46 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c ckPtrmap(pCheck,
18e47 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 pgno, PTRMAP_BT
18e48 52 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a REE, iPage, 0);.
18e49 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
18e4a 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 checkTreePage(p
18e4b 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f Check, pgno, zCo
18e4c 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 ntext);. }. .
18e4d 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d /* Check for com
18e4e 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f plete coverage o
18e4f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a f the page. */.
18e50 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e data = pPage->
18e51 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 aData;. hdr = p
18e52 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
18e53 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 . hit = sqlite3
18e54 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d PageMalloc( pBt-
18e55 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 >pageSize );. i
18e56 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 f( hit==0 ){.
18e57 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 pCheck->mallocF
18e58 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c ailed = 1;. }el
18e59 73 65 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 se{. u16 cont
18e5a 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 entOffset = get2
18e5b 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
18e5c 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ]);. assert(
18e5d 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 contentOffset<=u
18e5e 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a sableSize ); /*
18e5f 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 Enforced by btr
18e60 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a eeInitPage() */.
18e61 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 memset(hit+c
18e62 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c ontentOffset, 0,
18e63 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 usableSize-cont
18e64 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 entOffset);.
18e65 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c 20 63 memset(hit, 1, c
18e66 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 ontentOffset);.
18e67 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 nCell = get2b
18e68 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
18e69 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 74 );. cellStart
18e6a 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a = hdr + 12 - 4*
18e6b 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 pPage->leaf;.
18e6c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
18e6d 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 l; i++){. i
18e6e 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 nt pc = get2byte
18e6f 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 (&data[cellStart
18e70 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 +i*2]);. u1
18e71 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 6 size = 1024;.
18e72 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 int j;.
18e73 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 if( pc<=usable
18e74 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 Size-4 ){.
18e75 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a size = cellSiz
18e76 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 ePtr(pPage, &dat
18e77 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a a[pc]);. }.
18e78 20 20 20 20 20 20 69 66 28 20 28 70 63 2b 73 69 if( (pc+si
18e79 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a ze-1)>=usableSiz
18e7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 e ){. che
18e7b 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18e7c 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 ck, 0, .
18e7d 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 "Corruption
18e7e 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c 6c detected in cell
18e7f 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c %d on page %d",
18e80 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 i,iPage,0);.
18e81 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
18e82 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 for(j=pc+size-1
18e83 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 ; j>=pc; j--) hi
18e84 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a t[j]++;. }.
18e85 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 }. i = ge
18e86 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
18e87 2b 31 5d 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 +1]);. while(
18e88 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e i>0 ){. in
18e89 74 20 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 t size, j;.
18e8a 20 61 73 73 65 72 74 28 20 69 3c 3d 75 73 61 62 assert( i<=usab
18e8b 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 leSize-4 );
18e8c 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 /* Enforced by b
18e8d 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a treeInitPage() *
18e8e 2f 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 /. size = g
18e8f 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 2b et2byte(&data[i+
18e90 32 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 2]);. asser
18e91 74 28 20 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c t( i+size<=usabl
18e92 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 eSize ); /* Enf
18e93 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e orced by btreeIn
18e94 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 itPage() */.
18e95 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 for(j=i+size-1
18e96 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 ; j>=i; j--) hit
18e97 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d [j]++;. j =
18e98 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
18e99 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i]);. asser
18e9a 74 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 t( j==0 || j>i+s
18e9b 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 ize ); /* Enfor
18e9c 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 ced by btreeInit
18e9d 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 Page() */.
18e9e 61 73 73 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c assert( j<=usabl
18e9f 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 eSize-4 ); /*
18ea0 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 Enforced by btre
18ea1 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 eInitPage() */.
18ea2 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 i = j;.
18ea3 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d }. for(i=cnt=
18ea4 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 0; i<usableSize;
18ea5 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
18ea6 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 hit[i]==0 ){.
18ea7 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 cnt++;.
18ea8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69 74 }else if( hit
18ea9 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 [i]>1 ){.
18eaa 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18eab 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 pCheck, 0,.
18eac 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 "Multiple u
18ead 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64 20 ses for byte %d
18eae 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c 20 of page %d", i,
18eaf 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 iPage);.
18eb0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
18eb1 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 }. if( cnt
18eb2 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b !=data[hdr+7] ){
18eb3 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 . checkAppe
18eb4 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c ndMsg(pCheck, 0,
18eb5 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72 61 . "Fra
18eb6 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 gmentation of %d
18eb7 20 62 79 74 65 73 20 72 65 70 6f 72 74 65 64 20 bytes reported
18eb8 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 as %d on page %d
18eb9 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 ",. cnt
18eba 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 , data[hdr+7], i
18ebb 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Page);. }. }
18ebc 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 . sqlite3PageFr
18ebd 65 65 28 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 ee(hit);. relea
18ebe 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
18ebf 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b return depth+1;
18ec0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
18ec1 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
18ec2 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 TY_CHECK */..#if
18ec3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18ec4 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
18ec5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
18ec6 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c ine does a compl
18ec7 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 ete check of the
18ec8 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c given BTree fil
18ec9 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a e. aRoot[] is.*
18eca 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 * an array of pa
18ecb 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 ges numbers were
18ecc 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 each page numbe
18ecd 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 r is the root pa
18ece 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 ge of.** a table
18ecf 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 . nRoot is the
18ed0 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 number of entrie
18ed1 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a s in aRoot..**.*
18ed2 2a 20 41 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 * A read-only or
18ed3 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e read-write tran
18ed4 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 saction must be
18ed5 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 63 61 opened before ca
18ed6 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 lling.** this fu
18ed7 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 nction..**.** Wr
18ed8 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ite the number o
18ed9 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 f error seen in
18eda 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 *pnErr. Except
18edb 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a for some memory.
18edc 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ** allocation er
18edd 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 rors, an error
18ede 6d 65 73 73 61 67 65 20 68 65 6c 64 20 69 6e 20 message held in
18edf 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
18ee0 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 from.** malloc i
18ee1 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a 70 s returned if *p
18ee2 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f nErr is non-zero
18ee3 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 . If *pnErr==0
18ee4 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 then NULL is.**
18ee5 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 20 returned. If a
18ee6 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
18ee7 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
18ee8 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
18ee9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18eea 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
18eeb 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 3BtreeIntegrityC
18eec 68 65 63 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 heck(. Btree *p
18eed 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 , /* The btr
18eee 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 ee to be checked
18eef 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 */. int *aRoot
18ef0 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 , /* An array
18ef1 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 of root pages nu
18ef2 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 69 76 69 mbers for indivi
18ef3 64 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 dual trees */.
18ef4 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a int nRoot, /*
18ef5 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
18ef6 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f es in aRoot[] */
18ef7 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 . int mxErr,
18ef8 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 69 /* Stop reporti
18ef9 6e 67 20 65 72 72 6f 72 73 20 61 66 74 65 72 20 ng errors after
18efa 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 this many */. i
18efb 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 nt *pnErr /*
18efc 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 Write number of
18efd 65 72 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 errors seen to t
18efe 68 69 73 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a his variable */.
18eff 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 ){. Pgno i;. i
18f00 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 nt nRef;. Integ
18f01 72 69 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 rityCk sCheck;.
18f02 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
18f03 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 p->pBt;. char
18f04 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 zErr[100];.. sq
18f05 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
18f06 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p);. assert( p-
18f07 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e >inTrans>TRANS_N
18f08 4f 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 ONE && pBt->inTr
18f09 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f ansaction>TRANS_
18f0a 4e 4f 4e 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d NONE );. nRef =
18f0b 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
18f0c 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 count(pBt->pPage
18f0d 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 r);. sCheck.pBt
18f0e 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 63 6b = pBt;. sCheck
18f0f 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 .pPager = pBt->p
18f10 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e Pager;. sCheck.
18f11 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 nPage = pagerPag
18f12 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 ecount(sCheck.pB
18f13 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 t);. sCheck.mxE
18f14 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 rr = mxErr;. sC
18f15 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 heck.nErr = 0;.
18f16 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 sCheck.mallocFa
18f17 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 iled = 0;. *pnE
18f18 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 rr = 0;. if( sC
18f19 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b heck.nPage==0 ){
18f1a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
18f1b 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 eLeave(p);. r
18f1c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 eturn 0;. }. s
18f1d 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 Check.anRef = sq
18f1e 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 lite3Malloc( (sC
18f1f 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 heck.nPage+1)*si
18f20 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 zeof(sCheck.anRe
18f21 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 f[0]) );. if( !
18f22 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a sCheck.anRef ){.
18f23 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a *pnErr = 1;.
18f24 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
18f25 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 Leave(p);. re
18f26 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f turn 0;. }. fo
18f27 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b r(i=0; i<=sCheck
18f28 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 .nPage; i++){ sC
18f29 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 heck.anRef[i] =
18f2a 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 0; }. i = PENDI
18f2b 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
18f2c 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 );. if( i<=sChe
18f2d 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 ck.nPage ){.
18f2e 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 sCheck.anRef[i]
18f2f 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = 1;. }. sqlit
18f30 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 e3StrAccumInit(&
18f31 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a sCheck.errMsg, z
18f32 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 Err, sizeof(zErr
18f33 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a ), 20000);.. /*
18f34 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 Check the integ
18f35 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 rity of the free
18f36 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 list. */. chec
18f37 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 kList(&sCheck, 1
18f38 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d , get4byte(&pBt-
18f39 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
18f3a 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2]),.
18f3b 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e get4byte(&pBt->
18f3c 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
18f3d 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 ]), "Main freeli
18f3e 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 st: ");.. /* Ch
18f3f 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c eck all the tabl
18f40 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 es.. */. for(i
18f41 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 =0; (int)i<nRoot
18f42 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 && sCheck.mxErr
18f43 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
18f44 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f aRoot[i]==0 ) co
18f45 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 ntinue;.#ifndef
18f46 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
18f47 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 VACUUM. if( p
18f48 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 Bt->autoVacuum &
18f49 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a & aRoot[i]>1 ){.
18f4a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18f4b 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 p(&sCheck, aRoot
18f4c 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 [i], PTRMAP_ROOT
18f4d 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 PAGE, 0, 0);.
18f4e 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 }.#endif. ch
18f4f 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 eckTreePage(&sCh
18f50 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 eck, aRoot[i], "
18f51 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f List of tree roo
18f52 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f ts: ");. }.. /
18f53 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72 * Make sure ever
18f54 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 y page in the fi
18f55 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 le is referenced
18f56 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b . */. for(i=1;
18f57 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 i<=sCheck.nPage
18f58 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 && sCheck.mxErr
18f59 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 ; i++){.#ifdef S
18f5a 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
18f5b 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43 ACUUM. if( sC
18f5c 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 heck.anRef[i]==0
18f5d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 ){. checkA
18f5e 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b ppendMsg(&sCheck
18f5f 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73 , 0, "Page %d is
18f60 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29 never used", i)
18f61 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.#else.
18f62 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 /* If the data
18f63 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 base supports au
18f64 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 to-vacuum, make
18f65 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 sure no tables c
18f66 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 ontain. ** re
18f67 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e ferences to poin
18f68 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 ter-map pages..
18f69 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 */. if( sC
18f6a 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 heck.anRef[i]==0
18f6b 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 && . (PTR
18f6c 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 MAP_PAGENO(pBt,
18f6d 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 i)!=i || !pBt->a
18f6e 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 utoVacuum) ){.
18f6f 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18f70 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 sg(&sCheck, 0, "
18f71 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 Page %d is never
18f72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 used", i);.
18f73 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b }. if( sCheck
18f74 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 .anRef[i]!=0 &&
18f75 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f . (PTRMAP_
18f76 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d PAGENO(pBt, i)==
18f77 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 i && pBt->autoVa
18f78 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 cuum) ){. c
18f79 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 heckAppendMsg(&s
18f7a 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 Check, 0, "Point
18f7b 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69 er map page %d i
18f7c 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69 s referenced", i
18f7d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
18f7e 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
18f7f 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69 ure this analysi
18f80 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 s did not leave
18f81 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65 any unref() page
18f82 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 s.. ** This is
18f83 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 an internal cons
18f84 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 istency check; a
18f85 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 n integrity chec
18f86 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e k. ** of the in
18f87 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 tegrity check..
18f88 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
18f89 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65 33 50 nRef != sqlite3P
18f8a 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 agerRefcount(pBt
18f8b 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a 20 20 ->pPager)) ){.
18f8c 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18f8d 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 (&sCheck, 0, .
18f8e 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 "Outstanding
18f8f 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 page count goes
18f90 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 from %d to %d d
18f91 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 uring this analy
18f92 73 69 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 sis",. nRef
18f93 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 , sqlite3PagerRe
18f94 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 fcount(pBt->pPag
18f95 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a er). );. }..
18f96 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 /* Clean up a
18f97 6e 64 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 nd report errors
18f98 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
18f99 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
18f9a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 43 sqlite3_free(sC
18f9b 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 heck.anRef);. i
18f9c 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 f( sCheck.malloc
18f9d 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 Failed ){. sq
18f9e 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 lite3StrAccumRes
18f9f 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 et(&sCheck.errMs
18fa0 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d g);. *pnErr =
18fa1 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a sCheck.nErr+1;.
18fa2 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
18fa3 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 }. *pnErr = sCh
18fa4 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 eck.nErr;. if(
18fa5 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 sCheck.nErr==0 )
18fa6 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
18fa7 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 Reset(&sCheck.er
18fa8 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 rMsg);. return
18fa9 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
18faa 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e 65 72 inish(&sCheck.er
18fab 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 rMsg);.}.#endif
18fac 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 /* SQLITE_OMIT_I
18fad 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a NTEGRITY_CHECK *
18fae 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 /../*.** Return
18faf 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d the full pathnam
18fb0 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 e of the underly
18fb1 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c ing database fil
18fb2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 e..**.** The pag
18fb3 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 er filename is i
18fb4 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 nvariant as long
18fb5 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69 73 as the pager is
18fb6 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 .** open so it i
18fb7 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 s safe to access
18fb8 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 53 without the BtS
18fb9 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a hared mutex..*/.
18fba 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
18fbb 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
18fbc 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
18fbd 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 me(Btree *p){.
18fbe 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e assert( p->pBt->
18fbf 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 pPager!=0 );. r
18fc0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 eturn sqlite3Pag
18fc1 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 erFilename(p->pB
18fc2 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f t->pPager);.}../
18fc3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
18fc4 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 pathname of the
18fc5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
18fc6 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 this database.
18fc7 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 The return.** va
18fc8 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 lue of this rout
18fc9 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 ine is the same
18fca 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
18fcb 65 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 ether the journa
18fcc 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 l file.** has be
18fcd 65 6e 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f en created or no
18fce 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 t..**.** The pag
18fcf 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e er journal filen
18fd0 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 ame is invariant
18fd1 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 as long as the
18fd2 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e pager is.** open
18fd3 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 so it is safe t
18fd4 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 o access without
18fd5 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 the BtShared mu
18fd6 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 tex..*/.SQLITE_P
18fd7 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
18fd8 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 r *sqlite3BtreeG
18fd9 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 etJournalname(Bt
18fda 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
18fdb 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 t( p->pBt->pPage
18fdc 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e r!=0 );. return
18fdd 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
18fde 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d rnalname(p->pBt-
18fdf 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a >pPager);.}../*.
18fe0 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 ** Return non-ze
18fe1 72 6f 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 ro if a transact
18fe2 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a ion is active..*
18fe3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18fe4 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
18fe5 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 eIsInTrans(Btree
18fe6 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
18fe7 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f p==0 || sqlite3_
18fe8 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
18fe9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 ->mutex) );. re
18fea 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 turn (p && (p->i
18feb 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
18fec 49 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ITE));.}../*.**
18fed 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 Return non-zero
18fee 69 66 20 61 20 72 65 61 64 20 28 6f 72 20 77 72 if a read (or wr
18fef 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e ite) transaction
18ff0 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 is active..*/.S
18ff1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18ff2 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
18ff3 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 InReadTrans(Btre
18ff4 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
18ff5 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p );. assert(
18ff6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
18ff7 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
18ff8 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e );. return p->
18ff9 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e inTrans!=TRANS_N
18ffa 4f 4e 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 ONE;.}..SQLITE_P
18ffb 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18ffc 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 e3BtreeIsInBacku
18ffd 70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 p(Btree *p){. a
18ffe 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 ssert( p );. as
18fff 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
19000 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
19001 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 mutex) );. retu
19002 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 rn p->nBackup!=0
19003 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
19004 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
19005 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
19006 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 blob of memory a
19007 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a ssociated with.*
19008 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72 65 * a single share
19009 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65 6d d-btree. The mem
1900a 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20 63 ory is used by c
1900b 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 lient code for i
1900c 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 ts own.** purpos
1900d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c es (for example,
1900e 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 67 68 to store a high
1900f 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 61 73 -level schema as
19010 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a sociated with .*
19011 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 * the shared-btr
19012 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 20 6c ee). The btree l
19013 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 65 66 ayer manages ref
19014 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 erence counting
19015 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 issues..**.** Th
19016 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 e first time thi
19017 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 s is called on a
19018 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c 20 6e shared-btree, n
19019 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 20 6d Bytes bytes of m
1901a 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c emory.** are all
1901b 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 ocated, zeroed,
1901c 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 6f 20 and returned to
1901d 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 the caller. For
1901e 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74 20 each subsequent
1901f 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 .** call the nBy
19020 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 tes parameter is
19021 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 ignored and a p
19022 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 61 ointer to the sa
19023 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 me blob.** of me
19024 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20 0a mory returned. .
19025 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 **.** If the nBy
19026 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 tes parameter is
19027 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 0 and the blob
19028 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f of memory has no
19029 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c t yet been.** al
1902a 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 located, a null
1902b 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 pointer is retur
1902c 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 ned. If the blob
1902d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1902e 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 n.** allocated,
1902f 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 it is returned a
19030 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 s normal..**.**
19031 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 Just before the
19032 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 73 20 shared-btree is
19033 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e 63 closed, the func
19034 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 tion passed as t
19035 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 he .** xFree arg
19036 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 6d ument when the m
19037 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
19038 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e 76 was made is inv
19039 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 oked on the .**
1903a 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 blob of allocate
1903b 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 d memory. This f
1903c 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e unction should n
1903d 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f ot call sqlite3_
1903e 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 free().** on the
1903f 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 memory, the btr
19040 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74 68 ee layer does th
19041 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 at..*/.SQLITE_PR
19042 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
19043 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 te3BtreeSchema(B
19044 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 tree *p, int nBy
19045 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 tes, void(*xFree
19046 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 )(void *)){. Bt
19047 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
19048 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 >pBt;. sqlite3B
19049 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
1904a 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65 6d if( !pBt->pSchem
1904b 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 a && nBytes ){.
1904c 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 pBt->pSchema
1904d 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
1904e 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 ero(nBytes);.
1904f 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d pBt->xFreeSchem
19050 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 a = xFree;. }.
19051 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
19052 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
19053 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a pBt->pSchema;.}.
19054 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 ./*.** Return SQ
19055 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 LITE_LOCKED_SHAR
19056 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f 74 68 EDCACHE if anoth
19057 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20 73 er user of the s
19058 61 6d 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 ame shared .** b
19059 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67 75 tree as the argu
1905a 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 ment handle hold
1905b 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c s an exclusive l
1905c 6f 63 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 ock on the .** s
1905d 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1905e 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20 53 51 le. Otherwise SQ
1905f 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 LITE_OK..*/.SQLI
19060 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19061 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d qlite3BtreeSchem
19062 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 aLocked(Btree *p
19063 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
19064 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
19065 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
19066 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
19067 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
19068 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 );. rc = queryS
19069 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
1906a 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f ock(p, MASTER_RO
1906b 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a OT, READ_LOCK);.
1906c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1906d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 LITE_OK || rc==S
1906e 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
1906f 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 73 71 REDCACHE );. sq
19070 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
19071 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
19072 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c .}...#ifndef SQL
19073 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
19074 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 CACHE./*.** Obta
19075 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 in a lock on the
19076 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f table whose roo
19077 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e 20 t page is iTab.
19078 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 The.** lock is
19079 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 a write lock if
1907a 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 isWritelock is t
1907b 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 6c 6f rue or a read lo
1907c 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 66 ck.** if it is f
1907d 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alse..*/.SQLITE_
1907e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1907f 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c te3BtreeLockTabl
19080 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 e(Btree *p, int
19081 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 iTab, u8 isWrite
19082 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 Lock){. int rc
19083 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
19084 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
19085 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b s!=TRANS_NONE );
19086 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 . if( p->sharab
19087 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 le ){. u8 loc
19088 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 kType = READ_LOC
19089 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b K + isWriteLock;
1908a 0a 20 20 20 20 61 73 73 65 72 74 28 20 52 45 41 . assert( REA
1908b 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f D_LOCK+1==WRITE_
1908c 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 LOCK );. asse
1908d 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d rt( isWriteLock=
1908e 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 =0 || isWriteLoc
1908f 6b 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c k==1 );.. sql
19090 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
19091 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 65 72 );. rc = quer
19092 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
19093 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c eLock(p, iTab, l
19094 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 66 ockType);. if
19095 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
19096 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 ){. rc = se
19097 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
19098 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c eLock(p, iTab, l
19099 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a ockType);. }.
1909a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1909b 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 Leave(p);. }.
1909c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
1909d 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
1909e 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
1909f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 B./*.** Argument
190a0 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61 20 pCsr must be a
190a1 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f cursor opened fo
190a2 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 r writing on an
190a3 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 .** INTKEY table
190a4 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
190a5 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 74 ing at a valid t
190a6 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 able entry. .**
190a7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f This function mo
190a8 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61 20 difies the data
190a9 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f stored as part o
190aa 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a f that entry..**
190ab 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 .** Only the dat
190ac 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e a content may on
190ad 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 ly be modified,
190ae 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 it is not possib
190af 6c 65 20 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 le to .** change
190b0 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 the length of t
190b1 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e 20 he data stored.
190b2 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e If this function
190b3 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 0a is called with.
190b4 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 ** parameters th
190b5 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 at attempt to wr
190b6 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 ite past the end
190b7 20 6f 66 20 74 68 65 20 65 78 69 73 74 69 6e 67 of the existing
190b8 20 64 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 data,.** no mod
190b9 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20 6d ifications are m
190ba 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 ade and SQLITE_C
190bb 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 72 6e ORRUPT is return
190bc 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
190bd 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
190be 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 3BtreePutData(Bt
190bf 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 Cursor *pCsr, u3
190c0 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
190c1 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 t, void *z){. i
190c2 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
190c3 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
190c4 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73 73 x(pCsr) );. ass
190c5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
190c6 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 ex_held(pCsr->pB
190c7 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
190c8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
190c9 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 sr->isIncrblobHa
190ca 6e 64 6c 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 ndle );.. rc =
190cb 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 restoreCursorPos
190cc 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 ition(pCsr);. i
190cd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
190ce 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
190cf 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 c;. }. assert(
190d0 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCsr->eState!=C
190d1 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
190d2 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d K );. if( pCsr-
190d3 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
190d4 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 VALID ){. ret
190d5 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 urn SQLITE_ABORT
190d6 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
190d7 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f k some assumptio
190d8 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 ns: . ** (a)
190d9 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70 the cursor is op
190da 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a en for writing,.
190db 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 72 65 ** (b) there
190dc 20 69 73 20 61 20 72 65 61 64 2f 77 72 69 74 65 is a read/write
190dd 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 transaction ope
190de 6e 2c 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68 n,. ** (c) th
190df 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c e connection hol
190e0 64 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 ds a write-lock
190e1 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 28 69 66 on the table (if
190e2 20 72 65 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a required),. **
190e3 20 20 20 28 64 29 20 74 68 65 72 65 20 61 72 65 (d) there are
190e4 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 no conflicting
190e5 72 65 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a read-locks, and.
190e6 20 20 2a 2a 20 20 20 28 65 29 20 74 68 65 20 63 ** (e) the c
190e7 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 ursor points at
190e8 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 a valid row of a
190e9 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a n intKey table..
190ea 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 72 */. if( !pCsr
190eb 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 ->wrFlag ){.
190ec 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 return SQLITE_RE
190ed 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 ADONLY;. }. as
190ee 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70 42 74 sert( !pCsr->pBt
190ef 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 ->readOnly && pC
190f0 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 sr->pBt->inTrans
190f1 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
190f2 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
190f3 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 hasSharedCacheT
190f4 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 ableLock(pCsr->p
190f5 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e Btree, pCsr->pgn
190f6 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a oRoot, 0, 2) );.
190f7 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65 assert( !hasRe
190f8 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 adConflicts(pCsr
190f9 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e ->pBtree, pCsr->
190fa 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 pgnoRoot) );. a
190fb 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61 70 50 ssert( pCsr->apP
190fc 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d age[pCsr->iPage]
190fd 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 ->intKey );.. r
190fe 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c eturn accessPayl
190ff 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74 oad(pCsr, offset
19100 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 , amt, (unsigned
19101 20 63 68 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d char *)z, 1);.}
19102 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 ../* .** Set a f
19103 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72 73 lag on this curs
19104 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65 20 or to cache the
19105 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 locations of pag
19106 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 es from the .**
19107 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f overflow list fo
19108 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f r the current ro
19109 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 w. This is used
1910a 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 by cursors opene
1910b 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 d.** for increme
1910c 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c ntal blob IO onl
1910d 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 y..**.** This fu
1910e 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66 6c nction sets a fl
1910f 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 ag only. The act
19110 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69 6f ual page locatio
19111 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 n cache.** (stor
19112 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 ed in BtCursor.a
19113 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 Overflow[]) is a
19114 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73 65 llocated and use
19115 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a d by function.**
19116 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 accessPayload()
19117 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75 6e (the worker fun
19118 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 ction for sqlite
19119 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e 64 3BtreeData() and
1911a 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 .** sqlite3Btree
1911b 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 PutData())..*/.S
1911c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1911d 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
1911e 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 acheOverflow(BtC
1911f 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
19120 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
19121 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
19122 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
19123 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
19124 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
19125 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
19126 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 ert(!pCur->isInc
19127 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 rblobHandle);.
19128 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f assert(!pCur->aO
19129 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 verflow);. pCur
1912a 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 ->isIncrblobHand
1912b 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 le = 1;.}.#endif
1912c 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1912d 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 * End of btree.c
1912e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1912f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19131 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
19132 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 61 63 * Begin file bac
19133 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a kup.c **********
19134 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19135 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19136 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e /./*.** 2009 Jan
19137 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 uary 28.**.** Th
19138 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
19139 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1913a 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1913b 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1913c 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1913d 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1913e 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1913f 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
19140 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
19141 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
19142 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
19143 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
19144 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
19145 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
19146 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
19147 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
19148 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
19149 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1914a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1914b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1914c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1914d 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
1914e 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
1914f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
19150 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 of the sqlite3_b
19151 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 ackup_XXX() .**
19152 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e API functions an
19153 64 20 74 68 65 20 72 65 6c 61 74 65 64 20 66 65 d the related fe
19154 61 74 75 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a 20 4d atures..*/../* M
19155 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 acro to find the
19156 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 minimum of two
19157 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a numeric values..
19158 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 */.#ifndef MIN.#
19159 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 define MIN(x,y)
1915a 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 ((x)<(y)?(x):(y
1915b 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )).#endif../*.**
1915c 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 Structure alloc
1915d 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 62 61 ated for each ba
1915e 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a ckup operation..
1915f 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
19160 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 3_backup {. sql
19161 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b 20 20 ite3* pDestDb;
19162 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 /* Destina
19163 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 tion database ha
19164 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ndle */. Btree
19165 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 *pDest;
19166 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f /* Destinatio
19167 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f n b-tree file */
19168 0a 20 20 75 33 32 20 69 44 65 73 74 53 63 68 65 . u32 iDestSche
19169 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f ma; /* O
1916a 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 riginal schema c
1916b 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69 6e 61 ookie in destina
1916c 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 tion */. int bD
1916d 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 estLocked;
1916e 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 /* True once
1916f 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 a write-transact
19170 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 ion is open on p
19171 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 Dest */.. Pgno
19172 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 iNext;
19173 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
19174 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 er of the next s
19175 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20 63 6f ource page to co
19176 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a py */. sqlite3*
19177 20 70 53 72 63 44 62 3b 20 20 20 20 20 20 20 20 pSrcDb;
19178 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 62 /* Source datab
19179 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
1917a 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20 20 20 Btree *pSrc;
1917b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 /* Sour
1917c 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65 20 2a ce b-tree file *
1917d 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 /.. int rc;
1917e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1917f 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 Backup process
19180 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 error code */..
19181 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20 76 61 /* These two va
19182 72 69 61 62 6c 65 73 20 61 72 65 20 73 65 74 20 riables are set
19183 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20 74 6f by every call to
19184 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 backup_step().
19185 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 They are. ** re
19186 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 ad by calls to b
19187 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
19188 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 ) and backup_pag
19189 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 ecount().. */.
1918a 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 Pgno nRemaining
1918b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
1918c 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c 65 66 ber of pages lef
1918d 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 t to copy */. P
1918e 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 gno nPagecount;
1918f 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
19190 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
19191 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 69 to copy */.. i
19192 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b 20 20 nt isAttached;
19193 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
19194 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61 73 20 once backup has
19195 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 20 been registered
19196 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a 20 20 with pager */.
19197 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
19198 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 pNext; /* Next
19199 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69 61 74 backup associat
1919a 65 64 20 77 69 74 68 20 73 6f 75 72 63 65 20 70 ed with source p
1919b 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ager */.};../*.*
1919c 2a 20 54 48 52 45 41 44 20 53 41 46 45 54 59 20 * THREAD SAFETY
1919d 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f NOTES:.**.** O
1919e 6e 63 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 nce it has been
1919f 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 62 61 created using ba
191a0 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 ckup_init(), a s
191a1 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 ingle sqlite3_ba
191a2 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 ckup.** struct
191a3 75 72 65 20 6d 61 79 20 62 65 20 61 63 63 65 73 ure may be acces
191a4 73 65 64 20 76 69 61 20 74 77 6f 20 67 72 6f 75 sed via two grou
191a5 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73 61 66 ps of thread-saf
191a6 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a e entry points:.
191a7 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 **.** * Via
191a8 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
191a9 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e up_XXX() API fun
191aa 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 ction backup_ste
191ab 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 p() and .**
191ac 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 backup_finish(
191ad 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20 66 75 ). Both these fu
191ae 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 nctions obtain t
191af 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
191b0 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 se.** hand
191b1 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74 68 65 le mutex and the
191b2 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
191b3 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63 d with the sourc
191b4 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 e BtShared .**
191b5 20 20 20 20 20 73 74 72 75 63 74 75 72 65 2c 20 structure,
191b6 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a in that order..*
191b7 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 *.** * Via t
191b8 68 65 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 he BackupUpdate(
191b9 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65 73 74 ) and BackupRest
191ba 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c art() functions,
191bb 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 which are.**
191bc 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 invoked by t
191bd 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 74 he pager layer t
191be 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f 75 73 o report various
191bf 20 73 74 61 74 65 20 63 68 61 6e 67 65 73 20 69 state changes i
191c0 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 20 70 n.** the p
191c1 61 67 65 20 63 61 63 68 65 20 61 73 73 6f 63 69 age cache associ
191c2 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f ated with the so
191c3 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 urce database. T
191c4 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 he mutex.**
191c5 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 associated wit
191c6 68 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 h the source dat
191c7 61 62 61 73 65 20 42 74 53 68 61 72 65 64 20 73 abase BtShared s
191c8 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 61 6c tructure will al
191c9 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 ways .** b
191ca 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69 74 68 e held when eith
191cb 65 72 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 er of these func
191cc 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 tions are invoke
191cd 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f d..**.** The o
191ce 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62 61 63 ther sqlite3_bac
191cf 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 75 kup_XXX() API fu
191d0 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f nctions, backup_
191d1 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a remaining() and.
191d2 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61 67 65 ** backup_page
191d3 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 count() are not
191d4 74 68 72 65 61 64 2d 73 61 66 65 20 66 75 6e 63 thread-safe func
191d5 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79 20 61 tions. If they a
191d6 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 re called.** w
191d7 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 hile some other
191d8 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c 69 6e thread is callin
191d9 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 g backup_step()
191da 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 or backup_finish
191db 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c (),.** the val
191dc 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d 61 79 ues returned may
191dd 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 be invalid. The
191de 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 re is no way for
191df 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 a call to.**
191e0 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 6f BackupUpdate() o
191e1 72 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 r BackupRestart(
191e2 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65 20 77 ) to interfere w
191e3 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 ith backup_remai
191e4 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 ning().** or b
191e5 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 ackup_pagecount(
191e6 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e )..**.** Depen
191e7 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 ding on the SQLi
191e8 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e te configuration
191e9 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 , the database h
191ea 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a andles and/or.**
191eb 20 20 20 74 68 65 20 42 74 72 65 65 20 6f 62 6a the Btree obj
191ec 65 63 74 73 20 6d 61 79 20 68 61 76 65 20 74 68 ects may have th
191ed 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 eir own mutexes
191ee 74 68 61 74 20 72 65 71 75 69 72 65 20 6c 6f 63 that require loc
191ef 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 king..** Non-s
191f0 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20 28 harable Btrees (
191f1 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
191f2 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 ses for example)
191f3 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a , do not have.**
191f4 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 6d 75 associated mu
191f5 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a texes..*/../*.**
191f6 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
191f7 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 r corresponding
191f8 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44 62 20 to database zDb
191f9 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 (i.e. "main", "t
191fa 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e emp").** in conn
191fb 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 ection handle pD
191fc 62 2e 20 49 66 20 73 75 63 68 20 61 20 64 61 74 b. If such a dat
191fd 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 abase cannot be
191fe 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a found, return.**
191ff 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
19200 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65 72 72 and write an err
19201 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 or message to pE
19202 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 rrorDb..**.** If
19203 20 74 68 65 20 22 74 65 6d 70 22 20 64 61 74 61 the "temp" data
19204 62 61 73 65 20 69 73 20 72 65 71 75 65 73 74 65 base is requeste
19205 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74 d, it may need t
19206 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79 20 74 o be opened by t
19207 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e his .** function
19208 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
19209 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 curs while doing
1920a 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 61 6e so, return 0 an
1920b 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 d write an .** e
1920c 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 rror message to
1920d 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 pErrorDb..*/.sta
1920e 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e 64 42 tic Btree *findB
1920f 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a 70 45 tree(sqlite3 *pE
19210 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65 33 20 rrorDb, sqlite3
19211 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *pDb, const char
19212 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 *zDb){. int i
19213 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e = sqlite3FindDbN
19214 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a ame(pDb, zDb);..
19215 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 if( i==1 ){.
19216 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
19217 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 30 3b . int rc = 0;
19218 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20 73 71 . pParse = sq
19219 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a lite3StackAllocZ
1921a 65 72 6f 28 70 45 72 72 6f 72 44 62 2c 20 73 69 ero(pErrorDb, si
1921b 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a zeof(*pParse));.
1921c 20 20 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d if( pParse==
1921d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1921e 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62 e3Error(pErrorDb
1921f 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 , SQLITE_NOMEM,
19220 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 "out of memory")
19221 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
19222 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
19223 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 72 else{. pPar
19224 73 65 2d 3e 64 62 20 3d 20 70 44 62 3b 0a 20 20 se->db = pDb;.
19225 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
19226 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 penTempDatabase(
19227 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 pParse) ){.
19228 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 sqlite3ErrorC
19229 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 lear(pParse);.
1922a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1922b 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 or(pErrorDb, pPa
1922c 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c 20 70 rse->rc, "%s", p
1922d 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b Parse->zErrMsg);
1922e 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1922f 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
19230 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
19231 33 53 74 61 63 6b 46 72 65 65 28 70 45 72 72 6f 3StackFree(pErro
19232 72 44 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 rDb, pParse);.
19233 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 }. if( rc )
19234 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 {. return 0
19235 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
19236 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 f( i<0 ){. sq
19237 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f lite3Error(pErro
19238 72 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f rDb, SQLITE_ERRO
19239 52 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 R, "unknown data
1923a 62 61 73 65 20 25 73 22 2c 20 7a 44 62 29 3b 0a base %s", zDb);.
1923b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1923c 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 44 62 2d }.. return pDb-
1923d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 7d 0a 0a >aDb[i].pBt;.}..
1923e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 20 /*.** Create an
1923f 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 70 sqlite3_backup p
19240 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 20 74 rocess to copy t
19241 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a he contents of z
19242 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20 63 6f SrcDb from.** co
19243 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
19244 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 74 44 pSrcDb to zDestD
19245 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 49 66 b in pDestDb. If
19246 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 successful, ret
19247 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 urn.** a pointer
19248 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71 6c 69 to the new sqli
19249 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 te3_backup objec
1924a 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 t..**.** If an e
1924b 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c rror occurs, NUL
1924c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e L is returned an
1924d 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 d an error code
1924e 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 and error messag
1924f 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 64 e.** stored in d
19250 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 70 atabase handle p
19251 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54 DestDb..*/.SQLIT
19252 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61 E_API sqlite3_ba
19253 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61 ckup *sqlite3_ba
19254 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c ckup_init(. sql
19255 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c 20 20 ite3* pDestDb,
19256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19257 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 /* Database t
19258 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20 o write to */.
19259 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 73 const char *zDes
1925a 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 tDb,
1925b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1925c 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e database within
1925d 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20 73 71 pDestDb */. sq
1925e 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c 20 20 lite3* pSrcDb,
1925f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19260 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
19261 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 connection to re
19262 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 6f 6e ad from */. con
19263 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44 62 20 st char *zSrcDb
19264 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19265 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 /* Name of da
19266 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 53 tabase within pS
19267 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c rcDb */.){. sql
19268 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 ite3_backup *p;
19269 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1926a 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
1926b 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4c eturn */.. /* L
1926c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65 20 64 ock the source d
1926d 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 atabase handle.
1926e 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 The destination
1926f 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68 61 database. ** ha
19270 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b ndle is not lock
19271 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 ed in this routi
19272 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 6c 6f ne, but it is lo
19273 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73 71 6c cked in. ** sql
19274 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
19275 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 73 20 (). The user is
19276 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e 73 75 required to ensu
19277 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a 2a 20 re that no. **
19278 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 63 63 other thread acc
19279 65 73 73 65 73 20 74 68 65 20 64 65 73 74 69 6e esses the destin
1927a 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 ation handle for
1927b 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a 20 20 the duration.
1927c 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b 75 70 ** of the backup
1927d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 operation. Any
1927e 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
1927f 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a the destination.
19280 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f ** database co
19281 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 nnection while a
19282 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 72 backup is in pr
19283 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75 73 65 ogress may cause
19284 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e 63 74 . ** a malfunct
19285 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c 6f 63 ion or a deadloc
19286 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 k.. */. sqlite
19287 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 53 3_mutex_enter(pS
19288 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 rcDb->mutex);.
19289 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1928a 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 ter(pDestDb->mut
1928b 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53 72 63 ex);.. if( pSrc
1928c 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b 0a 20 Db==pDestDb ){.
1928d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
1928e 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 44 62 . pDestDb
1928f 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 , SQLITE_ERROR,
19290 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 "source and dest
19291 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 ination must be
19292 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20 29 3b distinct". );
19293 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65 . p = 0;. }e
19294 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c lse {. /* All
19295 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
19296 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 61 a new sqlite3_ba
19297 63 6b 75 70 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ckup object */.
19298 20 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f p = (sqlite3_
19299 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 74 65 33 backup *)sqlite3
1929a 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 73 _malloc(sizeof(s
1929b 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b qlite3_backup));
1929c 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 . if( !p ){.
1929d 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1929e 72 28 70 44 65 73 74 44 62 2c 20 53 51 4c 49 54 r(pDestDb, SQLIT
1929f 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 E_NOMEM, 0);.
192a0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
192a1 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 the allocation s
192a2 75 63 63 65 65 64 65 64 2c 20 70 6f 70 75 6c 61 ucceeded, popula
192a3 74 65 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 te the new objec
192a4 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b t. */. if( p ){
192a5 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 . memset(p, 0
192a6 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 , sizeof(sqlite3
192a7 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 20 70 _backup));. p
192a8 2d 3e 70 53 72 63 20 3d 20 66 69 6e 64 42 74 72 ->pSrc = findBtr
192a9 65 65 28 70 44 65 73 74 44 62 2c 20 70 53 72 63 ee(pDestDb, pSrc
192aa 44 62 2c 20 7a 53 72 63 44 62 29 3b 0a 20 20 20 Db, zSrcDb);.
192ab 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69 6e 64 p->pDest = find
192ac 42 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70 Btree(pDestDb, p
192ad 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44 62 29 DestDb, zDestDb)
192ae 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 44 62 ;. p->pDestDb
192af 20 3d 20 70 44 65 73 74 44 62 3b 0a 20 20 20 20 = pDestDb;.
192b0 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53 72 63 p->pSrcDb = pSrc
192b1 44 62 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 Db;. p->iNext
192b2 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 69 73 41 = 1;. p->isA
192b3 74 74 61 63 68 65 64 20 3d 20 30 3b 0a 0a 20 20 ttached = 0;..
192b4 20 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53 72 63 if( 0==p->pSrc
192b5 20 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73 74 20 || 0==p->pDest
192b6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 ){. /* One
192b7 28 6f 72 20 62 6f 74 68 29 20 6f 66 20 74 68 65 (or both) of the
192b8 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65 73 named databases
192b9 20 64 69 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 did not exist.
192ba 41 6e 20 65 72 72 6f 72 20 68 61 73 0a 20 20 20 An error has.
192bb 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 ** already be
192bc 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 en written into
192bd 74 68 65 20 70 44 65 73 74 44 62 20 68 61 6e 64 the pDestDb hand
192be 6c 65 2e 20 41 6c 6c 20 74 68 61 74 20 69 73 20 le. All that is
192bf 6c 65 66 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f left. ** to
192c0 20 64 6f 20 68 65 72 65 20 69 73 20 66 72 65 65 do here is free
192c1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 the sqlite3_bac
192c2 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e 0a 20 kup structure..
192c3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
192c4 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 lite3_free(p);.
192c5 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 20 20 p = 0;.
192c6 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b }. }. if( p ){
192c7 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 . p->pSrc->nB
192c8 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 ackup++;. }..
192c9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
192ca 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 ave(pDestDb->mut
192cb 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d ex);. sqlite3_m
192cc 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72 63 44 utex_leave(pSrcD
192cd 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 b->mutex);. ret
192ce 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
192cf 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61 Argument rc is a
192d0 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
192d1 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 ode. Return true
192d2 20 69 66 20 74 68 69 73 20 65 72 72 6f 72 20 69 if this error i
192d3 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 s .** considered
192d4 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e fatal if encoun
192d5 74 65 72 65 64 20 64 75 72 69 6e 67 20 61 20 62 tered during a b
192d6 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
192d7 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61 All errors.** a
192d8 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61 re considered fa
192d9 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53 tal except for S
192da 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53 QLITE_BUSY and S
192db 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f QLITE_LOCKED..*/
192dc 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 46 61 .static int isFa
192dd 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72 63 29 talError(int rc)
192de 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 21 3d {. return (rc!=
192df 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
192e0 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 =SQLITE_BUSY &&
192e1 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c 49 54 ALWAYS(rc!=SQLIT
192e2 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a 0a 2f E_LOCKED));.}../
192e3 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a *.** Parameter z
192e4 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73 20 74 SrcData points t
192e5 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 o a buffer conta
192e6 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61 20 66 ining the data f
192e7 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53 72 63 or .** page iSrc
192e8 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 Pg from the sour
192e9 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6f 70 ce database. Cop
192ea 79 20 74 68 69 73 20 64 61 74 61 20 69 6e 74 6f y this data into
192eb 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69 6e 61 the .** destina
192ec 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 2a tion database..*
192ed 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 /.static int bac
192ee 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c 69 74 kupOnePage(sqlit
192ef 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 50 67 e3_backup *p, Pg
192f0 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e 73 74 no iSrcPg, const
192f1 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29 7b 0a u8 *zSrcData){.
192f2 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 Pager * const
192f3 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c pDestPager = sql
192f4 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 ite3BtreePager(p
192f5 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 ->pDest);. cons
192f6 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d t int nSrcPgsz =
192f7 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
192f8 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 PageSize(p->pSrc
192f9 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74 50 67 );. int nDestPg
192fa 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 sz = sqlite3Btre
192fb 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e eGetPageSize(p->
192fc 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 pDest);. const
192fd 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 int nCopy = MIN(
192fe 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 74 50 nSrcPgsz, nDestP
192ff 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20 69 36 gsz);. const i6
19300 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 69 53 4 iEnd = (i64)iS
19301 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 50 67 rcPg*(i64)nSrcPg
19302 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 sz;.. int rc =
19303 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 36 34 SQLITE_OK;. i64
19304 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65 72 74 iOff;.. assert
19305 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 ( p->bDestLocked
19306 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 );. assert( !i
19307 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72 sFatalError(p->r
19308 63 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 c) );. assert(
19309 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f iSrcPg!=PENDING_
1930a 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 BYTE_PAGE(p->pSr
1930b 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 61 73 73 c->pBt) );. ass
1930c 65 72 74 28 20 7a 53 72 63 44 61 74 61 20 29 3b ert( zSrcData );
1930d 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 74 68 65 .. /* Catch the
1930e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 case where the
1930f 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 destination is a
19310 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
19311 62 61 73 65 20 61 6e 64 20 74 68 65 0a 20 20 2a base and the. *
19312 2a 20 70 61 67 65 20 73 69 7a 65 73 20 6f 66 20 * page sizes of
19313 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64 the source and d
19314 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 66 66 65 estination diffe
19315 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e r. . */. if( n
19316 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 74 50 67 SrcPgsz!=nDestPg
19317 73 7a 20 26 26 20 73 71 6c 69 74 65 33 50 61 67 sz && sqlite3Pag
19318 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69 74 65 erIsMemdb(sqlite
19319 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 3BtreePager(p->p
1931a 44 65 73 74 29 29 20 29 7b 0a 20 20 20 20 72 63 Dest)) ){. rc
1931b 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e = SQLITE_READON
1931c 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 LY;. }.. /* Th
1931d 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 is loop runs onc
1931e 65 20 66 6f 72 20 65 61 63 68 20 64 65 73 74 69 e for each desti
1931f 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 70 61 6e nation page span
19320 6e 65 64 20 62 79 20 74 68 65 20 73 6f 75 72 63 ned by the sourc
19321 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e 20 46 6f e . ** page. Fo
19322 72 20 65 61 63 68 20 69 74 65 72 61 74 69 6f 6e r each iteration
19323 2c 20 76 61 72 69 61 62 6c 65 20 69 4f 66 66 20 , variable iOff
19324 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 79 is set to the by
19325 74 65 20 6f 66 66 73 65 74 0a 20 20 2a 2a 20 6f te offset. ** o
19326 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f f the destinatio
19327 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 66 n page.. */. f
19328 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d 28 69 36 or(iOff=iEnd-(i6
19329 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 63 3d 3d 4)nSrcPgsz; rc==
1932a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 SQLITE_OK && iOf
1932b 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b 3d 6e 44 f<iEnd; iOff+=nD
1932c 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 20 44 62 estPgsz){. Db
1932d 50 61 67 65 20 2a 70 44 65 73 74 50 67 20 3d 20 Page *pDestPg =
1932e 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 44 65 73 0;. Pgno iDes
1932f 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f t = (Pgno)(iOff/
19330 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b 0a 20 20 nDestPgsz)+1;.
19331 20 20 69 66 28 20 69 44 65 73 74 3d 3d 50 45 4e if( iDest==PEN
19332 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
19333 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 20 ->pDest->pBt) )
19334 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 continue;. if
19335 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 ( SQLITE_OK==(rc
19336 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
19337 65 74 28 70 44 65 73 74 50 61 67 65 72 2c 20 69 et(pDestPager, i
19338 44 65 73 74 2c 20 26 70 44 65 73 74 50 67 29 29 Dest, &pDestPg))
19339 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f . && SQLITE_
1933a 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK==(rc = sqlite
1933b 33 50 61 67 65 72 57 72 69 74 65 28 70 44 65 73 3PagerWrite(pDes
1933c 74 50 67 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 tPg)). ){.
1933d 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e const u8 *zIn
1933e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69 4f 66 = &zSrcData[iOf
1933f 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a 20 20 20 f%nSrcPgsz];.
19340 20 20 20 75 38 20 2a 7a 44 65 73 74 44 61 74 61 u8 *zDestData
19341 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
19342 65 74 44 61 74 61 28 70 44 65 73 74 50 67 29 3b etData(pDestPg);
19343 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 75 74 20 . u8 *zOut
19344 3d 20 26 7a 44 65 73 74 44 61 74 61 5b 69 4f 66 = &zDestData[iOf
19345 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b 0a 0a 20 f%nDestPgsz];..
19346 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 /* Copy the
19347 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 73 data from the s
19348 6f 75 72 63 65 20 70 61 67 65 20 69 6e 74 6f 20 ource page into
19349 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
1934a 70 61 67 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54 page.. ** T
1934b 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 42 74 hen clear the Bt
1934c 72 65 65 20 6c 61 79 65 72 20 4d 65 6d 50 61 67 ree layer MemPag
1934d 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 2e 20 42 e.isInit flag. B
1934e 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 6c 65 0a oth this module.
1934f 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 ** and the
19350 20 70 61 67 65 72 20 63 6f 64 65 20 75 73 65 20 pager code use
19351 74 68 69 73 20 74 72 69 63 6b 20 28 63 6c 65 61 this trick (clea
19352 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 62 ring the first b
19353 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 yte. ** of
19354 74 68 65 20 70 61 67 65 20 27 65 78 74 72 61 27 the page 'extra'
19355 20 73 70 61 63 65 20 74 6f 20 69 6e 76 61 6c 69 space to invali
19356 64 61 74 65 20 74 68 65 20 42 74 72 65 65 20 6c date the Btree l
19357 61 79 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 63 ayers. ** c
19358 61 63 68 65 64 20 70 61 72 73 65 20 6f 66 20 74 ached parse of t
19359 68 65 20 70 61 67 65 29 2e 20 4d 65 6d 50 61 67 he page). MemPag
1935a 65 2e 69 73 49 6e 69 74 20 69 73 20 6d 61 72 6b e.isInit is mark
1935b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 22 4d 55 ed . ** "MU
1935c 53 54 20 42 45 20 46 49 52 53 54 22 20 66 6f 72 ST BE FIRST" for
1935d 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e 0a 20 this purpose..
1935e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d 65 */. me
1935f 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20 mcpy(zOut, zIn,
19360 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 28 28 nCopy);. ((
19361 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 u8 *)sqlite3Page
19362 72 47 65 74 45 78 74 72 61 28 70 44 65 73 74 50 rGetExtra(pDestP
19363 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 g))[0] = 0;.
19364 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 }. sqlite3Pag
19365 65 72 55 6e 72 65 66 28 70 44 65 73 74 50 67 29 erUnref(pDestPg)
19366 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 ;. }.. return
19367 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
19368 70 46 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 pFile is current
19369 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 ly larger than i
1936a 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 6e Size bytes, then
1936b 20 74 72 75 6e 63 61 74 65 20 69 74 20 74 6f 0a truncate it to.
1936c 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 69 7a 65 ** exactly iSize
1936d 20 62 79 74 65 73 2e 20 49 66 20 70 46 69 6c 65 bytes. If pFile
1936e 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 72 20 74 is not larger t
1936f 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73 2c han iSize bytes,
19370 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 66 75 then.** this fu
19371 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
19372 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 p..**.** Return
19373 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 SQLITE_OK if eve
19374 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 rything is succe
19375 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c ssful, or an SQL
19376 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f ite error .** co
19377 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f de if an error o
19378 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 ccurs..*/.static
19379 20 69 6e 74 20 62 61 63 6b 75 70 54 72 75 6e 63 int backupTrunc
1937a 61 74 65 46 69 6c 65 28 73 71 6c 69 74 65 33 5f ateFile(sqlite3_
1937b 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 36 34 file *pFile, i64
1937c 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 34 20 69 iSize){. i64 i
1937d 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 74 20 72 Current;. int r
1937e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
1937f 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26 69 43 eSize(pFile, &iC
19380 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 urrent);. if( r
19381 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
19382 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65 20 29 iCurrent>iSize )
19383 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
19384 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 46 69 e3OsTruncate(pFi
19385 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a le, iSize);. }.
19386 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19387 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 /*.** Register t
19388 68 69 73 20 62 61 63 6b 75 70 20 6f 62 6a 65 63 his backup objec
19389 74 20 77 69 74 68 20 74 68 65 20 61 73 73 6f 63 t with the assoc
1938a 69 61 74 65 64 20 73 6f 75 72 63 65 20 70 61 67 iated source pag
1938b 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c 6c 62 61 er for.** callba
1938c 63 6b 73 20 77 68 65 6e 20 70 61 67 65 73 20 61 cks when pages a
1938d 72 65 20 63 68 61 6e 67 65 64 20 6f 72 20 74 68 re changed or th
1938e 65 20 63 61 63 68 65 20 69 6e 76 61 6c 69 64 61 e cache invalida
1938f 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ted..*/.static v
19390 6f 69 64 20 61 74 74 61 63 68 42 61 63 6b 75 70 oid attachBackup
19391 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33 5f 62 Object(sqlite3_b
19392 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c ackup *p){. sql
19393 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 ite3_backup **pp
19394 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
19395 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
19396 65 78 28 70 2d 3e 70 53 72 63 29 20 29 3b 0a 20 ex(p->pSrc) );.
19397 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 pp = sqlite3Pag
19398 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69 erBackupPtr(sqli
19399 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d te3BtreePager(p-
1939a 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d 3e 70 4e >pSrc));. p->pN
1939b 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70 ext = *pp;. *pp
1939c 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 41 74 74 = p;. p->isAtt
1939d 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a ached = 1;.}../*
1939e 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 65 20 70 .** Copy nPage p
1939f 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 6f ages from the so
193a0 75 72 63 65 20 62 2d 74 72 65 65 20 74 6f 20 74 urce b-tree to t
193a1 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a he destination..
193a2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
193a3 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t sqlite3_backup
193a4 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 _step(sqlite3_ba
193a5 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 ckup *p, int nPa
193a6 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a ge){. int rc;..
193a7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
193a8 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d enter(p->pSrcDb-
193a9 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 >mutex);. sqlit
193aa 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e e3BtreeEnter(p->
193ab 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e pSrc);. if( p->
193ac 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
193ad 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
193ae 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d er(p->pDestDb->m
193af 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 utex);. }.. rc
193b0 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 = p->rc;. if(
193b1 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 72 63 !isFatalError(rc
193b2 29 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a ) ){. Pager *
193b3 20 63 6f 6e 73 74 20 70 53 72 63 50 61 67 65 72 const pSrcPager
193b4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
193b5 61 67 65 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 ager(p->pSrc);
193b6 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 /* Source pag
193b7 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 er */. Pager
193b8 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 * const pDestPag
193b9 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 er = sqlite3Btre
193ba 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 ePager(p->pDest)
193bb 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 61 67 65 ; /* Dest page
193bc 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b r */. int ii;
193bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
193bf 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 terator variable
193c0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 */. int nSrc
193c1 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 Page = -1;
193c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
193c3 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 64 62 20 ze of source db
193c4 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 in pages */.
193c5 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 int bCloseTrans
193c6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
193c7 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 72 /* True if sr
193c8 63 20 64 62 20 72 65 71 75 69 72 65 73 20 75 6e c db requires un
193c9 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 locking */..
193ca 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 /* If the source
193cb 20 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e pager is curren
193cc 74 6c 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 tly in a write-t
193cd 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 ransaction, retu
193ce 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 rn. ** SQLITE
193cf 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c _BUSY immediatel
193d0 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 y.. */. if
193d1 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26 26 20 ( p->pDestDb &&
193d2 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e p->pSrc->pBt->in
193d3 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
193d4 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 NS_WRITE ){.
193d5 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
193d6 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 SY;. }else{.
193d7 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
193d8 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 _OK;. }..
193d9 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 /* Lock the dest
193da 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
193db 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6c , if it is not l
193dc 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 2e 20 2a ocked already. *
193dd 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 /. if( SQLITE
193de 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 _OK==rc && p->bD
193df 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 estLocked==0.
193e0 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d && SQLITE_OK==
193e1 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 (rc = sqlite3Btr
193e2 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e eeBeginTrans(p->
193e3 70 44 65 73 74 2c 20 32 29 29 20 0a 20 20 20 20 pDest, 2)) .
193e4 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44 65 73 ){. p->bDes
193e5 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 tLocked = 1;.
193e6 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 sqlite3BtreeG
193e7 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c etMeta(p->pDest,
193e8 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 BTREE_SCHEMA_VE
193e9 52 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74 RSION, &p->iDest
193ea 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a Schema);. }..
193eb 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
193ec 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d is no open read-
193ed 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 transaction on t
193ee 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
193ef 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20 se, open. **
193f0 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 one now. If a tr
193f1 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 ansaction is ope
193f2 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 69 ned here, then i
193f3 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 t will be closed
193f4 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 . ** before t
193f5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 his function exi
193f6 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ts.. */. i
193f7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
193f8 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 && 0==sqlite3Bt
193f9 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 reeIsInReadTrans
193fa 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 (p->pSrc) ){.
193fb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
193fc 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 treeBeginTrans(p
193fd 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 ->pSrc, 0);.
193fe 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 bCloseTrans =
193ff 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 1;. }. .
19400 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 65 72 /* Now that ther
19401 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b e is a read-lock
19402 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 on the source d
19403 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 20 74 atabase, query t
19404 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 he. ** source
19405 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 20 6e pager for the n
19406 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
19407 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
19408 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
19409 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1940a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1940b 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
1940c 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e 53 72 (pSrcPager, &nSr
1940d 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 cPage);. }.
1940e 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e 50 61 for(ii=0; (nPa
1940f 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61 67 65 ge<0 || ii<nPage
19410 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c 3d 28 ) && p->iNext<=(
19411 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 26 26 Pgno)nSrcPage &&
19412 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 !rc; ii++){.
19413 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 const Pgno iS
19414 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b rcPg = p->iNext;
19415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19416 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 20 /* Source page
19417 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 number */.
19418 69 66 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 if( iSrcPg!=PEND
19419 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d ING_BYTE_PAGE(p-
1941a 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b 0a 20 >pSrc->pBt) ){.
1941b 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 DbPage *p
1941c 53 72 63 50 67 3b 20 20 20 20 20 20 20 20 20 20 SrcPg;
1941d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1941e 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 /* Source pag
1941f 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 e object */.
19420 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
19421 50 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67 PagerGet(pSrcPag
19422 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 er, iSrcPg, &pSr
19423 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66 cPg);. if
19424 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
19425 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
19426 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 = backupOnePage(
19427 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c 69 74 p, iSrcPg, sqlit
19428 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
19429 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20 20 20 SrcPg));.
1942a 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
1942b 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20 nref(pSrcPg);.
1942c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1942d 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74 2b 2b p->iNext++
1942e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1942f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
19430 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67 65 63 . p->nPagec
19431 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67 65 3b ount = nSrcPage;
19432 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d 61 69 . p->nRemai
19433 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67 65 2b ning = nSrcPage+
19434 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20 20 20 1-p->iNext;.
19435 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74 3e 28 if( p->iNext>(
19436 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 29 7b Pgno)nSrcPage ){
19437 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
19438 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
19439 20 7d 65 6c 73 65 20 69 66 28 20 21 70 2d 3e 69 }else if( !p->i
1943a 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20 sAttached ){.
1943b 20 20 20 20 20 61 74 74 61 63 68 42 61 63 6b 75 attachBacku
1943c 70 4f 62 6a 65 63 74 28 70 29 3b 0a 20 20 20 20 pObject(p);.
1943d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1943e 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 /* Update the s
1943f 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 66 69 chema version fi
19440 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73 74 69 eld in the desti
19441 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e nation database.
19442 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 This. ** is
19443 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
19444 74 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 t the schema-ver
19445 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64 6f 65 73 sion really does
19446 20 63 68 61 6e 67 65 20 69 6e 0a 20 20 20 20 2a change in. *
19447 2a 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 * the case where
19448 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 the source and
19449 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
1944a 62 61 73 65 73 20 68 61 76 65 20 74 68 65 0a 20 bases have the.
1944b 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68 65 6d ** same schem
1944c 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a a version.. *
1944d 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
1944e 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 20 20 20 LITE_DONE .
1944f 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 && (rc = sqlite3
19450 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 BtreeUpdateMeta(
19451 70 2d 3e 70 44 65 73 74 2c 31 2c 70 2d 3e 69 44 p->pDest,1,p->iD
19452 65 73 74 53 63 68 65 6d 61 2b 31 29 29 3d 3d 53 estSchema+1))==S
19453 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 29 7b 0a QLITE_OK. ){.
19454 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 const int
19455 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d 20 73 nSrcPagesize = s
19456 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
19457 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b geSize(p->pSrc);
19458 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 . const int
19459 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 3d nDestPagesize =
1945a 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
1945b 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 PageSize(p->pDes
1945c 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 44 t);. int nD
1945d 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 20 0a estTruncate;. .
1945e 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 if( p->pDe
1945f 73 74 44 62 20 29 7b 0a 20 20 20 20 20 20 20 20 stDb ){.
19460 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 sqlite3ResetInte
19461 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d 3e 70 44 rnalSchema(p->pD
19462 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 20 20 20 estDb, 0);.
19463 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 }.. /* Set
19464 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 74 nDestTruncate t
19465 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62 o the final numb
19466 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 er of pages in t
19467 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 he destination.
19468 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 ** database
19469 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 . The complicati
1946a 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20 on here is that
1946b 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
1946c 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73 69 page. ** si
1946d 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 ze may be differ
1946e 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75 72 63 ent to the sourc
1946f 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a 20 20 e page size. .
19470 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
19471 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 If the source pa
19472 67 65 20 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c ge size is small
19473 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 74 er than the dest
19474 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 69 7a ination page siz
19475 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 72 6f 75 e, . ** rou
19476 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 73 20 63 nd up. In this c
19477 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 ase the call to
19478 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
19479 65 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 0a 20 e() below will.
1947a 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68 65 20 ** fix the
1947b 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
1947c 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69 73 20 . However it is
1947d 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c important to cal
1947e 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 l. ** sqlit
1947f 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 e3PagerTruncateI
19480 6d 61 67 65 28 29 20 68 65 72 65 20 73 6f 20 74 mage() here so t
19481 68 61 74 20 61 6e 79 20 70 61 67 65 73 20 69 6e hat any pages in
19482 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 the . ** d
19483 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c 65 20 estination file
19484 74 68 61 74 20 6c 69 65 20 62 65 79 6f 6e 64 20 that lie beyond
19485 74 68 65 20 6e 44 65 73 74 54 72 75 6e 63 61 74 the nDestTruncat
19486 65 20 70 61 67 65 20 6d 61 72 6b 20 61 72 65 0a e page mark are.
19487 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c ** journal
19488 6c 65 64 20 62 79 20 50 61 67 65 72 43 6f 6d 6d led by PagerComm
19489 69 74 50 68 61 73 65 4f 6e 65 28 29 20 62 65 66 itPhaseOne() bef
1948a 6f 72 65 20 74 68 65 79 20 61 72 65 20 64 65 73 ore they are des
1948b 74 72 6f 79 65 64 0a 20 20 20 20 20 20 2a 2a 20 troyed. **
1948c 62 79 20 74 68 65 20 66 69 6c 65 20 74 72 75 6e by the file trun
1948d 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f cation.. */
1948e 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50 . if( nSrcP
1948f 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 agesize<nDestPag
19490 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 esize ){.
19491 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 6e 44 65 int ratio = nDe
19492 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 72 63 50 stPagesize/nSrcP
19493 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 20 20 20 agesize;.
19494 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d nDestTruncate =
19495 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74 69 6f (nSrcPage+ratio
19496 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20 -1)/ratio;.
19497 20 20 20 69 66 28 20 6e 44 65 73 74 54 72 75 6e if( nDestTrun
19498 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e 44 49 cate==(int)PENDI
19499 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e NG_BYTE_PAGE(p->
1949a 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b 0a 20 pDest->pBt) ){.
1949b 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 nDestTr
1949c 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 20 20 20 uncate--;.
1949d 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
1949e 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 . nDestTr
1949f 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50 61 67 uncate = nSrcPag
194a0 65 20 2a 20 28 6e 53 72 63 50 61 67 65 73 69 7a e * (nSrcPagesiz
194a1 65 2f 6e 44 65 73 74 50 61 67 65 73 69 7a 65 29 e/nDestPagesize)
194a2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
194a3 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
194a4 63 61 74 65 49 6d 61 67 65 28 70 44 65 73 74 50 cateImage(pDestP
194a5 61 67 65 72 2c 20 6e 44 65 73 74 54 72 75 6e 63 ager, nDestTrunc
194a6 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 ate);.. if(
194a7 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 nSrcPagesize<nD
194a8 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 estPagesize ){.
194a9 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 /* If the
194aa 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73 69 7a source page-siz
194ab 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 e is smaller tha
194ac 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f n the destinatio
194ad 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 20 20 n page-size,.
194ae 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 78 74 72 ** two extr
194af 61 20 74 68 69 6e 67 73 20 6d 61 79 20 6e 65 65 a things may nee
194b0 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a 20 20 20 d to happen:.
194b1 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
194b2 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 73 74 69 ** * The desti
194b3 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64 20 nation may need
194b4 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65 64 2c to be truncated,
194b5 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 0a and. **.
194b6 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 44 ** * D
194b7 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 ata stored on th
194b8 65 20 70 61 67 65 73 20 69 6d 6d 65 64 69 61 74 e pages immediat
194b9 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ely following th
194ba 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 e . **
194bb 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 pending-byte p
194bc 61 67 65 20 69 6e 20 74 68 65 20 73 6f 75 72 63 age in the sourc
194bd 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 6e e database may n
194be 65 65 64 20 74 6f 20 62 65 0a 20 20 20 20 20 20 eed to be.
194bf 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 64 20 ** copied
194c0 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 into the destina
194c1 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 tion database..
194c2 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
194c3 20 20 63 6f 6e 73 74 20 69 36 34 20 69 53 69 7a const i64 iSiz
194c4 65 20 3d 20 28 69 36 34 29 6e 53 72 63 50 61 67 e = (i64)nSrcPag
194c5 65 73 69 7a 65 20 2a 20 28 69 36 34 29 6e 53 72 esize * (i64)nSr
194c6 63 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 73 cPage;. s
194c7 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f qlite3_file * co
194c8 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 nst pFile = sqli
194c9 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 44 65 te3PagerFile(pDe
194ca 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 stPager);..
194cb 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
194cc 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 );. asse
194cd 72 74 28 20 28 69 36 34 29 6e 44 65 73 74 54 72 rt( (i64)nDestTr
194ce 75 6e 63 61 74 65 2a 28 69 36 34 29 6e 44 65 73 uncate*(i64)nDes
194cf 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 69 53 69 tPagesize >= iSi
194d0 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 20 20 20 ze || (.
194d1 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 nDestTrunc
194d2 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e 44 49 ate==(int)(PENDI
194d3 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e NG_BYTE_PAGE(p->
194d4 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29 0a 20 pDest->pBt)-1).
194d5 20 20 20 20 20 20 20 20 20 20 26 26 20 69 53 69 && iSi
194d6 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 ze>=PENDING_BYTE
194d7 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 && iSize<=PENDI
194d8 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 50 61 67 NG_BYTE+nDestPag
194d9 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 29 29 esize. ))
194da 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 53 51 ;. if( SQ
194db 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
194dc 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 qlite3PagerCommi
194dd 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73 74 50 tPhaseOne(pDestP
194de 61 67 65 72 2c 20 30 2c 20 31 29 29 0a 20 20 20 ager, 0, 1)).
194df 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f && SQLITE_
194e0 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 6b 75 70 OK==(rc = backup
194e1 54 72 75 6e 63 61 74 65 46 69 6c 65 28 70 46 69 TruncateFile(pFi
194e2 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 20 20 20 le, iSize)).
194e3 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f && SQLITE_O
194e4 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
194e5 50 61 67 65 72 53 79 6e 63 28 70 44 65 73 74 50 PagerSync(pDestP
194e6 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 29 ager)). )
194e7 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 20 {. i64
194e8 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 20 20 iOff;.
194e9 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 i64 iEnd = MIN(P
194ea 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20 6e 44 ENDING_BYTE + nD
194eb 65 73 74 50 61 67 65 73 69 7a 65 2c 20 69 53 69 estPagesize, iSi
194ec 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 ze);. f
194ed 6f 72 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 or(.
194ee 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54 iOff=PENDING_BYT
194ef 45 2b 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 20 E+nSrcPagesize;
194f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 3d . rc=
194f1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f =SQLITE_OK && iO
194f2 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 20 20 20 ff<iEnd; .
194f3 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e 53 72 63 iOff+=nSrc
194f4 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20 Pagesize.
194f5 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ){.
194f6 20 20 50 67 48 64 72 20 2a 70 53 72 63 50 67 20 PgHdr *pSrcPg
194f7 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
194f8 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 const Pgno iSrc
194f9 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 Pg = (Pgno)((iOf
194fa 66 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 29 2b f/nSrcPagesize)+
194fb 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1);.
194fc 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
194fd 72 47 65 74 28 70 53 72 63 50 61 67 65 72 2c 20 rGet(pSrcPager,
194fe 69 53 72 63 50 67 2c 20 26 70 53 72 63 50 67 29 iSrcPg, &pSrcPg)
194ff 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
19500 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
19501 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
19502 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 73 71 6c u8 *zData = sql
19503 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
19504 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 (pSrcPg);.
19505 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
19506 69 74 65 33 4f 73 57 72 69 74 65 28 70 46 69 6c ite3OsWrite(pFil
19507 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 63 50 61 e, zData, nSrcPa
19508 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 gesize, iOff);.
19509 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1950a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1950b 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 PagerUnref(pSrcP
1950c 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a g);. }.
1950d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1950e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
1950f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
19510 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
19511 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 30 29 DestPager, 0, 0)
19512 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 ;. }. .
19513 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d /* Finish com
19514 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72 61 6e mitting the tran
19515 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 saction to the d
19516 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
19517 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 ase. */. if
19518 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a ( SQLITE_OK==rc.
19519 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 && SQLITE
1951a 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK==(rc = sqlit
1951b 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1951c 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74 29 29 seTwo(p->pDest))
1951d 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 . ){.
1951e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f rc = SQLITE_DO
1951f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 NE;. }.
19520 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 62 }. . /* If b
19521 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 20 74 72 CloseTrans is tr
19522 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 ue, then this fu
19523 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 61 20 nction opened a
19524 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e read transaction
19525 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 . ** on the s
19526 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 ource database.
19527 43 6c 6f 73 65 20 74 68 65 20 72 65 61 64 20 74 Close the read t
19528 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 72 65 2e ransaction here.
19529 20 54 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a There is. **
1952a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 no need to chec
1952b 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c k the return val
1952c 75 65 73 20 6f 66 20 74 68 65 20 62 74 72 65 65 ues of the btree
1952d 20 6d 65 74 68 6f 64 73 20 68 65 72 65 2c 20 61 methods here, a
1952e 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74 s. ** "commit
1952f 74 69 6e 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c ting" a read-onl
19530 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 y transaction ca
19531 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a nnot fail.. *
19532 2f 0a 20 20 20 20 69 66 28 20 62 43 6c 6f 73 65 /. if( bClose
19533 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 54 Trans ){. T
19534 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32 ESTONLY( int rc2
19535 20 29 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e );. TESTON
19536 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73 71 6c LY( rc2 = ) sql
19537 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
19538 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72 63 2c haseOne(p->pSrc,
19539 20 30 29 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 0);. TESTO
1953a 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20 73 71 NLY( rc2 |= ) sq
1953b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1953c 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53 72 63 PhaseTwo(p->pSrc
1953d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1953e 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 rc2==SQLITE_OK
1953f 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 );. }. .
19540 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 7d 0a p->rc = rc;. }.
19541 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 if( p->pDestDb
19542 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
19543 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 mutex_leave(p->p
19544 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a DestDb->mutex);.
19545 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
19546 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29 eeLeave(p->pSrc)
19547 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
19548 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 72 63 44 x_leave(p->pSrcD
19549 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 b->mutex);. ret
1954a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1954b 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 Release all res
1954c 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 ources associate
1954d 64 20 77 69 74 68 20 61 6e 20 73 71 6c 69 74 65 d with an sqlite
1954e 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e 64 6c 65 3_backup* handle
1954f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
19550 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b int sqlite3_back
19551 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 65 up_finish(sqlite
19552 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 3_backup *p){.
19553 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
19554 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 *pp;
19555 20 20 20 20 20 2f 2a 20 50 74 72 20 74 6f 20 68 /* Ptr to h
19556 65 61 64 20 6f 66 20 70 61 67 65 72 73 20 62 61 ead of pagers ba
19557 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a 20 20 73 ckup list */. s
19558 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
19559 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 tex;
1955a 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 /* Mutex to
1955b 70 72 6f 74 65 63 74 20 73 6f 75 72 63 65 20 64 protect source d
1955c 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 atabase */. int
1955d 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1955e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1955f 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
19560 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e turn */.. /* En
19561 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 20 ter the mutexes
19562 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 */. if( p==0 )
19563 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
19564 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
19565 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 x_enter(p->pSrcD
19566 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c b->mutex);. sql
19567 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
19568 2d 3e 70 53 72 63 29 3b 0a 20 20 6d 75 74 65 78 ->pSrc);. mutex
19569 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 = p->pSrcDb->mu
1956a 74 65 78 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 tex;. if( p->pD
1956b 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c estDb ){. sql
1956c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1956d 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 (p->pDestDb->mut
1956e 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 ex);. }.. /* D
1956f 65 74 61 63 68 20 74 68 69 73 20 62 61 63 6b 75 etach this backu
19570 70 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 p from the sourc
19571 65 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 69 66 e pager. */. if
19572 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a ( p->pDestDb ){.
19573 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 p->pSrc->nBa
19574 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 ckup--;. }. if
19575 28 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 ( p->isAttached
19576 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71 6c 69 ){. pp = sqli
19577 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 te3PagerBackupPt
19578 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 r(sqlite3BtreePa
19579 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 ger(p->pSrc));.
1957a 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21 3d 70 while( *pp!=p
1957b 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d 20 26 ){. pp = &
1957c 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 (*pp)->pNext;.
1957d 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 2d }. *pp = p-
1957e 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f >pNext;. }.. /
1957f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 * If a transacti
19580 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f 70 65 6e on is still open
19581 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2c 20 72 on the Btree, r
19582 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 2a 2f 0a oll it back. */.
19583 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f sqlite3BtreeRo
19584 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 73 74 29 llback(p->pDest)
19585 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 ;.. /* Set the
19586 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 74 68 error code of th
19587 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
19588 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a tabase handle. *
19589 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e 72 63 3d /. rc = (p->rc=
1958a 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20 =SQLITE_DONE) ?
1958b 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 2d 3e 72 SQLITE_OK : p->r
1958c 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f c;. sqlite3Erro
1958d 72 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 72 63 r(p->pDestDb, rc
1958e 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 78 69 74 , 0);.. /* Exit
1958f 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 6e 64 the mutexes and
19590 20 66 72 65 65 20 74 68 65 20 62 61 63 6b 75 70 free the backup
19591 20 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 context structu
19592 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e re. */. if( p->
19593 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
19594 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
19595 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d ve(p->pDestDb->m
19596 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c utex);. }. sql
19597 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
19598 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70 ->pSrc);. if( p
19599 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
1959a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
1959b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
1959c 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
1959d 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b x);. return rc;
1959e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1959f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
195a0 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 ages still to be
195a1 20 62 61 63 6b 65 64 20 75 70 20 61 73 20 6f 66 backed up as of
195a2 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
195a3 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 .** call to sqli
195a4 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
195a5 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
195a6 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 int sqlite3_bac
195a7 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 kup_remaining(sq
195a8 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 lite3_backup *p)
195a9 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 {. return p->nR
195aa 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f 2a 0a emaining;.}../*.
195ab 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f ** Return the to
195ac 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 tal number of pa
195ad 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 ges in the sourc
195ae 65 20 64 61 74 61 62 61 73 65 20 61 73 20 6f 66 e database as of
195af 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a 20 72 65 the most .** re
195b0 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c cent call to sql
195b1 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
195b2 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
195b3 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 I int sqlite3_ba
195b4 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 ckup_pagecount(s
195b5 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
195b6 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e ){. return p->n
195b7 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a 0a 2f 2a Pagecount;.}../*
195b8 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
195b9 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 n is called afte
195ba 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f r the contents o
195bb 66 20 70 61 67 65 20 69 50 61 67 65 20 6f 66 20 f page iPage of
195bc 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 20 64 61 the.** source da
195bd 74 61 62 61 73 65 20 68 61 76 65 20 62 65 65 6e tabase have been
195be 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 20 70 61 modified. If pa
195bf 67 65 20 69 50 61 67 65 20 68 61 73 20 61 6c 72 ge iPage has alr
195c0 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20 63 6f eady been .** co
195c1 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 pied into the de
195c2 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
195c3 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 se, then the dat
195c4 61 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 a written to the
195c5 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 .** destination
195c6 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64 61 74 is now invalidat
195c7 65 64 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74 ed. The destinat
195c8 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50 61 67 ion copy of iPag
195c9 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 e needs.** to be
195ca 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 updated with th
195cb 65 20 6e 65 77 20 64 61 74 61 20 62 65 66 6f 72 e new data befor
195cc 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 e the backup ope
195cd 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d ration is.** com
195ce 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 plete..**.** It
195cf 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
195d0 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 the mutex associ
195d1 61 74 65 64 20 77 69 74 68 20 74 68 65 20 42 74 ated with the Bt
195d2 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a Shared object.**
195d3 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
195d4 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 o the source dat
195d5 61 62 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 abase is held wh
195d6 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
195d7 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a is.** called..*
195d8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
195d9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 void sqlite3Bac
195da 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 74 65 kupUpdate(sqlite
195db 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 3_backup *pBacku
195dc 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 p, Pgno iPage, c
195dd 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b onst u8 *aData){
195de 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
195df 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 p *p;
195e0 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
195e1 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a tor variable */.
195e2 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b for(p=pBackup;
195e3 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
195e4 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
195e5 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
195e6 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 p->pSrc->pBt->mu
195e7 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20 tex) );. if(
195e8 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d !isFatalError(p-
195e9 3e 72 63 29 20 26 26 20 69 50 61 67 65 3c 70 2d >rc) && iPage<p-
195ea 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 >iNext ){.
195eb 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 20 70 72 /* The backup pr
195ec 6f 63 65 73 73 20 70 20 68 61 73 20 61 6c 72 65 ocess p has alre
195ed 61 64 79 20 63 6f 70 69 65 64 20 70 61 67 65 20 ady copied page
195ee 69 50 61 67 65 2e 20 42 75 74 20 6e 6f 77 20 69 iPage. But now i
195ef 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 62 t. ** has b
195f0 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20 een modified by
195f1 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e a transaction on
195f2 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
195f3 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a r. Copy. **
195f4 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 6e the new data in
195f5 74 6f 20 74 68 65 20 62 61 63 6b 75 70 2e 0a 20 to the backup..
195f6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e */. in
195f7 74 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 t rc = backupOne
195f8 50 61 67 65 28 70 2c 20 69 50 61 67 65 2c 20 61 Page(p, iPage, a
195f9 44 61 74 61 29 3b 0a 20 20 20 20 20 20 61 73 73 Data);. ass
195fa 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
195fb 42 55 53 59 20 26 26 20 72 63 21 3d 53 51 4c 49 BUSY && rc!=SQLI
195fc 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 TE_LOCKED );.
195fd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
195fe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
195ff 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 p->rc = rc;.
19600 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
19601 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74 20 74 ./*.** Restart t
19602 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 he backup proces
19603 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 s. This is calle
19604 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 d when the pager
19605 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 65 63 74 layer.** detect
19606 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 s that the datab
19607 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 ase has been mod
19608 69 66 69 65 64 20 62 79 20 61 6e 20 65 78 74 65 ified by an exte
19609 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a rnal database.**
1960a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 20 connection. In
1960b 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 this case there
1960c 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b 6e 6f is no way of kno
1960d 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20 74 68 wing which of th
1960e 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 e.** pages that
1960f 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69 65 64 have been copied
19610 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e into the destin
19611 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 61 ation database a
19612 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 76 61 6c re still .** val
19613 69 64 20 61 6e 64 20 77 68 69 63 68 20 61 72 65 id and which are
19614 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 65 6e 74 not, so the ent
19615 69 72 65 20 70 72 6f 63 65 73 73 20 6e 65 65 64 ire process need
19616 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 74 65 s to be restarte
19617 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 d..**.** It is a
19618 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 ssumed that the
19619 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 mutex associated
1961a 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72 with the BtShar
1961b 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 ed object.** cor
1961c 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 responding to th
1961d 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
1961e 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 74 e is held when t
1961f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a his function is.
19620 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 ** called..*/.SQ
19621 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19622 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 d sqlite3BackupR
19623 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 5f 62 estart(sqlite3_b
19624 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 29 7b ackup *pBackup){
19625 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
19626 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 p *p;
19627 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
19628 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a tor variable */.
19629 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b for(p=pBackup;
1962a 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
1962b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
1962c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1962d 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 p->pSrc->pBt->mu
1962e 74 65 78 29 20 29 3b 0a 20 20 20 20 70 2d 3e 69 tex) );. p->i
1962f 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a Next = 1;. }.}.
19630 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19631 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a OMIT_VACUUM./*.*
19632 2a 20 43 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c * Copy the compl
19633 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 ete content of p
19634 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54 BtFrom into pBtT
19635 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 74 69 6f o. A transactio
19636 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 63 74 n.** must be act
19637 69 76 65 20 66 6f 72 20 62 6f 74 68 20 66 69 6c ive for both fil
19638 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 es..**.** The si
19639 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 6f 20 6d ze of file pTo m
1963a 61 79 20 62 65 20 72 65 64 75 63 65 64 20 62 79 ay be reduced by
1963b 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e this operation.
1963c 20 49 66 20 61 6e 79 74 68 69 6e 67 20 0a 2a 2a If anything .**
1963d 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74 68 65 goes wrong, the
1963e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 transaction on
1963f 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 pTo is rolled ba
19640 63 6b 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 ck. If successfu
19641 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 l, the .** trans
19642 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
19643 74 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 ted before retur
19644 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ning..*/.SQLITE_
19645 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19646 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 te3BtreeCopyFile
19647 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 (Btree *pTo, Btr
19648 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e ee *pFrom){. in
19649 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f t rc;. sqlite3_
1964a 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71 6c 69 backup b;. sqli
1964b 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 54 te3BtreeEnter(pT
1964c 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 o);. sqlite3Btr
1964d 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a eeEnter(pFrom);.
1964e 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 61 6e 20 . /* Set up an
1964f 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f sqlite3_backup o
19650 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 33 5f 62 bject. sqlite3_b
19651 61 63 6b 75 70 2e 70 44 65 73 74 44 62 20 6d 75 ackup.pDestDb mu
19652 73 74 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 st be set. ** t
19653 6f 20 30 2e 20 54 68 69 73 20 69 73 20 75 73 65 o 0. This is use
19654 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 d by the impleme
19655 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 ntations of sqli
19656 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
19657 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c 69 74 ). ** and sqlit
19658 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 e3_backup_finish
19659 28 29 20 74 6f 20 64 65 74 65 63 74 20 74 68 61 () to detect tha
1965a 74 20 74 68 65 79 20 61 72 65 20 62 65 69 6e 67 t they are being
1965b 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 66 72 6f called. ** fro
1965c 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c m this function,
1965d 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20 62 79 not directly by
1965e 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a 2f 0a the user.. */.
1965f 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 30 2c 20 memset(&b, 0,
19660 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 20 62 2e sizeof(b));. b.
19661 70 53 72 63 44 62 20 3d 20 70 46 72 6f 6d 2d 3e pSrcDb = pFrom->
19662 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 3d 20 70 db;. b.pSrc = p
19663 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 73 74 20 From;. b.pDest
19664 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e 65 78 74 = pTo;. b.iNext
19665 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 78 37 46 = 1;.. /* 0x7F
19666 46 46 46 46 46 46 20 69 73 20 74 68 65 20 68 61 FFFFFF is the ha
19667 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 rd limit for the
19668 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
19669 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 0a 20 in a database.
1966a 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 70 61 73 ** file. By pas
1966b 73 69 6e 67 20 74 68 69 73 20 61 73 20 74 68 65 sing this as the
1966c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
1966d 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 20 2a 2a to copy to. **
1966e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
1966f 73 74 65 70 28 29 2c 20 77 65 20 63 61 6e 20 67 step(), we can g
19670 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
19671 65 20 63 6f 70 79 20 66 69 6e 69 73 68 65 73 20 e copy finishes
19672 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 . ** within a s
19673 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 6e 6c 65 ingle call (unle
19674 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ss an error occu
19675 72 73 29 2e 20 54 68 65 20 61 73 73 65 72 74 28 rs). The assert(
19676 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a ) statement. **
19677 20 63 68 65 63 6b 73 20 74 68 69 73 20 61 73 73 checks this ass
19678 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d 3e 72 63 umption - (p->rc
19679 29 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 ) should be set
1967a 74 6f 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 to either SQLITE
1967b 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f 72 20 61 _DONE . ** or a
1967c 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20 n error code..
1967d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 */. sqlite3_bac
1967e 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30 78 37 kup_step(&b, 0x7
1967f 46 46 46 46 46 46 46 29 3b 0a 20 20 61 73 73 65 FFFFFFF);. asse
19680 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49 54 45 rt( b.rc!=SQLITE
19681 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 _OK );. rc = sq
19682 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
19683 69 73 68 28 26 62 29 3b 0a 20 20 69 66 28 20 72 ish(&b);. if( r
19684 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
19685 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d 3e 70 61 pTo->pBt->pa
19686 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 30 3b geSizeFixed = 0;
19687 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 . }.. sqlite3B
19688 74 72 65 65 4c 65 61 76 65 28 70 46 72 6f 6d 29 treeLeave(pFrom)
19689 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1968a 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 20 72 65 Leave(pTo);. re
1968b 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
1968c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1968d 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 2a 2a _VACUUM */../***
1968e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1968f 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a of backup.c ****
19690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19692 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
19693 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
19694 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 n file vdbemem.c
19695 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
19696 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19697 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
19698 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a * 2004 May 26.**
19699 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1969a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1969b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1969c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1969d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1969e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1969f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
196a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
196a1 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
196a2 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
196a3 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
196a4 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
196a5 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
196a6 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
196a7 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
196a8 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
196a9 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
196aa 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
196ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
196ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
196af 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
196b0 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 tains code use t
196b1 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 o manipulate "Me
196b2 6d 22 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 m" structure. A
196b3 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 "Mem".** stores
196b4 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 a single value
196b5 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 4d 65 in the VDBE. Me
196b6 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 m is an opaque s
196b7 74 72 75 63 74 75 72 65 20 76 69 73 69 62 6c 65 tructure visible
196b8 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 .** only within
196b9 74 68 65 20 56 44 42 45 2e 20 20 49 6e 74 65 72 the VDBE. Inter
196ba 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 72 65 face routines re
196bb 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 fer to a Mem usi
196bc 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 ng the.** name s
196bd 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2f 0a 0a qlite_value.*/..
196be 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 /*.** Call sqlit
196bf 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
196c0 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 lob() on the sup
196c1 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 plied value (typ
196c2 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 e Mem*).** P if
196c3 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 required..*/.#de
196c4 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 fine expandBlob(
196c5 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 P) (((P)->flags&
196c6 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 MEM_Zero)?sqlite
196c7 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
196c8 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 ob(P):0)../*.**
196c9 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 If pMem is an ob
196ca 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 ject with a vali
196cb 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 d string represe
196cc 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f ntation, this ro
196cd 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 utine.** ensures
196ce 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e the internal en
196cf 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 coding for the s
196d0 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 tring representa
196d1 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 tion is.** 'desi
196d2 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 redEnc', one of
196d3 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
196d4 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 ITE_UTF16LE or S
196d5 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a QLITE_UTF16BE..*
196d6 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 *.** If pMem is
196d7 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a not a string obj
196d8 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f ect, or the enco
196d9 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 ding of the stri
196da 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 ng.** representa
196db 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 tion is already
196dc 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 stored using the
196dd 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 requested encod
196de 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a ing, then this.*
196df 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e * routine is a n
196e0 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 o-op..**.** SQLI
196e1 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
196e2 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 d if the convers
196e3 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 ion is successfu
196e4 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 l (or not requir
196e5 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e ed)..** SQLITE_N
196e6 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 OMEM may be retu
196e7 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 rned if a malloc
196e8 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 () fails during
196e9 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 conversion.** be
196ea 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a tween formats..*
196eb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
196ec 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
196ed 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d ChangeEncoding(M
196ee 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 em *pMem, int de
196ef 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 siredEnc){. int
196f0 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 28 rc;. assert( (
196f1 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
196f2 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
196f3 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 45 assert( desiredE
196f4 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc==SQLITE_UTF8
196f5 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 || desiredEnc==S
196f6 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 QLITE_UTF16LE.
196f7 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 73 69 || desi
196f8 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
196f9 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 TF16BE );. if(
196fa 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 !(pMem->flags&ME
196fb 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e M_Str) || pMem->
196fc 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 enc==desiredEnc
196fd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
196fe 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 LITE_OK;. }. a
196ff 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
19700 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
19701 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
19702 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 b->mutex) );.#if
19703 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19704 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20 53 UTF16. return S
19705 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c QLITE_ERROR;.#el
19706 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e se.. /* MemTran
19707 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74 75 slate() may retu
19708 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 rn SQLITE_OK or
19709 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 SQLITE_NOMEM. If
1970a 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e NOMEM is return
1970b 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 ed,. ** then th
1970c 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 e encoding of th
1970d 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 e value may not
1970e 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 have changed..
1970f 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
19710 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
19711 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 e(pMem, (u8)desi
19712 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 redEnc);. asser
19713 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 t(rc==SQLITE_OK
19714 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 || rc==SQLITE
19715 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 _NOMEM);. asser
19716 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 t(rc==SQLITE_OK
19717 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 || pMem->enc!
19718 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 =desiredEnc);.
19719 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
1971a 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d E_NOMEM || pMem-
1971b 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 >enc==desiredEnc
1971c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1971d 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
1971e 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e Make sure pMem->
1971f 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 z points to a wr
19720 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f itable allocatio
19721 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 0a 2a n of at least .*
19722 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a * n bytes..**.**
19723 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 If the memory c
19724 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 63 6f ell currently co
19725 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 ntains string or
19726 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e blob data.** an
19727 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 d the third argu
19728 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 ment passed to t
19729 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1972a 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 true, the .** cu
1972b 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 rrent content of
1972c 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72 65 the cell is pre
1972d 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69 73 served. Otherwis
1972e 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 e, it may.** be
1972f 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a discarded. .**.
19730 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
19731 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 sets the MEM_Dy
19732 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 n flag and clear
19733 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 s any xDel callb
19734 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 ack..** It also
19735 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d clears MEM_Ephem
19736 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e and MEM_Static.
19737 20 49 66 20 74 68 65 20 70 72 65 73 65 72 76 65 If the preserve
19738 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 flag is .** not
19739 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a set, Mem.n is z
1973a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 eroed..*/.SQLITE
1973b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1973c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1973d 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e Mem *pMem, int n
1973e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 7b , int preserve){
1973f 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e 3d 0a . assert( 1 >=.
19740 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c ((pMem->zMal
19741 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 loc && pMem->zMa
19742 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f lloc==pMem->z) ?
19743 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 1 : 0) +. ((
19744 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
19745 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 _Dyn)&&pMem->xDe
19746 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 l) ? 1 : 0) + .
19747 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 ((pMem->flags
19748 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 &MEM_Ephem) ? 1
19749 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70 4d : 0) + . ((pM
1974a 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 em->flags&MEM_St
1974b 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 atic) ? 1 : 0).
1974c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
1974d 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 Mem->flags&MEM_R
1974e 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 owSet)==0 );..
1974f 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 if( n<32 ) n = 3
19750 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 2;. if( sqlite3
19751 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 DbMallocSize(pMe
19752 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 m->db, pMem->zMa
19753 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 lloc)<n ){. i
19754 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20 70 f( preserve && p
19755 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d Mem->z==pMem->zM
19756 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 alloc ){. p
19757 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a Mem->z = pMem->z
19758 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 Malloc = sqlite3
19759 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
1975a 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e pMem->db, pMem->
1975b 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 z, n);. pre
1975c 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d serve = 0;. }
1975d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
1975e 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e te3DbFree(pMem->
1975f 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f db, pMem->zMallo
19760 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e c);. pMem->
19761 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 zMalloc = sqlite
19762 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 3DbMallocRaw(pMe
19763 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d m->db, n);. }
19764 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65 6d . }.. if( pMem
19765 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 65 20 ->z && preserve
19766 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 && pMem->zMalloc
19767 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 && pMem->z!=pMe
19768 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 m->zMalloc ){.
19769 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a memcpy(pMem->z
1976a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c Malloc, pMem->z,
1976b 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 pMem->n);. }.
1976c 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
1976d 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d &MEM_Dyn && pMem
1976e 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d ->xDel ){. pM
1976f 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a em->xDel((void *
19770 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d )(pMem->z));. }
19771 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d .. pMem->z = pM
19772 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 em->zMalloc;. i
19773 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b f( pMem->z==0 ){
19774 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
19775 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d = MEM_Null;. }
19776 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e else{. pMem->
19777 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 flags &= ~(MEM_E
19778 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 phem|MEM_Static)
19779 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 ;. }. pMem->xD
1977a 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e el = 0;. return
1977b 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 (pMem->z ? SQLI
1977c 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e TE_OK : SQLITE_N
1977d 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 OMEM);.}../*.**
1977e 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e 20 4d Make the given M
1977f 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 em object MEM_Dy
19780 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 n. In other wor
19781 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a ds, make it so.*
19782 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 54 20 * that any TEXT
19783 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 or BLOB content
19784 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d is stored in mem
19785 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
19786 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 m.** malloc().
19787 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 20 In this way, we
19788 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6d 65 know that the me
19789 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 6f 20 mory is safe to
1978a 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 be.** overwritte
1978b 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a n or altered..**
1978c 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
1978d 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
1978e 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 or SQLITE_NOMEM
1978f 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e if malloc fails.
19790 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19791 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19792 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
19793 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 le(Mem *pMem){.
19794 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 74 int f;. assert
19795 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19796 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19797 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
19798 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19799 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ( (pMem->flags&M
1979a 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
1979b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d . expandBlob(pM
1979c 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d em);. f = pMem-
1979d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 66 >flags;. if( (f
1979e 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c &(MEM_Str|MEM_Bl
1979f 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 ob)) && pMem->z!
197a0 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 =pMem->zMalloc )
197a1 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
197a2 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
197a3 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 m, pMem->n + 2,
197a4 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 1) ){. retu
197a5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
197a6 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
197a7 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b >z[pMem->n] = 0;
197a8 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 . pMem->z[pMe
197a9 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 m->n+1] = 0;.
197aa 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 pMem->flags |=
197ab 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 MEM_Term;. }..
197ac 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
197ad 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 K;.}../*.** If t
197ae 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 he given Mem* ha
197af 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 s a zero-filled
197b0 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e tail, turn it in
197b1 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a to an ordinary.*
197b2 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e * blob stored in
197b3 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c dynamically all
197b4 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f ocated space..*/
197b5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
197b6 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 OMIT_INCRBLOB.SQ
197b7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
197b8 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 sqlite3VdbeMemE
197b9 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 xpandBlob(Mem *p
197ba 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d Mem){. if( pMem
197bb 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ->flags & MEM_Ze
197bc 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 ro ){. int nB
197bd 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 yte;. assert(
197be 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pMem->flags&MEM
197bf 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 _Blob );. ass
197c0 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
197c1 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 s&MEM_RowSet)==0
197c2 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
197c3 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 pMem->db==0 || s
197c4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
197c5 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 d(pMem->db->mute
197c6 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 x) );.. /* Se
197c7 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20 6e t nByte to the n
197c8 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
197c9 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 equired to store
197ca 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62 6c the expanded bl
197cb 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 ob. */. nByte
197cc 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 = pMem->n + pMe
197cd 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 m->u.nZero;.
197ce 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a if( nByte<=0 ){.
197cf 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31 3b nByte = 1;
197d0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
197d1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
197d2 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 w(pMem, nByte, 1
197d3 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
197d4 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
197d5 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 }.. memse
197d6 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d t(&pMem->z[pMem-
197d7 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e >n], 0, pMem->u.
197d8 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d nZero);. pMem
197d9 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e ->n += pMem->u.n
197da 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e Zero;. pMem->
197db 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a flags &= ~(MEM_Z
197dc 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 ero|MEM_Term);.
197dd 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
197de 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a TE_OK;.}.#endif.
197df 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
197e0 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 e the given Mem
197e1 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e is \u0000 termin
197e2 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ated..*/.SQLITE_
197e3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
197e4 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 te3VdbeMemNulTer
197e5 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d minate(Mem *pMem
197e6 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ){. assert( pMe
197e7 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
197e8 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
197e9 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
197ea 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e );. if( (pMem->
197eb 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d flags & MEM_Term
197ec 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 )!=0 || (pMem->f
197ed 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d lags & MEM_Str)=
197ee 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
197ef 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a SQLITE_OK; /*
197f0 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a Nothing to do *
197f1 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 /. }. if( sqli
197f2 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
197f3 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 Mem, pMem->n+2,
197f4 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 1) ){. return
197f5 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
197f6 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 }. pMem->z[pMe
197f7 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 m->n] = 0;. pMe
197f8 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 m->z[pMem->n+1]
197f9 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 = 0;. pMem->fla
197fa 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a gs |= MEM_Term;.
197fb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
197fc 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 OK;.}../*.** Add
197fd 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 MEM_Str to the
197fe 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e 74 set of represent
197ff 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 ations for the g
19800 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 iven Mem. Numbe
19801 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 rs.** are conver
19802 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ted using sqlite
19803 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 3_snprintf(). C
19804 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 onverting a BLOB
19805 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 to a string.**
19806 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
19807 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72 65 * Existing repre
19808 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 sentations MEM_I
19809 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 nt and MEM_Real
1980a 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 are *not* invali
1980b 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d dated..**.** A M
1980c 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 EM_Null value wi
1980d 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73 73 ll never be pass
1980e 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 ed to this funct
1980f 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 69 ion. This functi
19810 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f on is.** used fo
19811 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c r converting val
19812 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72 20 ues to text for
19813 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 returning to the
19814 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61 0a user (i.e. via.
19815 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
19816 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 _text()), or for
19817 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 76 ensuring that v
19818 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65 64 alues to be used
19819 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 as btree.** key
1981a 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20 49 s are strings. I
1981b 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61 73 n the former cas
1981c 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 e a NULL pointer
1981d 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 65 is returned the
1981e 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68 65 .** user and the
1981f 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e 74 later is an int
19820 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e ernal programmin
19821 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 g error..*/.SQLI
19822 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19823 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
19824 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d ingify(Mem *pMem
19825 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e , int enc){. in
19826 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
19827 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 ;. int fg = pMe
19828 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 m->flags;. cons
19829 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 t int nByte = 32
1982a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ;.. assert( pMe
1982b 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
1982c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1982d 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
1982e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 );. assert( !(f
1982f 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 g&MEM_Zero) );.
19830 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 4d assert( !(fg&(M
19831 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
19832 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 66 ) );. assert( f
19833 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 g&(MEM_Int|MEM_R
19834 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 eal) );. assert
19835 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ( (pMem->flags&M
19836 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b EM_RowSet)==0 );
19837 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
19838 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
19839 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 28 pMem) );... if(
1983a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
1983b 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c row(pMem, nByte,
1983c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 0) ){. retur
1983d 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
1983e 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 }.. /* For a
1983f 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c Real or Integer,
19840 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 use sqlite3_mpr
19841 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75 63 intf() to produc
19842 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a e the UTF-8. **
19843 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e string represen
19844 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 tation of the va
19845 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 68 lue. Then, if th
19846 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f 64 e required encod
19847 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d ing. ** is UTF-
19848 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 65 16le or UTF-16be
19849 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 6f do a translatio
1984a 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49 n.. ** . ** FI
1984b 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 X ME: It would b
1984c 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c 69 e better if sqli
1984d 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 te3_snprintf() c
1984e 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a ould do UTF-16..
1984f 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 20 */. if( fg &
19850 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 MEM_Int ){. s
19851 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
19852 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 nByte, pMem->z,
19853 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e "%lld", pMem->u.
19854 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 i);. }else{.
19855 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d 45 assert( fg & ME
19856 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 M_Real );. sq
19857 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
19858 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 Byte, pMem->z, "
19859 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 %!.15g", pMem->r
1985a 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e );. }. pMem->n
1985b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
1985c 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 70 30(pMem->z);. p
1985d 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
1985e 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e E_UTF8;. pMem->
1985f 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 flags |= MEM_Str
19860 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 6c |MEM_Term;. sql
19861 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
19862 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 coding(pMem, enc
19863 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
19864 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 }../*.** Memory
19865 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69 cell pMem contai
19866 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f ns the context o
19867 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 f an aggregate f
19868 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 unction..** This
19869 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 routine calls t
1986a 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68 he finalize meth
1986b 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e 63 od for that func
1986c 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 65 tion. The.** re
1986d 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 72 sult of the aggr
1986e 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 20 egate is stored
1986f 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0a back into pMem..
19870 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
19871 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 65 ITE_ERROR if the
19872 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f 72 finalizer repor
19873 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 ts an error. SQ
19874 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 72 LITE_OK.** other
19875 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f wise..*/.SQLITE_
19876 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19877 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 te3VdbeMemFinali
19878 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 75 ze(Mem *pMem, Fu
19879 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a 20 ncDef *pFunc){.
1987a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1987b 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 _OK;. if( ALWAY
1987c 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e 63 S(pFunc && pFunc
1987d 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b 0a ->xFinalize) ){.
1987e 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 sqlite3_cont
1987f 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73 73 ext ctx;. ass
19880 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 ert( (pMem->flag
19881 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 s & MEM_Null)!=0
19882 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d || pFunc==pMem-
19883 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 61 >u.pDef );. a
19884 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
19885 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
19886 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
19887 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 b->mutex) );.
19888 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30 2c memset(&ctx, 0,
19889 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 sizeof(ctx));.
1988a 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d ctx.s.flags =
1988b 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 MEM_Null;. c
1988c 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e tx.s.db = pMem->
1988d 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d db;. ctx.pMem
1988e 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 = pMem;. ctx
1988f 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a .pFunc = pFunc;.
19890 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 pFunc->xFina
19891 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 20 lize(&ctx);.
19892 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d assert( 0==(pMem
19893 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 ->flags&MEM_Dyn)
19894 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 && !pMem->xDel
19895 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
19896 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 Free(pMem->db, p
19897 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 Mem->zMalloc);.
19898 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 memcpy(pMem,
19899 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 &ctx.s, sizeof(c
1989a 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d tx.s));. rc =
1989b 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 ctx.isError;.
1989c 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1989d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d ../*.** If the m
1989e 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 emory cell conta
1989f 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c ins a string val
198a0 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 ue that must be
198a1 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f freed by.** invo
198a2 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61 6c king an external
198a3 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 callback, free
198a4 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 it now. Calling
198a5 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a this function.**
198a6 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 61 does not free a
198a7 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 ny Mem.zMalloc b
198a8 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 uffer..*/.SQLITE
198a9 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
198aa 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
198ab 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 aseExternal(Mem
198ac 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
198ad 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
198ae 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
198af 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
198b0 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c testcase( p->fl
198b1 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 29 3b ags & MEM_Agg );
198b2 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e . testcase( p->
198b3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 flags & MEM_Dyn
198b4 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
198b5 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
198b6 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 63 61 wSet );. testca
198b7 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d se( p->flags & M
198b8 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 69 66 EM_Frame );. if
198b9 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f ( p->flags&(MEM_
198ba 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f Agg|MEM_Dyn|MEM_
198bb 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 6d 65 RowSet|MEM_Frame
198bc 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ) ){. if( p->
198bd 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b flags&MEM_Agg ){
198be 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
198bf 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c beMemFinalize(p,
198c0 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 p->u.pDef);.
198c1 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 assert( (p->f
198c2 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d lags & MEM_Agg)=
198c3 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 =0 );. sqli
198c4 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
198c5 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 e(p);. }else
198c6 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d if( p->flags&MEM
198c7 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 _Dyn && p->xDel
198c8 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
198c9 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 (p->flags&MEM_R
198ca 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 owSet)==0 );.
198cb 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 p->xDel((void
198cc 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 *)p->z);.
198cd 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 p->xDel = 0;.
198ce 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c }else if( p->fl
198cf 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 ags&MEM_RowSet )
198d0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 {. sqlite3R
198d1 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e owSetClear(p->u.
198d2 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 65 pRowSet);. }e
198d3 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 lse if( p->flags
198d4 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 20 &MEM_Frame ){.
198d5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
198d6 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 20 emSetNull(p);.
198d7 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
198d8 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d Release any mem
198d9 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 ory held by the
198da 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 Mem. This may le
198db 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 ave the Mem in a
198dc 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e n.** inconsisten
198dd 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78 61 t state, for exa
198de 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a mple with (Mem.z
198df 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d ==0) and.** (Mem
198e0 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 .type==SQLITE_TE
198e1 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 XT)..*/.SQLITE_P
198e2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
198e3 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
198e4 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c e(Mem *p){. sql
198e5 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
198e6 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 seExternal(p);.
198e7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
198e8 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 ->db, p->zMalloc
198e9 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 );. p->z = 0;.
198ea 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b p->zMalloc = 0;
198eb 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a . p->xDel = 0;.
198ec 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
198ed 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 20 64 a 64-bit IEEE d
198ee 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d ouble into a 64-
198ef 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
198f0 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f er..** If the do
198f1 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 uble is too larg
198f2 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 30 30 e, return 0x8000
198f3 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 000000000000..**
198f4 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 .** Most systems
198f5 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 74 68 appear to do th
198f6 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 73 73 is simply by ass
198f7 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 igning.** variab
198f8 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 74 20 les and without
198f9 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 65 20 the extra range
198fa 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 tests. But.** t
198fb 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 74 73 here are reports
198fc 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 74 68 that windows th
198fd 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 69 6f rows an expectio
198fe 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 n.** if the floa
198ff 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
19900 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
19901 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 . (See ticket #2
19902 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 880.).** Because
19903 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c we do not compl
19904 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 etely understand
19905 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 the problem, we
19906 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 will.** take th
19907 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61 e conservative a
19908 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 pproach and alwa
19909 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 73 74 ys do range test
1990a 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 65 s.** before atte
1990b 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 mpting the conve
1990c 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 rsion..*/.static
1990d 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 i64 doubleToInt
1990e 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 64(double r){.
1990f 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d /*. ** Many com
19910 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 6e pilers we encoun
19911 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e ter do not defin
19912 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 e constants for
19913 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d the. ** minimum
19914 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d and maximum 64-
19915 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 bit integers, or
19916 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 68 65 they define the
19917 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 m. ** inconsist
19918 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 ently. And many
19919 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 do not understa
1991a 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 nd the "LL" nota
1991b 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 tion.. ** So we
1991c 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 define our own
1991d 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 static constants
1991e 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 here using noth
1991f 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 ing. ** larger
19920 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 69 6e than a 32-bit in
19921 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a teger constant..
19922 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f */. static co
19923 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 20 3d nst i64 maxInt =
19924 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a LARGEST_INT64;.
19925 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 static const i
19926 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 64 minInt = SMAL
19927 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 LEST_INT64;.. i
19928 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e f( r<(double)min
19929 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Int ){. retur
1992a 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 n minInt;. }els
1992b 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 e if( r>(double)
1992c 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a maxInt ){. /*
1992d 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 minInt is corre
1992e 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 ct here - not ma
1992f 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 20 xInt. It turns
19930 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e 69 out that assigni
19931 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 72 79 ng. ** a very
19932 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65 20 large positive
19933 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 number to an int
19934 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e 20 eger results in
19935 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 20 20 a very large.
19936 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 6e 74 ** negative int
19937 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 eger. This make
19938 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 s no sense, but
19939 69 74 20 69 73 20 77 68 61 74 20 78 38 36 20 68 it is what x86 h
1993a 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 ardware. ** d
1993b 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 oes so for compa
1993c 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 6c 6c tibility we will
1993d 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 6e 20 do the same in
1993e 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 software. */.
1993f 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a return minInt;.
19940 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
19941 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a urn (i64)r;. }.
19942 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
19943 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74 some kind of int
19944 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 63 68 eger value which
19945 20 69 73 20 74 68 65 20 62 65 73 74 20 77 65 20 is the best we
19946 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70 can do.** at rep
19947 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76 61 resenting the va
19948 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20 64 lue that *pMem d
19949 65 73 63 72 69 62 65 73 20 61 73 20 61 6e 20 69 escribes as an i
1994a 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d nteger..** If pM
1994b 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 em is an integer
1994c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
1994d 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 20 70 is exact. If p
1994e 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 Mem is.** a floa
1994f 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20 ting-point then
19950 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
19951 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 67 65 ed is the intege
19952 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d r part..** If pM
19953 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f em is a string o
19954 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 r blob, then we
19955 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74 20 make an attempt
19956 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 to convert.** it
19957 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72 20 into a integer
19958 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 2e and return that.
19959 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 If pMem repres
1995a 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 51 ents an.** an SQ
1995b 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 72 65 L-NULL value, re
1995c 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 turn 0..**.** If
1995d 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 73 pMem represents
1995e 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c a string value,
1995f 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 6d 69 its encoding mi
19960 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 2e 0a ght be changed..
19961 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19962 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 E i64 sqlite3Vdb
19963 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70 eIntValue(Mem *p
19964 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 Mem){. int flag
19965 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 s;. assert( pMe
19966 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
19967 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
19968 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
19969 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
1996a 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
1996b 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 6c 61 T(pMem) );. fla
1996c 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 gs = pMem->flags
1996d 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
1996e 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 MEM_Int ){. r
1996f 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b eturn pMem->u.i;
19970 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 . }else if( fla
19971 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b gs & MEM_Real ){
19972 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f 75 62 . return doub
19973 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e leToInt64(pMem->
19974 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 r);. }else if(
19975 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 flags & (MEM_Str
19976 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 |MEM_Blob) ){.
19977 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 i64 value;.
19978 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 pMem->flags |=
19979 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 MEM_Str;. if(
1997a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1997b 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c geEncoding(pMem,
1997c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 SQLITE_UTF8).
1997d 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 || sqlite3V
1997e 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
1997f 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 te(pMem) ){.
19980 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
19981 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d }. assert( pM
19982 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c em->z );. sql
19983 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d ite3Atoi64(pMem-
19984 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 >z, &value);.
19985 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 return value;.
19986 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
19987 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a rn 0;. }.}../*.
19988 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 65 ** Return the be
19989 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f st representatio
1998a 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 77 n of pMem that w
1998b 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 e can get into a
1998c 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 .** double. If
1998d 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79 20 pMem is already
1998e 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 a double or an i
1998f 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 69 nteger, return i
19990 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 ts.** value. If
19991 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67 20 it is a string
19992 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 or blob, try to
19993 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 convert it to a
19994 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 double..** If it
19995 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 is a NULL, retu
19996 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 rn 0.0..*/.SQLIT
19997 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c 65 E_PRIVATE double
19998 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c sqlite3VdbeReal
19999 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 Value(Mem *pMem)
1999a 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d {. assert( pMem
1999b 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
1999c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
1999d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
1999e 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 ;. assert( EIGH
1999f 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
199a0 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 (pMem) );. if(
199a1 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pMem->flags & ME
199a2 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 M_Real ){. re
199a3 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 turn pMem->r;.
199a4 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e }else if( pMem->
199a5 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
199a6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 64 ){. return (d
199a7 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b ouble)pMem->u.i;
199a8 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 . }else if( pMe
199a9 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f m->flags & (MEM_
199aa 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
199ab 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 . /* (double)
199ac 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 0 In case of SQL
199ad 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
199ae 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 G_POINT... */.
199af 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28 double val = (
199b0 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 70 4d double)0;. pM
199b1 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d em->flags |= MEM
199b2 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71 _Str;. if( sq
199b3 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
199b4 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 ncoding(pMem, SQ
199b5 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20 LITE_UTF8).
199b6 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 || sqlite3Vdbe
199b7 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
199b8 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 2f pMem) ){. /
199b9 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 * (double)0 In c
199ba 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
199bb 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
199bc 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 T... */. re
199bd 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a turn (double)0;.
199be 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
199bf 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 ( pMem->z );.
199c0 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 sqlite3AtoF(pMe
199c1 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 m->z, &val);.
199c2 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d return val;. }
199c3 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 64 6f else{. /* (do
199c4 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f uble)0 In case o
199c5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
199c6 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 OATING_POINT...
199c7 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 28 64 */. return (d
199c8 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a 0a ouble)0;. }.}..
199c9 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 /*.** The MEM st
199ca 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 61 ructure is alrea
199cb 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 dy a MEM_Real.
199cc 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 Try to also make
199cd 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 it a.** MEM_Int
199ce 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 if we can..*/.S
199cf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
199d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e id sqlite3VdbeIn
199d1 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 tegerAffinity(Me
199d2 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 m *pMem){. asse
199d3 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 rt( pMem->flags
199d4 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 & MEM_Real );.
199d5 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
199d6 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
199d7 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 t)==0 );. asser
199d8 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
199d9 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
199da 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
199db 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
199dc 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
199dd 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b IGNMENT(pMem) );
199de 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 .. pMem->u.i =
199df 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d doubleToInt64(pM
199e0 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f 6e em->r);.. /* On
199e1 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 6c 75 ly mark the valu
199e2 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 e as an integer
199e3 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 if. **. **
199e4 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d 74 72 (1) the round-tr
199e5 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 ip conversion re
199e6 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 73 al->int->real is
199e7 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 20 a no-op, and.
199e8 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 69 6e ** (2) The in
199e9 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 65 72 teger is neither
199ea 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e 6f 72 the largest nor
199eb 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a 20 20 the smallest.
199ec 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 69 62 ** possib
199ed 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 63 6b le integer (tick
199ee 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a 0a 20 et #3922). **.
199ef 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 ** The second a
199f0 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 20 69 nd third terms i
199f1 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
199f2 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e 66 6f conditional enfo
199f3 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 73 65 rces. ** the se
199f4 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 20 75 cond condition u
199f5 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70 74 nder the assumpt
199f6 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 69 6f ion that additio
199f7 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 73 65 n overflow cause
199f8 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f s. ** values to
199f9 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 20 4f wrap around. O
199fa 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 2c 20 n x86 hardware,
199fb 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 69 the third term i
199fc 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 74 72 s always. ** tr
199fd 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 65 20 ue and could be
199fe 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 77 65 omitted. But we
199ff 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 65 63 leave it in bec
19a00 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 ause other. **
19a01 61 72 63 68 69 74 65 63 74 75 72 65 73 20 6d 69 architectures mi
19a02 67 68 74 20 62 65 68 61 76 65 20 64 69 66 66 65 ght behave diffe
19a03 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 rently.. */. i
19a04 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75 f( pMem->r==(dou
19a05 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26 ble)pMem->u.i &&
19a06 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c pMem->u.i>SMALL
19a07 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 20 20 EST_INT64.
19a08 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e && ALWAYS(pMem->
19a09 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 u.i<LARGEST_INT6
19a0a 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 4) ){. pMem->
19a0b 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 flags |= MEM_Int
19a0c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
19a0d 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 onvert pMem to t
19a0e 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 6e ype integer. In
19a0f 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 validate any pri
19a10 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f or representatio
19a11 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ns..*/.SQLITE_PR
19a12 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19a13 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 3VdbeMemIntegeri
19a14 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 fy(Mem *pMem){.
19a15 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
19a16 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19a17 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
19a18 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
19a19 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
19a1a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19a1b 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 et)==0 );. asse
19a1c 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
19a1d 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
19a1e 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d ;.. pMem->u.i =
19a1f 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 sqlite3VdbeIntV
19a20 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 alue(pMem);. Me
19a21 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 mSetTypeFlag(pMe
19a22 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 m, MEM_Int);. r
19a23 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
19a24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
19a25 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 t pMem so that i
19a26 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 4d t is of type MEM
19a27 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 _Real..** Invali
19a28 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 date any prior r
19a29 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a epresentations..
19a2a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19a2b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19a2c 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 eMemRealify(Mem
19a2d 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 *pMem){. assert
19a2e 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19a2f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19a30 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
19a31 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19a32 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
19a33 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a GNMENT(pMem) );.
19a34 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c . pMem->r = sql
19a35 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
19a36 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 e(pMem);. MemSe
19a37 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 tTypeFlag(pMem,
19a38 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 MEM_Real);. ret
19a39 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
19a3a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
19a3b 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 pMem so that it
19a3c 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 has types MEM_Re
19a3d 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 al or MEM_Int or
19a3e 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 both..** Invali
19a3f 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72 date any prior r
19a40 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a epresentations..
19a41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19a42 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19a43 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d eMemNumerify(Mem
19a44 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c *pMem){. doubl
19a45 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 e r1, r2;. i64
19a46 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d i;. assert( (pM
19a47 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d em->flags & (MEM
19a48 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 _Int|MEM_Real|ME
19a49 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 M_Null))==0 );.
19a4a 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
19a4b 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f flags & (MEM_Blo
19a4c 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 b|MEM_Str))!=0 )
19a4d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
19a4e 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
19a4f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
19a50 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
19a51 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 ;. r1 = sqlite3
19a52 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d VdbeRealValue(pM
19a53 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c em);. i = doubl
19a54 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 eToInt64(r1);.
19a55 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a r2 = (double)i;.
19a56 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a if( r1==r2 ){.
19a57 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19a58 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65 emIntegerify(pMe
19a59 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 m);. }else{.
19a5a 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 pMem->r = r1;.
19a5b 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
19a5c 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c g(pMem, MEM_Real
19a5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
19a5e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
19a5f 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 .** Delete any p
19a60 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e revious value an
19a61 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 d set the value
19a62 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 stored in *pMem
19a63 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 to NULL..*/.SQLI
19a64 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19a65 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19a66 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 tNull(Mem *pMem)
19a67 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c {. if( pMem->fl
19a68 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 ags & MEM_Frame
19a69 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
19a6a 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 70 4d beFrameDelete(pM
19a6b 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b 0a 20 em->u.pFrame);.
19a6c 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 }. if( pMem->f
19a6d 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19a6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
19a6f 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d RowSetClear(pMem
19a70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 ->u.pRowSet);.
19a71 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c }. MemSetTypeFl
19a72 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c ag(pMem, MEM_Nul
19a73 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 l);. pMem->type
19a74 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a = SQLITE_NULL;.
19a75 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
19a76 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c any previous val
19a77 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 ue and set the v
19a78 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f alue to be a BLO
19a79 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e B of length.** n
19a7a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 containing all
19a7b 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 zeros..*/.SQLITE
19a7c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19a7d 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a lite3VdbeMemSetZ
19a7e 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 eroBlob(Mem *pMe
19a7f 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c m, int n){. sql
19a80 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19a81 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d se(pMem);. pMem
19a82 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c ->flags = MEM_Bl
19a83 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 ob|MEM_Zero;. p
19a84 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
19a85 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d TE_BLOB;. pMem-
19a86 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c >n = 0;. if( n<
19a87 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 0 ) n = 0;. pMe
19a88 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a m->u.nZero = n;.
19a89 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 pMem->enc = SQ
19a8a 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 66 64 LITE_UTF8;..#ifd
19a8b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
19a8c 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 74 65 NCRBLOB. sqlite
19a8d 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
19a8e 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 m, n, 0);. if(
19a8f 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70 pMem->z ){. p
19a90 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20 Mem->n = n;.
19a91 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 memset(pMem->z,
19a92 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 0, n);. }.#endi
19a93 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 f.}../*.** Delet
19a94 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 e any previous v
19a95 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 alue and set the
19a96 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
19a97 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a *pMem to val,.*
19a98 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 * manifest type
19a99 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 INTEGER..*/.SQLI
19a9a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19a9b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19a9c 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d tInt64(Mem *pMem
19a9d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73 71 , i64 val){. sq
19a9e 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19a9f 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 ase(pMem);. pMe
19aa0 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20 m->u.i = val;.
19aa1 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
19aa2 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74 M_Int;. pMem->t
19aa3 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
19aa4 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 EGER;.}../*.** D
19aa5 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f elete any previo
19aa6 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 us value and set
19aa7 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 the value store
19aa8 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 d in *pMem to va
19aa9 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 l,.** manifest t
19aaa 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c ype REAL..*/.SQL
19aab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19aac 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19aad 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d etDouble(Mem *pM
19aae 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b em, double val){
19aaf 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 . if( sqlite3Is
19ab0 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 20 20 NaN(val) ){.
19ab1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19ab2 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 7d tNull(pMem);. }
19ab3 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
19ab4 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
19ab5 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d pMem);. pMem-
19ab6 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70 4d >r = val;. pM
19ab7 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
19ab8 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e Real;. pMem->
19ab9 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c type = SQLITE_FL
19aba 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a OAT;. }.}../*.*
19abb 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 * Delete any pre
19abc 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 vious value and
19abd 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 set the value of
19abe 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a 2a pMem to be an.*
19abf 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20 * empty boolean
19ac0 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 index..*/.SQLITE
19ac1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19ac2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 lite3VdbeMemSetR
19ac3 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d 29 owSet(Mem *pMem)
19ac4 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
19ac5 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 73 = pMem->db;. as
19ac6 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 sert( db!=0 );.
19ac7 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
19ac8 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19ac9 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 et)==0 );. sqli
19aca 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19acb 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d e(pMem);. pMem-
19acc 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 >zMalloc = sqlit
19acd 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
19ace 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62 2d , 64);. if( db-
19acf 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
19ad0 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
19ad1 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d = MEM_Null;. }
19ad2 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
19ad3 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 ( pMem->zMalloc
19ad4 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 );. pMem->u.p
19ad5 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65 33 RowSet = sqlite3
19ad6 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20 70 RowSetInit(db, p
19ad7 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 Mem->zMalloc, .
19ad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19ada 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 4d sqlite3DbM
19adb 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d allocSize(db, pM
19adc 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 em->zMalloc));.
19add 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
19ade 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29 3b >u.pRowSet!=0 );
19adf 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
19ae0 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 = MEM_RowSet;.
19ae1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
19ae2 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 4d rn true if the M
19ae3 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 em object contai
19ae4 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f ns a TEXT or BLO
19ae5 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f B that is.** too
19ae6 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20 73 large - whose s
19ae7 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c 49 ize exceeds SQLI
19ae8 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a TE_MAX_LENGTH..*
19ae9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19aea 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19aeb 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 MemTooBig(Mem *p
19aec 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
19aed 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 db!=0 );. if( p
19aee 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 ->flags & (MEM_S
19aef 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a tr|MEM_Blob) ){.
19af0 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e int n = p->n
19af1 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 ;. if( p->fla
19af2 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b gs & MEM_Zero ){
19af3 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 . n += p->u
19af4 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 .nZero;. }.
19af5 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 return n>p->db
19af6 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
19af7 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 LIMIT_LENGTH];.
19af8 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 0a }. return 0; .
19af9 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 }../*.** Size of
19afa 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20 struct Mem not
19afb 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d 65 including the Me
19afc 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 m.zMalloc member
19afd 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d ..*/.#define MEM
19afe 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f 74 CELLSIZE (size_t
19aff 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d 3e )(&(((Mem *)0)->
19b00 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a zMalloc))../*.**
19b01 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c 6f 77 Make an shallow
19b02 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 copy of pFrom i
19b03 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 nto pTo. Prior
19b04 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 contents of.** p
19b05 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 20 54 To are freed. T
19b06 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c he pFrom->z fiel
19b07 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 63 61 d is not duplica
19b08 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 72 6f ted. If.** pFro
19b09 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 74 68 m->z is used, th
19b0a 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 en pTo->z points
19b0b 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 68 69 to the same thi
19b0c 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a ng as pFrom->z.*
19b0d 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 74 73 * and flags gets
19b0e 20 73 72 63 54 79 70 65 20 28 65 69 74 68 65 72 srcType (either
19b0f 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 4d 45 MEM_Ephem or ME
19b10 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 M_Static)..*/.SQ
19b11 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19b12 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
19b13 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 ShallowCopy(Mem
19b14 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 *pTo, const Mem
19b15 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 63 54 *pFrom, int srcT
19b16 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ype){. assert(
19b17 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 (pFrom->flags &
19b18 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
19b19 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
19b1a 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
19b1b 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 l(pTo);. memcpy
19b1c 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d (pTo, pFrom, MEM
19b1d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f CELLSIZE);. pTo
19b1e 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 ->xDel = 0;. if
19b1f 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 ( (pFrom->flags&
19b20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 MEM_Dyn)!=0 || p
19b21 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e From->z==pFrom->
19b22 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 zMalloc ){. p
19b23 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d To->flags &= ~(M
19b24 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 EM_Dyn|MEM_Stati
19b25 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 c|MEM_Ephem);.
19b26 20 20 61 73 73 65 72 74 28 20 73 72 63 54 79 70 assert( srcTyp
19b27 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 e==MEM_Ephem ||
19b28 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 srcType==MEM_Sta
19b29 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e tic );. pTo->
19b2a 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 70 65 flags |= srcType
19b2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d ;. }.}../*.** M
19b2c 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 ake a full copy
19b2d 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 of pFrom into pT
19b2e 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e o. Prior conten
19b2f 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a ts of pTo are.**
19b30 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 74 68 freed before th
19b31 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a e copy is made..
19b32 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19b33 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19b34 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 eMemCopy(Mem *pT
19b35 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 o, const Mem *pF
19b36 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d rom){. int rc =
19b37 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 SQLITE_OK;.. a
19b38 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 ssert( (pFrom->f
19b39 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19b3a 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 t)==0 );. sqlit
19b3b 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19b3c 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 External(pTo);.
19b3d 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 memcpy(pTo, pFr
19b3e 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 om, MEMCELLSIZE)
19b3f 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 ;. pTo->flags &
19b40 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 = ~MEM_Dyn;.. i
19b41 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d f( pTo->flags&(M
19b42 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 EM_Str|MEM_Blob)
19b43 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 ){. if( 0==(
19b44 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d pFrom->flags&MEM
19b45 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 20 _Static) ){.
19b46 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 pTo->flags |=
19b47 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 MEM_Ephem;.
19b48 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
19b49 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c eMemMakeWriteabl
19b4a 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 e(pTo);. }.
19b4b 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
19b4c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 }../*.** Transfe
19b4d 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f r the contents o
19b4e 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 f pFrom to pTo.
19b4f 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 6c Any existing val
19b50 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 ue in pTo is.**
19b51 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 freed. If pFrom
19b52 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 72 contains ephemer
19b53 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 20 al data, a copy
19b54 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 is made..**.** p
19b55 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e From contains an
19b56 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 SQL NULL when t
19b57 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
19b58 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rns..*/.SQLITE_P
19b59 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
19b5a 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d te3VdbeMemMove(M
19b5b 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 em *pTo, Mem *pF
19b5c 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 rom){. assert(
19b5d 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pFrom->db==0 ||
19b5e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
19b5f 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 ld(pFrom->db->mu
19b60 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19b61 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 ( pTo->db==0 ||
19b62 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
19b63 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 ld(pTo->db->mute
19b64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
19b65 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pFrom->db==0 ||
19b66 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 pTo->db==0 || pF
19b67 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 rom->db==pTo->db
19b68 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 );.. sqlite3Vd
19b69 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 6f beMemRelease(pTo
19b6a 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c );. memcpy(pTo,
19b6b 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d pFrom, sizeof(M
19b6c 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 em));. pFrom->f
19b6d 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
19b6e 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d . pFrom->xDel =
19b6f 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 0;. pFrom->zMa
19b70 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a lloc = 0;.}../*.
19b71 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 ** Change the va
19b72 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 lue of a Mem to
19b73 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 61 be a string or a
19b74 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 BLOB..**.** The
19b75 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 memory manageme
19b76 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70 65 nt strategy depe
19b77 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 nds on the value
19b78 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 of the xDel.**
19b79 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 68 parameter. If th
19b7a 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 69 e value passed i
19b7b 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 s SQLITE_TRANSIE
19b7c 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a NT, then the .**
19b7d 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 65 string is copie
19b7e 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 d into a (possib
19b7f 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 66 ly existing) buf
19b80 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 74 fer managed by t
19b81 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 he .** Mem struc
19b82 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c ture. Otherwise,
19b83 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 75 any existing bu
19b84 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61 6e ffer is freed an
19b85 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 d the.** pointer
19b86 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 copied..**.** I
19b87 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 f the string is
19b88 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 74 too large (if it
19b89 20 65 78 63 65 65 64 73 20 74 68 65 20 53 51 4c exceeds the SQL
19b8a 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
19b8b 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 .** size limit)
19b8c 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 then no memory a
19b8d 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 llocation occurs
19b8e 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 . If the string
19b8f 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 can be.** store
19b90 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 d without alloca
19b91 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 ting memory, the
19b92 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20 6d n it is. If a m
19b93 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
19b94 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 .** is required
19b95 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74 72 to store the str
19b96 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 ing, then value
19b97 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 of pMem is uncha
19b98 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 nged. In.** eit
19b99 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54 45 her case, SQLITE
19b9a 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75 72 _TOOBIG is retur
19b9b 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
19b9c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19b9d 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
19b9e 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 . Mem *pMem,
19b9f 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
19ba0 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 cell to set to
19ba1 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a string value */.
19ba2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c const char *z,
19ba3 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 /* String
19ba4 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 pointer */. int
19ba5 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
19ba6 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 /* Bytes in str
19ba7 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 ing, or negative
19ba8 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 */. u8 enc,
19ba9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 /* Enc
19baa 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 oding of z. 0 f
19bab 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f or BLOBs */. vo
19bac 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
19bad 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 ) /* Destructor
19bae 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 function */.){.
19baf 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 int nByte = n;
19bb0 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 /* New valu
19bb1 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f e for pMem->n */
19bb2 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 . int iLimit;
19bb3 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
19bb4 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 m allowed string
19bb5 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f or blob size */
19bb6 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 . u16 flags = 0
19bb7 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 ; /* New va
19bb8 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c lue for pMem->fl
19bb9 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ags */.. assert
19bba 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19bbb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19bbc 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
19bbd 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19bbe 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
19bbf 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
19bc0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 );.. /* If z is
19bc1 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
19bc2 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e set pMem to con
19bc3 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c tain an SQL NULL
19bc4 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b . */. if( !z ){
19bc5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19bc6 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 MemSetNull(pMem)
19bc7 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
19bc8 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 ITE_OK;. }.. i
19bc9 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 f( pMem->db ){.
19bca 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d iLimit = pMem
19bcb 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ->db->aLimit[SQL
19bcc 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
19bcd 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
19bce 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f iLimit = SQLITE_
19bcf 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a MAX_LENGTH;. }.
19bd0 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d flags = (enc==
19bd1 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 0?MEM_Blob:MEM_S
19bd2 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 tr);. if( nByte
19bd3 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 <0 ){. assert
19bd4 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 ( enc!=0 );.
19bd5 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f if( enc==SQLITE_
19bd6 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f UTF8 ){. fo
19bd7 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 r(nByte=0; nByte
19bd8 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 <=iLimit && z[nB
19bd9 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d yte]; nByte++){}
19bda 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
19bdb 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e for(nByte=0; n
19bdc 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 Byte<=iLimit &&
19bdd 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 (z[nByte] | z[nB
19bde 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d yte+1]); nByte+=
19bdf 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 2){}. }. f
19be0 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d lags |= MEM_Term
19be1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
19be2 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 following block
19be3 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c sets the new val
19be4 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 ues of Mem.z and
19be5 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 Mem.xDel. It.
19be6 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 ** also sets a f
19be7 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 lag in local var
19be8 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f iable "flags" to
19be9 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 indicate the me
19bea 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 mory. ** manage
19beb 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d ment (one of MEM
19bec 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 _Dyn or MEM_Stat
19bed 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ic).. */. if(
19bee 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 xDel==SQLITE_TRA
19bef 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e NSIENT ){. in
19bf0 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 t nAlloc = nByte
19bf1 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 ;. if( flags&
19bf2 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 MEM_Term ){.
19bf3 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 nAlloc += (enc
19bf4 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a ==SQLITE_UTF8?1:
19bf5 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 2);. }. if
19bf6 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 ( nByte>iLimit )
19bf7 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
19bf8 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 QLITE_TOOBIG;.
19bf9 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }. if( sqli
19bfa 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19bfb 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 Mem, nAlloc, 0)
19bfc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
19bfd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
19bfe 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 }. memcpy(p
19bff 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f Mem->z, z, nAllo
19c00 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 c);. }else if(
19c01 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e xDel==SQLITE_DYN
19c02 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 AMIC ){. sqli
19c03 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19c04 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 e(pMem);. pMe
19c05 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 m->zMalloc = pMe
19c06 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a m->z = (char *)z
19c07 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c ;. pMem->xDel
19c08 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
19c09 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19c0a 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
19c0b 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 pMem->z = (c
19c0c 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 har *)z;. pMe
19c0d 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a m->xDel = xDel;.
19c0e 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 flags |= ((x
19c0f 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 Del==SQLITE_STAT
19c10 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d IC)?MEM_Static:M
19c11 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 EM_Dyn);. }..
19c12 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b pMem->n = nByte;
19c13 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d . pMem->flags =
19c14 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e flags;. pMem->
19c15 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 enc = (enc==0 ?
19c16 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e SQLITE_UTF8 : en
19c17 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 c);. pMem->type
19c18 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c = (enc==0 ? SQL
19c19 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 ITE_BLOB : SQLIT
19c1a 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 E_TEXT);..#ifnde
19c1b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
19c1c 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e F16. if( pMem->
19c1d 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc!=SQLITE_UTF8
19c1e 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d && sqlite3VdbeM
19c1f 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d emHandleBom(pMem
19c20 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
19c21 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
19c22 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 }.#endif.. if(
19c23 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a nByte>iLimit ){.
19c24 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
19c25 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 E_TOOBIG;. }..
19c26 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
19c27 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 K;.}../*.** Comp
19c28 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63 are the values c
19c29 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 ontained by the
19c2a 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 two memory cells
19c2b 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e , returning.** n
19c2c 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 egative, zero or
19c2d 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65 positive if pMe
19c2e 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c m1 is less than,
19c2f 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 equal to, or gr
19c30 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d eater.** than pM
19c31 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 em2. Sorting ord
19c32 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 er is NULL's fir
19c33 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 st, followed by
19c34 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72 numbers (integer
19c35 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 s.** and reals)
19c36 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c sorted numerical
19c37 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 ly, followed by
19c38 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20 text ordered by
19c39 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a the collating.**
19c3a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 sequence pColl
19c3b 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 and finally blob
19c3c 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65 's ordered by me
19c3d 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 mcmp()..**.** Tw
19c3e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 o NULL values ar
19c3f 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 e considered equ
19c40 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 al by this funct
19c41 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
19c42 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19c43 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e e3MemCompare(con
19c44 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 st Mem *pMem1, c
19c45 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c onst Mem *pMem2,
19c46 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a const CollSeq *
19c47 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 pColl){. int rc
19c48 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a ;. int f1, f2;.
19c49 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 int combined_f
19c4a 6c 61 67 73 3b 0a 0a 20 20 66 31 20 3d 20 70 4d lags;.. f1 = pM
19c4b 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 em1->flags;. f2
19c4c 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b = pMem2->flags;
19c4d 0a 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 . combined_flag
19c4e 73 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 s = f1|f2;. ass
19c4f 65 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 ert( (combined_f
19c50 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19c51 74 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 t)==0 );. . /*
19c52 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 If one value is
19c53 4e 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 NULL, it is less
19c54 20 74 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e than the other.
19c55 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a If both values.
19c56 20 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 ** are NULL, r
19c57 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 eturn 0.. */.
19c58 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 if( combined_fla
19c59 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 gs&MEM_Null ){.
19c5a 20 20 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45 return (f2&ME
19c5b 4d 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 M_Null) - (f1&ME
19c5c 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 M_Null);. }..
19c5d 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 /* If one value
19c5e 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 is a number and
19c5f 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 the other is not
19c60 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 , the number is
19c61 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f less.. ** If bo
19c62 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 th are numbers,
19c63 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73 compare as reals
19c64 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61 if one is a rea
19c65 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72 l, or as integer
19c66 73 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 s. ** if both v
19c67 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 67 65 alues are intege
19c68 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 rs.. */. if( c
19c69 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d ombined_flags&(M
19c6a 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
19c6b 20 29 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31 ){. if( !(f1
19c6c 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 &(MEM_Int|MEM_Re
19c6d 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 al)) ){. re
19c6e 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
19c6f 20 20 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f if( !(f2&(MEM_
19c70 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 Int|MEM_Real)) )
19c71 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d {. return -
19c72 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
19c73 20 28 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f (f1 & f2 & MEM_
19c74 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Int)==0 ){.
19c75 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a double r1, r2;.
19c76 20 20 20 20 20 20 69 66 28 20 28 66 31 26 4d 45 if( (f1&ME
19c77 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Real)==0 ){.
19c78 20 20 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62 r1 = (doub
19c79 6c 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 le)pMem1->u.i;.
19c7a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
19c7b 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e r1 = pMem1->
19c7c 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 r;. }.
19c7d 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 if( (f2&MEM_Rea
19c7e 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
19c7f 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d r2 = (double)pM
19c80 65 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 em2->u.i;.
19c81 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
19c82 32 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 2 = pMem2->r;.
19c83 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
19c84 72 31 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d r1<r2 ) return -
19c85 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e 1;. if( r1>
19c86 72 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 r2 ) return 1;.
19c87 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
19c88 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
19c89 61 73 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 assert( f1&MEM_I
19c8a 6e 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 nt );. asse
19c8b 72 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 rt( f2&MEM_Int )
19c8c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d ;. if( pMem
19c8d 31 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 1->u.i < pMem2->
19c8e 75 2e 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b u.i ) return -1;
19c8f 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 . if( pMem1
19c90 2d 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 ->u.i > pMem2->u
19c91 2e 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 .i ) return 1;.
19c92 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
19c93 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
19c94 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 f one value is a
19c95 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20 string and the
19c96 6f 74 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c other is a blob,
19c97 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c the string is l
19c98 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 ess.. ** If bot
19c99 68 20 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63 h are strings, c
19c9a 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 74 68 65 ompare using the
19c9b 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 collating funct
19c9c 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ions.. */. if(
19c9d 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 combined_flags&
19c9e 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 MEM_Str ){. i
19c9f 66 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 f( (f1 & MEM_Str
19ca0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )==0 ){. re
19ca1 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
19ca2 20 20 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f if( (f2 & MEM_
19ca3 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Str)==0 ){.
19ca4 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 return -1;.
19ca5 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
19ca6 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 Mem1->enc==pMem2
19ca7 2d 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 ->enc );. ass
19ca8 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d ert( pMem1->enc=
19ca9 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 =SQLITE_UTF8 ||
19caa 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65 . pMe
19cab 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f m1->enc==SQLITE_
19cac 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 UTF16LE || pMem1
19cad 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ->enc==SQLITE_UT
19cae 46 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a F16BE );.. /*
19caf 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 The collation s
19cb0 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 equence must be
19cb1 64 65 66 69 6e 65 64 20 61 74 20 74 68 69 73 20 defined at this
19cb2 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 point, even if.
19cb3 20 20 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64 ** the user d
19cb4 65 6c 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 eletes the colla
19cb5 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66 tion sequence af
19cb6 74 65 72 20 74 68 65 20 76 64 62 65 20 70 72 6f ter the vdbe pro
19cb7 67 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 gram is. ** c
19cb8 6f 6d 70 69 6c 65 64 20 28 74 68 69 73 20 77 61 ompiled (this wa
19cb9 73 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65 s not always the
19cba 20 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 case).. */.
19cbb 20 20 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c assert( !pCol
19cbc 6c 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 l || pColl->xCmp
19cbd 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f );.. if( pCo
19cbe 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
19cbf 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c pMem1->enc==pCol
19cc0 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 l->enc ){.
19cc1 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73 /* The strings
19cc2 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 are already in
19cc3 74 68 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f the correct enco
19cc4 64 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a ding. Call the.
19cc5 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 ** compa
19cc6 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 rison function d
19cc7 69 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 irectly */.
19cc8 20 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d return pColl-
19cc9 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 >xCmp(pColl->pUs
19cca 65 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d er,pMem1->n,pMem
19ccb 31 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 1->z,pMem2->n,pM
19ccc 65 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d em2->z);. }
19ccd 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f else{. co
19cce 6e 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 nst void *v1, *v
19ccf 32 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 2;. int n
19cd0 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 1, n2;. M
19cd1 65 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 4d em c1;. M
19cd2 65 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 6d em c2;. m
19cd3 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 73 69 emset(&c1, 0, si
19cd4 7a 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 20 20 zeof(c1));.
19cd5 20 20 20 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 memset(&c2, 0
19cd6 2c 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 , sizeof(c2));.
19cd7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
19cd8 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
19cd9 28 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d (&c1, pMem1, MEM
19cda 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 _Ephem);.
19cdb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19cdc 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 hallowCopy(&c2,
19cdd 70 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d pMem2, MEM_Ephem
19cde 29 3b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20 );. v1 =
19cdf 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
19ce0 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a ((sqlite3_value*
19ce1 29 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 )&c1, pColl->enc
19ce2 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d 20 );. n1 =
19ce3 76 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e v1==0 ? 0 : c1.n
19ce4 3b 0a 20 20 20 20 20 20 20 20 76 32 20 3d 20 73 ;. v2 = s
19ce5 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
19ce6 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
19ce7 26 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 &c2, pColl->enc)
19ce8 3b 0a 20 20 20 20 20 20 20 20 6e 32 20 3d 20 76 ;. n2 = v
19ce9 32 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 2==0 ? 0 : c2.n;
19cea 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 43 . rc = pC
19ceb 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d oll->xCmp(pColl-
19cec 3e 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 >pUser, n1, v1,
19ced 6e 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 20 20 n2, v2);.
19cee 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19cef 65 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 20 20 elease(&c1);.
19cf0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19cf1 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 29 3b MemRelease(&c2);
19cf2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
19cf3 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
19cf4 7d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 }. /* If a NU
19cf5 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70 LL pointer was p
19cf6 61 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c assed as the col
19cf7 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 late function, f
19cf8 61 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 20 20 all through.
19cf9 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 ** to the blob c
19cfa 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 ase and use memc
19cfb 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a mp(). */. }. .
19cfc 20 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 /* Both values
19cfd 20 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 must be blobs.
19cfe 20 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 6d Compare using m
19cff 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 emcmp(). */. r
19d00 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 c = memcmp(pMem1
19d01 2d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 ->z, pMem2->z, (
19d02 70 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e pMem1->n>pMem2->
19d03 6e 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d n)?pMem2->n:pMem
19d04 31 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 1->n);. if( rc=
19d05 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 =0 ){. rc = p
19d06 4d 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d Mem1->n - pMem2-
19d07 3e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e >n;. }. return
19d08 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f rc;.}../*.** Mo
19d09 76 65 20 64 61 74 61 20 6f 75 74 20 6f 66 20 61 ve data out of a
19d0a 20 62 74 72 65 65 20 6b 65 79 20 6f 72 20 64 61 btree key or da
19d0b 74 61 20 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 ta field and int
19d0c 6f 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72 o a Mem structur
19d0d 65 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f e..** The data o
19d0e 72 20 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66 r key is taken f
19d0f 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 rom the entry th
19d10 61 74 20 70 43 75 72 20 69 73 20 63 75 72 72 65 at pCur is curre
19d11 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a ntly pointing.**
19d12 20 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 to. offset and
19d13 20 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 amt determine w
19d14 68 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 hat portion of t
19d15 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74 he data or key t
19d16 6f 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b o retrieve..** k
19d17 65 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65 ey is true to ge
19d18 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c t the key or fal
19d19 73 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20 se to get data.
19d1a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77 The result is w
19d1b 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 ritten.** into t
19d1c 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e he pMem element.
19d1d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 .**.** The pMem
19d1e 73 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73 structure is ass
19d1f 75 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 umed to be unini
19d20 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 tialized. Any p
19d21 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 rior content.**
19d22 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 is overwritten w
19d23 69 74 68 6f 75 74 20 62 65 69 6e 67 20 66 72 65 ithout being fre
19d24 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 ed..**.** If thi
19d25 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 s routine fails
19d26 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 for any reason (
19d27 6d 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e malloc returns N
19d28 55 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a ULL or unable.**
19d29 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68 to read from th
19d2a 65 20 64 69 73 6b 29 20 74 68 65 6e 20 74 68 65 e disk) then the
19d2b 20 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 69 6e pMem is left in
19d2c 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
19d2d 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 state..*/.SQLIT
19d2e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19d2f 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
19d30 42 74 72 65 65 28 0a 20 20 42 74 43 75 72 73 6f Btree(. BtCurso
19d31 72 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 r *pCur, /* Cu
19d32 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 rsor pointing at
19d33 20 72 65 63 6f 72 64 20 74 6f 20 72 65 74 72 69 record to retri
19d34 65 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 eve. */. int of
19d35 66 73 65 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f fset, /* O
19d36 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 73 ffset from the s
19d37 74 61 72 74 20 6f 66 20 64 61 74 61 20 74 6f 20 tart of data to
19d38 72 65 74 75 72 6e 20 62 79 74 65 73 20 66 72 6f return bytes fro
19d39 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c m. */. int amt,
19d3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
19d3b 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
19d3c 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 return. */. int
19d3d 20 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f key, /
19d3e 2a 20 49 66 20 74 72 75 65 2c 20 72 65 74 72 69 * If true, retri
19d3f 65 76 65 20 66 72 6f 6d 20 74 68 65 20 62 74 72 eve from the btr
19d40 65 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 ee key, not data
19d41 2e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d . */. Mem *pMem
19d42 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
19d43 20 52 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20 Return data in
19d44 74 68 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75 this Mem structu
19d45 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 re. */.){. char
19d46 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20 *zData;
19d47 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65 /* Data from the
19d48 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a btree layer */.
19d49 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 int available
19d4a 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 = 0; /* Number
19d4b 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 of bytes availab
19d4c 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 le on the local
19d4d 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 btree page */.
19d4e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
19d4f 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f OK; /* Return co
19d50 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 de */.. assert(
19d51 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
19d52 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 sorIsValid(pCur)
19d53 20 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 );.. /* Note:
19d54 74 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 the calls to Btr
19d55 65 65 4b 65 79 46 65 74 63 68 28 29 20 61 6e 64 eeKeyFetch() and
19d56 20 44 61 74 61 46 65 74 63 68 28 29 20 62 65 6c DataFetch() bel
19d57 6f 77 20 61 73 73 65 72 74 28 29 20 0a 20 20 2a ow assert() . *
19d58 2a 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 * that both the
19d59 42 74 53 68 61 72 65 64 20 61 6e 64 20 64 61 74 BtShared and dat
19d5a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 abase handle mut
19d5b 65 78 65 73 20 61 72 65 20 68 65 6c 64 2e 20 2a exes are held. *
19d5c 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 /. assert( (pMe
19d5d 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 m->flags & MEM_R
19d5e 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 owSet)==0 );. i
19d5f 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 f( key ){. zD
19d60 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 ata = (char *)sq
19d61 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 lite3BtreeKeyFet
19d62 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 ch(pCur, &availa
19d63 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ble);. }else{.
19d64 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 zData = (char
19d65 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 *)sqlite3BtreeD
19d66 61 74 61 46 65 74 63 68 28 70 43 75 72 2c 20 26 ataFetch(pCur, &
19d67 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a available);. }.
19d68 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 21 assert( zData!
19d69 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 =0 );.. if( off
19d6a 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 set+amt<=availab
19d6b 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 le && (pMem->fla
19d6c 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 gs&MEM_Dyn)==0 )
19d6d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
19d6e 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
19d6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d );. pMem->z =
19d70 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b &zData[offset];
19d71 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
19d72 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f = MEM_Blob|MEM_
19d73 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 Ephem;. }else i
19d74 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 f( SQLITE_OK==(r
19d75 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
19d76 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 emGrow(pMem, amt
19d77 2b 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 70 +2, 0)) ){. p
19d78 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
19d79 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 _Blob|MEM_Dyn|ME
19d7a 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d M_Term;. pMem
19d7b 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 ->enc = 0;. p
19d7c 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
19d7d 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 TE_BLOB;. if(
19d7e 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 key ){. rc
19d7f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
19d80 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c ey(pCur, offset,
19d81 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a amt, pMem->z);.
19d82 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
19d83 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
19d84 65 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66 eeData(pCur, off
19d85 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e set, amt, pMem->
19d86 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d z);. }. pM
19d87 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a em->z[amt] = 0;.
19d88 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b pMem->z[amt+
19d89 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 1] = 0;. if(
19d8a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
19d8b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
19d8c 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 beMemRelease(pMe
19d8d 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 m);. }. }.
19d8e 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a pMem->n = amt;..
19d8f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19d90 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
19d91 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
19d92 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 le internally, i
19d93 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 t is not part of
19d94 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c the.** external
19d95 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 API. It works i
19d96 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79 20 n a similar way
19d97 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 to sqlite3_value
19d98 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 _text(),.** exce
19d99 70 74 20 74 68 65 20 64 61 74 61 20 72 65 74 75 pt the data retu
19d9a 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20 65 rned is in the e
19d9b 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69 65 ncoding specifie
19d9c 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 0a d by the second.
19d9d 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77 68 ** parameter, wh
19d9e 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 ich must be one
19d9f 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 of SQLITE_UTF16B
19da0 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c E, SQLITE_UTF16L
19da1 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 E or.** SQLITE_U
19da2 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 TF8..**.** (2006
19da3 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e -02-16:) The en
19da4 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f c value can be o
19da5 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 r-ed with SQLITE
19da6 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a _UTF16_ALIGNED..
19da7 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68 ** If that is th
19da8 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 e case, then the
19da9 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20 result must be
19daa 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 aligned on an ev
19dab 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 en byte.** bound
19dac 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ary..*/.SQLITE_P
19dad 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
19dae 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 d *sqlite3ValueT
19daf 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ext(sqlite3_valu
19db0 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 e* pVal, u8 enc)
19db1 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 {. if( !pVal )
19db2 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 return 0;.. ass
19db3 65 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 ert( pVal->db==0
19db4 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
19db5 78 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d x_held(pVal->db-
19db6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
19db7 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 ert( (enc&3)==(e
19db8 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 nc&~SQLITE_UTF16
19db9 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 _ALIGNED) );. a
19dba 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c ssert( (pVal->fl
19dbb 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
19dbc 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 )==0 );.. if( p
19dbd 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e Val->flags&MEM_N
19dbe 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ull ){. retur
19dbf 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 0;. }. asser
19dc0 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 t( (MEM_Blob>>3)
19dc1 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 == MEM_Str );.
19dc2 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 pVal->flags |=
19dc3 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d (pVal->flags & M
19dc4 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 EM_Blob)>>3;. e
19dc5 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b xpandBlob(pVal);
19dc6 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 . if( pVal->fla
19dc7 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 gs&MEM_Str ){.
19dc8 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
19dc9 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c ngeEncoding(pVal
19dca 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f , enc & ~SQLITE_
19dcb 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a UTF16_ALIGNED);.
19dcc 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53 if( (enc & S
19dcd 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
19dce 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 NED)!=0 && 1==(1
19dcf 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 &SQLITE_PTR_TO_I
19dd0 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a NT(pVal->z)) ){.
19dd1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
19dd2 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Val->flags & (ME
19dd3 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 M_Ephem|MEM_Stat
19dd4 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 ic))!=0 );.
19dd5 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
19dd6 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
19dd7 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f (pVal)!=SQLITE_O
19dd8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
19dd9 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 urn 0;. }.
19dda 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
19ddb 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
19ddc 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c ate(pVal);. }el
19ddd 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
19dde 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d (pVal->flags&MEM
19ddf 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 _Blob)==0 );.
19de0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19de1 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 tringify(pVal, e
19de2 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 nc);. assert(
19de3 20 30 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 0==(1&SQLITE_PT
19de4 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a R_TO_INT(pVal->z
19de5 29 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 )) );. }. asse
19de6 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 rt(pVal->enc==(e
19de7 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 nc & ~SQLITE_UTF
19de8 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 16_ALIGNED) || p
19de9 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 Val->db==0.
19dea 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56 61 6c || pVal
19deb 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
19dec 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56 61 6c ed );. if( pVal
19ded 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 ->enc==(enc & ~S
19dee 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
19def 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65 74 75 NED) ){. retu
19df0 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 rn pVal->z;. }e
19df1 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
19df2 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
19df3 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c Create a new sql
19df4 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
19df5 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
19df6 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c VATE sqlite3_val
19df7 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 ue *sqlite3Value
19df8 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64 62 29 New(sqlite3 *db)
19df9 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c {. Mem *p = sql
19dfa 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
19dfb 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 (db, sizeof(*p))
19dfc 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
19dfd 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f p->flags = MEM_
19dfe 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 Null;. p->typ
19dff 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
19e00 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b . p->db = db;
19e01 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
19e02 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
19e03 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 a new sqlite3_v
19e04 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e alue object, con
19e05 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 taining the valu
19e06 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a e of pExpr..**.*
19e07 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b * This only work
19e08 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d 70 6c s for very simpl
19e09 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 e expressions th
19e0a 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e at consist of on
19e0b 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f e constant.** to
19e0c 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 ken (i.e. "5", "
19e0d 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69 6e 67 5.1", "'a string
19e0e 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 70 72 '"). If the expr
19e0f 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 ession can.** be
19e10 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 65 63 converted direc
19e11 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 tly into a value
19e12 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
19e13 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
19e14 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 d.** a pointer w
19e15 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c ritten to *ppVal
19e16 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 . The caller is
19e17 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
19e18 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 deallocating.**
19e19 74 68 65 20 76 61 6c 75 65 20 62 79 20 70 61 73 the value by pas
19e1a 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69 74 sing it to sqlit
19e1b 65 33 56 61 6c 75 65 46 72 65 65 28 29 20 6c 61 e3ValueFree() la
19e1c 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 ter on. If the e
19e1d 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e xpression.** can
19e1e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 not be converted
19e1f 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68 65 to a value, the
19e20 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65 74 20 n *ppVal is set
19e21 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 to NULL..*/.SQLI
19e22 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19e23 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 qlite3ValueFromE
19e24 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a xpr(. sqlite3 *
19e25 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 db,
19e26 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
19e27 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
19e28 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 Expr *pExpr,
19e29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
19e2a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 e expression to
19e2b 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 evaluate */. u8
19e2c 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 enc,
19e2d 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 /* Encod
19e2e 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 ing to use */.
19e2f 75 38 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 u8 affinity,
19e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66 /* Aff
19e31 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a inity to use */.
19e32 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
19e33 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 **ppVal /* W
19e34 72 69 74 65 20 74 68 65 20 6e 65 77 20 76 61 6c rite the new val
19e35 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ue here */.){.
19e36 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a int op;. char *
19e37 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 zVal = 0;. sqli
19e38 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 te3_value *pVal
19e39 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 = 0;.. if( !pEx
19e3a 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c pr ){. *ppVal
19e3b 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
19e3c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
19e3d 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 op = pExpr->op
19e3e 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 ;. if( op==TK_R
19e3f 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 20 6f EGISTER ){. o
19e40 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 32 3b 20 p = pExpr->op2;
19e41 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 /* This only ha
19e42 70 70 65 6e 73 20 77 69 74 68 20 53 51 4c 49 54 ppens with SQLIT
19e43 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a E_ENABLE_STAT2 *
19e44 2f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6f 70 3d /. }.. if( op=
19e45 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 =TK_STRING || op
19e46 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 ==TK_FLOAT || op
19e47 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a ==TK_INTEGER ){.
19e48 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 pVal = sqlit
19e49 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a e3ValueNew(db);.
19e4a 20 20 20 20 69 66 28 20 70 56 61 6c 3d 3d 30 20 if( pVal==0
19e4b 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
19e4c 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
19e4d 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
19e4e 5f 49 6e 74 56 61 6c 75 65 29 20 29 7b 0a 20 20 _IntValue) ){.
19e4f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19e50 65 6d 53 65 74 49 6e 74 36 34 28 70 56 61 6c 2c emSetInt64(pVal,
19e51 20 28 69 36 34 29 70 45 78 70 72 2d 3e 75 2e 69 (i64)pExpr->u.i
19e52 56 61 6c 75 65 29 3b 0a 20 20 20 20 7d 65 6c 73 Value);. }els
19e53 65 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 e{. zVal =
19e54 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
19e55 64 62 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f db, pExpr->u.zTo
19e56 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ken);. if(
19e57 7a 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e zVal==0 ) goto n
19e58 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c o_mem;. sql
19e59 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
19e5a 70 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 pVal, -1, zVal,
19e5b 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
19e5c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 ITE_DYNAMIC);.
19e5d 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 if( op==TK_F
19e5e 4c 4f 41 54 20 29 20 70 56 61 6c 2d 3e 74 79 70 LOAT ) pVal->typ
19e5f 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 e = SQLITE_FLOAT
19e60 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
19e61 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 (op==TK_INTEGER
19e62 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 || op==TK_FLOAT
19e63 29 20 26 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 ) && affinity==S
19e64 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 QLITE_AFF_NONE )
19e65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
19e66 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 alueApplyAffinit
19e67 79 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 y(pVal, SQLITE_A
19e68 46 46 5f 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 FF_NUMERIC, SQLI
19e69 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 TE_UTF8);. }e
19e6a 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
19e6b 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 e3ValueApplyAffi
19e6c 6e 69 74 79 28 70 56 61 6c 2c 20 61 66 66 69 6e nity(pVal, affin
19e6d 69 74 79 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 ity, SQLITE_UTF8
19e6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
19e6f 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 enc!=SQLITE_UTF
19e70 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 8 ){. sqlit
19e71 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
19e72 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b ding(pVal, enc);
19e73 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
19e74 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 f( op==TK_UMINUS
19e75 20 29 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c ) {. if( SQL
19e76 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 ITE_OK==sqlite3V
19e77 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c alueFromExpr(db,
19e78 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 pExpr->pLeft,enc
19e79 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 ,affinity,&pVal)
19e7a 20 29 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e ){. pVal->
19e7b 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d u.i = -1 * pVal-
19e7c 3e 75 2e 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 >u.i;. /* (
19e7d 64 6f 75 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 double)-1 In cas
19e7e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 e of SQLITE_OMIT
19e7f 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e _FLOATING_POINT.
19e80 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c .. */. pVal
19e81 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 ->r = (double)-1
19e82 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 * pVal->r;.
19e83 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }. }.#ifndef SQ
19e84 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
19e85 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 ITERAL. else if
19e86 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b ( op==TK_BLOB ){
19e87 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 . int nVal;.
19e88 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
19e89 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 ->u.zToken[0]=='
19e8a 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a x' || pExpr->u.z
19e8b 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b Token[0]=='X' );
19e8c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
19e8d 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d pr->u.zToken[1]=
19e8e 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61 ='\'' );. pVa
19e8f 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 l = sqlite3Value
19e90 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 New(db);. if(
19e91 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f !pVal ) goto no
19e92 5f 6d 65 6d 3b 0a 20 20 20 20 7a 56 61 6c 20 3d _mem;. zVal =
19e93 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 &pExpr->u.zToke
19e94 6e 5b 32 5d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d n[2];. nVal =
19e95 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
19e96 28 7a 56 61 6c 29 2d 31 3b 0a 20 20 20 20 61 73 (zVal)-1;. as
19e97 73 65 72 74 28 20 7a 56 61 6c 5b 6e 56 61 6c 5d sert( zVal[nVal]
19e98 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 73 71 =='\'' );. sq
19e99 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
19e9a 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 tr(pVal, sqlite3
19e9b 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 HexToBlob(db, zV
19e9c 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f al, nVal), nVal/
19e9d 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2,.
19e9e 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 0, S
19e9f 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a QLITE_DYNAMIC);.
19ea0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 }.#endif.. if
19ea1 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20 73 71 ( pVal ){. sq
19ea2 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 lite3VdbeMemStor
19ea3 65 54 79 70 65 28 70 56 61 6c 29 3b 0a 20 20 7d eType(pVal);. }
19ea4 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c . *ppVal = pVal
19ea5 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
19ea6 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 E_OK;..no_mem:.
19ea7 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
19ea8 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 d = 1;. sqlite3
19ea9 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 6c 29 DbFree(db, zVal)
19eaa 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 ;. sqlite3Value
19eab 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 Free(pVal);. *p
19eac 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 pVal = 0;. retu
19ead 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
19eae 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
19eaf 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75 the string valu
19eb0 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f e of an sqlite3_
19eb1 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a value object.*/.
19eb2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19eb3 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 oid sqlite3Value
19eb4 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 74 65 SetStr(. sqlite
19eb5 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 3_value *v,
19eb6 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20 73 /* Value to be s
19eb7 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 et */. int n,
19eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19eb9 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e Length of strin
19eba 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 g z */. const v
19ebb 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f oid *z, /
19ebc 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e 65 * Text of the ne
19ebd 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38 w string */. u8
19ebe 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 enc,
19ebf 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 /* Encoding
19ec0 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64 to use */. void
19ec1 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 (*xDel)(void*)
19ec2 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 /* Destructor
19ec3 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 2a for the string *
19ec4 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 20 73 /.){. if( v ) s
19ec5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
19ec6 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c Str((Mem *)v, z,
19ec7 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a n, enc, xDel);.
19ec8 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e }../*.** Free an
19ec9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
19eca 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f bject.*/.SQLITE_
19ecb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19ecc 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 ite3ValueFree(sq
19ecd 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b lite3_value *v){
19ece 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75 . if( !v ) retu
19ecf 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 rn;. sqlite3Vdb
19ed0 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d eMemRelease((Mem
19ed1 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 *)v);. sqlite3
19ed2 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 DbFree(((Mem*)v)
19ed3 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a ->db, v);.}../*.
19ed4 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
19ed5 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
19ed6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c the sqlite3_val
19ed7 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 6d 69 ue object assumi
19ed8 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 75 73 ng.** that it us
19ed9 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 es the encoding
19eda 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f "enc".*/.SQLITE_
19edb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19edc 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 te3ValueBytes(sq
19edd 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
19ede 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65 l, u8 enc){. Me
19edf 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 m *p = (Mem*)pVa
19ee0 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 l;. if( (p->fla
19ee1 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d gs & MEM_Blob)!=
19ee2 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 6c 75 0 || sqlite3Valu
19ee3 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e 63 29 eText(pVal, enc)
19ee4 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 ){. if( p->f
19ee5 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
19ee6 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
19ee7 70 2d 3e 6e 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 p->n + p->u.nZer
19ee8 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 o;. }else{.
19ee9 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b return p->n;
19eea 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
19eeb 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a urn 0;.}../*****
19eec 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
19eed 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a vdbemem.c *****
19eee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ef0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
19ef1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
19ef2 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63 20 2a file vdbeaux.c *
19ef3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ef4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ef5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
19ef6 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 20 36 2003 September 6
19ef7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
19ef8 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
19ef9 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
19efa 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
19efb 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
19efc 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
19efd 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
19efe 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
19eff 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
19f00 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
19f01 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
19f02 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
19f03 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
19f04 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
19f05 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
19f06 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
19f07 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
19f08 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
19f09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19f0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
19f0d 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
19f0e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 tains code used
19f0f 66 6f 72 20 63 72 65 61 74 69 6e 67 2c 20 64 65 for creating, de
19f10 73 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f stroying, and po
19f11 70 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 pulating.** a VD
19f12 42 45 20 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 BE (or an "sqlit
19f13 65 33 5f 73 74 6d 74 22 20 61 73 20 69 74 20 69 e3_stmt" as it i
19f14 73 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f s known to the o
19f15 75 74 73 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 utside world.)
19f16 50 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 Prior.** to vers
19f17 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 ion 2.8.7, all t
19f18 68 69 73 20 63 6f 64 65 20 77 61 73 20 63 6f 6d his code was com
19f19 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 76 bined into the v
19f1a 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66 69 6c dbe.c source fil
19f1b 65 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 20 66 e..** But that f
19f1c 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e 67 20 ile was getting
19f1d 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69 73 20 too big so this
19f1e 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 72 65 subroutines were
19f1f 20 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f 0a 0a split out..*/..
19f20 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 ../*.** When deb
19f21 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20 ugging the code
19f22 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73 generator in a s
19f23 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72 ymbolic debugger
19f24 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 , one can.** set
19f25 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
19f26 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 20 AddopTrace to 1
19f27 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 and all opcodes
19f28 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 0a will be printed.
19f29 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 61 ** as they are a
19f2a 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 74 dded to the inst
19f2b 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a ruction stream..
19f2c 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
19f2d 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
19f2e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19f2f 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
19f30 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a = 0;.#endif.../*
19f31 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
19f32 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 virtual databas
19f33 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c e engine..*/.SQL
19f34 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 ITE_PRIVATE Vdbe
19f35 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 *sqlite3VdbeCre
19f36 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ate(sqlite3 *db)
19f37 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 {. Vdbe *p;. p
19f38 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
19f39 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
19f3a 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28 f(Vdbe) );. if(
19f3b 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
19f3c 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a ;. p->db = db;.
19f3d 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20 if( db->pVdbe
19f3e 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 ){. db->pVdbe
19f3f 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d ->pPrev = p;. }
19f40 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 . p->pNext = db
19f41 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 ->pVdbe;. p->pP
19f42 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 rev = 0;. db->p
19f43 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d Vdbe = p;. p->m
19f44 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
19f45 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e C_INIT;. return
19f46 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d p;.}../*.** Rem
19f47 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74 ember the SQL st
19f48 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61 ring for a prepa
19f49 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a red statement..*
19f4a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19f4b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19f4c 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c eSetSql(Vdbe *p,
19f4d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
19f4e 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 65 int n, int isPre
19f4f 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73 65 72 pareV2){. asser
19f50 74 28 20 69 73 50 72 65 70 61 72 65 56 32 3d 3d t( isPrepareV2==
19f51 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65 56 32 1 || isPrepareV2
19f52 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d ==0 );. if( p==
19f53 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 64 0 ) return;.#ifd
19f54 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
19f55 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 50 72 RACE. if( !isPr
19f56 65 70 61 72 65 56 32 20 29 20 72 65 74 75 72 6e epareV2 ) return
19f57 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 ;.#endif. asser
19f58 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b t( p->zSql==0 );
19f59 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c . p->zSql = sql
19f5a 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d ite3DbStrNDup(p-
19f5b 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d >db, z, n);. p-
19f5c 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d 20 28 >isPrepareV2 = (
19f5d 75 38 29 69 73 50 72 65 70 61 72 65 56 32 3b 0a u8)isPrepareV2;.
19f5e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
19f5f 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74 the SQL associat
19f60 65 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72 ed with a prepar
19f61 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a ed statement.*/.
19f62 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
19f63 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 char *sqlite3_s
19f64 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ql(sqlite3_stmt
19f65 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
19f66 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
19f67 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d mt;. return (p-
19f68 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70 >isPrepareV2 ? p
19f69 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a ->zSql : 0);.}..
19f6a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 /*.** Swap all c
19f6b 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 ontent between t
19f6c 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 wo VDBE structur
19f6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
19f6e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
19f6f 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 e3VdbeSwap(Vdbe
19f70 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a *pA, Vdbe *pB){.
19f71 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d Vdbe tmp, *pTm
19f72 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b p;. char *zTmp;
19f73 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 . tmp = *pA;.
19f74 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 *pA = *pB;. *pB
19f75 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d = tmp;. pTmp =
19f76 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 pA->pNext;. pA
19f77 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e ->pNext = pB->pN
19f78 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 ext;. pB->pNext
19f79 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 = pTmp;. pTmp
19f7a 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 = pA->pPrev;. p
19f7b 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 A->pPrev = pB->p
19f7c 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 Prev;. pB->pPre
19f7d 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 v = pTmp;. zTmp
19f7e 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 = pA->zSql;. p
19f7f 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 A->zSql = pB->zS
19f80 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d ql;. pB->zSql =
19f81 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 zTmp;. pB->isP
19f82 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 repareV2 = pA->i
19f83 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 sPrepareV2;.}..#
19f84 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
19f85 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 UG./*.** Turn tr
19f86 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a acing on or off.
19f87 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19f88 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19f89 62 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c beTrace(Vdbe *p,
19f8a 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 FILE *trace){.
19f8b 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 p->trace = trac
19f8c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e;.}.#endif../*.
19f8d 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 ** Resize the Vd
19f8e 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 be.aOp array so
19f8f 74 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 that it is at le
19f90 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 ast one op large
19f91 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 r than .** it wa
19f92 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f s..**.** If an o
19f93 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 ut-of-memory err
19f94 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
19f95 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 resizing the arr
19f96 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 ay, return.** SQ
19f97 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 LITE_NOMEM. In t
19f98 68 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f his case Vdbe.aO
19f99 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c p and Vdbe.nOpAl
19f9a 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 loc remain .** u
19f9b 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 nchanged (this i
19f9c 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 s so that any op
19f9d 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c codes already al
19f9e 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a located can be .
19f9f 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 ** correctly dea
19fa0 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 llocated along w
19fa1 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 ith the rest of
19fa2 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 the Vdbe)..*/.st
19fa3 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 atic int growOpA
19fa4 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 rray(Vdbe *p){.
19fa5 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 VdbeOp *pNew;.
19fa6 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e int nNew = (p->
19fa7 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f nOpAlloc ? p->nO
19fa8 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 pAlloc*2 : (int)
19fa9 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 (1024/sizeof(Op)
19faa 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c ));. pNew = sql
19fab 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d ite3DbRealloc(p-
19fac 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 >db, p->aOp, nNe
19fad 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 w*sizeof(Op));.
19fae 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 if( pNew ){.
19faf 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 p->nOpAlloc = s
19fb0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 qlite3DbMallocSi
19fb1 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f ze(p->db, pNew)/
19fb2 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 sizeof(Op);.
19fb3 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 p->aOp = pNew;.
19fb4 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 }. return (pNe
19fb5 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 w ? SQLITE_OK :
19fb6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
19fb7 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
19fb8 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f w instruction to
19fb9 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 the list of ins
19fba 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e tructions curren
19fbb 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 t in the.** VDBE
19fbc 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 . Return the ad
19fbd 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 dress of the new
19fbe 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
19fbf 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a .** Parameters:.
19fc0 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 **.** p
19fc1 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 Pointer
19fc2 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a to the VDBE.**.
19fc3 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 ** op
19fc4 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 The opcode
19fc5 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 for this instru
19fc6 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ction.**.** p
19fc7 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 1, p2, p3 O
19fc8 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 perands.**.** Us
19fc9 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 e the sqlite3Vdb
19fca 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 eResolveLabel()
19fcb 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 function to fix
19fcc 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a an address and.*
19fcd 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 * the sqlite3Vdb
19fce 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 eChangeP4() func
19fcf 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 tion to change t
19fd0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
19fd1 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a P4.** operand..*
19fd2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19fd3 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19fd4 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 AddOp3(Vdbe *p,
19fd5 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 int op, int p1,
19fd6 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b int p2, int p3){
19fd7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 . int i;. Vdbe
19fd8 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 Op *pOp;.. i =
19fd9 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 p->nOp;. assert
19fda 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
19fdb 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
19fdc 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 assert( op>0 &&
19fdd 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 op<0xff );. if
19fde 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 ( p->nOpAlloc<=i
19fdf 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 ){. if( grow
19fe0 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 OpArray(p) ){.
19fe1 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
19fe2 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 }. }. p->nOp
19fe3 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e ++;. pOp = &p->
19fe4 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f aOp[i];. pOp->o
19fe5 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a pcode = (u8)op;.
19fe6 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 pOp->p5 = 0;.
19fe7 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 pOp->p1 = p1;.
19fe8 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 pOp->p2 = p2;.
19fe9 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 pOp->p3 = p3;.
19fea 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a pOp->p4.p = 0;.
19feb 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
19fec 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d P4_NOTUSED;. p-
19fed 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 >expired = 0;.#i
19fee 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
19fef 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e G. pOp->zCommen
19ff0 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c t = 0;. if( sql
19ff1 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 ite3VdbeAddopTra
19ff2 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 ce ) sqlite3Vdbe
19ff3 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 PrintOp(0, i, &p
19ff4 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 ->aOp[i]);.#endi
19ff5 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 f.#ifdef VDBE_PR
19ff6 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 OFILE. pOp->cyc
19ff7 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e les = 0;. pOp->
19ff8 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a cnt = 0;.#endif.
19ff9 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 return i;.}.SQ
19ffa 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19ffb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19ffc 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 p0(Vdbe *p, int
19ffd 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 op){. return sq
19ffe 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
19fff 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b p, op, 0, 0, 0);
1a000 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
1a001 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1a002 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c eAddOp1(Vdbe *p,
1a003 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 int op, int p1)
1a004 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1a005 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
1a006 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d op, p1, 0, 0);.}
1a007 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a008 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
1a009 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69 ddOp2(Vdbe *p, i
1a00a 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 nt op, int p1, i
1a00b 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e nt p2){. return
1a00c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1a00d 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 p3(p, op, p1, p2
1a00e 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 , 0);.}.../*.**
1a00f 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 Add an opcode th
1a010 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 at includes the
1a011 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f p4 value as a po
1a012 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 inter..*/.SQLITE
1a013 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a014 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a ite3VdbeAddOp4(.
1a015 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 Vdbe *p,
1a016 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 /* Add the
1a017 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20 opcode to this
1a018 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 VM */. int op,
1a019 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1a01a 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f he new opcode */
1a01b 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20 . int p1,
1a01c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31 /* The P1
1a01d 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
1a01e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20 t p2,
1a01f 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72 /* The P2 oper
1a020 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c and */. int p3,
1a021 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a022 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a The P3 operand *
1a023 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1a024 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 zP4, /* The P
1a025 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 4 operand */. i
1a026 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20 nt p4type
1a027 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 /* P4 operand
1a028 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e type */.){. in
1a029 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 t addr = sqlite3
1a02a 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 VdbeAddOp3(p, op
1a02b 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 , p1, p2, p3);.
1a02c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a02d 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 geP4(p, addr, zP
1a02e 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65 4, p4type);. re
1a02f 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a turn addr;.}../*
1a030 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f 64 .** Add an opcod
1a031 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20 e that includes
1a032 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 73 20 the p4 value as
1a033 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 an integer..*/.S
1a034 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a035 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
1a036 4f 70 34 49 6e 74 28 0a 20 20 56 64 62 65 20 2a Op4Int(. Vdbe *
1a037 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a p, /*
1a038 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 Add the opcode
1a039 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 to this VM */.
1a03a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
1a03b 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f /* The new o
1a03c 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 pcode */. int p
1a03d 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 1, /
1a03e 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 * The P1 operand
1a03f 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 */. int p2,
1a040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1a041 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 P2 operand */.
1a042 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 int p3,
1a043 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f /* The P3 o
1a044 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
1a045 70 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p4
1a046 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e /* The P4 operan
1a047 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 d as an integer
1a048 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 */.){. int addr
1a049 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
1a04a 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 dOp3(p, op, p1,
1a04b 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c 69 74 p2, p3);. sqlit
1a04c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 70 e3VdbeChangeP4(p
1a04d 2c 20 61 64 64 72 2c 20 53 51 4c 49 54 45 5f 49 , addr, SQLITE_I
1a04e 4e 54 5f 54 4f 5f 50 54 52 28 70 34 29 2c 20 50 NT_TO_PTR(p4), P
1a04f 34 5f 49 4e 54 33 32 29 3b 0a 20 20 72 65 74 75 4_INT32);. retu
1a050 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a rn addr;.}../*.*
1a051 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 * Create a new s
1a052 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f ymbolic label fo
1a053 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e r an instruction
1a054 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f that has yet to
1a055 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 be.** coded. T
1a056 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 he symbolic labe
1a057 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 l is really just
1a058 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 a negative numb
1a059 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 er. The.** labe
1a05a 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 l can be used as
1a05b 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66 the P2 value of
1a05c 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 an operation.
1a05d 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 Later, when.** t
1a05e 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f he label is reso
1a05f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 66 lved to a specif
1a060 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 20 ic address, the
1a061 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a VDBE will scan.*
1a062 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70 * through its op
1a063 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 eration list and
1a064 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 change all valu
1a065 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d es of P2 which m
1a066 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 atch.** the labe
1a067 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c l into the resol
1a068 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a ved address..**.
1a069 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 ** The VDBE know
1a06a 73 20 74 68 61 74 20 61 20 50 32 20 76 61 6c 75 s that a P2 valu
1a06b 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63 e is a label bec
1a06c 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a ause labels are.
1a06d 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69 ** always negati
1a06e 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73 ve and P2 values
1a06f 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 are suppose to
1a070 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e be non-negative.
1a071 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 .** Hence, a neg
1a072 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 69 ative P2 value i
1a073 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68 s a label that h
1a074 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 73 as yet to be res
1a075 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 olved..**.** Zer
1a076 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 o is returned if
1a077 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1a078 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
1a079 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1a07a 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 VdbeMakeLabel(Vd
1a07b 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b be *p){. int i;
1a07c 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c . i = p->nLabel
1a07d 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d ++;. assert( p-
1a07e 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1a07f 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
1a080 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c i>=p->nLabelAll
1a081 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 oc ){. int n
1a082 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 = p->nLabelAlloc
1a083 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d 3e 61 *2 + 5;. p->a
1a084 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 44 Label = sqlite3D
1a085 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 bReallocOrFree(p
1a086 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c ->db, p->aLabel,
1a087 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1a088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a089 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 6f 66 n*sizeof
1a08a 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b (p->aLabel[0]));
1a08b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c . p->nLabelAl
1a08c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d loc = sqlite3DbM
1a08d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c allocSize(p->db,
1a08e 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 7a 65 p->aLabel)/size
1a08f 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 of(p->aLabel[0])
1a090 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 ;. }. if( p->a
1a091 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e Label ){. p->
1a092 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a aLabel[i] = -1;.
1a093 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d }. return -1-
1a094 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f i;.}../*.** Reso
1a095 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74 6f lve label "x" to
1a096 20 62 65 20 74 68 65 20 61 64 64 72 65 73 73 20 be the address
1a097 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 of the next inst
1a098 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 ruction to.** be
1a099 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65 20 inserted. The
1a09a 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d 75 parameter "x" mu
1a09b 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62 74 st have been obt
1a09c 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 ained from.** a
1a09d 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 prior call to sq
1a09e 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
1a09f 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f el()..*/.SQLITE_
1a0a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a0a1 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
1a0a2 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e abel(Vdbe *p, in
1a0a3 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 t x){. int j =
1a0a4 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28 20 -1-x;. assert(
1a0a5 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
1a0a6 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 AGIC_INIT );. a
1a0a7 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a ssert( j>=0 && j
1a0a8 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 <p->nLabel );.
1a0a9 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b if( p->aLabel ){
1a0aa 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a . p->aLabel[j
1a0ab 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a ] = p->nOp;. }.
1a0ac 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
1a0ad 5f 44 45 42 55 47 20 2f 2a 20 73 71 6c 69 74 65 _DEBUG /* sqlite
1a0ae 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 3AssertMayAbort(
1a0af 29 20 6c 6f 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a ) logic */../*.*
1a0b0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1a0b1 74 79 70 65 20 61 6e 64 20 66 75 6e 63 74 69 6f type and functio
1a0b2 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 74 n are used to it
1a0b3 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c erate through al
1a0b4 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 l opcodes.** in
1a0b5 61 20 56 64 62 65 20 6d 61 69 6e 20 70 72 6f 67 a Vdbe main prog
1a0b6 72 61 6d 20 61 6e 64 20 65 61 63 68 20 6f 66 20 ram and each of
1a0b7 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 the sub-programs
1a0b8 20 28 74 72 69 67 67 65 72 73 29 20 69 74 20 6d (triggers) it m
1a0b9 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 ay .** invoke di
1a0ba 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 rectly or indire
1a0bb 63 74 6c 79 2e 20 49 74 20 73 68 6f 75 6c 64 20 ctly. It should
1a0bc 62 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f be used as follo
1a0bd 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a ws:.**.** Op *
1a0be 70 4f 70 3b 0a 2a 2a 20 20 20 56 64 62 65 4f 70 pOp;.** VdbeOp
1a0bf 49 74 65 72 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a Iter sIter;.**.*
1a0c0 2a 20 20 20 6d 65 6d 73 65 74 28 26 73 49 74 65 * memset(&sIte
1a0c1 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 r, 0, sizeof(sIt
1a0c2 65 72 29 29 3b 0a 2a 2a 20 20 20 73 49 74 65 72 er));.** sIter
1a0c3 2e 76 20 3d 20 76 3b 20 20 20 20 20 20 20 20 20 .v = v;
1a0c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0c5 20 20 20 2f 2f 20 76 20 69 73 20 6f 66 20 74 79 // v is of ty
1a0c6 70 65 20 56 64 62 65 2a 20 0a 2a 2a 20 20 20 77 pe Vdbe* .** w
1a0c7 68 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 hile( (pOp = opI
1a0c8 74 65 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 terNext(&sIter))
1a0c9 20 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 44 6f ){.** // Do
1a0ca 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20 something with
1a0cb 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 pOp.** }.**
1a0cc 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d sqlite3DbFree(v-
1a0cd 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 >db, sIter.apSub
1a0ce 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 );.** .*/.typede
1a0cf 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 49 f struct VdbeOpI
1a0d0 74 65 72 20 56 64 62 65 4f 70 49 74 65 72 3b 0a ter VdbeOpIter;.
1a0d1 73 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 struct VdbeOpIte
1a0d2 72 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 r {. Vdbe *v;
1a0d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0d4 20 2f 2a 20 56 64 62 65 20 74 6f 20 69 74 65 72 /* Vdbe to iter
1a0d5 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 ate through the
1a0d6 6f 70 63 6f 64 65 73 20 6f 66 20 2a 2f 0a 20 20 opcodes of */.
1a0d7 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 61 70 53 SubProgram **apS
1a0d8 75 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 ub; /* Ar
1a0d9 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67 72 61 ray of subprogra
1a0da 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 ms */. int nSub
1a0db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1a0dc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1a0dd 65 6e 74 72 69 65 73 20 69 6e 20 61 70 53 75 62 entries in apSub
1a0de 20 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64 72 3b */. int iAddr;
1a0df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a0e0 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e /* Address of n
1a0e1 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1a0e2 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 to return */. i
1a0e3 6e 74 20 69 53 75 62 3b 20 20 20 20 20 20 20 20 nt iSub;
1a0e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 3d /* 0 =
1a0e5 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2c 20 31 main program, 1
1a0e6 20 3d 20 66 69 72 73 74 20 73 75 62 2d 70 72 6f = first sub-pro
1a0e7 67 72 61 6d 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a gram etc. */.};.
1a0e8 73 74 61 74 69 63 20 4f 70 20 2a 6f 70 49 74 65 static Op *opIte
1a0e9 72 4e 65 78 74 28 56 64 62 65 4f 70 49 74 65 72 rNext(VdbeOpIter
1a0ea 20 2a 70 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 *p){. Vdbe *v
1a0eb 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 = p->v;. Op *pR
1a0ec 65 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 61 4f et = 0;. Op *aO
1a0ed 70 3b 0a 20 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 p;. int nOp;..
1a0ee 20 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d if( p->iSub<=p-
1a0ef 3e 6e 53 75 62 20 29 7b 0a 0a 20 20 20 20 69 66 >nSub ){.. if
1a0f0 28 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a ( p->iSub==0 ){.
1a0f1 20 20 20 20 20 20 61 4f 70 20 3d 20 76 2d 3e 61 aOp = v->a
1a0f2 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 Op;. nOp =
1a0f3 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 v->nOp;. }els
1a0f4 65 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 70 e{. aOp = p
1a0f5 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d ->apSub[p->iSub-
1a0f6 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 1]->aOp;. n
1a0f7 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d Op = p->apSub[p-
1a0f8 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 >iSub-1]->nOp;.
1a0f9 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1a0fa 20 70 2d 3e 69 41 64 64 72 3c 6e 4f 70 20 29 3b p->iAddr<nOp );
1a0fb 0a 0a 20 20 20 20 70 52 65 74 20 3d 20 26 61 4f .. pRet = &aO
1a0fc 70 5b 70 2d 3e 69 41 64 64 72 5d 3b 0a 20 20 20 p[p->iAddr];.
1a0fd 20 70 2d 3e 69 41 64 64 72 2b 2b 3b 0a 20 20 20 p->iAddr++;.
1a0fe 20 69 66 28 20 70 2d 3e 69 41 64 64 72 3d 3d 6e if( p->iAddr==n
1a0ff 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 Op ){. p->i
1a100 53 75 62 2b 2b 3b 0a 20 20 20 20 20 20 70 2d 3e Sub++;. p->
1a101 69 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 7d iAddr = 0;. }
1a102 0a 20 20 0a 20 20 20 20 69 66 28 20 70 52 65 74 . . if( pRet
1a103 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 ->p4type==P4_SUB
1a104 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 PROGRAM ){.
1a105 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 70 2d int nByte = (p-
1a106 3e 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 >nSub+1)*sizeof(
1a107 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 SubProgram*);.
1a108 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 int j;.
1a109 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e for(j=0; j<p->n
1a10a 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Sub; j++){.
1a10b 20 20 20 69 66 28 20 70 2d 3e 61 70 53 75 62 5b if( p->apSub[
1a10c 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34 2e 70 50 72 j]==pRet->p4.pPr
1a10d 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 ogram ) break;.
1a10e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
1a10f 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 j==p->nSub ){.
1a110 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 20 p->apSub
1a111 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
1a112 6f 63 4f 72 46 72 65 65 28 76 2d 3e 64 62 2c 20 ocOrFree(v->db,
1a113 70 2d 3e 61 70 53 75 62 2c 20 6e 42 79 74 65 29 p->apSub, nByte)
1a114 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 ;. if( !p
1a115 2d 3e 61 70 53 75 62 20 29 7b 0a 20 20 20 20 20 ->apSub ){.
1a116 20 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 pRet = 0;.
1a117 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1a118 20 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 p->apSub
1a119 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 [p->nSub++] = pR
1a11a 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b et->p4.pProgram;
1a11b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1a11c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
1a11d 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a return pRet;.}..
1a11e 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 /*.** Check if t
1a11f 68 65 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 he program store
1a120 64 20 69 6e 20 74 68 65 20 56 4d 20 61 73 73 6f d in the VM asso
1a121 63 69 61 74 65 64 20 77 69 74 68 20 70 50 61 72 ciated with pPar
1a122 73 65 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 se may.** throw
1a123 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 an ABORT excepti
1a124 6f 6e 20 28 63 61 75 73 69 6e 67 20 74 68 65 20 on (causing the
1a125 73 74 61 74 65 6d 65 6e 74 2c 20 62 75 74 20 6e statement, but n
1a126 6f 74 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 ot entire transa
1a127 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 ction.** to be r
1a128 6f 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54 68 69 olled back). Thi
1a129 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 s condition is t
1a12a 72 75 65 20 69 66 20 74 68 65 20 6d 61 69 6e 20 rue if the main
1a12b 70 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a program or any.*
1a12c 2a 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 * sub-programs c
1a12d 6f 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 ontains any of t
1a12e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a he following:.**
1a12f 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 .** * OP_Halt
1a130 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f with P1=SQLITE_
1a131 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 CONSTRAINT and P
1a132 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 2=OE_Abort..**
1a133 20 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c * OP_HaltIfNul
1a134 6c 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 l with P1=SQLITE
1a135 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 _CONSTRAINT and
1a136 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 P2=OE_Abort..**
1a137 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a * OP_Destroy.
1a138 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 ** * OP_VUpda
1a139 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 te.** * OP_VR
1a13a 65 6e 61 6d 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 ename.** * OP
1a13b 5f 46 6b 43 6f 75 6e 74 65 72 20 77 69 74 68 20 _FkCounter with
1a13c 50 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61 74 65 P2==0 (immediate
1a13d 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e foreign key con
1a13e 73 74 72 61 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 straint).**.** T
1a13f 68 65 6e 20 63 68 65 63 6b 20 74 68 61 74 20 74 hen check that t
1a140 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 72 73 he value of Pars
1a141 65 2e 6d 61 79 41 62 6f 72 74 20 69 73 20 74 72 e.mayAbort is tr
1a142 75 65 20 69 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 ue if an.** ABOR
1a143 54 20 6d 61 79 20 62 65 20 74 68 72 6f 77 6e 2c T may be thrown,
1a144 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 or false otherw
1a145 69 73 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 ise. Return true
1a146 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6d if it does.** m
1a147 61 74 63 68 2c 20 6f 72 20 66 61 6c 73 65 20 6f atch, or false o
1a148 74 68 65 72 77 69 73 65 2e 20 54 68 69 73 20 66 therwise. This f
1a149 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 74 65 6e unction is inten
1a14a 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 61 ded to be used a
1a14b 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e 20 s.** part of an
1a14c 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 assert statement
1a14d 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 in the compiler
1a14e 2e 20 53 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a . Similar to:.**
1a14f 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 73 71 .** assert( sq
1a150 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d lite3VdbeAssertM
1a151 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 2d 3e ayAbort(pParse->
1a152 70 56 64 62 65 2c 20 70 50 61 72 73 65 2d 3e 6d pVdbe, pParse->m
1a153 61 79 41 62 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 ayAbort) );.*/.S
1a154 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1a155 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 t sqlite3VdbeAss
1a156 65 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 ertMayAbort(Vdbe
1a157 20 2a 76 2c 20 69 6e 74 20 6d 61 79 41 62 6f 72 *v, int mayAbor
1a158 74 29 7b 0a 20 20 69 6e 74 20 68 61 73 41 62 6f t){. int hasAbo
1a159 72 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f rt = 0;. Op *pO
1a15a 70 3b 0a 20 20 56 64 62 65 4f 70 49 74 65 72 20 p;. VdbeOpIter
1a15b 73 49 74 65 72 3b 0a 20 20 6d 65 6d 73 65 74 28 sIter;. memset(
1a15c 26 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f &sIter, 0, sizeo
1a15d 66 28 73 49 74 65 72 29 29 3b 0a 20 20 73 49 74 f(sIter));. sIt
1a15e 65 72 2e 76 20 3d 20 76 3b 0a 0a 20 20 77 68 69 er.v = v;.. whi
1a15f 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 le( (pOp = opIte
1a160 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 21 3d rNext(&sIter))!=
1a161 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 63 0 ){. int opc
1a162 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 ode = pOp->opcod
1a163 65 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64 e;. if( opcod
1a164 65 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 7c 7c e==OP_Destroy ||
1a165 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 opcode==OP_VUpd
1a166 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f ate || opcode==O
1a167 50 5f 56 52 65 6e 61 6d 65 20 0a 23 69 66 6e 64 P_VRename .#ifnd
1a168 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
1a169 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 OREIGN_KEY.
1a16a 7c 7c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 || (opcode==OP_F
1a16b 6b 43 6f 75 6e 74 65 72 20 26 26 20 70 4f 70 2d kCounter && pOp-
1a16c 3e 70 31 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1==0 && pOp->p
1a16d 32 3d 3d 31 29 20 0a 23 65 6e 64 69 66 0a 20 20 2==1) .#endif.
1a16e 20 20 20 7c 7c 20 28 28 6f 70 63 6f 64 65 3d 3d || ((opcode==
1a16f 4f 50 5f 48 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 OP_Halt || opcod
1a170 65 3d 3d 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c e==OP_HaltIfNull
1a171 29 20 0a 20 20 20 20 20 20 26 26 20 28 70 4f 70 ) . && (pOp
1a172 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e ->p1==SQLITE_CON
1a173 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e STRAINT && pOp->
1a174 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 29 29 0a 20 p2==OE_Abort)).
1a175 20 20 20 29 7b 0a 20 20 20 20 20 20 68 61 73 41 ){. hasA
1a176 62 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 bort = 1;.
1a177 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
1a178 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1a179 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 (v->db, sIter.ap
1a17a 53 75 62 29 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 Sub);.. /* Retu
1a17b 72 6e 20 74 72 75 65 20 69 66 20 68 61 73 41 62 rn true if hasAb
1a17c 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f ort==mayAbort. O
1a17d 72 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 r if a malloc fa
1a17e 69 6c 75 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 ilure occured..
1a17f 20 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61 ** If malloc fa
1a180 69 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 77 iled, then the w
1a181 68 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76 hile() loop abov
1a182 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 69 e may not have i
1a183 74 65 72 61 74 65 64 0a 20 20 2a 2a 20 74 68 72 terated. ** thr
1a184 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 ough all opcodes
1a185 20 61 6e 64 20 68 61 73 41 62 6f 72 74 20 6d 61 and hasAbort ma
1a186 79 20 62 65 20 73 65 74 20 69 6e 63 6f 72 72 65 y be set incorre
1a187 63 74 6c 79 2e 20 52 65 74 75 72 6e 0a 20 20 2a ctly. Return. *
1a188 2a 20 74 72 75 65 20 66 6f 72 20 74 68 69 73 20 * true for this
1a189 63 61 73 65 20 74 6f 20 70 72 65 76 65 6e 74 20 case to prevent
1a18a 74 68 65 20 61 73 73 65 72 74 28 29 20 69 6e 20 the assert() in
1a18b 74 68 65 20 63 61 6c 6c 65 72 73 20 66 72 61 6d the callers fram
1a18c 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 66 61 69 6c e. ** from fail
1a18d 69 6e 67 2e 20 20 2a 2f 0a 20 20 72 65 74 75 72 ing. */. retur
1a18e 6e 20 28 20 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f n ( v->db->mallo
1a18f 63 46 61 69 6c 65 64 20 7c 7c 20 68 61 73 41 62 cFailed || hasAb
1a190 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 20 29 3b ort==mayAbort );
1a191 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1a192 49 54 45 5f 44 45 42 55 47 20 2d 20 74 68 65 20 ITE_DEBUG - the
1a193 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d 61 79 sqlite3AssertMay
1a194 41 62 6f 72 74 28 29 20 66 75 6e 63 74 69 6f 6e Abort() function
1a195 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 */../*.** Loop
1a196 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 through the prog
1a197 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 ram looking for
1a198 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 P2 values that a
1a199 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f re negative.** o
1a19a 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 n jump instructi
1a19b 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 ons. Each such
1a19c 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c value is a label
1a19d 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a . Resolve the.*
1a19e 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 * label by setti
1a19f 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 ng the P2 value
1a1a0 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e to its correct n
1a1a1 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a on-zero value..*
1a1a2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1a1a3 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 e is called once
1a1a4 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 after all opcod
1a1a5 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 es have been ins
1a1a6 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 erted..**.** Var
1a1a7 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 iable *pMaxFuncA
1a1a8 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 rgs is set to th
1a1a9 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 e maximum value
1a1aa 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 of any P2 argume
1a1ab 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f nt .** to an OP_
1a1ac 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 Function, OP_Agg
1a1ad 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 Step or OP_VFilt
1a1ae 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 er opcode. This
1a1af 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 is used by .** s
1a1b0 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 qlite3VdbeMakeRe
1a1b1 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 ady() to size th
1a1b2 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 e Vdbe.apArg[] a
1a1b3 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rray..**.** The
1a1b4 4f 70 2e 6f 70 66 6c 61 67 73 20 66 69 65 6c 64 Op.opflags field
1a1b5 20 69 73 20 73 65 74 20 6f 6e 20 61 6c 6c 20 6f is set on all o
1a1b6 70 63 6f 64 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 pcodes..*/.stati
1a1b7 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 c void resolveP2
1a1b8 56 61 6c 75 65 73 28 56 64 62 65 20 2a 70 2c 20 Values(Vdbe *p,
1a1b9 69 6e 74 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 int *pMaxFuncArg
1a1ba 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 s){. int i;. i
1a1bb 6e 74 20 6e 4d 61 78 41 72 67 73 20 3d 20 2a 70 nt nMaxArgs = *p
1a1bc 4d 61 78 46 75 6e 63 41 72 67 73 3b 0a 20 20 4f MaxFuncArgs;. O
1a1bd 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 2a 61 p *pOp;. int *a
1a1be 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 65 Label = p->aLabe
1a1bf 6c 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 l;. p->readOnly
1a1c0 20 3d 20 31 3b 0a 20 20 66 6f 72 28 70 4f 70 3d = 1;. for(pOp=
1a1c1 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 p->aOp, i=p->nOp
1a1c2 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 -1; i>=0; i--, p
1a1c3 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70 Op++){. u8 op
1a1c4 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f code = pOp->opco
1a1c5 64 65 3b 0a 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 de;.. pOp->op
1a1c6 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 4f flags = sqlite3O
1a1c7 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 pcodeProperty[op
1a1c8 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 20 6f code];. if( o
1a1c9 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 pcode==OP_Functi
1a1ca 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 on || opcode==OP
1a1cb 5f 41 67 67 53 74 65 70 20 29 7b 0a 20 20 20 20 _AggStep ){.
1a1cc 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d if( pOp->p5>nM
1a1cd 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 axArgs ) nMaxArg
1a1ce 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 20 s = pOp->p5;.
1a1cf 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 }else if( opcod
1a1d0 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f e==OP_Transactio
1a1d1 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d 30 20 n && pOp->p2!=0
1a1d2 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61 64 ){. p->read
1a1d3 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64 65 Only = 0;.#ifnde
1a1d4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1a1d5 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d RTUALTABLE. }
1a1d6 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d else if( opcode=
1a1d7 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 =OP_VUpdate ){.
1a1d8 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 if( pOp->p2
1a1d9 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 >nMaxArgs ) nMax
1a1da 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a Args = pOp->p2;.
1a1db 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 }else if( op
1a1dc 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 code==OP_VFilter
1a1dd 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b ){. int n;
1a1de 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1a1df 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 ->nOp - i >= 3 )
1a1e0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a1e1 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d pOp[-1].opcode==
1a1e2 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 OP_Integer );.
1a1e3 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e n = pOp[-1].
1a1e4 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e p1;. if( n>
1a1e5 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 nMaxArgs ) nMaxA
1a1e6 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a rgs = n;.#endif.
1a1e7 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 28 }.. if( (
1a1e8 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f pOp->opflags & O
1a1e9 50 46 4c 47 5f 4a 55 4d 50 29 21 3d 30 20 26 26 PFLG_JUMP)!=0 &&
1a1ea 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 pOp->p2<0 ){.
1a1eb 20 20 20 20 61 73 73 65 72 74 28 20 2d 31 2d 70 assert( -1-p
1a1ec 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c Op->p2<p->nLabel
1a1ed 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 );. pOp->p
1a1ee 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 2 = aLabel[-1-pO
1a1ef 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 p->p2];. }.
1a1f0 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 }. sqlite3DbFre
1a1f1 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 e(p->db, p->aLab
1a1f2 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c el);. p->aLabel
1a1f3 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 = 0;.. *pMaxFu
1a1f4 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 ncArgs = nMaxArg
1a1f5 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 s;.}../*.** Retu
1a1f6 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f rn the address o
1a1f7 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 f the next instr
1a1f8 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 uction to be ins
1a1f9 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 erted..*/.SQLITE
1a1fa 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a1fb 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
1a1fc 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ddr(Vdbe *p){.
1a1fd 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1a1fe 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
1a1ff 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d T );. return p-
1a200 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 >nOp;.}../*.** T
1a201 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
1a202 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
1a203 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f o the array of o
1a204 70 63 6f 64 65 73 20 61 73 73 6f 63 69 61 74 65 pcodes associate
1a205 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 d with.** the Vd
1a206 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 be passed as the
1a207 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
1a208 20 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65 It is the calle
1a209 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 rs responsibilit
1a20a 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 y.** to arrange
1a20b 66 6f 72 20 74 68 65 20 72 65 74 75 72 6e 65 64 for the returned
1a20c 20 61 72 72 61 79 20 74 6f 20 62 65 20 65 76 65 array to be eve
1a20d 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20 75 73 ntually freed us
1a20e 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 ing the .** vdbe
1a20f 46 72 65 65 4f 70 41 72 72 61 79 28 29 20 66 75 FreeOpArray() fu
1a210 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 nction..**.** Be
1a211 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 fore returning,
1a212 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 74 6f 20 *pnOp is set to
1a213 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e the number of en
1a214 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 65 74 tries in the ret
1a215 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 urned.** array.
1a216 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 Also, *pnMaxArg
1a217 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6c 61 is set to the la
1a218 72 67 65 72 20 6f 66 20 69 74 73 20 63 75 72 72 rger of its curr
1a219 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a ent value and .*
1a21a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
1a21b 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 56 entries in the V
1a21c 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 dbe.apArg[] arra
1a21d 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 78 y required to ex
1a21e 65 63 75 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 ecute the .** re
1a21f 74 75 72 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a turned program..
1a220 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a221 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 E VdbeOp *sqlite
1a222 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 3VdbeTakeOpArray
1a223 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 (Vdbe *p, int *p
1a224 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 nOp, int *pnMaxA
1a225 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 rg){. VdbeOp *a
1a226 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 Op = p->aOp;. a
1a227 73 73 65 72 74 28 20 61 4f 70 20 26 26 20 21 70 ssert( aOp && !p
1a228 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a229 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 ed );.. /* Chec
1a22a 6b 20 74 68 61 74 20 73 71 6c 69 74 65 33 56 64 k that sqlite3Vd
1a22b 62 65 55 73 65 73 42 74 72 65 65 28 29 20 77 61 beUsesBtree() wa
1a22c 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 s not called on
1a22d 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 this VM */. ass
1a22e 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e ert( p->aMutex.n
1a22f 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 Mutex==0 );.. r
1a230 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70 esolveP2Values(p
1a231 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a , pnMaxArg);. *
1a232 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 pnOp = p->nOp;.
1a233 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 p->aOp = 0;. r
1a234 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a eturn aOp;.}../*
1a235 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 .** Add a whole
1a236 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f list of operatio
1a237 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 ns to the operat
1a238 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 ion stack. Retu
1a239 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 rn the.** addres
1a23a 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f s of the first o
1a23b 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a peration added..
1a23c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a23d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1a23e 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 eAddOpList(Vdbe
1a23f 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 *p, int nOp, Vdb
1a240 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 eOpList const *a
1a241 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b Op){. int addr;
1a242 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
1a243 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1a244 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d INIT );. if( p-
1a245 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e >nOp + nOp > p->
1a246 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 nOpAlloc && grow
1a247 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 OpArray(p) ){.
1a248 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1a249 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b addr = p->nOp;
1a24a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f . if( ALWAYS(nO
1a24b 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 p>0) ){. int
1a24c 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73 i;. VdbeOpLis
1a24d 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 t const *pIn = a
1a24e 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b Op;. for(i=0;
1a24f 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e i<nOp; i++, pIn
1a250 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 ++){. int p
1a251 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 2 = pIn->p2;.
1a252 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 VdbeOp *pOut
1a253 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 = &p->aOp[i+addr
1a254 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f ];. pOut->o
1a255 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 pcode = pIn->opc
1a256 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d ode;. pOut-
1a257 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 >p1 = pIn->p1;.
1a258 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 26 26 if( p2<0 &&
1a259 20 28 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 (sqlite3OpcodeP
1a25a 72 6f 70 65 72 74 79 5b 70 4f 75 74 2d 3e 6f 70 roperty[pOut->op
1a25b 63 6f 64 65 5d 20 26 20 4f 50 46 4c 47 5f 4a 55 code] & OPFLG_JU
1a25c 4d 50 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 MP)!=0 ){.
1a25d 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 pOut->p2 = add
1a25e 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 r + ADDR(p2);.
1a25f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1a260 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 pOut->p2 = p2
1a261 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1a262 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e pOut->p3 = pIn->
1a263 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e p3;. pOut->
1a264 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 p4type = P4_NOTU
1a265 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d SED;. pOut-
1a266 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 >p4.p = 0;.
1a267 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 pOut->p5 = 0;.#
1a268 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1a269 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a UG. pOut->z
1a26a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 Comment = 0;.
1a26b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
1a26c 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a beAddopTrace ){.
1a26d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1a26e 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b dbePrintOp(0, i+
1a26f 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b addr, &p->aOp[i+
1a270 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a addr]);. }.
1a271 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 #endif. }.
1a272 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a p->nOp += nOp;.
1a273 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 }. return add
1a274 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e r;.}../*.** Chan
1a275 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
1a276 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 the P1 operand f
1a277 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e or a specific in
1a278 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
1a279 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
1a27a 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 eful when a larg
1a27b 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 e program is loa
1a27c 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 ded from a.** st
1a27d 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 atic array using
1a27e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1a27f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e pList but we wan
1a280 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 t to make a.** f
1a281 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 ew minor changes
1a282 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e to the program.
1a283 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a284 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a285 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 dbeChangeP1(Vdbe
1a286 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 *p, int addr, i
1a287 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 nt val){. asser
1a288 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 t( p!=0 );. ass
1a289 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a ert( addr>=0 );.
1a28a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 if( p->nOp>add
1a28b 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b r ){. p->aOp[
1a28c 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a addr].p1 = val;.
1a28d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
1a28e 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
1a28f 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 the P2 operand
1a290 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 for a specific i
1a291 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 nstruction..** T
1a292 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
1a293 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e seful for settin
1a294 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 g a jump destina
1a295 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
1a296 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a297 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 ite3VdbeChangeP2
1a298 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 (Vdbe *p, int ad
1a299 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 dr, int val){.
1a29a 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a assert( p!=0 );.
1a29b 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d assert( addr>=
1a29c 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 0 );. if( p->nO
1a29d 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d p>addr ){. p-
1a29e 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 >aOp[addr].p2 =
1a29f 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a val;. }.}../*.*
1a2a0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c * Change the val
1a2a1 75 65 20 6f 66 20 74 68 65 20 50 33 20 6f 70 65 ue of the P3 ope
1a2a2 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 rand for a speci
1a2a3 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e fic instruction.
1a2a4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a2a5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a2a6 64 62 65 43 68 61 6e 67 65 50 33 28 56 64 62 65 dbeChangeP3(Vdbe
1a2a7 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 *p, int addr, i
1a2a8 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 nt val){. asser
1a2a9 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 t( p!=0 );. ass
1a2aa 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a ert( addr>=0 );.
1a2ab 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 if( p->nOp>add
1a2ac 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b r ){. p->aOp[
1a2ad 61 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a addr].p3 = val;.
1a2ae 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
1a2af 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
1a2b0 20 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20 the P5 operand
1a2b1 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 for the most rec
1a2b2 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f ently.** added o
1a2b3 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c peration..*/.SQL
1a2b4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a2b5 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a2b6 67 65 50 35 28 56 64 62 65 20 2a 70 2c 20 75 38 geP5(Vdbe *p, u8
1a2b7 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 val){. assert(
1a2b8 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 p!=0 );. if( p
1a2b9 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 ->aOp ){. ass
1a2ba 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b ert( p->nOp>0 );
1a2bb 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e . p->aOp[p->n
1a2bc 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a Op-1].p5 = val;.
1a2bd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
1a2be 6e 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 nge the P2 opera
1a2bf 6e 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f nd of instructio
1a2c0 6e 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 n addr so that i
1a2c1 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 t points to.** t
1a2c2 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 he address of th
1a2c3 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1a2c4 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a on to be coded..
1a2c5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a2c6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a2c7 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 beJumpHere(Vdbe
1a2c8 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 *p, int addr){.
1a2c9 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a2ca 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d geP2(p, addr, p-
1a2cb 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a >nOp);.}.../*.**
1a2cc 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 If the input Fu
1a2cd 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 ncDef structure
1a2ce 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 is ephemeral, th
1a2cf 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a en free it. If.
1a2d0 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 ** the FuncDef i
1a2d1 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 s not ephermal,
1a2d2 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e then do nothing.
1a2d3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a2d4 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e freeEphemeralFun
1a2d5 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 ction(sqlite3 *d
1a2d6 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 b, FuncDef *pDef
1a2d7 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 ){. if( ALWAYS(
1a2d8 70 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e pDef) && (pDef->
1a2d9 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 flags & SQLITE_F
1a2da 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b UNC_EPHEM)!=0 ){
1a2db 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1a2dc 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 ee(db, pDef);.
1a2dd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 }.}../*.** Delet
1a2de 65 20 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 e a P4 value if
1a2df 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 necessary..*/.st
1a2e0 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 atic void freeP4
1a2e1 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
1a2e2 74 20 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a t p4type, void *
1a2e3 70 34 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b p4){. if( p4 ){
1a2e4 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 34 74 . switch( p4t
1a2e5 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 ype ){. cas
1a2e6 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 e P4_REAL:.
1a2e7 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a case P4_INT64:.
1a2e8 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 case P4_MP
1a2e9 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 RINTF:. cas
1a2ea 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 e P4_DYNAMIC:.
1a2eb 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 case P4_KEYI
1a2ec 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 NFO:. case
1a2ed 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 P4_INTARRAY:.
1a2ee 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
1a2ef 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 FO_HANDOFF: {.
1a2f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1a2f1 72 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 ree(db, p4);.
1a2f2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a2f3 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 }. case P
1a2f4 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 4_VDBEFUNC: {.
1a2f5 20 20 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a VdbeFunc *
1a2f6 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 pVdbeFunc = (Vdb
1a2f7 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 eFunc *)p4;.
1a2f8 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 freeEphemera
1a2f9 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 lFunction(db, pV
1a2fa 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b dbeFunc->pFunc);
1a2fb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a2fc 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 VdbeDeleteAuxDat
1a2fd 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b a(pVdbeFunc, 0);
1a2fe 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a2ff 44 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 DbFree(db, pVdbe
1a300 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 Func);. b
1a301 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
1a302 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 case P4_FUNC
1a303 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 DEF: {. f
1a304 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 reeEphemeralFunc
1a305 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 tion(db, (FuncDe
1a306 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 f*)p4);.
1a307 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1a308 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d case P4_MEM
1a309 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 : {. sqli
1a30a 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 te3ValueFree((sq
1a30b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 lite3_value*)p4)
1a30c 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
1a30d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
1a30e 61 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a ase P4_VTAB : {.
1a30f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1a310 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c tabUnlock((VTabl
1a311 65 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 e *)p4);.
1a312 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1a313 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 case P4_SU
1a314 42 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 BPROGRAM : {.
1a315 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1a316 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 ProgramDelete(db
1a317 2c 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 , (SubProgram *)
1a318 70 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 p4, 1);.
1a319 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1a31a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1a31b 2a 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65 * Free the space
1a31c 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 allocated for a
1a31d 4f 70 20 61 6e 64 20 61 6e 79 20 70 34 20 76 61 Op and any p4 va
1a31e 6c 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 lues allocated f
1a31f 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 or the.** opcode
1a320 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 s contained with
1a321 69 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 6e 6f in. If aOp is no
1a322 74 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 73 73 t NULL it is ass
1a323 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 umed to contain
1a324 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e .** nOp entries.
1a325 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 .*/.static void
1a326 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 vdbeFreeOpArray
1a327 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 (sqlite3 *db, Op
1a328 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b *aOp, int nOp){
1a329 0a 20 20 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 . if( aOp ){.
1a32a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 Op *pOp;. f
1a32b 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c or(pOp=aOp; pOp<
1a32c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b &aOp[nOp]; pOp++
1a32d 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 ){. freeP4(
1a32e 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c db, pOp->p4type,
1a32f 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 pOp->p4.p);.#if
1a330 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1a331 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1a332 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 Free(db, pOp->zC
1a333 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 omment);.#endif
1a334 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 . }. }.
1a335 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1a336 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a b, aOp);.}../*.*
1a337 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 * Decrement the
1a338 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 ref-count on the
1a339 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 SubProgram stru
1a33a 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 cture passed as
1a33b 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 the.** second ar
1a33c 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 72 gument. If the r
1a33d 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 ef-count reaches
1a33e 20 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 65 20 zero, free the
1a33f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
1a340 20 54 68 65 20 61 72 72 61 79 20 6f 66 20 56 44 The array of VD
1a341 42 45 20 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 BE opcodes store
1a342 64 20 61 73 20 53 75 62 50 72 6f 67 72 61 6d 2e d as SubProgram.
1a343 61 4f 70 20 69 73 20 66 72 65 65 64 20 69 66 0a aOp is freed if.
1a344 2a 2a 20 65 69 74 68 65 72 20 74 68 65 20 72 65 ** either the re
1a345 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 f-count reaches
1a346 7a 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 zero or paramete
1a347 72 20 66 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d r freeop is non-
1a348 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 zero..**.** Sinc
1a349 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f e the array of o
1a34a 70 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 20 74 pcodes pointed t
1a34b 6f 20 62 79 20 53 75 62 50 72 6f 67 72 61 6d 2e o by SubProgram.
1a34c 61 4f 70 20 6d 61 79 20 64 69 72 65 63 74 6c 79 aOp may directly
1a34d 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 74 6c .** or indirectl
1a34e 79 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 y contain a refe
1a34f 72 65 6e 63 65 20 74 6f 20 74 68 65 20 53 75 62 rence to the Sub
1a350 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 Program structur
1a351 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 e itself..** By
1a352 70 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 passing a non-ze
1a353 72 6f 20 66 72 65 65 6f 70 20 70 61 72 61 6d 65 ro freeop parame
1a354 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 ter, the caller
1a355 6d 61 79 20 65 6e 73 75 72 65 20 74 68 61 74 20 may ensure that
1a356 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 all.** SubProgra
1a357 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64 m structures and
1a358 20 74 68 65 69 72 20 61 4f 70 20 61 72 72 61 79 their aOp array
1a359 73 20 61 72 65 20 66 72 65 65 64 2c 20 65 76 65 s are freed, eve
1a35a 6e 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 n when there.**
1a35b 61 72 65 20 73 75 63 68 20 63 69 72 63 75 6c 61 are such circula
1a35c 72 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f r references..*/
1a35d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a35e 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a35f 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 73 71 ProgramDelete(sq
1a360 6c 69 74 65 33 20 2a 64 62 2c 20 53 75 62 50 72 lite3 *db, SubPr
1a361 6f 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 ogram *p, int fr
1a362 65 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 20 29 eeop){. if( p )
1a363 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d {. assert( p-
1a364 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 >nRef>0 );. i
1a365 66 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e f( freeop || p->
1a366 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 nRef==1 ){.
1a367 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f Op *aOp = p->aO
1a368 70 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 p;. p->aOp
1a369 3d 20 30 3b 0a 20 20 20 20 20 20 76 64 62 65 46 = 0;. vdbeF
1a36a 72 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 61 reeOpArray(db, a
1a36b 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 Op, p->nOp);.
1a36c 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 p->nOp = 0;.
1a36d 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 }. p->nRef
1a36e 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e --;. if( p->n
1a36f 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ref==0 ){.
1a370 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1a371 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , p);. }. }.
1a372 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 }.../*.** Change
1a373 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72 74 N opcodes start
1a374 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20 4e ing at addr to N
1a375 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 o-ops..*/.SQLITE
1a376 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a377 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 lite3VdbeChangeT
1a378 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 oNoop(Vdbe *p, i
1a379 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b nt addr, int N){
1a37a 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b . if( p->aOp ){
1a37b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 . VdbeOp *pOp
1a37c 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d = &p->aOp[addr]
1a37d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 ;. sqlite3 *d
1a37e 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 b = p->db;. w
1a37f 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 hile( N-- ){.
1a380 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f freeP4(db, pO
1a381 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e p->p4type, pOp->
1a382 70 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d p4.p);. mem
1a383 73 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 set(pOp, 0, size
1a384 6f 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 of(pOp[0]));.
1a385 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d pOp->opcode =
1a386 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 OP_Noop;.
1a387 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d pOp++;. }. }
1a388 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 .}../*.** Change
1a389 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1a38a 65 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 e P4 operand for
1a38b 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 a specific inst
1a38c 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 ruction..** This
1a38d 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 routine is usef
1a38e 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 ul when a large
1a38f 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 program is loade
1a390 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 d from a.** stat
1a391 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 ic array using s
1a392 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c qlite3VdbeAddOpL
1a393 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 ist but we want
1a394 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 to make a.** few
1a395 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 minor changes t
1a396 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a o the program..*
1a397 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 *.** If n>=0 the
1a398 6e 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 n the P4 operand
1a399 20 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 is dynamic, mea
1a39a 6e 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 ning that a copy
1a39b 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e of.** the strin
1a39c 67 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d g is made into m
1a39d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
1a39e 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
1a39f 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 oc()..** A value
1a3a0 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 of n==0 means c
1a3a1 6f 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 opy bytes of zP4
1a3a2 20 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 up to and inclu
1a3a3 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 ding the.** firs
1a3a4 74 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 t null byte. If
1a3a5 20 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e n>0 then copy n
1a3a6 2b 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e +1 bytes of zP4.
1a3a7 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f .**.** If n==P4_
1a3a8 4b 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 KEYINFO it means
1a3a9 20 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 that zP4 is a p
1a3aa 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 ointer to a KeyI
1a3ab 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a nfo structure..*
1a3ac 2a 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 * A copy is made
1a3ad 20 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 of the KeyInfo
1a3ae 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d structure into m
1a3af 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
1a3b0 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d rom.** sqlite3_m
1a3b1 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 alloc, to be fre
1a3b2 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 ed when the Vdbe
1a3b3 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a is finalized..*
1a3b4 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f * n==P4_KEYINFO_
1a3b5 48 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 HANDOFF indicate
1a3b6 73 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 s that zP4 point
1a3b7 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 s to a KeyInfo s
1a3b8 74 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 tructure.** stor
1a3b9 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 ed in memory tha
1a3ba 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 t the caller has
1a3bb 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
1a3bc 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 qlite3_malloc. T
1a3bd 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 he .** caller sh
1a3be 6f 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 ould not free th
1a3bf 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 e allocation, it
1a3c0 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 will be freed w
1a3c1 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a hen the Vdbe is.
1a3c2 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a ** finalized..**
1a3c3 20 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 .** Other value
1a3c4 73 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 s of n (P4_STATI
1a3c5 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 C, P4_COLLSEQ et
1a3c6 63 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 c.) indicate tha
1a3c7 74 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 t zP4 points.**
1a3c8 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 to a string or s
1a3c9 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 tructure that is
1a3ca 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 guaranteed to e
1a3cb 78 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 xist for the lif
1a3cc 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 etime of.** the
1a3cd 56 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 Vdbe. In these c
1a3ce 61 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 ases we can just
1a3cf 20 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 copy the pointe
1a3d0 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 r..**.** If addr
1a3d1 3c 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 <0 then change P
1a3d2 34 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 4 on the most re
1a3d3 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 cently inserted
1a3d4 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
1a3d5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a3d6 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
1a3d7 68 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c hangeP4(Vdbe *p,
1a3d8 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 int addr, const
1a3d9 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 char *zP4, int
1a3da 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 n){. Op *pOp;.
1a3db 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
1a3dc 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a assert( p!=0 );.
1a3dd 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 db = p->db;.
1a3de 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
1a3df 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
1a3e0 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f T );. if( p->aO
1a3e1 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c p==0 || db->mall
1a3e2 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1a3e3 69 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e if ( n!=P4_KEYIN
1a3e4 46 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 FO && n!=P4_VTAB
1a3e5 20 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65 50 ) {. freeP
1a3e6 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 4(db, n, (void*)
1a3e7 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a *(char**)&zP4);.
1a3e8 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
1a3e9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1a3ea 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 p->nOp>0 );. as
1a3eb 73 65 72 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f sert( addr<p->nO
1a3ec 70 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c p );. if( addr<
1a3ed 30 20 29 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 0 ){. addr =
1a3ee 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a p->nOp - 1;. }.
1a3ef 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b pOp = &p->aOp[
1a3f0 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 addr];. freeP4(
1a3f1 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c db, pOp->p4type,
1a3f2 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 pOp->p4.p);. p
1a3f3 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 Op->p4.p = 0;.
1a3f4 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 if( n==P4_INT32
1a3f5 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 ){. /* Note:
1a3f6 74 68 69 73 20 63 61 73 74 20 69 73 20 73 61 66 this cast is saf
1a3f7 65 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 6f e, because the o
1a3f8 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 rigin data point
1a3f9 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 was an int.
1a3fa 2a 2a 20 74 68 61 74 20 77 61 73 20 63 61 73 74 ** that was cast
1a3fb 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 68 61 to a (const cha
1a3fc 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 r *). */. pOp
1a3fd 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f ->p4.i = SQLITE_
1a3fe 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b PTR_TO_INT(zP4);
1a3ff 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 . pOp->p4type
1a400 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d = P4_INT32;. }
1a401 65 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 else if( zP4==0
1a402 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 ){. pOp->p4.p
1a403 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 = 0;. pOp->p
1a404 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
1a405 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ED;. }else if(
1a406 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b n==P4_KEYINFO ){
1a407 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b . KeyInfo *pK
1a408 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 eyInfo;. int
1a409 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a nField, nByte;..
1a40a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b nField = ((K
1a40b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 eyInfo*)zP4)->nF
1a40c 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 ield;. nByte
1a40d 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e = sizeof(*pKeyIn
1a40e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 fo) + (nField-1)
1a40f 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f *sizeof(pKeyInfo
1a410 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 ->aColl[0]) + nF
1a411 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e ield;. pKeyIn
1a412 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c fo = sqlite3Mall
1a413 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 oc( nByte );.
1a414 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 pOp->p4.pKeyInf
1a415 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 o = pKeyInfo;.
1a416 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 if( pKeyInfo )
1a417 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 53 6f 72 {. u8 *aSor
1a418 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 tOrder;. me
1a419 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a mcpy(pKeyInfo, z
1a41a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 P4, nByte);.
1a41b 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 aSortOrder = p
1a41c 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 KeyInfo->aSortOr
1a41d 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 der;. if( a
1a41e 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 SortOrder ){.
1a41f 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 pKeyInfo->a
1a420 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 SortOrder = (uns
1a421 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b 65 igned char*)&pKe
1a422 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 yInfo->aColl[nFi
1a423 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 eld];. me
1a424 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 mcpy(pKeyInfo->a
1a425 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 SortOrder, aSort
1a426 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a Order, nField);.
1a427 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f }. pO
1a428 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b p->p4type = P4_K
1a429 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 EYINFO;. }els
1a42a 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e e{. p->db->
1a42b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
1a42c 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 ;. pOp->p4t
1a42d 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 ype = P4_NOTUSED
1a42e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
1a42f 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 if( n==P4_KEYINF
1a430 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 O_HANDOFF ){.
1a431 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f pOp->p4.p = (vo
1a432 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 id*)zP4;. pOp
1a433 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 ->p4type = P4_KE
1a434 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 YINFO;. }else i
1a435 66 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b f( n==P4_VTAB ){
1a436 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d . pOp->p4.p =
1a437 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 (void*)zP4;.
1a438 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
1a439 34 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 4_VTAB;. sqli
1a43a 74 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 te3VtabLock((VTa
1a43b 62 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 ble *)zP4);.
1a43c 61 73 73 65 72 74 28 20 28 28 56 54 61 62 6c 65 assert( ((VTable
1a43d 20 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e *)zP4)->db==p->
1a43e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 db );. }else if
1a43f 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 ( n<0 ){. pOp
1a440 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 ->p4.p = (void*)
1a441 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 zP4;. pOp->p4
1a442 74 79 70 65 20 3d 20 28 73 69 67 6e 65 64 20 63 type = (signed c
1a443 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a har)n;. }else{.
1a444 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e if( n==0 ) n
1a445 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
1a446 33 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 30(zP4);. pOp
1a447 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 ->p4.z = sqlite3
1a448 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c DbStrNDup(p->db,
1a449 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f zP4, n);. pO
1a44a 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 p->p4type = P4_D
1a44b 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 YNAMIC;. }.}..#
1a44c 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
1a44d 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 63 .** Change the c
1a44e 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 omment on the th
1a44f 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
1a450 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f coded instructio
1a451 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 n. Or.** insert
1a452 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 a No-op and add
1a453 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 the comment to
1a454 74 68 61 74 20 6e 65 77 20 69 6e 73 74 72 75 63 that new instruc
1a455 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d tion. This.** m
1a456 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20 65 61 akes the code ea
1a457 73 69 65 72 20 74 6f 20 72 65 61 64 20 64 75 72 sier to read dur
1a458 69 6e 67 20 64 65 62 75 67 67 69 6e 67 2e 20 20 ing debugging.
1a459 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 61 70 None of this hap
1a45a 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f pens.** in a pro
1a45b 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a duction build..*
1a45c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a45d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1a45e 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 eComment(Vdbe *p
1a45f 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
1a460 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 ormat, ...){. v
1a461 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 a_list ap;. if(
1a462 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 !p ) return;.
1a463 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
1a464 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b || p->aOp==0 );
1a465 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f . assert( p->aO
1a466 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 p==0 || p->aOp[p
1a467 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e ->nOp-1].zCommen
1a468 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d t==0 || p->db->m
1a469 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
1a46a 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 if( p->nOp ){.
1a46b 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 char **pz = &
1a46c 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d p->aOp[p->nOp-1]
1a46d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 .zComment;. v
1a46e 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
1a46f 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 mat);. sqlite
1a470 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 3DbFree(p->db, *
1a471 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 pz);. *pz = s
1a472 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 qlite3VMPrintf(p
1a473 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 ->db, zFormat, a
1a474 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 p);. va_end(a
1a475 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 p);. }.}.SQLITE
1a476 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a477 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d lite3VdbeNoopCom
1a478 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f ment(Vdbe *p, co
1a479 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 nst char *zForma
1a47a 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 t, ...){. va_li
1a47b 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 20 st ap;. if( !p
1a47c 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 ) return;. sqli
1a47d 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 70 2c te3VdbeAddOp0(p,
1a47e 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 OP_Noop);. ass
1a47f 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c ert( p->nOp>0 ||
1a480 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 p->aOp==0 );.
1a481 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d assert( p->aOp==
1a482 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 0 || p->aOp[p->n
1a483 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d Op-1].zComment==
1a484 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 0 || p->db->mall
1a485 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 ocFailed );. if
1a486 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 ( p->nOp ){.
1a487 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e char **pz = &p->
1a488 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 aOp[p->nOp-1].zC
1a489 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 omment;. va_s
1a48a 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
1a48b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
1a48c 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 Free(p->db, *pz)
1a48d 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 ;. *pz = sqli
1a48e 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 te3VMPrintf(p->d
1a48f 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b b, zFormat, ap);
1a490 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b . va_end(ap);
1a491 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f . }.}.#endif /
1a492 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a * NDEBUG */../*.
1a493 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 ** Return the op
1a494 63 6f 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e code for a given
1a495 20 61 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 address. If th
1a496 65 20 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c e address is -1,
1a497 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 then.** return
1a498 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
1a499 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 y inserted opcod
1a49a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 e..**.** If a me
1a49b 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1a49c 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
1a49d 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 ed prior to the
1a49e 63 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a calling of this.
1a49f 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e ** routine, then
1a4a0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1a4a1 64 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c dummy VdbeOp wil
1a4a2 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 l be returned.
1a4a3 54 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 That opcode.** i
1a4a4 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 s readable and w
1a4a5 72 69 74 61 62 6c 65 2c 20 62 75 74 20 69 74 20 ritable, but it
1a4a6 68 61 73 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 has no effect.
1a4a7 54 68 65 20 72 65 74 75 72 6e 20 6f 66 20 61 20 The return of a
1a4a8 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 dummy.** opcode
1a4a9 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 allows the call
1a4aa 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 to continue func
1a4ab 74 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 61 20 tioning after a
1a4ac 4f 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 6f 75 OOM fault withou
1a4ad 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 t.** having to c
1a4ae 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
1a4af 68 65 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 he return from t
1a4b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
1a4b1 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a valid pointer..
1a4b2 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 **.** About the
1a4b3 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
1a4b4 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 IT_TRACE: Norma
1a4b5 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e lly, this routin
1a4b6 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 e is never calle
1a4b7 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e d.** unless p->n
1a4b8 4f 70 3e 30 2e 20 20 54 68 69 73 20 69 73 20 62 Op>0. This is b
1a4b9 65 63 61 75 73 65 20 69 6e 20 74 68 65 20 61 62 ecause in the ab
1a4ba 73 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 45 5f sense of SQLITE_
1a4bb 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 OMIT_TRACE,.** a
1a4bc 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 74 72 n OP_Trace instr
1a4bd 75 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 uction is always
1a4be 20 69 6e 73 65 72 74 65 64 20 62 79 20 73 71 6c inserted by sql
1a4bf 69 74 65 33 56 64 62 65 47 65 74 28 29 20 61 73 ite3VdbeGet() as
1a4c0 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 soon as.** a ne
1a4c1 77 20 56 44 42 45 20 69 73 20 63 72 65 61 74 65 w VDBE is create
1a4c2 64 2e 20 20 53 6f 20 77 65 20 61 72 65 20 66 72 d. So we are fr
1a4c3 65 65 20 74 6f 20 73 65 74 20 61 64 64 72 20 74 ee to set addr t
1a4c4 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f o p->nOp-1 witho
1a4c5 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 ut.** having to
1a4c6 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 double-check to
1a4c7 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 make sure that t
1a4c8 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e he result is non
1a4c9 2d 6e 65 67 61 74 69 76 65 2e 20 42 75 74 0a 2a -negative. But.*
1a4ca 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 * if SQLITE_OMIT
1a4cb 5f 54 52 41 43 45 20 69 73 20 64 65 66 69 6e 65 _TRACE is define
1a4cc 64 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 65 20 d, the OP_Trace
1a4cd 69 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 is omitted and w
1a4ce 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 e do need to.**
1a4cf 63 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 check the value
1a4d0 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f of p->nOp-1 befo
1a4d1 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a re continuing..*
1a4d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a4d3 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 VdbeOp *sqlite3
1a4d4 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 20 2a VdbeGetOp(Vdbe *
1a4d5 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 p, int addr){.
1a4d6 73 74 61 74 69 63 20 56 64 62 65 4f 70 20 64 75 static VdbeOp du
1a4d7 6d 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 mmy;. assert( p
1a4d8 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
1a4d9 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 GIC_INIT );. if
1a4da 28 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 ( addr<0 ){.#ifd
1a4db 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1a4dc 52 41 43 45 0a 20 20 20 20 69 66 28 20 70 2d 3e RACE. if( p->
1a4dd 6e 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 nOp==0 ) return
1a4de 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 &dummy;.#endif.
1a4df 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 addr = p->nOp
1a4e0 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 - 1;. }. asse
1a4e1 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26 26 20 rt( (addr>=0 &&
1a4e2 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 addr<p->nOp) ||
1a4e3 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
1a4e4 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e led );. if( p->
1a4e5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a4e6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 ){. return &
1a4e7 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a dummy;. }else{.
1a4e8 20 20 20 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 return &p->a
1a4e9 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a Op[addr];. }.}.
1a4ea 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1a4eb 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
1a4ec 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e N) || !defined(N
1a4ed 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c DEBUG) \. ||
1a4ee 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 defined(VDBE_PR
1a4ef 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 OFILE) || define
1a4f0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a d(SQLITE_DEBUG).
1a4f1 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 /*.** Compute a
1a4f2 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63 string that desc
1a4f3 72 69 62 65 73 20 74 68 65 20 50 34 20 70 61 72 ribes the P4 par
1a4f4 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 ameter for an op
1a4f5 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 code..** Use zTe
1a4f6 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 75 69 mp for any requi
1a4f7 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 62 75 red temporary bu
1a4f8 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 ffer space..*/.s
1a4f9 74 61 74 69 63 20 63 68 61 72 20 2a 64 69 73 70 tatic char *disp
1a4fa 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 layP4(Op *pOp, c
1a4fb 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 har *zTemp, int
1a4fc 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a nTemp){. char *
1a4fd 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 zP4 = zTemp;. a
1a4fe 73 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 ssert( nTemp>=20
1a4ff 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f );. switch( pO
1a500 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 20 20 p->p4type ){.
1a501 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f case P4_KEYINFO
1a502 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 63 61 73 _STATIC:. cas
1a503 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a e P4_KEYINFO: {.
1a504 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a int i, j;.
1a505 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 KeyInfo *p
1a506 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 KeyInfo = pOp->p
1a507 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 4.pKeyInfo;.
1a508 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a509 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
1a50a 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 "keyinfo(%d", p
1a50b 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 KeyInfo->nField)
1a50c 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 71 6c 69 ;. i = sqli
1a50d 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d te3Strlen30(zTem
1a50e 70 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d p);. for(j=
1a50f 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 0; j<pKeyInfo->n
1a510 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 Field; j++){.
1a511 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 CollSeq *pC
1a512 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e oll = pKeyInfo->
1a513 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 aColl[j];.
1a514 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 if( pColl ){.
1a515 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d int n =
1a516 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a517 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a (pColl->zName);.
1a518 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 2b if( i+
1a519 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 n>nTemp-6 ){.
1a51a 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
1a51b 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 &zTemp[i],",..."
1a51c 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ,4);.
1a51d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1a51e 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7a 54 }. zT
1a51f 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a emp[i++] = ',';.
1a520 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4b if( pK
1a521 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 eyInfo->aSortOrd
1a522 65 72 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e er && pKeyInfo->
1a523 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b aSortOrder[j] ){
1a524 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 65 . zTe
1a525 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 mp[i++] = '-';.
1a526 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1a527 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 memcpy(&zTe
1a528 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e mp[i], pColl->zN
1a529 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 ame,n+1);.
1a52a 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 i += n;.
1a52b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 2b }else if( i+
1a52c 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 4<nTemp-6 ){.
1a52d 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a memcpy(&z
1a52e 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 Temp[i],",nil",4
1a52f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 20 2b );. i +
1a530 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 = 4;. }.
1a531 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 }. zTe
1a532 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 mp[i++] = ')';.
1a533 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 zTemp[i] =
1a534 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 0;. assert(
1a535 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 i<nTemp );.
1a536 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1a537 20 20 20 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 case P4_COLLS
1a538 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c EQ: {. Coll
1a539 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 Seq *pColl = pOp
1a53a 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 ->p4.pColl;.
1a53b 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a53c 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
1a53d 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 "collseq(%.20s)
1a53e 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 ", pColl->zName)
1a53f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1a540 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a541 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 _FUNCDEF: {.
1a542 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 FuncDef *pDef
1a543 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
1a544 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1a545 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
1a546 54 65 6d 70 2c 20 22 25 73 28 25 64 29 22 2c 20 Temp, "%s(%d)",
1a547 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 pDef->zName, pDe
1a548 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 f->nArg);.
1a549 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1a54a 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 case P4_INT64:
1a54b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1a54c 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 snprintf(nTemp,
1a54d 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a zTemp, "%lld", *
1a54e 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 pOp->p4.pI64);.
1a54f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a550 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e }. case P4_IN
1a551 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c T32: {. sql
1a552 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1a553 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 emp, zTemp, "%d"
1a554 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 , pOp->p4.i);.
1a555 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1a556 0a 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 . case P4_REA
1a557 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 L: {. sqlit
1a558 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
1a559 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 p, zTemp, "%.16g
1a55a 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 ", *pOp->p4.pRea
1a55b 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b l);. break;
1a55c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1a55d 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 P4_MEM: {.
1a55e 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d Mem *pMem = pOp-
1a55f 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 >p4.pMem;.
1a560 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
1a561 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1a562 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ==0 );. if(
1a563 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
1a564 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 EM_Str ){.
1a565 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b zP4 = pMem->z;
1a566 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1a567 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
1a568 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 EM_Int ){.
1a569 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a56a 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
1a56b 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 "%lld", pMem->u
1a56c 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 .i);. }else
1a56d 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
1a56e 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 & MEM_Real ){.
1a56f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
1a570 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a nprintf(nTemp, z
1a571 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 Temp, "%.16g", p
1a572 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d Mem->r);. }
1a573 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
1a574 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 sert( pMem->flag
1a575 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a s & MEM_Blob );.
1a576 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 22 28 zP4 = "(
1a577 62 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a blob)";. }.
1a578 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a579 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
1a57a 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1a57b 42 4c 45 0a 20 20 20 20 63 61 73 65 20 50 34 5f BLE. case P4_
1a57c 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 VTAB: {. sq
1a57d 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1a57e 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 b = pOp->p4.pVta
1a57f 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 b->pVtab;.
1a580 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a581 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 (nTemp, zTemp, "
1a582 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 vtab:%p:%p", pVt
1a583 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 ab, pVtab->pModu
1a584 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b le);. break
1a585 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
1a586 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 case P4_INTAR
1a587 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c RAY: {. sql
1a588 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1a589 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 emp, zTemp, "int
1a58a 61 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62 array");. b
1a58b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1a58c 63 61 73 65 20 50 34 5f 53 55 42 50 52 4f 47 52 case P4_SUBPROGR
1a58d 41 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 AM: {. sqli
1a58e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 te3_snprintf(nTe
1a58f 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 mp, zTemp, "prog
1a590 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65 ram");. bre
1a591 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
1a592 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a fault: {. z
1a593 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a P4 = pOp->p4.z;.
1a594 20 20 20 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 if( zP4==0
1a595 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 ){. zP4
1a596 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 = zTemp;.
1a597 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 zTemp[0] = 0;.
1a598 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1a599 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34 21 3d . assert( zP4!=
1a59a 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 0 );. return zP
1a59b 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 4;.}.#endif../*.
1a59c 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 74 68 ** Declare to th
1a59d 65 20 56 64 62 65 20 74 68 61 74 20 74 68 65 20 e Vdbe that the
1a59e 42 54 72 65 65 20 6f 62 6a 65 63 74 20 61 74 20 BTree object at
1a59f 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 db->aDb[i] is us
1a5a0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1a5a1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a5a2 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 e3VdbeUsesBtree(
1a5a3 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b Vdbe *p, int i){
1a5a4 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 . int mask;. a
1a5a5 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
1a5a6 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 <p->db->nDb && i
1a5a7 3c 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 <sizeof(u32)*8 )
1a5a8 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c 28 69 ;. assert( i<(i
1a5a9 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 nt)sizeof(p->btr
1a5aa 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d eeMask)*8 );. m
1a5ab 61 73 6b 20 3d 20 28 28 75 33 32 29 31 29 3c 3c ask = ((u32)1)<<
1a5ac 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 74 72 i;. if( (p->btr
1a5ad 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d eeMask & mask)==
1a5ae 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 0 ){. p->btre
1a5af 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 eMask |= mask;.
1a5b0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d sqlite3BtreeM
1a5b1 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 utexArrayInsert(
1a5b2 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 &p->aMutex, p->d
1a5b3 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a b->aDb[i].pBt);.
1a5b4 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 }.}...#if defi
1a5b5 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 ned(VDBE_PROFILE
1a5b6 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
1a5b7 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a ITE_DEBUG)./*.**
1a5b8 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 Print a single
1a5b9 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f opcode. This ro
1a5ba 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f utine is used fo
1a5bb 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 r debugging only
1a5bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a5bd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a5be 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 VdbePrintOp(FILE
1a5bf 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 *pOut, int pc,
1a5c0 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 Op *pOp){. char
1a5c1 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 *zP4;. char zP
1a5c2 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 tr[50];. static
1a5c3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
1a5c4 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 rmat1 = "%4d %-1
1a5c5 33 73 20 25 34 64 20 25 34 64 20 25 34 64 20 25 3s %4d %4d %4d %
1a5c6 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a -4s %.2X %s\n";.
1a5c7 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 if( pOut==0 )
1a5c8 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 pOut = stdout;.
1a5c9 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 zP4 = displayP4
1a5ca 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 (pOp, zPtr, size
1a5cb 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 of(zPtr));. fpr
1a5cc 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d intf(pOut, zForm
1a5cd 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 at1, pc, .
1a5ce 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d sqlite3OpcodeNam
1a5cf 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 e(pOp->opcode),
1a5d0 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 pOp->p1, pOp->p2
1a5d1 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 , pOp->p3, zP4,
1a5d2 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 pOp->p5,.#ifdef
1a5d3 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1a5d4 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 pOp->zComment
1a5d5 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 ? pOp->zComment
1a5d6 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 : "".#else.
1a5d7 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b "".#endif. );
1a5d8 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b . fflush(pOut);
1a5d9 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1a5da 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 Release an arra
1a5db 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 y of N Mem eleme
1a5dc 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nts.*/.static vo
1a5dd 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 id releaseMemArr
1a5de 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e ay(Mem *p, int N
1a5df 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 ){. if( p && N
1a5e0 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 ){. Mem *pEnd
1a5e1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 ;. sqlite3 *d
1a5e2 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 b = p->db;. u
1a5e3 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 8 malloc_failed
1a5e4 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c = db->mallocFail
1a5e5 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 ed;. for(pEnd
1a5e6 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 =&p[N]; p<pEnd;
1a5e7 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 p++){. asse
1a5e8 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e rt( (&p[1])==pEn
1a5e9 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b d || p[0].db==p[
1a5ea 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 1].db );..
1a5eb 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 /* This block is
1a5ec 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e really an inlin
1a5ed 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 ed version of sq
1a5ee 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1a5ef 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 ase(). ** t
1a5f0 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e 74 hat takes advant
1a5f1 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 74 20 age of the fact
1a5f2 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 that the memory
1a5f3 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 cell value is .
1a5f4 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 ** being se
1a5f5 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 t to NULL after
1a5f6 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64 79 releasing any dy
1a5f7 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 2e namic resources.
1a5f8 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
1a5f9 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63 61 ** The justifica
1a5fa 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 tion for duplica
1a5fb 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68 61 ting code is tha
1a5fc 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a t according to .
1a5fd 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 ** callgri
1a5fe 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 73 20 nd, this causes
1a5ff 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 20 63 a certain test c
1a600 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 20 43 ase to hit the C
1a601 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a PU 4.7 . **
1a602 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28 78 percent less (x
1a603 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 86 linux, gcc ve
1a604 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 rsion 4.1.2, -O6
1a605 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20 20 ) than if .
1a606 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 ** sqlite3MemRe
1a607 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 61 6c lease() were cal
1a608 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 led from here. W
1a609 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 ith -O2, this ju
1a60a 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 mps. ** to
1a60b 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68 65 6.6 percent. The
1a60c 20 74 65 73 74 20 63 61 73 65 20 69 73 20 69 6e test case is in
1a60d 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f 77 serting 1000 row
1a60e 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a s into a table .
1a60f 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f ** with no
1a610 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20 61 indexes using a
1a611 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65 64 single prepared
1a612 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e INSERT statemen
1a613 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20 20 t, bind() .
1a614 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29 2e ** and reset().
1a615 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 72 6f Inserts are gro
1a616 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61 6e uped into a tran
1a617 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a saction.. *
1a618 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 66 /. if( p->f
1a619 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 lags&(MEM_Agg|ME
1a61a 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c M_Dyn|MEM_Frame|
1a61b 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 MEM_RowSet) ){.
1a61c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a61d 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29 3b beMemRelease(p);
1a61e 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1a61f 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 p->zMalloc ){.
1a620 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
1a621 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c Free(db, p->zMal
1a622 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70 2d loc);. p-
1a623 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 >zMalloc = 0;.
1a624 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e }.. p->
1a625 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1a626 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e ;. }. db->
1a627 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d mallocFailed = m
1a628 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 alloc_failed;.
1a629 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 }.}../*.** Delet
1a62a 65 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 e a VdbeFrame ob
1a62b 6a 65 63 74 20 61 6e 64 20 69 74 73 20 63 6f 6e ject and its con
1a62c 74 65 6e 74 73 2e 20 56 64 62 65 46 72 61 6d 65 tents. VdbeFrame
1a62d 20 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 objects are.**
1a62e 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 allocated by the
1a62f 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f OP_Program opco
1a630 64 65 20 69 6e 20 73 71 6c 69 74 65 33 56 64 62 de in sqlite3Vdb
1a631 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 eExec()..*/.SQLI
1a632 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a633 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 sqlite3VdbeFrame
1a634 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 Delete(VdbeFrame
1a635 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 *p){. int i;.
1a636 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 Mem *aMem = Vdb
1a637 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 eFrameMem(p);.
1a638 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 VdbeCursor **apC
1a639 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 sr = (VdbeCursor
1a63a 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 **)&aMem[p->nCh
1a63b 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 ildMem];. for(i
1a63c 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 =0; i<p->nChildC
1a63d 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 sr; i++){. sq
1a63e 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 lite3VdbeFreeCur
1a63f 73 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b sor(p->v, apCsr[
1a640 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 i]);. }. relea
1a641 73 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c seMemArray(aMem,
1a642 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a p->nChildMem);.
1a643 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a644 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a p->v->db, p);.}.
1a645 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a646 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a OMIT_EXPLAIN./*.
1a647 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74 69 6e ** Give a listin
1a648 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d g of the program
1a649 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 in the virtual
1a64a 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 machine..**.** T
1a64b 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 he interface is
1a64c 74 68 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 the same as sqli
1a64d 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 te3VdbeExec().
1a64e 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a But instead of.*
1a64f 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f * running the co
1a650 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 de, it invokes t
1a651 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 he callback once
1a652 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74 72 75 for each instru
1a653 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 ction..** This f
1a654 65 61 74 75 72 65 20 69 73 20 75 73 65 64 20 74 eature is used t
1a655 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 o implement "EXP
1a656 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 LAIN"..**.** Whe
1a657 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c n p->explain==1,
1a658 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f each instructio
1a659 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 20 57 68 n is listed. Wh
1a65a 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e en.** p->explain
1a65b 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 ==2, only OP_Exp
1a65c 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e lain instruction
1a65d 73 20 61 72 65 20 6c 69 73 74 65 64 20 61 6e 64 s are listed and
1a65e 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 these.** are sh
1a65f 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 own in a differe
1a660 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 nt format. p->e
1a661 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 xplain==2 is use
1a662 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a d to implement.*
1a663 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 * EXPLAIN QUERY
1a664 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f PLAN..*/.SQLITE_
1a665 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1a666 74 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 te3VdbeList(. V
1a667 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 dbe *p
1a668 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1a669 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 VDBE */.){. int
1a66a 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 nRow;
1a66b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a66c 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 /* Total numbe
1a66d 72 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 65 74 r of rows to ret
1a66e 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 urn */. int nSu
1a66f 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 b = 0;
1a670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a671 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 Number of sub-v
1a672 64 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61 72 dbes seen so far
1a673 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d */. SubProgram
1a674 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20 20 **apSub = 0;
1a675 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
1a676 72 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65 73 ray of sub-vdbes
1a677 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 */. Mem *pSub
1a678 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a = 0;. sqlite3 *
1a679 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e db = p->db;. in
1a67a 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t i;. int rc =
1a67b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d SQLITE_OK;. Mem
1a67c 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 *pMem = p->pRes
1a67d 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 ultSet = &p->aMe
1a67e 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 m[1];.. assert(
1a67f 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 p->explain );.
1a680 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1a681 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1a682 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 N );. assert( d
1a683 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
1a684 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 _MAGIC_BUSY );.
1a685 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d assert( p->rc==
1a686 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
1a687 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
1a688 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 || p->rc==SQLITE
1a689 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 _NOMEM );.. /*
1a68a 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73 Even though this
1a68b 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 opcode does not
1a68c 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72 use dynamic str
1a68d 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 ings for. ** th
1a68e 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 e result, result
1a68f 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 columns may bec
1a690 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74 ome dynamic if t
1a691 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 he user calls.
1a692 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
1a693 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73 n_text16(), caus
1a694 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f ing a translatio
1a695 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f n to UTF-16 enco
1a696 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c ding.. */. rel
1a697 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 easeMemArray(pMe
1a698 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d m, 8);.. if( p-
1a699 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 >rc==SQLITE_NOME
1a69a 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 M ){. /* This
1a69b 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 happens if a ma
1a69c 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 lloc() inside a
1a69d 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
1a69e 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 column_text() or
1a69f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f . ** sqlite3_
1a6a0 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 column_text16()
1a6a1 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 failed. */.
1a6a2 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a6a3 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
1a6a4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1a6a5 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 }.. /* Figure
1a6a6 6f 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 out total number
1a6a7 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 of rows that wi
1a6a8 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 ll be returned b
1a6a9 79 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 y this . ** EXP
1a6aa 4c 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a LAIN program. *
1a6ab 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f /. nRow = p->nO
1a6ac 70 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c p;. if( p->expl
1a6ad 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 ain==1 ){. pS
1a6ae 75 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d ub = &p->aMem[9]
1a6af 3b 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e ;. if( pSub->
1a6b0 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 flags&MEM_Blob )
1a6b1 7b 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70 {. nSub = p
1a6b2 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 Sub->n/sizeof(Vd
1a6b3 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75 be*);. apSu
1a6b4 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 b = (SubProgram
1a6b5 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 **)pSub->z;.
1a6b6 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1a6b7 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nSub; i++){.
1a6b8 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 nRow += apSub
1a6b9 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a [i]->nOp;. }.
1a6ba 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 }.. do{. i
1a6bb 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 = p->pc++;. }w
1a6bc 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 hile( i<nRow &&
1a6bd 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 p->explain==2 &&
1a6be 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 p->aOp[i].opcod
1a6bf 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b e!=OP_Explain );
1a6c0 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 . if( i>=nRow )
1a6c1 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 {. p->rc = SQ
1a6c2 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 LITE_OK;. rc
1a6c3 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
1a6c4 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 }else if( db->u
1a6c5 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 1.isInterrupted
1a6c6 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 ){. p->rc = S
1a6c7 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b QLITE_INTERRUPT;
1a6c8 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1a6c9 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 _ERROR;. sqli
1a6ca 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1a6cb 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1a6cc 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 s", sqlite3ErrSt
1a6cd 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c r(p->rc));. }el
1a6ce 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b se{. char *z;
1a6cf 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 . Op *pOp;.
1a6d0 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 if( i<p->nOp )
1a6d1 7b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 70 {. pOp = &p
1a6d2 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 ->aOp[i];. }e
1a6d3 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a lse{. int j
1a6d4 3b 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e ;. i -= p->
1a6d5 6e 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a nOp;. for(j
1a6d6 3d 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d =0; i>=apSub[j]-
1a6d7 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 >nOp; j++){.
1a6d8 20 20 20 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a i -= apSub[j
1a6d9 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a ]->nOp;. }.
1a6da 20 20 20 20 20 20 70 4f 70 20 3d 20 26 61 70 53 pOp = &apS
1a6db 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 ub[j]->aOp[i];.
1a6dc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e }. if( p->
1a6dd 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 explain==1 ){.
1a6de 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1a6df 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
1a6e0 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1a6e1 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 LITE_INTEGER;.
1a6e2 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
1a6e3 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1a6e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6e5 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 /* Program cou
1a6e6 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d nter */. pM
1a6e7 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 em++;. . p
1a6e8 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a6e9 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c _Static|MEM_Str|
1a6ea 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 MEM_Term;.
1a6eb 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a pMem->z = (char*
1a6ec 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 )sqlite3OpcodeNa
1a6ed 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b me(pOp->opcode);
1a6ee 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 /* Opcode */.
1a6ef 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 assert( pMe
1a6f0 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 m->z!=0 );.
1a6f1 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 pMem->n = sqlit
1a6f2 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d e3Strlen30(pMem-
1a6f3 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d >z);. pMem-
1a6f4 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 >type = SQLITE_T
1a6f5 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d EXT;. pMem-
1a6f6 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 >enc = SQLITE_UT
1a6f7 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b F8;. pMem++
1a6f8 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 ;.. if( pOp
1a6f9 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 ->p4type==P4_SUB
1a6fa 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 PROGRAM ){.
1a6fb 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 int nByte = (
1a6fc 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 nSub+1)*sizeof(S
1a6fd 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 ubProgram*);.
1a6fe 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 int j;.
1a6ff 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e for(j=0; j<n
1a700 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 Sub; j++){.
1a701 20 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a if( apSub[j
1a702 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 ]==pOp->p4.pProg
1a703 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ram ) break;.
1a704 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1a705 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 f( j==nSub && SQ
1a706 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 LITE_OK==sqlite3
1a707 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 VdbeMemGrow(pSub
1a708 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 , nByte, 1) ){.
1a709 20 20 20 20 20 20 20 20 20 61 70 53 75 62 20 3d apSub =
1a70a 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 (SubProgram **)
1a70b 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 pSub->z;.
1a70c 20 20 20 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d apSub[nSub++]
1a70d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 = pOp->p4.pProg
1a70e 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 ram;. p
1a70f 53 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Sub->flags |= ME
1a710 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 M_Blob;.
1a711 20 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 pSub->n = nSub
1a712 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 *sizeof(SubProgr
1a713 61 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a am*);. }.
1a714 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
1a715 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a716 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d MEM_Int;. pM
1a717 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 em->u.i = pOp->p
1a718 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
1a719 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1a71a 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 1 */. pMem->t
1a71b 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 ype = SQLITE_INT
1a71c 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b EGER;. pMem++
1a71d 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ;.. pMem->fla
1a71e 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1a71f 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f pMem->u.i = pO
1a720 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 20 20 p->p2;
1a721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a722 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 /* P2 */. pMe
1a723 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1a724 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d _INTEGER;. pM
1a725 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 em++;.. if( p
1a726 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
1a727 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1a728 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1a729 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 pMem->u.i = p
1a72a 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 20 20 Op->p3;
1a72b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a72c 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 /* P3 */.
1a72d 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a72e 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 ITE_INTEGER;.
1a72f 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d pMem++;. }
1a730 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 .. if( sqlite
1a731 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 3VdbeMemGrow(pMe
1a732 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 m, 32, 0) ){
1a733 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f /* P4 */
1a734 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1a735 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a736 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ed );. retu
1a737 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
1a738 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
1a739 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e >flags = MEM_Dyn
1a73a 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 |MEM_Str|MEM_Ter
1a73b 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c m;. z = displ
1a73c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e ayP4(pOp, pMem->
1a73d 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 z, 32);. if(
1a73e 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 z!=pMem->z ){.
1a73f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1a740 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a emSetStr(pMem, z
1a741 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 , -1, SQLITE_UTF
1a742 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 8, 0);. }else
1a743 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1a744 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 pMem->z!=0 );.
1a745 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 pMem->n = sq
1a746 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d lite3Strlen30(pM
1a747 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d em->z);. pM
1a748 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 em->enc = SQLITE
1a749 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 _UTF8;. }.
1a74a 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1a74b 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 LITE_TEXT;. p
1a74c 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 Mem++;.. if(
1a74d 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b p->explain==1 ){
1a74e 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1a74f 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
1a750 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 em, 4, 0) ){.
1a751 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
1a752 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a753 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 );. retu
1a754 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
1a755 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
1a756 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a757 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d _Dyn|MEM_Str|MEM
1a758 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 _Term;. pMe
1a759 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 m->n = 2;.
1a75a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a75b 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e (3, pMem->z, "%.
1a75c 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 2x", pOp->p5);
1a75d 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 /* P5 */.
1a75e 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a75f 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 ITE_TEXT;.
1a760 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
1a761 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 TE_UTF8;. p
1a762 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 Mem++;. .#ifdef
1a763 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1a764 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f if( pOp->zCo
1a765 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 mment ){.
1a766 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1a767 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b EM_Str|MEM_Term;
1a768 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a . pMem->z
1a769 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 = pOp->zComment
1a76a 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e ;. pMem->
1a76b 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
1a76c 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
1a76d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 pMem->enc
1a76e 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 = SQLITE_UTF8;.
1a76f 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 pMem->typ
1a770 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b e = SQLITE_TEXT;
1a771 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e . }else.#en
1a772 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 dif. {.
1a773 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1a774 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 = MEM_Null;
1a775 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a776 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a /* Comment */.
1a777 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 pMem->ty
1a778 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
1a779 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1a77a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 . p->nResColu
1a77b 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 mn = 8 - 5*(p->e
1a77c 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 xplain-1);. p
1a77d 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b ->rc = SQLITE_OK
1a77e 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1a77f 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 E_ROW;. }. ret
1a780 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
1a781 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1a782 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 EXPLAIN */..#ifd
1a783 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a784 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 /*.** Print the
1a785 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65 SQL that was use
1a786 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 d to generate a
1a787 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f VDBE program..*/
1a788 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a789 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a78a 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 PrintSql(Vdbe *p
1a78b 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 ){. int nOp = p
1a78c 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 ->nOp;. VdbeOp
1a78d 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c *pOp;. if( nOp<
1a78e 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 1 ) return;. pO
1a78f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a p = &p->aOp[0];.
1a790 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1a791 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 e==OP_Trace && p
1a792 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 Op->p4.z!=0 ){.
1a793 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1a794 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
1a795 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
1a796 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b Isspace(*z) ) z+
1a797 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 +;. printf("S
1a798 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b QL: [%s]\n", z);
1a799 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 . }.}.#endif..#
1a79a 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
1a79b 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 TE_OMIT_TRACE) &
1a79c 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
1a79d 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 _ENABLE_IOTRACE)
1a79e 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 ./*.** Print an
1a79f 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20 IOTRACE message
1a7a0 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 showing SQL cont
1a7a1 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ent..*/.SQLITE_P
1a7a2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a7a3 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
1a7a4 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e l(Vdbe *p){. in
1a7a5 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a t nOp = p->nOp;.
1a7a6 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 VdbeOp *pOp;.
1a7a7 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 if( sqlite3IoTr
1a7a8 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ace==0 ) return;
1a7a9 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 . if( nOp<1 ) r
1a7aa 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 eturn;. pOp = &
1a7ab 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 p->aOp[0];. if(
1a7ac 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1a7ad 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 _Trace && pOp->p
1a7ae 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 4.z!=0 ){. in
1a7af 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 t i, j;. char
1a7b0 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 z[1000];. sq
1a7b1 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
1a7b2 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 izeof(z), z, "%s
1a7b3 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 ", pOp->p4.z);.
1a7b4 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 for(i=0; sqli
1a7b5 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 te3Isspace(z[i])
1a7b6 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 ; i++){}. for
1a7b7 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 (j=0; z[i]; i++)
1a7b8 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
1a7b9 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 te3Isspace(z[i])
1a7ba 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1a7bb 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 z[i-1]!=' ' ){.
1a7bc 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 z[j++]
1a7bd 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d = ' ';. }
1a7be 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1a7bf 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a z[j++] = z
1a7c0 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 [i];. }.
1a7c1 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b }. z[j] = 0;
1a7c2 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72 . sqlite3IoTr
1a7c3 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 ace("SQL %s\n",
1a7c4 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 z);. }.}.#endif
1a7c5 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
1a7c6 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45 _TRACE && SQLITE
1a7c7 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 _ENABLE_IOTRACE
1a7c8 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 */../*.** Alloca
1a7c9 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20 te space from a
1a7ca 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65 fixed size buffe
1a7cb 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 r. Make *pp poi
1a7cc 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c nt to the.** all
1a7cd 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 20 28 ocated space. (
1a7ce 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 61 20 63 Note: pp is a c
1a7cf 68 61 72 2a 20 72 61 74 68 65 72 20 74 68 61 6e har* rather than
1a7d0 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 a void** to.**
1a7d1 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 work around the
1a7d2 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 pointer aliasing
1a7d3 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a rules of C.) *
1a7d4 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 pp should initia
1a7d5 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 lly.** be zero.
1a7d6 20 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a If *pp is not z
1a7d7 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 ero, that means
1a7d8 74 68 61 74 20 74 68 65 20 73 70 61 63 65 20 68 that the space h
1a7d9 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 as already.** be
1a7da 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
1a7db 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1a7dc 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e a noop..**.** n
1a7dd 42 79 74 65 20 69 73 20 74 68 65 20 6e 75 6d 62 Byte is the numb
1a7de 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
1a7df 70 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a pace needed..**.
1a7e0 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 ** *ppFrom point
1a7e1 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 73 70 to available sp
1a7e2 61 63 65 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 ace and pEnd poi
1a7e3 6e 74 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f nts to the end o
1a7e4 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 f the.** availab
1a7e5 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 le space..**.**
1a7e6 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 6f 75 *pnByte is a cou
1a7e7 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 nter of the numb
1a7e8 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
1a7e9 70 61 63 65 20 74 68 61 74 20 68 61 76 65 20 66 pace that have f
1a7ea 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f ailed.** to allo
1a7eb 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 65 20 cate. If there
1a7ec 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 20 is insufficient
1a7ed 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d space in *ppFrom
1a7ee 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a to satisfy the.
1a7ef 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68 65 6e ** request, then
1a7f0 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 increment *pnBy
1a7f1 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 te by the amount
1a7f2 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 2e of the request.
1a7f3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a7f4 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 63 68 allocSpace(. ch
1a7f5 61 72 20 2a 70 70 2c 20 20 20 20 20 20 20 20 20 ar *pp,
1a7f6 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 /* IN/OUT: Se
1a7f7 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 t *pp to point t
1a7f8 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 o allocated buff
1a7f9 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 er */. int nByt
1a7fa 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e, /*
1a7fb 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1a7fc 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 to allocate */.
1a7fd 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 u8 **ppFrom,
1a7fe 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a /* IN/OUT:
1a7ff 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a Allocate from *
1a800 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a ppFrom */. u8 *
1a801 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 pEnd,
1a802 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 /* Pointer to 1
1a803 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 byte past the e
1a804 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 nd of *ppFrom bu
1a805 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ffer */. int *p
1a806 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 20 2f nByte /
1a807 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 * If allocation
1a808 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 cannot be made,
1a809 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 increment *pnByt
1a80a 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 e */.){. assert
1a80b 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
1a80c 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 GNMENT(*ppFrom)
1a80d 29 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f 69 64 );. if( (*(void
1a80e 2a 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 **)pp)==0 ){.
1a80f 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 nByte = ROUND8(
1a810 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 nByte);. if(
1a811 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 &(*ppFrom)[nByte
1a812 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 ] <= pEnd ){.
1a813 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d *(void**)pp =
1a814 20 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d (void *)*ppFrom
1a815 3b 0a 20 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 ;. *ppFrom
1a816 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 += nByte;. }e
1a817 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 lse{. *pnBy
1a818 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 te += nByte;.
1a819 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
1a81a 50 72 65 70 61 72 65 20 61 20 76 69 72 74 75 61 Prepare a virtua
1a81b 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 l machine for ex
1a81c 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 ecution. This i
1a81d 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 20 73 nvolves things s
1a81e 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 uch.** as alloca
1a81f 74 69 6e 67 20 73 74 61 63 6b 20 73 70 61 63 65 ting stack space
1a820 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e and initializin
1a821 67 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f g the program co
1a822 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 unter..** After
1a823 74 68 65 20 56 44 42 45 20 68 61 73 20 62 65 20 the VDBE has be
1a824 70 72 65 70 70 65 64 2c 20 69 74 20 63 61 6e 20 prepped, it can
1a825 62 65 20 65 78 65 63 75 74 65 64 20 62 79 20 6f be executed by o
1a826 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 ne or more.** ca
1a827 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 56 64 lls to sqlite3Vd
1a828 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a beExec(). .**.*
1a829 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e * This is the on
1a82a 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 ly way to move a
1a82b 20 56 44 42 45 20 66 72 6f 6d 20 56 44 42 45 5f VDBE from VDBE_
1a82c 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a MAGIC_INIT to.**
1a82d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e VDBE_MAGIC_RUN.
1a82e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
1a82f 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c tion may be call
1a830 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ed more than onc
1a831 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 e on a single vi
1a832 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a rtual machine..*
1a833 2a 20 54 68 65 20 66 69 72 73 74 20 63 61 6c 6c * The first call
1a834 20 69 73 20 6d 61 64 65 20 77 68 69 6c 65 20 63 is made while c
1a835 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c ompiling the SQL
1a836 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 statement. Subs
1a837 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 equent.** calls
1a838 61 72 65 20 6d 61 64 65 20 61 73 20 70 61 72 74 are made as part
1a839 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 20 of the process
1a83a 6f 66 20 72 65 73 65 74 74 69 6e 67 20 61 20 73 of resetting a s
1a83b 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a tatement to be.*
1a83c 2a 20 72 65 2d 65 78 65 63 75 74 65 64 20 28 66 * re-executed (f
1a83d 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 rom a call to sq
1a83e 6c 69 74 65 33 5f 72 65 73 65 74 28 29 29 2e 20 lite3_reset()).
1a83f 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 The nVar, nMem,
1a840 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 nCursor .** and
1a841 69 73 45 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 isExplain parame
1a842 74 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 ters are only pa
1a843 73 73 65 64 20 63 6f 72 72 65 63 74 20 76 61 6c ssed correct val
1a844 75 65 73 20 74 68 65 20 66 69 72 73 74 20 74 69 ues the first ti
1a845 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 me.** the functi
1a846 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e on is called. On
1a847 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
1a848 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f s, from sqlite3_
1a849 72 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a reset(), nVar.**
1a84a 20 69 73 20 70 61 73 73 65 64 20 2d 31 20 61 6e is passed -1 an
1a84b 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 d nMem, nCursor
1a84c 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 61 72 and isExplain ar
1a84d 65 20 61 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 e all passed zer
1a84e 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 o..*/.SQLITE_PRI
1a84f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a850 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 3VdbeMakeReady(.
1a851 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 Vdbe *p,
1a852 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a853 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a /* The VDBE */.
1a854 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 int nVar,
1a855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a856 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f /* Number of '?
1a857 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 51 4c ' see in the SQL
1a858 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
1a859 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 int nMem,
1a85a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a85b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
1a85c 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f ry cells to allo
1a85d 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 cate */. int nC
1a85e 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 20 20 ursor,
1a85f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1a860 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 74 6f er of cursors to
1a861 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 allocate */. i
1a862 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 nt nArg,
1a863 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a864 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 Maximum number
1a865 6f 66 20 61 72 67 73 20 69 6e 20 53 75 62 50 72 of args in SubPr
1a866 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 ograms */. int
1a867 69 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 isExplain,
1a868 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1a869 75 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 ue if the EXPLAI
1a86a 4e 20 6b 65 79 77 6f 72 64 73 20 69 73 20 70 72 N keywords is pr
1a86b 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 esent */. int u
1a86c 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 sesStmtJournal
1a86d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1a86e 65 20 74 6f 20 73 65 74 20 56 64 62 65 2e 75 73 e to set Vdbe.us
1a86f 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f esStmtJournal */
1a870 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 .){. int n;. s
1a871 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
1a872 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 db;.. assert( p
1a873 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1a874 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
1a875 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 MAGIC_INIT );..
1a876 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 /* There should
1a877 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 be at least one
1a878 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 opcode.. */.
1a879 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
1a87a 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 );.. /* Set th
1a87b 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 45 5f e magic to VDBE_
1a87c 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 MAGIC_RUN sooner
1a87d 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74 rather than lat
1a87e 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 er. */. p->magi
1a87f 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 c = VDBE_MAGIC_R
1a880 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 UN;.. /* For ea
1a881 63 68 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 ch cursor requir
1a882 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 ed, also allocat
1a883 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e e a memory cell.
1a884 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c Memory. ** cel
1a885 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 ls (nMem+1-nCurs
1a886 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 or)..nMem, inclu
1a887 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 sive, will never
1a888 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a be used by. **
1a889 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
1a88a 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 m. Instead they
1a88b 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f are used to allo
1a88c 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 cate space for.
1a88d 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 ** VdbeCursor/B
1a88e 74 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 tCursor structur
1a88f 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 es. The blob of
1a890 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
1a891 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 d with . ** cur
1a892 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 sor 0 is stored
1a893 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e in memory cell n
1a894 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c Mem. Memory cell
1a895 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 (nMem-1). ** s
1a896 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f tores the blob o
1a897 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 f memory associa
1a898 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 ted with cursor
1a899 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 1, etc.. **. *
1a89a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f * See also: allo
1a89b 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 cateCursor()..
1a89c 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 */. nMem += nCu
1a89d 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f rsor;.. /* Allo
1a89e 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d cate space for m
1a89f 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c emory registers,
1a8a0 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 SQL variables,
1a8a1 56 44 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 VDBE cursors and
1a8a2 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 . ** an array
1a8a3 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 to marshal SQL f
1a8a4 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 unction argument
1a8a5 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e s in. This is on
1a8a6 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a ly done the. **
1a8a7 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 first time this
1a8a8 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
1a8a9 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65 6e 20 led for a given
1a8aa 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 VDBE, not when i
1a8ab 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 t is. ** being
1a8ac 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 called from sqli
1a8ad 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f 20 72 te3_reset() to r
1a8ae 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c eset the virtual
1a8af 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 machine.. */.
1a8b0 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 if( nVar>=0 &&
1a8b1 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f ALWAYS(db->mallo
1a8b2 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20 cFailed==0) ){.
1a8b3 20 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 28 75 u8 *zCsr = (u
1a8b4 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 8 *)&p->aOp[p->n
1a8b5 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e Op];. u8 *zEn
1a8b6 64 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f d = (u8 *)&p->aO
1a8b7 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a p[p->nOpAlloc];.
1a8b8 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 int nByte;.
1a8b9 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 resolveP2Valu
1a8ba 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 es(p, &nArg);.
1a8bb 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 p->usesStmtJou
1a8bc 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 73 53 rnal = (u8)usesS
1a8bd 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 tmtJournal;.
1a8be 69 66 28 20 69 73 45 78 70 6c 61 69 6e 20 26 26 if( isExplain &&
1a8bf 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 nMem<10 ){.
1a8c0 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 nMem = 10;.
1a8c1 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 }. memset(zC
1a8c2 73 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 sr, 0, zEnd-zCsr
1a8c3 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 28 );. zCsr += (
1a8c4 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 29 26 37 zCsr - (u8*)0)&7
1a8c5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 49 ;. assert( EI
1a8c6 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
1a8c7 4e 54 28 7a 43 73 72 29 20 29 3b 0a 0a 20 20 20 NT(zCsr) );..
1a8c8 20 64 6f 20 7b 0a 20 20 20 20 20 20 6e 42 79 74 do {. nByt
1a8c9 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 6c 6c e = 0;. all
1a8ca 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 ocSpace((char*)&
1a8cb 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 p->aMem, nMem*si
1a8cc 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 zeof(Mem), &zCsr
1a8cd 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b , zEnd, &nByte);
1a8ce 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 . allocSpac
1a8cf 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 56 61 e((char*)&p->aVa
1a8d0 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d r, nVar*sizeof(M
1a8d1 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 em), &zCsr, zEnd
1a8d2 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 , &nByte);.
1a8d3 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 allocSpace((cha
1a8d4 72 2a 29 26 70 2d 3e 61 70 41 72 67 2c 20 6e 41 r*)&p->apArg, nA
1a8d5 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c rg*sizeof(Mem*),
1a8d6 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e &zCsr, zEnd, &n
1a8d7 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c Byte);. all
1a8d8 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 ocSpace((char*)&
1a8d9 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73 p->azVar, nVar*s
1a8da 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a izeof(char*), &z
1a8db 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 Csr, zEnd, &nByt
1a8dc 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 e);. allocS
1a8dd 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e pace((char*)&p->
1a8de 61 70 43 73 72 2c 20 0a 20 20 20 20 20 20 20 20 apCsr, .
1a8df 20 20 20 20 20 20 20 20 20 6e 43 75 72 73 6f 72 nCursor
1a8e0 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 *sizeof(VdbeCurs
1a8e1 6f 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e or*), &zCsr, zEn
1a8e2 64 2c 20 26 6e 42 79 74 65 0a 20 20 20 20 20 20 d, &nByte.
1a8e3 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 42 79 );. if( nBy
1a8e4 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d te ){. p-
1a8e5 3e 70 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 >pFree = sqlite3
1a8e6 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
1a8e7 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d nByte);. }
1a8e8 0a 20 20 20 20 20 20 7a 43 73 72 20 3d 20 70 2d . zCsr = p-
1a8e9 3e 70 46 72 65 65 3b 0a 20 20 20 20 20 20 7a 45 >pFree;. zE
1a8ea 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 nd = &zCsr[nByte
1a8eb 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 6e ];. }while( n
1a8ec 42 79 74 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c Byte && !db->mal
1a8ed 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 locFailed );..
1a8ee 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 p->nCursor = (
1a8ef 75 31 36 29 6e 43 75 72 73 6f 72 3b 0a 20 20 20 u16)nCursor;.
1a8f0 20 69 66 28 20 70 2d 3e 61 56 61 72 20 29 7b 0a if( p->aVar ){.
1a8f1 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 p->nVar =
1a8f2 28 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 (ynVar)nVar;.
1a8f3 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 for(n=0; n<nV
1a8f4 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 ar; n++){.
1a8f5 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 p->aVar[n].fla
1a8f6 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1a8f7 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e p->aVar[n
1a8f8 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 ].db = db;.
1a8f9 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
1a8fa 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 p->aMem ){.
1a8fb 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 p->aMem--;
1a8fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a8fd 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 /* aMem[] goes
1a8fe 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f from 1..nMem */
1a8ff 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d . p->nMem =
1a900 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 nMem;
1a901 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20 20 20 /*
1a902 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d not from 0..nMem
1a903 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 -1 */. for(
1a904 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b n=1; n<=nMem; n+
1a905 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 +){. p->a
1a906 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d Mem[n].flags = M
1a907 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 EM_Null;.
1a908 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d p->aMem[n].db =
1a909 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 db;. }.
1a90a 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 }. }.#ifdef SQ
1a90b 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 LITE_DEBUG. for
1a90c 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b (n=1; n<p->nMem;
1a90d 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 n++){. asser
1a90e 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 t( p->aMem[n].db
1a90f 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 ==db );. }.#end
1a910 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 if.. p->pc = -1
1a911 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 ;. p->rc = SQLI
1a912 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f TE_OK;. p->erro
1a913 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f rAction = OE_Abo
1a914 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e rt;. p->explain
1a915 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 |= isExplain;.
1a916 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 p->magic = VDBE
1a917 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d _MAGIC_RUN;. p-
1a918 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 >nChange = 0;.
1a919 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 31 3b p->cacheCtr = 1;
1a91a 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 . p->minWriteFi
1a91b 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a leFormat = 255;.
1a91c 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 p->iStatement
1a91d 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42 45 = 0;.#ifdef VDBE
1a91e 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 _PROFILE. {.
1a91f 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 int i;. for(
1a920 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 i=0; i<p->nOp; i
1a921 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f ++){. p->aO
1a922 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 p[i].cnt = 0;.
1a923 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 p->aOp[i].cy
1a924 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a cles = 0;. }.
1a925 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a }.#endif.}../*
1a926 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 45 .** Close a VDBE
1a927 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 cursor and rele
1a928 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 6f ase all the reso
1a929 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73 6f urces that curso
1a92a 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f r .** happens to
1a92b 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 hold..*/.SQLITE
1a92c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a92d 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 lite3VdbeFreeCur
1a92e 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64 62 sor(Vdbe *p, Vdb
1a92f 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 eCursor *pCx){.
1a930 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 if( pCx==0 ){.
1a931 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
1a932 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29 7b if( pCx->pBt ){
1a933 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1a934 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 eClose(pCx->pBt)
1a935 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 78 ;. /* The pCx
1a936 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 ->pCursor will b
1a937 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 e close automati
1a938 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 69 cally, if it exi
1a939 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 sts, by. ** t
1a93a 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a he call above. *
1a93b 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 /. }else if( pC
1a93c 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 x->pCursor ){.
1a93d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
1a93e 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 oseCursor(pCx->p
1a93f 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 Cursor);. }.#if
1a940 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1a941 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1a942 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 75 if( pCx->pVtabCu
1a943 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 rsor ){. sqli
1a944 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1a945 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 *pVtabCursor = p
1a946 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b Cx->pVtabCursor;
1a947 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 . const sqlit
1a948 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1a949 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c le = pCx->pModul
1a94a 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 e;. p->inVtab
1a94b 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 Method = 1;.
1a94c 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1a94d 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 etyOff(p->db);.
1a94e 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f pModule->xClo
1a94f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 3b se(pVtabCursor);
1a950 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 . (void)sqlit
1a951 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 e3SafetyOn(p->db
1a952 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 );. p->inVtab
1a953 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a Method = 0;. }.
1a954 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
1a955 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 73 20 Copy the values
1a956 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 stored in the Vd
1a957 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72 beFrame structur
1a958 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e 20 54 e to its Vdbe. T
1a959 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 his.** is used,
1a95a 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 for example, whe
1a95b 6e 20 61 20 74 72 69 67 67 65 72 20 73 75 62 2d n a trigger sub-
1a95c 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c 74 65 program is halte
1a95d 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a 2a 20 d to restore.**
1a95e 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d control to the m
1a95f 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a ain program..*/.
1a960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1a961 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 nt sqlite3VdbeFr
1a962 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 46 ameRestore(VdbeF
1a963 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0a 20 rame *pFrame){.
1a964 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61 6d Vdbe *v = pFram
1a965 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d e->v;. v->aOp =
1a966 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 pFrame->aOp;.
1a967 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d v->nOp = pFrame-
1a968 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 >nOp;. v->aMem
1a969 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a = pFrame->aMem;.
1a96a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 v->nMem = pFra
1a96b 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 me->nMem;. v->a
1a96c 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 pCsr = pFrame->a
1a96d 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 pCsr;. v->nCurs
1a96e 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 or = pFrame->nCu
1a96f 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c rsor;. v->db->l
1a970 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72 61 6d astRowid = pFram
1a971 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 e->lastRowid;.
1a972 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 v->nChange = pFr
1a973 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 ame->nChange;.
1a974 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 return pFrame->p
1a975 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 c;.}../*.** Clos
1a976 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a e all cursors..*
1a977 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 *.** Also releas
1a978 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 e any dynamic me
1a979 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 mory held by the
1a97a 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62 65 2e VM in the Vdbe.
1a97b 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 aMem memory .**
1a97c 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 73 cell array. This
1a97d 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 is necessary as
1a97e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1a97f 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 array may conta
1a980 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 in.** pointers t
1a981 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 o VdbeFrame obje
1a982 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79 20 69 cts, which may i
1a983 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 n turn contain p
1a984 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 ointers to.** op
1a985 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 en cursors..*/.s
1a986 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 tatic void close
1a987 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20 AllCursors(Vdbe
1a988 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 *p){. if( p->pF
1a989 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 rame ){. Vdbe
1a98a 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 Frame *pFrame =
1a98b 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 20 66 p->pFrame;. f
1a98c 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 or(pFrame=p->pFr
1a98d 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 50 61 ame; pFrame->pPa
1a98e 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 46 72 rent; pFrame=pFr
1a98f 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 ame->pParent);.
1a990 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 sqlite3VdbeFr
1a991 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 6d ameRestore(pFram
1a992 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 72 e);. }. p->pFr
1a993 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 ame = 0;. p->nF
1a994 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 rame = 0;.. if(
1a995 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 20 20 p->apCsr ){.
1a996 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 int i;. for(
1a997 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f i=0; i<p->nCurso
1a998 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56 r; i++){. V
1a999 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 dbeCursor *pC =
1a99a 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20 p->apCsr[i];.
1a99b 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 20 20 if( pC ){.
1a99c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1a99d 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 43 FreeCursor(p, pC
1a99e 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 );. p->ap
1a99f 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 Csr[i] = 0;.
1a9a0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1a9a1 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 if( p->aMem ){.
1a9a2 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 releaseMemArr
1a9a3 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 ay(&p->aMem[1],
1a9a4 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d 0a p->nMem);. }.}.
1a9a5 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 ./*.** Clean up
1a9a6 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 65 the VM after exe
1a9a7 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 cution..**.** Th
1a9a8 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1a9a9 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c automatically cl
1a9aa 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 2c ose any cursors,
1a9ab 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a lists, and/or.*
1a9ac 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 77 * sorters that w
1a9ad 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 ere left open.
1a9ae 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 It also deletes
1a9af 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a the values of.**
1a9b0 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 variables in th
1a9b1 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a e aVar[] array..
1a9b2 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 43 */.static void C
1a9b3 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 7b leanup(Vdbe *p){
1a9b4 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1a9b5 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 66 20 p->db;..#ifdef
1a9b6 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f SQLITE_DEBUG. /
1a9b7 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 72 74 * Execute assert
1a9b8 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f () statements to
1a9b9 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 ensure that the
1a9ba 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e Vdbe.apCsr[] an
1a9bb 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d 65 d . ** Vdbe.aMe
1a9bc 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65 20 m[] arrays have
1a9bd 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c 65 already been cle
1a9be 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 20 69 aned up. */. i
1a9bf 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
1a9c0 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 i<p->nCursor; i
1a9c1 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ++) assert( p->a
1a9c2 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 70 pCsr==0 || p->ap
1a9c3 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 66 Csr[i]==0 );. f
1a9c4 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d or(i=1; i<=p->nM
1a9c5 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28 em; i++) assert(
1a9c6 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 70 p->aMem==0 || p
1a9c7 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d ->aMem[i].flags=
1a9c8 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 6e =MEM_Null );.#en
1a9c9 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 dif.. sqlite3Db
1a9ca 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
1a9cb 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d Msg);. p->zErrM
1a9cc 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 sg = 0;. p->pRe
1a9cd 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a sultSet = 0;.}..
1a9ce 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 /*.** Set the nu
1a9cf 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 mber of result c
1a9d0 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c olumns that will
1a9d1 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 be returned by
1a9d2 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 this SQL.** stat
1a9d3 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 6e ement. This is n
1a9d4 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c ow set at compil
1a9d5 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20 74 e time, rather t
1a9d6 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 han during.** ex
1a9d7 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ecution of the v
1a9d8 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 dbe program so t
1a9d9 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 hat sqlite3_colu
1a9da 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a mn_count() can.*
1a9db 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 * be called on a
1a9dc 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
1a9dd 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 before sqlite3_s
1a9de 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tep()..*/.SQLITE
1a9df 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a9e0 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 lite3VdbeSetNumC
1a9e1 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ols(Vdbe *p, int
1a9e2 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 nResColumn){.
1a9e3 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 Mem *pColName;.
1a9e4 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 int n;. sqlite
1a9e5 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 3 *db = p->db;..
1a9e6 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 releaseMemArra
1a9e7 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 y(p->aColName, p
1a9e8 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c ->nResColumn*COL
1a9e9 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 NAME_N);. sqlit
1a9ea 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1a9eb 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d aColName);. n =
1a9ec 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e nResColumn*COLN
1a9ed 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 AME_N;. p->nRes
1a9ee 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 6e 52 Column = (u16)nR
1a9ef 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 esColumn;. p->a
1a9f0 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 ColName = pColNa
1a9f1 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 me = (Mem*)sqlit
1a9f2 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
1a9f3 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e b, sizeof(Mem)*n
1a9f4 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f );. if( p->aCo
1a9f5 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 lName==0 ) retur
1a9f6 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 n;. while( n--
1a9f7 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e > 0 ){. pColN
1a9f8 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d ame->flags = MEM
1a9f9 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e _Null;. pColN
1a9fa 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b ame->db = p->db;
1a9fb 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b . pColName++;
1a9fc 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
1a9fd 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 t the name of th
1a9fe 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20 e idx'th column
1a9ff 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 to be returned b
1aa00 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d y the SQL statem
1aa01 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 ent..** zName mu
1aa02 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 st be a pointer
1aa03 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 to a nul termina
1aa04 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a ted string..**.*
1aa05 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 74 * This call must
1aa06 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 61 be made after a
1aa07 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
1aa08 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29 VdbeSetNumCols()
1aa09 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e 61 ..**.** The fina
1aa0a 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44 65 l parameter, xDe
1aa0b 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f l, must be one o
1aa0c 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 f SQLITE_DYNAMIC
1aa0d 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 0a , SQLITE_STATIC.
1aa0e 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52 41 ** or SQLITE_TRA
1aa0f 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69 73 NSIENT. If it is
1aa10 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c SQLITE_DYNAMIC,
1aa11 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 65 72 then the buffer
1aa12 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 pointed.** to b
1aa13 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65 20 y zName will be
1aa14 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 33 freed by sqlite3
1aa15 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 74 68 DbFree() when th
1aa16 65 20 76 64 62 65 20 69 73 20 64 65 73 74 72 6f e vdbe is destro
1aa17 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 yed..*/.SQLITE_P
1aa18 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1aa19 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 e3VdbeSetColName
1aa1a 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 (. Vdbe *p,
1aa1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa1c 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65 69 /* Vdbe bei
1aa1d 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f ng configured */
1aa1e 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20 20 . int idx,
1aa1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
1aa21 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 column zName app
1aa22 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 lies to */. int
1aa23 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 var,
1aa24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1aa25 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e One of the COLN
1aa26 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 AME_* constants
1aa27 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1aa28 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 *zName,
1aa29 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1aa2a 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 to buffer conta
1aa2b 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 ining name */.
1aa2c 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1aa2d 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 d*)
1aa2e 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 /* Memory manag
1aa2f 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 66 ement strategy f
1aa30 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 or zName */.){.
1aa31 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a int rc;. Mem *
1aa32 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65 pColName;. asse
1aa33 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43 rt( idx<p->nResC
1aa34 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72 olumn );. asser
1aa35 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e t( var<COLNAME_N
1aa36 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d );. if( p->db-
1aa37 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
1aa38 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 7a 4e . assert( !zN
1aa39 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51 4c ame || xDel!=SQL
1aa3a 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 ITE_DYNAMIC );.
1aa3b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1aa3c 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 _NOMEM;. }. as
1aa3d 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d sert( p->aColNam
1aa3e 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 e!=0 );. pColNa
1aa3f 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 me = &(p->aColNa
1aa40 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 me[idx+var*p->nR
1aa41 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 esColumn]);. rc
1aa42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
1aa43 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 mSetStr(pColName
1aa44 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c , zName, -1, SQL
1aa45 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b ITE_UTF8, xDel);
1aa46 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 30 . assert( rc!=0
1aa47 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 || !zName || (p
1aa48 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d ColName->flags&M
1aa49 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a 20 EM_Term)!=0 );.
1aa4a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1aa4b 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 77 *.** A read or w
1aa4c 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
1aa4d 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
1aa4e 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61 74 be active on dat
1aa4f 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 abase handle.**
1aa50 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 db. If a transac
1aa51 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 tion is active,
1aa52 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 68 commit it. If th
1aa53 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 74 ere is a.** writ
1aa54 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 70 e-transaction sp
1aa55 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e anning more than
1aa56 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 one database fi
1aa57 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 le, this routine
1aa58 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20 6f .** takes care o
1aa59 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 f the master jou
1aa5a 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a rnal trickery..*
1aa5b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 /.static int vdb
1aa5c 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 eCommit(sqlite3
1aa5d 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a 20 *db, Vdbe *p){.
1aa5e 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 int i;. int nT
1aa5f 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 rans = 0; /* Nu
1aa60 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 mber of database
1aa61 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 76 65 s with an active
1aa62 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
1aa63 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d on */. int rc =
1aa64 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
1aa65 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 t needXcommit =
1aa66 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;..#ifdef SQLIT
1aa67 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1aa68 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 74 68 BLE. /* With th
1aa69 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74 is option, sqlit
1aa6a 65 33 56 74 61 62 53 79 6e 63 28 29 20 69 73 20 e3VtabSync() is
1aa6b 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73 69 defined to be si
1aa6c 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c 49 54 mply . ** SQLIT
1aa6d 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74 E_OK so p is not
1aa6e 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 55 used. . */. U
1aa6f 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1aa70 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a p);.#endif.. /*
1aa71 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e Before doing an
1aa72 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c ything else, cal
1aa73 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61 l the xSync() ca
1aa74 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 llback for any.
1aa75 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 ** virtual modu
1aa76 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65 le tables writte
1aa77 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 n in this transa
1aa78 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 ction. This has
1aa79 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 to. ** be done
1aa7a 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 before determini
1aa7b 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73 ng whether a mas
1aa7c 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1aa7d 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 is . ** requir
1aa7e 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28 ed, as an xSync(
1aa7f 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 ) callback may a
1aa80 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 dd an attached d
1aa81 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 atabase. ** to
1aa82 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
1aa83 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c . */. rc = sql
1aa84 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c ite3VtabSync(db,
1aa85 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 &p->zErrMsg);.
1aa86 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1aa87 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
1aa88 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 rc;. }.. /* T
1aa89 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 his loop determi
1aa8a 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63 nes (a) if the c
1aa8b 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c ommit hook shoul
1aa8c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 d be invoked and
1aa8d 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 . ** (b) how ma
1aa8e 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ny database file
1aa8f 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74 s have open writ
1aa90 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 e transactions,
1aa91 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 not . ** includ
1aa92 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74 ing the temp dat
1aa93 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d abase. (b) is im
1aa94 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 portant because
1aa95 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 if more than .
1aa96 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 ** one database
1aa97 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e file has an open
1aa98 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1aa99 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 on, a master jou
1aa9a 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 rnal. ** file i
1aa9b 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 s required for a
1aa9c 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e n atomic commit.
1aa9d 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 . */ . for(i=0
1aa9e 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1aa9f 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70 ){ . Btree *p
1aaa0 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
1aaa1 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c pBt;. if( sql
1aaa2 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 ite3BtreeIsInTra
1aaa3 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 ns(pBt) ){.
1aaa4 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 needXcommit = 1
1aaa5 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 ;. if( i!=1
1aaa6 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 ) nTrans++;.
1aaa7 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
1aaa8 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 there are any wr
1aaa9 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 ite-transactions
1aaaa 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 at all, invoke
1aaab 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 the commit hook
1aaac 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f */. if( needXco
1aaad 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d mmit && db->xCom
1aaae 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 mitCallback ){.
1aaaf 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
1aab0 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 SafetyOff(db);.
1aab1 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d rc = db->xCom
1aab2 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e mitCallback(db->
1aab3 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 pCommitArg);.
1aab4 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1aab5 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 fetyOn(db);.
1aab6 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
1aab7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1aab8 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a NSTRAINT;. }.
1aab9 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 }.. /* The si
1aaba 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d mple case - no m
1aabb 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 ore than one dat
1aabc 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 abase file (not
1aabd 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a counting the. *
1aabe 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 * TEMP database)
1aabf 20 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 has a transacti
1aac0 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 on active. The
1aac1 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f re is no need fo
1aac2 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 r the. ** maste
1aac3 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a r-journal.. **.
1aac4 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 ** If the retu
1aac5 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 rn value of sqli
1aac6 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e te3BtreeGetFilen
1aac7 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 ame() is a zero
1aac8 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 length. ** stri
1aac9 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 ng, it means the
1aaca 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 main database i
1aacb 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 s :memory: or a
1aacc 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a temp file. In .
1aacd 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 ** that case w
1aace 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 e do not support
1aacf 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 atomic multi-fi
1aad0 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 le commits, so u
1aad1 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d se the . ** sim
1aad2 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f ple case then to
1aad3 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d o.. */. if( 0=
1aad4 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 =sqlite3Strlen30
1aad5 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 (sqlite3BtreeGet
1aad6 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 Filename(db->aDb
1aad7 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 [0].pBt)). ||
1aad8 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 nTrans<=1. ){.
1aad9 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d for(i=0; rc==
1aada 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 SQLITE_OK && i<d
1aadb 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 b->nDb; i++){.
1aadc 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
1aadd 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
1aade 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
1aadf 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
1aae0 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1aae1 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 tPhaseOne(pBt, 0
1aae2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1aae3 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 .. /* Do the
1aae4 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 commit only if a
1aae5 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63 ll databases suc
1aae6 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 cessfully comple
1aae7 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20 te phase 1. .
1aae8 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 ** If one of th
1aae9 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e BtreeCommitPha
1aaea 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 seOne() calls fa
1aaeb 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61 ils, this indica
1aaec 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f tes an. ** IO
1aaed 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c error while del
1aaee 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 eting or truncat
1aaef 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 ing a journal fi
1aaf0 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 le. It is unlike
1aaf1 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 ly,. ** but c
1aaf2 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 ould happen. In
1aaf3 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f this case abando
1aaf4 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 n processing and
1aaf5 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f return the erro
1aaf6 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f r.. */. fo
1aaf7 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 r(i=0; rc==SQLIT
1aaf8 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 E_OK && i<db->nD
1aaf9 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 b; i++){. B
1aafa 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e tree *pBt = db->
1aafb 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
1aafc 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 if( pBt ){.
1aafd 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1aafe 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 3BtreeCommitPhas
1aaff 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 eTwo(pBt);.
1ab00 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
1ab01 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ab02 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1ab03 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 tabCommit(db);.
1ab04 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 }. }.. /* T
1ab05 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 he complex case
1ab06 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 6c - There is a mul
1ab07 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 72 ti-file write-tr
1ab08 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 65 ansaction active
1ab09 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 75 .. ** This requ
1ab0a 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a 6f ires a master jo
1ab0b 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e urnal file to en
1ab0c 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 sure the transac
1ab0d 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d tion is. ** com
1ab0e 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e mitted atomicly.
1ab0f 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
1ab10 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f LITE_OMIT_DISKIO
1ab11 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c . else{. sql
1ab12 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d ite3_vfs *pVfs =
1ab13 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 db->pVfs;. i
1ab14 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b nt needSync = 0;
1ab15 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 . char *zMast
1ab16 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c er = 0; /* Fil
1ab17 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 6d e-name for the m
1ab18 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f aster journal */
1ab19 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 . char const
1ab1a 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c *zMainFile = sql
1ab1b 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 ite3BtreeGetFile
1ab1c 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e name(db->aDb[0].
1ab1d 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pBt);. sqlite
1ab1e 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3_file *pMaster
1ab1f 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 = 0;. i64 off
1ab20 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 set = 0;. int
1ab21 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 res;.. /* Se
1ab22 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a 6f lect a master jo
1ab23 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
1ab24 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 */. do {.
1ab25 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 u32 iRandom;.
1ab26 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1ab27 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
1ab28 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
1ab29 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
1ab2a 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e (iRandom), &iRan
1ab2b 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73 dom);. zMas
1ab2c 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 ter = sqlite3MPr
1ab2d 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25 intf(db, "%s-mj%
1ab2e 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 08X", zMainFile,
1ab2f 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 66 iRandom&0x7ffff
1ab30 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 fff);. if(
1ab31 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 !zMaster ){.
1ab32 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1ab33 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
1ab34 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1ab35 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 te3OsAccess(pVfs
1ab36 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 , zMaster, SQLIT
1ab37 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c E_ACCESS_EXISTS,
1ab38 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 69 &res);. }whi
1ab39 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
1ab3a 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 20 K && res );.
1ab3b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1ab3c 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 K ){. /* Op
1ab3d 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f en the master jo
1ab3e 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 urnal. */.
1ab3f 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 rc = sqlite3OsOp
1ab40 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a enMalloc(pVfs, z
1ab41 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 72 Master, &pMaster
1ab42 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c , . SQL
1ab43 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
1ab44 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 TE|SQLITE_OPEN_C
1ab45 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 20 REATE|.
1ab46 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
1ab47 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 LUSIVE|SQLITE_OP
1ab48 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 EN_MASTER_JOURNA
1ab49 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 L, 0. );.
1ab4a 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
1ab4b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1ab4c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1ab4d 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 (db, zMaster);.
1ab4e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1ab4f 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 }. . /* W
1ab50 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 rite the name of
1ab51 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 each database f
1ab52 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e 73 ile in the trans
1ab53 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 action into the
1ab54 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 new. ** maste
1ab55 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
1ab56 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
1ab57 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 rs at this point
1ab58 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e close. ** an
1ab59 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 d delete the mas
1ab5a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1ab5b 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 . All the indivi
1ab5c 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c dual journal fil
1ab5d 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 es. ** still
1ab5e 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 have 'null' as t
1ab5f 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
1ab60 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 l pointer, so th
1ab61 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 ey will roll.
1ab62 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e ** back indepen
1ab63 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 6c dently if a fail
1ab64 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 ure occurs..
1ab65 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
1ab66 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1ab67 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 . Btree *pB
1ab68 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
1ab69 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d Bt;. if( i=
1ab6a 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 =1 ) continue;
1ab6b 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 54 /* Ignore the T
1ab6c 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a EMP database */.
1ab6d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1ab6e 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 3BtreeIsInTrans(
1ab6f 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt) ){.
1ab70 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c char const *zFil
1ab71 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 e = sqlite3Btree
1ab72 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 GetJournalname(p
1ab73 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Bt);. if(
1ab74 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 zFile[0]==0 ) c
1ab75 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e ontinue; /* Ign
1ab76 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 ore :memory: dat
1ab77 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 abases */.
1ab78 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 20 if( !needSync
1ab79 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 && !sqlite3Btree
1ab7a 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 74 SyncDisabled(pBt
1ab7b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e ) ){. n
1ab7c 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 eedSync = 1;.
1ab7d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 }. r
1ab7e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
1ab7f 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c te(pMaster, zFil
1ab80 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e e, sqlite3Strlen
1ab81 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 30(zFile)+1, off
1ab82 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 set);. of
1ab83 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 fset += sqlite3S
1ab84 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 trlen30(zFile)+1
1ab85 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1ab86 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1ab87 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1ab88 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 OsCloseFree(pMas
1ab89 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ter);.
1ab8a 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
1ab8b 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 pVfs, zMaster, 0
1ab8c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1ab8d 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1ab8e 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 Master);.
1ab8f 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1ab90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1ab91 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 }.. /* Sy
1ab92 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f nc the master jo
1ab93 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 urnal file. If t
1ab94 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 he IOCAP_SEQUENT
1ab95 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 2a IAL device. *
1ab96 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 * flag is set th
1ab97 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 is is not requir
1ab98 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
1ab99 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 f( needSync .
1ab9a 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 65 33 && 0==(sqlite3
1ab9b 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
1ab9c 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 29 ristics(pMaster)
1ab9d 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 &SQLITE_IOCAP_SE
1ab9e 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 20 26 QUENTIAL). &
1ab9f 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 & SQLITE_OK!=(rc
1aba0 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
1aba1 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 (pMaster, SQLITE
1aba2 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 _SYNC_NORMAL)).
1aba3 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1aba4 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 te3OsCloseFree(p
1aba5 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 73 Master);. s
1aba6 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
1aba7 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 Vfs, zMaster, 0)
1aba8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 ;. sqlite3D
1aba9 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 bFree(db, zMaste
1abaa 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e r);. return
1abab 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
1abac 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 /* Sync all the
1abad 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 db files involve
1abae 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 d in the transac
1abaf 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 tion. The same c
1abb0 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 all. ** sets
1abb1 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1abb2 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 al pointer in ea
1abb3 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f ch individual jo
1abb4 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a urnal. If. **
1abb5 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1abb6 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 here, do not de
1abb7 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 lete the master
1abb8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 journal file..
1abb9 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 **. ** If t
1abba 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 he error occurs
1abbb 64 75 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 during the first
1abbc 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 call to. **
1abbd 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1abbe 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 itPhaseOne(), th
1abbf 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 63 68 en there is a ch
1abc0 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20 20 ance that the.
1abc1 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 ** master jour
1abc2 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 nal file will be
1abc3 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20 77 orphaned. But w
1abc4 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 e cannot delete
1abc5 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 it,. ** in ca
1abc6 73 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f se the master jo
1abc7 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
1abc8 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f was written into
1abc9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 the journal.
1abca 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 ** file before
1abcb 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63 75 the failure occu
1abcc 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 rred.. */.
1abcd 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 for(i=0; rc==SQ
1abce 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d LITE_OK && i<db-
1abcf 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 >nDb; i++){ .
1abd0 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 Btree *pBt =
1abd1 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1abd2 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b if( pBt ){
1abd3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1abd4 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1abd5 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d PhaseOne(pBt, zM
1abd6 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a aster);. }.
1abd7 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1abd8 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 3OsCloseFree(pMa
1abd9 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ster);. if( r
1abda 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1abdb 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1abdc 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 ree(db, zMaster)
1abdd 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
1abde 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
1abdf 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 Delete the mast
1abe0 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
1abe1 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 74 68 This commits th
1abe2 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 e transaction. A
1abe3 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f 69 6e fter. ** doin
1abe4 67 20 74 68 69 73 20 74 68 65 20 64 69 72 65 63 g this the direc
1abe5 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64 20 61 tory is synced a
1abe6 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e 79 20 gain before any
1abe7 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 20 2a individual. *
1abe8 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 * transaction fi
1abe9 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65 64 2e les are deleted.
1abea 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
1abeb 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1abec 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
1abed 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 1);. sqlite3D
1abee 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 bFree(db, zMaste
1abef 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 r);. zMaster
1abf0 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 20 = 0;. if( rc
1abf1 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1abf2 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f rc;. }.. /
1abf3 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 * All files and
1abf4 64 69 72 65 63 74 6f 72 69 65 73 20 68 61 76 65 directories have
1abf5 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 already been sy
1abf6 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c nced, so the fol
1abf7 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61 lowing. ** ca
1abf8 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 lls to sqlite3Bt
1abf9 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 reeCommitPhaseTw
1abfa 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f o() are only clo
1abfb 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a 20 sing files and.
1abfc 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f ** deleting o
1abfd 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 r truncating jou
1abfe 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68 rnals. If someth
1abff 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 20 77 ing goes wrong w
1ac00 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 hile. ** this
1ac01 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 65 is happening we
1ac02 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63 61 don't really ca
1ac03 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74 re. The integrit
1ac04 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 y of the. **
1ac05 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 transaction is a
1ac06 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74 65 65 lready guarantee
1ac07 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 72 61 d, but some stra
1ac08 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c y 'cold' journal
1ac09 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 65 20 s. ** may be
1ac0a 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65 lying around. Re
1ac0b 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 turning an error
1ac0c 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c 70 code won't help
1ac0d 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f matters.. */
1ac0e 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73 69 6d . disable_sim
1ac0f 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
1ac10 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ();. sqlite3B
1ac11 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
1ac12 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b ();. for(i=0;
1ac13 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
1ac14 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a { . Btree *
1ac15 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
1ac16 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 .pBt;. if(
1ac17 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 pBt ){. s
1ac18 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1ac19 74 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b 0a tPhaseTwo(pBt);.
1ac1a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1ac1b 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
1ac1c 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
1ac1d 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 enable_simulated
1ac1e 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 0a 20 _io_errors();..
1ac1f 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f sqlite3VtabCo
1ac20 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 mmit(db);. }.#e
1ac21 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 ndif.. return r
1ac22 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 c;.}../* .** Thi
1ac23 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
1ac24 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 that the sqlite
1ac25 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 3.activeVdbeCnt
1ac26 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0a 2a count variable.*
1ac27 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e 75 * matches the nu
1ac28 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 20 69 mber of vdbe's i
1ac29 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69 74 n the list sqlit
1ac2a 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 61 72 e3.pVdbe that ar
1ac2b 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 61 e.** currently a
1ac2c 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72 74 ctive. An assert
1ac2d 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68 65 ion fails if the
1ac2e 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20 6e two counts do n
1ac2f 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 69 ot match..** Thi
1ac30 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c s is an internal
1ac31 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 self-check only
1ac32 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 6e 20 - it is not an
1ac33 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 65 73 essential proces
1ac34 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a sing.** step..**
1ac35 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f .** This is a no
1ac36 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20 69 73 -op if NDEBUG is
1ac37 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 defined..*/.#if
1ac38 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 ndef NDEBUG.stat
1ac39 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 63 74 ic void checkAct
1ac3a 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c 69 74 iveVdbeCnt(sqlit
1ac3b 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 e3 *db){. Vdbe
1ac3c 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 *p;. int cnt =
1ac3d 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 0;. int nWrite
1ac3e 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70 = 0;. p = db->p
1ac3f 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 Vdbe;. while( p
1ac40 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d ){. if( p->m
1ac41 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1ac42 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 _RUN && p->pc>=0
1ac43 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b ){. cnt++;
1ac44 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 65 . if( p->re
1ac45 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69 adOnly==0 ) nWri
1ac46 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 te++;. }.
1ac47 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 p = p->pNext;.
1ac48 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d }. assert( cnt=
1ac49 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 =db->activeVdbeC
1ac4a 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nt );. assert(
1ac4b 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 69 74 nWrite==db->writ
1ac4c 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 eVdbeCnt );.}.#e
1ac4d 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68 65 63 lse.#define chec
1ac4e 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 78 kActiveVdbeCnt(x
1ac4f 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
1ac50 46 6f 72 20 65 76 65 72 79 20 42 74 72 65 65 20 For every Btree
1ac51 74 68 61 74 20 69 6e 20 64 61 74 61 62 61 73 65 that in database
1ac52 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 connection db w
1ac53 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 65 65 hich .** has bee
1ac54 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 72 69 n modified, "tri
1ac55 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 p" or invalidate
1ac56 20 65 61 63 68 20 63 75 72 73 6f 72 20 69 6e 0a each cursor in.
1ac57 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20 6d 69 ** that Btree mi
1ac58 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6d 6f ght have been mo
1ac59 64 69 66 69 65 64 20 73 6f 20 74 68 61 74 20 74 dified so that t
1ac5a 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 61 6e he cursor.** can
1ac5b 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 never be used a
1ac5c 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70 70 gain. This happ
1ac5d 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 ens when a rollb
1ac5e 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 ack.*** occurs.
1ac5f 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 69 70 We have to trip
1ac60 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 63 all the other c
1ac61 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 ursors, even.**
1ac62 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68 65 cursor from othe
1ac63 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72 65 r VMs in differe
1ac64 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e nt database conn
1ac65 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 ections,.** so t
1ac66 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d hat none of them
1ac67 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 65 20 try to use the
1ac68 64 61 74 61 20 61 74 20 77 68 69 63 68 20 74 68 data at which th
1ac69 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 6e 74 ey.** were point
1ac6a 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20 6e 6f ing and which no
1ac6b 77 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 w may have been
1ac6c 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a 20 74 changed due.** t
1ac6d 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a o the rollback..
1ac6e 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 **.** Remember t
1ac6f 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b 20 63 hat a rollback c
1ac70 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c 65 73 an delete tables
1ac71 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a 2a 2a complete and.**
1ac72 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 61 67 reorder rootpag
1ac73 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20 6e 6f es. So it is no
1ac74 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a 75 73 t sufficient jus
1ac75 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 68 65 t to save.** the
1ac76 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 state of the cu
1ac77 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65 20 74 rsor. We have t
1ac78 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 o invalidate the
1ac79 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 cursor.** so th
1ac7a 61 74 20 69 74 20 69 73 20 6e 65 76 65 72 20 75 at it is never u
1ac7b 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 sed again..*/.st
1ac7c 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 atic void invali
1ac7d 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 dateCursorsOnMod
1ac7e 69 66 69 65 64 42 74 72 65 65 73 28 73 71 6c 69 ifiedBtrees(sqli
1ac7f 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 te3 *db){. int
1ac80 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c i;. for(i=0; i<
1ac81 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1ac82 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62 Btree *p = db
1ac83 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
1ac84 20 20 69 66 28 20 70 20 26 26 20 73 71 6c 69 74 if( p && sqlit
1ac85 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 e3BtreeIsInTrans
1ac86 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c (p) ){. sql
1ac87 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c ite3BtreeTripAll
1ac88 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49 54 Cursors(p, SQLIT
1ac89 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 7d 0a E_ABORT);. }.
1ac8a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 }.}../*.** If
1ac8b 74 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20 the Vdbe passed
1ac8c 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
1ac8d 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 ument opened a s
1ac8e 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
1ac8f 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 tion,.** close i
1ac90 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 t now. Argument
1ac91 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69 74 68 eOp must be eith
1ac92 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c er SAVEPOINT_ROL
1ac93 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 LBACK or.** SAVE
1ac94 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 POINT_RELEASE. I
1ac95 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e f it is SAVEPOIN
1ac96 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e T_ROLLBACK, then
1ac97 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a the statement.*
1ac98 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
1ac99 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 rolled back. If
1ac9a 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f 49 4e eOp is SAVEPOIN
1ac9b 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 T_RELEASE, then
1ac9c 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e the .** statemen
1ac9d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 t transaction is
1ac9e 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a commtted..**.**
1ac9f 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
1aca0 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 occurs, an SQLIT
1aca1 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f E_IOERR_XXX erro
1aca2 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
1aca3 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ed. .** Otherwis
1aca4 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a e SQLITE_OK..*/.
1aca5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1aca6 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c nt sqlite3VdbeCl
1aca7 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62 oseStatement(Vdb
1aca8 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a e *p, int eOp){.
1aca9 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 sqlite3 *const
1acaa 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 db = p->db;. i
1acab 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1acac 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 K;.. /* If p->i
1acad 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67 72 65 Statement is gre
1acae 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 ater than zero,
1acaf 74 68 65 6e 20 74 68 69 73 20 56 64 62 65 20 6f then this Vdbe o
1acb0 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 pened a . ** st
1acb1 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1acb2 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ion that should
1acb3 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 be closed here.
1acb4 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 The only excepti
1acb5 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 74 20 on. ** is that
1acb6 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 an IO error may
1acb7 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20 63 61 have occured, ca
1acb8 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e using an emergen
1acb9 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a cy rollback.. *
1acba 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 28 * In this case (
1acbb 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d db->nStatement==
1acbc 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 0), and there is
1acbd 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a nothing to do..
1acbe 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e */. if( db->n
1acbf 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e Statement && p->
1acc0 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 iStatement ){.
1acc1 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e int i;. con
1acc2 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e st int iSavepoin
1acc3 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e t = p->iStatemen
1acc4 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 t-1;.. assert
1acc5 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 ( eOp==SAVEPOINT
1acc6 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 _ROLLBACK || eOp
1acc7 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
1acc8 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ASE);. assert
1acc9 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 ( db->nStatement
1acca 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 >0 );. assert
1accb 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d ( p->iStatement=
1accc 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 =(db->nStatement
1accd 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 +db->nSavepoint)
1acce 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 );.. for(i=0
1accf 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1acd0 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72 63 ){ . int rc
1acd1 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 2 = SQLITE_OK;.
1acd2 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 Btree *pBt
1acd3 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1acd4 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 ;. if( pBt
1acd5 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 ){. if( e
1acd6 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f Op==SAVEPOINT_RO
1acd7 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 LLBACK ){.
1acd8 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 rc2 = sqlite
1acd9 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 3BtreeSavepoint(
1acda 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 pBt, SAVEPOINT_R
1acdb 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 70 6f OLLBACK, iSavepo
1acdc 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a int);. }.
1acdd 20 20 20 20 20 20 20 20 69 66 28 20 72 63 32 3d if( rc2=
1acde 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1acdf 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 rc2 = sq
1ace0 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f lite3BtreeSavepo
1ace1 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 int(pBt, SAVEPOI
1ace2 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 61 76 NT_RELEASE, iSav
1ace3 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 epoint);.
1ace4 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 }. if( r
1ace5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1ace6 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 rc = r
1ace7 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 c2;. }.
1ace8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1ace9 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d db->nStatement--
1acea 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d ;. p->iStatem
1aceb 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a ent = 0;.. /*
1acec 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e If the statemen
1aced 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 t transaction is
1acee 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 being rolled ba
1acef 63 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f 72 65 ck, also restore
1acf0 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 the . ** dat
1acf1 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 65 abase handles de
1acf2 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e ferred constrain
1acf3 74 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 68 65 t counter to the
1acf4 20 76 61 6c 75 65 20 69 74 20 68 61 64 20 77 68 value it had wh
1acf5 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 en . ** the s
1acf6 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1acf7 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e tion was opened.
1acf8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f 70 */. if( eOp
1acf9 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
1acfa 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 64 62 BACK ){. db
1acfb 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 ->nDeferredCons
1acfc 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e = p->nStmtDefCon
1acfd 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 s;. }. }. r
1acfe 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1acff 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 ** If SQLite is
1ad00 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 70 70 compiled to supp
1ad01 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 ort shared-cache
1ad02 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 65 20 mode and to be
1ad03 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a 20 74 threadsafe,.** t
1ad04 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 74 61 his routine obta
1ad05 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20 61 73 ins the mutex as
1ad06 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 sociated with ea
1ad07 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 ch BtShared stru
1ad08 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 6d 61 cture.** that ma
1ad09 79 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 y be accessed by
1ad0a 20 74 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 the VM passed a
1ad0b 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 s an argument. I
1ad0c 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a 2a 2a n doing so it.**
1ad0d 20 73 65 74 73 20 74 68 65 20 42 74 53 68 61 72 sets the BtShar
1ad0e 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66 20 ed.db member of
1ad0f 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 53 68 each of the BtSh
1ad10 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 2c ared structures,
1ad11 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 68 61 ensuring.** tha
1ad12 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 75 t the correct bu
1ad13 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 sy-handler callb
1ad14 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 ack is invoked i
1ad15 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a f required..**.*
1ad16 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e * If SQLite is n
1ad17 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 62 75 ot threadsafe bu
1ad18 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 73 t does support s
1ad19 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 hared-cache mode
1ad1a 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 , then.** sqlite
1ad1b 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 3BtreeEnterAll()
1ad1c 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 is invoked to s
1ad1d 65 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e et the BtShared.
1ad1e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 db variables.**
1ad1f 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 61 72 of all of BtShar
1ad20 65 64 20 73 74 72 75 63 74 75 72 65 73 20 61 63 ed structures ac
1ad21 63 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 cessible via the
1ad22 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1ad23 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 .** associated
1ad24 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f 66 20 with the VM. Of
1ad25 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 73 75 course only a su
1ad26 62 73 65 74 20 6f 66 20 74 68 65 73 65 20 73 74 bset of these st
1ad27 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 6c 6c ructures.** will
1ad28 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 be accessed by
1ad29 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 20 63 the VM, and we c
1ad2a 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e 62 74 ould use Vdbe.bt
1ad2b 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 75 72 reeMask to figur
1ad2c 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 65 74 e.** that subset
1ad2d 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72 65 20 out, but there
1ad2e 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 65 20 is no advantage
1ad2f 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a 0a to doing so..**.
1ad30 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 ** If SQLite is
1ad31 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 61 not threadsafe a
1ad32 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 nd does not supp
1ad33 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 ort shared-cache
1ad34 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a 20 66 mode, this.** f
1ad35 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
1ad36 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 op..*/.#ifndef S
1ad37 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
1ad38 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 D_CACHE.SQLITE_P
1ad39 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1ad3a 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 te3VdbeMutexArra
1ad3b 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 7b yEnter(Vdbe *p){
1ad3c 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
1ad3d 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
1ad3e 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 BtreeMutexArrayE
1ad3f 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 nter(&p->aMutex)
1ad40 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 ;.#else. sqlite
1ad41 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 70 3BtreeEnterAll(p
1ad42 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a ->db);.#endif.}.
1ad43 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
1ad44 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1ad45 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 61 alled when a tra
1ad46 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 nsaction opened
1ad47 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 by the database
1ad48 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 .** handle assoc
1ad49 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56 iated with the V
1ad4a 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 M passed as an a
1ad4b 72 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 rgument is about
1ad4c 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 to be .** commi
1ad4d 74 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 61 tted. If there a
1ad4e 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 re outstanding d
1ad4f 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 eferred foreign
1ad50 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a key constraint.*
1ad51 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 * violations, re
1ad52 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1ad53 52 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 R. Otherwise, SQ
1ad54 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 LITE_OK..**.** I
1ad55 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 f there are outs
1ad56 74 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 tanding FK viola
1ad57 74 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 66 tions and this f
1ad58 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
1ad59 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 .** SQLITE_ERROR
1ad5a 2c 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 , set the result
1ad5b 20 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51 of the VM to SQ
1ad5c 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
1ad5d 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 and write.** an
1ad5e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f error message to
1ad5f 20 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e it. Then return
1ad60 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a SQLITE_ERROR..*
1ad61 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1ad62 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
1ad63 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 Y.SQLITE_PRIVATE
1ad64 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1ad65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c CheckFk(Vdbe *p,
1ad66 20 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b 0a int deferred){.
1ad67 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1ad68 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 65 p->db;. if( (de
1ad69 66 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 ferred && db->nD
1ad6a 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c eferredCons>0) |
1ad6b 7c 20 28 21 64 65 66 65 72 72 65 64 20 26 26 20 | (!deferred &&
1ad6c 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 p->nFkConstraint
1ad6d 3e 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 >0) ){. p->rc
1ad6e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 = SQLITE_CONSTR
1ad6f 41 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 AINT;. p->err
1ad70 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 orAction = OE_Ab
1ad71 6f 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ort;. sqlite3
1ad72 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1ad73 72 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 rrMsg, db, "fore
1ad74 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 ign key constrai
1ad75 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 nt failed");.
1ad76 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1ad77 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 RROR;. }. retu
1ad78 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1ad79 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
1ad7a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
1ad7b 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 lled the when a
1ad7c 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 VDBE tries to ha
1ad7d 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 lt. If the VDBE
1ad7e 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 .** has made cha
1ad7f 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 nges and is in a
1ad80 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 utocommit mode,
1ad81 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 then commit thos
1ad82 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 e.** changes. I
1ad83 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 f a rollback is
1ad84 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 needed, then do
1ad85 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a the rollback..**
1ad86 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1ad87 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 is the only way
1ad88 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 to move the sta
1ad89 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a te of a VM from.
1ad8a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f ** SQLITE_MAGIC_
1ad8b 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 RUN to SQLITE_MA
1ad8c 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 GIC_HALT. It is
1ad8d 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 harmless to.**
1ad8e 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 call this on a V
1ad8f 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 M that is in the
1ad90 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 SQLITE_MAGIC_HA
1ad91 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 LT state..**.**
1ad92 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
1ad93 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f code. If the co
1ad94 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 mmit could not c
1ad95 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 omplete because
1ad96 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 of.** lock conte
1ad97 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 ntion, return SQ
1ad98 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 LITE_BUSY. If S
1ad99 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 QLITE_BUSY is re
1ad9a 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 turned, it.** me
1ad9b 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 ans the close di
1ad9c 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 d not happen and
1ad9d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 needs to be rep
1ad9e 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 eated..*/.SQLITE
1ad9f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1ada0 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 ite3VdbeHalt(Vdb
1ada1 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
1ada2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ada3 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
1ada4 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 to store transi
1ada5 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 ent return codes
1ada6 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
1ada7 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a b = p->db;.. /*
1ada8 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 This function c
1ada9 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 ontains the logi
1adaa 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 c that determine
1adab 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 s if a statement
1adac 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 or. ** transac
1adad 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d tion will be com
1adae 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 mitted or rolled
1adaf 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c back as a resul
1adb0 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 t of the. ** ex
1adb1 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 ecution of this
1adb2 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
1adb3 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 . **. ** If a
1adb4 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ny of the follow
1adb5 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 ing errors occur
1adb6 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 :. **. **
1adb7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a SQLITE_NOMEM. *
1adb8 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 * SQLITE_IOE
1adb9 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 RR. ** SQLI
1adba 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 TE_FULL. **
1adbb 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 SQLITE_INTERRUP
1adbc 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e T. **. ** Then
1adbd 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 the internal ca
1adbe 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 che might have b
1adbf 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 een left in an i
1adc0 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a nconsistent. **
1adc1 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 state. We need
1adc2 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 to rollback the
1adc3 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1adc4 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 action, if there
1adc5 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 is. ** one, or
1adc6 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 the complete tr
1adc7 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 ansaction if the
1adc8 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 re is no stateme
1adc9 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a nt transaction..
1adca 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 */.. if( p->d
1adcb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1adcc 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 ){. p->rc = S
1adcd 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
1adce 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f . closeAllCurso
1adcf 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e rs(p);. if( p->
1add0 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 magic!=VDBE_MAGI
1add1 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 C_RUN ){. ret
1add2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1add3 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 }. checkActive
1add4 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20 VdbeCnt(db);..
1add5 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20 /* No commit or
1add6 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 20 rollback needed
1add7 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 6e if the program n
1add8 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f 0a ever started */.
1add9 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 if( p->pc>=0 )
1adda 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 20 {. int mrc;
1addb 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 6f /* Primary erro
1addc 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72 r code from p->r
1addd 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 74 c */. int eSt
1adde 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20 atementOp = 0;.
1addf 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c int isSpecial
1ade0 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 Error;
1ade1 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 /* Set to true
1ade2 20 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 20 if a 'special'
1ade3 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a error */.. /*
1ade4 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 Lock all btrees
1ade5 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74 61 used by the sta
1ade6 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 tement */. sq
1ade7 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 lite3VdbeMutexAr
1ade8 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 rayEnter(p);..
1ade9 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f /* Check for o
1adea 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 ne of the specia
1adeb 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 l errors */.
1adec 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 78 mrc = p->rc & 0x
1aded 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ff;. assert(
1adee 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f p->rc!=SQLITE_IO
1adef 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 20 ERR_BLOCKED );
1adf0 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e 6f /* This error no
1adf1 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 2a longer exists *
1adf2 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c 45 /. isSpecialE
1adf3 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 rror = mrc==SQLI
1adf4 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d TE_NOMEM || mrc=
1adf5 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 =SQLITE_IOERR.
1adf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1adf7 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 || mrc==SQLIT
1adf8 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20 6d E_INTERRUPT || m
1adf9 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 3b rc==SQLITE_FULL;
1adfa 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 63 69 . if( isSpeci
1adfb 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 alError ){.
1adfc 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 79 /* If the query
1adfd 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 was read-only,
1adfe 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f we need do no ro
1adff 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f llback at all. O
1ae00 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20 20 therwise,.
1ae01 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68 20 ** proceed with
1ae02 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64 the special hand
1ae03 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ling.. */.
1ae04 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 if( !p->rea
1ae05 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51 dOnly || mrc!=SQ
1ae06 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29 LITE_INTERRUPT )
1ae07 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 6d {. if( (m
1ae08 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1ae09 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f || mrc==SQLITE_
1ae0a 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 65 73 FULL) && p->uses
1ae0b 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 StmtJournal ){.
1ae0c 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d eStatem
1ae0d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e entOp = SAVEPOIN
1ae0e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 T_ROLLBACK;.
1ae0f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ae10 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 66 /* We are f
1ae11 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 orced to roll ba
1ae12 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 74 72 ck the active tr
1ae13 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 ansaction. Befor
1ae14 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 e doing.
1ae15 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 ** so, abort a
1ae16 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 ny other stateme
1ae17 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65 20 nts this handle
1ae18 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 63 currently has ac
1ae19 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 tive..
1ae1a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 */. inv
1ae1b 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e alidateCursorsOn
1ae1c 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 64 ModifiedBtrees(d
1ae1d 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 b);. sq
1ae1e 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c lite3RollbackAll
1ae1f 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
1ae20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 sqlite3CloseSave
1ae21 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 points(db);.
1ae22 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f db->autoCo
1ae23 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 mmit = 1;.
1ae24 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1ae25 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 }.. /* Check
1ae26 66 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 66 6f for immediate fo
1ae27 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 reign key violat
1ae28 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 ions. */. if(
1ae29 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
1ae2a 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
1ae2b 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c e3VdbeCheckFk(p,
1ae2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 0);. }. .
1ae2d 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f /* If the auto
1ae2e 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 -commit flag is
1ae2f 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 set and this is
1ae30 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 the only active
1ae31 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20 56 writer . ** V
1ae32 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65 69 M, then we do ei
1ae33 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72 ther a commit or
1ae34 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 rollback of the
1ae35 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 current transac
1ae36 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 tion. . **.
1ae37 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 ** Note: This
1ae38 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 block also runs
1ae39 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 if one of the sp
1ae3a 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61 6e ecial errors han
1ae3b 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f dled . ** abo
1ae3c 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e ve has occurred.
1ae3d 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 . */. if(
1ae3e 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 !sqlite3VtabInS
1ae3f 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26 26 ync(db) . &&
1ae40 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1ae41 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72 69 . && db->wri
1ae42 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e 72 teVdbeCnt==(p->r
1ae43 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 20 eadOnly==0) .
1ae44 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d ){. if( p-
1ae45 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c >rc==SQLITE_OK |
1ae46 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f | (p->errorActio
1ae47 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 n==OE_Fail && !i
1ae48 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20 29 sSpecialError) )
1ae49 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 {. if( sq
1ae4a 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
1ae4b 28 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 (p, 1) ){.
1ae4c 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1ae4d 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 MutexArrayLeave(
1ae4e 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 &p->aMutex);.
1ae4f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
1ae50 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1ae51 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a }. /*
1ae52 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 The auto-commit
1ae53 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 flag is true, t
1ae54 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 he vdbe program
1ae55 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 20 0a was successful .
1ae56 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 69 ** or hi
1ae57 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 t an 'OR FAIL' c
1ae58 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68 onstraint and th
1ae59 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 72 ere are no defer
1ae5a 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 20 red foreign.
1ae5b 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 74 ** key const
1ae5c 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 20 75 raints to hold u
1ae5d 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f p the transactio
1ae5e 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 20 n. This means a
1ae5f 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 20 commit .
1ae60 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 ** is required.
1ae61 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d */. rc =
1ae62 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 vdbeCommit(db,
1ae63 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 p);. if(
1ae64 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
1ae65 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
1ae66 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 ite3BtreeMutexAr
1ae67 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 rayLeave(&p->aMu
1ae68 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 tex);.
1ae69 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
1ae6a 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 SY;. }els
1ae6b 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 e if( rc!=SQLITE
1ae6c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1ae6d 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 p->rc = rc;.
1ae6e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f sqlite3Ro
1ae6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 llbackAll(db);.
1ae70 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1ae71 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 db->nDef
1ae72 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 20 erredCons = 0;.
1ae73 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1ae74 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 CommitInternalCh
1ae75 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 20 anges(db);.
1ae76 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1ae77 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1ae78 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
1ae79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1ae7a 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d db->nStatement =
1ae7b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 0;. }else if
1ae7c 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d ( eStatementOp==
1ae7d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
1ae7e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1ae7f 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f || p->errorActio
1ae80 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 n==OE_Fail ){.
1ae81 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74 eStatement
1ae82 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 Op = SAVEPOINT_R
1ae83 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 ELEASE;. }e
1ae84 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 lse if( p->error
1ae85 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 Action==OE_Abort
1ae86 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 ){. eSta
1ae87 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 tementOp = SAVEP
1ae88 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 OINT_ROLLBACK;.
1ae89 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1ae8a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 75 invalidateCu
1ae8b 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 rsorsOnModifiedB
1ae8c 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 20 trees(db);.
1ae8d 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 sqlite3Rollba
1ae8e 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 ckAll(db);.
1ae8f 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 sqlite3CloseS
1ae90 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 avepoints(db);.
1ae91 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1ae92 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 ommit = 1;.
1ae93 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
1ae94 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e 74 /* If eStatement
1ae95 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 Op is non-zero,
1ae96 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 then a statement
1ae97 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 transaction nee
1ae98 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 ds to. ** be
1ae99 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c committed or rol
1ae9a 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 led back. Call s
1ae9b 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 qlite3VdbeCloseS
1ae9c 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 tatement() to.
1ae9d 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 ** do so. If t
1ae9e 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 his operation re
1ae9f 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 turns an error,
1aea0 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 and the current
1aea1 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a statement. **
1aea2 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 error code is S
1aea3 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 QLITE_OK or SQLI
1aea4 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 TE_CONSTRAINT, t
1aea5 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 6f hen set the erro
1aea6 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f r. ** code to
1aea7 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a the new value..
1aea8 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 */. if( e
1aea9 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 StatementOp ){.
1aeaa 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1aeab 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 3VdbeCloseStatem
1aeac 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 6e ent(p, eStatemen
1aead 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 tOp);. if(
1aeae 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 rc && (p->rc==SQ
1aeaf 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 LITE_OK || p->rc
1aeb0 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 ==SQLITE_CONSTRA
1aeb1 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 20 INT) ){.
1aeb2 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 p->rc = rc;.
1aeb3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1aeb4 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1aeb5 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 );. p->zE
1aeb6 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 rrMsg = 0;.
1aeb7 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 }. }. .
1aeb8 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 61 /* If this was a
1aeb9 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 n INSERT, UPDATE
1aeba 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e or DELETE and n
1aebb 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e o statement tran
1aebc 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 saction. ** h
1aebd 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 as been rolled b
1aebe 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 ack, update the
1aebf 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1aec0 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 ion change-count
1aec1 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 er. . */.
1aec2 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 if( p->changeCnt
1aec3 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 On ){. if(
1aec4 65 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 eStatementOp!=SA
1aec5 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b VEPOINT_ROLLBACK
1aec6 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1aec7 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 te3VdbeSetChange
1aec8 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 s(db, p->nChange
1aec9 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
1aeca 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1aecb 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 dbeSetChanges(db
1aecc 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
1aecd 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d p->nChange =
1aece 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 0;. }. .
1aecf 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 /* Rollback or
1aed0 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 6d commit any schem
1aed1 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 6f a changes that o
1aed2 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 ccurred. */.
1aed3 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 if( p->rc!=SQLIT
1aed4 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 E_OK && db->flag
1aed5 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 s&SQLITE_InternC
1aed6 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 hanges ){.
1aed7 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 sqlite3ResetInte
1aed8 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 rnalSchema(db, 0
1aed9 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 );. db->fla
1aeda 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 gs = (db->flags
1aedb 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 | SQLITE_InternC
1aedc 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a hanges);. }..
1aedd 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 /* Release t
1aede 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 he locks */.
1aedf 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
1aee0 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e xArrayLeave(&p->
1aee1 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 aMutex);. }..
1aee2 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 65 /* We have succe
1aee3 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 ssfully halted a
1aee4 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 4d nd closed the VM
1aee5 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 66 . Record this f
1aee6 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d act. */. if( p-
1aee7 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 >pc>=0 ){. db
1aee8 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2d ->activeVdbeCnt-
1aee9 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 72 -;. if( !p->r
1aeea 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 eadOnly ){.
1aeeb 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e db->writeVdbeCn
1aeec 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 t--;. }. a
1aeed 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 ssert( db->activ
1aeee 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 eVdbeCnt>=db->wr
1aeef 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 iteVdbeCnt );.
1aef0 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 }. p->magic = V
1aef1 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a DBE_MAGIC_HALT;.
1aef2 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 checkActiveVdb
1aef3 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 20 eCnt(db);. if(
1aef4 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
1aef5 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 led ){. p->rc
1aef6 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1aef7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 . }.. /* If th
1aef8 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c e auto-commit fl
1aef9 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 ag is set to tru
1aefa 65 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b e, then any lock
1aefb 73 20 74 68 61 74 20 77 65 72 65 20 68 65 6c 64 s that were held
1aefc 0a 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 . ** by connect
1aefd 69 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 20 ion db have now
1aefe 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 been released. C
1aeff 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 all sqlite3Conne
1af00 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 ctionUnlocked()
1af01 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 . ** to invoke
1af02 61 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e 6c any required unl
1af03 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock-notify callb
1af04 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 acks.. */. if(
1af05 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1af06 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f ){. sqlite3Co
1af07 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 nnectionUnlocked
1af08 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 (db);. }.. ass
1af09 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 ert( db->activeV
1af0a 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e dbeCnt>0 || db->
1af0b 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c autoCommit==0 ||
1af0c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d db->nStatement=
1af0d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 =0 );. return S
1af0e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a QLITE_OK;.}.../*
1af0f 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 6f .** Each VDBE ho
1af10 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f lds the result o
1af11 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e f the most recen
1af12 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 t sqlite3_step()
1af13 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 call.** in p->r
1af14 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 c. This routine
1af15 20 73 65 74 73 20 74 68 61 74 20 72 65 73 75 6c sets that resul
1af16 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 t back to SQLITE
1af17 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 _OK..*/.SQLITE_P
1af18 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1af19 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70 te3VdbeResetStep
1af1a 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 7b Result(Vdbe *p){
1af1b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 . p->rc = SQLIT
1af1c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
1af1d 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 61 lean up a VDBE a
1af1e 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 62 fter execution b
1af1f 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 ut do not delete
1af20 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 79 the VDBE just y
1af21 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 et..** Write any
1af22 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
1af23 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 into *pzErrMsg.
1af24 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 Return the resu
1af25 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 lt code..**.** A
1af26 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e fter this routin
1af27 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 44 e is run, the VD
1af28 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 61 BE should be rea
1af29 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 dy to be execute
1af2a 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a d.** again..**.*
1af2b 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 * To look at it
1af2c 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 69 another way, thi
1af2d 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 73 s routine resets
1af2e 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
1af2f 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 e.** virtual mac
1af30 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d hine from VDBE_M
1af31 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 AGIC_RUN or VDBE
1af32 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 6b _MAGIC_HALT back
1af33 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 to.** VDBE_MAGI
1af34 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 C_INIT..*/.SQLIT
1af35 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1af36 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 lite3VdbeReset(V
1af37 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 dbe *p){. sqlit
1af38 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 e3 *db;. db = p
1af39 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 ->db;.. /* If t
1af3a 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 he VM did not ru
1af3b 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 n to completion
1af3c 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 or if it encount
1af3d 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 ered an. ** err
1af3e 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 or, then it migh
1af3f 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 t not have been
1af40 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 2e halted properly.
1af41 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 So halt. ** i
1af42 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 t now.. */. (v
1af43 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1af44 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 yOn(db);. sqlit
1af45 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 e3VdbeHalt(p);.
1af46 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1af47 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 fetyOff(db);..
1af48 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 68 /* If the VDBE h
1af49 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 70 as be run even p
1af4a 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 artially, then t
1af4b 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 6f ransfer the erro
1af4c 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 r code. ** and
1af4d 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 error message fr
1af4e 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 6f om the VDBE into
1af4f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1af50 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 42 se structure. B
1af51 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 56 ut. ** if the V
1af52 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 65 DBE has just bee
1af53 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 74 n set to run but
1af54 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c has not actuall
1af55 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a 20 y executed any.
1af56 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ** instructions
1af57 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 20 yet, leave the
1af58 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 72 main database er
1af59 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ror information
1af5a 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a unchanged.. */.
1af5b 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 if( p->pc>=0 )
1af5c 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 {. if( p->zEr
1af5d 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 rMsg ){. sq
1af5e 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
1af5f 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
1af60 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 sqlite3ValueSetS
1af61 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 tr(db->pErr,-1,p
1af62 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 ->zErrMsg,SQLITE
1af63 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 _UTF8,SQLITE_TRA
1af64 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 NSIENT);. s
1af65 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1af66 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 alloc();. d
1af67 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e b->errCode = p->
1af68 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
1af69 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1af6a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 ErrMsg);. p
1af6b 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1af6c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e }else if( p->
1af6d 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 rc ){. sqli
1af6e 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e te3Error(db, p->
1af6f 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 rc, 0);. }els
1af70 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
1af71 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 Error(db, SQLITE
1af72 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 _OK, 0);. }.
1af73 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 }else if( p->rc
1af74 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 29 && p->expired )
1af75 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 {. /* The exp
1af76 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 65 ired flag was se
1af77 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 65 t on the VDBE be
1af78 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63 fore the first c
1af79 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 all. ** to sq
1af7a 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 6f lite3_step(). Fo
1af7b 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 r consistency (s
1af7c 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 65 ince sqlite3_ste
1af7d 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 p() was. ** c
1af7e 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 20 alled), set the
1af7f 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69 database error i
1af80 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 20 77 n this case as w
1af81 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ell.. */.
1af82 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c sqlite3Error(db,
1af83 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 p->rc, 0);.
1af84 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 sqlite3ValueSetS
1af85 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c tr(db->pErr, -1,
1af86 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c p->zErrMsg, SQL
1af87 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
1af88 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 _TRANSIENT);.
1af89 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1af8a 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1af8b 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d p->zErrMsg =
1af8c 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 0;. }.. /* Re
1af8d 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 claim all memory
1af8e 20 75 73 65 64 20 62 79 20 74 68 65 20 56 44 42 used by the VDB
1af8f 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 E. */. Cleanup
1af90 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 (p);.. /* Save
1af91 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d profiling inform
1af92 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 ation from this
1af93 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 VDBE run.. */.#
1af94 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
1af95 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 LE. {. FILE
1af96 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 *out = fopen("vd
1af97 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c be_profile.out",
1af98 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 6f "a");. if( o
1af99 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ut ){. int
1af9a 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 i;. fprintf
1af9b 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a (out, "---- ");.
1af9c 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
1af9d 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 <p->nOp; i++){.
1af9e 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f fprintf(o
1af9f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 ut, "%02x", p->a
1afa0 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 Op[i].opcode);.
1afa1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 72 }. fpr
1afa2 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
1afa3 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
1afa4 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a i<p->nOp; i++){.
1afa5 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 fprintf(
1afa6 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 out, "%6d %10lld
1afa7 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 %8lld ",.
1afa8 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 p->aOp[i].c
1afa9 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 nt,. p
1afaa 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c ->aOp[i].cycles,
1afab 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 . p->a
1afac 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d Op[i].cnt>0 ? p-
1afad 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 >aOp[i].cycles/p
1afae 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 ->aOp[i].cnt : 0
1afaf 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 . );.
1afb0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1afb1 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 rintOp(out, i, &
1afb2 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 p->aOp[i]);.
1afb3 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 }. fclose
1afb4 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (out);. }. }
1afb5 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 .#endif. p->mag
1afb6 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f ic = VDBE_MAGIC_
1afb7 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 INIT;. return p
1afb8 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 ->rc & db->errMa
1afb9 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c sk;.}. ./*.** Cl
1afba 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 74 ean up and delet
1afbb 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20 65 e a VDBE after e
1afbc 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 xecution. Retur
1afbd 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 n an integer whi
1afbe 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 ch is.** the res
1afbf 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 65 ult code. Write
1afc0 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 any error messa
1afc1 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a ge text into *pz
1afc2 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 ErrMsg..*/.SQLIT
1afc3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1afc4 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a lite3VdbeFinaliz
1afc5 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e e(Vdbe *p){. in
1afc6 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1afc7 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 ;. if( p->magic
1afc8 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e ==VDBE_MAGIC_RUN
1afc9 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 || p->magic==VD
1afca 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b BE_MAGIC_HALT ){
1afcb 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1afcc 33 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a 20 3VdbeReset(p);.
1afcd 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 assert( (rc &
1afce 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 p->db->errMask)
1afcf 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 ==rc );. }. sq
1afd0 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 lite3VdbeDelete(
1afd1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
1afd2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 .}../*.** Call t
1afd3 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f he destructor fo
1afd4 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65 r each auxdata e
1afd5 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e ntry in pVdbeFun
1afd6 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 c for which.** t
1afd7 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
1afd8 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 bit in mask is
1afd9 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20 clear. Auxdata
1afda 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33 entries beyond 3
1afdb 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20 1.** are always
1afdc 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 destroyed. To d
1afdd 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 estroy all auxda
1afde 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c ta entries, call
1afdf 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
1afe0 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a with mask==0..*
1afe1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1afe2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1afe3 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 eDeleteAuxData(V
1afe4 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 dbeFunc *pVdbeFu
1afe5 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 nc, int mask){.
1afe6 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
1afe7 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 0; i<pVdbeFunc->
1afe8 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nAux; i++){.
1afe9 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a struct AuxData *
1afea 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e pAux = &pVdbeFun
1afeb 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 c->apAux[i];.
1afec 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 if( (i>31 || !(
1afed 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c mask&(((u32)1)<<
1afee 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 i))) && pAux->pA
1afef 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ux ){. if(
1aff0 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b pAux->xDelete ){
1aff1 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 . pAux->x
1aff2 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 Delete(pAux->pAu
1aff3 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 x);. }.
1aff4 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 pAux->pAux = 0
1aff5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
1aff6 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 *.** Delete an e
1aff7 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 ntire VDBE..*/.S
1aff8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1aff9 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 id sqlite3VdbeDe
1affa 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 lete(Vdbe *p){.
1affb 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 sqlite3 *db;..
1affc 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 if( NEVER(p==0)
1affd 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 ) return;. db
1affe 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 = p->db;. if( p
1afff 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70 ->pPrev ){. p
1b000 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d ->pPrev->pNext =
1b001 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c p->pNext;. }el
1b002 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
1b003 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a db->pVdbe==p );.
1b004 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 db->pVdbe =
1b005 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 p->pNext;. }.
1b006 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a if( p->pNext ){.
1b007 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 p->pNext->pP
1b008 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a rev = p->pPrev;.
1b009 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d }. releaseMem
1b00a 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 Array(p->aVar, p
1b00b 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 ->nVar);. relea
1b00c 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 seMemArray(p->aC
1b00d 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 olName, p->nResC
1b00e 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 olumn*COLNAME_N)
1b00f 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72 ;. vdbeFreeOpAr
1b010 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 ray(db, p->aOp,
1b011 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 p->nOp);. sqlit
1b012 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1b013 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 aLabel);. sqlit
1b014 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1b015 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c aColName);. sql
1b016 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1b017 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 ->zSql);. p->ma
1b018 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
1b019 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 _DEAD;. sqlite3
1b01a 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 DbFree(db, p->pF
1b01b 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 ree);. sqlite3D
1b01c 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a bFree(db, p);.}.
1b01d 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
1b01e 20 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73 the cursor p is
1b01f 20 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f ready to read o
1b020 72 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 r write the row
1b021 74 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 to which it.** w
1b022 61 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e as last position
1b023 65 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65 ed. Return an e
1b024 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 rror code if an
1b025 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f OOM fault or I/O
1b026 20 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e error.** preven
1b027 74 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 ts us from posit
1b028 69 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f ioning the curso
1b029 72 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 r to its correct
1b02a 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a position..**.**
1b02b 20 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 If a MoveTo ope
1b02c 72 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e ration is pendin
1b02d 67 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 g on the given c
1b02e 75 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 ursor, then do t
1b02f 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f hat.** MoveTo no
1b030 77 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 w. If no move i
1b031 73 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b s pending, check
1b032 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 to see if the r
1b033 6f 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 ow has been.** d
1b034 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 eleted out from
1b035 75 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 under the cursor
1b036 20 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 and if it has,
1b037 6d 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a mark the row as.
1b038 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a ** a NULL row..*
1b039 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73 *.** If the curs
1b03a 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f or is already po
1b03b 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f inting to the co
1b03c 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68 rrect row and th
1b03d 61 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f at row has.** no
1b03e 74 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f t been deleted o
1b03f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 ut from under th
1b040 65 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 e cursor, then t
1b041 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
1b042 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 no-op..*/.SQLIT
1b043 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1b044 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
1b045 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 oveto(VdbeCursor
1b046 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 *p){. if( p->d
1b047 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b eferredMoveto ){
1b048 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 . int res, rc
1b049 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1b04a 54 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 TEST. extern
1b04b 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 int sqlite3_sear
1b04c 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 ch_count;.#endif
1b04d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1b04e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 isTable );. r
1b04f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1b050 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 MovetoUnpacked(p
1b051 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d ->pCursor, 0, p-
1b052 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 >movetoTarget, 0
1b053 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 , &res);. if(
1b054 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1b055 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 . p->lastRowi
1b056 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 d = p->movetoTar
1b057 67 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 get;. p->rowi
1b058 64 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 dIsValid = ALWAY
1b059 53 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a S(res==0) ?1:0;.
1b05a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 if( NEVER(re
1b05b 73 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 s<0) ){. rc
1b05c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e = sqlite3BtreeN
1b05d 65 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 ext(p->pCursor,
1b05e 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 &res);. if(
1b05f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
1b060 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
1b061 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 LITE_TEST. sq
1b062 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 lite3_search_cou
1b063 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 nt++;.#endif.
1b064 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 p->deferredMove
1b065 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 to = 0;. p->c
1b066 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
1b067 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 HE_STALE;. }els
1b068 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e e if( ALWAYS(p->
1b069 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 pCursor) ){.
1b06a 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 int hasMoved;.
1b06b 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
1b06c 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 e3BtreeCursorHas
1b06d 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 Moved(p->pCursor
1b06e 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 , &hasMoved);.
1b06f 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1b070 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 61 n rc;. if( ha
1b071 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 20 sMoved ){.
1b072 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d p->cacheStatus =
1b073 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1b074 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d p->nullRow =
1b075 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
1b076 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1b077 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
1b078 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
1b079 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 ns:.**.** sqlite
1b07a 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
1b07b 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ).** sqlite3Vdbe
1b07c 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a SerialTypeLen().
1b07d 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 ** sqlite3VdbeSe
1b07e 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c rialLen().** sql
1b07f 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 ite3VdbeSerialPu
1b080 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 t().** sqlite3Vd
1b081 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a beSerialGet().**
1b082 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 20 .** encapsulate
1b083 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 65 the code that se
1b084 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 rializes values
1b085 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 53 for storage in S
1b086 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e QLite.** data an
1b087 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2e d index records.
1b088 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 64 Each serialized
1b089 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 value consists
1b08a 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d of a.** 'serial-
1b08b 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 type' and a blob
1b08c 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 65 of data. The se
1b08d 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e 20 rial type is an
1b08e 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 8-byte unsigned.
1b08f 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 ** integer, stor
1b090 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e 0a ed as a varint..
1b091 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 **.** In an SQLi
1b092 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2c te index record,
1b093 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 the serial type
1b094 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 63 is stored direc
1b095 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 tly before.** th
1b096 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 e blob of data t
1b097 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f 6e hat it correspon
1b098 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c ds to. In a tabl
1b099 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 e record, all se
1b09a 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 rial.** types ar
1b09b 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 e stored at the
1b09c 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 start of the rec
1b09d 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f ord, and the blo
1b09e 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a bs of data at.**
1b09f 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 the end. Hence
1b0a0 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 these functions
1b0a1 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 allow the caller
1b0a2 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a to handle the.*
1b0a3 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 6e * serial-type an
1b0a4 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 65 d data blob sepe
1b0a5 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rately..**.** Th
1b0a6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c e following tabl
1b0a7 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 e describes the
1b0a8 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 20 various storage
1b0a9 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 61 classes for data
1b0aa 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c :.**.** serial
1b0ab 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 74 type byt
1b0ac 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 20 es of data
1b0ad 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d type.** ------
1b0ae 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d -------- ---
1b0af 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 ------------
1b0b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a ---------------.
1b0b1 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 ** 0
1b0b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
1b0b3 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a NULL.
1b0b4 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 ** 1
1b0b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20 1
1b0b6 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 signe
1b0b7 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 d integer.**
1b0b8 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 2
1b0b9 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20 2
1b0ba 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 signed inte
1b0bb 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 ger.** 3
1b0bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b0bd 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 73 3 s
1b0be 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
1b0bf 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 4
1b0c0 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 20 4
1b0c1 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 signed
1b0c2 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 integer.**
1b0c3 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5
1b0c4 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20 6
1b0c5 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 signed intege
1b0c6 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 20 r.** 6
1b0c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b0c8 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 8 sig
1b0c9 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 ned integer.**
1b0ca 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 20 7
1b0cb 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 20 8
1b0cc 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f 61 IEEE floa
1b0cd 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 t.** 8
1b0ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b0cf 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 0 Int
1b0d0 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a eger constant 0.
1b0d1 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 20 ** 9
1b0d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 0
1b0d3 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 Integ
1b0d4 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a er constant 1.**
1b0d5 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 20 10,11
1b0d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b0d7 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 reserve
1b0d8 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a d for expansion.
1b0d9 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 ** N>=12 and
1b0da 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 even (N-12
1b0db 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 0a )/2 BLOB.
1b0dc 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 ** N>=13 and
1b0dd 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 33 odd (N-13
1b0de 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 0a )/2 text.
1b0df 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 **.** The 8 and
1b0e0 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 64 9 types were add
1b0e1 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c ed in 3.3.0, fil
1b0e2 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 e format 4. Pri
1b0e3 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f or versions.** o
1b0e4 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f f SQLite will no
1b0e5 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 6f t understand tho
1b0e6 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e se serial types.
1b0e7 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .*/../*.** Retur
1b0e8 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 70 n the serial-typ
1b0e9 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 e for the value
1b0ea 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a stored in pMem..
1b0eb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b0ec 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 E u32 sqlite3Vdb
1b0ed 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 eSerialType(Mem
1b0ee 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f *pMem, int file_
1b0ef 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 format){. int f
1b0f0 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 lags = pMem->fla
1b0f1 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 gs;. int n;..
1b0f2 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 if( flags&MEM_Nu
1b0f3 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ll ){. return
1b0f4 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 0;. }. if( fl
1b0f5 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 ags&MEM_Int ){.
1b0f6 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
1b0f7 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 20 whether to use
1b0f8 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 1, 2, 4, 6 or 8
1b0f9 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 bytes. */.# de
1b0fa 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 28 fine MAX_6BYTE (
1b0fb 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 30 (((i64)0x0000800
1b0fc 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 0)<<32)-1). i
1b0fd 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 64 i = pMem->u.i
1b0fe 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 20 ;. u64 u;.
1b0ff 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 if( file_format
1b100 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 20 >=4 && (i&1)==i
1b101 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1b102 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 8+(u32)i;. }.
1b103 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 u = i<0 ? -i
1b104 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 3c : i;. if( u<
1b105 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 3b =127 ) return 1;
1b106 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 36 . if( u<=3276
1b107 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7 ) return 2;.
1b108 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 37 if( u<=8388607
1b109 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 ) return 3;.
1b10a 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 36 if( u<=21474836
1b10b 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 47 ) return 4;.
1b10c 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 if( u<=MAX_6B
1b10d 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b 0a YTE ) return 5;.
1b10e 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 return 6;.
1b10f 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 }. if( flags&ME
1b110 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 M_Real ){. re
1b111 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 turn 7;. }. as
1b112 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e sert( pMem->db->
1b113 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 mallocFailed ||
1b114 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d flags&(MEM_Str|M
1b115 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 EM_Blob) );. n
1b116 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 = pMem->n;. if(
1b117 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 flags & MEM_Zer
1b118 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d o ){. n += pM
1b119 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d em->u.nZero;. }
1b11a 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 . assert( n>=0
1b11b 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e 2a );. return ((n*
1b11c 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 2) + 12 + ((flag
1b11d 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b s&MEM_Str)!=0));
1b11e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1b11f 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 the length of t
1b120 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f he data correspo
1b121 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 nding to the sup
1b122 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 70 plied serial-typ
1b123 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1b124 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
1b125 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1b126 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 n(u32 serial_typ
1b127 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 6c e){. if( serial
1b128 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 _type>=12 ){.
1b129 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c 5f return (serial_
1b12a 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 type-12)/2;. }e
1b12b 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 lse{. static
1b12c 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b 5d const u8 aSize[]
1b12d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c = { 0, 1, 2, 3,
1b12e 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 4, 6, 8, 8, 0,
1b12f 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 0, 0, 0 };. r
1b130 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 eturn aSize[seri
1b131 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a al_type];. }.}.
1b132 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
1b133 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 74 on an architect
1b134 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d 65 ure with mixed-e
1b135 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a ndian floating .
1b136 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 ** points (ex: A
1b137 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 74 RM7) then swap t
1b138 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 73 he lower 4 bytes
1b139 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 with the .** up
1b13a 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 65 per 4 bytes. Re
1b13b 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 2e turn the result.
1b13c 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 .**.** For most
1b13d 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 architectures, t
1b13e 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a his is a no-op..
1b13f 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 **.** (later):
1b140 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 It is reported t
1b141 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d 69 o me that the mi
1b142 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c xed-endian probl
1b143 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 em.** on ARM7 is
1b144 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 47 an issue with G
1b145 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 CC, not with the
1b146 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 20 ARM7 chip. It
1b147 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 seems.** that ea
1b148 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 rly versions of
1b149 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 74 GCC stored the t
1b14a 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 34 wo words of a 64
1b14b 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e -bit.** float in
1b14c 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 the wrong order
1b14d 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 6f . And that erro
1b14e 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 61 r has been propa
1b14f 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 gated.** ever si
1b150 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 nce. The blame
1b151 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
1b152 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 6f ly with GCC, tho
1b153 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 ugh..** GCC migh
1b154 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 79 t have just copy
1b155 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 ing the problem
1b156 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d from a prior com
1b157 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 piler..** I am a
1b158 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 lso told that ne
1b159 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 wer versions of
1b15a 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 GCC that follow
1b15b 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 a different.** A
1b15c 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65 20 BI get the byte
1b15d 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a order right..**.
1b15e 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 73 ** Developers us
1b15f 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e ing SQLite on an
1b160 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d ARM7 should com
1b161 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 65 pile and run the
1b162 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f ir.** applicatio
1b163 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 n using -DSQLITE
1b164 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 73 _DEBUG=1 at leas
1b165 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 t once. With DE
1b166 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 BUG.** enabled,
1b167 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 6c some asserts bel
1b168 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 ow will ensure t
1b169 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 64 hat the byte ord
1b16a 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e er of.** floatin
1b16b 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 g point values i
1b16c 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a s correct..**.**
1b16d 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 46 (2007-08-30) F
1b16e 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 61 rank van Vugt ha
1b16f 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20 70 s studied this p
1b170 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a roblem closely.*
1b171 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 68 * and has send h
1b172 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 is findings to t
1b173 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c 6f he SQLite develo
1b174 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 pers. Frank.**
1b175 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d 65 writes that some
1b176 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f Linux kernels o
1b177 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f ffer floating po
1b178 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 int hardware.**
1b179 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 emulation that u
1b17a 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 ses only 32-bit
1b17b 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 61 mantissas instea
1b17c 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 d of a full .**
1b17d 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 69 48-bits as requi
1b17e 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45 20 red by the IEEE
1b17f 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 73 standard. (This
1b180 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 is the.** CONFI
1b181 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f 70 G_FPE_FASTFPE op
1b182 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 tion.) On such
1b183 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e systems, floatin
1b184 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 g point.** byte
1b185 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 swapping becomes
1b186 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 very complicate
1b187 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 6f d. To avoid pro
1b188 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 blems,.** the ne
1b189 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77 61 cessary byte swa
1b18a 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 64 pping is carried
1b18b 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 2d out using a 64-
1b18c 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 bit integer.** r
1b18d 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 2d ather than a 64-
1b18e 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e bit float. Fran
1b18f 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 61 k assures us tha
1b190 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 0a t the code here.
1b191 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d ** works for him
1b192 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 6c . We, the devel
1b193 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 opers, have no w
1b194 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e ay to independen
1b195 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 tly.** verify th
1b196 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 65 is, but Frank se
1b197 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 ems to know what
1b198 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 he is talking a
1b199 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 bout.** so we tr
1b19a 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 ust him..*/.#ifd
1b19b 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f ef SQLITE_MIXED_
1b19c 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
1b19d 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 6c AT.static u64 fl
1b19e 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 7b oatSwap(u64 in){
1b19f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 . union {. u
1b1a0 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 64 r;. u32 i[
1b1a1 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 2];. } u;. u32
1b1a2 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b t;.. u.r = in;
1b1a3 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 . t = u.i[0];.
1b1a4 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d u.i[0] = u.i[1]
1b1a5 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a ;. u.i[1] = t;.
1b1a6 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a return u.r;.}.
1b1a7 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 # define swapMix
1b1a8 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 edEndianFloat(X)
1b1a9 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 X = floatSwap(
1b1aa 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e X).#else.# defin
1b1ab 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 e swapMixedEndia
1b1ac 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 nFloat(X).#endif
1b1ad 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 ../*.** Write th
1b1ae 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 e serialized dat
1b1af 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 a blob for the v
1b1b0 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 alue stored in p
1b1b1 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 Mem into .** buf
1b1b2 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 . It is assumed
1b1b3 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 that the caller
1b1b4 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 has allocated su
1b1b5 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e 0a fficient space..
1b1b6 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1b1b7 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 mber of bytes wr
1b1b8 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 itten..**.** nBu
1b1b9 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 f is the amount
1b1ba 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 6e of space left in
1b1bb 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 buf[]. nBuf mu
1b1bc 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 st always be.**
1b1bd 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 large enough to
1b1be 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 20 hold the entire
1b1bf 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 field. Except,
1b1c0 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 0a if the field is.
1b1c1 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 61 ** a blob with a
1b1c2 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 zero-filled tai
1b1c3 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 l, then buf[] mi
1b1c4 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 20 ght be just the
1b1c5 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f right.** size to
1b1c6 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67 hold everything
1b1c7 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 20 except for the
1b1c8 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c zero-filled tail
1b1c9 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 . If buf[].** i
1b1ca 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 s only big enoug
1b1cb 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f h to hold the no
1b1cc 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 74 n-zero prefix, t
1b1cd 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 hen only write t
1b1ce 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e hat.** prefix in
1b1cf 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 to buf[]. But i
1b1d0 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 f buf[] is large
1b1d1 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 enough to hold
1b1d2 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 both the.** pref
1b1d3 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c 20 ix and the tail
1b1d4 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 70 then write the p
1b1d5 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 68 refix and set th
1b1d6 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a e tail to all.**
1b1d7 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 zeros..**.** Re
1b1d8 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
1b1d9 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c of bytes actuall
1b1da 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 y written into b
1b1db 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 uf[]. The numbe
1b1dc 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 6e r.** of bytes in
1b1dd 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 the zero-filled
1b1de 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 tail is include
1b1df 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 20 d in the return
1b1e0 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 value only.** if
1b1e1 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 72 those bytes wer
1b1e2 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 5b e zeroed in buf[
1b1e3 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 ]..*/ .SQLITE_PR
1b1e4 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
1b1e5 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 3VdbeSerialPut(u
1b1e6 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 8 *buf, int nBuf
1b1e7 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 , Mem *pMem, int
1b1e8 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 file_format){.
1b1e9 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
1b1ea 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1b1eb 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 rialType(pMem, f
1b1ec 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 ile_format);. u
1b1ed 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 32 len;.. /* In
1b1ee 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 2a teger and Real *
1b1ef 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 /. if( serial_t
1b1f0 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 6c ype<=7 && serial
1b1f1 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 _type>0 ){. u
1b1f2 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 64 v;. u32 i;
1b1f3 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f . if( serial_
1b1f4 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 type==7 ){.
1b1f5 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
1b1f6 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d v)==sizeof(pMem-
1b1f7 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d >r) );. mem
1b1f8 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 cpy(&v, &pMem->r
1b1f9 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 , sizeof(v));.
1b1fa 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 swapMixedEnd
1b1fb 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 ianFloat(v);.
1b1fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 }else{. v
1b1fd 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 = pMem->u.i;.
1b1fe 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d }. len = i =
1b1ff 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b200 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c alTypeLen(serial
1b201 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 _type);. asse
1b202 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 rt( len<=(u32)nB
1b203 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 uf );. while(
1b204 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 i-- ){. bu
1b205 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 78 f[i] = (u8)(v&0x
1b206 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d FF);. v >>=
1b207 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 8;. }. re
1b208 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 turn len;. }..
1b209 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 6c /* String or bl
1b20a 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 ob */. if( seri
1b20b 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 al_type>=12 ){.
1b20c 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
1b20d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 >n + ((pMem->fla
1b20e 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 gs & MEM_Zero)?p
1b20f 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a Mem->u.nZero:0).
1b210 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d 20 ==
1b211 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 (int)sqlite3Vdbe
1b212 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 SerialTypeLen(se
1b213 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 rial_type) );.
1b214 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1b215 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c n<=nBuf );. l
1b216 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 en = pMem->n;.
1b217 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 4d memcpy(buf, pM
1b218 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 em->z, len);.
1b219 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
1b21a 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
1b21b 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d len += pMem
1b21c 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 ->u.nZero;.
1b21d 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 assert( nBuf>=0
1b21e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 );. if( le
1b21f 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 7b n > (u32)nBuf ){
1b220 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 . len = (
1b221 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 u32)nBuf;.
1b222 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 }. memset(&
1b223 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c buf[pMem->n], 0,
1b224 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 len-pMem->n);.
1b225 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
1b226 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e len;. }.. /* N
1b227 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 ULL or constants
1b228 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 0 or 1 */. ret
1b229 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
1b22a 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 Deserialize the
1b22b 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 data blob pointe
1b22c 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 73 d to by buf as s
1b22d 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 61 erial type seria
1b22e 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 l_type.** and st
1b22f 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1b230 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 n pMem. Return
1b231 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1b232 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 tes read..*/ .SQ
1b233 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
1b234 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b235 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 alGet(. const u
1b236 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 75 nsigned char *bu
1b237 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 f, /* Buffer
1b238 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 to deserialize
1b239 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 from */. u32 se
1b23a 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 rial_type,
1b23b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 61 /* Seria
1b23c 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 69 l type to deseri
1b23d 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a alize */. Mem *
1b23e 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 20 pMem
1b23f 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
1b240 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 ry cell to write
1b241 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 value into */.)
1b242 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 69 {. switch( seri
1b243 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 63 al_type ){. c
1b244 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 ase 10: /* Res
1b245 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 erved for future
1b246 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 use */. case
1b247 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 11: /* Reserv
1b248 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 ed for future us
1b249 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 3a e */. case 0:
1b24a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 { /* NULL */.
1b24b 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b24c 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1b24d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1b24e 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a case 1: { /*
1b24f 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 1-byte signed i
1b250 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
1b251 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 pMem->u.i = (sig
1b252 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 3b ned char)buf[0];
1b253 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1b254 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1b255 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
1b256 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a 20 }. case 2:
1b257 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e { /* 2-byte sign
1b258 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 ed integer */.
1b259 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
1b25a 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 (((signed char)b
1b25b 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 uf[0])<<8) | buf
1b25c 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d [1];. pMem-
1b25d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1b25e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 ;. return 2
1b25f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1b260 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 3: { /* 3-byte
1b261 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a signed integer *
1b262 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
1b263 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 i = (((signed ch
1b264 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 ar)buf[0])<<16)
1b265 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 | (buf[1]<<8) |
1b266 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d buf[2];. pM
1b267 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1b268 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Int;. retur
1b269 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 3;. }. c
1b26a 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 ase 4: { /* 4-by
1b26b 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
1b26c 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d r */. pMem-
1b26d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c >u.i = (buf[0]<<
1b26e 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 24) | (buf[1]<<1
1b26f 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 6) | (buf[2]<<8)
1b270 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 | buf[3];.
1b271 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1b272 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 EM_Int;. re
1b273 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 turn 4;. }.
1b274 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 case 5: { /* 6
1b275 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 -byte signed int
1b276 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 eger */. u6
1b277 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 20 4 x = (((signed
1b278 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 char)buf[0])<<8)
1b279 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 | buf[1];.
1b27a 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 5d u32 y = (buf[2]
1b27b 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c <<24) | (buf[3]<
1b27c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c <16) | (buf[4]<<
1b27d 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 8) | buf[5];.
1b27e 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c x = (x<<32) |
1b27f 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e y;. pMem->
1b280 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b u.i = *(i64*)&x;
1b281 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1b282 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1b283 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 return 6;.
1b284 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a 20 }. case 6:
1b285 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e /* 8-byte sign
1b286 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 ed integer */.
1b287 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 case 7: { /* I
1b288 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 EEE floating poi
1b289 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 nt */. u64
1b28a 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b 0a x;. u32 y;.
1b28b 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 #if !defined(NDE
1b28c 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 BUG) && !defined
1b28d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f (SQLITE_OMIT_FLO
1b28e 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 ATING_POINT).
1b28f 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 /* Verify tha
1b290 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 66 t integers and f
1b291 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
1b292 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 61 6d lues use the sam
1b293 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 e. ** byte
1b294 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 order. Or, that
1b295 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 if SQLITE_MIXED
1b296 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c _ENDIAN_64BIT_FL
1b297 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 OAT is. **
1b298 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 2d defined that 64-
1b299 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 bit floating poi
1b29a 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 nt values really
1b29b 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 20 are mixed.
1b29c 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 ** endian..
1b29d 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 */. stati
1b29e 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 3d c const u64 t1 =
1b29f 20 28 28 75 36 34 29 30 78 33 66 66 30 30 30 30 ((u64)0x3ff0000
1b2a0 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 0)<<32;. st
1b2a1 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c atic const doubl
1b2a2 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 e r1 = 1.0;.
1b2a3 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 u64 t2 = t1;.
1b2a4 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e swapMixedEn
1b2a5 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 dianFloat(t2);.
1b2a6 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a assert( siz
1b2a7 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 eof(r1)==sizeof(
1b2a8 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 t2) && memcmp(&r
1b2a9 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 1, &t2, sizeof(r
1b2aa 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 1))==0 );.#endif
1b2ab 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 66 .. x = (buf
1b2ac 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b [0]<<24) | (buf[
1b2ad 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 1]<<16) | (buf[2
1b2ae 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a ]<<8) | buf[3];.
1b2af 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b 34 y = (buf[4
1b2b0 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 5d ]<<24) | (buf[5]
1b2b1 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c <<16) | (buf[6]<
1b2b2 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 <8) | buf[7];.
1b2b3 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 x = (x<<32)
1b2b4 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 73 | y;. if( s
1b2b5 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b erial_type==6 ){
1b2b6 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 . pMem->u
1b2b7 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a .i = *(i64*)&x;.
1b2b8 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c pMem->fl
1b2b9 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1b2ba 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1b2bb 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 assert( size
1b2bc 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 of(x)==8 && size
1b2bd 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 of(pMem->r)==8 )
1b2be 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d 69 ;. swapMi
1b2bf 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 xedEndianFloat(x
1b2c0 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 );. memcp
1b2c1 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 y(&pMem->r, &x,
1b2c2 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 sizeof(x));.
1b2c3 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b2c4 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 = sqlite3IsNaN(p
1b2c5 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 Mem->r) ? MEM_Nu
1b2c6 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 ll : MEM_Real;.
1b2c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
1b2c8 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 8;. }.
1b2c9 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 case 8: /* I
1b2ca 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 nteger 0 */.
1b2cb 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e case 9: { /* In
1b2cc 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 teger 1 */.
1b2cd 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 pMem->u.i = ser
1b2ce 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 ial_type-8;.
1b2cf 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b2d0 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 MEM_Int;. r
1b2d1 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 eturn 0;. }.
1b2d2 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
1b2d3 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 u32 len = (s
1b2d4 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 erial_type-12)/2
1b2d5 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 ;. pMem->z
1b2d6 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 = (char *)buf;.
1b2d7 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c pMem->n = l
1b2d8 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e en;. pMem->
1b2d9 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 xDel = 0;.
1b2da 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 26 if( serial_type&
1b2db 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 20 0x01 ){.
1b2dc 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b2dd 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 M_Str | MEM_Ephe
1b2de 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a m;. }else{.
1b2df 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c pMem->fl
1b2e0 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c ags = MEM_Blob |
1b2e1 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 MEM_Ephem;.
1b2e2 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
1b2e3 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a len;. }. }.
1b2e4 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a return 0;.}...
1b2e5 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
1b2e6 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 nKey-byte encodi
1b2e7 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 69 ng of a record i
1b2e8 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 n pKey[], parse
1b2e9 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e the.** record in
1b2ea 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 to a UnpackedRec
1b2eb 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 20 ord structure.
1b2ec 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
1b2ed 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 to.** that stru
1b2ee 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cture..**.** The
1b2ef 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
1b2f0 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 20 n might provide
1b2f1 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f 66 szSpace bytes of
1b2f2 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 memory.** space
1b2f3 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 69 at pSpace. Thi
1b2f4 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 75 s space can be u
1b2f5 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 sed to hold the
1b2f6 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 returned.** VDbe
1b2f7 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 72 ParsedRecord str
1b2f8 75 63 74 75 72 65 20 69 66 20 69 74 20 69 73 20 ucture if it is
1b2f9 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 large enough. I
1b2fa 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 f it is.** not b
1b2fb 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 ig enough, space
1b2fc 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
1b2fd 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
1b2fe 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ()..**.** The re
1b2ff 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 65 turned structure
1b300 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 should be close
1b301 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a d by a call to.*
1b302 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c * sqlite3VdbeDel
1b303 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
1b304 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f d()..*/ .SQLITE_
1b305 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64 PRIVATE Unpacked
1b306 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 Record *sqlite3V
1b307 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 dbeRecordUnpack(
1b308 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 . KeyInfo *pKey
1b309 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 Info, /* Inf
1b30a 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
1b30b 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 he record format
1b30c 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 */. int nKey,
1b30d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b30e 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e 61 Size of the bina
1b30f 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 ry record */. c
1b310 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
1b311 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e /* The bin
1b312 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 ary record */.
1b313 63 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20 char *pSpace,
1b314 20 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 /* Unalig
1b315 6e 65 64 20 73 70 61 63 65 20 61 76 61 69 6c 61 ned space availa
1b316 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ble to hold the
1b317 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 object */. int
1b318 73 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 20 szSpace
1b319 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 /* Size of pS
1b31a 70 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 pace[] in bytes
1b31b 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e */.){. const un
1b31c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 signed char *aKe
1b31d 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 y = (const unsig
1b31e 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 3b ned char *)pKey;
1b31f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1b320 64 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e d *p; /* The un
1b321 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 68 packed record th
1b322 61 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 72 at we will retur
1b323 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 n */. int nByte
1b324 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 ; /* Me
1b325 6d 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 65 mory space neede
1b326 64 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 d to hold p, in
1b327 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 bytes */. int d
1b328 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 75 ;. u32 idx;. u
1b329 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 16 u;
1b32a 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c /* Unsigned l
1b32b 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1b32c 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 u32 szHdr;. Me
1b32d 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e m *pMem;. int n
1b32e 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f Off; /
1b32f 2a 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 63 * Increase pSpac
1b330 65 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 74 e by this much t
1b331 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 o 8-byte align i
1b332 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a t */. . /*. *
1b333 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 69 * We want to shi
1b334 66 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 ft the pointer p
1b335 53 70 61 63 65 20 75 70 20 73 75 63 68 20 74 68 Space up such th
1b336 61 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 20 at it is 8-byte
1b337 61 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 aligned.. ** Th
1b338 75 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 63 us, we need to c
1b339 61 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 65 alculate a value
1b33a 2c 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 , nOff, between
1b33b 30 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 0 and 7, to shif
1b33c 74 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 t . ** it by.
1b33d 49 66 20 70 53 70 61 63 65 20 69 73 20 61 6c 72 If pSpace is alr
1b33e 65 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 67 eady 8-byte alig
1b33f 6e 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 ned, nOff should
1b340 20 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 be zero.. */.
1b341 20 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 51 nOff = (8 - (SQ
1b342 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 LITE_PTR_TO_INT(
1b343 70 53 70 61 63 65 29 20 26 20 37 29 29 20 26 20 pSpace) & 7)) &
1b344 37 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 7;. pSpace += n
1b345 4f 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d Off;. szSpace -
1b346 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 = nOff;. nByte
1b347 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 = ROUND8(sizeof(
1b348 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 UnpackedRecord))
1b349 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 + sizeof(Mem)*(
1b34a 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1b34b 2b 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 +1);. if( nByte
1b34c 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 20 >szSpace ){.
1b34d 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c p = sqlite3DbMal
1b34e 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d locRaw(pKeyInfo-
1b34f 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 >db, nByte);.
1b350 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
1b351 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 rn 0;. p->fla
1b352 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 gs = UNPACKED_NE
1b353 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 4b ED_FREE | UNPACK
1b354 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b ED_NEED_DESTROY;
1b355 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 . }else{. p
1b356 3d 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 = (UnpackedRecor
1b357 64 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 70 d*)pSpace;. p
1b358 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b ->flags = UNPACK
1b359 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b ED_NEED_DESTROY;
1b35a 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e . }. p->pKeyIn
1b35b 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 fo = pKeyInfo;.
1b35c 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 p->nField = pKe
1b35d 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 yInfo->nField +
1b35e 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 1;. p->aMem = p
1b35f 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 Mem = (Mem*)&((c
1b360 68 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 har*)p)[ROUND8(s
1b361 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 izeof(UnpackedRe
1b362 63 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 cord))];. asser
1b363 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
1b364 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b IGNMENT(pMem) );
1b365 0a 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 69 . idx = getVari
1b366 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 nt32(aKey, szHdr
1b367 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a );. d = szHdr;.
1b368 20 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 u = 0;. while
1b369 28 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 75 ( idx<szHdr && u
1b36a 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c <p->nField && d<
1b36b 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 32 =nKey ){. u32
1b36c 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 serial_type;..
1b36d 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 72 idx += getVar
1b36e 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 5d int32(&aKey[idx]
1b36f 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a , serial_type);.
1b370 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
1b371 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 pKeyInfo->enc;.
1b372 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b pMem->db = pK
1b373 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 eyInfo->db;.
1b374 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b pMem->flags = 0;
1b375 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c . pMem->zMall
1b376 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d oc = 0;. d +=
1b377 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b378 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 alGet(&aKey[d],
1b379 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 serial_type, pMe
1b37a 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a m);. pMem++;.
1b37b 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 u++;. }. a
1b37c 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e ssert( u<=pKeyIn
1b37d 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 fo->nField + 1 )
1b37e 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 ;. p->nField =
1b37f 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 u;. return (voi
1b380 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 d*)p;.}../*.** T
1b381 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 his routine dest
1b382 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 roys a UnpackedR
1b383 65 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f ecord object..*/
1b384 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b385 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1b386 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 DeleteUnpackedRe
1b387 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 cord(UnpackedRec
1b388 6f 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 ord *p){. int i
1b389 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a ;. Mem *pMem;..
1b38a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
1b38b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 ;. assert( p->f
1b38c 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f lags & UNPACKED_
1b38d 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a NEED_DESTROY );.
1b38e 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d for(i=0, pMem=
1b38f 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 p->aMem; i<p->nF
1b390 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b ield; i++, pMem+
1b391 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 +){. /* The u
1b392 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 69 npacked record i
1b393 73 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 s always constru
1b394 63 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 20 cted by the.
1b395 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 6e ** sqlite3VdbeUn
1b396 70 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 6e packRecord() fun
1b397 63 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 ction above, whi
1b398 63 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 ch makes all.
1b399 20 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 ** strings and
1b39a 62 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 41 blobs static. A
1b39b 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 nd none of the e
1b39c 6c 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 20 lements are.
1b39d 2a 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f 72 ** ever transfor
1b39e 6d 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 med, so there is
1b39f 20 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 20 never anything
1b3a0 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a to delete.. *
1b3a1 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 /. if( NEVER(
1b3a2 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 pMem->zMalloc) )
1b3a3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b3a4 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
1b3a5 7d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 }. if( p->flags
1b3a6 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 & UNPACKED_NEED
1b3a7 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 6c _FREE ){. sql
1b3a8 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 4b ite3DbFree(p->pK
1b3a9 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a eyInfo->db, p);.
1b3aa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
1b3ab 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 s function compa
1b3ac 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 6c res the two tabl
1b3ad 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 e rows or index
1b3ae 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 records.** speci
1b3af 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 fied by {nKey1,
1b3b0 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 pKey1} and pPKey
1b3b1 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 2. It returns a
1b3b2 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a negative, zero.
1b3b3 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 ** or positive i
1b3b4 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 69 nteger if key1 i
1b3b5 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 s less than, equ
1b3b6 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 al to or .** gre
1b3b7 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 ater than key2.
1b3b8 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 The {nKey1, pKe
1b3b9 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 20 y1} key must be
1b3ba 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 a blob.** create
1b3bb 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 d by th OP_MakeR
1b3bc 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 ecord opcode of
1b3bd 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 70 the VDBE. The p
1b3be 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 PKey2.** key mus
1b3bf 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b 65 t be a parsed ke
1b3c0 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 6e y such as obtain
1b3c1 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 ed from.** sqlit
1b3c2 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f 72 e3VdbeParseRecor
1b3c3 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e d..**.** Key1 an
1b3c4 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 d Key2 do not ha
1b3c5 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 ve to contain th
1b3c6 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 e same number of
1b3c7 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 fields..** The
1b3c8 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 66 key with fewer f
1b3c9 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c 79 ields is usually
1b3ca 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 74 compares less t
1b3cb 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 han the .** long
1b3cc 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 er key. However
1b3cd 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 if the UNPACKED
1b3ce 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 69 _INCRKEY flags i
1b3cf 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 0a n pPKey2 is set.
1b3d0 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f ** and the commo
1b3d1 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20 65 n prefixes are e
1b3d2 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 qual, then key1
1b3d3 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 is less than key
1b3d4 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 2..** Or if the
1b3d5 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 UNPACKED_MATCH_P
1b3d6 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 65 REFIX flag is se
1b3d7 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 78 t and the prefix
1b3d8 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c es are.** equal,
1b3d9 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 61 then the keys a
1b3da 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f re considered to
1b3db 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a be equal and.**
1b3dc 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f 6e the parts beyon
1b3dd 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 d the common pre
1b3de 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 2e fix are ignored.
1b3df 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e .**.** If the UN
1b3e0 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f PACKED_IGNORE_RO
1b3e1 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 2c WID flag is set,
1b3e2 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 62 then the last b
1b3e3 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 yte of.** the he
1b3e4 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 73 ader of pKey1 is
1b3e5 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 ignored. It is
1b3e6 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 4b assumed that pK
1b3e7 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 ey1 is.** an ind
1b3e8 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 ex key, and thus
1b3e9 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f 77 ends with a row
1b3ea 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 6c id value. The l
1b3eb 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 ast byte.** of t
1b3ec 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 74 he header will t
1b3ed 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 20 herefore be the
1b3ee 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 serial type of t
1b3ef 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 he rowid:.** one
1b3f0 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 of 1, 2, 3, 4,
1b3f1 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 5, 6, 8, or 9 -
1b3f2 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 69 the integer seri
1b3f3 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 al types..** The
1b3f4 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 serial type of
1b3f5 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 the final rowid
1b3f6 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 61 will always be a
1b3f7 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a single byte..**
1b3f8 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 By ignoring thi
1b3f9 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 74 s last byte of t
1b3fa 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 6f he header, we fo
1b3fb 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 rce the comparis
1b3fc 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 on.** to ignore
1b3fd 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 the rowid at the
1b3fe 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f end of key1..*/
1b3ff 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b400 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 int sqlite3VdbeR
1b401 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 ecordCompare(.
1b402 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 int nKey1, const
1b403 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a void *pKey1, /*
1b404 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 Left key */. U
1b405 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 npackedRecord *p
1b406 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a 20 PKey2 /*
1b407 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a Right key */.){.
1b408 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 20 int d1;
1b409 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 /* Offset i
1b40a 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 nto aKey[] of ne
1b40b 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 xt data element
1b40c 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 20 */. u32 idx1;
1b40d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
1b40e 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 t into aKey[] of
1b40f 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c 65 next header ele
1b410 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a ment */. u32 sz
1b411 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 Hdr1; /*
1b412 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1b413 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 in header */. i
1b414 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 nt i = 0;. int
1b415 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 nField;. int rc
1b416 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e = 0;. const un
1b417 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 signed char *aKe
1b418 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 y1 = (const unsi
1b419 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 gned char *)pKey
1b41a 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 1;. KeyInfo *pK
1b41b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 eyInfo;. Mem me
1b41c 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 m1;.. pKeyInfo
1b41d 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e = pPKey2->pKeyIn
1b41e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d fo;. mem1.enc =
1b41f 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a pKeyInfo->enc;.
1b420 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 mem1.db = pKey
1b421 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 2f 2a 20 6d Info->db;. /* m
1b422 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b 20 20 em1.flags = 0;
1b423 2f 2f 20 57 69 6c 6c 20 62 65 20 69 6e 69 74 69 // Will be initi
1b424 61 6c 69 7a 65 64 20 62 79 20 73 71 6c 69 74 65 alized by sqlite
1b425 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 3VdbeSerialGet()
1b426 20 2a 2f 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 */. VVA_ONLY(
1b427 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 mem1.zMalloc = 0
1b428 3b 20 29 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64 ; ) /* Only need
1b429 65 64 20 62 79 20 61 73 73 65 72 74 28 29 20 73 ed by assert() s
1b42a 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 0a 20 20 tatements */..
1b42b 2f 2a 20 43 6f 6d 70 69 6c 65 72 73 20 6d 61 79 /* Compilers may
1b42c 20 63 6f 6d 70 6c 61 69 6e 20 74 68 61 74 20 6d complain that m
1b42d 65 6d 31 2e 75 2e 69 20 69 73 20 70 6f 74 65 6e em1.u.i is poten
1b42e 74 69 61 6c 6c 79 20 75 6e 69 6e 69 74 69 61 6c tially uninitial
1b42f 69 7a 65 64 2e 0a 20 20 2a 2a 20 57 65 20 63 6f ized.. ** We co
1b430 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 uld initialize i
1b431 74 2c 20 61 73 20 73 68 6f 77 6e 20 68 65 72 65 t, as shown here
1b432 2c 20 74 6f 20 73 69 6c 65 6e 63 65 20 74 68 6f , to silence tho
1b433 73 65 20 63 6f 6d 70 6c 61 69 6e 74 73 2e 0a 20 se complaints..
1b434 20 2a 2a 20 42 75 74 20 69 6e 20 66 61 63 74 2c ** But in fact,
1b435 20 6d 65 6d 31 2e 75 2e 69 20 77 69 6c 6c 20 6e mem1.u.i will n
1b436 65 76 65 72 20 61 63 74 75 61 6c 6c 79 20 62 65 ever actually be
1b437 20 75 73 65 64 20 69 6e 69 74 69 61 6c 69 7a 65 used initialize
1b438 64 2c 20 61 6e 64 20 64 6f 69 6e 67 20 0a 20 20 d, and doing .
1b439 2a 2a 20 74 68 65 20 75 6e 6e 65 63 65 73 73 61 ** the unnecessa
1b43a 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ry initializatio
1b43b 6e 20 68 61 73 20 61 20 6d 65 61 73 75 72 61 62 n has a measurab
1b43c 6c 65 20 6e 65 67 61 74 69 76 65 20 70 65 72 66 le negative perf
1b43d 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 69 6d 70 ormance. ** imp
1b43e 61 63 74 2c 20 73 69 6e 63 65 20 74 68 69 73 20 act, since this
1b43f 72 6f 75 74 69 6e 65 20 69 73 20 61 20 76 65 72 routine is a ver
1b440 79 20 68 69 67 68 20 72 75 6e 6e 65 72 2e 20 20 y high runner.
1b441 41 6e 64 20 73 6f 2c 20 77 65 20 63 68 6f 6f 73 And so, we choos
1b442 65 0a 20 20 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 e. ** to ignore
1b443 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 the compiler wa
1b444 72 6e 69 6e 67 73 20 61 6e 64 20 6c 65 61 76 65 rnings and leave
1b445 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 75 this variable u
1b446 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 ninitialized..
1b447 2a 2f 0a 20 20 2f 2a 20 20 6d 65 6d 31 2e 75 2e */. /* mem1.u.
1b448 69 20 3d 20 30 3b 20 20 2f 2f 20 6e 6f 74 20 6e i = 0; // not n
1b449 65 65 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73 eeded, here to s
1b44a 69 6c 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 ilence compiler
1b44b 77 61 72 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 warning */. .
1b44c 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e 74 idx1 = getVarint
1b44d 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72 31 32(aKey1, szHdr1
1b44e 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72 31 );. d1 = szHdr1
1b44f 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e ;. if( pPKey2->
1b450 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 flags & UNPACKED
1b451 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b _IGNORE_ROWID ){
1b452 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 . szHdr1--;.
1b453 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b }. nField = pK
1b454 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a eyInfo->nField;.
1b455 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 7a while( idx1<sz
1b456 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 32 Hdr1 && i<pPKey2
1b457 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 ->nField ){.
1b458 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 31 u32 serial_type1
1b459 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 ;.. /* Read t
1b45a 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 20 he serial types
1b45b 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c 65 for the next ele
1b45c 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 79 ment in each key
1b45d 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d . */. idx1 +=
1b45e 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 4b getVarint32( aK
1b45f 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 6c ey1+idx1, serial
1b460 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 66 _type1 );. if
1b461 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73 ( d1>=nKey1 && s
1b462 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b463 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 TypeLen(serial_t
1b464 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b 3b ype1)>0 ) break;
1b465 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 .. /* Extract
1b466 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62 the values to b
1b467 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 e compared..
1b468 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c */. d1 += sql
1b469 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 ite3VdbeSerialGe
1b46a 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 t(&aKey1[d1], se
1b46b 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d rial_type1, &mem
1b46c 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 1);.. /* Do t
1b46d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 he comparison.
1b46e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
1b46f 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 lite3MemCompare(
1b470 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e &mem1, &pPKey2->
1b471 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20 aMem[i],.
1b472 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b473 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70 i<nField ? p
1b474 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 KeyInfo->aColl[i
1b475 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ] : 0);. if(
1b476 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 rc!=0 ){. a
1b477 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d 61 6c ssert( mem1.zMal
1b478 6c 6f 63 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 65 loc==0 ); /* Se
1b479 65 20 63 6f 6d 6d 65 6e 74 20 62 65 6c 6f 77 20 e comment below
1b47a 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 */.. /* Inv
1b47b 65 72 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 ert the result i
1b47c 66 20 77 65 20 61 72 65 20 75 73 69 6e 67 20 44 f we are using D
1b47d 45 53 43 20 73 6f 72 74 20 6f 72 64 65 72 2e 20 ESC sort order.
1b47e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4b 65 */. if( pKe
1b47f 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 yInfo->aSortOrde
1b480 72 20 26 26 20 69 3c 6e 46 69 65 6c 64 20 26 26 r && i<nField &&
1b481 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
1b482 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 Order[i] ){.
1b483 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 rc = -rc;.
1b484 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 }. .
1b485 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 49 /* If the PREFI
1b486 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 73 X_SEARCH flag is
1b487 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 65 set and all fie
1b488 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 66 lds except the f
1b489 69 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 72 6f inal. ** ro
1b48a 77 69 64 20 66 69 65 6c 64 20 77 65 72 65 20 65 wid field were e
1b48b 71 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 qual, then clear
1b48c 20 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 the PREFIX_SEAR
1b48d 43 48 20 66 6c 61 67 20 61 6e 64 20 73 65 74 20 CH flag and set
1b48e 0a 20 20 20 20 20 20 2a 2a 20 70 50 4b 65 79 32 . ** pPKey2
1b48f 2d 3e 72 6f 77 69 64 20 74 6f 20 74 68 65 20 76 ->rowid to the v
1b490 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 alue of the rowi
1b491 64 20 66 69 65 6c 64 20 69 6e 20 28 70 4b 65 79 d field in (pKey
1b492 31 2c 20 6e 4b 65 79 31 29 2e 0a 20 20 20 20 20 1, nKey1)..
1b493 20 2a 2a 20 54 68 69 73 20 69 73 20 75 73 65 64 ** This is used
1b494 20 62 79 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 by the OP_IsUni
1b495 71 75 65 20 6f 70 63 6f 64 65 2e 0a 20 20 20 20 que opcode..
1b496 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 */. if( (
1b497 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 pPKey2->flags &
1b498 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f UNPACKED_PREFIX_
1b499 53 45 41 52 43 48 29 20 26 26 20 69 3d 3d 28 70 SEARCH) && i==(p
1b49a 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 PKey2->nField-1)
1b49b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1b49c 72 74 28 20 69 64 78 31 3d 3d 73 7a 48 64 72 31 rt( idx1==szHdr1
1b49d 20 26 26 20 72 63 20 29 3b 0a 20 20 20 20 20 20 && rc );.
1b49e 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31 2e 66 assert( mem1.f
1b49f 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 lags & MEM_Int )
1b4a0 3b 0a 20 20 20 20 20 20 20 20 70 50 4b 65 79 32 ;. pPKey2
1b4a1 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 41 ->flags &= ~UNPA
1b4a2 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
1b4a3 43 48 3b 0a 20 20 20 20 20 20 20 20 70 50 4b 65 CH;. pPKe
1b4a4 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 y2->rowid = mem1
1b4a5 2e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 .u.i;. }.
1b4a6 20 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1b4a7 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b rc;. }. i+
1b4a8 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 +;. }.. /* No
1b4a9 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1b4aa 6e 20 69 73 20 65 76 65 72 20 75 73 65 64 20 6f n is ever used o
1b4ab 6e 20 6d 65 6d 31 2e 20 20 50 72 6f 76 65 20 74 n mem1. Prove t
1b4ac 68 69 73 20 75 73 69 6e 67 0a 20 20 2a 2a 20 74 his using. ** t
1b4ad 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
1b4ae 65 72 74 28 29 2e 20 20 49 66 20 74 68 65 20 61 ert(). If the a
1b4af 73 73 65 72 74 28 29 20 66 61 69 6c 73 2c 20 69 ssert() fails, i
1b4b0 74 20 69 6e 64 69 63 61 74 65 73 20 61 0a 20 20 t indicates a.
1b4b1 2a 2a 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 61 ** memory leak a
1b4b2 6e 64 20 61 20 6e 65 65 64 20 74 6f 20 63 61 6c nd a need to cal
1b4b3 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d l sqlite3VdbeMem
1b4b4 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 2e 0a Release(&mem1)..
1b4b5 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6d */. assert( m
1b4b6 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 em1.zMalloc==0 )
1b4b7 3b 0a 0a 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65 ;.. /* rc==0 he
1b4b8 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e re means that on
1b4b9 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61 e of the keys ra
1b4ba 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20 n out of fields
1b4bb 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 65 and. ** all the
1b4bc 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68 fields up to th
1b4bd 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 65 71 at point were eq
1b4be 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e 50 41 ual. If the UNPA
1b4bf 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20 20 2a CKED_INCRKEY. *
1b4c0 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 * flag is set, t
1b4c1 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20 74 69 hen break the ti
1b4c2 65 20 62 79 20 74 72 65 61 74 69 6e 67 20 6b 65 e by treating ke
1b4c3 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a 20 20 y2 as larger..
1b4c4 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43 4b 45 ** If the UPACKE
1b4c5 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 66 D_PREFIX_MATCH f
1b4c6 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e lag is set, then
1b4c7 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f keys with commo
1b4c8 6e 20 70 72 65 66 69 78 65 73 0a 20 20 2a 2a 20 n prefixes. **
1b4c9 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 are considered t
1b4ca 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 o be equal. Oth
1b4cb 65 72 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 erwise, the long
1b4cc 65 72 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 er key is the .
1b4cd 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20 ** larger. As
1b4ce 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 it happens, the
1b4cf 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 pPKey2 will alwa
1b4d0 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 ys be the longer
1b4d1 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 69 . ** if there i
1b4d2 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 2e 0a s a difference..
1b4d3 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 */. assert( r
1b4d4 63 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 50 c==0 );. if( pP
1b4d5 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e Key2->flags & UN
1b4d6 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 PACKED_INCRKEY )
1b4d7 7b 0a 20 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 {. rc = -1;.
1b4d8 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b 65 79 }else if( pPKey
1b4d9 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 2->flags & UNPAC
1b4da 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 KED_PREFIX_MATCH
1b4db 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 ){. /* Leave
1b4dc 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 7d 65 6c 73 rc==0 */. }els
1b4dd 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 64 72 e if( idx1<szHdr
1b4de 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 1 ){. rc = 1;
1b4df 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1b4e0 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 ;.}. ../*.** pCu
1b4e1 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 r points at an i
1b4e2 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 61 74 ndex entry creat
1b4e3 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f ed using the OP_
1b4e4 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 MakeRecord opcod
1b4e5 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72 e..** Read the r
1b4e6 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 20 66 owid (the last f
1b4e7 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f ield in the reco
1b4e8 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74 rd) and store it
1b4e9 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 in *rowid..** R
1b4ea 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
1b4eb 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f if everything wo
1b4ec 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 rks, or an error
1b4ed 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e code otherwise.
1b4ee 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68 .**.** pCur migh
1b4ef 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f t be pointing to
1b4f0 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 20 66 text obtained f
1b4f1 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 rom a corrupt da
1b4f2 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 tabase file..**
1b4f3 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 So the content c
1b4f4 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 annot be trusted
1b4f5 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 61 74 . Do appropriat
1b4f6 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 e checks on the
1b4f7 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 content..*/.SQLI
1b4f8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1b4f9 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 qlite3VdbeIdxRow
1b4fa 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 id(sqlite3 *db,
1b4fb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
1b4fc 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a 20 20 69 i64 *rowid){. i
1b4fd 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 64 nCellKey = 0;
1b4fe 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 . int rc;. u32
1b4ff 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 2f szHdr; /
1b500 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 * Size of the he
1b501 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 74 79 ader */. u32 ty
1b502 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53 peRowid; /* S
1b503 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 erial type of th
1b504 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 75 33 32 e rowid */. u32
1b505 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20 20 2f lenRowid; /
1b506 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 72 6f * Size of the ro
1b507 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d 2c 20 wid */. Mem m,
1b508 76 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 v;.. UNUSED_PAR
1b509 41 4d 45 54 45 52 28 64 62 29 3b 0a 0a 20 20 2f AMETER(db);.. /
1b50a 2a 20 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f * Get the size o
1b50b 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 f the index entr
1b50c 79 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 y. Only indices
1b50d 20 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73 entries of less
1b50e 0a 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 . ** than 2GiB
1b50f 61 72 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e are support - an
1b510 79 74 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 ything large mus
1b511 74 20 62 65 20 64 61 74 61 62 61 73 65 20 63 6f t be database co
1b512 72 72 75 70 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41 rruption.. ** A
1b513 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 ny corruption is
1b514 20 64 65 74 65 63 74 65 64 20 69 6e 20 73 71 6c detected in sql
1b515 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 ite3BtreeParseCe
1b516 6c 6c 50 74 72 28 29 2c 20 74 68 6f 75 67 68 2c llPtr(), though,
1b517 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 63 6f so. ** this co
1b518 64 65 20 63 61 6e 20 73 61 66 65 6c 79 20 61 73 de can safely as
1b519 73 75 6d 65 20 74 68 61 74 20 6e 43 65 6c 6c 4b sume that nCellK
1b51a 65 79 20 69 73 20 33 32 2d 62 69 74 73 20 20 0a ey is 32-bits .
1b51b 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
1b51c 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1b51d 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 rIsValid(pCur) )
1b51e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1b51f 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 BtreeKeySize(pCu
1b520 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 r, &nCellKey);.
1b521 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1b522 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 20 2f 2a ITE_OK ); /*
1b523 20 70 43 75 72 20 69 73 20 61 6c 77 61 79 73 20 pCur is always
1b524 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 valid so KeySize
1b525 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a cannot fail */.
1b526 20 20 61 73 73 65 72 74 28 20 28 6e 43 65 6c 6c assert( (nCell
1b527 4b 65 79 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 Key & SQLITE_MAX
1b528 5f 55 33 32 29 3d 3d 28 75 36 34 29 6e 43 65 6c _U32)==(u64)nCel
1b529 6c 4b 65 79 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 lKey );.. /* Re
1b52a 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65 ad in the comple
1b52b 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 te content of th
1b52c 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f e index entry */
1b52d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c . memset(&m, 0,
1b52e 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 sizeof(m));. r
1b52f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1b530 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72 emFromBtree(pCur
1b531 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b , 0, (int)nCellK
1b532 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 ey, 1, &m);. if
1b533 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 ( rc ){. retu
1b534 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
1b535 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 The index entry
1b536 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 74 68 must begin with
1b537 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 20 2a a header size *
1b538 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 /. (void)getVar
1b539 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 int32((u8*)m.z,
1b53a 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 61 szHdr);. testca
1b53b 73 65 28 20 73 7a 48 64 72 3d 3d 33 20 29 3b 0a se( szHdr==3 );.
1b53c 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 testcase( szHd
1b53d 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20 r==m.n );. if(
1b53e 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c 33 unlikely(szHdr<3
1b53f 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d || (int)szHdr>m
1b540 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 .n) ){. goto
1b541 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 idx_rowid_corrup
1b542 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 tion;. }.. /*
1b543 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 6f The last field o
1b544 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75 f the index shou
1b545 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 ld be an integer
1b546 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 20 - the ROWID..
1b547 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 ** Verify that t
1b548 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 72 65 he last entry re
1b549 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 67 ally is an integ
1b54a 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 er. */. (void)g
1b54b 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 etVarint32((u8*)
1b54c 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 &m.z[szHdr-1], t
1b54d 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 ypeRowid);. tes
1b54e 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 tcase( typeRowid
1b54f 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==1 );. testcas
1b550 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 e( typeRowid==2
1b551 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 );. testcase( t
1b552 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20 ypeRowid==3 );.
1b553 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 testcase( typeR
1b554 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73 owid==4 );. tes
1b555 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 tcase( typeRowid
1b556 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==5 );. testcas
1b557 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 20 e( typeRowid==6
1b558 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 );. testcase( t
1b559 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20 ypeRowid==8 );.
1b55a 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 testcase( typeR
1b55b 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28 owid==9 );. if(
1b55c 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f unlikely(typeRo
1b55d 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77 wid<1 || typeRow
1b55e 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 69 id>9 || typeRowi
1b55f 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f 74 d==7) ){. got
1b560 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 o idx_rowid_corr
1b561 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65 uption;. }. le
1b562 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 nRowid = sqlite3
1b563 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1b564 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 n(typeRowid);.
1b565 74 65 73 74 63 61 73 65 28 20 28 75 33 32 29 6d testcase( (u32)m
1b566 2e 6e 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 .n==szHdr+lenRow
1b567 69 64 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 id );. if( unli
1b568 6b 65 6c 79 28 28 75 33 32 29 6d 2e 6e 3c 73 7a kely((u32)m.n<sz
1b569 48 64 72 2b 6c 65 6e 52 6f 77 69 64 29 20 29 7b Hdr+lenRowid) ){
1b56a 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f . goto idx_ro
1b56b 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a wid_corruption;.
1b56c 20 20 7d 0a 0a 20 20 2f 2a 20 46 65 74 63 68 20 }.. /* Fetch
1b56d 74 68 65 20 69 6e 74 65 67 65 72 20 6f 66 66 20 the integer off
1b56e 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 the end of the i
1b56f 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 ndex record */.
1b570 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b571 61 6c 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b alGet((u8*)&m.z[
1b572 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 m.n-lenRowid], t
1b573 79 70 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 ypeRowid, &v);.
1b574 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b *rowid = v.u.i;
1b575 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b576 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 mRelease(&m);.
1b577 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1b578 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 ;.. /* Jump her
1b579 65 20 69 66 20 64 61 74 61 62 61 73 65 20 63 6f e if database co
1b57a 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 74 65 rruption is dete
1b57b 63 74 65 64 20 61 66 74 65 72 20 6d 20 68 61 73 cted after m has
1b57c 20 62 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 been. ** alloc
1b57d 61 74 65 64 2e 20 20 46 72 65 65 20 74 68 65 20 ated. Free the
1b57e 6d 20 6f 62 6a 65 63 74 20 61 6e 64 20 72 65 74 m object and ret
1b57f 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
1b580 50 54 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 PT. */.idx_rowid
1b581 5f 63 6f 72 72 75 70 74 69 6f 6e 3a 0a 20 20 74 _corruption:. t
1b582 65 73 74 63 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c estcase( m.zMall
1b583 6f 63 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 oc!=0 );. sqlit
1b584 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1b585 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 (&m);. return S
1b586 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1b587 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d PT;.}../*.** Com
1b588 70 61 72 65 20 74 68 65 20 6b 65 79 20 6f 66 20 pare the key of
1b589 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 the index entry
1b58a 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 20 69 that cursor pC i
1b58b 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 67 s pointing to ag
1b58c 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79 ainst.** the key
1b58d 20 73 74 72 69 6e 67 20 69 6e 20 70 55 6e 70 61 string in pUnpa
1b58e 63 6b 65 64 2e 20 20 57 72 69 74 65 20 69 6e 74 cked. Write int
1b58f 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 o *pRes a number
1b590 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 .** that is nega
1b591 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 tive, zero, or p
1b592 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73 ositive if pC is
1b593 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
1b594 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 l to,.** or grea
1b595 74 65 72 20 74 68 61 6e 20 70 55 6e 70 61 63 6b ter than pUnpack
1b596 65 64 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 ed. Return SQLI
1b597 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
1b598 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 ..**.** pUnpacke
1b599 64 20 69 73 20 65 69 74 68 65 72 20 63 72 65 61 d is either crea
1b59a 74 65 64 20 77 69 74 68 6f 75 74 20 61 20 72 6f ted without a ro
1b59b 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e 63 61 wid or is trunca
1b59c 74 65 64 20 73 6f 20 74 68 61 74 20 69 74 0a 2a ted so that it.*
1b59d 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f 77 69 * omits the rowi
1b59e 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 d at the end. T
1b59f 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 he rowid at the
1b5a0 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 end of the index
1b5a1 20 65 6e 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e entry.** is ign
1b5a2 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e 20 20 48 ored as well. H
1b5a3 65 6e 63 65 2c 20 74 68 69 73 20 72 6f 75 74 69 ence, this routi
1b5a4 6e 65 20 6f 6e 6c 79 20 63 6f 6d 70 61 72 65 73 ne only compares
1b5a5 20 74 68 65 20 70 72 65 66 69 78 65 73 20 0a 2a the prefixes .*
1b5a6 2a 20 6f 66 20 74 68 65 20 6b 65 79 73 20 70 72 * of the keys pr
1b5a7 69 6f 72 20 74 6f 20 74 68 65 20 66 69 6e 61 6c ior to the final
1b5a8 20 72 6f 77 69 64 2c 20 6e 6f 74 20 74 68 65 20 rowid, not the
1b5a9 65 6e 74 69 72 65 20 6b 65 79 2e 0a 2a 2f 0a 53 entire key..*/.S
1b5aa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1b5ab 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 t sqlite3VdbeIdx
1b5ac 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 KeyCompare(. Vd
1b5ad 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 beCursor *pC,
1b5ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1b5af 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 cursor to compa
1b5b0 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 re against */.
1b5b1 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1b5b2 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 pUnpacked, /* U
1b5b3 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 npacked version
1b5b4 6f 66 20 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72 of key to compar
1b5b5 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 e against */. i
1b5b6 6e 74 20 2a 72 65 73 20 20 20 20 20 20 20 20 20 nt *res
1b5b7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
1b5b8 69 74 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 ite the comparis
1b5b9 6f 6e 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a on result here *
1b5ba 2f 0a 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c /.){. i64 nCell
1b5bb 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 Key = 0;. int r
1b5bc 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 c;. BtCursor *p
1b5bd 43 75 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f Cur = pC->pCurso
1b5be 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 61 r;. Mem m;.. a
1b5bf 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1b5c0 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 reeCursorIsValid
1b5c1 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
1b5c2 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
1b5c3 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c Size(pCur, &nCel
1b5c4 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 lKey);. assert(
1b5c5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1b5c6 3b 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 ; /* pCur is
1b5c7 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 always valid so
1b5c8 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 KeySize cannot f
1b5c9 61 69 6c 20 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c ail */. /* nCel
1b5ca 6c 4b 65 79 20 77 69 6c 6c 20 61 6c 77 61 79 73 lKey will always
1b5cb 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e be between 0 an
1b5cc 64 20 30 78 66 66 66 66 66 66 66 66 20 62 65 63 d 0xffffffff bec
1b5cd 61 75 73 65 20 6f 66 20 74 68 65 20 73 61 79 0a ause of the say.
1b5ce 20 20 2a 2a 20 74 68 61 74 20 62 74 72 65 65 50 ** that btreeP
1b5cf 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 61 6e arseCellPtr() an
1b5d0 64 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 d sqlite3GetVari
1b5d1 6e 74 33 32 28 29 20 61 72 65 20 69 6d 70 6c 65 nt32() are imple
1b5d2 6d 65 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 mented */. if(
1b5d3 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e nCellKey<=0 || n
1b5d4 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 CellKey>0x7fffff
1b5d5 66 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d ff ){. *res =
1b5d6 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 0;. return S
1b5d7 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 QLITE_CORRUPT;.
1b5d8 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 }. memset(&m,
1b5d9 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 0, sizeof(m));.
1b5da 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1b5db 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 eMemFromBtree(pC
1b5dc 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 ->pCursor, 0, (i
1b5dd 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 nt)nCellKey, 1,
1b5de 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b &m);. if( rc ){
1b5df 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
1b5e0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 }. assert( pU
1b5e1 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 npacked->flags &
1b5e2 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 UNPACKED_IGNORE
1b5e3 5f 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 _ROWID );. *res
1b5e4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1b5e5 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c cordCompare(m.n,
1b5e6 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 m.z, pUnpacked)
1b5e7 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1b5e8 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 emRelease(&m);.
1b5e9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1b5ea 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 K;.}../*.** This
1b5eb 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 routine sets th
1b5ec 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 e value to be re
1b5ed 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 turned by subseq
1b5ee 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a uent calls to.**
1b5ef 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 sqlite3_changes
1b5f0 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 () on the databa
1b5f1 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 se handle 'db'.
1b5f2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b5f3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1b5f4 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 dbeSetChanges(sq
1b5f5 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e lite3 *db, int n
1b5f6 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 Change){. asser
1b5f7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b5f8 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 _held(db->mutex)
1b5f9 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 );. db->nChang
1b5fa 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 e = nChange;. d
1b5fb 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 b->nTotalChange
1b5fc 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f += nChange;.}../
1b5fd 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 *.** Set a flag
1b5fe 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 in the vdbe to u
1b5ff 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 pdate the change
1b600 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 counter when it
1b601 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a is finalised.**
1b602 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 or reset..*/.SQ
1b603 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1b604 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 d sqlite3VdbeCou
1b605 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a ntChanges(Vdbe *
1b606 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 v){. v->changeC
1b607 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a ntOn = 1;.}../*.
1b608 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 ** Mark every pr
1b609 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1b60a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1b60b 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a database conn
1b60c 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 ection.** as exp
1b60d 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 ired..**.** An e
1b60e 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 xpired statement
1b60f 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f means that reco
1b610 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 mpilation of the
1b611 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a statement is.**
1b612 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 recommend. Sta
1b613 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 tements expire w
1b614 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 hen things happe
1b615 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 n that make thei
1b616 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 r.** programs ob
1b617 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e solete. Removin
1b618 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 g user-defined f
1b619 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c unctions or coll
1b61a 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 ating.** sequenc
1b61b 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 es, or changing
1b61c 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e an authorization
1b61d 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 function are th
1b61e 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 e types of.** th
1b61f 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 ings that make p
1b620 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1b621 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a ts obsolete..*/.
1b622 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1b623 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 oid sqlite3Expir
1b624 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 ePreparedStateme
1b625 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 nts(sqlite3 *db)
1b626 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 {. Vdbe *p;. f
1b627 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 or(p = db->pVdbe
1b628 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 ; p; p=p->pNext)
1b629 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 {. p->expired
1b62a 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a = 1;. }.}../*.
1b62b 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 ** Return the da
1b62c 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 tabase associate
1b62d 64 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e d with the Vdbe.
1b62e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b62f 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 TE sqlite3 *sqli
1b630 74 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a te3VdbeDb(Vdbe *
1b631 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e v){. return v->
1b632 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 db;.}../*.** Ret
1b633 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
1b634 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 an sqlite3_valu
1b635 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 e structure cont
1b636 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 aining the value
1b637 20 62 6f 75 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 bound.** parame
1b638 74 65 72 20 69 56 61 72 20 6f 66 20 56 4d 20 76 ter iVar of VM v
1b639 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 . Except, if the
1b63a 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 51 4c value is an SQL
1b63b 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 0a 2a NULL, return .*
1b63c 2a 20 30 20 69 6e 73 74 65 61 64 2e 20 55 6e 6c * 0 instead. Unl
1b63d 65 73 73 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 ess it is NULL,
1b63e 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 61 apply affinity a
1b63f 66 66 20 28 6f 6e 65 20 6f 66 20 74 68 65 20 53 ff (one of the S
1b640 51 4c 49 54 45 5f 41 46 46 5f 2a 0a 2a 2a 20 63 QLITE_AFF_*.** c
1b641 6f 6e 73 74 61 6e 74 73 29 20 74 6f 20 74 68 65 onstants) to the
1b642 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 72 65 value before re
1b643 74 75 72 6e 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a turning it..**.*
1b644 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 * The returned v
1b645 61 6c 75 65 20 6d 75 73 74 20 62 65 20 66 72 65 alue must be fre
1b646 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ed by the caller
1b647 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 61 using sqlite3Va
1b648 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2f 0a 53 51 lueFree()..*/.SQ
1b649 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
1b64a 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 ite3_value *sqli
1b64b 74 65 33 56 64 62 65 47 65 74 56 61 6c 75 65 28 te3VdbeGetValue(
1b64c 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 56 61 Vdbe *v, int iVa
1b64d 72 2c 20 75 38 20 61 66 66 29 7b 0a 20 20 61 73 r, u8 aff){. as
1b64e 73 65 72 74 28 20 69 56 61 72 3e 30 20 29 3b 0a sert( iVar>0 );.
1b64f 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 4d if( v ){. M
1b650 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 76 2d 3e 61 em *pMem = &v->a
1b651 56 61 72 5b 69 56 61 72 2d 31 5d 3b 0a 20 20 20 Var[iVar-1];.
1b652 20 69 66 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 if( 0==(pMem->f
1b653 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1b654 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1b655 33 5f 76 61 6c 75 65 20 2a 70 52 65 74 20 3d 20 3_value *pRet =
1b656 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 sqlite3ValueNew(
1b657 76 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 69 66 v->db);. if
1b658 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 ( pRet ){.
1b659 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b65a 43 6f 70 79 28 28 4d 65 6d 20 2a 29 70 52 65 74 Copy((Mem *)pRet
1b65b 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 , pMem);.
1b65c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 sqlite3ValueApp
1b65d 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 74 2c lyAffinity(pRet,
1b65e 20 61 66 66 2c 20 53 51 4c 49 54 45 5f 55 54 46 aff, SQLITE_UTF
1b65f 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 8);. sqli
1b660 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 te3VdbeMemStoreT
1b661 79 70 65 28 28 4d 65 6d 20 2a 29 70 52 65 74 29 ype((Mem *)pRet)
1b662 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1b663 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 20 20 20 return pRet;.
1b664 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1b665 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 0;.}../*.** Conf
1b666 69 67 75 72 65 20 53 51 4c 20 76 61 72 69 61 62 igure SQL variab
1b667 6c 65 20 69 56 61 72 20 73 6f 20 74 68 61 74 20 le iVar so that
1b668 62 69 6e 64 69 6e 67 20 61 20 6e 65 77 20 76 61 binding a new va
1b669 6c 75 65 20 74 6f 20 69 74 20 73 69 67 6e 61 6c lue to it signal
1b66a 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f s.** to sqlite3_
1b66b 72 65 6f 70 74 69 6d 69 7a 65 28 29 20 74 68 61 reoptimize() tha
1b66c 74 20 72 65 2d 70 72 65 70 61 72 69 6e 67 20 74 t re-preparing t
1b66d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 he statement may
1b66e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 20 result.** in a
1b66f 62 65 74 74 65 72 20 71 75 65 72 79 20 70 6c 61 better query pla
1b670 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1b671 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1b672 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28 3VdbeSetVarmask(
1b673 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 56 61 Vdbe *v, int iVa
1b674 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 56 r){. assert( iV
1b675 61 72 3e 30 20 29 3b 0a 20 20 69 66 28 20 69 56 ar>0 );. if( iV
1b676 61 72 3e 33 32 20 29 7b 0a 20 20 20 20 76 2d 3e ar>32 ){. v->
1b677 65 78 70 6d 61 73 6b 20 3d 20 30 78 66 66 66 66 expmask = 0xffff
1b678 66 66 66 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ffff;. }else{.
1b679 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 7c 3d v->expmask |=
1b67a 20 28 28 75 33 32 29 31 20 3c 3c 20 28 69 56 61 ((u32)1 << (iVa
1b67b 72 2d 31 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a r-1));. }.}../*
1b67c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1b67d 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20 2a d of vdbeaux.c *
1b67e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b67f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1b681 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1b682 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70 69 gin file vdbeapi
1b683 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1b684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1b686 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a .** 2004 May 26.
1b687 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
1b688 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1b689 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1b68a 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1b68b 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1b68c 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1b68d 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1b68e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
1b68f 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
1b690 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
1b691 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
1b692 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
1b693 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
1b694 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
1b695 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1b696 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
1b697 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
1b698 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1b699 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b69a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b69b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b69c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1b69d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
1b69e 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 ontains code use
1b69f 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 to implement AP
1b6a0 49 73 20 74 68 61 74 20 61 72 65 20 70 61 72 74 Is that are part
1b6a1 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e of the.** VDBE.
1b6a2 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c .*/..#ifndef SQL
1b6a3 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 ITE_OMIT_DEPRECA
1b6a4 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e TED./*.** Return
1b6a5 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 TRUE (non-zero)
1b6a6 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e of the statemen
1b6a7 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e t supplied as an
1b6a8 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 73 0a argument needs.
1b6a9 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 ** to be recompi
1b6aa 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d 65 6e led. A statemen
1b6ab 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 t needs to be re
1b6ac 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 76 65 compiled wheneve
1b6ad 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 74 69 r the.** executi
1b6ae 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 on environment c
1b6af 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 79 20 hanges in a way
1b6b0 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 65 72 that would alter
1b6b1 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 the program.**
1b6b2 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 72 65 that sqlite3_pre
1b6b3 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 65 73 pare() generates
1b6b4 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
1b6b5 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 73 if new functions
1b6b6 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 or.** collating
1b6b7 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 72 sequences are r
1b6b8 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 66 20 egistered or if
1b6b9 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 66 75 an authorizer fu
1b6ba 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 nction is.** add
1b6bb 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e 0a 2a ed or changed..*
1b6bc 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1b6bd 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 sqlite3_expired
1b6be 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1b6bf 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1b6c0 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1b6c1 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c . return p==0 |
1b6c2 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a 7d 0a | p->expired;.}.
1b6c3 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
1b6c4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
1b6c5 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 76 ine destroys a v
1b6c6 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 irtual machine t
1b6c7 68 61 74 20 69 73 20 63 72 65 61 74 65 64 20 62 hat is created b
1b6c8 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 y.** the sqlite3
1b6c9 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 74 69 _compile() routi
1b6ca 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20 ne. The integer
1b6cb 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e 20 53 returned is an S
1b6cc 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73 QLITE_.** succes
1b6cd 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 20 74 s/failure code t
1b6ce 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 hat describes th
1b6cf 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 65 63 e result of exec
1b6d0 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 75 61 uting the virtua
1b6d1 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a l.** machine..**
1b6d2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1b6d3 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 sets the error
1b6d4 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20 code and string
1b6d5 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 returned by.** s
1b6d6 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
1b6d7 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 , sqlite3_errmsg
1b6d8 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 () and sqlite3_e
1b6d9 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 rrmsg16()..*/.SQ
1b6da 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1b6db 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 ite3_finalize(sq
1b6dc 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1b6dd 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
1b6de 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a if( pStmt==0 ){.
1b6df 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1b6e0 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
1b6e1 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 Vdbe *v = (Vdbe
1b6e2 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c *)pStmt;. sql
1b6e3 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 ite3 *db = v->db
1b6e4 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ;.#if SQLITE_THR
1b6e5 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 EADSAFE. sqli
1b6e6 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
1b6e7 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b = v->db->mutex;
1b6e8 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 .#endif. sqli
1b6e9 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1b6ea 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d mutex);. rc =
1b6eb 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 sqlite3VdbeFina
1b6ec 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 63 20 lize(v);. rc
1b6ed 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 = sqlite3ApiExit
1b6ee 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 (db, rc);. sq
1b6ef 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1b6f0 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 e(mutex);. }.
1b6f1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1b6f2 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20 74 68 .** Terminate th
1b6f3 65 20 63 75 72 72 65 6e 74 20 65 78 65 63 75 74 e current execut
1b6f4 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 ion of an SQL st
1b6f5 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 73 65 atement and rese
1b6f6 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 t it.** back to
1b6f7 69 74 73 20 73 74 61 72 74 69 6e 67 20 73 74 61 its starting sta
1b6f8 74 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 te so that it ca
1b6f9 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41 20 73 n be reused. A s
1b6fa 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 6f 6d uccess code from
1b6fb 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20 65 78 .** the prior ex
1b6fc 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74 75 72 ecution is retur
1b6fd 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
1b6fe 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 routine sets the
1b6ff 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 error code and
1b700 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 string returned
1b701 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 by.** sqlite3_er
1b702 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33 rcode(), sqlite3
1b703 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 _errmsg() and sq
1b704 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
1b705 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b706 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 73 65 int sqlite3_rese
1b707 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1b708 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 pStmt){. int rc
1b709 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 ;. if( pStmt==0
1b70a 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1b70b 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b ITE_OK;. }else{
1b70c 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 . Vdbe *v = (
1b70d 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 Vdbe*)pStmt;.
1b70e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1b70f 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 nter(v->db->mute
1b710 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c x);. rc = sql
1b711 69 74 65 33 56 64 62 65 52 65 73 65 74 28 76 29 ite3VdbeReset(v)
1b712 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1b713 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 2d 31 eMakeReady(v, -1
1b714 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 , 0, 0, 0, 0, 0)
1b715 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72 ;. assert( (r
1b716 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d c & (v->db->errM
1b717 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 20 20 ask))==rc );.
1b718 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1b719 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 29 3b Exit(v->db, rc);
1b71a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1b71b 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e ex_leave(v->db->
1b71c 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
1b71d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1b71e 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70 61 * Set all the pa
1b71f 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65 20 rameters in the
1b720 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 compiled SQL sta
1b721 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a tement to NULL..
1b722 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1b723 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f t sqlite3_clear_
1b724 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 bindings(sqlite3
1b725 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
1b726 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 int i;. int rc
1b727 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1b728 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a Vdbe *p = (Vdbe*
1b729 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 )pStmt;.#if SQLI
1b72a 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 TE_THREADSAFE.
1b72b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
1b72c 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29 70 utex = ((Vdbe*)p
1b72d 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 Stmt)->db->mutex
1b72e 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 ;.#endif. sqlit
1b72f 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
1b730 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 utex);. for(i=0
1b731 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b ; i<p->nVar; i++
1b732 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1b733 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 70 2d beMemRelease(&p-
1b734 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 70 >aVar[i]);. p
1b735 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 73 20 ->aVar[i].flags
1b736 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a = MEM_Null;. }.
1b737 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61 if( p->isPrepa
1b738 72 65 56 32 20 26 26 20 70 2d 3e 65 78 70 6d 61 reV2 && p->expma
1b739 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 sk ){. p->exp
1b73a 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 ired = 1;. }.
1b73b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1b73c 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 ave(mutex);. re
1b73d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a turn rc;.}.../**
1b73e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b73f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 ********** sqlit
1b740 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a e3_value_ *****
1b741 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b742 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1b743 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
1b744 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e 66 ines extract inf
1b745 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 ormation from a
1b746 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 Mem or sqlite3_v
1b747 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 alue.** structur
1b748 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
1b749 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1b74a 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 ite3_value_blob(
1b74b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b74c 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d Val){. Mem *p =
1b74d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 (Mem*)pVal;. i
1b74e 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d f( p->flags & (M
1b74f 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 EM_Blob|MEM_Str)
1b750 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1b751 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 dbeMemExpandBlob
1b752 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 (p);. p->flag
1b753 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 s &= ~MEM_Str;.
1b754 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d p->flags |= M
1b755 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 EM_Blob;. ret
1b756 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 urn p->z;. }els
1b757 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 e{. return sq
1b758 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1b759 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 (pVal);. }.}.SQ
1b75a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1b75b 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
1b75c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1b75d 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1b75e 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 sqlite3ValueByte
1b75f 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 s(pVal, SQLITE_U
1b760 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 TF8);.}.SQLITE_A
1b761 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 PI int sqlite3_v
1b762 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c alue_bytes16(sql
1b763 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1b764 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1b765 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 te3ValueBytes(pV
1b766 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 al, SQLITE_UTF16
1b767 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 NATIVE);.}.SQLIT
1b768 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c E_API double sql
1b769 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c ite3_value_doubl
1b76a 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 e(sqlite3_value
1b76b 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
1b76c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c sqlite3VdbeReal
1b76d 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c Value((Mem*)pVal
1b76e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b76f 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 int sqlite3_valu
1b770 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 e_int(sqlite3_va
1b771 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 lue *pVal){. re
1b772 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69 74 65 turn (int)sqlite
1b773 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 3VdbeIntValue((M
1b774 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c em*)pVal);.}.SQL
1b775 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 ITE_API sqlite_i
1b776 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c nt64 sqlite3_val
1b777 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 ue_int64(sqlite3
1b778 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 _value *pVal){.
1b779 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
1b77a 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d dbeIntValue((Mem
1b77b 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 *)pVal);.}.SQLIT
1b77c 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 E_API const unsi
1b77d 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 gned char *sqlit
1b77e 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71 e3_value_text(sq
1b77f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b780 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 6f l){. return (co
1b781 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1b782 72 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65 r *)sqlite3Value
1b783 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 Text(pVal, SQLIT
1b784 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 E_UTF8);.}.#ifnd
1b785 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1b786 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
1b787 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
1b788 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1b789 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
1b78a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1b78b 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
1b78c 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 (pVal, SQLITE_UT
1b78d 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 F16NATIVE);.}.SQ
1b78e 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1b78f 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c oid *sqlite3_val
1b790 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 ue_text16be(sqli
1b791 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
1b792 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1b793 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c e3ValueText(pVal
1b794 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 , SQLITE_UTF16BE
1b795 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b796 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
1b797 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1b798 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 le(sqlite3_value
1b799 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
1b79a 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 n sqlite3ValueTe
1b79b 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f xt(pVal, SQLITE_
1b79c 55 54 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 UTF16LE);.}.#end
1b79d 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1b79e 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 T_UTF16 */.SQLIT
1b79f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b7a0 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 3_value_type(sql
1b7a1 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c ite3_value* pVal
1b7a2 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 61 6c ){. return pVal
1b7a3 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a ->type;.}../****
1b7a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b7a5 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
1b7a6 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a _result_ ******
1b7a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b7a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 *********.** The
1b7a9 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
1b7aa 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20 nes are used by
1b7ab 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e user-defined fun
1b7ac 63 74 69 6f 6e 73 20 74 6f 20 73 70 65 63 69 66 ctions to specif
1b7ad 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f y.** the functio
1b7ae 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 n result..**.**
1b7af 54 68 65 20 73 65 74 53 74 72 4f 72 45 72 72 6f The setStrOrErro
1b7b0 72 28 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c r() funtion call
1b7b1 73 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d s sqlite3VdbeMem
1b7b2 53 65 74 53 74 72 28 29 20 74 6f 20 73 74 6f 72 SetStr() to stor
1b7b3 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 e the.** result
1b7b4 61 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 as a string or b
1b7b5 6c 6f 62 20 62 75 74 20 69 66 20 74 68 65 20 73 lob but if the s
1b7b6 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 tring or blob is
1b7b7 20 74 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a too large, it.*
1b7b8 2a 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 * then sets the
1b7b9 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 error code to SQ
1b7ba 4c 49 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 LITE_TOOBIG.*/.s
1b7bb 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 52 65 tatic void setRe
1b7bc 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 0a sultStrOrError(.
1b7bd 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1b7be 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e t *pCtx, /* Fun
1b7bf 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f ction context */
1b7c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1b7c1 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 , /* St
1b7c2 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a ring pointer */.
1b7c3 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 int n,
1b7c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
1b7c5 65 73 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 es in string, or
1b7c6 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 negative */. u
1b7c7 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 8 enc,
1b7c8 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 /* Encodi
1b7c9 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 ng of z. 0 for
1b7ca 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 BLOBs */. void
1b7cb 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 (*xDel)(void*)
1b7cc 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 /* Destructor
1b7cd 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a function */.){.
1b7ce 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1b7cf 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 eMemSetStr(&pCtx
1b7d0 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 ->s, z, n, enc,
1b7d1 78 44 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f xDel)==SQLITE_TO
1b7d2 4f 42 49 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 OBIG ){. sqli
1b7d3 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
1b7d4 5f 74 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a 20 _toobig(pCtx);.
1b7d5 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 }.}.SQLITE_API
1b7d6 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b7d7 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 ult_blob(. sqli
1b7d8 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b7d9 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 x, . const void
1b7da 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a *z, . int n, .
1b7db 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
1b7dc 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 oid *).){. asse
1b7dd 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 rt( n>=0 );. as
1b7de 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b7df 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b7e0 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b7e1 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 setResultStrOrE
1b7e2 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c rror(pCtx, z, n,
1b7e3 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 0, xDel);.}.SQL
1b7e4 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b7e5 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
1b7e6 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 le(sqlite3_conte
1b7e7 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65 xt *pCtx, double
1b7e8 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 rVal){. assert
1b7e9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b7ea 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b7eb 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
1b7ec 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f ite3VdbeMemSetDo
1b7ed 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 72 uble(&pCtx->s, r
1b7ee 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 Val);.}.SQLITE_A
1b7ef 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b7f0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c result_error(sql
1b7f1 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b7f2 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tx, const char *
1b7f3 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 z, int n){. ass
1b7f4 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b7f5 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b7f6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b7f7 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 pCtx->isError =
1b7f8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1b7f9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b7fa 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a tStr(&pCtx->s, z
1b7fb 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , n, SQLITE_UTF8
1b7fc 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
1b7fd 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 NT);.}.#ifndef S
1b7fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1b7ff 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b800 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b801 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f error16(sqlite3_
1b802 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 context *pCtx, c
1b803 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e onst void *z, in
1b804 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 t n){. assert(
1b805 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b806 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b807 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d utex) );. pCtx-
1b808 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 >isError = SQLIT
1b809 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 E_ERROR;. sqlit
1b80a 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1b80b 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 &pCtx->s, z, n,
1b80c 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 SQLITE_UTF16NATI
1b80d 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 VE, SQLITE_TRANS
1b80e 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a IENT);.}.#endif.
1b80f 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b810 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
1b811 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
1b812 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 56 xt *pCtx, int iV
1b813 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 al){. assert( s
1b814 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b815 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1b816 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
1b817 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 3VdbeMemSetInt64
1b818 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 29 (&pCtx->s, (i64)
1b819 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f iVal);.}.SQLITE_
1b81a 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b81b 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 _result_int64(sq
1b81c 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b81d 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a Ctx, i64 iVal){.
1b81e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b81f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b820 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b821 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1b822 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74 MemSetInt64(&pCt
1b823 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 x->s, iVal);.}.S
1b824 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b825 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 qlite3_result_nu
1b826 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ll(sqlite3_conte
1b827 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 xt *pCtx){. ass
1b828 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b829 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b82a 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b82b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b82c 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b tNull(&pCtx->s);
1b82d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1b82e 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b82f 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 74 65 t_text(. sqlite
1b830 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1b831 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a . const char *
1b832 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 z, . int n,. v
1b833 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1b834 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 *).){. assert(
1b835 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b836 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b837 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 mutex) );. setR
1b838 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 esultStrOrError(
1b839 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 pCtx, z, n, SQLI
1b83a 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a TE_UTF8, xDel);.
1b83b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1b83c 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1b83d 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1b83e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 te3_result_text1
1b83f 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 6(. sqlite3_con
1b840 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 text *pCtx, . c
1b841 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 onst void *z, .
1b842 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 int n, . void
1b843 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a (*xDel)(void *).
1b844 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b845 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b846 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b847 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c x) );. setResul
1b848 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 tStrOrError(pCtx
1b849 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
1b84a 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c TF16NATIVE, xDel
1b84b 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b84c 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b84d 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 ult_text16be(.
1b84e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b84f 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
1b850 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 void *z, . int
1b851 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 n, . void (*xDe
1b852 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 l)(void *).){.
1b853 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b854 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b855 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b856 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f . setResultStrO
1b857 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 rError(pCtx, z,
1b858 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 n, SQLITE_UTF16B
1b859 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 E, xDel);.}.SQLI
1b85a 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1b85b 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 te3_result_text1
1b85c 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6le(. sqlite3_c
1b85d 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 ontext *pCtx, .
1b85e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 const void *z,
1b85f 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 . int n, . voi
1b860 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a d (*xDel)(void *
1b861 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ).){. assert( s
1b862 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b863 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1b864 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 tex) );. setRes
1b865 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 ultStrOrError(pC
1b866 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 tx, z, n, SQLITE
1b867 5f 55 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b _UTF16LE, xDel);
1b868 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1b869 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a ITE_OMIT_UTF16 *
1b86a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
1b86b 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b86c 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 _value(sqlite3_c
1b86d 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 73 71 ontext *pCtx, sq
1b86e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b86f 6c 75 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 lue){. assert(
1b870 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b871 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b872 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1b873 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 e3VdbeMemCopy(&p
1b874 43 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b Ctx->s, pValue);
1b875 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1b876 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b877 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 t_zeroblob(sqlit
1b878 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b879 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 , int n){. asse
1b87a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b87b 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b87c 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
1b87d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b87e 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e ZeroBlob(&pCtx->
1b87f 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f s, n);.}.SQLITE_
1b880 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b881 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f _result_error_co
1b882 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 de(sqlite3_conte
1b883 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 xt *pCtx, int er
1b884 72 43 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e rCode){. pCtx->
1b885 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 isError = errCod
1b886 65 3b 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 73 e;. if( pCtx->s
1b887 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c .flags & MEM_Nul
1b888 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 l ){. sqlite3
1b889 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 VdbeMemSetStr(&p
1b88a 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45 Ctx->s, sqlite3E
1b88b 72 72 53 74 72 28 65 72 72 43 6f 64 65 29 2c 20 rrStr(errCode),
1b88c 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 -1, .
1b88d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 SQ
1b88e 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
1b88f 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d E_STATIC);. }.}
1b890 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 ../* Force an SQ
1b891 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f LITE_TOOBIG erro
1b892 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r. */.SQLITE_API
1b893 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b894 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 sult_error_toobi
1b895 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 g(sqlite3_contex
1b896 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 t *pCtx){. asse
1b897 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b898 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b899 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1b89a 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 Ctx->isError = S
1b89b 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 QLITE_TOOBIG;.
1b89c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b89d 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 tStr(&pCtx->s, "
1b89e 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 string or blob t
1b89f 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 oo big", -1, .
1b8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b8a1 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 SQLITE_UTF8
1b8a2 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
1b8a3 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 ;.}../* An SQLIT
1b8a4 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a E_NOMEM error. *
1b8a5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
1b8a6 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b8a7 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c _error_nomem(sql
1b8a8 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b8a9 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 tx){. assert( s
1b8aa 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b8ab 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1b8ac 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
1b8ad 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1b8ae 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 &pCtx->s);. pCt
1b8af 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c x->isError = SQL
1b8b0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 ITE_NOMEM;. pCt
1b8b1 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 x->s.db->mallocF
1b8b2 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a ailed = 1;.}../*
1b8b3 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 .** Execute the
1b8b4 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c statement pStmt,
1b8b5 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20 either until a
1b8b6 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 row of data is r
1b8b7 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 eady, the.** sta
1b8b8 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 tement is comple
1b8b9 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 tely executed or
1b8ba 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1b8bb 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1b8bc 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 tine implements
1b8bd 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 the bulk of the
1b8be 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68 65 logic behind the
1b8bf 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a sqlite_step().*
1b8c0 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 * API. The only
1b8c1 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 thing omitted i
1b8c2 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 s the automatic
1b8c3 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a recompile if a .
1b8c4 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 ** schema change
1b8c5 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 has occurred.
1b8c6 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20 68 That detail is h
1b8c7 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a andled by the.**
1b8c8 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73 outer sqlite3_s
1b8c9 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70 72 tep() wrapper pr
1b8ca 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 ocedure..*/.stat
1b8cb 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 ic int sqlite3St
1b8cc 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 ep(Vdbe *p){. s
1b8cd 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e qlite3 *db;. in
1b8ce 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
1b8cf 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 p);. if( p->mag
1b8d0 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic!=VDBE_MAGIC_R
1b8d1 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e UN ){. return
1b8d2 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1b8d3 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 }.. /* Assert
1b8d4 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 that malloc() h
1b8d5 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f as not failed */
1b8d6 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 . db = p->db;.
1b8d7 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
1b8d8 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 ailed ){. ret
1b8d9 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1b8da 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e ;. }.. if( p->
1b8db 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 pc<=0 && p->expi
1b8dc 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20 41 red ){. if( A
1b8dd 4c 57 41 59 53 28 70 2d 3e 72 63 3d 3d 53 51 4c LWAYS(p->rc==SQL
1b8de 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d ITE_OK || p->rc=
1b8df 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 29 20 =SQLITE_SCHEMA)
1b8e0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d ){. p->rc =
1b8e1 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a SQLITE_SCHEMA;.
1b8e2 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 }. rc = S
1b8e3 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1b8e4 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 goto end_of_ste
1b8e5 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c p;. }. if( sql
1b8e6 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1b8e7 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1b8e8 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1b8e9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1b8ea 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 _MISUSE;. }. i
1b8eb 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 f( p->pc<0 ){.
1b8ec 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
1b8ed 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 e no other state
1b8ee 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 ments currently
1b8ef 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 running, then.
1b8f0 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69 ** reset the i
1b8f1 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20 nterrupt flag.
1b8f2 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 This prevents a
1b8f3 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
1b8f4 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a interrupt. **
1b8f5 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 from interrupti
1b8f6 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 ng a statement t
1b8f7 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 hat has not yet
1b8f8 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a started.. */.
1b8f9 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 if( db->acti
1b8fa 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a veVdbeCnt==0 ){.
1b8fb 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 db->u1.isI
1b8fc 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a nterrupted = 0;.
1b8fd 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 }.. asser
1b8fe 74 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 t( db->writeVdbe
1b8ff 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74 Cnt>0 || db->aut
1b900 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 oCommit==0 || db
1b901 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3d ->nDeferredCons=
1b902 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 =0 );..#ifndef S
1b903 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1b904 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 . if( db->xPr
1b905 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e ofile && !db->in
1b906 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 it.busy ){.
1b907 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1b908 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 sqlite3OsCur
1b909 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 rentTime(db->pVf
1b90a 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 s, &rNow);.
1b90b 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 p->startTime =
1b90c 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 (u64)((rNow - (i
1b90d 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a nt)rNow)*3600.0*
1b90e 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 24.0*1000000000.
1b90f 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0);. }.#endif
1b910 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 .. db->active
1b911 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 VdbeCnt++;. i
1b912 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d f( p->readOnly==
1b913 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62 0 ) db->writeVdb
1b914 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 eCnt++;. p->p
1b915 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 c = 0;. }.#ifnd
1b916 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 ef SQLITE_OMIT_E
1b917 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e XPLAIN. if( p->
1b918 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 explain ){. r
1b919 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c c = sqlite3VdbeL
1b91a 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a ist(p);. }else.
1b91b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1b91c 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f _OMIT_EXPLAIN */
1b91d 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 . {. rc = sq
1b91e 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29 lite3VdbeExec(p)
1b91f 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c ;. }.. if( sql
1b920 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1b921 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 ) ){. rc = SQ
1b922 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
1b923 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1b924 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a _OMIT_TRACE. /*
1b925 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 Invoke the prof
1b926 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 ile callback if
1b927 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a there is one. *
1b928 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 /. if( rc!=SQLI
1b929 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 TE_ROW && db->xP
1b92a 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 rofile && !db->i
1b92b 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e 7a nit.busy && p->z
1b92c 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c Sql ){. doubl
1b92d 65 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 e rNow;. u64
1b92e 65 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 elapseTime;..
1b92f 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
1b930 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 tTime(db->pVfs,
1b931 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 &rNow);. elap
1b932 73 65 54 69 6d 65 20 3d 20 28 75 36 34 29 28 28 seTime = (u64)((
1b933 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 rNow - (int)rNow
1b934 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 )*3600.0*24.0*10
1b935 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 00000000.0);.
1b936 20 65 6c 61 70 73 65 54 69 6d 65 20 2d 3d 20 70 elapseTime -= p
1b937 2d 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20 20 20 ->startTime;.
1b938 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 db->xProfile(db
1b939 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 ->pProfileArg, p
1b93a 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 73 65 54 69 ->zSql, elapseTi
1b93b 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a me);. }.#endif.
1b93c 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d . db->errCode =
1b93d 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 rc;. if( SQLIT
1b93e 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 E_NOMEM==sqlite3
1b93f 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 ApiExit(p->db, p
1b940 2d 3e 72 63 29 20 29 7b 0a 20 20 20 20 70 2d 3e ->rc) ){. p->
1b941 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1b942 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f 66 5f 73 74 M;. }.end_of_st
1b943 65 70 3a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 ep:. /* At this
1b944 20 70 6f 69 6e 74 20 6c 6f 63 61 6c 20 76 61 72 point local var
1b945 69 61 62 6c 65 20 72 63 20 68 6f 6c 64 73 20 74 iable rc holds t
1b946 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 he value that sh
1b947 6f 75 6c 64 20 62 65 20 0a 20 20 2a 2a 20 72 65 ould be . ** re
1b948 74 75 72 6e 65 64 20 69 66 20 74 68 69 73 20 73 turned if this s
1b949 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 6f 6d tatement was com
1b94a 70 69 6c 65 64 20 75 73 69 6e 67 20 74 68 65 20 piled using the
1b94b 6c 65 67 61 63 79 20 0a 20 20 2a 2a 20 73 71 6c legacy . ** sql
1b94c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 69 ite3_prepare() i
1b94d 6e 74 65 72 66 61 63 65 2e 20 41 63 63 6f 72 64 nterface. Accord
1b94e 69 6e 67 20 74 6f 20 74 68 65 20 64 6f 63 73 2c ing to the docs,
1b94f 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 0a 20 this can only.
1b950 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 ** be one of th
1b951 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 e values in the
1b952 66 69 72 73 74 20 61 73 73 65 72 74 28 29 20 62 first assert() b
1b953 65 6c 6f 77 2e 20 56 61 72 69 61 62 6c 65 20 70 elow. Variable p
1b954 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 6f 6e 74 61 ->rc . ** conta
1b955 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 74 68 ins the value th
1b956 61 74 20 77 6f 75 6c 64 20 62 65 20 72 65 74 75 at would be retu
1b957 72 6e 65 64 20 69 66 20 73 71 6c 69 74 65 33 5f rned if sqlite3_
1b958 66 69 6e 61 6c 69 7a 65 28 29 20 0a 20 20 2a 2a finalize() . **
1b959 20 77 65 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 were called on
1b95a 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 20 20 2a statement p.. *
1b95b 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d /. assert( rc==
1b95c 53 51 4c 49 54 45 5f 52 4f 57 20 20 7c 7c 20 72 SQLITE_ROW || r
1b95d 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 c==SQLITE_DONE
1b95e 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 || rc==SQLITE_E
1b95f 52 52 4f 52 20 0a 20 20 20 20 20 20 20 7c 7c 20 RROR . ||
1b960 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 rc==SQLITE_BUSY
1b961 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4d 49 || rc==SQLITE_MI
1b962 53 55 53 45 0a 20 20 29 3b 0a 20 20 61 73 73 65 SUSE. );. asse
1b963 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 rt( p->rc!=SQLIT
1b964 45 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63 21 3d E_ROW && p->rc!=
1b965 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 SQLITE_DONE );.
1b966 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 61 72 if( p->isPrepar
1b967 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c 49 54 eV2 && rc!=SQLIT
1b968 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 51 4c E_ROW && rc!=SQL
1b969 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 ITE_DONE ){.
1b96a 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 /* If this state
1b96b 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 65 ment was prepare
1b96c 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f d using sqlite3_
1b96d 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20 61 6e prepare_v2(), an
1b96e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 65 72 72 6f d an. ** erro
1b96f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c 20 74 r has occured, t
1b970 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 20 65 hen return the e
1b971 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70 2d 3e rror code in p->
1b972 72 63 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a rc to the. **
1b973 20 63 61 6c 6c 65 72 2e 20 53 65 74 20 74 68 65 caller. Set the
1b974 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 74 error code in t
1b975 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
1b976 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 76 le to the same v
1b977 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 alue.. */ .
1b978 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72 43 6f rc = db->errCo
1b979 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 7d 0a de = p->rc;. }.
1b97a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64 62 2d return (rc&db-
1b97b 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a >errMask);.}../*
1b97c 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
1b97d 74 6f 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d top-level implem
1b97e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 entation of sqli
1b97f 74 65 33 5f 73 74 65 70 28 29 2e 20 20 43 61 6c te3_step(). Cal
1b980 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 53 74 65 70 l.** sqlite3Step
1b981 28 29 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 () to do most of
1b982 20 74 68 65 20 77 6f 72 6b 2e 20 20 49 66 20 61 the work. If a
1b983 20 73 63 68 65 6d 61 20 65 72 72 6f 72 20 6f 63 schema error oc
1b984 63 75 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 curs,.** call sq
1b985 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29 lite3Reprepare()
1b986 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0a and try again..
1b987 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1b988 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 t sqlite3_step(s
1b989 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1b98a 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 mt){. int rc =
1b98b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1b98c 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 if( pStmt ){.
1b98d 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 int cnt = 0;.
1b98e 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 Vdbe *v = (Vd
1b98f 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 be*)pStmt;. s
1b990 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e qlite3 *db = v->
1b991 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f db;. sqlite3_
1b992 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e mutex_enter(db->
1b993 6d 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c mutex);. whil
1b994 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 e( (rc = sqlite3
1b995 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 Step(v))==SQLITE
1b996 5f 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 _SCHEMA.
1b997 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a && cnt++ < 5.
1b998 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 72 && (r
1b999 63 20 3d 20 73 71 6c 69 74 65 33 52 65 70 72 65 c = sqlite3Repre
1b99a 70 61 72 65 28 76 29 29 3d 3d 53 51 4c 49 54 45 pare(v))==SQLITE
1b99b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
1b99c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 ite3_reset(pStmt
1b99d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 70 69 );. v->expi
1b99e 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 red = 0;. }.
1b99f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1b9a0 45 5f 53 43 48 45 4d 41 20 26 26 20 41 4c 57 41 E_SCHEMA && ALWA
1b9a1 59 53 28 76 2d 3e 69 73 50 72 65 70 61 72 65 56 YS(v->isPrepareV
1b9a2 32 29 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d 2) && ALWAYS(db-
1b9a3 3e 70 45 72 72 29 20 29 7b 0a 20 20 20 20 20 20 >pErr) ){.
1b9a4 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f 63 63 /* This case occ
1b9a5 75 72 73 20 61 66 74 65 72 20 66 61 69 6c 69 6e urs after failin
1b9a6 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 20 61 g to recompile a
1b9a7 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e 74 2e n sql statement.
1b9a8 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 . ** The e
1b9a9 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f rror message fro
1b9aa 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 69 6c m the SQL compil
1b9ab 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 er has already b
1b9ac 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 20 20 een loaded .
1b9ad 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 61 ** into the da
1b9ae 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 tabase handle. T
1b9af 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 his block copies
1b9b0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
1b9b1 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f ge . ** fro
1b9b2 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 m the database h
1b9b3 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20 73 andle into the s
1b9b4 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65 74 tatement and set
1b9b5 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a s the statement.
1b9b6 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d ** program
1b9b7 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74 6f counter to 0 to
1b9b8 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 68 65 ensure that whe
1b9b9 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 n the statement
1b9ba 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e is . ** fin
1b9bb 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 74 20 alized or reset
1b9bc 74 68 65 20 70 61 72 73 65 72 20 65 72 72 6f 72 the parser error
1b9bd 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 message is avai
1b9be 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 20 20 lable via.
1b9bf 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 ** sqlite3_errms
1b9c0 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
1b9c1 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 20 20 errcode()..
1b9c2 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
1b9c3 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 63 6f char *zErr = (co
1b9c4 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 nst char *)sqlit
1b9c5 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 62 e3_value_text(db
1b9c6 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 20 20 ->pErr); .
1b9c7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1b9c8 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , v->zErrMsg);.
1b9c9 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 if( !db->ma
1b9ca 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1b9cb 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 v->zErrMsg
1b9cc 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
1b9cd 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 up(db, zErr);.
1b9ce 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
1b9cf 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 v->zErrMsg
1b9d0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 2d 3e = 0;. v->
1b9d1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1b9d2 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
1b9d3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1b9d4 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3ApiExit(db, rc)
1b9d5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1b9d6 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
1b9d7 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1b9d8 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1b9d9 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72 Extract the user
1b9da 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c data from a sql
1b9db 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 ite3_context str
1b9dc 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 ucture and retur
1b9dd 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 n a.** pointer t
1b9de 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f o it..*/.SQLITE_
1b9df 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
1b9e0 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 6c 69 3_user_data(sqli
1b9e1 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b te3_context *p){
1b9e2 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 . assert( p &&
1b9e3 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 p->pFunc );. re
1b9e4 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e 70 turn p->pFunc->p
1b9e5 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a UserData;.}../*.
1b9e6 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 ** Extract the u
1b9e7 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 ser data from a
1b9e8 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b9e9 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 structure and re
1b9ea 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 turn a.** pointe
1b9eb 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 r to it..*/.SQLI
1b9ec 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a TE_API sqlite3 *
1b9ed 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f sqlite3_context_
1b9ee 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 db_handle(sqlite
1b9ef 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 3_context *p){.
1b9f0 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d assert( p && p-
1b9f1 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 >pFunc );. retu
1b9f2 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a 2f rn p->s.db;.}../
1b9f3 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1b9f4 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d ng is the implem
1b9f5 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 entation of an S
1b9f6 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 QL function that
1b9f7 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 6c 73 always.** fails
1b9f8 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d with an error m
1b9f9 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 20 74 essage stating t
1b9fa 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e hat the function
1b9fb 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 65 0a is used in the.
1b9fc 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 78 74 ** wrong context
1b9fd 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f . The sqlite3_o
1b9fe 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e verload_function
1b9ff 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 6f 6e () API might con
1ba00 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 66 75 struct.** SQL fu
1ba01 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 20 nction that use
1ba02 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 6f 20 this routine so
1ba03 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f that the functio
1ba04 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a 2a 2a ns will exist.**
1ba05 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 for name resolu
1ba06 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 63 74 tion but are act
1ba07 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 65 64 ually overloaded
1ba08 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 75 6e by the xFindFun
1ba09 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20 ction.** method
1ba0a 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 of virtual table
1ba0b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
1ba0c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1ba0d 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 3InvalidFunction
1ba0e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
1ba0f 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 20 2f ext *context, /
1ba10 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 * The function c
1ba11 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a alling context *
1ba12 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c /. int NotUsed,
1ba13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ba14 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 * Number of argu
1ba15 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e ments to the fun
1ba16 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 ction */. sqlit
1ba17 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 e3_value **NotUs
1ba18 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f ed2 /* Value o
1ba19 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20 f each argument
1ba1a 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 */.){. const ch
1ba1b 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74 ar *zName = cont
1ba1c 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d ext->pFunc->zNam
1ba1d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b e;. char *zErr;
1ba1e 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1ba1f 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
1ba20 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 72 20 tUsed2);. zErr
1ba21 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 = sqlite3_mprint
1ba22 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62 6c 65 f(. "unable
1ba23 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e to use function
1ba24 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 75 65 %s in the reque
1ba25 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a sted context", z
1ba26 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Name);. sqlite3
1ba27 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f _result_error(co
1ba28 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 ntext, zErr, -1)
1ba29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ;. sqlite3_free
1ba2a 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a (zErr);.}../*.**
1ba2b 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 65 74 Allocate or ret
1ba2c 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 urn the aggregat
1ba2d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 61 20 e context for a
1ba2e 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 user function.
1ba2f 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 A new.** context
1ba30 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e is allocated on
1ba31 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 2e the first call.
1ba32 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c Subsequent cal
1ba33 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a ls return the.**
1ba34 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 68 same context th
1ba35 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 at was returned
1ba36 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a on prior calls..
1ba37 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
1ba38 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 id *sqlite3_aggr
1ba39 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 egate_context(sq
1ba3a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1ba3b 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 , int nByte){.
1ba3c 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 Mem *pMem;. ass
1ba3d 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 ert( p && p->pFu
1ba3e 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e nc && p->pFunc->
1ba3f 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 65 72 xStep );. asser
1ba40 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1ba41 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d _held(p->s.db->m
1ba42 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 6d 20 utex) );. pMem
1ba43 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 = p->pMem;. if(
1ba44 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 (pMem->flags &
1ba45 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20 MEM_Agg)==0 ){.
1ba46 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 if( nByte==0
1ba47 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1ba48 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 VdbeMemReleaseEx
1ba49 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 ternal(pMem);.
1ba4a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1ba4b 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
1ba4c 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 pMem->z = 0;.
1ba4d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1ba4e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1ba4f 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
1ba50 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 0);. pMem->
1ba51 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b flags = MEM_Agg;
1ba52 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 . pMem->u.p
1ba53 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a Def = p->pFunc;.
1ba54 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e if( pMem->
1ba55 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d z ){. mem
1ba56 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 set(pMem->z, 0,
1ba57 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a nByte);. }.
1ba58 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1ba59 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e rn (void*)pMem->
1ba5a 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 z;.}../*.** Retu
1ba5b 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 20 rn the auxilary
1ba5c 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 66 data pointer, if
1ba5d 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 41 any, for the iA
1ba5e 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 74 rg'th argument t
1ba5f 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 75 o.** the user-fu
1ba60 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 nction defined b
1ba61 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 y pCtx..*/.SQLIT
1ba62 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
1ba63 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
1ba64 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1ba65 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 29 *pCtx, int iArg)
1ba66 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 {. VdbeFunc *pV
1ba67 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65 dbeFunc;.. asse
1ba68 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1ba69 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1ba6a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1ba6b 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d VdbeFunc = pCtx-
1ba6c 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 >pVdbeFunc;. if
1ba6d 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 ( !pVdbeFunc ||
1ba6e 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d iArg>=pVdbeFunc-
1ba6f 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 >nAux || iArg<0
1ba70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1ba71 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 56 . }. return pV
1ba72 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 dbeFunc->apAux[i
1ba73 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a Arg].pAux;.}../*
1ba74 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 69 .** Set the auxi
1ba75 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 lary data pointe
1ba76 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 6e r and delete fun
1ba77 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69 ction, for the i
1ba78 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65 Arg'th.** argume
1ba79 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d 66 nt to the user-f
1ba7a 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 unction defined
1ba7b 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 65 by pCtx. Any pre
1ba7c 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a vious value is.*
1ba7d 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 6c * deleted by cal
1ba7e 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20 ling the delete
1ba7f 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 function specifi
1ba80 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 73 ed when it was s
1ba81 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 et..*/.SQLITE_AP
1ba82 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 I void sqlite3_s
1ba83 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71 et_auxdata(. sq
1ba84 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1ba85 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 67 Ctx, . int iArg
1ba86 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c , . void *pAux,
1ba87 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 . void (*xDele
1ba88 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 te)(void*).){.
1ba89 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a struct AuxData *
1ba8a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 65 pAuxData;. Vdbe
1ba8b 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b Func *pVdbeFunc;
1ba8c 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 20 . if( iArg<0 )
1ba8d 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20 goto failed;..
1ba8e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1ba8f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1ba90 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1ba91 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 . pVdbeFunc = p
1ba92 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a Ctx->pVdbeFunc;.
1ba93 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 if( !pVdbeFunc
1ba94 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e || pVdbeFunc->n
1ba95 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20 Aux<=iArg ){.
1ba96 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 64 int nAux = (pVd
1ba97 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 75 beFunc ? pVdbeFu
1ba98 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20 nc->nAux : 0);.
1ba99 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d int nMalloc =
1ba9a 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63 sizeof(VdbeFunc
1ba9b 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63 ) + sizeof(struc
1ba9c 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b t AuxData)*iArg;
1ba9d 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d . pVdbeFunc =
1ba9e 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
1ba9f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56 c(pCtx->s.db, pV
1baa0 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 dbeFunc, nMalloc
1baa1 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 62 );. if( !pVdb
1baa2 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 67 eFunc ){. g
1baa3 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
1baa4 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62 }. pCtx->pVdb
1baa5 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e eFunc = pVdbeFun
1baa6 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 c;. memset(&p
1baa7 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b VdbeFunc->apAux[
1baa8 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 nAux], 0, sizeof
1baa9 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29 (struct AuxData)
1baaa 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b *(iArg+1-nAux));
1baab 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e . pVdbeFunc->
1baac 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20 nAux = iArg+1;.
1baad 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 pVdbeFunc->pF
1baae 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e unc = pCtx->pFun
1baaf 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 61 c;. }.. pAuxDa
1bab0 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d ta = &pVdbeFunc-
1bab1 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20 >apAux[iArg];.
1bab2 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 41 if( pAuxData->pA
1bab3 75 78 20 26 26 20 70 41 75 78 44 61 74 61 2d 3e ux && pAuxData->
1bab4 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 70 xDelete ){. p
1bab5 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 AuxData->xDelete
1bab6 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 29 (pAuxData->pAux)
1bab7 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 61 ;. }. pAuxData
1bab8 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 ->pAux = pAux;.
1bab9 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 pAuxData->xDele
1baba 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20 te = xDelete;.
1babb 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a return;..failed:
1babc 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 29 . if( xDelete )
1babd 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 41 {. xDelete(pA
1babe 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e ux);. }.}..#ifn
1babf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bac0 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a DEPRECATED./*.**
1bac1 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1bac2 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20 er of times the
1bac3 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66 Step function of
1bac4 20 61 20 61 67 67 72 65 67 61 74 65 20 68 61 73 a aggregate has
1bac5 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 been .** called
1bac6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
1bac7 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 ction is depreca
1bac8 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65 ted. Do not use
1bac9 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 65 it for new code
1baca 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 . It is.** prov
1bacb 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 ide only to avoi
1bacc 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 63 d breaking legac
1bacd 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 67 y code. New agg
1bace 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a regate function.
1bacf 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
1bad0 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 74 ns should keep t
1bad1 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20 heir own counts
1bad2 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 67 within their agg
1bad3 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 regate.** contex
1bad4 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
1bad5 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 int sqlite3_agg
1bad6 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c regate_count(sql
1bad7 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 ite3_context *p)
1bad8 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 {. assert( p &&
1bad9 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d 3e 70 p->pMem && p->p
1bada 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 Func && p->pFunc
1badb 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65 74 ->xStep );. ret
1badc 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a urn p->pMem->n;.
1badd 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1bade 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1badf 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1bae0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 the result set f
1bae1 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 or the statement
1bae2 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 pStmt..*/.SQLIT
1bae3 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bae4 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 3_column_count(s
1bae5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bae6 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d mt){. Vdbe *pVm
1bae7 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 = (Vdbe *)pStmt
1bae8 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20 3f ;. return pVm ?
1bae9 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e pVm->nResColumn
1baea 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 : 0;.}../*.** R
1baeb 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1baec 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69 6c of values avail
1baed 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 75 able from the cu
1baee 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 rrent row of the
1baef 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 78 .** currently ex
1baf0 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e ecuting statemen
1baf1 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 t pStmt..*/.SQLI
1baf2 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1baf3 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 e3_data_count(sq
1baf4 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1baf5 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 t){. Vdbe *pVm
1baf6 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
1baf7 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c . if( pVm==0 ||
1baf8 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 pVm->pResultSet
1baf9 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
1bafa 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 return pVm->nR
1bafb 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a esColumn;.}.../*
1bafc 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
1bafd 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 if column iCol
1bafe 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 61 of the given sta
1baff 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64 2e tement is valid.
1bb00 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72 If.** it is, r
1bb01 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1bb02 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20 74 to the Mem for t
1bb03 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74 he value of that
1bb04 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 column..** If i
1bb05 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69 64 Col is not valid
1bb06 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 , return a point
1bb07 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69 63 er to a Mem whic
1bb08 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a h has a value.**
1bb09 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 of NULL..*/.sta
1bb0a 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d tic Mem *columnM
1bb0b 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 em(sqlite3_stmt
1bb0c 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1bb0d 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20 69 Vdbe *pVm;. i
1bb0e 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a nt vals;. Mem *
1bb0f 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 pOut;.. pVm = (
1bb10 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1bb11 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d 2d 3e if( pVm && pVm->
1bb12 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 26 26 pResultSet!=0 &&
1bb13 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 i<pVm->nResColu
1bb14 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a 20 20 mn && i>=0 ){.
1bb15 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bb16 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d enter(pVm->db->m
1bb17 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c 73 20 utex);. vals
1bb18 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 = sqlite3_data_c
1bb19 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 ount(pStmt);.
1bb1a 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52 pOut = &pVm->pR
1bb1b 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d esultSet[i];. }
1bb1c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 else{. /* If
1bb1d 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 the value passed
1bb1e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 as the second a
1bb1f 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74 20 6f rgument is out o
1bb20 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e 0a f range, return.
1bb21 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 ** a pointer
1bb22 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
1bb23 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f 62 6a g static Mem obj
1bb24 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74 61 69 ect which contai
1bb25 6e 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 ns the. ** va
1bb26 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20 45 76 lue SQL NULL. Ev
1bb27 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 4d 65 en though the Me
1bb28 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 m structure cont
1bb29 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e 74 0a ains an element.
1bb2a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70 65 20 69 ** of type i
1bb2b 36 34 2c 20 6f 6e 20 63 65 72 74 61 69 6e 20 61 64, on certain a
1bb2c 72 63 68 69 74 65 63 74 75 72 65 20 28 78 38 36 rchitecture (x86
1bb2d 29 20 77 69 74 68 20 63 65 72 74 61 69 6e 20 63 ) with certain c
1bb2e 6f 6d 70 69 6c 65 72 0a 20 20 20 20 2a 2a 20 73 ompiler. ** s
1bb2f 77 69 74 63 68 65 73 20 28 2d 4f 73 29 2c 20 67 witches (-Os), g
1bb30 63 63 20 6d 61 79 20 61 6c 69 67 6e 20 74 68 69 cc may align thi
1bb31 73 20 4d 65 6d 20 6f 62 6a 65 63 74 20 6f 6e 20 s Mem object on
1bb32 61 20 34 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 a 4-byte boundar
1bb33 79 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 y. ** instead
1bb34 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 6f 6e of an 8-byte on
1bb35 65 2e 20 54 68 69 73 20 61 6c 6c 20 77 6f 72 6b e. This all work
1bb36 73 20 66 69 6e 65 2c 20 65 78 63 65 70 74 20 74 s fine, except t
1bb37 68 61 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 hat when. **
1bb38 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c running with SQL
1bb39 49 54 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65 ITE_DEBUG define
1bb3a 64 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 d the SQLite cod
1bb3b 65 20 73 6f 6d 65 74 69 6d 65 73 20 61 73 73 65 e sometimes asse
1bb3c 72 74 28 29 73 0a 20 20 20 20 2a 2a 20 74 68 61 rt()s. ** tha
1bb3d 74 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72 t a Mem structur
1bb3e 65 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 e is located on
1bb3f 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 an 8-byte bounda
1bb40 72 79 2e 20 54 6f 20 70 72 65 76 65 6e 74 0a 20 ry. To prevent.
1bb41 20 20 20 2a 2a 20 74 68 69 73 20 61 73 73 65 72 ** this asser
1bb42 74 28 29 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67 t() from failing
1bb43 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 , when building
1bb44 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 with SQLITE_DEBU
1bb45 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a G defined. **
1bb46 20 75 73 69 6e 67 20 67 63 63 2c 20 66 6f 72 63 using gcc, forc
1bb47 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62 65 20 e nullMem to be
1bb48 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 75 8-byte aligned u
1bb49 73 69 6e 67 20 74 68 65 20 6d 61 67 69 63 61 6c sing the magical
1bb4a 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72 69 62 . ** __attrib
1bb4b 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 ute__((aligned(8
1bb4c 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f 0a 20 ))) macro. */.
1bb4d 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
1bb4e 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a 23 69 66 Mem nullMem .#if
1bb4f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1bb50 44 45 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 DEBUG) && define
1bb51 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20 20 20 20 d(__GNUC__).
1bb52 20 20 5f 5f 61 74 74 72 69 62 75 74 65 5f 5f 28 __attribute__(
1bb53 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20 0a 23 (aligned(8))) .#
1bb54 65 6e 64 69 66 0a 20 20 20 20 20 20 3d 20 7b 7b endif. = {{
1bb55 30 7d 2c 20 28 64 6f 75 62 6c 65 29 30 2c 20 30 0}, (double)0, 0
1bb56 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c , "", 0, MEM_Nul
1bb57 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 l, SQLITE_NULL,
1bb58 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 20 20 20 20 0, 0, 0 };..
1bb59 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57 41 59 if( pVm && ALWAY
1bb5a 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a 20 20 S(pVm->db) ){.
1bb5b 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1bb5c 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d x_enter(pVm->db-
1bb5d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 73 >mutex);. s
1bb5e 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d 2d qlite3Error(pVm-
1bb5f 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 >db, SQLITE_RANG
1bb60 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 E, 0);. }.
1bb61 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e pOut = (Mem*)&n
1bb62 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 ullMem;. }. re
1bb63 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a turn pOut;.}../*
1bb64 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1bb65 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 n is called afte
1bb66 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 71 r invoking an sq
1bb67 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58 20 lite3_value_XXX
1bb68 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a function on a .*
1bb69 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 28 * column value (
1bb6a 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65 74 i.e. a value ret
1bb6b 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61 74 urned by evaluat
1bb6c 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65 ing an SQL expre
1bb6d 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 ssion in the.**
1bb6e 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20 61 select list of a
1bb6f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1bb70 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75 73 t) that may caus
1bb71 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 e a malloc() fai
1bb72 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c lure. If .** mal
1bb73 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 loc() has failed
1bb74 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d 61 , the threads ma
1bb75 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
1bb76 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20 74 is cleared and t
1bb77 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 he result.** cod
1bb78 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 70 e of statement p
1bb79 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c 49 Stmt set to SQLI
1bb7a 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 TE_NOMEM..**.**
1bb7b 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 Specifically, th
1bb7c 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f is is called fro
1bb7d 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 m within:.**.**
1bb7e 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
1bb7f 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20 20 mn_int().**
1bb80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
1bb81 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73 71 nt64().** sq
1bb82 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
1bb83 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 t().** sqlit
1bb84 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
1bb85 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
1bb86 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 3_column_real().
1bb87 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1bb88 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a olumn_bytes().**
1bb89 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
1bb8a 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a 2a umn_bytes16().**
1bb8b 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72 20 .** But not for
1bb8c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
1bb8d 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65 76 lob(), which nev
1bb8e 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 er calls malloc(
1bb8f 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
1bb90 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 d columnMallocFa
1bb91 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73 74 ilure(sqlite3_st
1bb92 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 2f mt *pStmt).{. /
1bb93 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 * If malloc() fa
1bb94 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20 65 iled during an e
1bb95 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 ncoding conversi
1bb96 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20 2a on within an. *
1bb97 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
1bb98 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20 73 _XXX API, then s
1bb99 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f et the return co
1bb9a 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d de of the statem
1bb9b 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 ent to. ** SQLI
1bb9c 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 TE_NOMEM. The ne
1bb9d 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70 xt call to _step
1bb9e 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c 6c () (if any) will
1bb9f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1bba0 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 RROR. ** and _f
1bba1 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20 72 inalize() will r
1bba2 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a eturn NOMEM.. *
1bba3 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 /. Vdbe *p = (V
1bba4 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 dbe *)pStmt;. i
1bba5 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 f( p ){. p->r
1bba6 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1bba7 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 it(p->db, p->rc)
1bba8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bba9 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1bbaa 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a >mutex);. }.}..
1bbab 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
1bbac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 ************* sq
1bbad 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a lite3_column_ *
1bbae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bbaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1bbb0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1bbb1 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
1bbb2 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65 6d d to access elem
1bbb3 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 ents of the curr
1bbb4 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 ent row.** in th
1bbb5 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f e result set..*/
1bbb6 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1bbb7 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
1bbb8 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 column_blob(sqli
1bbb9 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bbba 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 int i){. const
1bbbb 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61 void *val;. va
1bbbc 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1bbbd 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 e_blob( columnMe
1bbbe 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 m(pStmt,i) );.
1bbbf 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 /* Even though t
1bbc0 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64 here is no encod
1bbc1 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 ing conversion,
1bbc2 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67 value_blob() mig
1bbc3 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 ht. ** need to
1bbc4 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f call malloc() to
1bbc5 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73 75 expand the resu
1bbc6 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62 lt of a zeroblob
1bbc7 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 () . ** express
1bbc8 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c ion. . */. col
1bbc9 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1bbca 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1bbcb 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1bbcc 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bbcd 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c column_bytes(sql
1bbce 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bbcf 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 , int i){. int
1bbd0 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1bbd1 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75 6d lue_bytes( colum
1bbd2 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1bbd3 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1bbd4 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1bbd5 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1bbd6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bbd7 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
1bbd8 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d es16(sqlite3_stm
1bbd9 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1bbda 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 {. int val = sq
1bbdb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1bbdc 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 s16( columnMem(p
1bbdd 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1bbde 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1bbdf 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1bbe0 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1bbe1 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 API double sqlit
1bbe2 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 e3_column_double
1bbe3 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bbe4 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1bbe5 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c double val = sql
1bbe6 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c ite3_value_doubl
1bbe7 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 e( columnMem(pSt
1bbe8 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1bbe9 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1bbea 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1bbeb 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1bbec 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
1bbed 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 lumn_int(sqlite3
1bbee 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1bbef 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 t i){. int val
1bbf0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1bbf1 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 int( columnMem(p
1bbf2 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1bbf3 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1bbf4 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1bbf5 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1bbf6 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 API sqlite_int64
1bbf7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1bbf8 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 int64(sqlite3_st
1bbf9 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bbfa 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 ){. sqlite_int6
1bbfb 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 4 val = sqlite3_
1bbfc 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c value_int64( col
1bbfd 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1bbfe 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1bbff 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1bc00 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1bc01 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1bc02 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1bc03 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1bc04 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d text(sqlite3_stm
1bc05 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1bc06 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e {. const unsign
1bc07 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20 73 ed char *val = s
1bc08 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1bc09 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 t( columnMem(pSt
1bc0a 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1bc0b 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1bc0c 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1bc0d 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1bc0e 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 I sqlite3_value
1bc0f 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1bc10 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 value(sqlite3_st
1bc11 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bc12 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d ){. Mem *pOut =
1bc13 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1bc14 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75 74 , i);. if( pOut
1bc15 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 ->flags&MEM_Stat
1bc16 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e ic ){. pOut->
1bc17 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 flags &= ~MEM_St
1bc18 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e atic;. pOut->
1bc19 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 flags |= MEM_Eph
1bc1a 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e em;. }. column
1bc1b 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1bc1c 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 tmt);. return (
1bc1d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 sqlite3_value *)
1bc1e 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 pOut;.}.#ifndef
1bc1f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1bc20 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
1bc21 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1bc22 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73 _column_text16(s
1bc23 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bc24 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f mt, int i){. co
1bc25 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d 20 nst void *val =
1bc26 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bc27 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 xt16( columnMem(
1bc28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f pStmt,i) );. co
1bc29 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1bc2a 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 e(pStmt);. retu
1bc2b 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66 rn val;.}.#endif
1bc2c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1bc2d 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f UTF16 */.SQLITE_
1bc2e 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bc2f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69 column_type(sqli
1bc30 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bc31 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 69 int i){. int i
1bc32 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 Type = sqlite3_v
1bc33 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75 6d alue_type( colum
1bc34 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1bc35 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1bc36 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1bc37 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a 7d return iType;.}
1bc38 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ../* The followi
1bc39 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 ng function is e
1bc3a 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 xperimental and
1bc3b 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 subject to chang
1bc3c 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 e or.** removal
1bc3d 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33 */./*int sqlite3
1bc3e 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f _column_numeric_
1bc3f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d type(sqlite3_stm
1bc40 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1bc41 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71 6c {.** return sql
1bc42 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 ite3_value_numer
1bc43 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d ic_type( columnM
1bc44 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 2a em(pStmt,i) );.*
1bc45 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e *}.*/../*.** Con
1bc46 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65 6c vert the N-th el
1bc47 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d 3e ement of pStmt->
1bc48 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20 pColName[] into
1bc49 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a 2a a string using.*
1bc4a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20 72 * xFunc() then r
1bc4b 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69 6e eturn that strin
1bc4c 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74 20 g. If N is out
1bc4d 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 6e of range, return
1bc4e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 0..**.** There
1bc4f 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d 65 are up to 5 name
1bc50 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d s for each colum
1bc51 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74 65 n. useType dete
1bc52 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a 20 rmines which.**
1bc53 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65 64 name is returned
1bc54 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65 20 . Here are the
1bc55 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 names:.**.**
1bc56 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75 6d 0 The colum
1bc57 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68 6f n name as it sho
1bc58 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65 64 uld be displayed
1bc59 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 for output.**
1bc5a 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61 74 1 The dat
1bc5b 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20 74 atype name for t
1bc5c 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 he column.**
1bc5d 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 2 The name
1bc5e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1bc5f 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 that the column
1bc60 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 derives from.**
1bc61 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e 61 3 The na
1bc62 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1bc63 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 that the column
1bc64 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 derives from.**
1bc65 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e 61 4 The na
1bc66 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1bc67 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20 column that the
1bc68 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 result column de
1bc69 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a rives from.**.**
1bc6a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 If the result i
1bc6b 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 63 s not a simple c
1bc6c 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1bc6d 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78 70 (if it is an exp
1bc6e 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20 ression.** or a
1bc6f 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20 75 constant) then u
1bc70 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61 6e seTypes 2, 3, an
1bc71 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e d 4 return NULL.
1bc72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 .*/.static const
1bc73 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d void *columnNam
1bc74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d e(. sqlite3_stm
1bc75 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74 20 t *pStmt,. int
1bc76 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 N,. const void
1bc77 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29 2c *(*xFunc)(Mem*),
1bc78 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a 29 . int useType.)
1bc79 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a {. const void *
1bc7a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65 20 ret = 0;. Vdbe
1bc7b 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 *p = (Vdbe *)pSt
1bc7c 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 mt;. int n;. s
1bc7d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e qlite3 *db = p->
1bc7e 64 62 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 db;. . assert(
1bc7f 20 64 62 21 3d 30 20 29 3b 0a 20 20 6e 20 3d 20 db!=0 );. n =
1bc80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 sqlite3_column_c
1bc81 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 69 ount(pStmt);. i
1bc82 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 f( N<n && N>=0 )
1bc83 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65 54 79 {. N += useTy
1bc84 70 65 2a 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 pe*n;. sqlite
1bc85 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 3_mutex_enter(db
1bc86 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 61 73 ->mutex);. as
1bc87 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 sert( db->malloc
1bc88 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 20 Failed==0 );.
1bc89 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 2d ret = xFunc(&p-
1bc8a 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 20 >aColName[N]);.
1bc8b 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 /* A malloc
1bc8c 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64 20 may have failed
1bc8d 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 78 46 inside of the xF
1bc8e 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 unc() call. If t
1bc8f 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 his. ** is th
1bc90 65 20 63 61 73 65 2c 20 63 6c 65 61 72 20 74 68 e case, clear th
1bc91 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 e mallocFailed f
1bc92 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e lag and return N
1bc93 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ULL.. */.
1bc94 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1bc95 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 64 62 iled ){. db
1bc96 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1bc97 20 30 3b 0a 20 20 20 20 20 20 72 65 74 20 3d 20 0;. ret =
1bc98 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 0;. }. sql
1bc99 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1bc9a 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d (db->mutex);. }
1bc9b 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d . return ret;.}
1bc9c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1bc9d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e he name of the N
1bc9e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 th column of the
1bc9f 20 72 65 73 75 6c 74 20 73 65 74 20 72 65 74 75 result set retu
1bca0 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 rned by SQL.** s
1bca1 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a tatement pStmt..
1bca2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1bca3 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1bca4 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 3_column_name(sq
1bca5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bca6 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1bca7 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1bca8 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1bca9 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1bcaa 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1bcab 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d lue_text, COLNAM
1bcac 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 E_NAME);.}.#ifnd
1bcad 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1bcae 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
1bcaf 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
1bcb0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 te3_column_name1
1bcb1 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 6(sqlite3_stmt *
1bcb2 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1bcb3 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1bcb4 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1bcb5 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1bcb6 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1bcb7 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 3_value_text16,
1bcb8 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d COLNAME_NAME);.}
1bcb9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
1bcba 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 20 79 onstraint: If y
1bcbb 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 ou have ENABLE_C
1bcbc 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 OLUMN_METADATA t
1bcbd 68 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 hen you must.**
1bcbe 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f not define OMIT_
1bcbf 44 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 DECLTYPE..*/.#if
1bcc0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
1bcc1 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 20 26 OMIT_DECLTYPE) &
1bcc2 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
1bcc3 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
1bcc4 45 54 41 44 41 54 41 29 0a 23 20 65 72 72 6f 72 ETADATA).# error
1bcc5 20 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 69 6e "Must not defin
1bcc6 65 20 62 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d e both SQLITE_OM
1bcc7 49 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 IT_DECLTYPE \.
1bcc8 20 20 20 20 20 20 20 61 6e 64 20 53 51 4c 49 54 and SQLIT
1bcc9 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
1bcca 4d 45 54 41 44 41 54 41 22 0a 23 65 6e 64 69 66 METADATA".#endif
1bccb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1bccc 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f _OMIT_DECLTYPE./
1bccd 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1bcce 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 column declarati
1bccf 6f 6e 20 74 79 70 65 20 28 69 66 20 61 70 70 6c on type (if appl
1bcd0 69 63 61 62 6c 65 29 20 6f 66 20 74 68 65 20 27 icable) of the '
1bcd1 69 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f i'th column.** o
1bcd2 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
1bcd3 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e of SQL statemen
1bcd4 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 t pStmt..*/.SQLI
1bcd5 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
1bcd6 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d r *sqlite3_colum
1bcd7 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 n_decltype(sqlit
1bcd8 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bcd9 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1bcda 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1bcdb 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1bcdc 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1bcdd 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1bcde 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 _text, COLNAME_D
1bcdf 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e ECLTYPE);.}.#ifn
1bce0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bce1 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 UTF16.SQLITE_API
1bce2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
1bce3 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c ite3_column_decl
1bce4 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73 type16(sqlite3_s
1bce5 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bce6 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
1bce7 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
1bce8 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
1bce9 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
1bcea 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1bceb 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 t16, COLNAME_DEC
1bcec 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 LTYPE);.}.#endif
1bced 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1bcee 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 UTF16 */.#endif
1bcef 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 /* SQLITE_OMIT_D
1bcf0 45 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 ECLTYPE */..#ifd
1bcf1 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
1bcf2 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
1bcf3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bcf4 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 e name of the da
1bcf5 74 61 62 61 73 65 20 66 72 6f 6d 20 77 68 69 63 tabase from whic
1bcf6 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d h a result colum
1bcf7 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 n derives..** NU
1bcf8 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 LL is returned i
1bcf9 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c f the result col
1bcfa 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 umn is an expres
1bcfb 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 sion or constant
1bcfc 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 or.** anything
1bcfd 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f else which is no
1bcfe 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 t an unabiguous
1bcff 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 reference to a d
1bd00 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a atabase column..
1bd01 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1bd02 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
1bd03 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 3_column_databas
1bd04 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 e_name(sqlite3_s
1bd05 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bd06 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
1bd07 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
1bd08 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
1bd09 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
1bd0a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1bd0b 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 t, COLNAME_DATAB
1bd0c 41 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 ASE);.}.#ifndef
1bd0d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1bd0e 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
1bd0f 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1bd10 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 _column_database
1bd11 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
1bd12 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bd13 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bd14 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bd15 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bd16 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bd17 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bd18 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 xt16, COLNAME_DA
1bd19 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 TABASE);.}.#endi
1bd1a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1bd1b 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a _UTF16 */../*.**
1bd1c 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 Return the name
1bd1d 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 66 72 of the table fr
1bd1e 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c om which a resul
1bd1f 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 t column derives
1bd20 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 ..** NULL is ret
1bd21 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 urned if the res
1bd22 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e ult column is an
1bd23 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 expression or c
1bd24 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e onstant or.** an
1bd25 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 ything else whic
1bd26 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 h is not an unab
1bd27 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 iguous reference
1bd28 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 to a database c
1bd29 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 olumn..*/.SQLITE
1bd2a 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1bd2b 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f *sqlite3_column_
1bd2c 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 table_name(sqlit
1bd2d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bd2e 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1bd2f 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1bd30 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1bd31 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1bd32 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1bd33 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 _text, COLNAME_T
1bd34 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 ABLE);.}.#ifndef
1bd35 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bd36 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
1bd37 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1bd38 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 3_column_table_n
1bd39 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 ame16(sqlite3_st
1bd3a 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e mt *pStmt, int N
1bd3b 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 ){. return colu
1bd3c 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 mnName(. pS
1bd3d 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 tmt, N, (const v
1bd3e 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 oid*(*)(Mem*))sq
1bd3f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1bd40 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 16, COLNAME_TABL
1bd41 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 E);.}.#endif /*
1bd42 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1bd43 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 6 */../*.** Retu
1bd44 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 rn the name of t
1bd45 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 he table column
1bd46 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 from which a res
1bd47 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 ult column deriv
1bd48 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 es..** NULL is r
1bd49 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 eturned if the r
1bd4a 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 esult column is
1bd4b 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 an expression or
1bd4c 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 constant or.**
1bd4d 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 anything else wh
1bd4e 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e ich is not an un
1bd4f 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e abiguous referen
1bd50 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 ce to a database
1bd51 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 column..*/.SQLI
1bd52 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
1bd53 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d r *sqlite3_colum
1bd54 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 n_origin_name(sq
1bd55 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bd56 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1bd57 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1bd58 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1bd59 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1bd5a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1bd5b 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d lue_text, COLNAM
1bd5c 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 E_COLUMN);.}.#if
1bd5d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1bd5e 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1bd5f 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1bd60 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 lite3_column_ori
1bd61 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 gin_name16(sqlit
1bd62 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bd63 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e int N){. return
1bd64 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 columnName(.
1bd65 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f pStmt, N, (co
1bd66 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d nst void*(*)(Mem
1bd67 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *))sqlite3_value
1bd68 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 _text16, COLNAME
1bd69 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 _COLUMN);.}.#end
1bd6a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bd6b 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 T_UTF16 */.#endi
1bd6c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 f /* SQLITE_ENAB
1bd6d 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
1bd6e 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a TA */.../*******
1bd6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd70 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
1bd71 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a _bind_ ********
1bd72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd73 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 ***.** .** Routi
1bd74 6e 65 73 20 75 73 65 64 20 74 6f 20 61 74 74 61 nes used to atta
1bd75 63 68 20 76 61 6c 75 65 73 20 74 6f 20 77 69 6c ch values to wil
1bd76 64 63 61 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 dcards in a comp
1bd77 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 iled SQL stateme
1bd78 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 nt..*/./*.** Unb
1bd79 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20 62 6f ind the value bo
1bd7a 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c 65 20 und to variable
1bd7b 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 i in virtual mac
1bd7c 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69 73 20 hine p. This is
1bd7d 74 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 the .** the same
1bd7e 20 61 73 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 as binding a NU
1bd7f 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 LL value to the
1bd80 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 column. If the "
1bd81 69 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a i" parameter is.
1bd82 2a 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c ** out of range,
1bd83 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e then SQLITE_RAN
1bd84 47 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 GE is returned.
1bd85 4f 74 68 65 77 69 73 65 20 53 51 4c 49 54 45 5f Othewise SQLITE_
1bd86 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 OK..**.** A succ
1bd87 65 73 73 66 75 6c 20 65 76 61 6c 75 61 74 69 6f essful evaluatio
1bd88 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e n of this routin
1bd89 65 20 61 63 71 75 69 72 65 73 20 74 68 65 20 6d e acquires the m
1bd8a 75 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 utex on p..** th
1bd8b 65 20 6d 75 74 65 78 20 69 73 20 72 65 6c 65 61 e mutex is relea
1bd8c 73 65 64 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 sed if any kind
1bd8d 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e of error occurs.
1bd8e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 .**.** The error
1bd8f 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20 code stored in
1bd90 64 61 74 61 62 61 73 65 20 70 2d 3e 64 62 20 69 database p->db i
1bd91 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 s overwritten wi
1bd92 74 68 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a th the return.**
1bd93 20 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 value in any ca
1bd94 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e se..*/.static in
1bd95 74 20 76 64 62 65 55 6e 62 69 6e 64 28 56 64 62 t vdbeUnbind(Vdb
1bd96 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 e *p, int i){.
1bd97 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 Mem *pVar;. if(
1bd98 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 p==0 ) return S
1bd99 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1bd9a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1bd9b 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ter(p->db->mutex
1bd9c 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 );. if( p->magi
1bd9d 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c!=VDBE_MAGIC_RU
1bd9e 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b N || p->pc>=0 ){
1bd9f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1bda0 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f r(p->db, SQLITE_
1bda1 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 MISUSE, 0);.
1bda2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1bda3 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1bda4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
1bda5 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
1bda6 0a 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e . if( i<1 || i>
1bda7 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 p->nVar ){. s
1bda8 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 qlite3Error(p->d
1bda9 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c b, SQLITE_RANGE,
1bdaa 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
1bdab 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1bdac 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 db->mutex);.
1bdad 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 return SQLITE_RA
1bdae 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a NGE;. }. i--;.
1bdaf 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 pVar = &p->aVa
1bdb0 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 r[i];. sqlite3V
1bdb1 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 dbeMemRelease(pV
1bdb2 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 ar);. pVar->fla
1bdb3 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1bdb4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d sqlite3Error(p-
1bdb5 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 >db, SQLITE_OK,
1bdb6 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 0);.. /* If the
1bdb7 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 bit correspondi
1bdb8 6e 67 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 ng to this varia
1bdb9 62 6c 65 20 69 6e 20 56 64 62 65 2e 65 78 70 6d ble in Vdbe.expm
1bdba 61 73 6b 20 69 73 20 73 65 74 2c 20 74 68 65 6e ask is set, then
1bdbb 20 0a 20 20 2a 2a 20 62 69 6e 64 69 6e 67 20 61 . ** binding a
1bdbc 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 74 68 new value to th
1bdbd 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 76 61 is variable inva
1bdbe 6c 69 64 61 74 65 73 20 74 68 65 20 63 75 72 72 lidates the curr
1bdbf 65 6e 74 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a ent query plan..
1bdc0 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 73 */. if( p->is
1bdc1 50 72 65 70 61 72 65 56 32 20 26 26 0a 20 20 20 PrepareV2 &&.
1bdc2 20 20 28 28 69 3c 33 32 20 26 26 20 70 2d 3e 65 ((i<32 && p->e
1bdc3 78 70 6d 61 73 6b 20 26 20 28 28 75 33 32 29 31 xpmask & ((u32)1
1bdc4 20 3c 3c 20 69 29 29 20 7c 7c 20 70 2d 3e 65 78 << i)) || p->ex
1bdc5 70 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 pmask==0xfffffff
1bdc6 66 29 0a 20 20 29 7b 0a 20 20 20 20 70 2d 3e 65 f). ){. p->e
1bdc7 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a xpired = 1;. }.
1bdc8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1bdc9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e OK;.}../*.** Bin
1bdca 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 d a text or BLOB
1bdcb 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 value..*/.stati
1bdcc 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28 0a c int bindText(.
1bdcd 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1bdce 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65 20 pStmt, /* The
1bdcf 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69 6e statement to bin
1bdd0 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 d against */. i
1bdd1 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 nt i,
1bdd2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1bdd3 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 f the parameter
1bdd4 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e to bind */. con
1bdd5 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 st void *zData,
1bdd6 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1bdd7 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62 65 o the data to be
1bdd8 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 bound */. int
1bdd9 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20 nData,
1bdda 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1bddb 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74 6f bytes of data to
1bddc 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 be bound */. v
1bddd 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1bdde 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75 63 *), /* Destruc
1bddf 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 tor for the data
1bde0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e */. u8 encodin
1bde1 67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g /*
1bde2 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 Encoding for the
1bde3 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 data */.){. Vd
1bde4 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 be *p = (Vdbe *)
1bde5 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 pStmt;. Mem *pV
1bde6 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 ar;. int rc;..
1bde7 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 rc = vdbeUnbind
1bde8 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 (p, i);. if( rc
1bde9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1bdea 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30 20 if( zData!=0
1bdeb 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d 20 ){. pVar =
1bdec 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 &p->aVar[i-1];.
1bded 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1bdee 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1bdef 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 Var, zData, nDat
1bdf0 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65 a, encoding, xDe
1bdf1 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 l);. if( rc
1bdf2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 ==SQLITE_OK && e
1bdf3 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20 ncoding!=0 ){.
1bdf4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1bdf5 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1bdf6 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28 70 ding(pVar, ENC(p
1bdf7 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d 0a ->db));. }.
1bdf8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1bdf9 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29 or(p->db, rc, 0)
1bdfa 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1bdfb 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 ite3ApiExit(p->d
1bdfc 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 b, rc);. }.
1bdfd 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bdfe 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1bdff 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1be00 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
1be01 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 Bind a blob valu
1be02 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 e to an SQL stat
1be03 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a ement variable..
1be04 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1be05 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 t sqlite3_bind_b
1be06 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 lob(. sqlite3_s
1be07 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 tmt *pStmt, . i
1be08 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 nt i, . const v
1be09 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 oid *zData, . i
1be0a 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 nt nData, . voi
1be0b 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1be0c 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e .){. return bin
1be0d 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 dText(pStmt, i,
1be0e 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 zData, nData, xD
1be0f 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 el, 0);.}.SQLITE
1be10 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1be11 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c _bind_double(sql
1be12 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1be13 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 , int i, double
1be14 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 rValue){. int r
1be15 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 c;. Vdbe *p = (
1be16 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1be17 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 rc = vdbeUnbind(
1be18 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d p, i);. if( rc=
1be19 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1be1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1be1b 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56 SetDouble(&p->aV
1be1c 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29 ar[i-1], rValue)
1be1d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1be1e 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1be1f 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
1be20 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
1be21 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1be22 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 e3_bind_int(sqli
1be23 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74 te3_stmt *p, int
1be24 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 7b i, int iValue){
1be25 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1be26 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20 3_bind_int64(p,
1be27 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29 3b i, (i64)iValue);
1be28 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
1be29 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 t sqlite3_bind_i
1be2a 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d nt64(sqlite3_stm
1be2b 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c t *pStmt, int i,
1be2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 56 sqlite_int64 iV
1be2d 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b alue){. int rc;
1be2e 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
1be2f 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 be *)pStmt;. rc
1be30 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
1be31 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
1be32 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1be33 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1be34 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72 5b tInt64(&p->aVar[
1be35 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20 i-1], iValue);.
1be36 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1be37 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
1be38 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1be39 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
1be3a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1be3b 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 bind_null(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 29 7b 0a 20 20 69 6e 74 20 72 63 3b nt i){. int rc;
1be3e 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
1be3f 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 be*)pStmt;. rc
1be40 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 = vdbeUnbind(p,
1be41 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 i);. if( rc==SQ
1be42 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 LITE_OK ){. s
1be43 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1be44 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1be45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1be46 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 c;.}.SQLITE_API
1be47 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1be48 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 65 _text( . sqlite
1be49 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 3_stmt *pStmt, .
1be4a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 int i, . cons
1be4b 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a t char *zData, .
1be4c 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 int nData, .
1be4d 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1be4e 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 d*).){. return
1be4f 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 bindText(pStmt,
1be50 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c i, zData, nData,
1be51 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 xDel, SQLITE_UT
1be52 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 F8);.}.#ifndef S
1be53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1be54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1be55 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
1be56 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 t16(. sqlite3_s
1be57 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 tmt *pStmt, . i
1be58 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 nt i, . const v
1be59 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 oid *zData, . i
1be5a 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 nt nData, . voi
1be5b 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1be5c 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e .){. return bin
1be5d 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 dText(pStmt, i,
1be5e 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 zData, nData, xD
1be5f 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 el, SQLITE_UTF16
1be60 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 NATIVE);.}.#endi
1be61 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1be62 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 _UTF16 */.SQLITE
1be63 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1be64 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 _bind_value(sqli
1be65 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1be66 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73 71 int i, const sq
1be67 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1be68 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a lue){. int rc;.
1be69 20 20 73 77 69 74 63 68 28 20 70 56 61 6c 75 65 switch( pValue
1be6a 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 ->type ){. ca
1be6b 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 se SQLITE_INTEGE
1be6c 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 R: {. rc =
1be6d 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 sqlite3_bind_int
1be6e 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 64(pStmt, i, pVa
1be6f 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 lue->u.i);.
1be70 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1be71 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c case SQLITE_FL
1be72 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 OAT: {. rc
1be73 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 = sqlite3_bind_d
1be74 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20 ouble(pStmt, i,
1be75 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20 pValue->r);.
1be76 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1be77 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 case SQLITE_B
1be78 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 LOB: {. if(
1be79 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26 pValue->flags &
1be7a 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
1be7b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1be7c 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
1be7d 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 pStmt, i, pValue
1be7e 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 ->u.nZero);.
1be7f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1be80 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 rc = sqlite3_bi
1be81 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 nd_blob(pStmt, i
1be82 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 , pValue->z, pVa
1be83 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 lue->n,SQLITE_TR
1be84 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 ANSIENT);.
1be85 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1be86 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
1be87 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 LITE_TEXT: {.
1be88 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78 74 rc = bindText
1be89 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75 (pStmt,i, pValu
1be8a 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c e->z, pValue->n,
1be8b 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
1be8c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 T,.
1be8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1be8e 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 pValue->enc);.
1be8f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1be90 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
1be91 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1be92 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 te3_bind_null(pS
1be93 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 62 tmt, i);. b
1be94 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1be95 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
1be96 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1be97 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 lite3_bind_zerob
1be98 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 lob(sqlite3_stmt
1be99 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 *pStmt, int i,
1be9a 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 int n){. int rc
1be9b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 ;. Vdbe *p = (V
1be9c 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 dbe *)pStmt;. r
1be9d 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 c = vdbeUnbind(p
1be9e 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , i);. if( rc==
1be9f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1bea0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1bea1 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 etZeroBlob(&p->a
1bea2 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 Var[i-1], n);.
1bea3 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bea4 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1bea5 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1bea6 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
1bea7 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1bea8 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 68 of wildcards th
1bea9 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 at can be potent
1beaa 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a ially bound to..
1beab 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1beac 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 70 is added to supp
1bead 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e ort DBD::SQLite.
1beae 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 .*/.SQLITE_API
1beaf 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1beb0 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
1beb1 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1beb2 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
1beb3 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1beb4 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 ;. return p ? p
1beb5 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f ->nVar : 0;.}../
1beb6 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61 *.** Create a ma
1beb7 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 pping from varia
1beb8 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 ble numbers to v
1beb9 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a ariable names.**
1beba 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56 in the Vdbe.azV
1bebb 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73 ar[] array, if s
1bebc 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f uch a mapping do
1bebd 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a es not already.*
1bebe 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 * exist..*/.stat
1bebf 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 61 ic void createVa
1bec0 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 rMap(Vdbe *p){.
1bec1 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 if( !p->okVar )
1bec2 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 {. int j;.
1bec3 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 73 71 Op *pOp;. sq
1bec4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1bec5 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b r(p->db->mutex);
1bec6 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 63 65 . /* The race
1bec7 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 20 condition here
1bec8 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 49 66 is harmless. If
1bec9 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 61 6c two threads cal
1beca 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 72 6f l this. ** ro
1becb 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 61 6d utine on the sam
1becc 65 20 56 64 62 65 20 61 74 20 74 68 65 20 73 61 e Vdbe at the sa
1becd 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 62 6f me time, they bo
1bece 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 20 20 th might end.
1becf 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c 69 7a ** up initializ
1bed0 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 7a 56 ing the Vdbe.azV
1bed1 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 61 ar[] array. Tha
1bed2 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 65 78 t is a little ex
1bed3 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 20 tra. ** work
1bed4 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 20 69 but it results i
1bed5 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65 n the same answe
1bed6 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f r.. */. fo
1bed7 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f r(j=0, pOp=p->aO
1bed8 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b p; j<p->nOp; j++
1bed9 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 , pOp++){.
1beda 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1bedb 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a =OP_Variable ){.
1bedc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1bedd 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 pOp->p1>0 && pOp
1bede 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b ->p1<=p->nVar );
1bedf 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 . p->azVa
1bee0 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 r[pOp->p1-1] = p
1bee1 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 Op->p4.z;.
1bee2 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6f }. }. p->o
1bee3 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 kVar = 1;. sq
1bee4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1bee5 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1bee6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1bee7 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1bee8 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 a wildcard para
1bee9 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20 4e meter. Return N
1beea 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65 78 ULL if the index
1beeb 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 61 .** is out of ra
1beec 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77 69 nge or if the wi
1beed 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d 65 ldcard is unname
1beee 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 d..**.** The res
1beef 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55 54 ult is always UT
1bef0 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 F-8..*/.SQLITE_A
1bef1 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
1bef2 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
1bef3 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 meter_name(sqlit
1bef4 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bef5 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a int i){. Vdbe *
1bef6 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1bef7 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 ;. if( p==0 ||
1bef8 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 i<1 || i>p->nVar
1bef9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
1befa 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 ;. }. createVa
1befb 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75 72 rMap(p);. retur
1befc 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b n p->azVar[i-1];
1befd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 .}../*.** Given
1befe 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d a wildcard param
1beff 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 72 eter name, retur
1bf00 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 n the index of t
1bf01 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77 he variable.** w
1bf02 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 20 ith that name.
1bf03 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 76 If there is no v
1bf04 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68 65 ariable with the
1bf05 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 given name,.**
1bf06 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c return 0..*/.SQL
1bf07 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1bf08 73 71 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d sqlite3VdbeParam
1bf09 65 74 65 72 49 6e 64 65 78 28 56 64 62 65 20 2a eterIndex(Vdbe *
1bf0a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a p, const char *z
1bf0b 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 Name, int nName)
1bf0c 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 {. int i;. if(
1bf0d 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 p==0 ){. ret
1bf0e 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 urn 0;. }. cre
1bf0f 61 74 65 56 61 72 4d 61 70 28 70 29 3b 20 0a 20 ateVarMap(p); .
1bf10 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 if( zName ){.
1bf11 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
1bf12 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nVar; i++){.
1bf13 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
1bf14 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 = p->azVar[i];.
1bf15 20 20 20 20 20 69 66 28 20 7a 20 26 26 20 6d 65 if( z && me
1bf16 6d 63 6d 70 28 7a 2c 7a 4e 61 6d 65 2c 6e 4e 61 mcmp(z,zName,nNa
1bf17 6d 65 29 3d 3d 30 20 26 26 20 7a 5b 6e 4e 61 6d me)==0 && z[nNam
1bf18 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 e]==0 ){.
1bf19 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 return i+1;.
1bf1a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1bf1b 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c return 0;.}.SQL
1bf1c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bf1d 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
1bf1e 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 er_index(sqlite3
1bf1f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f _stmt *pStmt, co
1bf20 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 nst char *zName)
1bf21 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1bf22 65 33 56 64 62 65 50 61 72 61 6d 65 74 65 72 49 e3VdbeParameterI
1bf23 6e 64 65 78 28 28 56 64 62 65 2a 29 70 53 74 6d ndex((Vdbe*)pStm
1bf24 74 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 t, zName, sqlite
1bf25 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3Strlen30(zName)
1bf26 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e );.}../*.** Tran
1bf27 73 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 sfer all binding
1bf28 73 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 s from the first
1bf29 20 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 statement over
1bf2a 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a to the second..*
1bf2b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1bf2c 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e int sqlite3Tran
1bf2d 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c sferBindings(sql
1bf2e 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d ite3_stmt *pFrom
1bf2f 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 Stmt, sqlite3_st
1bf30 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 mt *pToStmt){.
1bf31 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 Vdbe *pFrom = (V
1bf32 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a dbe*)pFromStmt;.
1bf33 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 Vdbe *pTo = (V
1bf34 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 dbe*)pToStmt;.
1bf35 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 int i;. assert(
1bf36 20 70 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d pTo->db==pFrom-
1bf37 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 >db );. assert(
1bf38 20 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f pTo->nVar==pFro
1bf39 6d 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c m->nVar );. sql
1bf3a 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1bf3b 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 (pTo->db->mutex)
1bf3c 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
1bf3d 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 From->nVar; i++)
1bf3e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1bf3f 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 eMemMove(&pTo->a
1bf40 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e Var[i], &pFrom->
1bf41 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 aVar[i]);. }.
1bf42 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1bf43 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 ave(pTo->db->mut
1bf44 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ex);. return SQ
1bf45 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e LITE_OK;.}..#ifn
1bf46 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1bf47 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a DEPRECATED./*.**
1bf48 20 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65 Deprecated exte
1bf49 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 rnal interface.
1bf4a 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 Internal/core S
1bf4b 51 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 QLite code.** sh
1bf4c 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 ould call sqlite
1bf4d 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 3TransferBinding
1bf4e 73 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d s..**.** Is is m
1bf4f 69 73 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 isuse to call th
1bf50 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 is routine with
1bf51 73 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 statements from
1bf52 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 different.** dat
1bf53 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1bf54 73 2e 20 20 42 75 74 20 61 73 20 74 68 69 73 20 s. But as this
1bf55 69 73 20 61 20 64 65 70 72 65 63 61 74 65 64 20 is a deprecated
1bf56 69 6e 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a interface, we.**
1bf57 20 77 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 will not bother
1bf58 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 to check for th
1bf59 61 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a at condition..**
1bf5a 0a 2a 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 .** If the two s
1bf5b 74 61 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 tatements contai
1bf5c 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 n a different nu
1bf5d 6d 62 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 mber of bindings
1bf5e 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c , then.** an SQL
1bf5f 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 ITE_ERROR is ret
1bf60 75 72 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 urned. Nothing
1bf61 65 6c 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e else can go wron
1bf62 67 2c 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a g, so otherwise.
1bf63 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
1bf64 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
1bf65 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bf66 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e te3_transfer_bin
1bf67 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 dings(sqlite3_st
1bf68 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 mt *pFromStmt, s
1bf69 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f qlite3_stmt *pTo
1bf6a 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1bf6b 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 From = (Vdbe*)pF
1bf6c 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 romStmt;. Vdbe
1bf6d 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 *pTo = (Vdbe*)pT
1bf6e 6f 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 oStmt;. if( pFr
1bf6f 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e om->nVar!=pTo->n
1bf70 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Var ){. retur
1bf71 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1bf72 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 }. if( pTo->i
1bf73 73 50 72 65 70 61 72 65 56 32 20 26 26 20 70 54 sPrepareV2 && pT
1bf74 6f 2d 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 o->expmask ){.
1bf75 20 20 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d pTo->expired =
1bf76 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 1;. }. if( pF
1bf77 72 6f 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32 rom->isPrepareV2
1bf78 20 26 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 && pFrom->expma
1bf79 73 6b 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d sk ){. pFrom-
1bf7a 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 >expired = 1;.
1bf7b 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 }. return sqlit
1bf7c 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e e3TransferBindin
1bf7d 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 gs(pFromStmt, pT
1bf7e 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 oStmt);.}.#endif
1bf7f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1bf80 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 he sqlite3* data
1bf81 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 base handle to w
1bf82 68 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65 hich the prepare
1bf83 64 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 d statement give
1bf84 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 n.** in the argu
1bf85 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 ment belongs. T
1bf86 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 his is the same
1bf87 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 database handle
1bf88 74 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 that was.** the
1bf89 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
1bf8a 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 o the sqlite3_pr
1bf8b 65 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73 epare() that was
1bf8c 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a used to create.
1bf8d 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ** the statement
1bf8e 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c in the first pl
1bf8f 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ace..*/.SQLITE_A
1bf90 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
1bf91 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 te3_db_handle(sq
1bf92 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bf93 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 t){. return pSt
1bf94 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 mt ? ((Vdbe*)pSt
1bf95 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a mt)->db : 0;.}..
1bf96 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
1bf97 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
1bf98 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 xt prepared stat
1bf99 65 6d 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d ement after pStm
1bf9a 74 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 t associated.**
1bf9b 77 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f with database co
1bf9c 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 nnection pDb. I
1bf9d 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c f pStmt is NULL,
1bf9e 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 return the firs
1bf9f 74 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 t.** prepared st
1bfa0 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 atement for the
1bfa1 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1bfa2 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c ion. Return NUL
1bfa3 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 L if there.** ar
1bfa4 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 e no more..*/.SQ
1bfa5 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
1bfa6 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e _stmt *sqlite3_n
1bfa7 65 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 ext_stmt(sqlite3
1bfa8 20 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 *pDb, sqlite3_s
1bfa9 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 tmt *pStmt){. s
1bfaa 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 qlite3_stmt *pNe
1bfab 78 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 xt;. sqlite3_mu
1bfac 74 65 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d tex_enter(pDb->m
1bfad 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 utex);. if( pSt
1bfae 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 mt==0 ){. pNe
1bfaf 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 xt = (sqlite3_st
1bfb0 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a mt*)pDb->pVdbe;.
1bfb1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
1bfb2 78 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 xt = (sqlite3_st
1bfb3 6d 74 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d mt*)((Vdbe*)pStm
1bfb4 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 t)->pNext;. }.
1bfb5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1bfb6 65 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 eave(pDb->mutex)
1bfb7 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 ;. return pNext
1bfb8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1bfb9 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 n the value of a
1bfba 20 73 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 status counter
1bfbb 66 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 for a prepared s
1bfbc 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 tatement.*/.SQLI
1bfbd 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bfbe 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 e3_stmt_status(s
1bfbf 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bfc0 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 mt, int op, int
1bfc1 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 resetFlag){. Vd
1bfc2 62 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 be *pVdbe = (Vdb
1bfc3 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 e*)pStmt;. int
1bfc4 76 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e v = pVdbe->aCoun
1bfc5 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 ter[op-1];. if(
1bfc6 20 72 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 resetFlag ) pVd
1bfc7 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d be->aCounter[op-
1bfc8 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 1] = 0;. return
1bfc9 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a v;.}../********
1bfca 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
1bfcb 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a beapi.c ********
1bfcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfce 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1bfcf 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1bfd0 65 20 76 64 62 65 74 72 61 63 65 2e 63 20 2a 2a e vdbetrace.c **
1bfd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfd3 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1bfd4 39 20 4e 6f 76 65 6d 62 65 72 20 32 35 0a 2a 2a 9 November 25.**
1bfd5 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1bfd6 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1bfd7 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1bfd8 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1bfd9 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1bfda 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1bfdb 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1bfdc 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1bfdd 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1bfde 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1bfdf 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1bfe0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1bfe1 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1bfe2 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1bfe3 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1bfe4 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1bfe5 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1bfe6 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1bfe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bfea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1bfeb 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1bfec 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 tains code used
1bfed 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 76 61 to insert the va
1bfee 6c 75 65 73 20 6f 66 20 68 6f 73 74 20 70 61 72 lues of host par
1bfef 61 6d 65 74 65 72 73 0a 2a 2a 20 28 61 6b 61 20 ameters.** (aka
1bff0 22 77 69 6c 64 63 61 72 64 73 22 29 20 69 6e 74 "wildcards") int
1bff1 6f 20 74 68 65 20 53 51 4c 20 74 65 78 74 20 6f o the SQL text o
1bff2 75 74 70 75 74 20 62 79 20 73 71 6c 69 74 65 33 utput by sqlite3
1bff3 5f 74 72 61 63 65 28 29 2e 0a 2a 2f 0a 0a 23 69 _trace()..*/..#i
1bff4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1bff5 54 5f 54 52 41 43 45 0a 0a 2f 2a 0a 2a 2a 20 7a T_TRACE../*.** z
1bff6 53 71 6c 20 69 73 20 61 20 7a 65 72 6f 2d 74 65 Sql is a zero-te
1bff7 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 rminated string
1bff8 6f 66 20 55 54 46 2d 38 20 53 51 4c 20 74 65 78 of UTF-8 SQL tex
1bff9 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e t. Return the n
1bffa 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 umber of.** byte
1bffb 73 20 69 6e 20 74 68 69 73 20 74 65 78 74 20 75 s in this text u
1bffc 70 20 74 6f 20 62 75 74 20 65 78 63 6c 75 64 69 p to but excludi
1bffd 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 68 61 ng the first cha
1bffe 72 61 63 74 65 72 20 69 6e 0a 2a 2a 20 61 20 68 racter in.** a h
1bfff 6f 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20 ost parameter.
1c000 49 66 20 74 68 65 20 74 65 78 74 20 63 6f 6e 74 If the text cont
1c001 61 69 6e 73 20 6e 6f 20 68 6f 73 74 20 70 61 72 ains no host par
1c002 61 6d 65 74 65 72 73 2c 20 72 65 74 75 72 6e 0a ameters, return.
1c003 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d ** the total num
1c004 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1c005 74 68 65 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 the text..*/.sta
1c006 74 69 63 20 69 6e 74 20 66 69 6e 64 4e 65 78 74 tic int findNext
1c007 48 6f 73 74 50 61 72 61 6d 65 74 65 72 28 63 6f HostParameter(co
1c008 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 nst char *zSql,
1c009 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 int *pnToken){.
1c00a 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b 0a int tokenType;.
1c00b 20 20 69 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 int nTotal = 0
1c00c 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 2a 70 ;. int n;.. *p
1c00d 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 77 68 nToken = 0;. wh
1c00e 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a ile( zSql[0] ){.
1c00f 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 n = sqlite3G
1c010 65 74 54 6f 6b 65 6e 28 28 75 38 2a 29 7a 53 71 etToken((u8*)zSq
1c011 6c 2c 20 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a l, &tokenType);.
1c012 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 assert( n>0
1c013 26 26 20 74 6f 6b 65 6e 54 79 70 65 21 3d 54 4b && tokenType!=TK
1c014 5f 49 4c 4c 45 47 41 4c 20 29 3b 0a 20 20 20 20 _ILLEGAL );.
1c015 69 66 28 20 74 6f 6b 65 6e 54 79 70 65 3d 3d 54 if( tokenType==T
1c016 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b 0a 20 20 K_VARIABLE ){.
1c017 20 20 20 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e *pnToken = n
1c018 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1c019 20 20 20 7d 0a 20 20 20 20 6e 54 6f 74 61 6c 20 }. nTotal
1c01a 2b 3d 20 6e 3b 0a 20 20 20 20 7a 53 71 6c 20 2b += n;. zSql +
1c01b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = n;. }. retur
1c01c 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 2f 2a 0a n nTotal;.}../*.
1c01d 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
1c01e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 ter to a string
1c01f 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e in memory obtain
1c020 65 64 20 66 6f 72 6d 20 73 71 6c 69 74 65 33 44 ed form sqlite3D
1c021 62 4d 61 6c 6c 6f 63 28 29 20 77 68 69 63 68 0a bMalloc() which.
1c022 2a 2a 20 68 6f 6c 64 73 20 61 20 63 6f 70 79 20 ** holds a copy
1c023 6f 66 20 7a 52 61 77 53 71 6c 20 62 75 74 20 77 of zRawSql but w
1c024 69 74 68 20 68 6f 73 74 20 70 61 72 61 6d 65 74 ith host paramet
1c025 65 72 73 20 65 78 70 61 6e 64 65 64 20 74 6f 20 ers expanded to
1c026 74 68 65 69 72 0a 2a 2a 20 63 75 72 72 65 6e 74 their.** current
1c027 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a bindings..**.**
1c028 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e The calling fun
1c029 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 ction is respons
1c02a 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 ible for making
1c02b 73 75 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 sure the memory
1c02c 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 65 returned.** is e
1c02d 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e ventually freed.
1c02e 0a 2a 2a 0a 2a 2a 20 41 4c 47 4f 52 49 54 48 4d .**.** ALGORITHM
1c02f 3a 20 20 53 63 61 6e 20 74 68 65 20 69 6e 70 75 : Scan the inpu
1c030 74 20 73 74 72 69 6e 67 20 6c 6f 6f 6b 69 6e 67 t string looking
1c031 20 66 6f 72 20 68 6f 73 74 20 70 61 72 61 6d 65 for host parame
1c032 74 65 72 73 20 69 6e 20 61 6e 79 20 6f 66 0a 2a ters in any of.*
1c033 2a 20 74 68 65 73 65 20 66 6f 72 6d 73 3a 20 20 * these forms:
1c034 3f 2c 20 3f 4e 2c 20 24 41 2c 20 40 41 2c 20 3a ?, ?N, $A, @A, :
1c035 41 2e 20 20 54 61 6b 65 20 63 61 72 65 20 74 6f A. Take care to
1c036 20 61 76 6f 69 64 20 74 65 78 74 20 77 69 74 68 avoid text with
1c037 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 in.** string lit
1c038 65 72 61 6c 73 2c 20 71 75 6f 74 65 64 20 69 64 erals, quoted id
1c039 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 2c 20 entifier names,
1c03a 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2e 20 20 46 and comments. F
1c03b 6f 72 20 74 65 78 74 20 66 6f 72 6d 73 2c 0a 2a or text forms,.*
1c03c 2a 20 74 68 65 20 68 6f 73 74 20 70 61 72 61 6d * the host param
1c03d 65 74 65 72 20 69 6e 64 65 78 20 69 73 20 66 6f eter index is fo
1c03e 75 6e 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 und by scanning
1c03f 74 68 65 20 70 65 72 70 61 72 65 64 0a 2a 2a 20 the perpared.**
1c040 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 statement for th
1c041 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1c042 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f 70 63 6f OP_Variable opco
1c043 64 65 2e 20 20 4f 6e 63 65 20 74 68 65 20 68 6f de. Once the ho
1c044 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 st.** parameter
1c045 69 6e 64 65 78 20 69 73 20 6b 6e 6f 77 6e 2c 20 index is known,
1c046 6c 6f 63 61 74 65 20 74 68 65 20 76 61 6c 75 65 locate the value
1c047 20 69 6e 20 70 2d 3e 61 56 61 72 5b 5d 2e 20 20 in p->aVar[].
1c048 54 68 65 6e 20 72 65 6e 64 65 72 0a 2a 2a 20 74 Then render.** t
1c049 68 65 20 76 61 6c 75 65 20 61 73 20 61 20 6c 69 he value as a li
1c04a 74 65 72 61 6c 20 69 6e 20 70 6c 61 63 65 20 6f teral in place o
1c04b 66 20 74 68 65 20 68 6f 73 74 20 70 61 72 61 6d f the host param
1c04c 65 74 65 72 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 eter name..*/.SQ
1c04d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
1c04e 72 20 2a 73 71 6c 69 74 65 33 56 64 62 65 45 78 r *sqlite3VdbeEx
1c04f 70 61 6e 64 53 71 6c 28 0a 20 20 56 64 62 65 20 pandSql(. Vdbe
1c050 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
1c051 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70 61 /* The prepa
1c052 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 62 65 red statement be
1c053 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 2a 2f ing evaluated */
1c054 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1c055 52 61 77 53 71 6c 20 20 20 20 20 20 2f 2a 20 52 RawSql /* R
1c056 61 77 20 74 65 78 74 20 6f 66 20 74 68 65 20 53 aw text of the S
1c057 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a QL statement */.
1c058 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1c059 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c05a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1c05b 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e nnection */. in
1c05c 74 20 69 64 78 20 3d 20 30 3b 20 20 20 20 20 20 t idx = 0;
1c05d 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1c05e 6f 66 20 61 20 68 6f 73 74 20 70 61 72 61 6d 65 of a host parame
1c05f 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 ter */. int nex
1c060 74 49 6e 64 65 78 20 3d 20 31 3b 20 20 20 20 20 tIndex = 1;
1c061 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 /* Index of ne
1c062 78 74 20 3f 20 68 6f 73 74 20 70 61 72 61 6d 65 xt ? host parame
1c063 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 ter */. int n;
1c064 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c065 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 /* Length of a
1c066 20 74 6f 6b 65 6e 20 70 72 65 66 69 78 20 2a 2f token prefix */
1c067 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 . int nToken;
1c068 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c069 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61 72 ength of the par
1c06a 61 6d 65 74 65 72 20 74 6f 6b 65 6e 20 2a 2f 0a ameter token */.
1c06b 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
1c06c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
1c06d 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1c06e 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 Mem *pVar;
1c06f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
1c070 65 20 6f 66 20 61 20 68 6f 73 74 20 70 61 72 61 e of a host para
1c071 6d 65 74 65 72 20 2a 2f 0a 20 20 53 74 72 41 63 meter */. StrAc
1c072 63 75 6d 20 6f 75 74 3b 20 20 20 20 20 20 20 20 cum out;
1c073 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 /* Accumulat
1c074 65 20 74 68 65 20 6f 75 74 70 75 74 20 68 65 72 e the output her
1c075 65 20 2a 2f 0a 20 20 63 68 61 72 20 7a 42 61 73 e */. char zBas
1c076 65 5b 31 30 30 5d 3b 20 20 20 20 20 20 20 20 20 e[100];
1c077 2f 2a 20 49 6e 69 74 69 61 6c 20 77 6f 72 6b 69 /* Initial worki
1c078 6e 67 20 73 70 61 63 65 20 2a 2f 0a 0a 20 20 64 ng space */.. d
1c079 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c b = p->db;. sql
1c07a 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
1c07b 28 26 6f 75 74 2c 20 7a 42 61 73 65 2c 20 73 69 (&out, zBase, si
1c07c 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 0a 20 20 zeof(zBase), .
1c07d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c07e 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 db->aLimit[S
1c07f 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1c080 54 48 5d 29 3b 0a 20 20 6f 75 74 2e 64 62 20 3d TH]);. out.db =
1c081 20 64 62 3b 0a 20 20 77 68 69 6c 65 28 20 7a 52 db;. while( zR
1c082 61 77 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 awSql[0] ){.
1c083 6e 20 3d 20 66 69 6e 64 4e 65 78 74 48 6f 73 74 n = findNextHost
1c084 50 61 72 61 6d 65 74 65 72 28 7a 52 61 77 53 71 Parameter(zRawSq
1c085 6c 2c 20 26 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 l, &nToken);.
1c086 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a assert( n>0 );.
1c087 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
1c088 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 cumAppend(&out,
1c089 7a 52 61 77 53 71 6c 2c 20 6e 29 3b 0a 20 20 20 zRawSql, n);.
1c08a 20 7a 52 61 77 53 71 6c 20 2b 3d 20 6e 3b 0a 20 zRawSql += n;.
1c08b 20 20 20 61 73 73 65 72 74 28 20 7a 52 61 77 53 assert( zRawS
1c08c 71 6c 5b 30 5d 20 7c 7c 20 6e 54 6f 6b 65 6e 3d ql[0] || nToken=
1c08d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 54 =0 );. if( nT
1c08e 6f 6b 65 6e 3d 3d 30 20 29 20 62 72 65 61 6b 3b oken==0 ) break;
1c08f 0a 20 20 20 20 69 66 28 20 7a 52 61 77 53 71 6c . if( zRawSql
1c090 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 [0]=='?' ){.
1c091 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3e 31 20 29 if( nToken>1 )
1c092 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1c093 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 ( sqlite3Isdigit
1c094 28 7a 52 61 77 53 71 6c 5b 31 5d 29 20 29 3b 0a (zRawSql[1]) );.
1c095 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 sqlite3G
1c096 65 74 49 6e 74 33 32 28 26 7a 52 61 77 53 71 6c etInt32(&zRawSql
1c097 5b 31 5d 2c 20 26 69 64 78 29 3b 0a 20 20 20 20 [1], &idx);.
1c098 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1c099 20 69 64 78 20 3d 20 6e 65 78 74 49 6e 64 65 78 idx = nextIndex
1c09a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
1c09b 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
1c09c 74 28 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 t( zRawSql[0]=='
1c09d 3a 27 20 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d :' || zRawSql[0]
1c09e 3d 3d 27 24 27 20 7c 7c 20 7a 52 61 77 53 71 6c =='$' || zRawSql
1c09f 5b 30 5d 3d 3d 27 40 27 20 29 3b 0a 20 20 20 20 [0]=='@' );.
1c0a0 20 20 74 65 73 74 63 61 73 65 28 20 7a 52 61 77 testcase( zRaw
1c0a1 53 71 6c 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20 Sql[0]==':' );.
1c0a2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a testcase( z
1c0a3 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 24 27 20 29 RawSql[0]=='$' )
1c0a4 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1c0a5 28 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 40 ( zRawSql[0]=='@
1c0a6 27 20 29 3b 0a 20 20 20 20 20 20 69 64 78 20 3d ' );. idx =
1c0a7 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72 61 sqlite3VdbePara
1c0a8 6d 65 74 65 72 49 6e 64 65 78 28 70 2c 20 7a 52 meterIndex(p, zR
1c0a9 61 77 53 71 6c 2c 20 6e 54 6f 6b 65 6e 29 3b 0a awSql, nToken);.
1c0aa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64 assert( id
1c0ab 78 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 x>0 );. }.
1c0ac 20 7a 52 61 77 53 71 6c 20 2b 3d 20 6e 54 6f 6b zRawSql += nTok
1c0ad 65 6e 3b 0a 20 20 20 20 6e 65 78 74 49 6e 64 65 en;. nextInde
1c0ae 78 20 3d 20 69 64 78 20 2b 20 31 3b 0a 20 20 20 x = idx + 1;.
1c0af 20 61 73 73 65 72 74 28 20 69 64 78 3e 30 20 26 assert( idx>0 &
1c0b0 26 20 69 64 78 3c 3d 70 2d 3e 6e 56 61 72 20 29 & idx<=p->nVar )
1c0b1 3b 0a 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d ;. pVar = &p-
1c0b2 3e 61 56 61 72 5b 69 64 78 2d 31 5d 3b 0a 20 20 >aVar[idx-1];.
1c0b3 20 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61 67 if( pVar->flag
1c0b4 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1c0b5 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
1c0b6 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 AccumAppend(&out
1c0b7 2c 20 22 4e 55 4c 4c 22 2c 20 34 29 3b 0a 20 20 , "NULL", 4);.
1c0b8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 56 61 72 }else if( pVar
1c0b9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1c0ba 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 t ){. sqlit
1c0bb 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 e3XPrintf(&out,
1c0bc 22 25 6c 6c 64 22 2c 20 70 56 61 72 2d 3e 75 2e "%lld", pVar->u.
1c0bd 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 i);. }else if
1c0be 28 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 20 ( pVar->flags &
1c0bf 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
1c0c0 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 sqlite3XPrintf
1c0c1 28 26 6f 75 74 2c 20 22 25 21 2e 31 35 67 22 2c (&out, "%!.15g",
1c0c2 20 70 56 61 72 2d 3e 72 29 3b 0a 20 20 20 20 7d pVar->r);. }
1c0c3 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e 66 else if( pVar->f
1c0c4 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 lags & MEM_Str )
1c0c5 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
1c0c6 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 20 20 _OMIT_UTF16.
1c0c7 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 u8 enc = ENC(d
1c0c8 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 6e b);. if( en
1c0c9 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 c!=SQLITE_UTF8 )
1c0ca 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 75 74 {. Mem ut
1c0cb 66 38 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 f8;. mems
1c0cc 65 74 28 26 75 74 66 38 2c 20 30 2c 20 73 69 7a et(&utf8, 0, siz
1c0cd 65 6f 66 28 75 74 66 38 29 29 3b 0a 20 20 20 20 eof(utf8));.
1c0ce 20 20 20 20 75 74 66 38 2e 64 62 20 3d 20 64 62 utf8.db = db
1c0cf 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1c0d0 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 3VdbeMemSetStr(&
1c0d1 75 74 66 38 2c 20 70 56 61 72 2d 3e 7a 2c 20 70 utf8, pVar->z, p
1c0d2 56 61 72 2d 3e 6e 2c 20 65 6e 63 2c 20 53 51 4c Var->n, enc, SQL
1c0d3 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 ITE_STATIC);.
1c0d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c0d5 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 ChangeEncoding(&
1c0d6 75 74 66 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 utf8, SQLITE_UTF
1c0d7 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 8);. sqli
1c0d8 74 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c te3XPrintf(&out,
1c0d9 20 22 27 25 2e 2a 71 27 22 2c 20 75 74 66 38 2e "'%.*q'", utf8.
1c0da 6e 2c 20 75 74 66 38 2e 7a 29 3b 0a 20 20 20 20 n, utf8.z);.
1c0db 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1c0dc 65 6d 52 65 6c 65 61 73 65 28 26 75 74 66 38 29 emRelease(&utf8)
1c0dd 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 ;. }else.#e
1c0de 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 ndif. {.
1c0df 20 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 sqlite3XPri
1c0e0 6e 74 66 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71 ntf(&out, "'%.*q
1c0e1 27 22 2c 20 70 56 61 72 2d 3e 6e 2c 20 70 56 61 '", pVar->n, pVa
1c0e2 72 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r->z);. }.
1c0e3 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 56 61 }else if( pVa
1c0e4 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a r->flags & MEM_Z
1c0e5 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ero ){. sql
1c0e6 69 74 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74 ite3XPrintf(&out
1c0e7 2c 20 22 7a 65 72 6f 62 6c 6f 62 28 25 64 29 22 , "zeroblob(%d)"
1c0e8 2c 20 70 56 61 72 2d 3e 75 2e 6e 5a 65 72 6f 29 , pVar->u.nZero)
1c0e9 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1c0ea 20 20 20 61 73 73 65 72 74 28 20 70 56 61 72 2d assert( pVar-
1c0eb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1c0ec 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 b );. sqlit
1c0ed 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
1c0ee 28 26 6f 75 74 2c 20 22 78 27 22 2c 20 32 29 3b (&out, "x'", 2);
1c0ef 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
1c0f0 69 3c 70 56 61 72 2d 3e 6e 3b 20 69 2b 2b 29 7b i<pVar->n; i++){
1c0f1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c0f2 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 XPrintf(&out, "%
1c0f3 30 32 78 22 2c 20 70 56 61 72 2d 3e 7a 5b 69 5d 02x", pVar->z[i]
1c0f4 26 30 78 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a &0xff);. }.
1c0f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
1c0f6 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 AccumAppend(&out
1c0f7 2c 20 22 27 22 2c 20 31 29 3b 0a 20 20 20 20 7d , "'", 1);. }
1c0f8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 . }. return sq
1c0f9 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
1c0fa 69 73 68 28 26 6f 75 74 29 3b 0a 7d 0a 0a 23 65 ish(&out);.}..#e
1c0fb 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 ndif /* #ifndef
1c0fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1c0fd 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
1c0fe 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1c0ff 65 74 72 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a etrace.c *******
1c100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c101 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c102 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1c103 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1c104 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c ********
1c105 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c106 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c107 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
1c108 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
1c109 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1c10a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1c10b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1c10c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1c10d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1c10e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1c10f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1c110 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1c111 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1c112 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1c113 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1c114 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1c115 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1c116 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1c117 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1c118 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1c119 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1c11a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1c11b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c11c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c11d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c11e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1c11f 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
1c120 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 file implements
1c121 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f execution metho
1c122 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 d of the .** Vir
1c123 74 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e tual Database En
1c124 67 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20 gine (VDBE). A
1c125 73 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22 separate file ("
1c126 76 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 vdbeaux.c").** h
1c127 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 andles housekeep
1c128 69 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68 ing details such
1c129 20 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64 as creating and
1c12a 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 deleting.** VDB
1c12b 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 E instances. Th
1c12c 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c is file is solel
1c12d 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20 y interested in
1c12e 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 executing.** the
1c12f 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a VDBE program..*
1c130 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 *.** In the exte
1c131 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 rnal interface,
1c132 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 an "sqlite3_stmt
1c133 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 *" is an opaque
1c134 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 pointer.** to a
1c135 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 VDBE..**.** The
1c136 53 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72 SQL parser gener
1c137 61 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77 ates a program w
1c138 68 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65 hich is then exe
1c139 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 cuted by.** the
1c13a 56 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77 VDBE to do the w
1c13b 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73 ork of the SQL s
1c13c 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 tatement. VDBE
1c13d 70 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a programs are .**
1c13e 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d similar in form
1c13f 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e to assembly lan
1c140 67 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67 guage. The prog
1c141 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a ram consists of.
1c142 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75 ** a linear sequ
1c143 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f ence of operatio
1c144 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74 ns. Each operat
1c145 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 ion has an opcod
1c146 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 e .** and 5 oper
1c147 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 ands. Operands
1c148 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61 P1, P2, and P3 a
1c149 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 re integers. Op
1c14a 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 erand P4 .** is
1c14b 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 a null-terminate
1c14c 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 d string. Opera
1c14d 6e 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69 nd P5 is an unsi
1c14e 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a gned character..
1c14f 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75 ** Few opcodes u
1c150 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 se all 5 operand
1c151 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 s..**.** Computa
1c152 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65 tion results are
1c153 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 stored on a set
1c154 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75 of registers nu
1c155 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 mbered beginning
1c156 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67 .** with 1 and g
1c157 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e oing up to Vdbe.
1c158 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69 nMem. Each regi
1c159 73 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a ster can store.*
1c15a 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 * either an inte
1c15b 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d ger, a null-term
1c15c 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61 inated string, a
1c15d 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a floating point.
1c15e 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 ** number, or th
1c15f 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c e SQL "NULL" val
1c160 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 ue. An implicit
1c161 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d conversion from
1c162 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 one.** type to
1c163 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73 the other occurs
1c164 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a as necessary..*
1c165 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 * .** Most of th
1c166 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 e code in this f
1c167 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20 ile is taken up
1c168 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 by the sqlite3Vd
1c169 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 beExec().** func
1c16a 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 tion which does
1c16b 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 the work of inte
1c16c 72 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20 rpreting a VDBE
1c16d 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 program..** But
1c16e 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61 other routines a
1c16f 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 re also provided
1c170 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c to help in buil
1c171 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f ding up.** a pro
1c172 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e gram instruction
1c173 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e by instruction.
1c174 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 .**.** Various s
1c175 63 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73 cripts scan this
1c176 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20 source file in
1c177 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 order to generat
1c178 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 e HTML.** docume
1c179 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 ntation, headers
1c17a 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 files, or other
1c17b 20 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20 derived files.
1c17c 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a The formatting.
1c17d 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 ** of the code i
1c17e 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20 n this file is,
1c17f 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 therefore, impor
1c180 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72 tant. See other
1c181 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 comments.** in
1c182 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 this file for de
1c183 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f tails. If in do
1c184 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 ubt, do not devi
1c185 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e ate from existin
1c186 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 g.** commenting
1c187 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 and indentation
1c188 70 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63 practices when c
1c189 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e hanging or addin
1c18a 67 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a g code..*/../*.*
1c18b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1c18c 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1c18d 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1c18e 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73 very time a curs
1c18f 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 or.** moves, eit
1c190 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65 her by the OP_Se
1c191 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f ekXX, OP_Next, o
1c192 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 r OP_Prev opcode
1c193 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 s. The test.**
1c194 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 procedures use t
1c195 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
1c196 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
1c197 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a t indices are.**
1c198 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 working correct
1c199 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 ly. This variab
1c19a 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 le has no functi
1c19b 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f on other than to
1c19c 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 .** help verify
1c19d 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 the correct oper
1c19e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 ation of the lib
1c19f 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 rary..*/.#ifdef
1c1a0 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
1c1a1 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1c1a2 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 e3_search_count
1c1a3 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
1c1a4 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f ** When this glo
1c1a5 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 bal variable is
1c1a6 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74 positive, it get
1c1a7 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e s decremented on
1c1a8 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 ce before.** eac
1c1a9 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e h instruction in
1c1aa 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e the VDBE. When
1c1ab 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 reaches zero, t
1c1ac 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70 he u1.isInterrup
1c1ad 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 ted.** field of
1c1ae 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75 the sqlite3 stru
1c1af 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20 cture is set in
1c1b0 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 order to simulat
1c1b1 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e e and interrupt.
1c1b2 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 .**.** This faci
1c1b3 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72 lity is used for
1c1b4 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 testing purpose
1c1b5 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 s only. It does
1c1b6 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a not function.**
1c1b7 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 in an ordinary
1c1b8 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 build..*/.#ifdef
1c1b9 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1c1ba 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1c1bb 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f te3_interrupt_co
1c1bc 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
1c1bd 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 ./*.** The next
1c1be 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1c1bf 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1c1c0 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f ach type the OP_
1c1c1 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 Sort opcode.** i
1c1c2 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65 s executed. The
1c1c3 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 test procedures
1c1c4 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d use this inform
1c1c5 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 ation to make su
1c1c6 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 re that.** sorti
1c1c7 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20 ng is occurring
1c1c8 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 or not occurring
1c1c9 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 at appropriate
1c1ca 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61 times. This va
1c1cb 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f riable.** has no
1c1cc 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 function other
1c1cd 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 than to help ver
1c1ce 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
1c1cf 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
1c1d0 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a .** library..*/.
1c1d1 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1c1d2 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
1c1d3 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 t sqlite3_sort_c
1c1d4 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
1c1d5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ../*.** The next
1c1d6 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1c1d7 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a records the siz
1c1d8 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 e of the largest
1c1d9 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 MEM_Blob.** or
1c1da 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73 MEM_Str that has
1c1db 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20 been used by a
1c1dc 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 VDBE opcode. Th
1c1dd 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 e test procedure
1c1de 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e s.** use this in
1c1df 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b formation to mak
1c1e0 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
1c1e1 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 zero-blob functi
1c1e2 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f onality.** is wo
1c1e3 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e rking correctly.
1c1e4 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 This variable
1c1e5 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e has no function
1c1e6 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a other than to.*
1c1e7 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 * help verify th
1c1e8 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 e correct operat
1c1e9 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 ion of the libra
1c1ea 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ry..*/.#ifdef SQ
1c1eb 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
1c1ec 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1c1ed 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 _max_blobsize =
1c1ee 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 0;.static void u
1c1ef 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 pdateMaxBlobsize
1c1f0 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 (Mem *p){. if(
1c1f1 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d (p->flags & (MEM
1c1f2 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 _Str|MEM_Blob))!
1c1f3 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 =0 && p->n>sqlit
1c1f4 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 e3_max_blobsize
1c1f5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
1c1f6 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d ax_blobsize = p-
1c1f7 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 >n;. }.}.#endif
1c1f8 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ../*.** The next
1c1f9 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1c1fa 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
1c1fb 65 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 each type the OP
1c1fc 5f 46 6f 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a _Found opcode.**
1c1fd 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 is executed. Th
1c1fe 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 is is used to te
1c1ff 73 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f st whether or no
1c200 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 t the foreign ke
1c201 79 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 y.** operation i
1c202 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 mplemented using
1c203 20 4f 50 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20 OP_FkIsZero is
1c204 77 6f 72 6b 69 6e 67 2e 20 54 68 69 73 20 76 61 working. This va
1c205 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f riable.** has no
1c206 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 function other
1c207 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 than to help ver
1c208 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
1c209 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
1c20a 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a .** library..*/.
1c20b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1c20c 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
1c20d 74 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f t sqlite3_found_
1c20e 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 count = 0;.#endi
1c20f 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 f../*.** Test a
1c210 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 register to see
1c211 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 if it exceeds th
1c212 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 e current maximu
1c213 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 m blob size..**
1c214 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f If it does, reco
1c215 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d rd the new maxim
1c216 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f um blob size..*/
1c217 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
1c218 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 ITE_TEST) && !de
1c219 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1c21a 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a T_BUILTIN_TEST).
1c21b 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f # define UPDATE_
1c21c 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 MAX_BLOBSIZE(P)
1c21d 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 updateMaxBlobsi
1c21e 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 ze(P).#else.# de
1c21f 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f fine UPDATE_MAX_
1c220 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 BLOBSIZE(P).#end
1c221 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 if../*.** Conver
1c222 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 t the given regi
1c223 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 ster into a stri
1c224 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f ng if it isn't o
1c225 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 ne.** already. R
1c226 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 eturn non-zero i
1c227 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
1c228 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ls..*/.#define S
1c229 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 tringify(P, enc)
1c22a 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 \. if(((P)->f
1c22b 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 lags&(MEM_Str|ME
1c22c 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 M_Blob))==0 && s
1c22d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
1c22e 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c ingify(P,enc)) \
1c22f 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f . { goto no_
1c230 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e mem; }../*.** An
1c231 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e ephemeral strin
1c232 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 g value (signifi
1c233 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 ed by the MEM_Ep
1c234 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 hem flag) contai
1c235 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 ns.** a pointer
1c236 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 to a dynamically
1c237 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e allocated strin
1c238 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 g where some oth
1c239 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 er entity.** is
1c23a 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
1c23b 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 deallocating tha
1c23c 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 t string. Becau
1c23d 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a se the register.
1c23e 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 ** does not cont
1c23f 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 rol the string,
1c240 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 it might be dele
1c241 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 ted without the
1c242 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 register.** know
1c243 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ing it..**.** Th
1c244 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 is routine conve
1c245 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c rts an ephemeral
1c246 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 string into a d
1c247 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
1c248 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 ated.** string t
1c249 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 hat the register
1c24a 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 itself controls
1c24b 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
1c24c 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 s, it.** convert
1c24d 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 s an MEM_Ephem s
1c24e 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 tring into an ME
1c24f 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f M_Dyn string..*/
1c250 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d .#define Deephem
1c251 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 eralize(P) \.
1c252 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 if( ((P)->flags&
1c253 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a MEM_Ephem)!=0 \.
1c254 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 && sqlite
1c255 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
1c256 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f eable(P) ){ goto
1c257 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a no_mem;}../*.**
1c258 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 Call sqlite3Vdb
1c259 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 eMemExpandBlob()
1c25a 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 on the supplied
1c25b 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d value (type Mem
1c25c 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 *).** P if requi
1c25d 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 red..*/.#define
1c25e 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 ExpandBlob(P) ((
1c25f 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a (P)->flags&MEM_Z
1c260 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 ero)?sqlite3Vdbe
1c261 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 MemExpandBlob(P)
1c262 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d :0)../*.** Argum
1c263 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 ent pMem points
1c264 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 at a register th
1c265 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 at will be passe
1c266 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 d to a.** user-d
1c267 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
1c268 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 or returned to t
1c269 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 he user as the r
1c26a 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 esult of a query
1c26b 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
1c26c 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d e sets the pMem-
1c26d 3e 74 79 70 65 20 76 61 72 69 61 62 6c 65 20 75 >type variable u
1c26e 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 sed by the sqlit
1c26f 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a e3_value_*() .**
1c270 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 routines..*/.SQ
1c271 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1c272 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
1c273 53 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 StoreType(Mem *p
1c274 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 Mem){. int flag
1c275 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b s = pMem->flags;
1c276 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d . if( flags & M
1c277 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 EM_Null ){. p
1c278 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1c279 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 TE_NULL;. }. e
1c27a 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
1c27b 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 MEM_Int ){. p
1c27c 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1c27d 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a TE_INTEGER;. }.
1c27e 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 else if( flags
1c27f 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 & MEM_Real ){.
1c280 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1c281 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 SQLITE_FLOAT;.
1c282 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 }. else if( fla
1c283 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a gs & MEM_Str ){.
1c284 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1c285 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 SQLITE_TEXT;.
1c286 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d }else{. pMem-
1c287 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 >type = SQLITE_B
1c288 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a LOB;. }.}../*.*
1c289 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 * Allocate VdbeC
1c28a 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 ursor number iCu
1c28b 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 r. Return a poi
1c28c 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 nter to it. Ret
1c28d 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 urn NULL.** if w
1c28e 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d e run out of mem
1c28f 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 ory..*/.static V
1c290 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 dbeCursor *alloc
1c291 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 ateCursor(. Vdb
1c292 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 e *p,
1c293 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
1c294 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 l machine */. i
1c295 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 nt iCur,
1c296 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1c297 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 the new VdbeCur
1c298 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 sor */. int nFi
1c299 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f eld, /
1c29a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * Number of fiel
1c29b 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ds in the table
1c29c 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e or index */. in
1c29d 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 t iDb,
1c29e 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 /* When data
1c29f 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 base the cursor
1c2a0 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d belongs to, or -
1c2a1 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 1 */. int isBtr
1c2a2 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 eeCursor /*
1c2a3 54 72 75 65 20 66 6f 72 20 42 2d 54 72 65 65 2e True for B-Tree.
1c2a4 20 20 46 61 6c 73 65 20 66 6f 72 20 70 73 65 75 False for pseu
1c2a5 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 74 61 62 do-table or vtab
1c2a6 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 */.){. /* Find
1c2a7 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1c2a8 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 that will be us
1c2a9 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ed to store the
1c2aa 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 blob of memory.
1c2ab 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72 ** required for
1c2ac 20 74 68 69 73 20 56 64 62 65 43 75 72 73 6f 72 this VdbeCursor
1c2ad 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69 structure. It i
1c2ae 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 s convenient to
1c2af 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 use a . ** vdbe
1c2b0 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 memory cell to
1c2b1 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 manage the memor
1c2b2 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
1c2b3 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a uired for a. **
1c2b4 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75 VdbeCursor stru
1c2b5 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 6f cture for the fo
1c2b6 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a llowing reasons:
1c2b7 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 . **. ** * S
1c2b8 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 20 ometimes cursor
1c2b9 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 numbers are used
1c2ba 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 for a couple of
1c2bb 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 different. **
1c2bc 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e 20 purposes in
1c2bd 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 a vdbe program.
1c2be 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 73 The different us
1c2bf 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 65 es might require
1c2c0 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 72 . ** differ
1c2c1 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 ent sized alloca
1c2c2 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 tions. Memory ce
1c2c3 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f 77 lls provide grow
1c2c4 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c able. ** al
1c2c5 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a locations.. **.
1c2c6 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 ** * When us
1c2c7 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ing ENABLE_MEMOR
1c2c8 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 Y_MANAGEMENT, me
1c2c9 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72 mory cell buffer
1c2ca 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 s can. ** b
1c2cb 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 e freed lazily v
1c2cc 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 ia the sqlite3_r
1c2cd 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
1c2ce 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 API. This. **
1c2cf 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 minimizes the
1c2d0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f number of mallo
1c2d1 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 c calls made by
1c2d2 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a the system.. **
1c2d3 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c . ** Memory cel
1c2d4 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 61 ls for cursors a
1c2d5 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 re allocated at
1c2d6 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 61 the top of the a
1c2d7 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 ddress. ** spac
1c2d8 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 e. Memory cell (
1c2d9 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 p->nMem) corresp
1c2da 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 30 onds to cursor 0
1c2db 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a . Space for. **
1c2dc 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 6e cursor 1 is man
1c2dd 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 aged by memory c
1c2de 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c ell (p->nMem-1),
1c2df 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d etc.. */. Mem
1c2e0 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 *pMem = &p->aMe
1c2e1 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b m[p->nMem-iCur];
1c2e2 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 .. int nByte;.
1c2e3 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1c2e4 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 = 0;. nByte =
1c2e5 0a 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69 . ROUND8(si
1c2e6 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 zeof(VdbeCursor)
1c2e7 29 20 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 ) + . (isBt
1c2e8 72 65 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 reeCursor?sqlite
1c2e9 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
1c2ea 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 ():0) + . 2
1c2eb 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 *nField*sizeof(u
1c2ec 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 32);.. assert(
1c2ed 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 iCur<p->nCursor
1c2ee 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 );. if( p->apCs
1c2ef 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 r[iCur] ){. s
1c2f0 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
1c2f1 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 rsor(p, p->apCsr
1c2f2 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e [iCur]);. p->
1c2f3 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b apCsr[iCur] = 0;
1c2f4 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 . }. if( SQLIT
1c2f5 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 E_OK==sqlite3Vdb
1c2f6 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
1c2f7 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 Byte, 0) ){.
1c2f8 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d p->apCsr[iCur] =
1c2f9 20 70 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 pCx = (VdbeCurs
1c2fa 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 or*)pMem->z;.
1c2fb 20 6d 65 6d 73 65 74 28 70 43 78 2c 20 30 2c 20 memset(pCx, 0,
1c2fc 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f sizeof(VdbeCurso
1c2fd 72 29 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 r));. pCx->iD
1c2fe 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 b = iDb;. pCx
1c2ff 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c ->nField = nFiel
1c300 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c d;. if( nFiel
1c301 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e d ){. pCx->
1c302 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 aType = (u32 *)&
1c303 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 pMem->z[ROUND8(s
1c304 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
1c305 29 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ))];. }. i
1c306 66 28 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 f( isBtreeCursor
1c307 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 ){. pCx->p
1c308 43 75 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73 Cursor = (BtCurs
1c309 6f 72 2a 29 0a 20 20 20 20 20 20 20 20 20 20 26 or*). &
1c30a 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 pMem->z[ROUND8(s
1c30b 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
1c30c 29 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 ))+2*nField*size
1c30d 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 20 20 of(u32)];.
1c30e 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1c30f 6f 72 5a 65 72 6f 28 70 43 78 2d 3e 70 43 75 72 orZero(pCx->pCur
1c310 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a sor);. }. }.
1c311 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a return pCx;.}.
1c312 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f ./*.** Try to co
1c313 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e nvert a value in
1c314 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 to a numeric rep
1c315 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 resentation if w
1c316 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 e can.** do so w
1c317 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 ithout loss of i
1c318 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 nformation. In
1c319 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
1c31a 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f the string.** lo
1c31b 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 oks like a numbe
1c31c 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e r, convert it in
1c31d 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 to a number. If
1c31e 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 it does not.**
1c31f 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 look like a numb
1c320 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f er, leave it alo
1c321 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ne..*/.static vo
1c322 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 id applyNumericA
1c323 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 ffinity(Mem *pRe
1c324 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d c){. if( (pRec-
1c325 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 >flags & (MEM_Re
1c326 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 al|MEM_Int))==0
1c327 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e ){. int realn
1c328 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 um;. sqlite3V
1c329 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
1c32a 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 te(pRec);. if
1c32b 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d ( (pRec->flags&M
1c32c 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20 EM_Str).
1c32d 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d && sqlite3IsNum
1c32e 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 ber(pRec->z, &re
1c32f 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 alnum, pRec->enc
1c330 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 ) ){. i64 v
1c331 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 alue;. sqli
1c332 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1c333 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 oding(pRec, SQLI
1c334 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 TE_UTF8);.
1c335 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 if( !realnum &&
1c336 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52 sqlite3Atoi64(pR
1c337 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 ec->z, &value) )
1c338 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e {. pRec->
1c339 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 u.i = value;.
1c33a 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 MemSetTypeF
1c33b 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e lag(pRec, MEM_In
1c33c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b t);. }else{
1c33d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c33e 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 VdbeMemRealify(p
1c33f 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Rec);. }.
1c340 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1c341 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 Processing is d
1c342 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 etermine by the
1c343 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 affinity paramet
1c344 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 er:.**.** SQLITE
1c345 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a _AFF_INTEGER:.**
1c346 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
1c347 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f :.** SQLITE_AFF_
1c348 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 NUMERIC:.** T
1c349 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 ry to convert pR
1c34a 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 ec to an integer
1c34b 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1c34c 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 or a .** floa
1c34d 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 ting-point repre
1c34e 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 sentation if an
1c34f 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e integer represen
1c350 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 tation.** is
1c351 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e not possible. N
1c352 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 ote that the int
1c353 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 eger representat
1c354 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 ion is.** alw
1c355 61 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 ays preferred, e
1c356 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e ven if the affin
1c357 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 ity is REAL, bec
1c358 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e ause.** an in
1c359 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 teger representa
1c35a 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 tion is more spa
1c35b 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 ce efficient on
1c35c 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 disk..**.** SQLI
1c35d 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 TE_AFF_TEXT:.**
1c35e 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 Convert pRec
1c35f 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73 to a text repres
1c360 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 entation..**.**
1c361 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a SQLITE_AFF_NONE:
1c362 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 .** No-op. p
1c363 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 Rec is unchanged
1c364 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1c365 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a applyAffinity(.
1c366 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 Mem *pRec,
1c367 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c /* The val
1c368 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 ue to apply affi
1c369 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 nity to */. cha
1c36a 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 r affinity,
1c36b 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
1c36c 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a to be applied *
1c36d 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 20 20 /. u8 enc
1c36e 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 /* Use t
1c36f 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e his text encodin
1c370 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 g */.){. if( af
1c371 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1c372 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f FF_TEXT ){. /
1c373 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 * Only attempt t
1c374 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f he conversion to
1c375 20 54 45 58 54 20 69 66 20 74 68 65 72 65 20 69 TEXT if there i
1c376 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 s an integer or
1c377 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 real. ** repr
1c378 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 esentation (blob
1c379 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 and NULL do not
1c37a 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20 get converted)
1c37b 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 but no string.
1c37c 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 ** representat
1c37d 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
1c37e 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c if( 0==(pRec->fl
1c37f 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 ags&MEM_Str) &&
1c380 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 (pRec->flags&(ME
1c381 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 M_Real|MEM_Int))
1c382 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1c383 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 3VdbeMemStringif
1c384 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 y(pRec, enc);.
1c385 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c }. pRec->fl
1c386 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 ags &= ~(MEM_Rea
1c387 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 l|MEM_Int);. }e
1c388 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 lse if( affinity
1c389 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e !=SQLITE_AFF_NON
1c38a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 E ){. assert(
1c38b 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1c38c 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c E_AFF_INTEGER ||
1c38d 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1c38e 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 E_AFF_REAL.
1c38f 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e || affin
1c390 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
1c391 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61 NUMERIC );. a
1c392 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1c393 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69 ity(pRec);. i
1c394 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 f( pRec->flags &
1c395 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 MEM_Real ){.
1c396 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e sqlite3VdbeIn
1c397 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 52 tegerAffinity(pR
1c398 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ec);. }. }.}
1c399 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 ../*.** Try to c
1c39a 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 65 20 onvert the type
1c39b 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 of a function ar
1c39c 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75 gument or a resu
1c39d 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 lt column.** int
1c39e 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 o a numeric repr
1c39f 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 esentation. Use
1c3a0 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 52 20 either INTEGER
1c3a1 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 76 65 or REAL whicheve
1c3a2 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69 r.** is appropri
1c3a3 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 ate. But only d
1c3a4 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e o the conversion
1c3a5 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
1c3a6 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f le without.** lo
1c3a7 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f ss of informatio
1c3a8 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 n and return the
1c3a9 20 72 65 76 69 73 65 64 20 74 79 70 65 20 6f 66 revised type of
1c3aa 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a the argument..*
1c3ab 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 *.** This is an
1c3ac 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 EXPERIMENTAL api
1c3ad 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 and is subject
1c3ae 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d to change or rem
1c3af 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oval..*/.SQLITE_
1c3b0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1c3b1 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 value_numeric_ty
1c3b2 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 pe(sqlite3_value
1c3b3 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a *pVal){. Mem *
1c3b4 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 pMem = (Mem*)pVa
1c3b5 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 l;. applyNumeri
1c3b6 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b cAffinity(pMem);
1c3b7 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1c3b8 6d 53 74 6f 72 65 54 79 70 65 28 70 4d 65 6d 29 mStoreType(pMem)
1c3b9 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d ;. return pMem-
1c3ba 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 >type;.}../*.**
1c3bb 45 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e Exported version
1c3bc 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 of applyAffinit
1c3bd 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f y(). This one wo
1c3be 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 rks on sqlite3_v
1c3bf 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 alue*, .** not t
1c3c0 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a he internal Mem*
1c3c1 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 type..*/.SQLITE
1c3c2 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1c3c3 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 lite3ValueApplyA
1c3c4 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 ffinity(. sqlit
1c3c5 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 e3_value *pVal,
1c3c6 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 . u8 affinity,
1c3c7 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 . u8 enc.){. a
1c3c8 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 pplyAffinity((Me
1c3c9 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 m *)pVal, affini
1c3ca 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 ty, enc);.}..#if
1c3cb 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1c3cc 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e ./*.** Write a n
1c3cd 69 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 ice string repre
1c3ce 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
1c3cf 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c contents of cel
1c3d0 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 l pMem.** into b
1c3d1 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 uffer zBuf, leng
1c3d2 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 th nBuf..*/.SQLI
1c3d3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1c3d4 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 sqlite3VdbeMemPr
1c3d5 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 ettyPrint(Mem *p
1c3d6 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Mem, char *zBuf)
1c3d7 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d {. char *zCsr =
1c3d8 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d zBuf;. int f =
1c3d9 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 pMem->flags;..
1c3da 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 static const ch
1c3db 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d ar *const encnam
1c3dc 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 es[] = {"(X)", "
1c3dd 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 (8)", "(16LE)",
1c3de 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 "(16BE)"};.. if
1c3df 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a ( f&MEM_Blob ){.
1c3e0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 int i;. c
1c3e1 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 har c;. if( f
1c3e2 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 & MEM_Dyn ){.
1c3e3 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 c = 'z';.
1c3e4 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c3e5 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
1c3e6 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 Ephem))==0 );.
1c3e7 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 }else if( f &
1c3e8 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 MEM_Static ){.
1c3e9 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 c = 't';.
1c3ea 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c3eb 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 (MEM_Dyn|MEM_Eph
1c3ec 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d em))==0 );. }
1c3ed 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d else if( f & MEM
1c3ee 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 _Ephem ){.
1c3ef 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 c = 'e';. a
1c3f0 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d ssert( (f & (MEM
1c3f1 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 _Static|MEM_Dyn)
1c3f2 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 )==0 );. }els
1c3f3 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 e{. c = 's'
1c3f4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1c3f5 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 ite3_snprintf(10
1c3f6 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 0, zCsr, "%c", c
1c3f7 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 );. zCsr += s
1c3f8 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
1c3f9 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Csr);. sqlite
1c3fa 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c3fb 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 zCsr, "%d[", pMe
1c3fc 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 m->n);. zCsr
1c3fd 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c3fe 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 30(zCsr);. fo
1c3ff 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 r(i=0; i<16 && i
1c400 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a <pMem->n; i++){.
1c401 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1c402 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1c403 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 , "%02X", ((int)
1c404 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 pMem->z[i] & 0xF
1c405 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 F));. zCsr
1c406 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c407 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 30(zCsr);. }.
1c408 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 for(i=0; i<1
1c409 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 6 && i<pMem->n;
1c40a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 i++){. char
1c40b 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b z = pMem->z[i];
1c40c 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 . if( z<32
1c40d 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 || z>126 ) *zCsr
1c40e 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 ++ = '.';.
1c40f 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a else *zCsr++ = z
1c410 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1c411 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 ite3_snprintf(10
1c412 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 0, zCsr, "]%s",
1c413 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 encnames[pMem->e
1c414 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b nc]);. zCsr +
1c415 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1c416 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 0(zCsr);. if(
1c417 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b f & MEM_Zero ){
1c418 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1c419 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 nprintf(100, zCs
1c41a 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 r,"+%dz",pMem->u
1c41b 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a .nZero);. z
1c41c 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 Csr += sqlite3St
1c41d 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 rlen30(zCsr);.
1c41e 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 }. *zCsr =
1c41f 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 '\0';. }else if
1c420 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b ( f & MEM_Str ){
1c421 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 . int j, k;.
1c422 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 zBuf[0] = ' '
1c423 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 ;. if( f & ME
1c424 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a M_Dyn ){. z
1c425 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 Buf[1] = 'z';.
1c426 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 assert( (f &
1c427 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d (MEM_Static|MEM
1c428 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 _Ephem))==0 );.
1c429 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 }else if( f &
1c42a 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 MEM_Static ){.
1c42b 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 zBuf[1] = '
1c42c 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 t';. assert
1c42d 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c ( (f & (MEM_Dyn|
1c42e 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 MEM_Ephem))==0 )
1c42f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1c430 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b f & MEM_Ephem ){
1c431 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d . zBuf[1] =
1c432 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 'e';. asse
1c433 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 rt( (f & (MEM_St
1c434 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d atic|MEM_Dyn))==
1c435 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0 );. }else{.
1c436 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 zBuf[1] =
1c437 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 's';. }. k
1c438 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 2;. sqlite
1c439 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c43a 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 &zBuf[k], "%d",
1c43b 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 pMem->n);. k
1c43c 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c43d 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 30(&zBuf[k]);.
1c43e 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b zBuf[k++] = '[
1c43f 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 ';. for(j=0;
1c440 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e j<15 && j<pMem->
1c441 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 n; j++){. u
1c442 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 8 c = pMem->z[j]
1c443 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 ;. if( c>=0
1c444 78 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b x20 && c<0x7f ){
1c445 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b . zBuf[k+
1c446 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 +] = c;. }e
1c447 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 lse{. zBu
1c448 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 f[k++] = '.';.
1c449 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1c44a 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b zBuf[k++] = ']';
1c44b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1c44c 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b rintf(100,&zBuf[
1c44d 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 k], encnames[pMe
1c44e 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 m->enc]);. k
1c44f 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c450 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 30(&zBuf[k]);.
1c451 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b zBuf[k++] = 0;
1c452 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 . }.}.#endif..#
1c453 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1c454 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 UG./*.** Print t
1c455 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 he value of a re
1c456 67 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 gister for traci
1c457 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a ng purposes:.*/.
1c458 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 static void memT
1c459 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a racePrint(FILE *
1c45a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 out, Mem *p){.
1c45b 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d if( p->flags & M
1c45c 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 EM_Null ){. f
1c45d 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 printf(out, " NU
1c45e 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 LL");. }else if
1c45f 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d ( (p->flags & (M
1c460 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 EM_Int|MEM_Str))
1c461 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 ==(MEM_Int|MEM_S
1c462 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e tr) ){. fprin
1c463 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c tf(out, " si:%ll
1c464 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d d", p->u.i);. }
1c465 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
1c466 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
1c467 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1c468 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e " i:%lld", p->u.
1c469 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 i);.#ifndef SQLI
1c46a 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
1c46b 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69 _POINT. }else i
1c46c 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 f( p->flags & ME
1c46d 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 M_Real ){. fp
1c46e 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 rintf(out, " r:%
1c46f 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69 g", p->r);.#endi
1c470 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d f. }else if( p-
1c471 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1c472 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e Set ){. fprin
1c473 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65 tf(out, " (rowse
1c474 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 t)");. }else{.
1c475 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30 char zBuf[200
1c476 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
1c477 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
1c478 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 (p, zBuf);. f
1c479 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 printf(out, " ")
1c47a 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
1c47b 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a t, "%s", zBuf);.
1c47c 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
1c47d 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 d registerTrace(
1c47e 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 FILE *out, int i
1c47f 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 Reg, Mem *p){.
1c480 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 fprintf(out, "RE
1c481 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 G[%d] = ", iReg)
1c482 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e ;. memTracePrin
1c483 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 t(out, p);. fpr
1c484 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
1c485 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 .}.#endif..#ifde
1c486 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 f SQLITE_DEBUG.#
1c487 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 define REGISTE
1c488 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28 R_TRACE(R,M) if(
1c489 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65 p->trace)registe
1c48a 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c rTrace(p->trace,
1c48b 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 R,M).#else.# de
1c48c 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 fine REGISTER_TR
1c48d 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a ACE(R,M).#endif.
1c48e 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ..#ifdef VDBE_PR
1c48f 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 OFILE../* .** hw
1c490 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 time.h contains
1c491 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 inline assembler
1c492 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d code for implem
1c493 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d enting .** high-
1c494 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 performance timi
1c495 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a ng routines..*/.
1c496 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1c497 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 Include hwtime.h
1c498 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
1c499 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a f vdbe.c *******
1c49a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1c49b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1c49c 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d Begin file hwtim
1c49d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
1c49e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c49f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1c4a0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 /*.** 2008 May 2
1c4a1 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
1c4a2 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1c4a3 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1c4a4 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1c4a5 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1c4a6 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1c4a7 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1c4a8 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1c4a9 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1c4aa 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1c4ab 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1c4ac 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1c4ad 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1c4ae 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1c4af 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1c4b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1c4b1 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1c4b2 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1c4b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4b7 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1c4b8 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 file contains i
1c4b9 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 nline asm code f
1c4ba 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 or retrieving "h
1c4bb 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 igh-performance"
1c4bc 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 .** counters for
1c4bd 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e x86 class CPUs.
1c4be 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 .*/.#ifndef _HWT
1c4bf 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f IME_H_.#define _
1c4c0 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a HWTIME_H_../*.**
1c4c1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
1c4c2 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
1c4c3 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 s on pentium-cla
1c4c4 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 ss (or newer) pr
1c4c5 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 ocessors..** It
1c4c6 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f uses the RDTSC o
1c4c7 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 pcode to read th
1c4c8 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 e cycle count va
1c4c9 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a lue out of the.*
1c4ca 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 * processor and
1c4cb 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c returns that val
1c4cc 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ue. This can be
1c4cd 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 used for high-r
1c4ce 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e es.** profiling.
1c4cf 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 .*/.#if (defined
1c4d0 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 (__GNUC__) || de
1c4d1 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 fined(_MSC_VER))
1c4d2 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 && \. (def
1c4d3 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 ined(i386) || de
1c4d4 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 fined(__i386__)
1c4d5 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 || defined(_M_IX
1c4d6 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 86)).. #if defi
1c4d7 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 ned(__GNUC__)..
1c4d8 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
1c4d9 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
1c4da 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
1c4db 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
1c4dc 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f lo, hi;. __
1c4dd 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
1c4de 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
1c4df 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 a" (lo), "=d" (h
1c4e0 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e i));. return
1c4e1 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 (sqlite_uint64)
1c4e2 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 hi << 32 | lo;.
1c4e3 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 }.. #elif defi
1c4e4 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 ned(_MSC_VER)..
1c4e5 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 __declspec(nake
1c4e6 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 d) __inline sqli
1c4e7 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 te_uint64 __cdec
1c4e8 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 l sqlite3Hwtime(
1c4e9 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 void){. __as
1c4ea 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 m {. rdts
1c4eb 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 c. ret
1c4ec 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c ; return val
1c4ed 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 ue at EDX:EAX.
1c4ee 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 }. }.. #end
1c4ef 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e if..#elif (defin
1c4f0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
1c4f1 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 defined(__x86_64
1c4f2 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
1c4f3 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
1c4f4 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
1c4f5 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
1c4f6 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 gned long val;.
1c4f7 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
1c4f8 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
1c4f9 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 c" : "=A" (val))
1c4fa 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 ;. return v
1c4fb 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 al;. }. .#elif
1c4fc 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
1c4fd 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f _) && defined(__
1c4fe 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c ppc__)).. __inl
1c4ff 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
1c500 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
1c501 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
1c502 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
1c503 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 g retval;.
1c504 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 unsigned long ju
1c505 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f nk;. __asm_
1c506 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
1c507 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 "\n\. 1
1c508 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 : mftbu %
1c509 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
1c50a 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 mftb %
1c50b 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 L0\n\.
1c50c 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 mftbu
1c50d 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %0\n\.
1c50e 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 cmpw
1c50f 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %0,%1\n\.
1c510 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 bne
1c511 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 1b".
1c512 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 : "=r"
1c513 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 (retval), "=r" (
1c514 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 junk));. re
1c515 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d turn retval;. }
1c516 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f ..#else.. #erro
1c517 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 r Need implement
1c518 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 ation of sqlite3
1c519 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 Hwtime() for you
1c51a 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f r platform... /
1c51b 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c *. ** To compil
1c51c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d e without implem
1c51d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 enting sqlite3Hw
1c51e 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
1c51f 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 platform,. ** y
1c520 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 ou can remove th
1c521 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 e above #error a
1c522 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f nd use the follo
1c523 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 wing. ** stub f
1c524 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 unction. You wi
1c525 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 ll lose timing s
1c526 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a upport for many.
1c527 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 ** of the debu
1c528 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e gging and testin
1c529 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 g utilities, but
1c52a 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 it should at.
1c52b 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 ** least compile
1c52c 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 and run.. */.S
1c52d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
1c52e 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
1c52f 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
1c530 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 ){ return ((sqli
1c531 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a te_uint64)0); }.
1c532 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
1c533 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 /* !defined(_HWT
1c534 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a IME_H_) */../***
1c535 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1c536 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a of hwtime.h ****
1c537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1c53a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
1c53b 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
1c53c 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 left off in vdbe
1c53d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1c53e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e **********/..#en
1c53f 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 dif../*.** The C
1c540 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 HECK_FOR_INTERRU
1c541 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 PT macro defined
1c542 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 here looks to s
1c543 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c ee if the.** sql
1c544 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
1c545 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 routine has bee
1c546 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 n called. If it
1c547 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a has been, then.
1c548 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 ** processing of
1c549 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 the VDBE progra
1c54a 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 m is interrupted
1c54b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 ..**.** This mac
1c54c 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 ro added to ever
1c54d 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 y instruction th
1c54e 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 at does a jump i
1c54f 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d n order to.** im
1c550 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 plement a loop.
1c551 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20 This test used
1c552 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 to be on every s
1c553 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f ingle instructio
1c554 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d n,.** but that m
1c555 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 eant we more tes
1c556 74 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 ting that we nee
1c557 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 ded. By only te
1c558 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 sting the.** fla
1c559 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 g on jump instru
1c55a 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 ctions, we get a
1c55b 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 (small) speed i
1c55c 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 mprovement..*/.#
1c55d 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 define CHECK_FOR
1c55e 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 _INTERRUPT \.
1c55f 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 if( db->u1.isInt
1c560 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 errupted ) goto
1c561 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 abort_due_to_int
1c562 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 errupt;..#ifdef
1c563 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 SQLITE_DEBUG.sta
1c564 74 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 tic int fileExis
1c565 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ts(sqlite3 *db,
1c566 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
1c567 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 e){. int res =
1c568 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 0;. int rc = SQ
1c569 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 LITE_OK;.#ifdef
1c56a 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a SQLITE_TEST. /*
1c56b 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72 65 If we are curre
1c56c 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 ntly testing IO
1c56d 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 errors, then do
1c56e 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 not call OsAcces
1c56f 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 s() to. ** test
1c570 20 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 for the presenc
1c571 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 e of zFile. This
1c572 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 is because any
1c573 49 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 IO error that.
1c574 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 ** occurs here w
1c575 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 ill not be repor
1c576 74 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 ted, causing the
1c577 20 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 test to fail..
1c578 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 */. extern int
1c579 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
1c57a 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 r_pending;. if(
1c57b 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
1c57c 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 r_pending<=0 ).#
1c57d 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 endif. rc = s
1c57e 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 qlite3OsAccess(d
1c57f 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 b->pVfs, zFile,
1c580 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
1c581 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 ISTS, &res);. r
1c582 65 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 eturn (res && rc
1c583 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a ==SQLITE_OK);.}.
1c584 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1c585 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 NDEBUG./*.** Thi
1c586 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e s function is on
1c587 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 ly called from w
1c588 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28 ithin an assert(
1c589 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 ) expression. It
1c58a 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20 .** checks that
1c58b 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 the sqlite3.nTra
1c58c 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c nsaction variabl
1c58d 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73 e is correctly s
1c58e 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d et to.** the num
1c58f 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 ber of non-trans
1c590 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
1c591 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 s currently in t
1c592 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 he .** linked li
1c593 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 73 st starting at s
1c594 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e qlite3.pSavepoin
1c595 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a t..** .** Usage:
1c596 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 .**.** asser
1c597 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e t( checkSavepoin
1c598 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f tCount(db) );.*/
1c599 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 .static int chec
1c59a 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 kSavepointCount(
1c59b 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
1c59c 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 int n = 0;. Sav
1c59d 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 epoint *p;. for
1c59e 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e (p=db->pSavepoin
1c59f 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 t; p; p=p->pNext
1c5a0 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 ) n++;. assert(
1c5a1 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f n==(db->nSavepo
1c5a2 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e int + db->isTran
1c5a3 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 sactionSavepoint
1c5a4 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b ) );. return 1;
1c5a5 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1c5a6 20 45 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 Execute as much
1c5a7 20 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 of a VDBE progr
1c5a8 61 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 am as we can the
1c5a9 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 n return..**.**
1c5aa 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 sqlite3VdbeMakeR
1c5ab 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20 63 eady() must be c
1c5ac 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69 alled before thi
1c5ad 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 s routine in ord
1c5ae 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 er to.** close t
1c5af 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20 he program with
1c5b0 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 a final OP_Halt
1c5b1 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74 68 and to set up th
1c5b2 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 e callbacks.** a
1c5b3 6e 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 nd the error mes
1c5b4 73 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a sage pointer..**
1c5b5 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72 .** Whenever a r
1c5b6 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74 ow or result dat
1c5b7 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 a is available,
1c5b8 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
1c5b9 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f l either.** invo
1c5ba 6b 65 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 ke the result ca
1c5bb 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 llback (if there
1c5bc 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 is one) or retu
1c5bd 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 rn with.** SQLIT
1c5be 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 E_ROW..**.** If
1c5bf 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 an attempt is ma
1c5c0 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 de to open a loc
1c5c1 6b 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 ked database, th
1c5c2 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
1c5c3 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69 ** will either i
1c5c4 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 nvoke the busy c
1c5c5 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 allback (if ther
1c5c6 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 e is one) or it
1c5c7 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 will.** return S
1c5c8 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a QLITE_BUSY..**.*
1c5c9 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
1c5ca 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d curs, an error m
1c5cb 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65 essage is writte
1c5cc 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 n to memory obta
1c5cd 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c ined.** from sql
1c5ce 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e ite3_malloc() an
1c5cf 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 d p->zErrMsg is
1c5d0 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f made to point to
1c5d1 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a that memory..**
1c5d2 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 The error code
1c5d3 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e is stored in p->
1c5d4 72 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 rc and this rout
1c5d5 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ine returns SQLI
1c5d6 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 TE_ERROR..**.**
1c5d7 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 If the callback
1c5d8 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e ever returns non
1c5d9 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
1c5da 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a program exits.**
1c5db 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 immediately. T
1c5dc 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 here will be no
1c5dd 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75 error message bu
1c5de 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c t the p->rc fiel
1c5df 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 d is.** set to S
1c5e0 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 QLITE_ABORT and
1c5e1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
1c5e2 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f l return SQLITE_
1c5e3 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d ERROR..**.** A m
1c5e4 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
1c5e5 20 65 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d error causes p-
1c5e6 3e 72 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f >rc to be set to
1c5e7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e SQLITE_NOMEM an
1c5e8 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e d this.** routin
1c5e9 65 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 e to return SQLI
1c5ea 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 TE_ERROR..**.**
1c5eb 4f 74 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f Other fatal erro
1c5ec 72 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 rs return SQLITE
1c5ed 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 _ERROR..**.** Af
1c5ee 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
1c5ef 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73 has finished, s
1c5f0 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 qlite3VdbeFinali
1c5f1 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a ze() should be.*
1c5f2 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 * used to clean
1c5f3 75 70 20 74 68 65 20 6d 65 73 73 20 74 68 61 74 up the mess that
1c5f4 20 77 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64 was left behind
1c5f5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1c5f6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1c5f7 64 62 65 45 78 65 63 28 0a 20 20 56 64 62 65 20 dbeExec(. Vdbe
1c5f8 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *p
1c5f9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
1c5fa 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 E */.){. int pc
1c5fb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c5fc 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 /* The prog
1c5fd 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 ram counter */.
1c5fe 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f Op *aOp = p->aO
1c5ff 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 p; /* C
1c600 6f 70 79 20 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f opy of p->aOp */
1c601 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20 . Op *pOp;
1c602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c603 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 Current operati
1c604 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d on */. int rc =
1c605 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
1c606 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
1c607 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 eturn */. sqlit
1c608 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 e3 *db = p->db;
1c609 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
1c60a 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 abase */. u8 re
1c60b 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 setSchemaOnFault
1c60c 20 3d 20 30 3b 20 2f 2a 20 52 65 73 65 74 20 73 = 0; /* Reset s
1c60d 63 68 65 6d 61 20 61 66 74 65 72 20 61 6e 20 65 chema after an e
1c60e 72 72 6f 72 20 69 66 20 74 72 75 65 20 2a 2f 0a rror if true */.
1c60f 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20 u8 encoding =
1c610 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20 ENC(db); /*
1c611 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 The database enc
1c612 6f 64 69 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66 oding */.#ifndef
1c613 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f SQLITE_OMIT_PRO
1c614 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 GRESS_CALLBACK.
1c615 20 69 6e 74 20 63 68 65 63 6b 50 72 6f 67 72 65 int checkProgre
1c616 73 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ss; /* T
1c617 72 75 65 20 69 66 20 70 72 6f 67 72 65 73 73 20 rue if progress
1c618 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 65 6e callbacks are en
1c619 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e abled */. int n
1c61a 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b ProgressOps = 0;
1c61b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 /* Opcodes
1c61c 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 executed since
1c61d 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
1c61e 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d k. */.#endif. M
1c61f 65 6d 20 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d em *aMem = p->aM
1c620 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 em; /* Cop
1c621 79 20 6f 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a y of p->aMem */.
1c622 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b Mem *pIn1 = 0;
1c623 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c624 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 1st input operan
1c625 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 d */. Mem *pIn2
1c626 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1c627 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f /* 2nd input o
1c628 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 perand */. Mem
1c629 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 *pIn3 = 0;
1c62a 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e /* 3rd in
1c62b 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 put operand */.
1c62c 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 Mem *pOut = 0;
1c62d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1c62e 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f utput operand */
1c62f 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 . int iCompare
1c630 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
1c631 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 Result of last
1c632 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 OP_Compare opera
1c633 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 tion */. int *a
1c634 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 Permute = 0;
1c635 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 /* Permutat
1c636 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 ion of columns f
1c637 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f or OP_Compare */
1c638 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
1c639 46 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 FILE. u64 start
1c63a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c63b 20 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 /* CPU clock c
1c63c 6f 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 ount at start of
1c63d 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 opcode */. int
1c63e 20 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 origPc;
1c63f 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 /* Progr
1c640 61 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 am counter at st
1c641 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f art of opcode */
1c642 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a .#endif. /*****
1c643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1c647 20 20 2a 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c ** Automatical
1c648 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 ly generated cod
1c649 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 e. **. ** The
1c64a 66 6f 6c 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 following union
1c64b 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 is automatically
1c64c 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 generated by th
1c64d 65 0a 20 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 e. ** vdbe-comp
1c64e 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 74 2e ress.tcl script.
1c64f 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 The purpose of
1c650 20 74 68 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 this union is t
1c651 6f 0a 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 o. ** reduce th
1c652 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 e amount of stac
1c653 6b 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 k space required
1c654 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1c655 6e 2e 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d n.. ** See comm
1c656 65 6e 74 73 20 69 6e 20 74 68 65 20 76 64 62 65 ents in the vdbe
1c657 2d 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 -compress.tcl sc
1c658 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 ript for details
1c659 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 .. */. union v
1c65a 64 62 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 dbeExecUnion {.
1c65b 20 20 20 73 74 72 75 63 74 20 4f 50 5f 59 69 65 struct OP_Yie
1c65c 6c 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ld_stack_vars {.
1c65d 20 20 20 20 20 20 69 6e 74 20 70 63 44 65 73 74 int pcDest
1c65e 3b 0a 20 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 ;. } aa;.
1c65f 73 74 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62 struct OP_Variab
1c660 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a le_stack_vars {.
1c661 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 int p1;
1c662 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 /* Variab
1c663 6c 65 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 le to copy from
1c664 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b */. int p2;
1c665 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c666 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f ister to copy to
1c667 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b */. int n;
1c668 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c669 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c mber of values l
1c66a 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
1c66b 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 Mem *pVar;
1c66c 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 /* Value b
1c66d 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64 eing transferred
1c66e 20 2a 2f 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20 */. } ab;.
1c66f 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65 struct OP_Move
1c670 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c671 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f char *zMallo
1c672 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 c; /* Holding
1c673 76 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c variable for all
1c674 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f ocated memory */
1c675 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 . int n;
1c676 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1c677 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6c r of registers l
1c678 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
1c679 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 int p1;
1c67a 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1c67b 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a r to copy from *
1c67c 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 /. int p2;
1c67d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 /* Regi
1c67e 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 ster to copy to
1c67f 2a 2f 0a 20 20 20 20 7d 20 61 63 3b 0a 20 20 20 */. } ac;.
1c680 20 73 74 72 75 63 74 20 4f 50 5f 52 65 73 75 6c struct OP_Resul
1c681 74 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 tRow_stack_vars
1c682 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 {. Mem *pMe
1c683 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a m;. int i;.
1c684 20 20 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 } ad;. st
1c685 72 75 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 ruct OP_Concat_s
1c686 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c687 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 20 i64 nByte;.
1c688 20 7d 20 61 65 3b 0a 20 20 20 20 73 74 72 75 63 } ae;. struc
1c689 74 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73 t OP_Remainder_s
1c68a 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c68b 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
1c68c 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 /* Combined ME
1c68d 4d 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 M_* flags from b
1c68e 6f 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 oth inputs */.
1c68f 20 20 20 20 69 36 34 20 69 41 3b 20 20 20 20 20 i64 iA;
1c690 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 /* Integer v
1c691 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 alue of left ope
1c692 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 rand */. i6
1c693 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 4 iB; /*
1c694 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f Integer value o
1c695 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 f right operand
1c696 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 */. double
1c697 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c rA; /* Real
1c698 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f value of left o
1c699 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 perand */.
1c69a 64 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 double rB;
1c69b 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 /* Real value of
1c69c 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a right operand *
1c69d 2f 0a 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20 /. } af;.
1c69e 73 74 72 75 63 74 20 4f 50 5f 46 75 6e 63 74 69 struct OP_Functi
1c69f 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a on_stack_vars {.
1c6a0 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
1c6a1 20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 Mem *pArg;.
1c6a2 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 sqlite3_cont
1c6a3 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73 ext ctx;. s
1c6a4 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
1c6a5 70 56 61 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 pVal;. int
1c6a6 6e 3b 0a 20 20 20 20 7d 20 61 67 3b 0a 20 20 20 n;. } ag;.
1c6a7 20 73 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74 struct OP_Shift
1c6a8 52 69 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 Right_stack_vars
1c6a9 20 7b 0a 20 20 20 20 20 20 69 36 34 20 61 3b 0a {. i64 a;.
1c6aa 20 20 20 20 20 20 69 36 34 20 62 3b 0a 20 20 20 i64 b;.
1c6ab 20 7d 20 61 68 3b 0a 20 20 20 20 73 74 72 75 63 } ah;. struc
1c6ac 74 20 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 t OP_Ge_stack_va
1c6ad 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 rs {. int r
1c6ae 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f es; /
1c6af 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 * Result of the
1c6b0 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 comparison of pI
1c6b1 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20 n1 against pIn3
1c6b2 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 */. char af
1c6b3 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 finity; /*
1c6b4 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 Affinity to use
1c6b5 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a for comparison *
1c6b6 2f 0a 20 20 20 20 7d 20 61 69 3b 0a 20 20 20 20 /. } ai;.
1c6b7 73 74 72 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72 struct OP_Compar
1c6b8 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 e_stack_vars {.
1c6b9 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
1c6ba 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 int i;. i
1c6bb 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 nt p1;. int
1c6bc 20 70 32 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 p2;. const
1c6bd 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
1c6be 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 fo;. int id
1c6bf 78 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 x;. CollSeq
1c6c0 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 *pColl; /* C
1c6c1 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1c6c2 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 e to use on this
1c6c3 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69 term */. i
1c6c4 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 nt bRev;
1c6c5 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 /* True for DE
1c6c6 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 SCENDING sort or
1c6c7 64 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b der */. } aj;
1c6c8 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f . struct OP_O
1c6c9 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 r_stack_vars {.
1c6ca 20 20 20 20 20 69 6e 74 20 76 31 3b 20 20 20 20 int v1;
1c6cb 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a /* Left operand:
1c6cc 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 0==FALSE, 1==T
1c6cd 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 RUE, 2==UNKNOWN
1c6ce 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 or NULL */.
1c6cf 20 69 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 int v2; /* R
1c6d0 69 67 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d ight operand: 0=
1c6d1 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c =FALSE, 1==TRUE,
1c6d2 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 2==UNKNOWN or N
1c6d3 55 4c 4c 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b ULL */. } ak;
1c6d4 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c6d5 66 4e 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 fNot_stack_vars
1c6d6 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 {. int c;.
1c6d7 20 20 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72 } al;. str
1c6d8 75 63 74 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 uct OP_Column_st
1c6d9 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c6da 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 u32 payloadSize
1c6db 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 ; /* Number of
1c6dc 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 bytes in the re
1c6dd 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 cord */. i6
1c6de 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 4 payloadSize64;
1c6df 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c6e0 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1c6e1 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 d */. int p
1c6e2 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1; /*
1c6e3 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 P1 value of the
1c6e4 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 opcode */.
1c6e5 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 int p2;
1c6e6 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 /* column nu
1c6e7 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 mber to retrieve
1c6e8 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 */. VdbeCu
1c6e9 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 rsor *pC; /*
1c6ea 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
1c6eb 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a */. char *z
1c6ec 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 Rec; /* P
1c6ed 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 ointer to comple
1c6ee 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a te record-data *
1c6ef 2f 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 /. BtCursor
1c6f0 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 *pCrsr; /* Th
1c6f1 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a e BTree cursor *
1c6f2 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 54 79 /. u32 *aTy
1c6f3 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 pe; /* aT
1c6f4 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 ype[i] holds the
1c6f5 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 numeric type of
1c6f6 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e the i-th column
1c6f7 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 */. u32 *a
1c6f8 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 Offset; /*
1c6f9 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 aOffset[i] is of
1c6fa 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 fset to start of
1c6fb 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 data for i-th c
1c6fc 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 olumn */. i
1c6fd 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
1c6fe 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 /* number of f
1c6ff 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 ields in the rec
1c700 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ord */. int
1c701 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 len;
1c702 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 /* The length of
1c703 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 the serialized
1c704 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c data for the col
1c705 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 umn */. int
1c706 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1c707 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1c708 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a */. char *z
1c709 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 Data; /* P
1c70a 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 art of the recor
1c70b 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 d being decoded
1c70c 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 */. Mem *pD
1c70d 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 est; /* W
1c70e 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 here to write th
1c70f 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75 e extracted valu
1c710 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73 e */. Mem s
1c711 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Mem; /*
1c712 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 For storing the
1c713 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 record being de
1c714 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 coded */. u
1c715 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 8 *zIdx;
1c716 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 /* Index into
1c717 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 header */.
1c718 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 u8 *zEndHdr;
1c719 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1c71a 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 first byte afte
1c71b 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a r the header */.
1c71c 20 20 20 20 20 20 75 33 32 20 6f 66 66 73 65 74 u32 offset
1c71d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 ; /* Offs
1c71e 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 et into the data
1c71f 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6f 66 */. u64 of
1c720 66 73 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 fset64; /*
1c721 36 34 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 64-bit offset.
1c722 36 34 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 64 bits needed t
1c723 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 o catch overflow
1c724 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a */. int sz
1c725 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Hdr; /*
1c726 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 Size of the head
1c727 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 er size field at
1c728 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 start of record
1c729 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 76 */. int av
1c72a 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ail; /*
1c72b 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1c72c 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 of available dat
1c72d 61 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a a */. Mem *
1c72e 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a pReg; /*
1c72f 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 PseudoTable inp
1c730 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 ut register */.
1c731 20 20 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72 } am;. str
1c732 75 63 74 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f uct OP_Affinity_
1c733 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c734 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c735 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 Affinity; /* T
1c736 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 he affinity to b
1c737 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20 e applied */.
1c738 20 20 20 63 68 61 72 20 63 41 66 66 3b 20 20 20 char cAff;
1c739 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
1c73a 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 single characte
1c73b 72 20 6f 66 20 61 66 66 69 6e 69 74 79 20 2a 2f r of affinity */
1c73c 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 . } an;. s
1c73d 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 truct OP_MakeRec
1c73e 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ord_stack_vars {
1c73f 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 . u8 *zNewR
1c740 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a ecord; /*
1c741 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
1c742 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 d the data for t
1c743 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
1c744 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 . Mem *pRec
1c745 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c746 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 The new record
1c747 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61 */. u64 nDa
1c748 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
1c749 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1c74a 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 es of data space
1c74b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 */. int nH
1c74c 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
1c74d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c74e 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 tes of header sp
1c74f 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 ace */. i64
1c750 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
1c751 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 /* Data spac
1c752 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 e required for t
1c753 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 his record */.
1c754 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 int nZero;
1c755 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c756 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 mber of zero byt
1c757 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 es at the end of
1c758 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1c759 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 int nVarint
1c75a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1c75b 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1c75c 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 n a varint */.
1c75d 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 u32 serial_t
1c75e 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 ype; /* Ty
1c75f 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 pe field */.
1c760 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 Mem *pData0;
1c761 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
1c762 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f t field to be co
1c763 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 mbined into the
1c764 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 record */.
1c765 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 Mem *pLast;
1c766 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 /* Last f
1c767 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f ield of the reco
1c768 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 rd */. int
1c769 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 nField;
1c76a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c76b 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 fields in the re
1c76c 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 cord */. ch
1c76d 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 ar *zAffinity;
1c76e 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 /* The affi
1c76f 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 nity string for
1c770 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1c771 20 20 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 int file_for
1c772 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 mat; /* Fi
1c773 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 le format to use
1c774 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f for encoding */
1c775 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
1c776 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c777 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a Space used in z
1c778 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 NewRecord[] */.
1c779 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 int len;
1c77a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c77b 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64 ength of a field
1c77c 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 */. } ao;.
1c77d 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e struct OP_Coun
1c77e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c77f 20 20 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b i64 nEntry;
1c780 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c781 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 *pCrsr;. } ap
1c782 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c783 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f Savepoint_stack_
1c784 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c785 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 p1;
1c786 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c787 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 Value of P1 oper
1c788 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 and */. cha
1c789 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
1c78a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c78b 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e Name of savepoin
1c78c 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e t */. int n
1c78d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65 Name;. Save
1c78e 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 point *pNew;.
1c78f 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 Savepoint *pS
1c790 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 avepoint;.
1c791 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b Savepoint *pTmp;
1c792 0a 20 20 20 20 20 20 69 6e 74 20 69 53 61 76 65 . int iSave
1c793 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 point;. int
1c794 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 ii;. } aq;.
1c795 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74 struct OP_Aut
1c796 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 oCommit_stack_va
1c797 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 rs {. int d
1c798 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1c799 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c ;. int iRol
1c79a 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 lback;. int
1c79b 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d turnOnAC;. }
1c79c 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ar;. struct
1c79d 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 OP_Transaction_s
1c79e 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c79f 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 Btree *pBt;.
1c7a0 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75 } as;. stru
1c7a1 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 ct OP_ReadCookie
1c7a2 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c7a3 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 int iMeta;.
1c7a4 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
1c7a5 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b int iCookie;
1c7a6 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 . } at;. s
1c7a7 74 72 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b truct OP_SetCook
1c7a8 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ie_stack_vars {.
1c7a9 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 Db *pDb;.
1c7aa 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 } au;. str
1c7ab 75 63 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f uct OP_VerifyCoo
1c7ac 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b kie_stack_vars {
1c7ad 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61 . int iMeta
1c7ae 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 ;. Btree *p
1c7af 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 Bt;. } av;.
1c7b0 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e struct OP_Open
1c7b1 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 Write_stack_vars
1c7b2 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 {. int nFi
1c7b3 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e eld;. KeyIn
1c7b4 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 fo *pKeyInfo;.
1c7b5 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 int p2;.
1c7b6 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1c7b7 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 int wrFlag;.
1c7b8 20 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 Btree *pX;.
1c7b9 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c7ba 70 43 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a pCur;. Db *
1c7bb 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 pDb;. } aw;.
1c7bc 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 struct OP_Ope
1c7bd 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b nEphemeral_stack
1c7be 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c7bf 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1c7c0 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 } ax;. str
1c7c1 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 uct OP_OpenPseud
1c7c2 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 o_stack_vars {.
1c7c3 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c7c4 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a *pCx;. } ay;.
1c7c5 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c7c6 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 ekGt_stack_vars
1c7c7 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b {. int res;
1c7c8 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 . int oc;.
1c7c9 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c7ca 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 *pC;. Unpac
1c7cb 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 kedRecord r;.
1c7cc 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 int nField;.
1c7cd 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 i64 iKey;
1c7ce 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 /* The rowid
1c7cf 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 we are to seek
1c7d0 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a to */. } az;.
1c7d1 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c7d2 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ek_stack_vars {.
1c7d3 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c7d4 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a *pC;. } ba;.
1c7d5 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f struct OP_Fo
1c7d6 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b und_stack_vars {
1c7d7 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61 . int alrea
1c7d8 64 79 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 dyExists;.
1c7d9 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c7da 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c7db 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 UnpackedRec
1c7dc 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 ord *pIdxKey;.
1c7dd 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
1c7de 72 64 20 72 3b 0a 20 20 20 20 20 20 63 68 61 72 rd r;. char
1c7df 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 aTempRec[ROUND8
1c7e0 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 (sizeof(Unpacked
1c7e1 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f Record)) + sizeo
1c7e2 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 f(Mem)*3 + 7];.
1c7e3 20 20 20 7d 20 62 62 3b 0a 20 20 20 20 73 74 72 } bb;. str
1c7e4 75 63 74 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f uct OP_IsUnique_
1c7e5 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c7e6 20 20 20 75 31 36 20 69 69 3b 0a 20 20 20 20 20 u16 ii;.
1c7e7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1c7e8 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c7e9 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75 *pCrsr;. u
1c7ea 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 16 nField;.
1c7eb 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20 Mem *aMx;.
1c7ec 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1c7ed 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1c7ee 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e /* B-Tree in
1c7ef 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a dex search key *
1c7f0 2f 0a 20 20 20 20 20 20 69 36 34 20 52 3b 20 20 /. i64 R;
1c7f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c7f2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
1c7f3 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 wid stored in re
1c7f4 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 gister P3 */.
1c7f5 20 7d 20 62 63 3b 0a 20 20 20 20 73 74 72 75 63 } bc;. struc
1c7f6 74 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 t OP_NotExists_s
1c7f7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c7f8 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c7f9 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c7fa 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 *pCrsr;. i
1c7fb 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36 nt res;. u6
1c7fc 34 20 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64 4 iKey;. } bd
1c7fd 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c7fe 4e 65 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 NewRowid_stack_v
1c7ff 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 ars {. i64
1c800 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
1c801 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f /* The new ro
1c802 77 69 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 wid */. Vdb
1c803 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 eCursor *pC;
1c804 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 /* Cursor of
1c805 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 table to get th
1c806 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 e new rowid */.
1c807 20 20 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 int res;
1c808 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1c809 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 esult of an sqli
1c80a 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a te3BtreeLast() *
1c80b 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b /. int cnt;
1c80c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c80d 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d * Counter to lim
1c80e 69 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 it the number of
1c80f 20 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 searches */.
1c810 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 Mem *pMem;
1c811 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c812 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 ister holding la
1c813 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20 rgest rowid for
1c814 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f AUTOINCREMENT */
1c815 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 . VdbeFrame
1c816 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a *pFrame; /*
1c817 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 Root frame of V
1c818 44 42 45 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b DBE */. } be;
1c819 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c81a 6e 73 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 nsertInt_stack_v
1c81b 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 ars {. Mem
1c81c 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a *pData; /*
1c81d 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e MEM cell holdin
1c81e 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 g data for the r
1c81f 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 ecord to be inse
1c820 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 rted */. Me
1c821 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 m *pKey;
1c822 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 /* MEM cell hold
1c823 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65 ing key for the
1c824 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c825 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 i64 iKey;
1c826 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 /* The intege
1c827 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 r ROWID or key f
1c828 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f or the record to
1c829 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
1c82a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c82b 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f *pC; /* Curso
1c82c 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 r to table into
1c82d 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 which insert is
1c82e 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 written */.
1c82f 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 int nZero;
1c830 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c831 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 zero-bytes to ap
1c832 70 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e pend */. in
1c833 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 t seekResult;
1c834 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69 /* Result of pri
1c835 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20 or seek or 0 if
1c836 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 no USESEEKRESULT
1c837 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 flag */. c
1c838 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 onst char *zDb;
1c839 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d /* database nam
1c83a 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 e - used by the
1c83b 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 update hook */.
1c83c 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1c83d 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 *zTbl; /* Table
1c83e 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 name - used by t
1c83f 68 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a he opdate hook *
1c840 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 /. int op;
1c841 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 /* Opc
1c842 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 ode for update h
1c843 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 ook: SQLITE_UPDA
1c844 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 TE or SQLITE_INS
1c845 45 52 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b ERT */. } bf;
1c846 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44 . struct OP_D
1c847 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 elete_stack_vars
1c848 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 {. i64 iKe
1c849 79 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 y;. VdbeCur
1c84a 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 sor *pC;. } b
1c84b 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 g;. struct OP
1c84c 5f 52 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f 76 _RowData_stack_v
1c84d 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 ars {. Vdbe
1c84e 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 Cursor *pC;.
1c84f 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c850 72 3b 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a r;. u32 n;.
1c851 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 i64 n64;.
1c852 20 20 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 72 } bh;. str
1c853 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61 uct OP_Rowid_sta
1c854 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c855 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c856 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 i64 v;.
1c857 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 sqlite3_vtab
1c858 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f *pVtab;. co
1c859 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
1c85a 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 le *pModule;.
1c85b 20 7d 20 62 69 3b 0a 20 20 20 20 73 74 72 75 63 } bi;. struc
1c85c 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 t OP_NullRow_sta
1c85d 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c85e 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c85f 20 20 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74 } bj;. st
1c860 72 75 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61 ruct OP_Last_sta
1c861 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c862 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c863 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a BtCursor *
1c864 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 pCrsr;. int
1c865 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a res;. } bk;.
1c866 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 struct OP_Re
1c867 77 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 wind_stack_vars
1c868 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 {. VdbeCurs
1c869 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 or *pC;. Bt
1c86a 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1c86b 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 int res;.
1c86c 20 20 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 75 } bl;. stru
1c86d 63 74 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b ct OP_Next_stack
1c86e 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c86f 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c870 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c871 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 rsr;. int r
1c872 65 73 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20 es;. } bm;.
1c873 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 49 struct OP_IdxI
1c874 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 73 nsert_stack_vars
1c875 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c876 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c877 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c878 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a int nKey;.
1c879 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1c87a 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e *zKey;. } bn
1c87b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c87c 49 64 78 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f IdxDelete_stack_
1c87d 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c87e 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c87f 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c880 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 sr;. int re
1c881 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 s;. Unpacke
1c882 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d dRecord r;. }
1c883 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bo;. struct
1c884 4f 50 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 63 OP_IdxRowid_stac
1c885 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 k_vars {. B
1c886 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c887 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c888 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20 *pC;. i64
1c889 72 6f 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b rowid;. } bp;
1c88a 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c88b 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 20 dxGE_stack_vars
1c88c 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 {. VdbeCurs
1c88d 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e or *pC;. in
1c88e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 t res;. Unp
1c88f 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 ackedRecord r;.
1c890 20 20 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 72 } bq;. str
1c891 75 63 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73 uct OP_Destroy_s
1c892 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c893 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 int iMoved;.
1c894 20 20 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 int iCnt;.
1c895 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b Vdbe *pVdbe;
1c896 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a . int iDb;.
1c897 20 20 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 74 } br;. st
1c898 72 75 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 ruct OP_Clear_st
1c899 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c89a 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20 int nChange;.
1c89b 20 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75 } bs;. stru
1c89c 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c ct OP_CreateTabl
1c89d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 e_stack_vars {.
1c89e 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 int pgno;.
1c89f 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a int flags;.
1c8a0 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 Db *pDb;.
1c8a1 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 72 } bt;. str
1c8a2 75 63 74 20 4f 50 5f 50 61 72 73 65 53 63 68 65 uct OP_ParseSche
1c8a3 6d 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ma_stack_vars {.
1c8a4 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 int iDb;.
1c8a5 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1c8a6 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 20 *zMaster;.
1c8a7 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 char *zSql;.
1c8a8 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 InitData initD
1c8a9 61 74 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20 ata;. } bu;.
1c8aa 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 74 struct OP_Int
1c8ab 65 67 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76 egrityCk_stack_v
1c8ac 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c8ad 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e nRoot; /* N
1c8ae 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 umber of tables
1c8af 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 to check. (Numb
1c8b0 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 er of root pages
1c8b1 2e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 .) */. int
1c8b2 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 *aRoot; /* A
1c8b3 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 rray of rootpage
1c8b4 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 numbers for tab
1c8b5 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 les to be checke
1c8b6 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a d */. int j
1c8b7 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f ; /* Lo
1c8b8 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1c8b9 20 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 int nErr;
1c8ba 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c8bb 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 errors reported
1c8bc 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a */. char *
1c8bd 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 z; /* Tex
1c8be 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 t of the error r
1c8bf 65 70 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d eport */. M
1c8c0 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f em *pnErr; /
1c8c1 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 * Register keepi
1c8c2 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f ng track of erro
1c8c3 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a rs remaining */.
1c8c4 20 20 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 74 } bv;. st
1c8c5 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 52 65 ruct OP_RowSetRe
1c8c6 61 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ad_stack_vars {.
1c8c7 20 20 20 20 20 20 69 36 34 20 76 61 6c 3b 0a 20 i64 val;.
1c8c8 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 73 74 72 } bw;. str
1c8c9 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 uct OP_RowSetTes
1c8ca 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c8cb 20 20 20 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 int iSet;.
1c8cc 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b int exists;
1c8cd 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 73 . } bx;. s
1c8ce 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d truct OP_Program
1c8cf 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c8d0 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 int nMem;
1c8d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1c8d2 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
1c8d3 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75 registers for su
1c8d4 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 b-program */.
1c8d5 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 int nByte;
1c8d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1c8d7 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 tes of runtime s
1c8d8 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f pace required fo
1c8d9 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
1c8da 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b . Mem *pRt;
1c8db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c8dc 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c * Register to al
1c8dd 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 locate runtime s
1c8de 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 pace */. Me
1c8df 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
1c8e0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
1c8e1 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 iterate through
1c8e2 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f memory cells */
1c8e3 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 . Mem *pEnd
1c8e4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1c8e5 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 * Last memory ce
1c8e6 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20 ll in new array
1c8e7 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 */. VdbeFra
1c8e8 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 me *pFrame;
1c8e9 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61 /* New vdbe fra
1c8ea 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e me to execute in
1c8eb 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 50 72 6f */. SubPro
1c8ec 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 gram *pProgram;
1c8ed 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d /* Sub-program
1c8ee 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 to execute */.
1c8ef 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 void *t;
1c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c8f1 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e Token identifyin
1c8f2 67 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 g trigger */.
1c8f3 20 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63 } by;. struc
1c8f4 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b t OP_Param_stack
1c8f5 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c8f6 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1c8f7 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b . Mem *pIn;
1c8f8 0a 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73 . } bz;. s
1c8f9 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f truct OP_MemMax_
1c8fa 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c8fb 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 Mem *pIn1;.
1c8fc 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c8fd 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 63 61 3b Frame;. } ca;
1c8fe 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 . struct OP_A
1c8ff 67 67 53 74 65 70 5f 73 74 61 63 6b 5f 76 61 72 ggStep_stack_var
1c900 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b s {. int n;
1c901 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
1c902 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 Mem *pMem;.
1c903 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a Mem *pRec;.
1c904 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f sqlite3_co
1c905 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 ntext ctx;.
1c906 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1c907 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d 20 63 62 *apVal;. } cb
1c908 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c909 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76 AggFinal_stack_v
1c90a 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 ars {. Mem
1c90b 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 63 63 3b *pMem;. } cc;
1c90c 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c90d 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f ncrVacuum_stack_
1c90e 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72 vars {. Btr
1c90f 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63 ee *pBt;. } c
1c910 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 d;. struct OP
1c911 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61 _VBegin_stack_va
1c912 72 73 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c rs {. VTabl
1c913 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20 e *pVTab;. }
1c914 63 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ce;. struct O
1c915 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 P_VOpen_stack_va
1c916 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c917 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 ursor *pCur;.
1c918 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f sqlite3_vtab_
1c919 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 cursor *pVtabCur
1c91a 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 sor;. sqlit
1c91b 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1c91c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f sqlite3_mo
1c91d 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c91e 20 20 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 } cf;. str
1c91f 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73 uct OP_VFilter_s
1c920 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c921 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 int nArg;.
1c922 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 int iQuery;.
1c923 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 const sqlite
1c924 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1c925 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 e;. Mem *pQ
1c926 75 65 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20 uery;. Mem
1c927 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71 *pArgc;. sq
1c928 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1c929 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1c92a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c92b 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c92c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c92d 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 r;. int res
1c92e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 ;. int i;.
1c92f 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 Mem **apArg
1c930 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 ;. } cg;.
1c931 73 74 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d struct OP_VColum
1c932 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 n_stack_vars {.
1c933 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
1c934 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 b *pVtab;.
1c935 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
1c936 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c937 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b Mem *pDest;
1c938 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 . sqlite3_c
1c939 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b ontext sContext;
1c93a 0a 20 20 20 20 7d 20 63 68 3b 0a 20 20 20 20 73 . } ch;. s
1c93b 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73 truct OP_VNext_s
1c93c 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c93d 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1c93e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e pVtab;. con
1c93f 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1c940 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 e *pModule;.
1c941 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
1c942 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c943 72 3b 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 20 r;. } ci;.
1c944 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61 struct OP_VRena
1c945 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a me_stack_vars {.
1c946 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c947 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c948 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20 Mem *pName;.
1c949 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75 63 } cj;. struc
1c94a 74 20 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61 t OP_VUpdate_sta
1c94b 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c94c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c94d 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 tab;. sqlit
1c94e 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1c94f 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 le;. int nA
1c950 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b rg;. int i;
1c951 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e . sqlite_in
1c952 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20 t64 rowid;.
1c953 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 Mem **apArg;.
1c954 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20 Mem *pX;.
1c955 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 72 75 63 } ck;. struc
1c956 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73 t OP_Pagecount_s
1c957 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c958 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 int p1;.
1c959 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 int nPage;.
1c95a 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a Pager *pPager;.
1c95b 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 } cl;. st
1c95c 72 75 63 74 20 4f 50 5f 54 72 61 63 65 5f 73 74 ruct OP_Trace_st
1c95d 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c95e 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20 char *zTrace;.
1c95f 20 20 20 7d 20 63 6d 3b 0a 20 20 7d 20 75 3b 0a } cm;. } u;.
1c960 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74 /* End automat
1c961 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 ically generated
1c962 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a code. ********
1c963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c964 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c965 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 ************/..
1c967 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1c968 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1c969 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 N ); /* sqlite3
1c96a 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73 _step() verifies
1c96b 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 this */. asser
1c96c 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 t( db->magic==SQ
1c96d 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
1c96e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1c96f 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
1c970 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d p);. if( p->rc=
1c971 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b =SQLITE_NOMEM ){
1c972 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1c973 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 pens if a malloc
1c974 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c () inside a call
1c975 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
1c976 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20 mn_text() or.
1c977 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** sqlite3_colu
1c978 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c mn_text16() fail
1c979 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f ed. */. goto
1c97a 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 no_mem;. }. a
1c97b 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 ssert( p->rc==SQ
1c97c 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 LITE_OK || p->rc
1c97d 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b ==SQLITE_BUSY );
1c97e 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 . p->rc = SQLIT
1c97f 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
1c980 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b p->explain==0 );
1c981 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 . p->pResultSet
1c982 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 = 0;. db->busy
1c983 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 Handler.nBusy =
1c984 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 0;. CHECK_FOR_I
1c985 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 NTERRUPT;. sqli
1c986 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
1c987 6c 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 l(p);.#ifndef SQ
1c988 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 LITE_OMIT_PROGRE
1c989 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 63 68 SS_CALLBACK. ch
1c98a 65 63 6b 50 72 6f 67 72 65 73 73 20 3d 20 64 62 eckProgress = db
1c98b 2d 3e 78 50 72 6f 67 72 65 73 73 21 3d 30 3b 0a ->xProgress!=0;.
1c98c 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
1c98d 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c LITE_DEBUG. sql
1c98e 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
1c98f 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 alloc();. if( p
1c990 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 ->pc==0 . && (
1c991 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 (p->db->flags &
1c992 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 SQLITE_VdbeListi
1c993 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 ng) || fileExist
1c994 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c s(db, "vdbe_expl
1c995 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 ain")). ){.
1c996 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 int i;. print
1c997 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 f("VDBE Program
1c998 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 Listing:\n");.
1c999 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c99a 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f ntSql(p);. fo
1c99b 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
1c99c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c i++){. sql
1c99d 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 ite3VdbePrintOp(
1c99e 73 74 64 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b stdout, i, &aOp[
1c99f 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 i]);. }. }.
1c9a0 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 if( fileExists(
1c9a1 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63 65 22 db, "vdbe_trace"
1c9a2 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 ) ){. p->trac
1c9a3 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a e = stdout;. }.
1c9a4 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
1c9a5 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 gnMalloc();.#end
1c9a6 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 if. for(pc=p->p
1c9a7 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b c; rc==SQLITE_OK
1c9a8 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 ; pc++){. ass
1c9a9 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63 ert( pc>=0 && pc
1c9aa 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 <p->nOp );. i
1c9ab 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1c9ac 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 led ) goto no_me
1c9ad 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 m;.#ifdef VDBE_P
1c9ae 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 ROFILE. origP
1c9af 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72 c = pc;. star
1c9b0 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d t = sqlite3Hwtim
1c9b1 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 e();.#endif.
1c9b2 70 4f 70 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a pOp = &aOp[pc];.
1c9b3 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c . /* Only all
1c9b4 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 ow tracing if SQ
1c9b5 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 LITE_DEBUG is de
1c9b6 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 fined.. */.#i
1c9b7 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1c9b8 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 G. if( p->tra
1c9b9 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ce ){. if(
1c9ba 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pc==0 ){.
1c9bb 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 printf("VDBE Ex
1c9bc 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e ecution Trace:\n
1c9bd 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ");. sqli
1c9be 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
1c9bf 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
1c9c0 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c9c1 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 ntOp(p->trace, p
1c9c2 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 c, pOp);. }.
1c9c3 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d if( p->trace=
1c9c4 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 =0 && pc==0 ){.
1c9c5 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
1c9c6 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1c9c7 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45 . if( fileE
1c9c8 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f xists(db, "vdbe_
1c9c9 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20 sqltrace") ){.
1c9ca 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1c9cb 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 ePrintSql(p);.
1c9cc 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1c9cd 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1c9ce 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 oc();. }.#end
1c9cf 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f if. .. /
1c9d0 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1c9d1 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d f we need to sim
1c9d2 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 ulate an interru
1c9d3 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 pt. This only h
1c9d4 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 appens. ** if
1c9d5 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69 we have a speci
1c9d6 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 al test build..
1c9d7 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
1c9d8 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28 ITE_TEST. if(
1c9d9 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1c9da 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 pt_count>0 ){.
1c9db 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
1c9dc 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 rrupt_count--;.
1c9dd 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1c9de 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c9df 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1c9e0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1c9e1 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (db);. }.
1c9e2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e }.#endif..#ifn
1c9e3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1c9e4 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 PROGRESS_CALLBAC
1c9e5 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 K. /* Call th
1c9e6 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c9e7 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e ack if it is con
1c9e8 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20 figured and the
1c9e9 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a required number.
1c9ea 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f ** of VDBE o
1c9eb 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65 ps have been exe
1c9ec 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69 cuted (either si
1c9ed 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74 nce this invocat
1c9ee 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 ion of. ** sq
1c9ef 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20 lite3VdbeExec()
1c9f0 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69 or since last ti
1c9f1 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 me the progress
1c9f2 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c callback was cal
1c9f3 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 led).. ** If
1c9f4 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1c9f5 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
1c9f6 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 n-zero, exit the
1c9f7 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1c9f8 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 with. ** a r
1c9f9 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 eturn code SQLIT
1c9fa 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a E_ABORT.. */.
1c9fb 20 20 20 20 69 66 28 20 63 68 65 63 6b 50 72 6f if( checkPro
1c9fc 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69 gress ){. i
1c9fd 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 f( db->nProgress
1c9fe 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 Ops==nProgressOp
1c9ff 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 s ){. int
1ca00 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 prc;. if
1ca01 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1ca02 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1ca03 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1ca04 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d e;. prc =
1ca05 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62 db->xProgress(db
1ca06 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b ->pProgressArg);
1ca07 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1ca08 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1ca09 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1ca0a 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1ca0b 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20 if( prc!=0
1ca0c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
1ca0d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1ca0e 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
1ca0f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
1ca10 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 lt;. }.
1ca11 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1ca12 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ps = 0;. }.
1ca13 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1ca14 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ps++;. }.#end
1ca15 69 66 0a 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e if.. /* On an
1ca16 79 20 6f 70 63 6f 64 65 20 77 69 74 68 20 74 68 y opcode with th
1ca17 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c 61 73 e "out2-prerelas
1ca18 65 22 20 74 61 67 2c 20 66 72 65 65 20 61 6e 79 e" tag, free any
1ca19 0a 20 20 20 20 2a 2a 20 65 78 74 65 72 6e 61 6c . ** external
1ca1a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 75 74 allocations out
1ca1b 20 6f 66 20 6d 65 6d 5b 70 32 5d 20 61 6e 64 20 of mem[p2] and
1ca1c 73 65 74 20 6d 65 6d 5b 70 32 5d 20 74 6f 20 62 set mem[p2] to b
1ca1d 65 0a 20 20 20 20 2a 2a 20 61 6e 20 75 6e 64 65 e. ** an unde
1ca1e 66 69 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 fined integer.
1ca1f 4f 70 63 6f 64 65 73 20 77 69 6c 6c 20 65 69 74 Opcodes will eit
1ca20 68 65 72 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 her fill in the
1ca21 69 6e 74 65 67 65 72 0a 20 20 20 20 2a 2a 20 76 integer. ** v
1ca22 61 6c 75 65 20 6f 72 20 63 6f 6e 76 65 72 74 20 alue or convert
1ca23 6d 65 6d 5b 70 32 5d 20 74 6f 20 61 20 64 69 66 mem[p2] to a dif
1ca24 66 65 72 65 6e 74 20 74 79 70 65 2e 0a 20 20 20 ferent type..
1ca25 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
1ca26 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 3d 3d 73 71 pOp->opflags==sq
1ca27 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 lite3OpcodePrope
1ca28 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d rty[pOp->opcode]
1ca29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d );. if( pOp-
1ca2a 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 >opflags & OPFLG
1ca2b 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 _OUT2_PRERELEASE
1ca2c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1ca2d 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 ( pOp->p2>0 );.
1ca2e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1ca2f 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p2<=p->nMem );
1ca30 0a 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 61 . pOut = &a
1ca31 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1ca32 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1ca33 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1ca34 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 l(pOut);. p
1ca35 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1ca36 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _Int;. }..
1ca37 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b /* Sanity check
1ca38 69 6e 67 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65 ing on other ope
1ca39 72 61 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 rands */.#ifdef
1ca3a 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1ca3b 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 if( (pOp->opfla
1ca3c 67 73 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 gs & OPFLG_IN1)!
1ca3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1ca3e 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1ca3f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1ca40 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1ca41 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 );. REGISTE
1ca42 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1ca43 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29 &aMem[pOp->p1])
1ca44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1ca45 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 (pOp->opflags &
1ca46 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b OPFLG_IN2)!=0 ){
1ca47 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1ca48 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 Op->p2>0 );.
1ca49 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ca4a 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 2<=p->nMem );.
1ca4b 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1ca4c 43 45 28 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 CE(pOp->p2, &aMe
1ca4d 6d 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 m[pOp->p2]);.
1ca4e 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4f 70 2d }. if( (pOp-
1ca4f 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 >opflags & OPFLG
1ca50 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN3)!=0 ){.
1ca51 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1ca52 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 3>0 );. ass
1ca53 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d ert( pOp->p3<=p-
1ca54 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 52 >nMem );. R
1ca55 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1ca56 70 2d 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 p->p3, &aMem[pOp
1ca57 2d 3e 70 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 ->p3]);. }.
1ca58 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c if( (pOp->opfl
1ca59 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 ags & OPFLG_OUT2
1ca5a 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 )!=0 ){. as
1ca5b 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1ca5c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1ca5d 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 pOp->p2<=p->nMe
1ca5e 6d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 m );. }. i
1ca5f 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 f( (pOp->opflags
1ca60 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d & OPFLG_OUT3)!=
1ca61 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
1ca62 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a t( pOp->p3>0 );.
1ca63 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1ca64 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1ca65 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
1ca66 20 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f . switch( pO
1ca67 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a p->opcode ){../*
1ca68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ca6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1ca6d 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 What follows is
1ca6e 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 63 68 a massive switch
1ca6f 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 72 65 statement where
1ca70 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 6c 65 each case imple
1ca71 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 61 72 ments a.** separ
1ca72 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ate instruction
1ca73 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d in the virtual m
1ca74 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 20 66 achine. If we f
1ca75 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 6c 0a ollow the usual.
1ca76 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63 ** indentation c
1ca77 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68 onventions, each
1ca78 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 65 20 case should be
1ca79 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 73 70 indented by 6 sp
1ca7a 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 aces. But.** th
1ca7b 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 20 77 at is a lot of w
1ca7c 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e 20 74 asted space on t
1ca7d 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20 he left margin.
1ca7e 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 69 74 So the code wit
1ca7f 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 74 63 hin.** the switc
1ca80 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c h statement will
1ca81 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f 6e 76 break with conv
1ca82 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c ention and be fl
1ca83 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65 ush-left. Anothe
1ca84 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 r.** big comment
1ca85 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 (similar to thi
1ca86 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b s one) will mark
1ca87 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 the point in th
1ca88 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20 e code where.**
1ca89 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 we transition ba
1ca8a 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 ck to normal ind
1ca8b 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 entation..**.**
1ca8c 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f The formatting o
1ca8d 66 20 65 61 63 68 20 63 61 73 65 20 69 73 20 69 f each case is i
1ca8e 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 20 6d mportant. The m
1ca8f 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 akefile for SQLi
1ca90 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 te.** generates
1ca91 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f 70 63 two C files "opc
1ca92 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 odes.h" and "opc
1ca93 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 6e 6e odes.c" by scann
1ca94 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65 ing this.** file
1ca95 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e looking for lin
1ca96 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 es that begin wi
1ca97 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 th "case OP_".
1ca98 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 The opcodes.h fi
1ca99 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 les.** will be f
1ca9a 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66 69 illed with #defi
1ca9b 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75 6e nes that give un
1ca9c 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61 6c ique integer val
1ca9d 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f ues to each.** o
1ca9e 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f 70 pcode and the op
1ca9f 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73 20 codes.c file is
1caa0 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20 61 filled with an a
1caa1 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73 20 rray of strings
1caa2 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73 74 where.** each st
1caa3 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 6d 62 ring is the symb
1caa4 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 74 68 olic name for th
1caa5 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1caa6 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 65 0a opcode. If the.
1caa7 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d 65 6e ** case statemen
1caa8 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 t is followed by
1caa9 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 a comment of th
1caaa 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20 e form "/# same
1caab 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 as ... #/".** th
1caac 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73 at comment is us
1caad 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
1caae 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 the particular v
1caaf 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f alue of the opco
1cab0 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 de..**.** Other
1cab1 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 65 20 keywords in the
1cab2 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c comment that fol
1cab3 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 20 61 lows each case a
1cab4 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f re used to.** co
1cab5 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 46 4c nstruct the OPFL
1cab6 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 76 61 G_INITIALIZER va
1cab7 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c lue that initial
1cab8 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f 70 65 izes opcodePrope
1cab9 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 rty[]..** Keywor
1caba 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c ds include: in1,
1cabb 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f in2, in3, out2_
1cabc 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 74 32 prerelease, out2
1cabd 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20 , out3. See.**
1cabe 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 the mkopcodeh.aw
1cabf 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 64 64 k script for add
1cac0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
1cac1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d ion..**.** Docum
1cac2 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20 56 entation about V
1cac3 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20 67 DBE opcodes is g
1cac4 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61 6e enerated by scan
1cac5 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a 2a ning this file.*
1cac6 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74 * for lines of t
1cac7 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63 hat contain "Opc
1cac8 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e ode:". That lin
1cac9 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65 71 e and all subseq
1caca 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 uent.** comment
1cacb 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69 lines are used i
1cacc 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f 6e n the generation
1cacd 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 of the opcode.h
1cace 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f tml documentatio
1cacf 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a n.** file..**.**
1cad0 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 SUMMARY:.**.**
1cad1 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20 69 Formatting i
1cad2 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 s important to s
1cad3 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61 6e cripts that scan
1cad4 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 this file..**
1cad5 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61 74 Do not deviat
1cad6 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 e from the forma
1cad7 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72 72 tting style curr
1cad8 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a ently in use..**
1cad9 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1cada 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cadb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cadc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1cadd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1cade 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74 ./* Opcode: Got
1cadf 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a o * P2 * * *.**.
1cae0 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f ** An unconditio
1cae1 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 nal jump to addr
1cae2 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e ess P2..** The n
1cae3 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1cae4 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 62 65 executed will be
1cae5 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 .** the one at
1cae6 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 74 68 index P2 from th
1cae7 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a e beginning of.*
1cae8 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a * the program..*
1cae9 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 /.case OP_Goto:
1caea 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1caeb 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b jump */. CHECK
1caec 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a _FOR_INTERRUPT;.
1caed 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1caee 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 1;. break;.}..
1caef 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 /* Opcode: Gosu
1caf0 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a b P1 P2 * * *.**
1caf1 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75 .** Write the cu
1caf2 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 6f 6e rrent address on
1caf3 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a to register P1.*
1caf4 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 * and then jump
1caf5 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a to address P2..*
1caf6 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a /.case OP_Gosub:
1caf7 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1caf8 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 jump, in1 */.
1caf9 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1cafa 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1cafb 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1cafc 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 MEM_Dyn)==0 );.
1cafd 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d pIn1->flags = M
1cafe 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e EM_Int;. pIn1->
1caff 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 u.i = pc;. REGI
1cb00 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1cb01 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 p1, pIn1);. pc
1cb02 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1cb03 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cb04 63 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 code: Return P1
1cb05 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a * * * *.**.** J
1cb06 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ump to the next
1cb07 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 instruction afte
1cb08 72 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e r the address in
1cb09 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f register P1..*/
1cb0a 0a 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a .case OP_Return:
1cb0b 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1cb0c 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 in1 */. pIn1 =
1cb0d 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1cb0e 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1cb0f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
1cb10 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 );. pc = (int)p
1cb11 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 In1->u.i;. brea
1cb12 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1cb13 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a Yield P1 * * *
1cb14 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 *.**.** Swap th
1cb15 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 e program counte
1cb16 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 r with the value
1cb17 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
1cb18 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c .*/.case OP_Yiel
1cb19 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 d: {
1cb1a 2f 2a 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 /* in1 */.#if 0
1cb1b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1cb1c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1cb1d 2e 61 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 .aa */. int pcD
1cb1e 65 73 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c est;.#endif /* l
1cb1f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1cb20 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a oved into u.aa *
1cb21 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d /. pIn1 = &aMem
1cb22 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1cb23 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 ert( (pIn1->flag
1cb24 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 s & MEM_Dyn)==0
1cb25 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 );. pIn1->flags
1cb26 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e = MEM_Int;. u.
1cb27 61 61 2e 70 63 44 65 73 74 20 3d 20 28 69 6e 74 aa.pcDest = (int
1cb28 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 )pIn1->u.i;. pI
1cb29 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 n1->u.i = pc;.
1cb2a 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1cb2b 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 Op->p1, pIn1);.
1cb2c 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 pc = u.aa.pcDes
1cb2d 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f t;. break;.}../
1cb2e 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 * Opcode: HaltI
1cb2f 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20 fNull P1 P2 P3
1cb30 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b P4 *.**.** Check
1cb31 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cb32 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69 gister P3. If i
1cb33 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 s is NULL then H
1cb34 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 alt using.** par
1cb35 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61 ameter P1, P2, a
1cb36 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73 nd P4 as if this
1cb37 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73 were a Halt ins
1cb38 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 truction. If th
1cb39 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 e.** value in re
1cb3a 67 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74 gister P3 is not
1cb3b 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 NULL, then this
1cb3c 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
1cb3d 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f -op..*/.case OP_
1cb3e 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 HaltIfNull: {
1cb3f 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 70 /* in3 */. p
1cb40 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In3 = &aMem[pOp-
1cb41 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e >p3];. if( (pIn
1cb42 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 3->flags & MEM_N
1cb43 75 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b ull)==0 ) break;
1cb44 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 . /* Fall throu
1cb45 67 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 gh into OP_Halt
1cb46 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a */.}../* Opcode:
1cb47 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50 Halt P1 P2 * P
1cb48 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 4 *.**.** Exit i
1cb49 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c mmediately. All
1cb4a 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 open cursors, e
1cb4b 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a tc are closed.**
1cb4c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a automatically..
1cb4d 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1cb4e 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75 result code retu
1cb4f 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
1cb50 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f exec(), sqlite3_
1cb51 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 reset(),.** or s
1cb52 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1cb53 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c ). For a normal
1cb54 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 halt, this shou
1cb55 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 ld be SQLITE_OK
1cb56 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f (0)..** For erro
1cb57 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f rs, it can be so
1cb58 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 me other value.
1cb59 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50 If P1!=0 then P
1cb5a 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 2 will determine
1cb5b 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e .** whether or n
1cb5c 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ot to rollback t
1cb5d 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
1cb5e 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 action. Do not
1cb5f 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 rollback.** if P
1cb60 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 2==OE_Fail. Do t
1cb61 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 he rollback if P
1cb62 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 2==OE_Rollback.
1cb63 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 If P2==OE_Abort
1cb64 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f ,.** then back o
1cb65 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 ut all changes t
1cb66 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 hat have occurre
1cb67 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78 d during this ex
1cb68 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a ecution of the.*
1cb69 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e * VDBE, but do n
1cb6a 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 ot rollback the
1cb6b 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a transaction. .**
1cb6c 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 .** If P4 is not
1cb6d 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 null then it is
1cb6e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1cb6f 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 e string..**.**
1cb70 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c There is an impl
1cb71 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22 ied "Halt 0 0 0"
1cb72 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 instruction ins
1cb73 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72 erted at the ver
1cb74 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 y end of.** ever
1cb75 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 y program. So a
1cb76 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c jump past the l
1cb77 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ast instruction
1cb78 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a of the program.*
1cb79 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 * is the same as
1cb7a 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e executing Halt.
1cb7b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 .*/.case OP_Halt
1cb7c 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 : {. if( pOp->p
1cb7d 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 1==SQLITE_OK &&
1cb7e 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 p->pFrame ){.
1cb7f 20 2f 2a 20 48 61 6c 74 20 74 68 65 20 73 75 62 /* Halt the sub
1cb80 2d 70 72 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e -program. Return
1cb81 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 control to the
1cb82 70 61 72 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f parent frame. */
1cb83 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a . VdbeFrame *
1cb84 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 pFrame = p->pFra
1cb85 6d 65 3b 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d me;. p->pFram
1cb86 65 20 3d 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 e = pFrame->pPar
1cb87 65 6e 74 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61 ent;. p->nFra
1cb88 6d 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 me--;. sqlite
1cb89 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
1cb8a 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b db, p->nChange);
1cb8b 0a 20 20 20 20 70 63 20 3d 20 73 71 6c 69 74 65 . pc = sqlite
1cb8c 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 3VdbeFrameRestor
1cb8d 65 28 70 46 72 61 6d 65 29 3b 0a 20 20 20 20 69 e(pFrame);. i
1cb8e 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 f( pOp->p2==OE_I
1cb8f 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 2f gnore ){. /
1cb90 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 * Instruction pc
1cb91 20 69 73 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 is the OP_Progr
1cb92 61 6d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 am that invoked
1cb93 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 the sub-program
1cb94 0a 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e . ** curren
1cb95 74 6c 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64 tly being halted
1cb96 2e 20 49 66 20 74 68 65 20 70 32 20 69 6e 73 74 . If the p2 inst
1cb97 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 ruction of this
1cb98 4f 50 5f 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a OP_Halt. **
1cb99 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
1cb9a 73 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 set to OE_Ignore
1cb9b 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70 , then the sub-p
1cb9c 72 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69 rogram is throwi
1cb9d 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49 ng. ** an I
1cb9e 47 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e GNORE exception.
1cb9f 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 In this case ju
1cba0 6d 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 mp to the addres
1cba1 73 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 s specified.
1cba2 20 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20 6f ** as the p2 o
1cba3 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50 f the calling OP
1cba4 5f 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 _Program. */.
1cba5 20 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b pc = p->aOp[
1cba6 70 63 5d 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a pc].p2-1;. }.
1cba7 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 aOp = p->aOp
1cba8 3b 0a 20 20 20 20 61 4d 65 6d 20 3d 20 70 2d 3e ;. aMem = p->
1cba9 61 4d 65 6d 3b 0a 20 20 20 20 62 72 65 61 6b 3b aMem;. break;
1cbaa 0a 20 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 . }.. p->rc =
1cbab 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 pOp->p1;. p->er
1cbac 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 rorAction = (u8)
1cbad 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 pOp->p2;. p->pc
1cbae 20 3d 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 = pc;. if( pOp
1cbaf 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 ->p4.z ){. sq
1cbb0 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1cbb1 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1cbb2 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 "%s", pOp->p4.z)
1cbb3 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c ;. }. rc = sql
1cbb4 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1cbb5 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
1cbb6 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 QLITE_BUSY || rc
1cbb7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 ==SQLITE_OK || r
1cbb8 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 c==SQLITE_ERROR
1cbb9 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1cbba 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1cbbb 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1cbbc 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 ITE_BUSY;. }els
1cbbd 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 e{. assert( r
1cbbe 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
1cbbf 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f p->rc==SQLITE_CO
1cbc0 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 NSTRAINT );.
1cbc1 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1cbc2 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 TE_OK || db->nDe
1cbc3 66 65 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a ferredCons>0 );.
1cbc4 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f rc = p->rc ?
1cbc5 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 SQLITE_ERROR :
1cbc6 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
1cbc7 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 . goto vdbe_ret
1cbc8 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 urn;.}../* Opcod
1cbc9 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 e: Integer P1 P2
1cbca 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1cbcb 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
1cbcc 76 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 value P1 is writ
1cbcd 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ten into registe
1cbce 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1cbcf 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 _Integer: {
1cbd0 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1cbd1 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 elease */. pOut
1cbd2 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b ->u.i = pOp->p1;
1cbd3 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cbd4 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 Opcode: Int64 *
1cbd5 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1cbd6 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1cbd7 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 to a 64-bit inte
1cbd8 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 ger value..** Wr
1cbd9 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 ite that value i
1cbda 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1cbdb 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 .*/.case OP_Int6
1cbdc 34 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 4: { /
1cbdd 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1cbde 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 e */. assert( p
1cbdf 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 Op->p4.pI64!=0 )
1cbe0 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1cbe1 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 *pOp->p4.pI64;.
1cbe2 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cbe3 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 code: Real * P2
1cbe4 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1cbe5 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1cbe6 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e a 64-bit floatin
1cbe7 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a g point value..*
1cbe8 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c * Write that val
1cbe9 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 ue into register
1cbea 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P2..*/.case OP_
1cbeb 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 Real: {
1cbec 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1cbed 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 _FLOAT, out2-pre
1cbee 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 release */. pOu
1cbef 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 t->flags = MEM_R
1cbf0 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 eal;. assert( !
1cbf1 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f sqlite3IsNaN(*pO
1cbf2 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a p->p4.pReal) );.
1cbf3 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 pOut->r = *pOp
1cbf4 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 ->p4.pReal;. br
1cbf5 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cbf6 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 e: String8 * P2
1cbf7 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1cbf8 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 points to a nul
1cbf9 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
1cbfa 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 string. This op
1cbfb 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 code is transfor
1cbfc 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 med .** into an
1cbfd 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 OP_String before
1cbfe 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 it is executed
1cbff 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 for the first ti
1cc00 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 me..*/.case OP_S
1cc01 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 tring8: {
1cc02 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1cc03 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 STRING, out2-pre
1cc04 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 release */. ass
1cc05 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d ert( pOp->p4.z!=
1cc06 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 0 );. pOp->opco
1cc07 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a de = OP_String;.
1cc08 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 pOp->p1 = sqli
1cc09 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d te3Strlen30(pOp-
1cc0a 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 >p4.z);..#ifndef
1cc0b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1cc0c 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 16. if( encodin
1cc0d 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 g!=SQLITE_UTF8 )
1cc0e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1cc0f 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1cc10 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c pOut, pOp->p4.z,
1cc11 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
1cc12 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
1cc13 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1cc14 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f LITE_TOOBIG ) go
1cc15 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 to too_big;.
1cc16 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
1cc17 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1cc18 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 Encoding(pOut, e
1cc19 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 ncoding) ) goto
1cc1a 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 no_mem;. asse
1cc1b 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f rt( pOut->zMallo
1cc1c 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 c==pOut->z );.
1cc1d 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e assert( pOut->
1cc1e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 flags & MEM_Dyn
1cc1f 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 );. pOut->zMa
1cc20 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f lloc = 0;. pO
1cc21 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ut->flags |= MEM
1cc22 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 _Static;. pOu
1cc23 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d t->flags &= ~MEM
1cc24 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f _Dyn;. if( pO
1cc25 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 p->p4type==P4_DY
1cc26 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 NAMIC ){. s
1cc27 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1cc28 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
1cc29 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 }. pOp->p4ty
1cc2a 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b pe = P4_DYNAMIC;
1cc2b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d . pOp->p4.z =
1cc2c 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f pOut->z;. pO
1cc2d 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b p->p1 = pOut->n;
1cc2e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
1cc2f 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c ( pOp->p1>db->aL
1cc30 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1cc31 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1cc32 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
1cc33 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 }. /* Fall thr
1cc34 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1cc35 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 case, OP_String
1cc36 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f */.}. ./* Opco
1cc37 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 de: String P1 P2
1cc38 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 * P4 *.**.** Th
1cc39 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 e string value P
1cc3a 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 4 of length P1 (
1cc3b 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 bytes) is stored
1cc3c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1cc3d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 .*/.case OP_Stri
1cc3e 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f ng: { /
1cc3f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1cc40 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 e */. assert( p
1cc41 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 Op->p4.z!=0 );.
1cc42 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1cc43 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 EM_Str|MEM_Stati
1cc44 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f c|MEM_Term;. pO
1cc45 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e ut->z = pOp->p4.
1cc46 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 z;. pOut->n = p
1cc47 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e Op->p1;. pOut->
1cc48 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a enc = encoding;.
1cc49 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1cc4a 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1cc4b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cc4c 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 de: Null * P2 *
1cc4d 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1cc4e 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 a NULL into regi
1cc4f 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 ster P2..*/.case
1cc50 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 OP_Null: {
1cc51 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1cc52 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f erelease */. pO
1cc53 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1cc54 4e 75 6c 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Null;. break;.}
1cc55 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c .../* Opcode: Bl
1cc56 6f 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a ob P1 P2 * P4.**
1cc57 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f .** P4 points to
1cc58 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 a blob of data
1cc59 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 P1 bytes long.
1cc5a 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c Store this.** bl
1cc5b 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ob in register P
1cc5c 32 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 2. This instruct
1cc5d 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 ion is not coded
1cc5e 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 directly.** by
1cc5f 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e the compiler. In
1cc60 73 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 stead, the compi
1cc61 6c 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 ler layer specif
1cc62 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 ies.** an OP_Hex
1cc63 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 Blob opcode, wit
1cc64 68 20 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 h the hex string
1cc65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1cc66 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 of.** the blob a
1cc67 73 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 s P4. This opcod
1cc68 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 e is transformed
1cc69 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a to an OP_Blob.*
1cc6a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 * the first time
1cc6b 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e it is executed.
1cc6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 .*/.case OP_Blob
1cc6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1cc6e 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1cc6f 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 lease */. asser
1cc70 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 t( pOp->p1 <= SQ
1cc71 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 LITE_MAX_LENGTH
1cc72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1cc73 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 MemSetStr(pOut,
1cc74 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e pOp->p4.z, pOp->
1cc75 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 p1, 0, 0);. pOu
1cc76 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1cc77 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f g;. UPDATE_MAX_
1cc78 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1cc79 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cc7a 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20 pcode: Variable
1cc7b 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1cc7c 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 .** Transfer the
1cc7d 20 76 61 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 values of bound
1cc7e 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 2e 2e parameters P1..
1cc7f 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 72 65 67 P1+P3-1 into reg
1cc80 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32 isters.** P2..P2
1cc81 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 +P3-1..**.** If
1cc82 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 the parameter is
1cc83 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 69 74 73 named, then its
1cc84 20 6e 61 6d 65 20 61 70 70 65 61 72 73 20 69 6e name appears in
1cc85 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a P4 and P3==1..*
1cc86 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 69 * The P4 value i
1cc87 73 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 s used by sqlite
1cc88 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
1cc89 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65 _name()..*/.case
1cc8a 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a OP_Variable: {.
1cc8b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1cc8c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1cc8d 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69 into u.ab */. i
1cc8e 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 nt p1;
1cc8f 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63 /* Variable to c
1cc90 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e opy from */. in
1cc91 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f t p2; /
1cc92 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f * Register to co
1cc93 70 79 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e py to */. int n
1cc94 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1cc95 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 umber of values
1cc96 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a left to copy */.
1cc97 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 Mem *pVar;
1cc98 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e /* Value bein
1cc99 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f g transferred */
1cc9a 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1cc9b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cc9c 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20 into u.ab */..
1cc9d 20 75 2e 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e u.ab.p1 = pOp->
1cc9e 70 31 20 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70 p1 - 1;. u.ab.p
1cc9f 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2 = pOp->p2;. u
1cca0 2e 61 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b .ab.n = pOp->p3;
1cca1 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e . assert( u.ab.
1cca2 70 31 3e 3d 30 20 26 26 20 75 2e 61 62 2e 70 31 p1>=0 && u.ab.p1
1cca3 2b 75 2e 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 +u.ab.n<=p->nVar
1cca4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1cca5 61 62 2e 70 32 3e 3d 31 20 26 26 20 75 2e 61 62 ab.p2>=1 && u.ab
1cca6 2e 70 32 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d .p2+u.ab.n-1<=p-
1cca7 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 >nMem );. asser
1cca8 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 t( pOp->p4.z==0
1cca9 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 7c 7c || pOp->p3==1 ||
1ccaa 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 0a 0a pOp->p3==0 );..
1ccab 20 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d while( u.ab.n-
1ccac 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 75 2e 61 - > 0 ){. u.a
1ccad 62 2e 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 b.pVar = &p->aVa
1ccae 72 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20 r[u.ab.p1++];.
1ccaf 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1ccb0 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e eMemTooBig(u.ab.
1ccb1 70 56 61 72 29 20 29 7b 0a 20 20 20 20 20 20 67 pVar) ){. g
1ccb2 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 oto too_big;.
1ccb3 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 61 }. pOut = &a
1ccb4 4d 65 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a Mem[u.ab.p2++];.
1ccb5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1ccb6 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1ccb7 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75 l(pOut);. pOu
1ccb8 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e t->flags = MEM_N
1ccb9 75 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ull;. sqlite3
1ccba 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
1ccbb 70 79 28 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56 py(pOut, u.ab.pV
1ccbc 61 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b ar, MEM_Static);
1ccbd 0a 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f . UPDATE_MAX_
1ccbe 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1ccbf 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1ccc0 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 /* Opcode: Move
1ccc1 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ccc2 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 ** Move the valu
1ccc3 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 es in register P
1ccc4 31 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 1..P1+P3-1 over
1ccc5 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 into.** register
1ccc6 73 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 s P2..P2+P3-1.
1ccc7 52 65 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 Registers P1..P1
1ccc8 2b 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 +P1-1 are.** lef
1ccc9 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c t holding a NULL
1ccca 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f . It is an erro
1cccb 72 20 66 6f 72 20 72 65 67 69 73 74 65 72 20 72 r for register r
1cccc 61 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b anges.** P1..P1+
1cccd 50 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b P3-1 and P2..P2+
1ccce 50 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e P3-1 to overlap.
1cccf 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 .*/.case OP_Move
1ccd0 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1ccd1 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1ccd2 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f ved into u.ac */
1ccd3 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 . char *zMalloc
1ccd4 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 ; /* Holding v
1ccd5 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f ariable for allo
1ccd6 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a cated memory */.
1ccd7 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 int n;
1ccd8 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1ccd9 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 registers left t
1ccda 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 o copy */. int
1ccdb 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p1; /*
1ccdc 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 Register to copy
1ccdd 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 from */. int p
1ccde 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2; /* R
1ccdf 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 egister to copy
1cce0 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 to */.#endif /*
1cce1 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cce2 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 moved into u.ac
1cce3 2a 2f 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70 */.. u.ac.n = p
1cce4 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70 Op->p3;. u.ac.p
1cce5 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1cce6 2e 61 63 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 .ac.p2 = pOp->p2
1cce7 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 ;. assert( u.ac
1cce8 2e 6e 3e 30 20 26 26 20 75 2e 61 63 2e 70 31 3e .n>0 && u.ac.p1>
1cce9 30 20 26 26 20 75 2e 61 63 2e 70 32 3e 30 20 29 0 && u.ac.p2>0 )
1ccea 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 ;. assert( u.ac
1cceb 2e 70 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 .p1+u.ac.n<=u.ac
1ccec 2e 70 32 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75 .p2 || u.ac.p2+u
1cced 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29 .ac.n<=u.ac.p1 )
1ccee 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 ;.. pIn1 = &aMe
1ccef 6d 5b 75 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f m[u.ac.p1];. pO
1ccf0 75 74 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 63 2e ut = &aMem[u.ac.
1ccf1 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e p2];. while( u.
1ccf2 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 ac.n-- ){. as
1ccf3 73 65 72 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65 sert( pOut<=&aMe
1ccf4 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 m[p->nMem] );.
1ccf5 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d assert( pIn1<=
1ccf6 26 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 &aMem[p->nMem] )
1ccf7 3b 0a 20 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c ;. u.ac.zMall
1ccf8 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c oc = pOut->zMall
1ccf9 6f 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d oc;. pOut->zM
1ccfa 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 alloc = 0;. s
1ccfb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1ccfc 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 e(pOut, pIn1);.
1ccfd 20 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 pIn1->zMalloc
1ccfe 20 3d 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b = u.ac.zMalloc;
1ccff 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 . REGISTER_TR
1cd00 41 43 45 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70 ACE(u.ac.p2++, p
1cd01 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b Out);. pIn1++
1cd02 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 ;. pOut++;.
1cd03 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1cd04 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 Opcode: Copy P1
1cd05 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1cd06 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 Make a copy of r
1cd07 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 egister P1 into
1cd08 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1cd09 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1cd0a 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 ion makes a deep
1cd0b 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
1cd0c 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 ue. A duplicate
1cd0d 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 .** is made of a
1cd0e 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f ny string or blo
1cd0f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 b constant. See
1cd10 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a also OP_SCopy..
1cd11 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a */.case OP_Copy:
1cd12 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1cd13 2a 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 * in1, out2 */.
1cd14 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn1 = &aMem[pO
1cd15 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d p->p1];. pOut =
1cd16 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b &aMem[pOp->p2];
1cd17 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 . assert( pOut!
1cd18 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 =pIn1 );. sqlit
1cd19 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
1cd1a 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c Copy(pOut, pIn1,
1cd1b 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 MEM_Ephem);. D
1cd1c 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f eephemeralize(pO
1cd1d 75 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f ut);. REGISTER_
1cd1e 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1cd1f 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1cd20 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f ../* Opcode: SCo
1cd21 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a py P1 P2 * * *.*
1cd22 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c *.** Make a shal
1cd23 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 low copy of regi
1cd24 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 ster P1 into reg
1cd25 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
1cd26 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1cd27 20 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 makes a shallow
1cd28 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
1cd29 75 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 ue. If the valu
1cd2a 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 e.** is a string
1cd2b 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 or blob, then t
1cd2c 68 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 he copy is only
1cd2d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1cd2e 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 .** original and
1cd2f 20 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 hence if the or
1cd30 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 iginal changes s
1cd31 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e o will the copy.
1cd32 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 .** Worse, if th
1cd33 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 e original is de
1cd34 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 allocated, the c
1cd35 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 opy becomes inva
1cd36 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 lid..** Thus the
1cd37 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 program must gu
1cd38 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 arantee that the
1cd39 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e original will n
1cd3a 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 ot change.** dur
1cd3b 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 ing the lifetime
1cd3c 20 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 of the copy. U
1cd3d 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 se OP_Copy to ma
1cd3e 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a ke a complete.**
1cd3f 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f copy..*/.case O
1cd40 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 P_SCopy: {
1cd41 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 6f 75 /* in1, ou
1cd42 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 t2 */. pIn1 = &
1cd43 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1cd44 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f pOut = &aMem[pO
1cd45 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 p->p2];. assert
1cd46 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a ( pOut!=pIn1 );.
1cd47 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cd48 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 ShallowCopy(pOut
1cd49 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 , pIn1, MEM_Ephe
1cd4a 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 m);. REGISTER_T
1cd4b 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f RACE(pOp->p2, pO
1cd4c 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1cd4d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 ./* Opcode: Resu
1cd4e 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 ltRow P1 P2 * *
1cd4f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 *.**.** The regi
1cd50 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68 sters P1 through
1cd51 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e P1+P2-1 contain
1cd52 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 a single row of
1cd53 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 69 .** results. Thi
1cd54 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 s opcode causes
1cd55 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 the sqlite3_step
1cd56 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 () call to termi
1cd57 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 nate.** with an
1cd58 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 72 SQLITE_ROW retur
1cd59 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 65 n code and it se
1cd5a 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 65 ts up the sqlite
1cd5b 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 3_stmt.** struct
1cd5c 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 ure to provide a
1cd5d 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f 70 ccess to the top
1cd5e 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 68 P1 values as th
1cd5f 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e e result.** row.
1cd60 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 .*/.case OP_Resu
1cd61 6c 74 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 ltRow: {.#if 0
1cd62 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cd63 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cd64 61 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 ad */. Mem *pMe
1cd65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64 m;. int i;.#end
1cd66 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1cd67 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1cd68 20 75 2e 61 64 20 2a 2f 0a 20 20 61 73 73 65 72 u.ad */. asser
1cd69 74 28 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e t( p->nResColumn
1cd6a 3d 3d 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 ==pOp->p2 );. a
1cd6b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 ssert( pOp->p1>0
1cd6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1cd6d 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 p->p1+pOp->p2<=p
1cd6e 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f ->nMem+1 );.. /
1cd6f 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d * If this statem
1cd70 65 6e 74 20 68 61 73 20 76 69 6f 6c 61 74 65 64 ent has violated
1cd71 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 immediate forei
1cd72 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1cd73 74 73 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 ts, do. ** not
1cd74 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
1cd75 72 20 6f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69 r of rows modifi
1cd76 65 64 2e 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52 ed. And do not R
1cd77 45 4c 45 41 53 45 20 74 68 65 20 73 74 61 74 65 ELEASE the state
1cd78 6d 65 6e 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 ment. ** transa
1cd79 63 74 69 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20 ction. It needs
1cd7a 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 to be rolled bac
1cd7b 6b 2e 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c k. */. if( SQL
1cd7c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
1cd7d 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
1cd7e 28 70 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 61 (p, 0)) ){. a
1cd7f 73 73 65 72 74 28 20 64 62 2d 3e 66 6c 61 67 73 ssert( db->flags
1cd80 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 &SQLITE_CountRow
1cd81 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 s );. assert(
1cd82 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 p->usesStmtJour
1cd83 6e 61 6c 20 29 3b 0a 20 20 20 20 62 72 65 61 6b nal );. break
1cd84 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1cd85 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 he SQLITE_CountR
1cd86 6f 77 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 ows flag is set
1cd87 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 in sqlite3.flags
1cd88 20 6d 61 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a mask, then. **
1cd89 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 DML statements
1cd8a 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f invoke this opco
1cd8b 64 65 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 de to return the
1cd8c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a number of rows.
1cd8d 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f ** modified to
1cd8e 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 the user. This
1cd8f 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
1cd90 74 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20 that a VM that.
1cd91 20 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74 ** opens a stat
1cd92 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1cd93 6e 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 n may invoke thi
1cd94 73 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 s opcode.. **.
1cd95 20 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73 ** In case this
1cd96 20 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65 is such a state
1cd97 6d 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 ment, close any
1cd98 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1cd99 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 ction. ** opene
1cd9a 64 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 d by this VM bef
1cd9b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f ore returning co
1cd9c 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 ntrol to the use
1cd9d 72 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20 r. This is to.
1cd9e 2a 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 ** ensure that s
1cd9f 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
1cda0 74 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 tions are always
1cda1 20 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 nested, not ove
1cda2 72 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 rlapping.. ** I
1cda3 66 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65 f the open state
1cda4 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e ment-transaction
1cda5 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 is not closed h
1cda6 65 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73 ere, then the us
1cda7 65 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 er. ** may step
1cda8 20 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74 another VM that
1cda9 20 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 opens its own s
1cdaa 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1cdab 74 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 tion. This. **
1cdac 6d 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 may lead to over
1cdad 6c 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e lapping statemen
1cdae 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a t transactions..
1cdaf 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 **. ** The st
1cdb0 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1cdb1 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 61 20 74 ion is never a t
1cdb2 6f 70 2d 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 op-level transac
1cdb3 74 69 6f 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a tion. Hence. *
1cdb4 2a 20 74 68 65 20 52 45 4c 45 41 53 45 20 63 61 * the RELEASE ca
1cdb5 6c 6c 20 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 ll below can nev
1cdb6 65 72 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 er fail.. */.
1cdb7 61 73 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74 assert( p->iStat
1cdb8 65 6d 65 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e ement==0 || db->
1cdb9 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 flags&SQLITE_Cou
1cdba 6e 74 52 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d ntRows );. rc =
1cdbb 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 sqlite3VdbeClos
1cdbc 65 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 eStatement(p, SA
1cdbd 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 VEPOINT_RELEASE)
1cdbe 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
1cdbf 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a !=SQLITE_OK) ){.
1cdc0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a break;. }..
1cdc1 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 /* Invalidate
1cdc2 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 all ephemeral cu
1cdc3 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 rsor row caches
1cdc4 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 */. p->cacheCtr
1cdc5 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 = (p->cacheCtr
1cdc6 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 + 2)|1;.. /* Ma
1cdc7 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 ke sure the resu
1cdc8 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 lts of the curre
1cdc9 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 nt row are \000
1cdca 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 terminated. **
1cdcb 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 and have an assi
1cdcc 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 gned type. The
1cdcd 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65 results are de-e
1cdce 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a phemeralized as.
1cdcf 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 ** as side eff
1cdd0 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 ect.. */. u.ad
1cdd1 2e 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 .pMem = p->pResu
1cdd2 6c 74 53 65 74 20 3d 20 26 61 4d 65 6d 5b 70 4f ltSet = &aMem[pO
1cdd3 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e p->p1];. for(u.
1cdd4 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70 ad.i=0; u.ad.i<p
1cdd5 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b Op->p2; u.ad.i++
1cdd6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1cdd7 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
1cdd8 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 e(&u.ad.pMem[u.a
1cdd9 64 2e 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 d.i]);. sqlit
1cdda 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 e3VdbeMemStoreTy
1cddb 70 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e pe(&u.ad.pMem[u.
1cddc 61 64 2e 69 5d 29 3b 0a 20 20 20 20 52 45 47 49 ad.i]);. REGI
1cddd 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1cdde 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64 p1+u.ad.i, &u.ad
1cddf 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a .pMem[u.ad.i]);.
1cde0 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
1cde1 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 llocFailed ) got
1cde2 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 o no_mem;.. /*
1cde3 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f Return SQLITE_RO
1cde4 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d W. */. p->pc =
1cde5 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 pc + 1;. rc =
1cde6 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f SQLITE_ROW;. go
1cde7 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1cde8 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
1cde9 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 2a 20 ncat P1 P2 P3 *
1cdea 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 *.**.** Add the
1cdeb 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 65 72 text in register
1cdec 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 P1 onto the end
1cded 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a of the text in.
1cdee 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 20 61 ** register P2 a
1cdef 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1cdf0 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1cdf1 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1cdf2 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 74 65 the P1 or P2 te
1cdf3 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e xt are NULL then
1cdf4 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 store NULL in P
1cdf5 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 3..**.** P3 =
1cdf6 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 P2 || P1.**.** I
1cdf7 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 t is illegal for
1cdf8 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65 P1 and P3 to be
1cdf9 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 the same regist
1cdfa 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a er. Sometimes,.*
1cdfb 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 20 73 * if P3 is the s
1cdfc 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 73 20 ame register as
1cdfd 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e P2, the implemen
1cdfe 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a tation is able.*
1cdff 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d * to avoid a mem
1ce00 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f cpy()..*/.case O
1ce01 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 P_Concat: {
1ce02 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1ce03 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c TK_CONCAT, in1,
1ce04 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 in2, out3 */.#i
1ce05 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ce06 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ce07 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34 to u.ae */. i64
1ce08 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f nByte;.#endif /
1ce09 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ce0a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1ce0b 65 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 e */.. pIn1 = &
1ce0c 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1ce0d 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn2 = &aMem[pO
1ce0e 70 2d 3e 70 32 5d 3b 0a 20 20 70 4f 75 74 20 3d p->p2];. pOut =
1ce0f 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b &aMem[pOp->p3];
1ce10 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21 . assert( pIn1!
1ce11 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28 =pOut );. if( (
1ce12 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1ce13 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d n2->flags) & MEM
1ce14 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
1ce15 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1ce16 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 ll(pOut);. br
1ce17 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 eak;. }. if( E
1ce18 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 xpandBlob(pIn1)
1ce19 7c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 || ExpandBlob(pI
1ce1a 6e 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 n2) ) goto no_me
1ce1b 6d 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 m;. Stringify(p
1ce1c 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a In1, encoding);.
1ce1d 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 Stringify(pIn2
1ce1e 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 , encoding);. u
1ce1f 2e 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 .ae.nByte = pIn1
1ce20 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 ->n + pIn2->n;.
1ce21 20 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e if( u.ae.nByte>
1ce22 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1ce23 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1ce24 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1ce25 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 big;. }. MemSe
1ce26 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1ce27 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 MEM_Str);. if(
1ce28 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1ce29 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e ow(pOut, (int)u.
1ce2a 61 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 ae.nByte+2, pOut
1ce2b 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 ==pIn2) ){. g
1ce2c 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1ce2d 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 if( pOut!=pIn2
1ce2e 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ){. memcpy(p
1ce2f 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c Out->z, pIn2->z,
1ce30 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 pIn2->n);. }.
1ce31 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a memcpy(&pOut->z
1ce32 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d [pIn2->n], pIn1-
1ce33 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 >z, pIn1->n);.
1ce34 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 pOut->z[u.ae.nBy
1ce35 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d te] = 0;. pOut-
1ce36 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d >z[u.ae.nByte+1]
1ce37 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c = 0;. pOut->fl
1ce38 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
1ce39 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e . pOut->n = (in
1ce3a 74 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 t)u.ae.nByte;.
1ce3b 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f pOut->enc = enco
1ce3c 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d ding;. UPDATE_M
1ce3d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1ce3e 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1ce3f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 * Opcode: Add P1
1ce40 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1ce41 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 Add the value i
1ce42 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f n register P1 to
1ce43 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ce44 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1ce45 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1ce46 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1ce47 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1ce48 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1ce49 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1ce4a 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1ce4b 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 Multiply P1 P2 P
1ce4c 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 3 * *.**.**.** M
1ce4d 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 ultiply the valu
1ce4e 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1ce4f 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e by the value in
1ce50 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 register P2.**
1ce51 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 and store the re
1ce52 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
1ce53 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 P3..** If eithe
1ce54 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c r input is NULL,
1ce55 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1ce56 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ULL..*/./* Opcod
1ce57 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 e: Subtract P1 P
1ce58 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 2 P3 * *.**.** S
1ce59 75 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 ubtract the valu
1ce5a 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1ce5b 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 from the value
1ce5c 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
1ce5d 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
1ce5e 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1ce5f 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1ce60 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1ce61 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1ce62 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1ce63 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 ode: Divide P1 P
1ce64 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 2 P3 * *.**.** D
1ce65 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 ivide the value
1ce66 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 in register P1 b
1ce67 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 y the value in r
1ce68 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e egister P2.** an
1ce69 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 d store the resu
1ce6a 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 lt in register P
1ce6b 33 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 3 (P3=P2/P1). If
1ce6c 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a the value in .*
1ce6d 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 * register P1 is
1ce6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
1ce6f 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 result is NULL.
1ce70 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1ce71 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 is .** NULL, the
1ce72 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1ce73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .*/./* Opcode: R
1ce74 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50 emainder P1 P2 P
1ce75 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 3 * *.**.** Comp
1ce76 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65 ute the remainde
1ce77 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20 r after integer
1ce78 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 division of the
1ce79 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 value in.** regi
1ce7a 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 ster P1 by the v
1ce7b 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1ce7c 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68 P2 and store th
1ce7d 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 e result in P3.
1ce7e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1ce7f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 in register P2
1ce80 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75 is zero the resu
1ce81 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 lt is NULL..** I
1ce82 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 f either operand
1ce83 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1ce84 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1ce85 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 .case OP_Add:
1ce86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ce87 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c /* same as TK_PL
1ce88 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 US, in1, in2, ou
1ce89 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 t3 */.case OP_Su
1ce8a 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20 btract:
1ce8b 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1ce8c 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 TK_MINUS, in1, i
1ce8d 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1ce8e 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 OP_Multiply:
1ce8f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1ce90 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 me as TK_STAR, i
1ce91 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1ce92 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a .case OP_Divide:
1ce93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ce94 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c /* same as TK_SL
1ce95 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f ASH, in1, in2, o
1ce96 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 ut3 */.case OP_R
1ce97 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 emainder: {
1ce98 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1ce99 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e TK_REM, in1, in
1ce9a 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 2, out3 */.#if 0
1ce9b 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1ce9c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ce9d 75 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c u.af */. int fl
1ce9e 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d ags; /* Com
1ce9f 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 bined MEM_* flag
1cea0 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 s from both inpu
1cea1 74 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 ts */. i64 iA;
1cea2 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
1cea3 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 er value of left
1cea4 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 operand */. i6
1cea5 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 4 iB; /*
1cea6 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f Integer value o
1cea7 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 f right operand
1cea8 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 */. double rA;
1cea9 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
1ceaa 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 ue of left opera
1ceab 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 nd */. double r
1ceac 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 B; /* Real
1cead 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f value of right o
1ceae 70 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 perand */.#endif
1ceaf 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ceb0 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ceb1 2e 61 66 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d .af */.. pIn1 =
1ceb2 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b &aMem[pOp->p1];
1ceb3 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 . applyNumericA
1ceb4 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 ffinity(pIn1);.
1ceb5 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn2 = &aMem[pO
1ceb6 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 70 6c 79 4e p->p2];. applyN
1ceb7 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 umericAffinity(p
1ceb8 49 6e 32 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 In2);. pOut = &
1ceb9 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1ceba 20 75 2e 61 66 2e 66 6c 61 67 73 20 3d 20 70 49 u.af.flags = pI
1cebb 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 n1->flags | pIn2
1cebc 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 ->flags;. if( (
1cebd 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d u.af.flags & MEM
1cebe 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f _Null)!=0 ) goto
1cebf 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 arithmetic_resu
1cec0 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 lt_is_null;. if
1cec1 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1cec2 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d pIn2->flags & M
1cec3 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 EM_Int)==MEM_Int
1cec4 20 29 7b 0a 20 20 20 20 75 2e 61 66 2e 69 41 20 ){. u.af.iA
1cec5 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn1->u.i;.
1cec6 20 75 2e 61 66 2e 69 42 20 3d 20 70 49 6e 32 2d u.af.iB = pIn2-
1cec7 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68 >u.i;. switch
1cec8 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1cec9 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 . case OP_A
1ceca 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66 dd: u.af
1cecb 2e 69 42 20 2b 3d 20 75 2e 61 66 2e 69 41 3b 20 .iB += u.af.iA;
1cecc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1cecd 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 case OP_Subtr
1cece 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 act: u.af.iB
1cecf 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 -= u.af.iA;
1ced0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1ced1 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a ase OP_Multiply:
1ced2 20 20 20 20 75 2e 61 66 2e 69 42 20 2a 3d 20 75 u.af.iB *= u
1ced3 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72 .af.iA; br
1ced4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
1ced5 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 OP_Divide: {.
1ced6 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 if( u.af.iA
1ced7 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 ==0 ) goto arith
1ced8 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
1ced9 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a null;. /*
1ceda 20 44 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61 Dividing the la
1cedb 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e rgest possible n
1cedc 65 67 61 74 69 76 65 20 36 34 2d 62 69 74 20 69 egative 64-bit i
1cedd 6e 74 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62 nteger (1<<63) b
1cede 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 y. ** -1
1cedf 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 returns an integ
1cee0 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 er too large to
1cee1 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 store in a 64-bi
1cee2 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a t data-type. On.
1cee3 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 ** some
1cee4 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 architectures, t
1cee5 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f he value overflo
1cee6 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f ws to (1<<63). O
1cee7 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 n others,.
1cee8 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 ** a SIGFPE is
1cee9 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c issued. The fol
1ceea 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
1ceeb 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 normalizes this
1ceec 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 . ** beha
1ceed 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c vior so that all
1ceee 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62 architectures b
1ceef 65 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 ehave as if inte
1cef0 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f ger. ** o
1cef1 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 verflow occurred
1cef2 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1cef3 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 if( u.af.iA
1cef4 3d 3d 2d 31 20 26 26 20 75 2e 61 66 2e 69 42 3d ==-1 && u.af.iB=
1cef5 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 =SMALLEST_INT64
1cef6 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 ) u.af.iA = 1;.
1cef7 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 2f u.af.iB /
1cef8 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 = u.af.iA;.
1cef9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cefa 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
1cefb 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 {. if( u
1cefc 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f .af.iA==0 ) goto
1cefd 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 arithmetic_resu
1cefe 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 lt_is_null;.
1ceff 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d if( u.af.iA=
1cf00 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 =-1 ) u.af.iA =
1cf01 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 1;. u.af.
1cf02 69 42 20 25 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 iB %= u.af.iA;.
1cf03 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1cf04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1cf05 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 66 pOut->u.i = u.af
1cf06 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 .iB;. MemSetT
1cf07 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1cf08 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b M_Int);. }else{
1cf09 0a 20 20 20 20 75 2e 61 66 2e 72 41 20 3d 20 73 . u.af.rA = s
1cf0a 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
1cf0b 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 75 lue(pIn1);. u
1cf0c 2e 61 66 2e 72 42 20 3d 20 73 71 6c 69 74 65 33 .af.rB = sqlite3
1cf0d 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 VdbeRealValue(pI
1cf0e 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 n2);. switch(
1cf0f 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a pOp->opcode ){.
1cf10 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 case OP_Ad
1cf11 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66 2e d: u.af.
1cf12 72 42 20 2b 3d 20 75 2e 61 66 2e 72 41 3b 20 20 rB += u.af.rA;
1cf13 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1cf14 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 case OP_Subtra
1cf15 63 74 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2d ct: u.af.rB -
1cf16 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 = u.af.rA;
1cf17 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
1cf18 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 se OP_Multiply:
1cf19 20 20 20 75 2e 61 66 2e 72 42 20 2a 3d 20 75 2e u.af.rB *= u.
1cf1a 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 af.rA; bre
1cf1b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1cf1c 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 P_Divide: {.
1cf1d 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 /* (double)0
1cf1e 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 In case of SQLI
1cf1f 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
1cf20 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 _POINT... */.
1cf21 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 72 41 if( u.af.rA
1cf22 3d 3d 28 64 6f 75 62 6c 65 29 30 20 29 20 67 6f ==(double)0 ) go
1cf23 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 to arithmetic_re
1cf24 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 sult_is_null;.
1cf25 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2f 3d u.af.rB /=
1cf26 20 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20 20 20 u.af.rA;.
1cf27 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1cf28 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 . default:
1cf29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 {. u.af.i
1cf2a 41 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 41 A = (i64)u.af.rA
1cf2b 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 ;. u.af.i
1cf2c 42 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 42 B = (i64)u.af.rB
1cf2d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e ;. if( u.
1cf2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 af.iA==0 ) goto
1cf2f 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c arithmetic_resul
1cf30 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 t_is_null;.
1cf31 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d if( u.af.iA==
1cf32 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 -1 ) u.af.iA = 1
1cf33 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 ;. u.af.r
1cf34 42 20 3d 20 28 64 6f 75 62 6c 65 29 28 75 2e 61 B = (double)(u.a
1cf35 66 2e 69 42 20 25 20 75 2e 61 66 2e 69 41 29 3b f.iB % u.af.iA);
1cf36 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1cf37 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1cf38 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e if( sqlite3IsN
1cf39 61 4e 28 75 2e 61 66 2e 72 42 29 20 29 7b 0a 20 aN(u.af.rB) ){.
1cf3a 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68 6d goto arithm
1cf3b 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e etic_result_is_n
1cf3c 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ull;. }. p
1cf3d 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 66 2e 72 42 Out->r = u.af.rB
1cf3e 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1cf3f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 Flag(pOut, MEM_R
1cf40 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 75 eal);. if( (u
1cf41 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f .af.flags & MEM_
1cf42 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Real)==0 ){.
1cf43 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 sqlite3VdbeInt
1cf44 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 egerAffinity(pOu
1cf45 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 t);. }. }.
1cf46 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 break;..arithmet
1cf47 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1cf48 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 l:. sqlite3Vdbe
1cf49 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 MemSetNull(pOut)
1cf4a 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cf4b 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 Opcode: CollSeq
1cf4c 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 * * P4.**.** P4
1cf4d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1cf4e 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 a CollSeq struc
1cf4f 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 t. If the next c
1cf50 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 all to a user fu
1cf51 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 nction.** or agg
1cf52 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c regate calls sql
1cf53 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
1cf54 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 eq(), this colla
1cf55 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 tion sequence wi
1cf56 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 ll.** be returne
1cf57 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 d. This is used
1cf58 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 by the built-in
1cf59 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 min(), max() and
1cf5a 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e nullif().** fun
1cf5b 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ctions..**.** Th
1cf5c 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 e interface used
1cf5d 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e by the implemen
1cf5e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 tation of the af
1cf5f 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e orementioned fun
1cf60 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 ctions.** to ret
1cf61 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 rieve the collat
1cf62 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 ion sequence set
1cf63 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 by this opcode
1cf64 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
1cf65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e .** publicly, on
1cf66 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 ly to user funct
1cf67 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 ions defined in
1cf68 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 func.c..*/.case
1cf69 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 OP_CollSeq: {.
1cf6a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1cf6b 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 ype==P4_COLLSEQ
1cf6c 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1cf6d 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 * Opcode: Functi
1cf6e 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 on P1 P2 P3 P4 P
1cf6f 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 5.**.** Invoke a
1cf70 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 user function (
1cf71 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1cf72 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 to a Function st
1cf73 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 ructure that.**
1cf74 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 defines the func
1cf75 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 tion) with P5 ar
1cf76 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 guments taken fr
1cf77 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 om register P2 a
1cf78 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 nd.** successors
1cf79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 . The result of
1cf7a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 the function is
1cf7b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1cf7c 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 ter P3..** Regis
1cf7d 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 ter P3 must not
1cf7e 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 be one of the fu
1cf7f 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a nction inputs..*
1cf80 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d *.** P1 is a 32-
1cf81 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 bit bitmask indi
1cf82 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f cating whether o
1cf83 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d r not each argum
1cf84 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 ent to the .** f
1cf85 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 unction was dete
1cf86 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e rmined to be con
1cf87 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 stant at compile
1cf88 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 time. If the fi
1cf89 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
1cf8a 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 was constant the
1cf8b 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 n bit 0 of P1 is
1cf8c 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 set. This is us
1cf8d 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a ed to determine.
1cf8e 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 ** whether meta
1cf8f 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
1cf90 77 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 with a user func
1cf91 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 tion argument us
1cf92 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 ing the.** sqlit
1cf93 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 e3_set_auxdata()
1cf94 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 API may be safe
1cf95 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 ly retained unti
1cf96 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e l the next.** in
1cf97 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 vocation of this
1cf98 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 opcode..**.** S
1cf99 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 ee also: AggStep
1cf9a 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f and AggFinal.*/
1cf9b 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f .case OP_Functio
1cf9c 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c n: {.#if 0 /* l
1cf9d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1cf9e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a oved into u.ag *
1cf9f 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d /. int i;. Mem
1cfa0 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 *pArg;. sqlite
1cfa1 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 3_context ctx;.
1cfa2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1cfa3 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b *apVal;. int n;
1cfa4 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1cfa5 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cfa6 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 into u.ag */..
1cfa7 20 75 2e 61 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70 u.ag.n = pOp->p
1cfa8 35 3b 0a 20 20 75 2e 61 67 2e 61 70 56 61 6c 20 5;. u.ag.apVal
1cfa9 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 = p->apArg;. as
1cfaa 73 65 72 74 28 20 75 2e 61 67 2e 61 70 56 61 6c sert( u.ag.apVal
1cfab 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b || u.ag.n==0 );
1cfac 0a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 67 .. assert( u.ag
1cfad 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 .n==0 || (pOp->p
1cfae 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2>0 && pOp->p2+u
1cfaf 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 .ag.n<=p->nMem+1
1cfb0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1cfb1 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c Op->p3<pOp->p2 |
1cfb2 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e | pOp->p3>=pOp->
1cfb3 70 32 2b 75 2e 61 67 2e 6e 20 29 3b 0a 20 20 75 p2+u.ag.n );. u
1cfb4 2e 61 67 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d .ag.pArg = &aMem
1cfb5 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 [pOp->p2];. for
1cfb6 28 75 2e 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e (u.ag.i=0; u.ag.
1cfb7 69 3c 75 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 i<u.ag.n; u.ag.i
1cfb8 2b 2b 2c 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 ++, u.ag.pArg++)
1cfb9 7b 0a 20 20 20 20 75 2e 61 67 2e 61 70 56 61 6c {. u.ag.apVal
1cfba 5b 75 2e 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e [u.ag.i] = u.ag.
1cfbb 70 41 72 67 3b 0a 20 20 20 20 73 71 6c 69 74 65 pArg;. sqlite
1cfbc 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 3VdbeMemStoreTyp
1cfbd 65 28 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 e(u.ag.pArg);.
1cfbe 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1cfbf 28 70 4f 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 (pOp->p2, u.ag.p
1cfc0 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 Arg);. }.. ass
1cfc1 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1cfc2 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 ==P4_FUNCDEF ||
1cfc3 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1cfc4 56 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 VDBEFUNC );. if
1cfc5 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1cfc6 34 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 4_FUNCDEF ){.
1cfc7 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 u.ag.ctx.pFunc
1cfc8 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
1cfc9 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 . u.ag.ctx.pV
1cfca 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d dbeFunc = 0;. }
1cfcb 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 else{. u.ag.c
1cfcc 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28 tx.pVdbeFunc = (
1cfcd 56 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 VdbeFunc*)pOp->p
1cfce 34 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 4.pVdbeFunc;.
1cfcf 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 u.ag.ctx.pFunc
1cfd0 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 = u.ag.ctx.pVdbe
1cfd1 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d Func->pFunc;. }
1cfd2 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1cfd3 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
1cfd4 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1cfd5 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d Out = &aMem[pOp-
1cfd6 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78 >p3];. u.ag.ctx
1cfd7 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e .s.flags = MEM_N
1cfd8 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e ull;. u.ag.ctx.
1cfd9 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61 s.db = db;. u.a
1cfda 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 g.ctx.s.xDel = 0
1cfdb 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a ;. u.ag.ctx.s.z
1cfdc 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f Malloc = 0;.. /
1cfdd 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c * The output cel
1cfde 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 l may already ha
1cfdf 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f ve a buffer allo
1cfe0 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a cated. Move. **
1cfe1 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
1cfe2 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e u.ag.ctx.s so in
1cfe3 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 case the user-f
1cfe4 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a unction can use.
1cfe5 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 ** the already
1cfe6 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1cfe7 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c r instead of all
1cfe8 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e ocating a new on
1cfe9 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 e.. */. sqlite
1cfea 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 3VdbeMemMove(&u.
1cfeb 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b ag.ctx.s, pOut);
1cfec 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1cfed 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d g(&u.ag.ctx.s, M
1cfee 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61 EM_Null);.. u.a
1cfef 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 g.ctx.isError =
1cff0 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 0;. if( u.ag.ct
1cff1 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 x.pFunc->flags &
1cff2 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
1cff3 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 DCOLL ){. ass
1cff4 65 72 74 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a ert( pOp>aOp );.
1cff5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b assert( pOp[
1cff6 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 -1].p4type==P4_C
1cff7 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 OLLSEQ );. as
1cff8 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 sert( pOp[-1].op
1cff9 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 code==OP_CollSeq
1cffa 20 29 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 );. u.ag.ctx
1cffb 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d .pColl = pOp[-1]
1cffc 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 .p4.pColl;. }.
1cffd 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1cffe 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1cfff 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1d000 73 75 73 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 suse;. (*u.ag.c
1d001 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 tx.pFunc->xFunc)
1d002 28 26 75 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 (&u.ag.ctx, u.ag
1d003 2e 6e 2c 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b .n, u.ag.apVal);
1d004 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1d005 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 fetyOn(db) ){.
1d006 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d007 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74 Release(&u.ag.ct
1d008 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 x.s);. goto a
1d009 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1d00a 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 se;. }. if( db
1d00b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1d00c 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 {. /* Even th
1d00d 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 ough a malloc()
1d00e 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 has failed, the
1d00f 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1d010 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 f the. ** use
1d011 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 r function may h
1d012 61 76 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 ave called an sq
1d013 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 lite3_result_XXX
1d014 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 () function.
1d015 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76 ** to return a v
1d016 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 alue. The follow
1d017 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 ing call release
1d018 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a s any resources.
1d019 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 ** associate
1d01a 64 20 77 69 74 68 20 73 75 63 68 20 61 20 76 61 d with such a va
1d01b 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 lue.. **.
1d01c 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d ** Note: Maybe M
1d01d 65 6d 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75 emRelease() shou
1d01e 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 ld be called if
1d01f 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1d020 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 ). ** fails a
1d021 6c 73 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 lso (the if(...)
1d022 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 statement above
1d023 29 2e 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65 ). But if people
1d024 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 are. ** misu
1d025 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65 sing sqlite, the
1d026 79 20 68 61 76 65 20 62 69 67 67 65 72 20 70 72 y have bigger pr
1d027 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 oblems than a le
1d028 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 aked value..
1d029 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1d02a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e beMemRelease(&u.
1d02b 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 ag.ctx.s);. g
1d02c 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1d02d 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 . /* If any aux
1d02e 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 6e 63 iliary data func
1d02f 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 tions have been
1d030 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75 called by this u
1d031 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 ser function,.
1d032 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 ** immediately c
1d033 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 all the destruct
1d034 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 or for any non-s
1d035 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 tatic values..
1d036 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 */. if( u.ag.ct
1d037 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 x.pVdbeFunc ){.
1d038 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 sqlite3VdbeDe
1d039 6c 65 74 65 41 75 78 44 61 74 61 28 75 2e 61 67 leteAuxData(u.ag
1d03a 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 .ctx.pVdbeFunc,
1d03b 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f pOp->p1);. pO
1d03c 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 20 p->p4.pVdbeFunc
1d03d 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 = u.ag.ctx.pVdbe
1d03e 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 Func;. pOp->p
1d03f 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42 45 46 4type = P4_VDBEF
1d040 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 UNC;. }.. /* I
1d041 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 f the function r
1d042 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 eturned an error
1d043 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 , throw an excep
1d044 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e tion */. if( u.
1d045 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29 ag.ctx.isError )
1d046 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
1d047 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1d048 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1d049 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1d04a 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a (&u.ag.ctx.s));.
1d04b 20 20 20 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 rc = u.ag.ct
1d04c 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a x.isError;. }..
1d04d 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 /* Copy the re
1d04e 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 sult of the func
1d04f 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 tion into regist
1d050 65 72 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 er P3 */. sqlit
1d051 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1d052 64 69 6e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 ding(&u.ag.ctx.s
1d053 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 , encoding);. s
1d054 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1d055 65 28 70 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 e(pOut, &u.ag.ct
1d056 78 2e 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 x.s);. if( sqli
1d057 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1d058 28 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f (pOut) ){. go
1d059 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1d05a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1d05b 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b (pOp->p3, pOut);
1d05c 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1d05d 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 OBSIZE(pOut);.
1d05e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d05f 6f 64 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 ode: BitAnd P1 P
1d060 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 2 P3 * *.**.** T
1d061 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 ake the bit-wise
1d062 20 41 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 AND of the valu
1d063 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 es in register P
1d064 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 1 and P2 and.**
1d065 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1d066 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1d067 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e .** If either in
1d068 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 put is NULL, the
1d069 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1d06a 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 .*/./* Opcode: B
1d06b 69 74 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 itOr P1 P2 P3 *
1d06c 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 *.**.** Take the
1d06d 20 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 bit-wise OR of
1d06e 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 the values in re
1d06f 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 gister P1 and P2
1d070 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 and.** store th
1d071 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1d072 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1d073 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1d074 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1d075 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1d076 70 63 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 pcode: ShiftLeft
1d077 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1d078 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e .** Shift the in
1d079 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 teger value in r
1d07a 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 egister P2 to th
1d07b 65 20 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a e left by the.**
1d07c 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 number of bits
1d07d 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
1d07e 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 integer in regi
1d07f 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 ser P1..** Store
1d080 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1d081 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1d082 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1d083 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1d084 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1d085 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 * Opcode: ShiftR
1d086 69 67 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 ight P1 P2 P3 *
1d087 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 *.**.** Shift th
1d088 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 e integer value
1d089 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 in register P2 t
1d08a 6f 20 74 68 65 20 72 69 67 68 74 20 62 79 20 74 o the right by t
1d08b 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
1d08c 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62 bits specified b
1d08d 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e y the integer in
1d08e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1d08f 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c Store the resul
1d090 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1d091 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1d092 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1d093 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1d094 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 ..*/.case OP_Bit
1d095 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 And:
1d096 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d097 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 TK_BITAND, in1,
1d098 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1d099 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 e OP_BitOr:
1d09a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d09b 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 same as TK_BITOR
1d09c 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1d09d 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 */.case OP_Shif
1d09e 74 4c 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 tLeft:
1d09f 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d0a0 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 K_LSHIFT, in1, i
1d0a1 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1d0a2 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 OP_ShiftRight:
1d0a3 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 { /* s
1d0a4 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 ame as TK_RSHIFT
1d0a5 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1d0a6 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1d0a7 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1d0a8 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f ved into u.ah */
1d0a9 0a 20 20 69 36 34 20 61 3b 0a 20 20 69 36 34 20 . i64 a;. i64
1d0aa 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 b;.#endif /* loc
1d0ab 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d0ac 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a ed into u.ah */.
1d0ad 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b . pIn1 = &aMem[
1d0ae 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 pOp->p1];. pIn2
1d0af 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 = &aMem[pOp->p2
1d0b0 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 ];. pOut = &aMe
1d0b1 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 m[pOp->p3];. if
1d0b2 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c ( (pIn1->flags |
1d0b3 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 pIn2->flags) &
1d0b4 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1d0b5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1d0b6 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 tNull(pOut);.
1d0b7 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e break;. }. u.
1d0b8 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 56 64 ah.a = sqlite3Vd
1d0b9 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 beIntValue(pIn2)
1d0ba 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c ;. u.ah.b = sql
1d0bb 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1d0bc 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 (pIn1);. switch
1d0bd 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1d0be 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 . case OP_Bit
1d0bf 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 2e 61 And: u.ah.a
1d0c0 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 &= u.ah.b;
1d0c1 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1d0c2 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 OP_BitOr:
1d0c3 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62 u.ah.a |= u.ah.b
1d0c4 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1d0c5 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 case OP_ShiftLe
1d0c6 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d ft: u.ah.a <<=
1d0c7 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 65 61 u.ah.b; brea
1d0c8 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 k;. default:
1d0c9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
1d0ca 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69 code==OP_ShiftRi
1d0cb 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ght );.
1d0cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d0cd 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e u.ah.a >>= u.ah.
1d0ce 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d b; break;. }
1d0cf 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 . pOut->u.i = u
1d0d0 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54 .ah.a;. MemSetT
1d0d1 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1d0d2 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1d0d3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 .}../* Opcode: A
1d0d4 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a ddImm P1 P2 * *
1d0d5 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 *.** .** Add th
1d0d6 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f e constant P2 to
1d0d7 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1d0d8 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 gister P1..** Th
1d0d9 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 e result is alwa
1d0da 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a ys an integer..*
1d0db 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e *.** To force an
1d0dc 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 65 y register to be
1d0dd 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 an integer, jus
1d0de 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 t add 0..*/.case
1d0df 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 OP_AddImm: {
1d0e0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 /* in1
1d0e1 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1d0e2 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71 m[pOp->p1];. sq
1d0e3 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
1d0e4 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 gerify(pIn1);.
1d0e5 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 pIn1->u.i += pOp
1d0e6 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d ->p2;. break;.}
1d0e7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 ../* Opcode: Mus
1d0e8 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a tBeInt P1 P2 * *
1d0e9 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 *.** .** Force
1d0ea 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1d0eb 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 ister P1 to be a
1d0ec 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 n integer. If t
1d0ed 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 he value.** in P
1d0ee 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 1 is not an inte
1d0ef 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 ger and cannot b
1d0f0 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f e converted into
1d0f1 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 an integer.** w
1d0f2 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 ithout data loss
1d0f3 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 , then jump imme
1d0f4 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f diately to P2, o
1d0f5 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 r if P2==0.** ra
1d0f6 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 ise an SQLITE_MI
1d0f7 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e SMATCH exception
1d0f8 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 ..*/.case OP_Mus
1d0f9 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 tBeInt: {
1d0fa 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1d0fb 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 1 */. pIn1 = &a
1d0fc 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1d0fd 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 applyAffinity(pI
1d0fe 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e n1, SQLITE_AFF_N
1d0ff 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 UMERIC, encoding
1d100 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e );. if( (pIn1->
1d101 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 flags & MEM_Int)
1d102 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
1d103 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 Op->p2==0 ){.
1d104 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d rc = SQLITE_M
1d105 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 ISMATCH;. g
1d106 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1d107 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 _error;. }els
1d108 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f e{. pc = pO
1d109 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d p->p2 - 1;. }
1d10a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 . }else{. Me
1d10b 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e mSetTypeFlag(pIn
1d10c 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 1, MEM_Int);. }
1d10d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d10e 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 Opcode: RealAffi
1d10f 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a nity P1 * * * *.
1d110 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 **.** If registe
1d111 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e r P1 holds an in
1d112 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 teger convert it
1d113 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 to a real value
1d114 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1d115 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e ode is used when
1d116 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f extracting info
1d117 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 rmation from a c
1d118 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 olumn that.** ha
1d119 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e s REAL affinity.
1d11a 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 Such column va
1d11b 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 lues may still b
1d11c 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 e stored as.** i
1d11d 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 ntegers, for spa
1d11e 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 ce efficiency, b
1d11f 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74 ut after extract
1d120 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d ion we want them
1d121 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 .** to have only
1d122 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a a real value..*
1d123 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 /.case OP_RealAf
1d124 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 finity: {
1d125 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
1d126 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 1 */. pIn1 = &a
1d127 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1d128 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1d129 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
1d12a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1d12b 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 ealify(pIn1);.
1d12c 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 }. break;.}..#i
1d12d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1d12e 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 T_CAST./* Opcode
1d12f 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 : ToText P1 * *
1d130 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 * *.**.** Force
1d131 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1d132 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 ister P1 to be t
1d133 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 ext..** If the v
1d134 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c alue is numeric,
1d135 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1d136 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 string using th
1d137 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 e.** equivalent
1d138 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c of printf(). Bl
1d139 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e ob values are un
1d13a 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 changed and.** a
1d13b 72 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69 re afterwards si
1d13c 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 mply interpreted
1d13d 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 as text..**.**
1d13e 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 A NULL value is
1d13f 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 not changed by t
1d140 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 his routine. It
1d141 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a remains NULL..*
1d142 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 /.case OP_ToText
1d143 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1d144 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d145 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 TK_TO_TEXT, in1
1d146 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1d147 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 m[pOp->p1];. if
1d148 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
1d149 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b MEM_Null ) break
1d14a 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f ;. assert( MEM_
1d14b 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e Str==(MEM_Blob>>
1d14c 33 29 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 3) );. pIn1->fl
1d14d 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c ags |= (pIn1->fl
1d14e 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 ags&MEM_Blob)>>3
1d14f 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 ;. applyAffinit
1d150 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 y(pIn1, SQLITE_A
1d151 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e FF_TEXT, encodin
1d152 67 29 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e g);. rc = Expan
1d153 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 dBlob(pIn1);. a
1d154 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
1d155 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 gs & MEM_Str ||
1d156 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1d157 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 );. pIn1->flag
1d158 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d s &= ~(MEM_Int|M
1d159 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 EM_Real|MEM_Blob
1d15a 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 |MEM_Zero);. UP
1d15b 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1d15c 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b E(pIn1);. break
1d15d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d15e 54 6f 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 ToBlob P1 * * *
1d15f 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1d160 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d161 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 42 ter P1 to be a B
1d162 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 LOB..** If the v
1d163 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c alue is numeric,
1d164 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1d165 20 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a string first..*
1d166 2a 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69 * Strings are si
1d167 6d 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74 mply reinterpret
1d168 65 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68 ed as blobs with
1d169 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f no change.** to
1d16a 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
1d16b 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 data..**.** A NU
1d16c 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1d16d 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1d16e 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1d16f 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1d170 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 se OP_ToBlob: {
1d171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d172 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
1d173 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 O_BLOB, in1 */.
1d174 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn1 = &aMem[pO
1d175 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 49 p->p1];. if( pI
1d176 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1d177 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 Null ) break;.
1d178 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1d179 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 & MEM_Blob)==0
1d17a 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 ){. applyAffi
1d17b 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 nity(pIn1, SQLIT
1d17c 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f E_AFF_TEXT, enco
1d17d 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72 ding);. asser
1d17e 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 t( pIn1->flags &
1d17f 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e MEM_Str || db->
1d180 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1d181 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1d182 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f ag(pIn1, MEM_Blo
1d183 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 b);. }else{.
1d184 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 pIn1->flags &=
1d185 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e ~(MEM_TypeMask&~
1d186 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 MEM_Blob);. }.
1d187 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1d188 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 SIZE(pIn1);. br
1d189 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d18a 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 e: ToNumeric P1
1d18b 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f * * * *.**.** Fo
1d18c 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1d18d 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1d18e 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68 be numeric (eith
1d18f 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 er an.** integer
1d190 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 or a floating-p
1d191 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a oint number.).**
1d192 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 If the value is
1d193 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 text or blob, t
1d194 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 ry to convert it
1d195 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65 to an using the
1d196 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f .** equivalent o
1d197 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 f atoi() or atof
1d198 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 () and store 0 i
1d199 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 f no such conver
1d19a 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 sion .** is poss
1d19b 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 ible..**.** A NU
1d19c 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1d19d 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1d19e 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1d19f 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1d1a0 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a se OP_ToNumeric:
1d1a1 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1d1a2 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d1a3 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e K_TO_NUMERIC, in
1d1a4 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 1 */. pIn1 = &a
1d1a5 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1d1a6 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1d1a7 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d & (MEM_Null|MEM
1d1a8 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d _Int|MEM_Real))=
1d1a9 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1d1aa 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 3VdbeMemNumerify
1d1ab 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 (pIn1);. }. br
1d1ac 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1d1ad 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
1d1ae 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a T */../* Opcode:
1d1af 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 ToInt P1 * * *
1d1b0 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1d1b1 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d1b2 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 6e 74 ter P1 be an int
1d1b3 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65 eger. If.** The
1d1b4 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e value is curren
1d1b5 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 tly a real numbe
1d1b6 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61 63 r, drop its frac
1d1b7 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 tional part..**
1d1b8 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1d1b9 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 text or blob, tr
1d1ba 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 y to convert it
1d1bb 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73 to an integer us
1d1bc 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 ing the.** equiv
1d1bd 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 alent of atoi()
1d1be 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e and store 0 if n
1d1bf 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f o such conversio
1d1c0 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a n is possible..*
1d1c1 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 *.** A NULL valu
1d1c2 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 e is not changed
1d1c3 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
1d1c4 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 . It remains NU
1d1c5 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 LL..*/.case OP_T
1d1c6 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 oInt: {
1d1c7 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d1c8 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 as TK_TO_INT, i
1d1c9 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 n1 */. pIn1 = &
1d1ca 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1d1cb 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1d1cc 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
1d1cd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1d1ce 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1d1cf 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 (pIn1);. }. br
1d1d0 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 eak;.}..#ifndef
1d1d1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 SQLITE_OMIT_CAST
1d1d2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 ./* Opcode: ToRe
1d1d3 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a al P1 * * * *.**
1d1d4 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
1d1d5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1d1d6 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 P1 to be a float
1d1d7 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 ing point number
1d1d8 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 ..** If The valu
1d1d9 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 e is currently a
1d1da 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 n integer, conve
1d1db 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 rt it..** If the
1d1dc 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f value is text o
1d1dd 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 r blob, try to c
1d1de 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 onvert it to an
1d1df 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 integer using th
1d1e0 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 e.** equivalent
1d1e1 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 of atoi() and st
1d1e2 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 ore 0.0 if no su
1d1e3 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 ch conversion is
1d1e4 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a possible..**.**
1d1e5 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1d1e6 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1d1e7 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1d1e8 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1d1e9 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 */.case OP_ToRea
1d1ea 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 l: {
1d1eb 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1d1ec 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 TK_TO_REAL, in1
1d1ed 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d */. pIn1 = &aM
1d1ee 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 em[pOp->p1];. i
1d1ef 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1d1f0 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
1d1f1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1d1f2 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 eMemRealify(pIn1
1d1f3 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1d1f4 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1d1f5 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a TE_OMIT_CAST */.
1d1f6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 ./* Opcode: Lt P
1d1f7 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1d1f8 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
1d1f9 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
1d1fa 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49 er P1 and P3. I
1d1fb 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31 f reg(P3)<reg(P1
1d1fc 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 ) then.** jump t
1d1fd 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a o address P2. .
1d1fe 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
1d1ff 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 ITE_JUMPIFNULL b
1d200 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 it of P5 is set
1d201 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50 and either reg(P
1d202 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 1) or.** reg(P3)
1d203 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 is NULL then ta
1d204 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 ke the jump. If
1d205 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
1d206 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 IFNULL .** bit i
1d207 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c s clear then fal
1d208 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72 l thru if either
1d209 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d20a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
1d20b 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 TE_AFF_MASK port
1d20c 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62 ion of P5 must b
1d20d 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 e an affinity ch
1d20e 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c aracter -.** SQL
1d20f 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 ITE_AFF_TEXT, SQ
1d210 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1d211 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
1d212 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 An attempt is ma
1d213 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 de .** to coerce
1d214 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63 both inputs acc
1d215 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61 ording to this a
1d216 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 ffinity before t
1d217 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e he.** comparison
1d218 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65 is made. If the
1d219 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
1d21a 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e is 0x00, then n
1d21b 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 umeric.** affini
1d21c 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 ty is used. Note
1d21d 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 that the affini
1d21e 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 ty conversions a
1d21f 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 re stored.** bac
1d220 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 k into the input
1d221 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d222 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f d P3. So this o
1d223 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a pcode can cause.
1d224 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68 ** persistent ch
1d225 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65 anges to registe
1d226 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a rs P1 and P3..**
1d227 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e .** Once any con
1d228 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 versions have ta
1d229 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e ken place, and n
1d22a 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 either value is
1d22b 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 NULL, .** the va
1d22c 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 lues are compare
1d22d 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 d. If both value
1d22e 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e s are blobs then
1d22f 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 memcmp() is.**
1d230 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
1d231 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 e the results of
1d232 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e the comparison.
1d233 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
1d234 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 .** are text, th
1d235 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 en the appropria
1d236 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e te collating fun
1d237 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 ction specified
1d238 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65 in.** P4 is use
1d239 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 d to do the comp
1d23a 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 arison. If P4 i
1d23b 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 s not specified
1d23c 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 then.** memcmp()
1d23d 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 is used to comp
1d23e 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e are text string.
1d23f 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
1d240 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c are.** numeric,
1d241 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 then a numeric
1d242 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73 comparison is us
1d243 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76 ed. If the two v
1d244 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 alues.** are of
1d245 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c different types,
1d246 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 then numbers ar
1d247 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
1d248 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 s than.** string
1d249 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 s and strings ar
1d24a 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
1d24b 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a s than blobs..**
1d24c 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 .** If the SQLIT
1d24d 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66 E_STOREP2 bit of
1d24e 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e P5 is set, then
1d24f 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 do not jump. I
1d250 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 nstead,.** store
1d251 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c a boolean resul
1d252 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20 t (either 0, or
1d253 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 1, or NULL) in r
1d254 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f egister P2..*/./
1d255 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 * Opcode: Ne P1
1d256 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1d257 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 * This works jus
1d258 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 t like the Lt op
1d259 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 code except that
1d25a 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b the jump is tak
1d25b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 en if.** the ope
1d25c 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 rands in registe
1d25d 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 rs P1 and P3 are
1d25e 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65 not equal. See
1d25f 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 the Lt opcode f
1d260 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c or.** additional
1d261 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
1d262 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 .** If SQLITE_NU
1d263 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50 LLEQ is set in P
1d264 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 5 then the resul
1d265 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 t of comparison
1d266 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 is always either
1d267 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73 .** true or fals
1d268 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e e and is never N
1d269 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70 ULL. If both op
1d26a 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 erands are NULL
1d26b 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a then the result.
1d26c 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e ** of comparison
1d26d 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20 65 is false. If e
1d26e 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d26f 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 NULL then the r
1d270 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a esult is true..*
1d271 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 65 * If neither ope
1d272 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 rand is NULL the
1d273 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 the result is t
1d274 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f he same as it wo
1d275 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65 uld be if.** the
1d276 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 SQLITE_NULLEQ f
1d277 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64 lag were omitted
1d278 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 from P5..*/./*
1d279 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32 Opcode: Eq P1 P2
1d27a 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1d27b 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
1d27c 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
1d27d 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
1d27e 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1d27f 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 if.** the opera
1d280 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 nds in registers
1d281 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65 P1 and P3 are e
1d282 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 qual..** See the
1d283 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 Lt opcode for a
1d284 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1d285 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ation..**.** If
1d286 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 SQLITE_NULLEQ is
1d287 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20 set in P5 then
1d288 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f the result of co
1d289 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 mparison is alwa
1d28a 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 ys either.** tru
1d28b 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69 e or false and i
1d28c 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 s never NULL. I
1d28d 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 f both operands
1d28e 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 are NULL then th
1d28f 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 e result.** of c
1d290 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75 omparison is tru
1d291 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70 e. If either op
1d292 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 erand is NULL th
1d293 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1d294 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65 false..** If ne
1d295 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d296 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65 NULL the the re
1d297 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65 sult is the same
1d298 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 as it would be
1d299 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 if.** the SQLITE
1d29a 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 _NULLEQ flag wer
1d29b 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 e omitted from P
1d29c 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 5..*/./* Opcode:
1d29d 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 Le P1 P2 P3 P4
1d29e 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1d29f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 rks just like th
1d2a0 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 e Lt opcode exce
1d2a1 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 pt that the jump
1d2a2 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 is taken if.**
1d2a3 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d2a4 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65 egister P3 is le
1d2a5 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1d2a6 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 to the content
1d2a7 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 of.** register P
1d2a8 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 1. See the Lt o
1d2a9 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
1d2aa 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1d2ab 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1d2ac 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Gt P1 P2 P3 P4 P
1d2ad 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
1d2ae 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
1d2af 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
1d2b0 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
1d2b1 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
1d2b2 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1d2b3 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 gister P3 is gre
1d2b4 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f ater than the co
1d2b5 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 ntent of.** regi
1d2b6 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 ster P1. See th
1d2b7 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 e Lt opcode for
1d2b8 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
1d2b9 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 mation..*/./* Op
1d2ba 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50 code: Ge P1 P2 P
1d2bb 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 3 P4 P5.**.** Th
1d2bc 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 is works just li
1d2bd 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ke the Lt opcode
1d2be 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
1d2bf 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 jump is taken i
1d2c0 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 f.** the content
1d2c1 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1d2c2 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1d2c3 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1d2c4 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 content of.** re
1d2c5 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 gister P1. See
1d2c6 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1d2c7 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1d2c8 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 ormation..*/.cas
1d2c9 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20 e OP_Eq:
1d2ca 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1d2cb 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 s TK_EQ, jump, i
1d2cc 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 n1, in3 */.case
1d2cd 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20 OP_Ne:
1d2ce 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d2cf 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 TK_NE, jump, in1
1d2d0 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 , in3 */.case OP
1d2d1 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 _Lt:
1d2d2 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1d2d3 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 _LT, jump, in1,
1d2d4 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c in3 */.case OP_L
1d2d5 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e:
1d2d6 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
1d2d7 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e E, jump, in1, in
1d2d8 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 3 */.case OP_Gt:
1d2d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d2da 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c * same as TK_GT,
1d2db 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1d2dc 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b */.case OP_Ge: {
1d2dd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d2de 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a same as TK_GE, j
1d2df 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1d2e0 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d2e1 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d2e2 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20 into u.ai */.
1d2e3 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 int res;
1d2e4 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
1d2e5 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1d2e6 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20 of pIn1 against
1d2e7 70 49 6e 33 20 2a 2f 0a 20 20 63 68 61 72 20 61 pIn3 */. char a
1d2e8 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a ffinity; /*
1d2e9 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 Affinity to use
1d2ea 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 for comparison
1d2eb 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1d2ec 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d2ed 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a ed into u.ai */.
1d2ee 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b . pIn1 = &aMem[
1d2ef 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 pOp->p1];. pIn3
1d2f0 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1d2f1 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e ];. if( (pIn1->
1d2f2 66 6c 61 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c flags | pIn3->fl
1d2f3 61 67 73 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b ags)&MEM_Null ){
1d2f4 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62 . /* One or b
1d2f5 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 oth operands are
1d2f6 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28 NULL */. if(
1d2f7 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 pOp->p5 & SQLIT
1d2f8 45 5f 4e 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20 E_NULLEQ ){.
1d2f9 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e /* If SQLITE_N
1d2fa 55 4c 4c 45 51 20 69 73 20 73 65 74 20 28 77 68 ULLEQ is set (wh
1d2fb 69 63 68 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 ich will only ha
1d2fc 70 70 65 6e 20 69 66 20 74 68 65 20 6f 70 65 72 ppen if the oper
1d2fd 61 74 6f 72 20 69 73 0a 20 20 20 20 20 20 2a 2a ator is. **
1d2fe 20 4f 50 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29 OP_Eq or OP_Ne)
1d2ff 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a then take the j
1d300 75 6d 70 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e ump or not depen
1d301 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a ding on whether.
1d302 20 20 20 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 ** or not
1d303 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 both operands ar
1d304 65 20 6e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f e null.. */
1d305 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1d306 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 Op->opcode==OP_E
1d307 71 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 q || pOp->opcode
1d308 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 ==OP_Ne );.
1d309 20 75 2e 61 69 2e 72 65 73 20 3d 20 28 70 49 6e u.ai.res = (pIn
1d30a 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 33 2d 1->flags & pIn3-
1d30b 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1d30c 6c 29 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 l)==0;. }else
1d30d 7b 0a 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 {. /* SQLIT
1d30e 45 5f 4e 55 4c 4c 45 51 20 69 73 20 63 6c 65 61 E_NULLEQ is clea
1d30f 72 20 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f r and at least o
1d310 6e 65 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 ne operand is NU
1d311 4c 4c 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 LL,. ** the
1d312 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 n the result is
1d313 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20 always NULL..
1d314 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 ** The jump i
1d315 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 s taken if the S
1d316 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1d317 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 bit is set..
1d318 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
1d319 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 pOp->p5 & SQLITE
1d31a 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 _STOREP2 ){.
1d31b 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d pOut = &aMem
1d31c 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 [pOp->p2];.
1d31d 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1d31e 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pOut, MEM_Null
1d31f 29 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 );. REGIS
1d320 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1d321 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 2, pOut);.
1d322 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 }else if( pOp->p
1d323 35 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 5 & SQLITE_JUMPI
1d324 46 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 FNULL ){.
1d325 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1d326 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1d327 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 reak;. }. }e
1d328 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 lse{. /* Neit
1d329 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e her operand is N
1d32a 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61 ULL. Do a compa
1d32b 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e rison. */. u.
1d32c 61 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f ai.affinity = pO
1d32d 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41 p->p5 & SQLITE_A
1d32e 46 46 5f 4d 41 53 4b 3b 0a 20 20 20 20 69 66 28 FF_MASK;. if(
1d32f 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 29 u.ai.affinity )
1d330 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 {. applyAff
1d331 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 69 inity(pIn1, u.ai
1d332 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 .affinity, encod
1d333 69 6e 67 29 3b 0a 20 20 20 20 20 20 61 70 70 6c ing);. appl
1d334 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 yAffinity(pIn3,
1d335 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 u.ai.affinity, e
1d336 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 ncoding);.
1d337 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1d338 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d iled ) goto no_m
1d339 65 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 em;. }.. a
1d33a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1d33b 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c pe==P4_COLLSEQ |
1d33c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d | pOp->p4.pColl=
1d33d 3d 30 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 =0 );. Expand
1d33e 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 20 20 Blob(pIn1);.
1d33f 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 ExpandBlob(pIn3)
1d340 3b 0a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d ;. u.ai.res =
1d341 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 sqlite3MemCompa
1d342 72 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 re(pIn3, pIn1, p
1d343 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 Op->p4.pColl);.
1d344 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 }. switch( pOp
1d345 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 ->opcode ){.
1d346 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 75 case OP_Eq: u
1d347 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 .ai.res = u.ai.r
1d348 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b es==0; break
1d349 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 ;. case OP_Ne
1d34a 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 : u.ai.res =
1d34b 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20 20 u.ai.res!=0;
1d34c 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 break;. case
1d34d 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69 2e OP_Lt: u.ai.
1d34e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 30 res = u.ai.res<0
1d34f 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 ; break;.
1d350 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 case OP_Le:
1d351 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 u.ai.res = u.ai
1d352 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 .res<=0; bre
1d353 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f ak;. case OP_
1d354 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 Gt: u.ai.res
1d355 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20 20 = u.ai.res>0;
1d356 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 break;. de
1d357 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e 61 fault: u.a
1d358 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 i.res = u.ai.res
1d359 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a >=0; break;.
1d35a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e }.. if( pOp->
1d35b 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 p5 & SQLITE_STOR
1d35c 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 EP2 ){. pOut
1d35d 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d = &aMem[pOp->p2]
1d35e 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1d35f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1d360 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 nt);. pOut->u
1d361 2e 69 20 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20 .i = u.ai.res;.
1d362 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1d363 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 E(pOp->p2, pOut)
1d364 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e ;. }else if( u.
1d365 61 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 ai.res ){. pc
1d366 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 = pOp->p2-1;.
1d367 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d368 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 Opcode: Permuta
1d369 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a tion * * * P4 *.
1d36a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 **.** Set the pe
1d36b 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62 rmutation used b
1d36c 79 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 y the OP_Compare
1d36d 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 operator to be
1d36e 74 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 the array.** of
1d36f 69 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a integers in P4..
1d370 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 **.** The permut
1d371 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 ation is only va
1d372 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 lid until the ne
1d373 78 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f xt OP_Permutatio
1d374 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a n, OP_Compare,.*
1d375 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 * OP_Halt, or OP
1d376 5f 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 _ResultRow. Typ
1d377 69 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 ically the OP_Pe
1d378 72 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 rmutation should
1d379 20 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 occur.** immedi
1d37a 61 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 ately prior to t
1d37b 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a he OP_Compare..*
1d37c 2f 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 /.case OP_Permut
1d37d 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 ation: {. asser
1d37e 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1d37f 50 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 P4_INTARRAY );.
1d380 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1d381 2e 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 .ai );. aPermut
1d382 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a e = pOp->p4.ai;.
1d383 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d384 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 pcode: Compare P
1d385 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1d386 2a 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 ** Compare to ve
1d387 63 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 ctors of registe
1d388 72 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 rs in reg(P1)..r
1d389 65 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c eg(P1+P3-1) (all
1d38a 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 this.** one "A"
1d38b 29 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 ) and in reg(P2)
1d38c 2e 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 ..reg(P2+P3-1) (
1d38d 22 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 "B"). Save the
1d38e 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 result of.** the
1d38f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 comparison for
1d390 75 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 use by the next
1d391 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 OP_Jump instruct
1d392 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 ..**.** P4 is a
1d393 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1d394 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 e that defines c
1d395 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1d396 65 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f es and sort.** o
1d397 72 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f rders for the co
1d398 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 mparison. The p
1d399 65 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 ermutation appli
1d39a 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a es to registers.
1d39b 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 ** only. The Ke
1d39c 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 yInfo elements a
1d39d 72 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 re used sequenti
1d39e 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ally..**.** The
1d39f 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 comparison is a
1d3a0 73 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c sort comparison,
1d3a1 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 so NULLs compar
1d3a2 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c e equal,.** NULL
1d3a3 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 s are less than
1d3a4 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 numbers, numbers
1d3a5 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 are less than s
1d3a6 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 trings,.** and s
1d3a7 74 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 trings are less
1d3a8 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 than blobs..*/.c
1d3a9 61 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 ase OP_Compare:
1d3aa 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1d3ab 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d3ac 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 d into u.aj */.
1d3ad 20 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b int n;. int i;
1d3ae 0a 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 . int p1;. int
1d3af 20 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 p2;. const Key
1d3b0 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a Info *pKeyInfo;.
1d3b1 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c int idx;. Col
1d3b2 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 lSeq *pColl;
1d3b3 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
1d3b4 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 uence to use on
1d3b5 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 this term */. i
1d3b6 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 nt bRev;
1d3b7 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 /* True for DE
1d3b8 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 SCENDING sort or
1d3b9 64 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a der */.#endif /*
1d3ba 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d3bb 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a moved into u.aj
1d3bc 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 */.. u.aj.n =
1d3bd 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e pOp->p3;. u.aj.
1d3be 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e pKeyInfo = pOp->
1d3bf 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 p4.pKeyInfo;. a
1d3c0 73 73 65 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 ssert( u.aj.n>0
1d3c1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 );. assert( u.a
1d3c2 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b j.pKeyInfo!=0 );
1d3c3 0a 20 20 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 . u.aj.p1 = pOp
1d3c4 2d 3e 70 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 ->p1;. u.aj.p2
1d3c5 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53 = pOp->p2;.#if S
1d3c6 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 QLITE_DEBUG. if
1d3c7 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 ( aPermute ){.
1d3c8 20 20 69 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b int k, mx = 0;
1d3c9 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c . for(k=0; k<
1d3ca 75 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 u.aj.n; k++) if(
1d3cb 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 aPermute[k]>mx
1d3cc 29 20 6d 78 20 3d 20 61 50 65 72 6d 75 74 65 5b ) mx = aPermute[
1d3cd 6b 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 k];. assert(
1d3ce 75 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 u.aj.p1>0 && u.a
1d3cf 6a 2e 70 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d j.p1+mx<=p->nMem
1d3d0 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +1 );. assert
1d3d1 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 ( u.aj.p2>0 && u
1d3d2 2e 61 6a 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d .aj.p2+mx<=p->nM
1d3d3 65 6d 2b 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b em+1 );. }else{
1d3d4 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 . assert( u.a
1d3d5 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 j.p1>0 && u.aj.p
1d3d6 31 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 1+u.aj.n<=p->nMe
1d3d7 6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 m+1 );. asser
1d3d8 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 t( u.aj.p2>0 &&
1d3d9 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d u.aj.p2+u.aj.n<=
1d3da 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d p->nMem+1 );. }
1d3db 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1d3dc 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72 E_DEBUG */. for
1d3dd 28 75 2e 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e (u.aj.i=0; u.aj.
1d3de 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 i<u.aj.n; u.aj.i
1d3df 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64 ++){. u.aj.id
1d3e0 78 20 3d 20 61 50 65 72 6d 75 74 65 20 3f 20 61 x = aPermute ? a
1d3e1 50 65 72 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 Permute[u.aj.i]
1d3e2 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45 : u.aj.i;. RE
1d3e3 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 GISTER_TRACE(u.a
1d3e4 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 j.p1+u.aj.idx, &
1d3e5 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 aMem[u.aj.p1+u.a
1d3e6 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 j.idx]);. REG
1d3e7 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a ISTER_TRACE(u.aj
1d3e8 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61 .p2+u.aj.idx, &a
1d3e9 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a Mem[u.aj.p2+u.aj
1d3ea 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 .idx]);. asse
1d3eb 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61 6a 2e rt( u.aj.i<u.aj.
1d3ec 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1d3ed 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70 43 6f );. u.aj.pCo
1d3ee 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e ll = u.aj.pKeyIn
1d3ef 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a 2e 69 fo->aColl[u.aj.i
1d3f0 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52 65 76 ];. u.aj.bRev
1d3f1 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f = u.aj.pKeyInfo
1d3f2 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 2e 61 ->aSortOrder[u.a
1d3f3 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61 j.i];. iCompa
1d3f4 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 re = sqlite3MemC
1d3f5 6f 6d 70 61 72 65 28 26 61 4d 65 6d 5b 75 2e 61 ompare(&aMem[u.a
1d3f6 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 j.p1+u.aj.idx],
1d3f7 26 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e &aMem[u.aj.p2+u.
1d3f8 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43 aj.idx], u.aj.pC
1d3f9 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 oll);. if( iC
1d3fa 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 ompare ){.
1d3fb 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20 if( u.aj.bRev )
1d3fc 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d iCompare = -iCom
1d3fd 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 pare;. brea
1d3fe 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 k;. }. }. a
1d3ff 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 Permute = 0;. b
1d400 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d401 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 de: Jump P1 P2 P
1d402 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1d403 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 to the instruct
1d404 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 ion at address P
1d405 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 1, P2, or P3 dep
1d406 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
1d407 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 r.** in the most
1d408 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 recent OP_Compa
1d409 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 re instruction t
1d40a 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 he P1 vector was
1d40b 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 less than.** eq
1d40c 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
1d40d 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 er than the P2 v
1d40e 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 ector, respectiv
1d40f 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ely..*/.case OP_
1d410 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 Jump: {
1d411 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1d412 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 if( iCompare<0
1d413 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1d414 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p1 - 1;. }else
1d415 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 if( iCompare==0
1d416 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1d417 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1d418 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d e{. pc = pOp-
1d419 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p3 - 1;. }. b
1d41a 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d41b 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 de: And P1 P2 P3
1d41c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1d41d 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 the logical AND
1d41e 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1d41f 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d420 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 d P2 and.** writ
1d421 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
1d422 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
1d423 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1d424 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 1 or P2 is 0 (fa
1d425 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 lse) then the re
1d426 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 sult is 0 even i
1d427 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 f.** the other i
1d428 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 nput is NULL. A
1d429 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f NULL and true o
1d42a 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 r two NULLs give
1d42b 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 .** a NULL outpu
1d42c 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a t..*/./* Opcode:
1d42d 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a Or P1 P2 P3 * *
1d42e 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
1d42f 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 logical OR of th
1d430 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1d431 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1d432 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1d433 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 answer in regist
1d434 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1d435 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1d436 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 is nonzero (true
1d437 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1d438 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a t is 1 (true).**
1d439 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 even if the oth
1d43a 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1d43b 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 . A NULL and fa
1d43c 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 lse or two NULLs
1d43d 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 .** give a NULL
1d43e 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 output..*/.case
1d43f 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 OP_And:
1d440 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d441 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 TK_AND, in1, in2
1d442 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1d443 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Or: {
1d444 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d445 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 K_OR, in1, in2,
1d446 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1d447 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d448 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d449 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20 k */. int v1;
1d44a 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e /* Left operan
1d44b 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d d: 0==FALSE, 1=
1d44c 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 =TRUE, 2==UNKNOW
1d44d 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 N or NULL */. i
1d44e 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 nt v2; /* Rig
1d44f 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 ht operand: 0==F
1d450 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1d451 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1d452 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c L */.#endif /* l
1d453 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d454 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a oved into u.ak *
1d455 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 /.. pIn1 = &aMe
1d456 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 m[pOp->p1];. if
1d457 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
1d458 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1d459 75 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d u.ak.v1 = 2;. }
1d45a 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 else{. u.ak.v
1d45b 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 1 = sqlite3VdbeI
1d45c 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 ntValue(pIn1)!=0
1d45d 3b 0a 20 20 7d 0a 20 20 70 49 6e 32 20 3d 20 26 ;. }. pIn2 = &
1d45e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1d45f 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 if( pIn2->flags
1d460 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1d461 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a u.ak.v2 = 2;.
1d462 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 }else{. u.a
1d463 6b 2e 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64 k.v2 = sqlite3Vd
1d464 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 beIntValue(pIn2)
1d465 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 !=0;. }. if( p
1d466 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 Op->opcode==OP_A
1d467 6e 64 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 nd ){. static
1d468 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1d469 63 68 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d char and_logic[]
1d46a 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c = { 0, 0, 0, 0,
1d46b 20 31 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 1, 2, 0, 2, 2 }
1d46c 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 ;. u.ak.v1 =
1d46d 61 6e 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 and_logic[u.ak.v
1d46e 31 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 1*3+u.ak.v2];.
1d46f 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 }else{. stati
1d470 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
1d471 20 63 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d char or_logic[]
1d472 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c = { 0, 1, 2, 1,
1d473 20 31 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 1, 1, 2, 1, 2 }
1d474 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 ;. u.ak.v1 =
1d475 6f 72 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 or_logic[u.ak.v1
1d476 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d *3+u.ak.v2];. }
1d477 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b . pOut = &aMem[
1d478 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 pOp->p3];. if(
1d479 75 2e 61 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 u.ak.v1==2 ){.
1d47a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1d47b 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 (pOut, MEM_Null)
1d47c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1d47d 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e Out->u.i = u.ak.
1d47e 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 v1;. MemSetTy
1d47f 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1d480 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 _Int);. }. bre
1d481 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1d482 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 : Not P1 P2 * *
1d483 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 *.**.** Interpre
1d484 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 t the value in r
1d485 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 20 egister P1 as a
1d486 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 boolean value.
1d487 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f Store the.** boo
1d488 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 lean complement
1d489 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 in register P2.
1d48a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e If the value in
1d48b 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d48c 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 .** NULL, then a
1d48d 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 NULL is stored
1d48e 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f in P2..*/.case O
1d48f 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 P_Not: {
1d490 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1d491 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 2c 20 as TK_NOT, in1,
1d492 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d out2 */. pIn1 =
1d493 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b &aMem[pOp->p1];
1d494 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b . pOut = &aMem[
1d495 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 pOp->p2];. if(
1d496 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1d497 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 M_Null ){. sq
1d498 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1d499 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c ull(pOut);. }el
1d49a 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 se{. sqlite3V
1d49b 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 dbeMemSetInt64(p
1d49c 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62 Out, !sqlite3Vdb
1d49d 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 eIntValue(pIn1))
1d49e 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d49f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 ../* Opcode: Bit
1d4a0 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Not P1 P2 * * *.
1d4a1 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 **.** Interpret
1d4a2 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d4a3 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e egister P1 as an
1d4a4 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65 integer. Store
1d4a5 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d the.** ones-com
1d4a6 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 plement of the P
1d4a7 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 1 value into reg
1d4a8 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31 ister P2. If P1
1d4a9 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c holds.** a NULL
1d4aa 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 then store a NU
1d4ab 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 LL in P2..*/.cas
1d4ac 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 e OP_BitNot: {
1d4ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1d4ae 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c me as TK_BITNOT,
1d4af 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 in1, out2 */.
1d4b0 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1d4b1 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 ->p1];. pOut =
1d4b2 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a &aMem[pOp->p2];.
1d4b3 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1d4b4 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1d4b5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d4b6 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b emSetNull(pOut);
1d4b7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
1d4b8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
1d4b9 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 nt64(pOut, ~sqli
1d4ba 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1d4bb 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 pIn1));. }. br
1d4bc 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d4bd 65 3a 20 49 66 20 50 31 20 50 32 20 50 33 20 2a e: If P1 P2 P3 *
1d4be 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d4bf 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
1d4c0 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d4c1 69 73 20 74 72 75 65 2e 20 20 54 68 65 20 76 61 is true. The va
1d4c2 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e lue is.** is con
1d4c3 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 sidered true if
1d4c4 69 74 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e it is numeric an
1d4c5 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 d non-zero. If
1d4c6 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 the value.** in
1d4c7 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 P1 is NULL then
1d4c8 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
1d4c9 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a P3 is true..*/.
1d4ca 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 /* Opcode: IfNot
1d4cb 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1d4cc 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 .** Jump to P2 i
1d4cd 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 f the value in r
1d4ce 65 67 69 73 74 65 72 20 50 31 20 69 73 20 46 61 egister P1 is Fa
1d4cf 6c 73 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 lse. The value
1d4d0 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 is.** is conside
1d4d1 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 68 red true if it h
1d4d2 61 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c as a numeric val
1d4d3 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 ue of zero. If
1d4d4 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 the value.** in
1d4d5 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 P1 is NULL then
1d4d6 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
1d4d7 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a P3 is true..*/.
1d4d8 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 case OP_If:
1d4d9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1d4da 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 ump, in1 */.case
1d4db 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 OP_IfNot: {
1d4dc 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1d4dd 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f in1 */.#if 0 /
1d4de 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d4df 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d4e0 6c 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 l */. int c;.#e
1d4e1 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d4e2 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d4e3 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 70 49 6e to u.al */. pIn
1d4e4 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 1 = &aMem[pOp->p
1d4e5 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 1];. if( pIn1->
1d4e6 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1d4e7 20 29 7b 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d ){. u.al.c =
1d4e8 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 pOp->p3;. }els
1d4e9 65 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 e{.#ifdef SQLITE
1d4ea 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
1d4eb 4f 49 4e 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20 OINT. u.al.c
1d4ec 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
1d4ed 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a Value(pIn1)!=0;.
1d4ee 23 65 6c 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63 #else. u.al.c
1d4ef 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1d4f0 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 alValue(pIn1)!=0
1d4f1 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 .0;.#endif. i
1d4f2 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1d4f3 4f 50 5f 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e OP_IfNot ) u.al.
1d4f4 63 20 3d 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d c = !u.al.c;. }
1d4f5 0a 20 20 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b . if( u.al.c ){
1d4f6 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1d4f7 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 2-1;. }. break
1d4f8 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d4f9 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a IsNull P1 P2 * *
1d4fa 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d4fb 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
1d4fc 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d4fd 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 is NULL..*/.case
1d4fe 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 OP_IsNull: {
1d4ff 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d500 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a as TK_ISNULL, j
1d501 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 ump, in1 */. pI
1d502 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n1 = &aMem[pOp->
1d503 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 p1];. if( (pIn1
1d504 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1d505 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 63 ll)!=0 ){. pc
1d506 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1d507 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d508 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 /* Opcode: NotNu
1d509 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ll P1 P2 * * *.*
1d50a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 *.** Jump to P2
1d50b 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 if the value in
1d50c 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6e register P1 is n
1d50d 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 ot NULL. .*/.ca
1d50e 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b se OP_NotNull: {
1d50f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d510 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c ame as TK_NOTNUL
1d511 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a L, jump, in1 */.
1d512 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1d513 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28 Op->p1];. if( (
1d514 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1d515 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Null)==0 ){.
1d516 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1d517 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1d518 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
1d519 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 50 olumn P1 P2 P3 P
1d51a 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 4 P5.**.** Inter
1d51b 70 72 65 74 20 74 68 65 20 64 61 74 61 20 74 68 pret the data th
1d51c 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 at cursor P1 poi
1d51d 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 72 75 nts to as a stru
1d51e 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 69 6e cture built usin
1d51f 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 g.** the MakeRec
1d520 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ord instruction.
1d521 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b 65 52 (See the MakeR
1d522 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 ecord opcode for
1d523 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 additional.** i
1d524 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1d525 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 the format of t
1d526 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 72 61 he data.) Extra
1d527 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c ct the P2-th col
1d528 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 umn.** from this
1d529 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 68 65 record. If the
1d52a 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 61 74 re are less that
1d52b 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 (P2+1) .** valu
1d52c 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 es in the record
1d52d 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c , extract a NULL
1d52e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
1d52f 65 20 65 78 74 72 61 63 74 65 64 20 69 73 20 73 e extracted is s
1d530 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1d531 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 r P3..**.** If t
1d532 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 he column contai
1d533 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32 ns fewer than P2
1d534 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 fields, then ex
1d535 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f tract a NULL. O
1d536 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 34 20 r,.** if the P4
1d537 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 50 34 argument is a P4
1d538 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 61 6c _MEM use the val
1d539 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 72 67 ue of the P4 arg
1d53a 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 ument as.** the
1d53b 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 result..**.** If
1d53c 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 the OPFLAG_CLEA
1d53d 52 43 41 43 48 45 20 62 69 74 20 69 73 20 73 65 RCACHE bit is se
1d53e 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31 20 69 t on P5 and P1 i
1d53f 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 s a pseudo-table
1d540 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e cursor,.** then
1d541 20 74 68 65 20 63 61 63 68 65 20 6f 66 20 74 68 the cache of th
1d542 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 73 65 e cursor is rese
1d543 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74 72 61 t prior to extra
1d544 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e cting the column
1d545 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 4f ..** The first O
1d546 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74 P_Column against
1d547 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 a pseudo-table
1d548 61 66 74 65 72 20 74 68 65 20 76 61 6c 75 65 20 after the value
1d549 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a of the content.*
1d54a 2a 20 72 65 67 69 73 74 65 72 20 68 61 73 20 63 * register has c
1d54b 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20 68 61 hanged should ha
1d54c 76 65 20 74 68 69 73 20 62 69 74 20 73 65 74 2e ve this bit set.
1d54d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 .*/.case OP_Colu
1d54e 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 mn: {.#if 0 /*
1d54f 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d550 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20 moved into u.am
1d551 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64 */. u32 payload
1d552 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 Size; /* Numbe
1d553 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
1d554 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36 e record */. i6
1d555 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 4 payloadSize64;
1d556 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1d557 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1d558 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 d */. int p1;
1d559 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 /* P1
1d55a 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 value of the opc
1d55b 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b ode */. int p2;
1d55c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 /* c
1d55d 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 olumn number to
1d55e 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 retrieve */. Vd
1d55f 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 beCursor *pC;
1d560 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 /* The VDBE cur
1d561 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a sor */. char *z
1d562 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 Rec; /* P
1d563 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 ointer to comple
1d564 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a te record-data *
1d565 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
1d566 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 rsr; /* The BT
1d567 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 ree cursor */.
1d568 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 u32 *aType;
1d569 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 /* aType[i] h
1d56a 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63 olds the numeric
1d56b 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74 type of the i-t
1d56c 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 h column */. u3
1d56d 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 2 *aOffset;
1d56e 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 /* aOffset[i] i
1d56f 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72 s offset to star
1d570 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d t of data for i-
1d571 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 th column */. i
1d572 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
1d573 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 /* number of f
1d574 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 ields in the rec
1d575 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e ord */. int len
1d576 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
1d577 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
1d578 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 serialized data
1d579 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 for the column
1d57a 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
1d57b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1d57c 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 counter */. cha
1d57d 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 r *zData;
1d57e 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72 /* Part of the r
1d57f 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f ecord being deco
1d580 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 ded */. Mem *pD
1d581 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 est; /* W
1d582 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 here to write th
1d583 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75 e extracted valu
1d584 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b e */. Mem sMem;
1d585 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
1d586 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63 storing the rec
1d587 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 ord being decode
1d588 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b d */. u8 *zIdx;
1d589 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
1d58a 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 2a ex into header *
1d58b 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b /. u8 *zEndHdr;
1d58c 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1d58d 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 r to first byte
1d58e 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72 after the header
1d58f 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 */. u32 offset
1d590 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 ; /* Offs
1d591 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 et into the data
1d592 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73 65 74 */. u64 offset
1d593 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 64; /* 64-b
1d594 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 62 it offset. 64 b
1d595 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 61 its needed to ca
1d596 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a tch overflow */.
1d597 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 int szHdr;
1d598 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1d599 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 the header size
1d59a 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f field at start o
1d59b 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e f record */. in
1d59c 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20 t avail;
1d59d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1d59e 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 tes of available
1d59f 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a data */. Mem *
1d5a0 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a pReg; /*
1d5a1 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 PseudoTable inp
1d5a2 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 ut register */.#
1d5a3 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1d5a4 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d5a5 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20 nto u.am */...
1d5a6 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 u.am.p1 = pOp->p
1d5a7 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70 1;. u.am.p2 = p
1d5a8 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70 Op->p2;. u.am.p
1d5a9 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 C = 0;. memset(
1d5aa 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73 &u.am.sMem, 0, s
1d5ab 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29 izeof(u.am.sMem)
1d5ac 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 );. assert( u.a
1d5ad 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 m.p1<p->nCursor
1d5ae 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1d5af 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1d5b0 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1d5b1 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26 61 4d u.am.pDest = &aM
1d5b2 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d em[pOp->p3];. M
1d5b3 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 75 2e emSetTypeFlag(u.
1d5b4 61 6d 2e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 am.pDest, MEM_Nu
1d5b5 6c 6c 29 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 ll);. u.am.zRec
1d5b6 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 = 0;.. /* This
1d5b7 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 block sets the
1d5b8 76 61 72 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 variable u.am.pa
1d5b9 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20 yloadSize to be
1d5ba 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
1d5bb 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 of. ** bytes i
1d5bc 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 n the record..
1d5bd 2a 2a 0a 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 **. ** u.am.zRe
1d5be 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20 74 c is set to be t
1d5bf 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 he complete text
1d5c0 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 of the record i
1d5c1 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c f it is availabl
1d5c2 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 e.. ** The comp
1d5c3 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 lete record text
1d5c4 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69 6c is always avail
1d5c5 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f 2d able for pseudo-
1d5c6 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 tables. ** If t
1d5c7 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f he record is sto
1d5c8 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72 2c red in a cursor,
1d5c9 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 the complete re
1d5ca 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d cord text. ** m
1d5cb 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62 6c ight be availabl
1d5cc 65 20 69 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 e in the u.am.p
1d5cd 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1d5ce 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 Or it might not
1d5cf 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 be.. ** If the
1d5d0 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61 data is unavaila
1d5d1 62 6c 65 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 ble, u.am.zRec
1d5d2 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a is set to NULL..
1d5d3 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 **. ** We als
1d5d4 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 o compute the nu
1d5d5 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1d5d6 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20 in the record.
1d5d7 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a For cursors,. *
1d5d8 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
1d5d9 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 columns is store
1d5da 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72 d in the VdbeCur
1d5db 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 sor.nField eleme
1d5dc 6e 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e nt.. */. u.am.
1d5dd 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e pC = p->apCsr[u.
1d5de 61 6d 2e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 am.p1];. assert
1d5df 28 20 75 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a ( u.am.pC!=0 );.
1d5e0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d5e1 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1d5e2 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e . assert( u.am.
1d5e3 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d pC->pVtabCursor=
1d5e4 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 =0 );.#endif. u
1d5e5 2e 61 6d 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d .am.pCrsr = u.am
1d5e6 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 .pC->pCursor;.
1d5e7 69 66 28 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d if( u.am.pCrsr!=
1d5e8 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 0 ){. /* The
1d5e9 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 record is stored
1d5ea 20 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a in a B-Tree */.
1d5eb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d5ec 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f VdbeCursorMoveto
1d5ed 28 75 2e 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 (u.am.pC);. i
1d5ee 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f f( rc ) goto abo
1d5ef 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1d5f0 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 . if( u.am.pC
1d5f1 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 ->nullRow ){.
1d5f2 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 u.am.payloadS
1d5f3 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ize = 0;. }el
1d5f4 73 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e se if( u.am.pC->
1d5f5 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e cacheStatus==p->
1d5f6 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 cacheCtr ){.
1d5f7 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 u.am.payloadSi
1d5f8 7a 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 ze = u.am.pC->pa
1d5f9 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 yloadSize;.
1d5fa 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 u.am.zRec = (ch
1d5fb 61 72 2a 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f ar*)u.am.pC->aRo
1d5fc 77 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 w;. }else if(
1d5fd 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d5fe 78 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 x ){. asser
1d5ff 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 t( sqlite3BtreeC
1d600 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 ursorIsValid(u.a
1d601 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 20 20 m.pCrsr) );.
1d602 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1d603 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e reeKeySize(u.am.
1d604 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 pCrsr, &u.am.pay
1d605 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 loadSize64);.
1d606 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
1d607 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a QLITE_OK ); /*
1d608 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66 True because of
1d609 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 CursorMoveto()
1d60a 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 call above */.
1d60b 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 /* sqlite3Bt
1d60c 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
1d60d 29 20 75 73 65 73 20 67 65 74 56 61 72 69 6e 74 ) uses getVarint
1d60e 33 32 28 29 20 74 6f 20 65 78 74 72 61 63 74 20 32() to extract
1d60f 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 the. ** pay
1d610 6c 6f 61 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 load size, so it
1d611 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 is impossible f
1d612 6f 72 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 or u.am.payloadS
1d613 69 7a 65 36 34 20 74 6f 20 62 65 0a 20 20 20 20 ize64 to be.
1d614 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e ** larger than
1d615 20 33 32 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 32 bits. */.
1d616 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61 6d assert( (u.am
1d617 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 .payloadSize64 &
1d618 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 SQLITE_MAX_U32)
1d619 3d 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c ==(u64)u.am.payl
1d61a 6f 61 64 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 oadSize64 );.
1d61b 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 u.am.payloadS
1d61c 69 7a 65 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e ize = (u32)u.am.
1d61d 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 payloadSize64;.
1d61e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d61f 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1d620 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 treeCursorIsVali
1d621 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b d(u.am.pCrsr) );
1d622 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1d623 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 te3BtreeDataSize
1d624 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.am.pCrsr, &u.
1d625 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b am.payloadSize);
1d626 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
1d627 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 c==SQLITE_OK );
1d628 20 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 /* DataSize()
1d629 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 cannot fail */.
1d62a 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
1d62b 20 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f u.am.pC->pseudo
1d62c 54 61 62 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 TableReg>0 ){.
1d62d 20 20 75 2e 61 6d 2e 70 52 65 67 20 3d 20 26 61 u.am.pReg = &a
1d62e 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 Mem[u.am.pC->pse
1d62f 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20 udoTableReg];.
1d630 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 assert( u.am.p
1d631 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Reg->flags & MEM
1d632 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61 _Blob );. u.a
1d633 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 m.payloadSize =
1d634 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20 u.am.pReg->n;.
1d635 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e u.am.zRec = u.
1d636 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20 am.pReg->z;.
1d637 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d638 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26 atus = (pOp->p5&
1d639 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 OPFLAG_CLEARCACH
1d63a 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45 E) ? CACHE_STALE
1d63b 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a : p->cacheCtr;.
1d63c 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d63d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 .payloadSize==0
1d63e 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 || u.am.zRec!=0
1d63f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1d640 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 /* Consider the
1d641 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a row to be NULL *
1d642 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f /. u.am.paylo
1d643 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a adSize = 0;. }.
1d644 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61 . /* If u.am.pa
1d645 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 yloadSize is 0,
1d646 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 then just store
1d647 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 a NULL */. if(
1d648 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d649 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
1d64a 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 t( u.am.pDest->f
1d64b 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b lags&MEM_Null );
1d64c 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c . goto op_col
1d64d 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 umn_out;. }. a
1d64e 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69 ssert( db->aLimi
1d64f 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1d650 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69 ENGTH]>=0 );. i
1d651 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 f( u.am.payloadS
1d652 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61 ize > (u32)db->a
1d653 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1d654 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1d655 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1d656 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65 }.. u.am.nFie
1d657 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46 ld = u.am.pC->nF
1d658 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20 ield;. assert(
1d659 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69 u.am.p2<u.am.nFi
1d65a 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 eld );.. /* Rea
1d65b 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20 d and parse the
1d65c 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53 table header. S
1d65d 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 tore the results
1d65e 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20 of the parse.
1d65f 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f ** into the reco
1d660 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20 rd header cache
1d661 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
1d662 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 rsor.. */. u.a
1d663 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70 m.aType = u.am.p
1d664 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20 C->aType;. if(
1d665 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d666 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 atus==p->cacheCt
1d667 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f r ){. u.am.aO
1d668 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d ffset = u.am.pC-
1d669 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 >aOffset;. }els
1d66a 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e e{. assert(u.
1d66b 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75 am.aType);. u
1d66c 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20 .am.avail = 0;.
1d66d 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 u.am.pC->aOff
1d66e 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73 set = u.am.aOffs
1d66f 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65 et = &u.am.aType
1d670 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20 [u.am.nField];.
1d671 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c u.am.pC->payl
1d672 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 oadSize = u.am.p
1d673 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 ayloadSize;.
1d674 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d675 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 atus = p->cacheC
1d676 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 tr;.. /* Figu
1d677 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 re out how many
1d678 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65 bytes are in the
1d679 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 header */. i
1d67a 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d67b 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 u.am.zData
1d67c 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20 = u.am.zRec;.
1d67d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
1d67e 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e f( u.am.pC->isIn
1d67f 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75 dex ){. u
1d680 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 .am.zData = (cha
1d681 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b r*)sqlite3BtreeK
1d682 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72 eyFetch(u.am.pCr
1d683 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29 sr, &u.am.avail)
1d684 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1d685 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 u.am.zDat
1d686 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
1d687 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
1d688 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.am.pCrsr, &u.
1d689 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 am.avail);.
1d68a 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b }. /* If K
1d68b 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65 eyFetch()/DataFe
1d68c 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f tch() managed to
1d68d 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20 get the entire
1d68e 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a payload,. *
1d68f 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f * save the paylo
1d690 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70 ad in the u.am.p
1d691 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1d692 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75 That will save u
1d693 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 s from. **
1d694 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 having to make a
1d695 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 dditional calls
1d696 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e to fetch the con
1d697 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a tent portion of.
1d698 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 ** the rec
1d699 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ord.. */.
1d69a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d69b 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20 .avail>=0 );.
1d69c 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c if( u.am.payl
1d69d 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 oadSize <= (u32)
1d69e 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20 u.am.avail ){.
1d69f 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 u.am.zRec
1d6a0 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 = u.am.zData;.
1d6a1 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 u.am.pC->a
1d6a2 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e Row = (u8*)u.am.
1d6a3 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c zData;. }el
1d6a4 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d se{. u.am
1d6a5 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20 .pC->aRow = 0;.
1d6a6 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1d6a7 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
1d6a8 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65 g assert is true
1d6a9 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63 in all cases ac
1d6aa 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a cept when. **
1d6ab 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1d6ac 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 le has been corr
1d6ad 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79 upted externally
1d6ae 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65 .. ** asse
1d6af 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 rt( u.am.zRec!=0
1d6b0 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d6b1 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d6b2 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d6b3 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 9 ); */. u.am
1d6b4 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69 .szHdr = getVari
1d6b5 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a nt32((u8*)u.am.z
1d6b6 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65 Data, u.am.offse
1d6b7 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 t);.. /* Make
1d6b8 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20 sure a corrupt
1d6b9 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 database has not
1d6ba 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65 given us an ove
1d6bb 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20 rsize header..
1d6bc 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77 ** Do this now
1d6bd 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65 to avoid an ove
1d6be 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c rsize memory all
1d6bf 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a ocation.. **.
1d6c0 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 ** Type entr
1d6c1 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65 ies can be betwe
1d6c2 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73 en 1 and 5 bytes
1d6c3 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e each. But 4 an
1d6c4 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 d 5 byte. **
1d6c5 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63 types use so muc
1d6c6 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61 h data space tha
1d6c7 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 t there can only
1d6c8 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20 be 4096 and 32
1d6c9 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 of. ** them,
1d6ca 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 respectively. S
1d6cb 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 o the maximum he
1d6cc 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75 ader length resu
1d6cd 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a lts from a. *
1d6ce 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f * 3-byte type fo
1d6cf 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61 r each of the ma
1d6d0 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63 ximum of 32768 c
1d6d1 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65 olumns plus thre
1d6d2 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62 e. ** extra b
1d6d3 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61 ytes for the hea
1d6d4 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c der length itsel
1d6d5 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20 f. 32768*3 + 3
1d6d6 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a = 98307.. */.
1d6d7 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66 if( u.am.off
1d6d8 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20 set > 98307 ){.
1d6d9 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1d6da 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1d6db 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c goto op_col
1d6dc 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a umn_out;. }..
1d6dd 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69 /* Compute i
1d6de 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e n u.am.len the n
1d6df 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d6e0 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74 f data we need t
1d6e1 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a o read in order.
1d6e2 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e ** to get u.
1d6e3 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76 am.nField type v
1d6e4 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66 alues. u.am.off
1d6e5 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20 set is an upper
1d6e6 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20 bound on this.
1d6e7 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e But. ** u.am.
1d6e8 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 nField might be
1d6e9 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65 significantly le
1d6ea 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 ss than the true
1d6eb 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1d6ec 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 ns. ** in the
1d6ed 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74 table, and in t
1d6ee 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d hat case, 5*u.am
1d6ef 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20 .nField+3 might
1d6f0 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 be smaller than
1d6f1 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20 u.am.offset..
1d6f2 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d ** We want to m
1d6f3 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e inimize u.am.len
1d6f4 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d in order to lim
1d6f5 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 it the size of t
1d6f6 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a he memory. **
1d6f7 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 allocation, esp
1d6f8 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 ecially if a cor
1d6f9 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1d6fa 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e le has caused u.
1d6fb 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a am.offset. **
1d6fc 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64 to be oversized
1d6fd 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69 . Offset is limi
1d6fe 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f ted to 98307 abo
1d6ff 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d ve. But 98307 m
1d700 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c ight. ** stil
1d701 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 l exceed Robson
1d702 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1d703 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 n limits on some
1d704 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e configurations.
1d705 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65 . ** On syste
1d706 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74 ms that cannot t
1d707 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65 olerate large me
1d708 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
1d709 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b , u.am.nField*5+
1d70a 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69 3. ** will li
1d70b 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61 kely be much sma
1d70c 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e ller since u.am.
1d70d 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65 nField will like
1d70e 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a ly be less than.
1d70f 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e ** 20 or so.
1d710 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74 This insures t
1d711 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 hat Robson memor
1d712 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d y allocation lim
1d713 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e its are. ** n
1d714 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e ot exceeded even
1d715 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74 for corrupt dat
1d716 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20 abase files..
1d717 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e */. u.am.len
1d718 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 = u.am.nField*5
1d719 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e + 3;. if( u.
1d71a 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e am.len > (int)u.
1d71b 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d am.offset ) u.am
1d71c 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d .len = (int)u.am
1d71d 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a .offset;.. /*
1d71e 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 The KeyFetch()
1d71f 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61 or DataFetch() a
1d720 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e bove are fast an
1d721 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 d will get the e
1d722 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 ntire. ** rec
1d723 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f ord header in mo
1d724 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 st cases. But t
1d725 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f hey will fail to
1d726 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 get the complet
1d727 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 e. ** record
1d728 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65 header if the re
1d729 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 cord header does
1d72a 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 not fit on a si
1d72b 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a ngle page. **
1d72c 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 in the B-Tree.
1d72d 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 When that happe
1d72e 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 ns, use sqlite3V
1d72f 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1d730 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 ) to. ** acqu
1d731 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 ire the complete
1d732 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 header text..
1d733 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e */. if( !u.
1d734 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e am.zRec && u.am.
1d735 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29 avail<u.am.len )
1d736 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 {. u.am.sMe
1d737 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 m.flags = 0;.
1d738 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20 u.am.sMem.db
1d739 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
1d73a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
1d73b 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 omBtree(u.am.pCr
1d73c 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c sr, 0, u.am.len,
1d73d 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d73e 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a x, &u.am.sMem);.
1d73f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1d740 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d741 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d goto op_colum
1d742 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 n_out;. }.
1d743 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 u.am.zData
1d744 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 = u.am.sMem.z;.
1d745 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45 }. u.am.zE
1d746 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75 ndHdr = (u8 *)&u
1d747 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c .am.zData[u.am.l
1d748 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49 en];. u.am.zI
1d749 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d dx = (u8 *)&u.am
1d74a 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64 .zData[u.am.szHd
1d74b 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e r];.. /* Scan
1d74c 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 the header and
1d74d 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 use it to fill i
1d74e 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65 n the u.am.aType
1d74f 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 [] and u.am.aOff
1d750 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 set[]. ** arr
1d751 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65 ays. u.am.aType
1d752 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f [u.am.i] will co
1d753 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69 ntain the type i
1d754 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75 nteger for the u
1d755 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20 .am.i-th. **
1d756 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e column and u.am.
1d757 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 aOffset[u.am.i]
1d758 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 will contain the
1d759 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f u.am.offset fro
1d75a 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
1d75b 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 ** of the re
1d75c 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72 cord to the star
1d75d 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f t of the data fo
1d75e 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20 r the u.am.i-th
1d75f 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 column. */.
1d760 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 u.am.offset64
1d761 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20 = u.am.offset;.
1d762 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b for(u.am.i=0;
1d763 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69 u.am.i<u.am.nFi
1d764 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a eld; u.am.i++){.
1d765 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a if( u.am.z
1d766 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 Idx<u.am.zEndHdr
1d767 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d ){. u.am
1d768 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d .aOffset[u.am.i]
1d769 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66 = (u32)u.am.off
1d76a 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75 set64;. u
1d76b 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56 .am.zIdx += getV
1d76c 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64 arint32(u.am.zId
1d76d 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e x, u.am.aType[u.
1d76e 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 am.i]);.
1d76f 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d u.am.offset64 +=
1d770 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1d771 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 alTypeLen(u.am.a
1d772 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 Type[u.am.i]);.
1d773 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d774 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69 /* If u.am.i
1d775 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e is less that u.
1d776 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 am.nField, then
1d777 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66 there are less f
1d778 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20 ields in this.
1d779 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 ** record
1d77a 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d than SetNumColum
1d77b 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65 ns indicated the
1d77c 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 re are columns i
1d77d 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a n the. **
1d77e 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20 table. Set the
1d77f 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20 u.am.offset for
1d780 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e any extra column
1d781 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e s not present in
1d782 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
1d783 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 record to 0. Thi
1d784 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c s tells code bel
1d785 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 ow to store a NU
1d786 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e LL. ** in
1d787 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 stead of deseria
1d788 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 lizing a value f
1d789 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a rom the record..
1d78a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1d78b 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b u.am.aOffset[
1d78c 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20 u.am.i] = 0;.
1d78d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
1d78e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1d78f 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29 ease(&u.am.sMem)
1d790 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e ;. u.am.sMem.
1d791 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1d792 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ;.. /* If we
1d793 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 have read more h
1d794 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 eader data than
1d795 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e was contained in
1d796 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 the header,.
1d797 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e ** or if the en
1d798 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 d of the last fi
1d799 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 eld appears to b
1d79a 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
1d79b 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 f the. ** rec
1d79c 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 ord, or if the e
1d79d 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
1d79e 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
1d79f 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e be before the en
1d7a0 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 d. ** of the
1d7a1 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c record (when all
1d7a2 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 fields present)
1d7a3 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 , then we must b
1d7a4 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a e dealing. **
1d7a5 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 with a corrupt
1d7a6 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
1d7a7 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a . if( (u.am.z
1d7a8 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48 Idx > u.am.zEndH
1d7a9 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73 dr)|| (u.am.offs
1d7aa 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c et64 > u.am.payl
1d7ab 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c oadSize). ||
1d7ac 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61 (u.am.zIdx==u.a
1d7ad 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61 m.zEndHdr && u.a
1d7ae 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34 m.offset64!=(u64
1d7af 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a )u.am.payloadSiz
1d7b0 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d e) ){. rc =
1d7b1 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1d7b2 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
1d7b3 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a op_column_out;.
1d7b4 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1d7b5 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 Get the column i
1d7b6 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75 nformation. If u
1d7b7 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d .am.aOffset[u.am
1d7b8 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f .p2] is non-zero
1d7b9 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65 , then. ** dese
1d7ba 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 rialize the valu
1d7bb 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 e from the recor
1d7bc 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73 d. If u.am.aOffs
1d7bd 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a et[u.am.p2] is z
1d7be 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 ero,. ** then t
1d7bf 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f here are not eno
1d7c0 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68 ugh fields in th
1d7c1 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69 e record to sati
1d7c2 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 sfy the. ** req
1d7c3 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63 uest. In this c
1d7c4 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c ase, set the val
1d7c5 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34 ue NULL or to P4
1d7c6 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61 if P4 is. ** a
1d7c7 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
1d7c8 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20 m object.. */.
1d7c9 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65 if( u.am.aOffse
1d7ca 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20 t[u.am.p2] ){.
1d7cb 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1d7cc 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 LITE_OK );. i
1d7cd 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d7ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d7cf 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
1d7d0 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b nal(u.am.pDest);
1d7d1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d7d2 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 beSerialGet((u8
1d7d3 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61 *)&u.am.zRec[u.a
1d7d4 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 m.aOffset[u.am.p
1d7d5 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 2]], u.am.aType[
1d7d6 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 u.am.p2], u.am.p
1d7d7 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Dest);. }else
1d7d8 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e {. u.am.len
1d7d9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1d7da 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d rialTypeLen(u.am
1d7db 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29 .aType[u.am.p2])
1d7dc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d7dd 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d dbeMemMove(&u.am
1d7de 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73 .sMem, u.am.pDes
1d7df 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 t);. rc = s
1d7e0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1d7e1 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 mBtree(u.am.pCrs
1d7e2 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b r, u.am.aOffset[
1d7e3 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c u.am.p2], u.am.l
1d7e4 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 en, u.am.pC->isI
1d7e5 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d ndex, &u.am.sMem
1d7e6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1d7e7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d7e8 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
1d7e9 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 lumn_out;.
1d7ea 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 }. u.am.zDa
1d7eb 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a ta = u.am.sMem.z
1d7ec 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d7ed 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 dbeSerialGet((u8
1d7ee 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e *)u.am.zData, u.
1d7ef 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 am.aType[u.am.p2
1d7f0 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a ], u.am.pDest);.
1d7f1 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70 }. u.am.p
1d7f2 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f Dest->enc = enco
1d7f3 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ding;. }else{.
1d7f4 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 if( pOp->p4ty
1d7f5 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 pe==P4_MEM ){.
1d7f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d7f7 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e emShallowCopy(u.
1d7f8 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 am.pDest, pOp->p
1d7f9 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 4.pMem, MEM_Stat
1d7fa 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ic);. }else{.
1d7fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1d7fc 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 am.pDest->flags&
1d7fd 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 MEM_Null );.
1d7fe 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 }. }.. /* If w
1d7ff 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c e dynamically al
1d800 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f located space to
1d801 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28 hold the data (
1d802 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 in the. ** sqli
1d803 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1d804 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 ree() call above
1d805 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 ) then transfer
1d806 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a control of that.
1d807 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 ** dynamically
1d808 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
1d809 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61 over to the u.a
1d80a 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72 m.pDest structur
1d80b 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65 e.. ** This pre
1d80c 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 vents a memory c
1d80d 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 opy.. */. if(
1d80e 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f u.am.sMem.zMallo
1d80f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
1d810 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e u.am.sMem.z==u.
1d811 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 am.sMem.zMalloc
1d812 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
1d813 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 (u.am.pDest->fla
1d814 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b gs & MEM_Dyn) );
1d815 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75 . assert( !(u
1d816 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 .am.pDest->flags
1d817 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d & (MEM_Blob|MEM
1d818 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70 _Str)) || u.am.p
1d819 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d Dest->z==u.am.sM
1d81a 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d em.z );. u.am
1d81b 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d .pDest->flags &=
1d81c 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d ~(MEM_Ephem|MEM
1d81d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e _Static);. u.
1d81e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 am.pDest->flags
1d81f 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 |= MEM_Term;.
1d820 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d u.am.pDest->z =
1d821 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 u.am.sMem.z;.
1d822 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d u.am.pDest->zM
1d823 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65 alloc = u.am.sMe
1d824 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a m.zMalloc;. }..
1d825 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1d826 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
1d827 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a le(u.am.pDest);.
1d828 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a .op_column_out:.
1d829 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1d82a 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74 BSIZE(u.am.pDest
1d82b 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
1d82c 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61 ACE(pOp->p3, u.a
1d82d 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61 m.pDest);. brea
1d82e 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d82f 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20 Affinity P1 P2
1d830 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 * P4 *.**.** App
1d831 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f ly affinities to
1d832 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72 a range of P2 r
1d833 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e egisters startin
1d834 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a g with P1..**.**
1d835 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 P4 is a string
1d836 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61 that is P2 chara
1d837 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 cters long. The
1d838 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66 nth character of
1d839 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 the.** string i
1d83a 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c ndicates the col
1d83b 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 umn affinity tha
1d83c 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
1d83d 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 for the nth.**
1d83e 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 memory cell in t
1d83f 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 he range..*/.cas
1d840 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b e OP_Affinity: {
1d841 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d842 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d843 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 into u.an */.
1d844 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66 const char *zAff
1d845 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 inity; /* The
1d846 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61 affinity to be a
1d847 70 70 6c 69 65 64 20 2a 2f 0a 20 20 63 68 61 72 pplied */. char
1d848 20 63 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 cAff;
1d849 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 /* A single
1d84a 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 61 66 character of af
1d84b 66 69 6e 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 finity */.#endif
1d84c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d84d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d84e 2e 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a .an */.. u.an.z
1d84f 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e Affinity = pOp->
1d850 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 p4.z;. assert(
1d851 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 21 3d u.an.zAffinity!=
1d852 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1d853 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 5b 70 4f .an.zAffinity[pO
1d854 70 2d 3e 70 32 5d 3d 3d 30 20 29 3b 0a 20 20 70 p->p2]==0 );. p
1d855 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In1 = &aMem[pOp-
1d856 3e 70 31 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 >p1];. while( (
1d857 75 2e 61 6e 2e 63 41 66 66 20 3d 20 2a 28 75 2e u.an.cAff = *(u.
1d858 61 6e 2e 7a 41 66 66 69 6e 69 74 79 2b 2b 29 29 an.zAffinity++))
1d859 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 !=0 ){. asser
1d85a 74 28 20 70 49 6e 31 20 3c 3d 20 26 70 2d 3e 61 t( pIn1 <= &p->a
1d85b 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a Mem[p->nMem] );.
1d85c 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 ExpandBlob(p
1d85d 49 6e 31 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 In1);. applyA
1d85e 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e ffinity(pIn1, u.
1d85f 61 6e 2e 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e an.cAff, encodin
1d860 67 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a g);. pIn1++;.
1d861 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d862 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52 /* Opcode: MakeR
1d863 65 63 6f 72 64 20 50 31 20 50 32 20 50 33 20 50 ecord P1 P2 P3 P
1d864 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 4 *.**.** Conver
1d865 74 20 50 32 20 72 65 67 69 73 74 65 72 73 20 62 t P2 registers b
1d866 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 31 eginning with P1
1d867 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 65 into a single e
1d868 6e 74 72 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65 ntry.** suitable
1d869 20 66 6f 72 20 75 73 65 20 61 73 20 61 20 64 61 for use as a da
1d86a 74 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64 ta record in a d
1d86b 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1d86c 20 61 73 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 as a key.** in
1d86d 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20 64 an index. The d
1d86e 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 66 6f etails of the fo
1d86f 72 6d 61 74 20 61 72 65 20 69 72 72 65 6c 65 76 rmat are irrelev
1d870 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a ant as long as.*
1d871 2a 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 * the OP_Column
1d872 6f 70 63 6f 64 65 20 63 61 6e 20 64 65 63 6f 64 opcode can decod
1d873 65 20 74 68 65 20 72 65 63 6f 72 64 20 6c 61 74 e the record lat
1d874 65 72 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 er..** Refer to
1d875 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d source code comm
1d876 65 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74 ents for the det
1d877 61 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f ails of the reco
1d878 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a rd.** format..**
1d879 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 .** P4 may be a
1d87a 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 string that is P
1d87b 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 2 characters lon
1d87c 67 2e 20 20 54 68 65 20 6e 74 68 20 63 68 61 72 g. The nth char
1d87d 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 acter of the.**
1d87e 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 string indicates
1d87f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 the column affi
1d880 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 nity that should
1d881 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 be used for the
1d882 20 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 nth.** field of
1d883 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a the index key..
1d884 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e **.** The mappin
1d885 67 20 66 72 6f 6d 20 63 68 61 72 61 63 74 65 72 g from character
1d886 20 74 6f 20 61 66 66 69 6e 69 74 79 20 69 73 20 to affinity is
1d887 67 69 76 65 6e 20 62 79 20 74 68 65 20 53 51 4c given by the SQL
1d888 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 ITE_AFF_.** macr
1d889 6f 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71 os defined in sq
1d88a 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a liteInt.h..**.**
1d88b 20 49 66 20 50 34 20 69 73 20 4e 55 4c 4c 20 74 If P4 is NULL t
1d88c 68 65 6e 20 61 6c 6c 20 69 6e 64 65 78 20 66 69 hen all index fi
1d88d 65 6c 64 73 20 68 61 76 65 20 74 68 65 20 61 66 elds have the af
1d88e 66 69 6e 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a finity NONE..*/.
1d88f 63 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f case OP_MakeReco
1d890 72 64 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 rd: {.#if 0 /*
1d891 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d892 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 moved into u.ao
1d893 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 */. u8 *zNewRec
1d894 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ord; /* A
1d895 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 buffer to hold
1d896 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
1d897 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 new record */.
1d898 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 Mem *pRec;
1d899 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
1d89a 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 ew record */. u
1d89b 36 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 64 nData;
1d89c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d89d 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
1d89e 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 space */. int
1d89f 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 nHdr;
1d8a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d8a1 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 bytes of header
1d8a2 73 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e space */. i64 n
1d8a3 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 Byte;
1d8a4 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 /* Data space
1d8a5 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 required for thi
1d8a6 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e s record */. in
1d8a7 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 t nZero;
1d8a8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d8a9 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 f zero bytes at
1d8aa 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 the end of the r
1d8ab 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
1d8ac 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 Varint;
1d8ad 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d8ae 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 ytes in a varint
1d8af 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c */. u32 serial
1d8b0 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 _type; /*
1d8b1 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 Type field */.
1d8b2 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 Mem *pData0;
1d8b3 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1d8b4 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 field to be comb
1d8b5 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 ined into the re
1d8b6 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 cord */. Mem *p
1d8b7 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 Last;
1d8b8 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f /* Last field o
1d8b9 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a f the record */.
1d8ba 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 int nField;
1d8bb 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1d8bc 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
1d8bd 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1d8be 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b char *zAffinity;
1d8bf 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 /* The af
1d8c0 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f finity string fo
1d8c1 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a r the record */.
1d8c2 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 int file_forma
1d8c3 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 t; /* File
1d8c4 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 format to use f
1d8c5 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 or encoding */.
1d8c6 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1d8c7 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
1d8c8 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 used in zNewRec
1d8c9 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c ord[] */. int l
1d8ca 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 en;
1d8cb 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 /* Length of a
1d8cc 20 66 69 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 field */.#endif
1d8cd 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d8ce 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d8cf 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 .ao */.. /* Ass
1d8d0 75 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 uming the record
1d8d1 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c contains N fiel
1d8d2 64 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 ds, the record f
1d8d3 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a ormat looks. **
1d8d4 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a like this:. **
1d8d5 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d . ** ----------
1d8d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d8d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d8d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d8d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 --------------.
1d8da 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c ** | hdr-size |
1d8db 20 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31 type 0 | type 1
1d8dc 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d | ... | type N-
1d8dd 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 1 | data0 | ...
1d8de 7c 20 64 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a | data N-1 |. *
1d8df 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
1d8e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d8e1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d8e2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d8e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a ----------. **.
1d8e4 20 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20 ** Data(0) is
1d8e5 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 taken from regis
1d8e6 74 65 72 20 50 31 2e 20 20 44 61 74 61 28 31 29 ter P1. Data(1)
1d8e7 20 63 6f 6d 65 73 20 66 72 6f 6d 20 72 65 67 69 comes from regi
1d8e8 73 74 65 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61 ster P1+1. ** a
1d8e9 6e 64 20 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a nd so froth.. *
1d8ea 2a 0a 20 20 2a 2a 20 45 61 63 68 20 74 79 70 65 *. ** Each type
1d8eb 20 66 69 65 6c 64 20 69 73 20 61 20 76 61 72 69 field is a vari
1d8ec 6e 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 nt representing
1d8ed 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 the serial type
1d8ee 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 of the. ** corr
1d8ef 65 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 20 65 esponding data e
1d8f0 6c 65 6d 65 6e 74 20 28 73 65 65 20 73 71 6c 69 lement (see sqli
1d8f1 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1d8f2 65 28 29 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68 e()). The. ** h
1d8f3 64 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 dr-size field is
1d8f4 20 61 6c 73 6f 20 61 20 76 61 72 69 6e 74 20 77 also a varint w
1d8f5 68 69 63 68 20 69 73 20 74 68 65 20 6f 66 66 73 hich is the offs
1d8f6 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 et from the begi
1d8f7 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68 nning. ** of th
1d8f8 65 20 72 65 63 6f 72 64 20 74 6f 20 64 61 74 61 e record to data
1d8f9 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 0.. */. u.ao.n
1d8fa 44 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 Data = 0;
1d8fb 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d8fc 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 ytes of data spa
1d8fd 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 ce */. u.ao.nHd
1d8fe 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 r = 0;
1d8ff 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1d900 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 es of header spa
1d901 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 ce */. u.ao.nBy
1d902 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 te = 0;
1d903 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 /* Data space re
1d904 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 quired for this
1d905 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f record */. u.ao
1d906 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 .nZero = 0;
1d907 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1d908 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 zero bytes at t
1d909 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 he end of the re
1d90a 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e cord */. u.ao.n
1d90b 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b Field = pOp->p1;
1d90c 0a 20 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 . u.ao.zAffinit
1d90d 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 y = pOp->p4.z;.
1d90e 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46 assert( u.ao.nF
1d90f 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ield>0 && pOp->p
1d910 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2>0 && pOp->p2+u
1d911 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e .ao.nField<=p->n
1d912 4d 65 6d 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e Mem+1 );. u.ao.
1d913 70 44 61 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75 pData0 = &aMem[u
1d914 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 .ao.nField];. u
1d915 2e 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 .ao.nField = pOp
1d916 2d 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 ->p2;. u.ao.pLa
1d917 73 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 st = &u.ao.pData
1d918 30 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 0[u.ao.nField-1]
1d919 3b 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f ;. u.ao.file_fo
1d91a 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 rmat = p->minWri
1d91b 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 teFileFormat;..
1d91c 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 /* Loop through
1d91d 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 the elements th
1d91e 61 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 at will make up
1d91f 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 the record to fi
1d920 67 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f gure. ** out ho
1d921 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 w much space is
1d922 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 required for the
1d923 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a new record.. *
1d924 2f 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 /. for(u.ao.pRe
1d925 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 c=u.ao.pData0; u
1d926 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 .ao.pRec<=u.ao.p
1d927 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b Last; u.ao.pRec+
1d928 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f +){. if( u.ao
1d929 2e 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 .zAffinity ){.
1d92a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1d92b 79 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 y(u.ao.pRec, u.a
1d92c 6f 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f o.zAffinity[u.ao
1d92d 2e 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 .pRec-u.ao.pData
1d92e 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 0], encoding);.
1d92f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 }. if( u.a
1d930 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 o.pRec->flags&ME
1d931 4d 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 M_Zero && u.ao.p
1d932 52 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 Rec->n>0 ){.
1d933 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d934 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e ExpandBlob(u.ao.
1d935 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pRec);. }.
1d936 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 u.ao.serial_typ
1d937 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 e = sqlite3VdbeS
1d938 65 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 erialType(u.ao.p
1d939 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 Rec, u.ao.file_f
1d93a 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f ormat);. u.ao
1d93b 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 .len = sqlite3Vd
1d93c 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1d93d 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 u.ao.serial_type
1d93e 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 );. u.ao.nDat
1d93f 61 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 a += u.ao.len;.
1d940 20 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 u.ao.nHdr +=
1d941 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e sqlite3VarintLen
1d942 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 (u.ao.serial_typ
1d943 65 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f e);. if( u.ao
1d944 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d .pRec->flags & M
1d945 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 EM_Zero ){.
1d946 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 /* Only pure ze
1d947 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 ro-filled BLOBs
1d948 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 can be input to
1d949 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 this Opcode..
1d94a 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 ** We do not
1d94b 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 allow blobs with
1d94c 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 a prefix and a
1d94d 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c zero-filled tail
1d94e 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e . */. u.ao.
1d94f 6e 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 nZero += u.ao.pR
1d950 65 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 ec->u.nZero;.
1d951 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e }else if( u.ao.
1d952 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 len ){. u.a
1d953 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 o.nZero = 0;.
1d954 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 }. }.. /* Add
1d955 20 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 the initial hea
1d956 64 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 der varint and t
1d957 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f otal the size */
1d958 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 . u.ao.nHdr +=
1d959 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 u.ao.nVarint = s
1d95a 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1d95b 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 u.ao.nHdr);. if
1d95c 28 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 ( u.ao.nVarint<s
1d95d 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1d95e 75 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 u.ao.nHdr) ){.
1d95f 20 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 u.ao.nHdr++;.
1d960 20 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 }. u.ao.nByte
1d961 3d 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f = u.ao.nHdr+u.ao
1d962 2e 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 .nData-u.ao.nZer
1d963 6f 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 o;. if( u.ao.nB
1d964 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 yte>db->aLimit[S
1d965 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1d966 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 TH] ){. goto
1d967 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 too_big;. }..
1d968 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
1d969 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 output register
1d96a 20 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 has a buffer la
1d96b 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 rge enough to st
1d96c 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 ore. ** the new
1d96d 20 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 record. The out
1d96e 70 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f put register (pO
1d96f 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c p->p3) is not al
1d970 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 lowed to. ** be
1d971 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 one of the inpu
1d972 74 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 t registers (bec
1d973 61 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ause the followi
1d974 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 ng call to. **
1d975 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1d976 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 ow() could clobb
1d977 65 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 er the value bef
1d978 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e ore it is used).
1d979 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1d97a 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 pOp->p3<pOp->p1
1d97b 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d || pOp->p3>=pOp-
1d97c 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 >p1+pOp->p2 );.
1d97d 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f pOut = &aMem[pO
1d97e 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 71 p->p3];. if( sq
1d97f 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1d980 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 6f (pOut, (int)u.ao
1d981 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 .nByte, 0) ){.
1d982 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1d983 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77 52 65 }. u.ao.zNewRe
1d984 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 cord = (u8 *)pOu
1d985 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 t->z;.. /* Writ
1d986 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a e the record */.
1d987 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74 56 61 u.ao.i = putVa
1d988 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e 65 77 rint32(u.ao.zNew
1d989 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e 48 64 Record, u.ao.nHd
1d98a 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 r);. for(u.ao.p
1d98b 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b Rec=u.ao.pData0;
1d98c 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f u.ao.pRec<=u.ao
1d98d 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 .pLast; u.ao.pRe
1d98e 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 73 c++){. u.ao.s
1d98f 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c erial_type = sql
1d990 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
1d991 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e pe(u.ao.pRec, u.
1d992 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b ao.file_format);
1d993 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 70 . u.ao.i += p
1d994 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e 61 6f utVarint32(&u.ao
1d995 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f .zNewRecord[u.ao
1d996 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69 61 6c .i], u.ao.serial
1d997 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 _type); /*
1d998 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 serial type */.
1d999 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 }. for(u.ao.pR
1d99a 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 ec=u.ao.pData0;
1d99b 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e u.ao.pRec<=u.ao.
1d99c 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 pLast; u.ao.pRec
1d99d 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 ++){ /* serial
1d99e 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e 61 6f data */. u.ao
1d99f 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 .i += sqlite3Vdb
1d9a0 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e 61 6f eSerialPut(&u.ao
1d9a1 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f .zNewRecord[u.ao
1d9a2 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61 6f 2e .i], (int)(u.ao.
1d9a3 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c 20 75 nByte-u.ao.i), u
1d9a4 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e 66 69 .ao.pRec,u.ao.fi
1d9a5 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a le_format);. }.
1d9a6 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 69 assert( u.ao.i
1d9a7 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29 3b 0a ==u.ao.nByte );.
1d9a8 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1d9a9 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1d9aa 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f =p->nMem );. pO
1d9ab 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61 ut->n = (int)u.a
1d9ac 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d o.nByte;. pOut-
1d9ad 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
1d9ae 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70 b | MEM_Dyn;. p
1d9af 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 Out->xDel = 0;.
1d9b0 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 if( u.ao.nZero
1d9b1 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e ){. pOut->u.n
1d9b2 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a 65 72 Zero = u.ao.nZer
1d9b3 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 o;. pOut->fla
1d9b4 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a gs |= MEM_Zero;.
1d9b5 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 }. pOut->enc
1d9b6 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 = SQLITE_UTF8;
1d9b7 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 /* In case the b
1d9b8 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76 lob is ever conv
1d9b9 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f erted to text */
1d9ba 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1d9bb 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 E(pOp->p3, pOut)
1d9bc 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ;. UPDATE_MAX_B
1d9bd 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 LOBSIZE(pOut);.
1d9be 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d9bf 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 20 50 code: Count P1 P
1d9c0 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 2 * * *.**.** St
1d9c1 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ore the number o
1d9c2 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 69 6e f entries (an in
1d9c3 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 6e 20 teger value) in
1d9c4 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 the table or ind
1d9c5 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79 ex .** opened by
1d9c6 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 72 65 cursor P1 in re
1d9c7 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66 gister P2.*/.#if
1d9c8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d9c9 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 73 65 _BTREECOUNT.case
1d9ca 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20 OP_Count: {
1d9cb 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1d9cc 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1d9cd 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1d9ce 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d9cf 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20 6e 45 u.ap */. i64 nE
1d9d0 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73 6f 72 ntry;. BtCursor
1d9d1 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69 66 20 *pCrsr;.#endif
1d9d2 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d9d3 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d9d4 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e 70 43 ap */.. u.ap.pC
1d9d5 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 rsr = p->apCsr[p
1d9d6 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 Op->p1]->pCursor
1d9d7 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70 43 72 ;. if( u.ap.pCr
1d9d8 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 sr ){. rc = s
1d9d9 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 qlite3BtreeCount
1d9da 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26 75 2e (u.ap.pCrsr, &u.
1d9db 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65 ap.nEntry);. }e
1d9dc 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e 6e 45 lse{. u.ap.nE
1d9dd 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 ntry = 0;. }.
1d9de 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70 pOut->u.i = u.ap
1d9df 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b .nEntry;. break
1d9e0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f ;.}.#endif../* O
1d9e1 70 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 pcode: Savepoint
1d9e2 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1d9e3 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 ** Open, release
1d9e4 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 or rollback the
1d9e5 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 savepoint named
1d9e6 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 by parameter P4
1d9e7 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f , depending.** o
1d9e8 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 n the value of P
1d9e9 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 1. To open a new
1d9ea 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d savepoint, P1==
1d9eb 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 0. To release (c
1d9ec 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 ommit) an.** exi
1d9ed 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c sting savepoint,
1d9ee 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f P1==1, or to ro
1d9ef 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 llback an existi
1d9f0 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d ng savepoint P1=
1d9f1 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 =2..*/.case OP_S
1d9f2 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 avepoint: {.#if
1d9f3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1d9f4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d9f5 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70 u.aq */. int p
1d9f6 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
1d9f7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
1d9f8 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e lue of P1 operan
1d9f9 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 d */. char *zNa
1d9fa 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 me;
1d9fb 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
1d9fc 66 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 f savepoint */.
1d9fd 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61 int nName;. Sa
1d9fe 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 vepoint *pNew;.
1d9ff 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 Savepoint *pSav
1da00 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f epoint;. Savepo
1da01 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74 int *pTmp;. int
1da02 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69 iSavepoint;. i
1da03 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a nt ii;.#endif /*
1da04 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1da05 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 moved into u.aq
1da06 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d */.. u.aq.p1 =
1da07 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 pOp->p1;. u.aq
1da08 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 .zName = pOp->p4
1da09 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 .z;.. /* Assert
1da0a 20 74 68 61 74 20 74 68 65 20 75 2e 61 71 2e 70 that the u.aq.p
1da0b 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 76 1 parameter is v
1da0c 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 alid. Also that
1da0d 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f if there is no o
1da0e 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 pen. ** transac
1da0f 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 tion, then there
1da10 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 cannot be any s
1da11 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a avepoints.. */.
1da12 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 assert( db->pS
1da13 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 avepoint==0 || d
1da14 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 b->autoCommit==0
1da15 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1da16 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1da17 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d _BEGIN||u.aq.p1=
1da18 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
1da19 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 SE||u.aq.p1==SAV
1da1a 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1da1b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d );. assert( db-
1da1c 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 >pSavepoint || d
1da1d 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e b->isTransaction
1da1e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a Savepoint==0 );.
1da1f 20 20 61 73 73 65 72 74 28 20 63 68 65 63 6b 53 assert( checkS
1da20 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 avepointCount(db
1da21 29 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71 ) );.. if( u.aq
1da22 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 .p1==SAVEPOINT_B
1da23 45 47 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 EGIN ){. if(
1da24 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1da25 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 >0 ){. /* A
1da26 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 new savepoint c
1da27 61 6e 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 annot be created
1da28 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 63 if there are ac
1da29 74 69 76 65 20 77 72 69 74 65 0a 20 20 20 20 20 tive write.
1da2a 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 ** statements (
1da2b 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 i.e. open read/w
1da2c 72 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c rite incremental
1da2d 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a blob handles)..
1da2e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1da2f 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1da30 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1da31 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 "cannot open sa
1da32 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 vepoint - ".
1da33 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 "SQL stateme
1da34 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 nts in progress"
1da35 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 );. rc = SQ
1da36 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
1da37 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 71 else{. u.aq
1da38 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 .nName = sqlite3
1da39 53 74 72 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e Strlen30(u.aq.zN
1da3a 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ame);.. /*
1da3b 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 Create a new sav
1da3c 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 epoint structure
1da3d 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e . */. u.aq.
1da3e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
1da3f 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 MallocRaw(db, si
1da40 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e 74 29 2b zeof(Savepoint)+
1da41 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20 u.aq.nName+1);.
1da42 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e 70 4e if( u.aq.pN
1da43 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e ew ){. u.
1da44 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d aq.pNew->zName =
1da45 20 28 63 68 61 72 20 2a 29 26 75 2e 61 71 2e 70 (char *)&u.aq.p
1da46 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 New[1];.
1da47 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70 4e 65 77 memcpy(u.aq.pNew
1da48 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e ->zName, u.aq.zN
1da49 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b ame, u.aq.nName+
1da4a 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 1);.. /*
1da4b 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f If there is no o
1da4c 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c pen transaction,
1da4d 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 then mark this
1da4e 61 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 as a special.
1da4f 20 20 20 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 ** "transac
1da50 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e tion savepoint".
1da51 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
1da52 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 db->autoCommit )
1da53 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e {. db->
1da54 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a autoCommit = 0;.
1da55 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 db->is
1da56 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1da57 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 oint = 1;.
1da58 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1da59 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e db->nSavepoin
1da5a 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a t++;. }..
1da5b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 /* Link
1da5c 74 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e the new savepoin
1da5d 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 t into the datab
1da5e 61 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 ase handle's lis
1da5f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e t. */. u.
1da60 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d aq.pNew->pNext =
1da61 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b db->pSavepoint;
1da62 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 . db->pSa
1da63 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 vepoint = u.aq.p
1da64 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 New;. u.a
1da65 71 2e 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65 q.pNew->nDeferre
1da66 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 dCons = db->nDef
1da67 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 erredCons;.
1da68 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
1da69 7b 0a 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 {. u.aq.iSave
1da6a 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 point = 0;..
1da6b 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 /* Find the name
1da6c 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 d savepoint. If
1da6d 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 there is no such
1da6e 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e savepoint, then
1da6f 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 an. ** an er
1da70 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
1da71 74 6f 20 74 68 65 20 75 73 65 72 2e 20 20 2a 2f to the user. */
1da72 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 . for(.
1da73 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 u.aq.pSavepoint
1da74 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 = db->pSavepoint
1da75 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61 ;. u.aq.pSa
1da76 76 65 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 vepoint && sqlit
1da77 65 33 53 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 e3StrICmp(u.aq.p
1da78 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 Savepoint->zName
1da79 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 , u.aq.zName);.
1da7a 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 u.aq.pSavep
1da7b 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 oint = u.aq.pSav
1da7c 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 epoint->pNext.
1da7d 20 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e ){. u.aq.
1da7e 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 iSavepoint++;.
1da7f 20 20 7d 0a 20 20 20 20 69 66 28 20 21 75 2e 61 }. if( !u.a
1da80 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a q.pSavepoint ){.
1da81 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 sqlite3Set
1da82 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1da83 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 sg, db, "no such
1da84 20 73 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c savepoint: %s",
1da85 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 u.aq.zName);.
1da86 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1da87 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 ERROR;. }else
1da88 20 69 66 28 0a 20 20 20 20 20 20 20 20 64 62 2d if(. db-
1da89 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 >writeVdbeCnt>0
1da8a 7c 7c 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 || (u.aq.p1==SAV
1da8b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1da8c 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 && db->activeVdb
1da8d 65 43 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 eCnt>1). ){.
1da8e 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f /* It is no
1da8f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 t possible to re
1da90 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 lease (commit) a
1da91 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68 savepoint if th
1da92 65 72 65 20 61 72 65 0a 20 20 20 20 20 20 2a 2a ere are. **
1da93 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73 74 active write st
1da94 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 atements. It is
1da95 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
1da96 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 70 rollback a savep
1da97 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 oint. ** if
1da98 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 there are any a
1da99 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 ctive statements
1da9a 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a at all.. *
1da9b 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 /. sqlite3S
1da9c 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1da9d 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 rMsg, db,.
1da9e 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 "cannot %s sav
1da9f 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 epoint - SQL sta
1daa0 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1daa1 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20 28 75 ess",. (u
1daa2 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1daa3 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f T_ROLLBACK ? "ro
1daa4 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 llback": "releas
1daa5 65 22 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 e"). );.
1daa6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1daa7 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a USY;. }else{.
1daa8 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d . /* Determ
1daa9 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
1daaa 6f 74 20 74 68 69 73 20 69 73 20 61 20 74 72 61 ot this is a tra
1daab 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 nsaction savepoi
1daac 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 nt. If so,.
1daad 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20 ** and this is
1daae 61 20 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e a RELEASE comman
1daaf 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 d, then the curr
1dab0 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a ent transaction.
1dab1 20 20 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d ** is comm
1dab2 69 74 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a itted.. */.
1dab3 20 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e int isTran
1dab4 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 saction = u.aq.p
1dab5 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1dab6 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61 ==0 && db->isTra
1dab7 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e nsactionSavepoin
1dab8 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 54 t;. if( isT
1dab9 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 75 2e ransaction && u.
1daba 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1dabb 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 _RELEASE ){.
1dabc 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 73 71 if( (rc = sq
1dabd 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
1dabe 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f (p, 1))!=SQLITE_
1dabf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1dac0 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1dac1 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1dac2 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1dac3 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 it = 1;.
1dac4 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 if( sqlite3VdbeH
1dac5 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 alt(p)==SQLITE_B
1dac6 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 USY ){.
1dac7 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 p->pc = pc;.
1dac8 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1dac9 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 ommit = 0;.
1daca 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 p->rc = rc
1dacb 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
1dacc 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 goto vd
1dacd 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 be_return;.
1dace 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d }. db-
1dacf 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 >isTransactionSa
1dad0 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 vepoint = 0;.
1dad1 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b rc = p->rc;
1dad2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1dad3 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 u.aq.iSave
1dad4 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 point = db->nSav
1dad5 65 70 6f 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 epoint - u.aq.iS
1dad6 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 avepoint - 1;.
1dad7 20 20 20 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 for(u.aq.i
1dad8 69 3d 30 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d i=0; u.aq.ii<db-
1dad9 3e 6e 44 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 >nDb; u.aq.ii++)
1dada 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
1dadb 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 sqlite3BtreeSav
1dadc 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 epoint(db->aDb[u
1dadd 2e 61 71 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 .aq.ii].pBt, u.a
1dade 71 2e 70 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 q.p1, u.aq.iSave
1dadf 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 point);.
1dae0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1dae1 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1dae2 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1dae3 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1dae4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1dae5 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e }. if( u.
1dae6 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1dae7 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 _ROLLBACK && (db
1dae8 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 ->flags&SQLITE_I
1dae9 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 nternChanges)!=0
1daea 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
1daeb 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1daec 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1daed 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1daee 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
1daef 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a lSchema(db, 0);.
1daf0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1daf1 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61 }.. /* Rega
1daf2 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
1daf3 72 20 74 68 69 73 20 69 73 20 61 20 52 45 4c 45 r this is a RELE
1daf4 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c ASE or ROLLBACK,
1daf5 20 64 65 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 destroy all.
1daf6 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 ** savepoints
1daf7 20 6e 65 73 74 65 64 20 69 6e 73 69 64 65 20 6f nested inside o
1daf8 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 f the savepoint
1daf9 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f being operated o
1dafa 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c n. */. whil
1dafb 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e e( db->pSavepoin
1dafc 74 21 3d 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 t!=u.aq.pSavepoi
1dafd 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e nt ){. u.
1dafe 61 71 2e 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 aq.pTmp = db->pS
1daff 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 avepoint;.
1db00 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 db->pSavepoint
1db01 20 3d 20 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e = u.aq.pTmp->pN
1db02 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ext;. sql
1db03 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 ite3DbFree(db, u
1db04 2e 61 71 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 .aq.pTmp);.
1db05 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e db->nSavepoin
1db06 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 t--;. }..
1db07 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73 20 /* If it is
1db08 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 a RELEASE, then
1db09 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 destroy the save
1db0a 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 point being oper
1db0b 61 74 65 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a ated on. **
1db0c 20 74 6f 6f 2e 20 49 66 20 69 74 20 69 73 20 61 too. If it is a
1db0d 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68 ROLLBACK TO, th
1db0e 65 6e 20 73 65 74 20 74 68 65 20 6e 75 6d 62 65 en set the numbe
1db0f 72 20 6f 66 20 64 65 66 65 72 72 65 64 0a 20 20 r of deferred.
1db10 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e ** constrain
1db11 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 t violations pre
1db12 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 sent in the data
1db13 62 61 73 65 20 74 6f 20 74 68 65 20 76 61 6c 75 base to the valu
1db14 65 20 73 74 6f 72 65 64 0a 20 20 20 20 20 20 2a e stored. *
1db15 2a 20 77 68 65 6e 20 74 68 65 20 73 61 76 65 70 * when the savep
1db16 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 oint was created
1db17 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 . */. if(
1db18 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 u.aq.p1==SAVEPOI
1db19 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 NT_RELEASE ){.
1db1a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1db1b 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 aq.pSavepoint==d
1db1c 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b b->pSavepoint );
1db1d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 . db->pSa
1db1e 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 vepoint = u.aq.p
1db1f 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1db20 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1db21 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 3DbFree(db, u.aq
1db22 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 .pSavepoint);.
1db23 20 20 20 20 20 20 69 66 28 20 21 69 73 54 72 61 if( !isTra
1db24 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 nsaction ){.
1db25 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 db->nSavep
1db26 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 oint--;.
1db27 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1db28 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 db->nDefe
1db29 72 72 65 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e rredCons = u.aq.
1db2a 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66 pSavepoint->nDef
1db2b 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 erredCons;.
1db2c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
1db2d 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1db2e 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 ode: AutoCommit
1db2f 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1db30 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61 * Set the databa
1db31 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 se auto-commit f
1db32 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 lag to P1 (1 or
1db33 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 0). If P2 is tru
1db34 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 e, roll.** back
1db35 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 any currently ac
1db36 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 tive btree trans
1db37 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 actions. If ther
1db38 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 e are any active
1db39 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 .** VMs (apart f
1db3a 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 rom this one), t
1db3b 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 hen a ROLLBACK f
1db3c 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 ails. A COMMIT
1db3d 66 61 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 fails if.** ther
1db3e 65 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69 e are active wri
1db3f 74 69 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 ting VMs or acti
1db40 76 65 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 ve VMs that use
1db41 73 68 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a shared cache..**
1db42 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1db43 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 tion causes the
1db44 56 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 VM to halt..*/.c
1db45 61 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 ase OP_AutoCommi
1db46 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c t: {.#if 0 /* l
1db47 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1db48 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a oved into u.ar *
1db49 2f 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 /. int desiredA
1db4a 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 utoCommit;. int
1db4b 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e iRollback;. in
1db4c 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 t turnOnAC;.#end
1db4d 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1db4e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1db4f 20 75 2e 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 u.ar */.. u.ar
1db50 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1db51 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 it = pOp->p1;.
1db52 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d u.ar.iRollback =
1db53 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 pOp->p2;. u.ar
1db54 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 .turnOnAC = u.ar
1db55 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1db56 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 it && !db->autoC
1db57 6f 6d 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28 ommit;. assert(
1db58 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 u.ar.desiredAut
1db59 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e oCommit==1 || u.
1db5a 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f ar.desiredAutoCo
1db5b 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 mmit==0 );. ass
1db5c 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 ert( u.ar.desire
1db5d 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c dAutoCommit==1 |
1db5e 7c 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b | u.ar.iRollback
1db5f 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1db60 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 db->activeVdbeC
1db61 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c nt>0 ); /* At l
1db62 65 61 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d east this one VM
1db63 20 69 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 is active */..
1db64 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e if( u.ar.turnOn
1db65 41 43 20 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c AC && u.ar.iRoll
1db66 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 back && db->acti
1db67 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 veVdbeCnt>1 ){.
1db68 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e /* If this in
1db69 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d struction implem
1db6a 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 ents a ROLLBACK
1db6b 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 and other VMs ar
1db6c 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 e. ** still r
1db6d 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 unning, and a tr
1db6e 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1db6f 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 ive, return an e
1db70 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a rror indicating.
1db71 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 ** that the
1db72 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 other VMs must c
1db73 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 omplete first..
1db74 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1db75 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1db76 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e ErrMsg, db, "can
1db77 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 not rollback tra
1db78 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 nsaction - ".
1db79 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d "SQL statem
1db7a 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 ents in progress
1db7b 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ");. rc = SQL
1db7c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 ITE_BUSY;. }els
1db7d 65 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f e if( u.ar.turnO
1db7e 6e 41 43 20 26 26 20 21 75 2e 61 72 2e 69 52 6f nAC && !u.ar.iRo
1db7f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 llback && db->wr
1db80 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a iteVdbeCnt>0 ){.
1db81 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
1db82 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 nstruction imple
1db83 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 ments a COMMIT a
1db84 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 nd other VMs are
1db85 20 77 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 writing. **
1db86 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1db87 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 indicating that
1db88 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 the other VMs mu
1db89 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 st complete firs
1db8a 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 t.. */. sq
1db8b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1db8c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1db8d 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 "cannot commit t
1db8e 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 ransaction - ".
1db8f 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 "SQL stat
1db90 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 ements in progre
1db91 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 ss");. rc = S
1db92 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 QLITE_BUSY;. }e
1db93 6c 73 65 20 69 66 28 20 75 2e 61 72 2e 64 65 73 lse if( u.ar.des
1db94 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d iredAutoCommit!=
1db95 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 db->autoCommit )
1db96 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 72 2e 69 {. if( u.ar.i
1db97 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 Rollback ){.
1db98 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 assert( u.ar.d
1db99 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1db9a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==1 );. sql
1db9b 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 ite3RollbackAll(
1db9c 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 db);. db->a
1db9d 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 utoCommit = 1;.
1db9e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 }else if( (rc
1db9f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 = sqlite3VdbeCh
1dba0 65 63 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 eckFk(p, 1))!=SQ
1dba1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1dba2 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 goto vdbe_retur
1dba3 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 n;. }else{.
1dba4 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1dba5 69 74 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 65 it = (u8)u.ar.de
1dba6 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b siredAutoCommit;
1dba7 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1dba8 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 e3VdbeHalt(p)==S
1dba9 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
1dbaa 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1dbab 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 ;. db->au
1dbac 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 toCommit = (u8)(
1dbad 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 1-u.ar.desiredAu
1dbae 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 toCommit);.
1dbaf 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1dbb0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
1dbb1 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 goto vdbe_r
1dbb2 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
1dbb3 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1dbb4 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d db->nStatement=
1dbb5 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
1dbb6 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 3CloseSavepoints
1dbb7 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 2d (db);. if( p-
1dbb8 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 >rc==SQLITE_OK )
1dbb9 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1dbba 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 ITE_DONE;. }e
1dbbb 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1dbbc 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1dbbd 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 }. goto vdb
1dbbe 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 e_return;. }els
1dbbf 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 e{. sqlite3Se
1dbc0 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1dbc1 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 Msg, db,.
1dbc2 20 28 21 75 2e 61 72 2e 64 65 73 69 72 65 64 41 (!u.ar.desiredA
1dbc3 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e utoCommit)?"cann
1dbc4 6f 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 ot start a trans
1dbc5 61 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 action within a
1dbc6 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 transaction":(.
1dbc7 20 20 20 20 20 20 20 28 75 2e 61 72 2e 69 52 6f (u.ar.iRo
1dbc8 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 llback)?"cannot
1dbc9 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 rollback - no tr
1dbca 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1dbcb 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20 ive":.
1dbcc 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
1dbcd 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 commit - no tra
1dbce 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
1dbcf 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d ve"));.. rc =
1dbd0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1dbd1 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1dbd2 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 * Opcode: Transa
1dbd3 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 ction P1 P2 * *
1dbd4 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 *.**.** Begin a
1dbd5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 transaction. Th
1dbd6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e e transaction en
1dbd7 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 ds when a Commit
1dbd8 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 or Rollback.**
1dbd9 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e opcode is encoun
1dbda 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e tered. Dependin
1dbdb 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 g on the ON CONF
1dbdc 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 LICT setting, th
1dbdd 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1dbde 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 might also be r
1dbdf 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e olled back if an
1dbe0 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e error is encoun
1dbe1 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 tered..**.** P1
1dbe2 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
1dbe3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1dbe4 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 e on which the t
1dbe5 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a ransaction is.**
1dbe6 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 started. Index
1dbe7 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 0 is the main d
1dbe8 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1dbe9 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a index 1 is the.
1dbea 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 ** file used for
1dbeb 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1dbec 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 s. Indices of 2
1dbed 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 or more are use
1dbee 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 d for.** attache
1dbef 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a d databases..**.
1dbf0 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d ** If P2 is non-
1dbf1 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 zero, then a wri
1dbf2 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te-transaction i
1dbf3 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 s started. A RE
1dbf4 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a SERVED lock is.*
1dbf5 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 * obtained on th
1dbf6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1dbf7 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 when a write-tra
1dbf8 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
1dbf9 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 ted. No.** othe
1dbfa 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 r process can st
1dbfb 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 art another writ
1dbfc 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 e transaction wh
1dbfd 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 ile this transac
1dbfe 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 tion is.** under
1dbff 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 way. Starting a
1dc00 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1dc01 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 on also creates
1dc02 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
1dc03 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 al. A.** write t
1dc04 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 ransaction must
1dc05 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 be started befor
1dc06 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 e any changes ca
1dc07 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 n be made to the
1dc08 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 .** database. I
1dc09 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 f P2 is 2 or gre
1dc0a 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 ater then an EXC
1dc0b 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 LUSIVE lock is a
1dc0c 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 lso obtained.**
1dc0d 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a on the file..**.
1dc0e 2a 2a 20 49 66 20 61 20 77 72 69 74 65 2d 74 72 ** If a write-tr
1dc0f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
1dc10 72 74 65 64 20 61 6e 64 20 74 68 65 20 56 64 62 rted and the Vdb
1dc11 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 e.usesStmtJourna
1dc12 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75 l flag is.** tru
1dc13 65 20 28 74 68 69 73 20 66 6c 61 67 20 69 73 20 e (this flag is
1dc14 73 65 74 20 69 66 20 74 68 65 20 56 64 62 65 20 set if the Vdbe
1dc15 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20 may modify more
1dc16 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64 than one row and
1dc17 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e may.** throw an
1dc18 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e ABORT exception
1dc19 29 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 ), a statement t
1dc1a 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 ransaction may a
1dc1b 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a lso be opened..*
1dc1c 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61 * More specifica
1dc1d 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 lly, a statement
1dc1e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1dc1f 6f 70 65 6e 65 64 20 69 66 66 20 74 68 65 20 64 opened iff the d
1dc20 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
1dc21 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 ction is current
1dc22 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f ly not in autoco
1dc23 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 mmit mode, or if
1dc24 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 there are other
1dc25 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74 65 .** active state
1dc26 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65 ments. A stateme
1dc27 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 nt transaction a
1dc28 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63 74 llows the affect
1dc29 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 42 s of this.** VDB
1dc2a 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 E to be rolled b
1dc2b 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72 ack after an err
1dc2c 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e or without havin
1dc2d 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 g to roll back t
1dc2e 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 he.** entire tra
1dc2f 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 nsaction. If no
1dc30 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
1dc31 65 72 65 64 2c 20 74 68 65 20 73 74 61 74 65 6d ered, the statem
1dc32 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a ent transaction.
1dc33 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 ** will automati
1dc34 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65 cally commit whe
1dc35 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 n the VDBE halts
1dc36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 ..**.** If P2 is
1dc37 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 zero, then a re
1dc38 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 ad-lock is obtai
1dc39 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
1dc3a 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 ase file..*/.cas
1dc3b 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e e OP_Transaction
1dc3c 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1dc3d 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1dc3e 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f ved into u.as */
1dc3f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 . Btree *pBt;.#
1dc40 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1dc41 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dc42 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61 nto u.as */.. a
1dc43 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1dc44 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 0 && pOp->p1<db-
1dc45 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1dc46 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1dc47 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 & (1<<pOp->p1))!
1dc48 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74 =0 );. u.as.pBt
1dc49 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e = db->aDb[pOp->
1dc4a 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 p1].pBt;.. if(
1dc4b 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 20 u.as.pBt ){.
1dc4c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1dc4d 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 73 eBeginTrans(u.as
1dc4e 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a .pBt, pOp->p2);.
1dc4f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1dc50 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 TE_BUSY ){.
1dc51 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 p->pc = pc;.
1dc52 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1dc53 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
1dc54 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1dc55 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 urn;. }. i
1dc56 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1dc57 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 ){. goto a
1dc58 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1dc59 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 r;. }.. if
1dc5a 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e ( pOp->p2 && p->
1dc5b 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a usesStmtJournal.
1dc5c 20 20 20 20 20 26 26 20 28 64 62 2d 3e 61 75 74 && (db->aut
1dc5d 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 oCommit==0 || db
1dc5e 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1dc5f 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 1). ){.
1dc60 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1dc61 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75 2e treeIsInTrans(u.
1dc62 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 20 as.pBt) );.
1dc63 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 if( p->iStateme
1dc64 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 nt==0 ){.
1dc65 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 assert( db->nSt
1dc66 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 atement>=0 && db
1dc67 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 ->nSavepoint>=0
1dc68 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e );. db->n
1dc69 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20 Statement++;.
1dc6a 20 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 p->iStateme
1dc6b 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f nt = db->nSavepo
1dc6c 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 int + db->nState
1dc6d 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ment;. }.
1dc6e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1dc6f 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 75 BtreeBeginStmt(u
1dc70 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61 .as.pBt, p->iSta
1dc71 74 65 6d 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 tement);..
1dc72 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 63 75 72 /* Store the cur
1dc73 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 rent value of th
1dc74 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1dc75 65 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 es deferred cons
1dc76 74 72 61 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 traint. **
1dc77 63 6f 75 6e 74 65 72 2e 20 49 66 20 74 68 65 20 counter. If the
1dc78 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1dc79 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62 ction needs to b
1dc7a 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20 e rolled back,.
1dc7b 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 ** the valu
1dc7c 65 20 6f 66 20 74 68 69 73 20 63 6f 75 6e 74 65 e of this counte
1dc7d 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 r needs to be re
1dc7e 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a stored too. */.
1dc7f 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65 p->nStmtDe
1dc80 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 fCons = db->nDef
1dc81 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d erredCons;. }
1dc82 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1dc83 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 ./* Opcode: Read
1dc84 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
1dc85 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 * *.**.** Read c
1dc86 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 ookie number P3
1dc87 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 from database P1
1dc88 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e and write it in
1dc89 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1dc8a 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 ** P3==1 is the
1dc8b 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 schema version.
1dc8c 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 P3==2 is the da
1dc8d 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a tabase format..*
1dc8e 2a 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 * P3==3 is the r
1dc8f 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 ecommended pager
1dc90 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 cache size, and
1dc91 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d so forth. P1==
1dc92 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 0 is.** the main
1dc93 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1dc94 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
1dc95 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 database file us
1dc96 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 ed to store.** t
1dc97 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
1dc98 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
1dc99 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b t be a read-lock
1dc9a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1dc9b 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 (either a trans
1dc9c 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 action.** must b
1dc9d 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 e started or the
1dc9e 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 re must be an op
1dc9f 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 en cursor) befor
1dca0 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 e.** executing t
1dca1 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e his instruction.
1dca2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 .*/.case OP_Read
1dca3 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 Cookie: {
1dca4 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1dca5 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 prerelease */.#i
1dca6 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1dca7 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1dca8 74 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 to u.at */. int
1dca9 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 iMeta;. int iD
1dcaa 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 b;. int iCookie
1dcab 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1dcac 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1dcad 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a d into u.at */..
1dcae 20 20 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 u.at.iDb = pOp
1dcaf 2d 3e 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f ->p1;. u.at.iCo
1dcb0 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a okie = pOp->p3;.
1dcb1 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1dcb2 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 3<SQLITE_N_BTREE
1dcb3 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 _META );. asser
1dcb4 74 28 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 t( u.at.iDb>=0 &
1dcb5 26 20 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e & u.at.iDb<db->n
1dcb6 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1dcb7 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 db->aDb[u.at.iDb
1dcb8 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 ].pBt!=0 );. as
1dcb9 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
1dcba 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 ask & (1<<u.at.i
1dcbb 44 62 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 Db))!=0 );.. sq
1dcbc 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
1dcbd 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 a(db->aDb[u.at.i
1dcbe 44 62 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 Db].pBt, u.at.iC
1dcbf 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75 ookie, (u32 *)&u
1dcc0 2e 61 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f .at.iMeta);. pO
1dcc1 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 ut->u.i = u.at.i
1dcc2 4d 65 74 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Meta;. break;.}
1dcc3 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 ../* Opcode: Set
1dcc4 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
1dcc5 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1dcc6 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1dcc7 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 egister P3 (inte
1dcc8 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e rpreted as an in
1dcc9 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 teger).** into c
1dcca 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 ookie number P2
1dccb 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 of database P1.
1dccc 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63 P2==1 is the sc
1dccd 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a hema version. .
1dcce 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 ** P2==2 is the
1dccf 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e database format.
1dcd0 20 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65 P2==3 is the re
1dcd1 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 commended pager
1dcd2 63 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 cache .** size,
1dcd3 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 and so forth. P
1dcd4 31 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e 1==0 is the main
1dcd5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1dcd6 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
1dcd7 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
1dcd8 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 e used to store
1dcd9 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1dcda 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 ..**.** A transa
1dcdb 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
1dcdc 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 arted before exe
1dcdd 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f cuting this opco
1dcde 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 de..*/.case OP_S
1dcdf 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 etCookie: {
1dce0 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 /* in3 */.#if
1dce1 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1dce2 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1dce3 20 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 u.au */. Db *p
1dce4 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f Db;.#endif /* lo
1dce5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1dce6 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f ved into u.au */
1dce7 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1dce8 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 p2<SQLITE_N_BTRE
1dce9 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 E_META );. asse
1dcea 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1dceb 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1dcec 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1dced 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1dcee 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1dcef 29 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 );. u.au.pDb =
1dcf0 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 &db->aDb[pOp->p1
1dcf1 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 ];. assert( u.a
1dcf2 75 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b u.pDb->pBt!=0 );
1dcf3 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b . pIn3 = &aMem[
1dcf4 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 73 71 6c 69 pOp->p3];. sqli
1dcf5 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
1dcf6 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a rify(pIn3);. /*
1dcf7 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 See note about
1dcf8 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f index shifting o
1dcf9 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 n OP_ReadCookie
1dcfa 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
1dcfb 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
1dcfc 28 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20 (u.au.pDb->pBt,
1dcfd 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 pOp->p2, (int)pI
1dcfe 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 n3->u.i);. if(
1dcff 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53 pOp->p2==BTREE_S
1dd00 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b CHEMA_VERSION ){
1dd01 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 . /* When the
1dd02 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
1dd03 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 hanges, record t
1dd04 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e he new cookie in
1dd05 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 ternally */.
1dd06 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d u.au.pDb->pSchem
1dd07 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 a->schema_cookie
1dd08 20 3d 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e = (int)pIn3->u.
1dd09 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 i;. db->flags
1dd0a 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 |= SQLITE_Inter
1dd0b 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 nChanges;. }els
1dd0c 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 e if( pOp->p2==B
1dd0d 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 TREE_FILE_FORMAT
1dd0e 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 ){. /* Recor
1dd0f 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 d changes in the
1dd10 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a file format */.
1dd11 20 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 u.au.pDb->pS
1dd12 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d chema->file_form
1dd13 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 at = (u8)pIn3->u
1dd14 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f .i;. }. if( pO
1dd15 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 p->p1==1 ){.
1dd16 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c /* Invalidate al
1dd17 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 l prepared state
1dd18 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 ments whenever t
1dd19 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
1dd1a 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 . ** schema i
1dd1b 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b s changed. Tick
1dd1c 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 et #1644 */.
1dd1d 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1dd1e 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1dd1f 64 62 29 3b 0a 20 20 20 20 70 2d 3e 65 78 70 69 db);. p->expi
1dd20 72 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 red = 0;. }. b
1dd21 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1dd22 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 de: VerifyCookie
1dd23 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 P1 P2 *.**.** C
1dd24 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f heck the value o
1dd25 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 f global databas
1dd26 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 e parameter numb
1dd27 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 er 0 (the.** sch
1dd28 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 ema version) and
1dd29 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 make sure it is
1dd2a 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a equal to P2. .
1dd2b 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74 ** P1 is the dat
1dd2c 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69 abase number whi
1dd2d 63 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20 ch is 0 for the
1dd2e 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1dd2f 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 le.** and 1 for
1dd30 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 the file holding
1dd31 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1dd32 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 s and some highe
1dd33 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 r number.** for
1dd34 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
1dd35 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ses..**.** The c
1dd36 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74 ookie changes it
1dd37 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72 s value whenever
1dd38 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
1dd39 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a hema changes..**
1dd3a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 This operation
1dd3b 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 is used to detec
1dd3c 74 20 77 68 65 6e 20 74 68 61 74 20 74 68 65 20 t when that the
1dd3d 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67 cookie has chang
1dd3e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 ed.** and that t
1dd3f 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 he current proce
1dd40 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65 ss needs to rere
1dd41 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a ad the schema..*
1dd42 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72 *.** Either a tr
1dd43 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 ansaction needs
1dd44 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73 74 61 to have been sta
1dd45 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 rted or an OP_Op
1dd46 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 en needs.** to b
1dd47 65 20 65 78 65 63 75 74 65 64 20 28 74 6f 20 65 e executed (to e
1dd48 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 20 stablish a read
1dd49 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69 lock) before thi
1dd4a 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 s opcode is.** i
1dd4b 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 nvoked..*/.case
1dd4c 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a OP_VerifyCookie:
1dd4d 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1dd4e 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1dd4f 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a ed into u.av */.
1dd50 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 int iMeta;. B
1dd51 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 tree *pBt;.#endi
1dd52 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1dd53 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1dd54 75 2e 61 76 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.av */. assert
1dd55 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1dd56 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1dd57 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1dd58 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1dd59 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1dd5a 0a 20 20 75 2e 61 76 2e 70 42 74 20 3d 20 64 62 . u.av.pBt = db
1dd5b 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1dd5c 42 74 3b 0a 20 20 69 66 28 20 75 2e 61 76 2e 70 Bt;. if( u.av.p
1dd5d 42 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Bt ){. sqlite
1dd5e 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 75 2e 3BtreeGetMeta(u.
1dd5f 61 76 2e 70 42 74 2c 20 42 54 52 45 45 5f 53 43 av.pBt, BTREE_SC
1dd60 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 HEMA_VERSION, (u
1dd61 33 32 20 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61 32 *)&u.av.iMeta
1dd62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1dd63 75 2e 61 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a u.av.iMeta = 0;.
1dd64 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 76 2e 69 }. if( u.av.i
1dd65 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b Meta!=pOp->p2 ){
1dd66 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1dd67 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1dd68 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d g);. p->zErrM
1dd69 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 sg = sqlite3DbSt
1dd6a 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 61 rDup(db, "databa
1dd6b 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 se schema has ch
1dd6c 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 anged");. /*
1dd6d 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f If the schema-co
1dd6e 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 okie from the da
1dd6f 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63 tabase file matc
1dd70 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 hes the cookie.
1dd71 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 ** stored wit
1dd72 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 h the in-memory
1dd73 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
1dd74 66 20 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f f the schema, do
1dd75 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f . ** not relo
1dd76 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 72 ad the schema fr
1dd77 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
1dd78 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 file.. **.
1dd79 20 2a 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74 ** If virtual-t
1dd7a 61 62 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65 ables are in use
1dd7b 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 , this is not ju
1dd7c 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 st an optimizati
1dd7d 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e on.. ** Often
1dd7e 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 , v-tables store
1dd7f 20 74 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f their data in o
1dd80 74 68 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c ther SQLite tabl
1dd81 65 73 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a es, which. **
1dd82 20 61 72 65 20 71 75 65 72 69 65 64 20 66 72 6f are queried fro
1dd83 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29 m within xNext()
1dd84 20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62 and other v-tab
1dd85 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 le methods using
1dd86 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64 . ** prepared
1dd87 20 71 75 65 72 69 65 73 2e 20 49 66 20 73 75 63 queries. If suc
1dd88 68 20 61 20 71 75 65 72 79 20 69 73 20 6f 75 74 h a query is out
1dd89 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 -of-date, we do
1dd8a 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 not want to.
1dd8b 2a 2a 20 64 69 73 63 61 72 64 20 74 68 65 20 64 ** discard the d
1dd8c 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20 atabase schema,
1dd8d 61 73 20 74 68 65 20 75 73 65 72 20 63 6f 64 65 as the user code
1dd8e 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 implementing th
1dd8f 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 e. ** v-table
1dd90 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62 would have to b
1dd91 65 20 72 65 61 64 79 20 66 6f 72 20 74 68 65 20 e ready for the
1dd92 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 sqlite3_vtab str
1dd93 75 63 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20 ucture itself.
1dd94 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c ** to be inval
1dd95 69 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20 idated whenever
1dd96 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 sqlite3_step() i
1dd97 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 s called from wi
1dd98 74 68 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d thin. ** a v-
1dd99 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 table method..
1dd9a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d */. if( db-
1dd9b 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 >aDb[pOp->p1].pS
1dd9c 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f chema->schema_co
1dd9d 6f 6b 69 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61 okie!=u.av.iMeta
1dd9e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1dd9f 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1dda0 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 hema(db, pOp->p1
1dda1 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 );. }.. sq
1dda2 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1dda3 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1dda4 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
1dda5 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 TE_SCHEMA;. }.
1dda6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1dda7 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 code: OpenRead P
1dda8 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1dda9 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d .** Open a read-
1ddaa 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 only cursor for
1ddab 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 the database tab
1ddac 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 le whose root pa
1ddad 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 ge is.** P2 in a
1ddae 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1ddaf 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
1ddb0 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 le is determined
1ddb1 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d by P3. .** P3==
1ddb2 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 0 means the main
1ddb3 20 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 database, P3==1
1ddb4 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 means the datab
1ddb5 61 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a ase used for .**
1ddb6 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1ddb7 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e s, and P3>1 mean
1ddb8 73 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 s used the corre
1ddb9 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 sponding attache
1ddba 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 d.** database.
1ddbb 47 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 Give the new cur
1ddbc 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 sor an identifie
1ddbd 72 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 r of P1. The P1
1ddbe 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 .** values need
1ddbf 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 not be contiguou
1ddc0 73 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c s but all P1 val
1ddc1 75 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d ues should be sm
1ddc2 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a all integers..**
1ddc3 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 It is an error
1ddc4 66 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 for P1 to be neg
1ddc5 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ative..**.** If
1ddc6 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 P5!=0 then use t
1ddc7 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1ddc8 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65 gister P2 as the
1ddc9 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a root page, not.
1ddca 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
1ddcb 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a P2 itself..**.**
1ddcc 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 There will be a
1ddcd 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 read lock on th
1ddce 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 e database whene
1ddcf 76 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a ver there is an.
1ddd0 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 ** open cursor.
1ddd1 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
1ddd2 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 was unlocked pr
1ddd3 69 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 ior to this inst
1ddd4 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 ruction.** then
1ddd5 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 a read lock is a
1ddd6 63 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20 cquired as part
1ddd7 6f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 of this instruct
1ddd8 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 ion. A read.**
1ddd9 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 lock allows othe
1ddda 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 r processes to r
1dddb 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 ead the database
1dddc 20 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a but prohibits.*
1dddd 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 * any other proc
1ddde 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 ess from modifyi
1dddf 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ng the database.
1dde0 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 The read lock
1dde1 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 is.** released w
1dde2 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 hen all cursors
1dde3 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 are closed. If
1dde4 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1dde5 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 attempts.** to
1dde6 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 get a read lock
1dde7 62 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 but fails, the s
1dde8 63 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 cript terminates
1dde9 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 with an.** SQLI
1ddea 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f TE_BUSY error co
1ddeb 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 de..**.** The P4
1ddec 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 value may be ei
1dded 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 ther an integer
1ddee 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 (P4_INT32) or a
1ddef 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 pointer to.** a
1ddf0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1ddf1 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 e (P4_KEYINFO).
1ddf2 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 If it is a point
1ddf3 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 er to a KeyInfo
1ddf4 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 .** structure, t
1ddf5 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 hen said structu
1ddf6 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 re defines the c
1ddf7 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 ontent and colla
1ddf8 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 ting .** sequenc
1ddf9 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 e of the index b
1ddfa 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 eing opened. Oth
1ddfb 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 erwise, if P4 is
1ddfc 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 an integer .**
1ddfd 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 value, it is set
1ddfe 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
1ddff 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1de00 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 table..**.** Se
1de01 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 e also OpenWrite
1de02 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1de03 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 OpenWrite P1 P2
1de04 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f P3 P4 P5.**.** O
1de05 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 pen a read/write
1de06 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 cursor named P1
1de07 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 on the table or
1de08 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1de09 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e t.** page is P2.
1de0a 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 Or if P5!=0 us
1de0b 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
1de0c 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 register P2 to
1de0d 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 find the.** root
1de0e 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 page..**.** The
1de0f 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 P4 value may be
1de10 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 either an integ
1de11 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 er (P4_INT32) or
1de12 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
1de13 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
1de14 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f ture (P4_KEYINFO
1de15 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f ). If it is a po
1de16 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
1de17 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 fo .** structure
1de18 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 , then said stru
1de19 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 cture defines th
1de1a 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f e content and co
1de1b 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 llating .** sequ
1de1c 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 ence of the inde
1de1d 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 x being opened.
1de1e 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 Otherwise, if P4
1de1f 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a is an integer .
1de20 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 ** value, it is
1de21 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
1de22 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1de23 74 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f the table, or to
1de24 20 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 the.** largest
1de25 69 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c index of any col
1de26 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 umn of the table
1de27 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c that is actuall
1de28 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 y used..**.** Th
1de29 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 is instruction w
1de2a 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f orks just like O
1de2b 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74 penRead except t
1de2c 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65 hat it opens the
1de2d 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 cursor.** in re
1de2e 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 ad/write mode.
1de2f 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c For a given tabl
1de30 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20 e, there can be
1de31 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 one or more read
1de32 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 -only.** cursors
1de33 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61 or a single rea
1de34 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62 d/write cursor b
1de35 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a ut not both..**.
1de36 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e ** See also Open
1de37 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 Read..*/.case OP
1de38 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 _OpenRead:.case
1de39 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a OP_OpenWrite: {.
1de3a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1de3b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1de3c 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69 into u.aw */. i
1de3d 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79 nt nField;. Key
1de3e 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a Info *pKeyInfo;.
1de3f 20 20 69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20 int p2;. int
1de40 69 44 62 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 iDb;. int wrFla
1de41 67 3b 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a g;. Btree *pX;.
1de42 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1de43 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 ur;. Db *pDb;.#
1de44 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1de45 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1de46 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 69 nto u.aw */.. i
1de47 66 28 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b f( p->expired ){
1de48 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1de49 5f 41 42 4f 52 54 3b 0a 20 20 20 20 62 72 65 61 _ABORT;. brea
1de4a 6b 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 77 2e 6e k;. }.. u.aw.n
1de4b 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61 Field = 0;. u.a
1de4c 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a w.pKeyInfo = 0;.
1de4d 20 20 75 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d u.aw.p2 = pOp-
1de4e 3e 70 32 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20 >p2;. u.aw.iDb
1de4f 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 = pOp->p3;. ass
1de50 65 72 74 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30 ert( u.aw.iDb>=0
1de51 20 26 26 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d && u.aw.iDb<db-
1de52 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1de53 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1de54 26 20 28 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29 & (1<<u.aw.iDb))
1de55 21 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44 !=0 );. u.aw.pD
1de56 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61 b = &db->aDb[u.a
1de57 77 2e 69 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70 w.iDb];. u.aw.p
1de58 58 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42 X = u.aw.pDb->pB
1de59 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 t;. assert( u.a
1de5a 77 2e 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28 w.pX!=0 );. if(
1de5b 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1de5c 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20 _OpenWrite ){.
1de5d 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 u.aw.wrFlag =
1de5e 31 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 77 2e 1;. if( u.aw.
1de5f 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
1de60 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d le_format < p->m
1de61 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 inWriteFileForma
1de62 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 t ){. p->mi
1de63 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 nWriteFileFormat
1de64 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 = u.aw.pDb->pSc
1de65 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
1de66 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 t;. }. }else
1de67 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61 {. u.aw.wrFla
1de68 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 g = 0;. }. if(
1de69 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 pOp->p5 ){.
1de6a 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3e assert( u.aw.p2>
1de6b 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1de6c 20 75 2e 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 u.aw.p2<=p->nMe
1de6d 6d 20 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 m );. pIn2 =
1de6e 26 61 4d 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a &aMem[u.aw.p2];.
1de6f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1de70 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e emIntegerify(pIn
1de71 32 29 3b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20 2);. u.aw.p2
1de72 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 = (int)pIn2->u.i
1de73 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61 ;. /* The u.a
1de74 77 2e 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79 w.p2 value alway
1de75 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70 s comes from a p
1de76 72 69 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61 rior OP_CreateTa
1de77 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20 ble opcode and.
1de78 20 20 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64 ** that opcod
1de79 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 73 65 e will always se
1de7a 74 20 74 68 65 20 75 2e 61 77 2e 70 32 20 76 61 t the u.aw.p2 va
1de7b 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65 lue to 2 or more
1de7c 20 6f 72 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20 or else fail..
1de7d 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 77 ** If there w
1de7e 65 72 65 20 61 20 66 61 69 6c 75 72 65 2c 20 74 ere a failure, t
1de7f 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 he prepared stat
1de80 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68 61 76 65 ement would have
1de81 20 68 61 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62 halted. ** b
1de82 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 efore reaching t
1de83 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e his instruction.
1de84 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 */. if( NEVE
1de85 52 28 75 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b R(u.aw.p2<2) ) {
1de86 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1de87 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1de88 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1de89 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1de8a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
1de8b 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1de8c 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75 KEYINFO ){. u
1de8d 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 .aw.pKeyInfo = p
1de8e 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b Op->p4.pKeyInfo;
1de8f 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e . u.aw.pKeyIn
1de90 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d fo->enc = ENC(p-
1de91 3e 64 62 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e >db);. u.aw.n
1de92 46 69 65 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65 Field = u.aw.pKe
1de93 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b yInfo->nField+1;
1de94 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 . }else if( pOp
1de95 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 ->p4type==P4_INT
1de96 33 32 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e 32 ){. u.aw.n
1de97 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e Field = pOp->p4.
1de98 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 i;. }. assert(
1de99 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 pOp->p1>=0 );.
1de9a 20 75 2e 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c u.aw.pCur = all
1de9b 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 ocateCursor(p, p
1de9c 4f 70 2d 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69 Op->p1, u.aw.nFi
1de9d 65 6c 64 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31 eld, u.aw.iDb, 1
1de9e 29 3b 0a 20 20 69 66 28 20 75 2e 61 77 2e 70 43 );. if( u.aw.pC
1de9f 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ur==0 ) goto no_
1dea0 6d 65 6d 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 mem;. u.aw.pCur
1dea1 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
1dea2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1dea3 65 65 43 75 72 73 6f 72 28 75 2e 61 77 2e 70 58 eeCursor(u.aw.pX
1dea4 2c 20 75 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e , u.aw.p2, u.aw.
1dea5 77 72 46 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65 wrFlag, u.aw.pKe
1dea6 79 49 6e 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72 yInfo, u.aw.pCur
1dea7 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e ->pCursor);. u.
1dea8 61 77 2e 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 aw.pCur->pKeyInf
1dea9 6f 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 o = u.aw.pKeyInf
1deaa 6f 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69 o;.. /* Since i
1deab 74 20 70 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65 t performs no me
1deac 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1dead 6f 72 20 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20 or IO, the only
1deae 76 61 6c 75 65 73 20 74 68 61 74 0a 20 20 2a 2a values that. **
1deaf 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1deb0 73 6f 72 28 29 20 6d 61 79 20 72 65 74 75 72 6e sor() may return
1deb1 20 61 72 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 are SQLITE_EMPT
1deb2 59 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e Y and SQLITE_OK.
1deb3 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50 . ** SQLITE_EMP
1deb4 54 59 20 69 73 20 6f 6e 6c 79 20 72 65 74 75 72 TY is only retur
1deb5 6e 65 64 20 77 68 65 6e 20 61 74 74 65 6d 70 74 ned when attempt
1deb6 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ing to open the
1deb7 74 61 62 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65 table. ** roote
1deb8 64 20 61 74 20 70 61 67 65 20 31 20 6f 66 20 61 d at page 1 of a
1deb9 20 7a 65 72 6f 2d 62 79 74 65 20 64 61 74 61 62 zero-byte datab
1deba 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 ase. */. asser
1debb 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d t( rc==SQLITE_EM
1debc 50 54 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 PTY || rc==SQLIT
1debd 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 E_OK );. if( rc
1debe 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 ==SQLITE_EMPTY )
1debf 7b 0a 20 20 20 20 75 2e 61 77 2e 70 43 75 72 2d {. u.aw.pCur-
1dec0 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 >pCursor = 0;.
1dec1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1dec2 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 ;. }.. /* Set
1dec3 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69 the VdbeCursor.i
1dec4 73 54 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 sTable and isInd
1dec5 65 78 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72 ex variables. Pr
1dec6 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 evious versions
1dec7 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 of. ** SQLite u
1dec8 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20 sed to check if
1dec9 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c the root-page fl
1deca 61 67 73 20 77 65 72 65 20 73 61 6e 65 20 61 74 ags were sane at
1decb 20 74 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a this point. **
1decc 20 61 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61 and report data
1decd 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 base corruption
1dece 69 66 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74 if they were not
1decf 2c 20 62 75 74 20 74 68 69 73 20 63 68 65 63 6b , but this check
1ded0 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20 has. ** since
1ded1 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62 moved into the b
1ded2 74 72 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a tree layer. */.
1ded3 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 u.aw.pCur->isT
1ded4 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 able = pOp->p4ty
1ded5 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a pe!=P4_KEYINFO;.
1ded6 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49 u.aw.pCur->isI
1ded7 6e 64 65 78 20 3d 20 21 75 2e 61 77 2e 70 43 75 ndex = !u.aw.pCu
1ded8 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 r->isTable;. br
1ded9 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1deda 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c e: OpenEphemeral
1dedb 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a P1 P2 * P4 *.**
1dedc 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 .** Open a new c
1dedd 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72 ursor P1 to a tr
1dede 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a ansient table..*
1dedf 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 * The cursor is
1dee0 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65 always opened re
1dee1 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66 ad/write even if
1dee2 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 .** the main da
1dee3 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f tabase is read-o
1dee4 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69 nly. The transi
1dee5 65 6e 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a ent or virtual.*
1dee6 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74 * table is delet
1dee7 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
1dee8 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 when the cursor
1dee9 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a is closed..**.*
1deea 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 * P2 is the numb
1deeb 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
1deec 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
1deed 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f le..** The curso
1deee 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 r points to a BT
1deef 72 65 65 20 74 61 62 6c 65 20 69 66 20 50 34 3d ree table if P4=
1def0 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65 =0 and to a BTre
1def1 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34 e index.** if P4
1def2 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 is not 0. If P
1def3 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 4 is not NULL, i
1def4 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 t points to a Ke
1def5 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a yInfo structure.
1def6 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 ** that defines
1def7 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 the format of ke
1def8 79 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e ys in the index.
1def9 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1defa 64 65 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c de was once call
1defb 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 ed OpenTemp. Bu
1defc 74 20 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a t that created.*
1defd 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 * confusion beca
1defe 75 73 65 20 74 68 65 20 74 65 72 6d 20 22 74 65 use the term "te
1deff 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 mp table", might
1df00 20 72 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a refer either.**
1df01 20 74 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65 to a TEMP table
1df02 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65 at the SQL leve
1df03 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 l, or to a table
1df04 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 opened by.** th
1df05 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e is opcode. Then
1df06 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 this opcode was
1df07 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 call OpenVirtua
1df08 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 l. But.** that
1df09 63 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f created confusio
1df0a 6e 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65 n with the whole
1df0b 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 virtual-table i
1df0c 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f dea..*/.case OP_
1df0d 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b OpenEphemeral: {
1df0e 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1df0f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1df10 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 into u.ax */.
1df11 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b VdbeCursor *pCx;
1df12 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1df13 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1df14 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 into u.ax */.
1df15 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 static const int
1df16 20 6f 70 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20 openFlags =.
1df17 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 SQLITE_OPEN_R
1df18 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 EADWRITE |.
1df19 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
1df1a 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 ATE |. SQLI
1df1b 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
1df1c 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
1df1d 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
1df1e 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 OSE |. SQLI
1df1f 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e TE_OPEN_TRANSIEN
1df20 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 T_DB;.. assert(
1df21 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 pOp->p1>=0 );.
1df22 20 75 2e 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f u.ax.pCx = allo
1df23 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f cateCursor(p, pO
1df24 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 p->p1, pOp->p2,
1df25 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e -1, 1);. if( u.
1df26 61 78 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f ax.pCx==0 ) goto
1df27 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e no_mem;. u.ax.
1df28 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 pCx->nullRow = 1
1df29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1df2a 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c BtreeFactory(db,
1df2b 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 0, 1, SQLITE_DE
1df2c 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 FAULT_TEMP_CACHE
1df2d 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 _SIZE, openFlags
1df2e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1df2f 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 2e &u.
1df30 61 78 2e 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 ax.pCx->pBt);.
1df31 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1df32 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 K ){. rc = sq
1df33 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
1df34 72 61 6e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70 rans(u.ax.pCx->p
1df35 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 Bt, 1);. }. if
1df36 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1df37 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 ){. /* If a t
1df38 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69 ransient index i
1df39 73 20 72 65 71 75 69 72 65 64 2c 20 63 72 65 61 s required, crea
1df3a 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 te it by calling
1df3b 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 . ** sqlite3B
1df3c 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 treeCreateTable(
1df3d 29 20 77 69 74 68 20 74 68 65 20 42 54 52 45 45 ) with the BTREE
1df3e 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62 _ZERODATA flag b
1df3f 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 efore. ** ope
1df40 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72 ning it. If a tr
1df41 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73 ansient table is
1df42 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20 required, just
1df43 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 use the. ** a
1df44 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 utomatically cre
1df45 61 74 65 64 20 74 61 62 6c 65 20 77 69 74 68 20 ated table with
1df46 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20 root-page 1 (an
1df47 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 INTKEY table)..
1df48 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f */. if( pO
1df49 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 p->p4.pKeyInfo )
1df4a 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f {. int pgno
1df4b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1df4c 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1df4d 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 KEYINFO );.
1df4e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1df4f 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e eeCreateTable(u.
1df50 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 ax.pCx->pBt, &pg
1df51 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 no, BTREE_ZERODA
1df52 54 41 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 TA);. if( r
1df53 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1df54 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1df55 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f pgno==MASTER_ROO
1df56 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72 T+1 );. r
1df57 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1df58 43 75 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d Cursor(u.ax.pCx-
1df59 3e 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20 >pBt, pgno, 1,.
1df5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1df5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1df5c 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 KeyInfo*)pOp->p4
1df5d 2e 7a 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 .z, u.ax.pCx->pC
1df5e 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 ursor);.
1df5f 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e u.ax.pCx->pKeyIn
1df60 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
1df61 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 75 yInfo;. u
1df62 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 .ax.pCx->pKeyInf
1df63 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e o->enc = ENC(p->
1df64 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 db);. }.
1df65 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 u.ax.pCx->isT
1df66 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 able = 0;. }e
1df67 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1df68 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1df69 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 or(u.ax.pCx->pBt
1df6a 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 , MASTER_ROOT, 1
1df6b 2c 20 30 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 , 0, u.ax.pCx->p
1df6c 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 75 Cursor);. u
1df6d 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 .ax.pCx->isTable
1df6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
1df6f 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e u.ax.pCx->isIn
1df70 64 65 78 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d dex = !u.ax.pCx-
1df71 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 >isTable;. brea
1df72 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1df73 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50 OpenPseudo P1 P
1df74 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 2 P3 * *.**.** O
1df75 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 pen a new cursor
1df76 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 that points to
1df77 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 a fake table tha
1df78 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e t contains a sin
1df79 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 gle.** row of da
1df7a 74 61 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 ta. The content
1df7b 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 of that one row
1df7c 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 in the content
1df7d 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 of memory.** reg
1df7e 69 73 74 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 ister P2. In ot
1df7f 68 65 72 20 77 6f 72 64 73 2c 20 63 75 72 73 6f her words, curso
1df80 72 20 50 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 r P1 becomes an
1df81 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 0a 2a alias for the .*
1df82 2a 20 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 * MEM_Blob conte
1df83 6e 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 nt contained in
1df84 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1df85 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c ** A pseudo-tabl
1df86 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 e created by thi
1df87 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 s opcode is used
1df88 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 to hold the a s
1df89 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 ingle.** row out
1df8a 70 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 put from the sor
1df8b 74 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 ter so that the
1df8c 72 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d row can be decom
1df8d 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e posed into.** in
1df8e 64 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 dividual columns
1df8f 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f using the OP_Co
1df90 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 lumn opcode. Th
1df91 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f e OP_Column opco
1df92 64 65 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c de.** is the onl
1df93 79 20 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 y cursor opcode
1df94 74 68 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20 that works with
1df95 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a a pseudo-table..
1df96 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20 **.** P3 is the
1df97 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 number of fields
1df98 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20 in the records
1df99 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f that will be sto
1df9a 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 red by.** the ps
1df9b 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 eudo-table..*/.c
1df9c 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 ase OP_OpenPseud
1df9d 6f 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c o: {.#if 0 /* l
1df9e 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1df9f 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a oved into u.ay *
1dfa0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1dfa1 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c pCx;.#endif /* l
1dfa2 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1dfa3 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a oved into u.ay *
1dfa4 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1dfa5 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 ->p1>=0 );. u.a
1dfa6 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 y.pCx = allocate
1dfa7 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 Cursor(p, pOp->p
1dfa8 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 1, pOp->p3, -1,
1dfa9 30 29 3b 0a 20 20 69 66 28 20 75 2e 61 79 2e 70 0);. if( u.ay.p
1dfaa 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f Cx==0 ) goto no_
1dfab 6d 65 6d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d mem;. u.ay.pCx-
1dfac 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 >nullRow = 1;.
1dfad 75 2e 61 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f u.ay.pCx->pseudo
1dfae 54 61 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e TableReg = pOp->
1dfaf 70 32 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e p2;. u.ay.pCx->
1dfb0 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 isTable = 1;. u
1dfb1 2e 61 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 .ay.pCx->isIndex
1dfb2 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1dfb3 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f ../* Opcode: Clo
1dfb4 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a se P1 * * * *.**
1dfb5 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 .** Close a curs
1dfb6 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 or previously op
1dfb7 65 6e 65 64 20 61 73 20 50 31 2e 20 20 49 66 20 ened as P1. If
1dfb8 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 P1 is not.** cur
1dfb9 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 rently open, thi
1dfba 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 s instruction is
1dfbb 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 a no-op..*/.cas
1dfbc 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 e OP_Close: {.
1dfbd 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1dfbe 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1dfbf 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 >nCursor );. sq
1dfc0 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 lite3VdbeFreeCur
1dfc1 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b sor(p, p->apCsr[
1dfc2 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e pOp->p1]);. p->
1dfc3 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d apCsr[pOp->p1] =
1dfc4 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
1dfc5 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 /* Opcode: SeekG
1dfc6 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a e P1 P2 P3 P4 *.
1dfc7 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 **.** If cursor
1dfc8 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 P1 refers to an
1dfc9 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 SQL table (B-Tre
1dfca 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 e that uses inte
1dfcb 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 ger keys), .** u
1dfcc 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 se the value in
1dfcd 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 74 register P3 as t
1dfce 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73 he key. If curs
1dfcf 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a or P1 refers .**
1dfd0 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 to an SQL index
1dfd1 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 , then P3 is the
1dfd2 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 first in an arr
1dfd3 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 ay of P4 registe
1dfd4 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 rs .** that are
1dfd5 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 used as an unpac
1dfd6 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a ked index key. .
1dfd7 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e **.** Reposition
1dfd8 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1dfd9 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f at it points to
1dfda 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e the smallest en
1dfdb 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 try that .** is
1dfdc 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1dfdd 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
1dfde 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1dfdf 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1dfe0 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
1dfe1 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1dfe2 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e key and P2 is n
1dfe3 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 ot zero, then ju
1dfe4 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
1dfe5 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c See also: Found,
1dfe6 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 NotFound, Disti
1dfe7 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 nct, SeekLt, See
1dfe8 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f kGt, SeekLe.*/./
1dfe9 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 * Opcode: SeekGt
1dfea 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1dfeb 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
1dfec 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
1dfed 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
1dfee 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
1dfef 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
1dff0 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1dff1 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 egister P3 as a
1dff2 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 key. If cursor P
1dff3 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 1 refers .** to
1dff4 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 an SQL index, th
1dff5 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 en P3 is the fir
1dff6 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f st in an array o
1dff7 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a f P4 registers .
1dff8 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 ** that are used
1dff9 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 as an unpacked
1dffa 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a index key. .**.*
1dffb 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 * Reposition cur
1dffc 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 sor P1 so that
1dffd 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
1dffe 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 smallest entry
1dfff 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 that .** is grea
1e000 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79 ter than the key
1e001 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1e002 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1e003 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a greater than .**
1e004 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1e005 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1e006 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1e007 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1e008 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 und, NotFound, D
1e009 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c istinct, SeekLt,
1e00a 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a SeekGe, SeekLe.
1e00b 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 */./* Opcode: Se
1e00c 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 ekLt P1 P2 P3 P4
1e00d 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 * .**.** If cur
1e00e 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f sor P1 refers to
1e00f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 an SQL table (B
1e010 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 -Tree that uses
1e011 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a integer keys), .
1e012 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 ** use the value
1e013 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1e014 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 as a key. If cur
1e015 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a sor P1 refers .*
1e016 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 * to an SQL inde
1e017 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 x, then P3 is th
1e018 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 e first in an ar
1e019 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 ray of P4 regist
1e01a 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 ers .** that are
1e01b 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 used as an unpa
1e01c 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 cked index key.
1e01d 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f .**.** Repositio
1e01e 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 n cursor P1 so t
1e01f 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 hat it points t
1e020 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e o the largest en
1e021 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 try that .** is
1e022 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 less than the ke
1e023 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 y value. If ther
1e024 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 e are no records
1e025 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 less than .** t
1e026 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 he key and P2 is
1e027 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1e028 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a jump to P2..**.*
1e029 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1e02a 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 d, NotFound, Dis
1e02b 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 tinct, SeekGt, S
1e02c 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f eekGe, SeekLe.*/
1e02d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1e02e 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a Le P1 P2 P3 P4 *
1e02f 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 .**.** If cursor
1e030 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e P1 refers to an
1e031 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 SQL table (B-Tr
1e032 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 ee that uses int
1e033 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 eger keys), .**
1e034 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e use the value in
1e035 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1e036 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1e037 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1e038 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1e039 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1e03a 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1e03b 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1e03c 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1e03d 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1e03e 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1e03f 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1e040 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1e041 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
1e042 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 e largest entry
1e043 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 that .** is less
1e044 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1e045 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e o the key value.
1e046 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1e047 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 records .** les
1e048 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
1e049 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 to the key and P
1e04a 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 2 is not zero, t
1e04b 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a hen jump to P2..
1e04c 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1e04d 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1e04e 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 Distinct, SeekG
1e04f 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c t, SeekGe, SeekL
1e050 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 t.*/.case OP_See
1e051 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kLt: /*
1e052 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1e053 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 e OP_SeekLe:
1e054 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e055 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 3 */.case OP_See
1e056 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kGe: /*
1e057 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1e058 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 e OP_SeekGt: {
1e059 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e05a 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 3 */.#if 0 /* l
1e05b 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e05c 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a oved into u.az *
1e05d 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 /. int res;. i
1e05e 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65 43 75 72 nt oc;. VdbeCur
1e05f 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63 sor *pC;. Unpac
1e060 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 69 kedRecord r;. i
1e061 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34 nt nField;. i64
1e062 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 iKey; /* T
1e063 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 65 20 he rowid we are
1e064 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65 to seek to */.#e
1e065 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e066 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e067 74 6f 20 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73 to u.az */.. as
1e068 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e069 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e06a 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1e06b 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 rt( pOp->p2!=0 )
1e06c 3b 0a 20 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d ;. u.az.pC = p-
1e06d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e06e 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e . assert( u.az.
1e06f 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1e070 74 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75 t( u.az.pC->pseu
1e071 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b doTableReg==0 );
1e072 0a 20 20 61 73 73 65 72 74 28 20 4f 50 5f 53 65 . assert( OP_Se
1e073 65 6b 4c 65 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c ekLe == OP_SeekL
1e074 74 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 t+1 );. assert(
1e075 20 4f 50 5f 53 65 65 6b 47 65 20 3d 3d 20 4f 50 OP_SeekGe == OP
1e076 5f 53 65 65 6b 4c 74 2b 32 20 29 3b 0a 20 20 61 _SeekLt+2 );. a
1e077 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 47 74 ssert( OP_SeekGt
1e078 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 33 20 == OP_SeekLt+3
1e079 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 );. if( u.az.pC
1e07a 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a ->pCursor!=0 ){.
1e07b 20 20 20 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f u.az.oc = pO
1e07c 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 75 p->opcode;. u
1e07d 2e 61 7a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .az.pC->nullRow
1e07e 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 = 0;. if( u.a
1e07f 7a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b z.pC->isTable ){
1e080 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e . /* The in
1e081 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20 put value in P3
1e082 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20 might be of any
1e083 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 type: integer, r
1e084 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 eal, string,.
1e085 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e ** blob, or N
1e086 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65 ULL. But it nee
1e087 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 ds to be an inte
1e088 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61 ger before we ca
1e089 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 n do. ** th
1e08a 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 e seek, so cover
1e08b 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 t it. */. p
1e08c 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In3 = &aMem[pOp-
1e08d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 61 70 70 6c >p3];. appl
1e08e 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 yNumericAffinity
1e08f 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e (pIn3);. u.
1e090 61 7a 2e 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 az.iKey = sqlite
1e091 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1e092 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e n3);. u.az.
1e093 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e094 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 = 0;.. /*
1e095 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 If the P3 value
1e096 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e could not be con
1e097 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 verted into an i
1e098 6e 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20 nteger without.
1e099 20 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 ** loss of
1e09a 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 information, the
1e09b 6e 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 n special proces
1e09c 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 sing is required
1e09d 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ... */. if(
1e09e 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 (pIn3->flags &
1e09f 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 MEM_Int)==0 ){.
1e0a0 20 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 if( (pIn3
1e0a1 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ->flags & MEM_Re
1e0a2 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 al)==0 ){.
1e0a3 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 /* If the P3
1e0a4 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 value cannot be
1e0a5 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
1e0a6 61 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 any kind of a nu
1e0a7 6d 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 mber,.
1e0a8 2a 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b ** then the seek
1e0a9 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1e0aa 2c 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 , so jump to P2
1e0ab 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 */. pc
1e0ac 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1e0ad 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1e0ae 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e0af 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 /* If we reach
1e0b0 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 this point, the
1e0b1 6e 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d n the P3 value m
1e0b2 75 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e ust be a floatin
1e0b3 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 g. ** poi
1e0b4 6e 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 nt number. */.
1e0b5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
1e0b6 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In3->flags & MEM
1e0b7 5f 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 _Real)!=0 );..
1e0b8 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 69 if( u.az.i
1e0b9 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e Key==SMALLEST_IN
1e0ba 54 36 34 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c T64 && (pIn3->r<
1e0bb 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 (double)u.az.iKe
1e0bc 79 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 y || pIn3->r>0)
1e0bd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1e0be 54 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 The P3 value is
1e0bf 74 6f 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 too large in mag
1e0c0 6e 69 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 nitude to be exp
1e0c1 72 65 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 ressed as an.
1e0c2 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 ** intege
1e0c3 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 r. */.
1e0c4 75 2e 61 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20 u.az.res = 1;.
1e0c5 20 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 if( pIn3
1e0c6 2d 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 ->r<0 ){.
1e0c7 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 if( u.az.oc
1e0c8 3e 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 20 20 >=OP_SeekGe ){
1e0c9 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d assert( u.az.oc=
1e0ca 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e =OP_SeekGe || u.
1e0cb 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 az.oc==OP_SeekGt
1e0cc 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
1e0cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e0ce 72 65 65 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 reeFirst(u.az.pC
1e0cf 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a ->pCursor, &u.az
1e0d0 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 .res);.
1e0d1 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1e0d2 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1e0d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1e0d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1e0d5 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
1e0d6 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1e0d7 20 75 2e 61 7a 2e 6f 63 3c 3d 4f 50 5f 53 65 65 u.az.oc<=OP_See
1e0d8 6b 4c 65 20 29 7b 20 20 61 73 73 65 72 74 28 20 kLe ){ assert(
1e0d9 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1e0da 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f Lt || u.az.oc==O
1e0db 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 P_SeekLe );.
1e0dc 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
1e0dd 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 qlite3BtreeLast(
1e0de 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.az.pC->pCursor
1e0df 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 , &u.az.res);.
1e0e0 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1e0e1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1e0e2 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1e0e3 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1e0e4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1e0e5 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 }. if(
1e0e6 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 u.az.res ){.
1e0e7 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f pc = pO
1e0e8 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1e0e9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1e0ea 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1e0eb 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f }else if( u.az.o
1e0ec 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 c==OP_SeekLt ||
1e0ed 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1e0ee 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Ge ){.
1e0ef 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 6c 69 /* Use the ceili
1e0f0 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f ng() function to
1e0f1 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 convert real->i
1e0f2 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 nt */.
1e0f3 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 if( pIn3->r > (d
1e0f4 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 ouble)u.az.iKey
1e0f5 29 20 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 ) u.az.iKey++;.
1e0f6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1e0f7 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 /* Use t
1e0f8 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 he floor() funct
1e0f9 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 ion to convert r
1e0fa 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 eal->int */.
1e0fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1e0fc 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 az.oc==OP_SeekLe
1e0fd 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f || u.az.oc==OP_
1e0fe 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 SeekGt );.
1e0ff 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 if( pIn3->r
1e100 3c 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 < (double)u.az.i
1e101 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d Key ) u.az.iKey-
1e102 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 -;. }.
1e103 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1e104 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1e105 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e toUnpacked(u.az.
1e106 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1e107 28 75 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 (u64)u.az.iKey,
1e108 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 0, &u.az.res);.
1e109 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1e10a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1e10b 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1e10c 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1e10d 20 7d 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 }. if( u.a
1e10e 7a 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 z.res==0 ){.
1e10f 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 u.az.pC->row
1e110 69 64 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 idIsValid = 1;.
1e111 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e u.az.pC->
1e112 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a lastRowid = u.az
1e113 2e 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20 .iKey;. }.
1e114 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1e115 75 2e 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f u.az.nField = pO
1e116 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 p->p4.i;. a
1e117 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1e118 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a pe==P4_INT32 );.
1e119 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1e11a 61 7a 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20 az.nField>0 );.
1e11b 20 20 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 u.az.r.pKey
1e11c 49 6e 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e Info = u.az.pC->
1e11d 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 pKeyInfo;.
1e11e 75 2e 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 u.az.r.nField =
1e11f 28 75 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 (u16)u.az.nField
1e120 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ;.. /* The
1e121 6e 65 78 74 20 6c 69 6e 65 20 6f 66 20 63 6f 64 next line of cod
1e122 65 20 63 6f 6d 70 75 74 65 73 20 61 73 20 66 6f e computes as fo
1e123 6c 6c 6f 77 73 2c 20 6f 6e 6c 79 20 66 61 73 74 llows, only fast
1e124 65 72 3a 0a 20 20 20 20 20 20 2a 2a 20 20 20 69 er:. ** i
1e125 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 f( u.az.oc==OP_S
1e126 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 eekGt || u.az.oc
1e127 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 ==OP_SeekLe ){.
1e128 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a ** u.az
1e129 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 .r.flags = UNPAC
1e12a 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 KED_INCRKEY;.
1e12b 20 20 20 2a 2a 20 20 20 7d 65 6c 73 65 7b 0a 20 ** }else{.
1e12c 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a ** u.az
1e12d 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 .r.flags = 0;.
1e12e 20 20 20 20 2a 2a 20 20 20 7d 0a 20 20 20 20 20 ** }.
1e12f 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 */. u.az.r
1e130 2e 66 6c 61 67 73 20 3d 20 28 75 31 36 29 28 55 .flags = (u16)(U
1e131 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 NPACKED_INCRKEY
1e132 2a 20 28 31 20 26 20 28 75 2e 61 7a 2e 6f 63 20 * (1 & (u.az.oc
1e133 2d 20 4f 50 5f 53 65 65 6b 4c 74 29 29 29 3b 0a - OP_SeekLt)));.
1e134 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1e135 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 47 74 az.oc!=OP_SeekGt
1e136 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 || u.az.r.flags
1e137 3d 3d 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b ==UNPACKED_INCRK
1e138 45 59 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 EY );. asse
1e139 72 74 28 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f rt( u.az.oc!=OP_
1e13a 53 65 65 6b 4c 65 20 7c 7c 20 75 2e 61 7a 2e 72 SeekLe || u.az.r
1e13b 2e 66 6c 61 67 73 3d 3d 55 4e 50 41 43 4b 45 44 .flags==UNPACKED
1e13c 5f 49 4e 43 52 4b 45 59 20 29 3b 0a 20 20 20 20 _INCRKEY );.
1e13d 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f assert( u.az.o
1e13e 63 21 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 c!=OP_SeekGe ||
1e13f 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d 3d 30 20 u.az.r.flags==0
1e140 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1e141 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 u.az.oc!=OP_See
1e142 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c kLt || u.az.r.fl
1e143 61 67 73 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20 ags==0 );..
1e144 20 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 26 u.az.r.aMem = &
1e145 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1e146 20 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 ExpandBlob(
1e147 75 2e 61 7a 2e 72 2e 61 4d 65 6d 29 3b 0a 20 20 u.az.r.aMem);.
1e148 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e149 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1e14a 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 ked(u.az.pC->pCu
1e14b 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 2c 20 30 rsor, &u.az.r, 0
1e14c 2c 20 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b , 0, &u.az.res);
1e14d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1e14e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1e14f 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 goto abort_d
1e150 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1e151 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e }. u.az.
1e152 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e153 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
1e154 75 2e 61 7a 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.az.pC->deferre
1e155 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 dMoveto = 0;.
1e156 20 75 2e 61 7a 2e 70 43 2d 3e 63 61 63 68 65 53 u.az.pC->cacheS
1e157 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1e158 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 ALE;.#ifdef SQLI
1e159 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 TE_TEST. sqli
1e15a 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 te3_search_count
1e15b 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 ++;.#endif. i
1e15c 66 28 20 75 2e 61 7a 2e 6f 63 3e 3d 4f 50 5f 53 f( u.az.oc>=OP_S
1e15d 65 65 6b 47 65 20 29 7b 20 20 61 73 73 65 72 74 eekGe ){ assert
1e15e 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 ( u.az.oc==OP_Se
1e15f 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d ekGe || u.az.oc=
1e160 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 =OP_SeekGt );.
1e161 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 if( u.az.res
1e162 3c 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d <0 || (u.az.res=
1e163 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f =0 && u.az.oc==O
1e164 50 5f 53 65 65 6b 47 74 29 20 29 7b 0a 20 20 20 P_SeekGt) ){.
1e165 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e166 33 42 74 72 65 65 4e 65 78 74 28 75 2e 61 7a 2e 3BtreeNext(u.az.
1e167 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e pC->pCursor, &u.
1e168 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 az.res);.
1e169 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1e16a 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f OK ) goto abort_
1e16b 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1e16c 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 u.az.pC->r
1e16d 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1e16e 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1e16f 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d u.az.res =
1e170 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1e171 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1e172 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 ert( u.az.oc==OP
1e173 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e _SeekLt || u.az.
1e174 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b oc==OP_SeekLe );
1e175 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e . if( u.az.
1e176 72 65 73 3e 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 res>0 || (u.az.r
1e177 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 es==0 && u.az.oc
1e178 3d 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a ==OP_SeekLt) ){.
1e179 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1e17a 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 ite3BtreePreviou
1e17b 73 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 s(u.az.pC->pCurs
1e17c 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a or, &u.az.res);.
1e17d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1e17e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
1e17f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1e180 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 ror;. u.a
1e181 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c z.pC->rowidIsVal
1e182 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 id = 0;. }e
1e183 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
1e184 75 2e 61 7a 2e 72 65 73 20 6d 69 67 68 74 20 62 u.az.res might b
1e185 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 e negative becau
1e186 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 se the table is
1e187 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f empty. Check to
1e188 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 . ** see
1e189 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 if this is the c
1e18a 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ase.. */.
1e18b 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 u.az.res
1e18c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 = sqlite3BtreeE
1e18d 6f 66 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 of(u.az.pC->pCur
1e18e 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 sor);. }.
1e18f 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1e190 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 pOp->p2>0 );.
1e191 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b if( u.az.res ){
1e192 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1e193 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 >p2 - 1;. }.
1e194 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
1e195 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e his happens when
1e196 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f attempting to o
1e197 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f pen the sqlite3_
1e198 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20 master table.
1e199 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63 63 ** for read acc
1e19a 65 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ess returns SQLI
1e19b 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68 69 TE_EMPTY. In thi
1e19c 73 20 63 61 73 65 20 61 6c 77 61 79 73 0a 20 20 s case always.
1e19d 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 ** take the ju
1e19e 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 mp (since there
1e19f 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 69 are no records i
1e1a0 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0a 20 20 n the table)..
1e1a1 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f */. pc = pO
1e1a2 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 p->p2 - 1;. }.
1e1a3 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1e1a4 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 20 50 32 code: Seek P1 P2
1e1a5 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 * * *.**.** P1
1e1a6 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 62 6c 65 is an open table
1e1a7 20 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 cursor and P2 i
1e1a8 73 20 61 20 72 6f 77 69 64 20 69 6e 74 65 67 65 s a rowid intege
1e1a9 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a 2a 20 66 r. Arrange.** f
1e1aa 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 20 73 6f or P1 to move so
1e1ab 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1e1ac 74 6f 20 74 68 65 20 72 6f 77 69 64 20 67 69 76 to the rowid giv
1e1ad 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 en by P2..**.**
1e1ae 54 68 69 73 20 69 73 20 61 63 74 75 61 6c 6c 79 This is actually
1e1af 20 61 20 64 65 66 65 72 72 65 64 20 73 65 65 6b a deferred seek
1e1b0 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 74 75 61 . Nothing actua
1e1b1 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 6e 74 69 lly happens unti
1e1b2 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 l.** the cursor
1e1b3 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 is used to read
1e1b4 61 20 72 65 63 6f 72 64 2e 20 20 54 68 61 74 20 a record. That
1e1b5 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 61 64 73 way, if no reads
1e1b6 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e .** occur, no un
1e1b7 6e 65 63 65 73 73 61 72 79 20 49 2f 4f 20 68 61 necessary I/O ha
1e1b8 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f ppens..*/.case O
1e1b9 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20 P_Seek: { /*
1e1ba 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a in2 */.#if 0 /*
1e1bb 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e1bc 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 moved into u.ba
1e1bd 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e1be 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 *pC;.#endif /*
1e1bf 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e1c0 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 20 moved into u.ba
1e1c1 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f */.. assert( pO
1e1c2 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1e1c3 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1e1c4 3b 0a 20 20 75 2e 62 61 2e 70 43 20 3d 20 70 2d ;. u.ba.pC = p-
1e1c5 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e1c6 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e . assert( u.ba.
1e1c7 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 pC!=0 );. if( A
1e1c8 4c 57 41 59 53 28 75 2e 62 61 2e 70 43 2d 3e 70 LWAYS(u.ba.pC->p
1e1c9 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 Cursor!=0) ){.
1e1ca 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e 70 assert( u.ba.p
1e1cb 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1e1cc 20 20 75 2e 62 61 2e 70 43 2d 3e 6e 75 6c 6c 52 u.ba.pC->nullR
1e1cd 6f 77 20 3d 20 30 3b 0a 20 20 20 20 70 49 6e 32 ow = 0;. pIn2
1e1ce 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 = &aMem[pOp->p2
1e1cf 5d 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e ];. u.ba.pC->
1e1d0 6d 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 73 movetoTarget = s
1e1d1 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1e1d2 75 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e ue(pIn2);. u.
1e1d3 62 61 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 ba.pC->rowidIsVa
1e1d4 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 lid = 0;. u.b
1e1d5 61 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f a.pC->deferredMo
1e1d6 76 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 veto = 1;. }.
1e1d7 62 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 break;.}. ../*
1e1d8 4f 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 Opcode: Found P1
1e1d9 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1e1da 2a 20 49 66 20 50 34 3d 3d 30 20 74 68 65 6e 20 * If P4==0 then
1e1db 72 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 register P3 hold
1e1dc 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 s a blob constru
1e1dd 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f cted by MakeReco
1e1de 72 64 2e 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20 rd. If.** P4>0
1e1df 74 68 65 6e 20 72 65 67 69 73 74 65 72 20 50 33 then register P3
1e1e0 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 66 is the first of
1e1e1 20 50 34 20 72 65 67 69 73 74 65 72 73 20 74 68 P4 registers th
1e1e2 61 74 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 at form an unpac
1e1e3 6b 65 64 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a ked.** record..*
1e1e4 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20 69 *.** Cursor P1 i
1e1e5 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 74 s on an index bt
1e1e6 72 65 65 2e 20 20 49 66 20 74 68 65 20 72 65 63 ree. If the rec
1e1e7 6f 72 64 20 69 64 65 6e 74 69 66 69 65 64 20 62 ord identified b
1e1e8 79 20 50 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69 y P3 and P4.** i
1e1e9 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 61 6e s a prefix of an
1e1ea 79 20 65 6e 74 72 79 20 69 6e 20 50 31 20 74 68 y entry in P1 th
1e1eb 65 6e 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 en a jump is mad
1e1ec 65 20 74 6f 20 50 32 20 61 6e 64 0a 2a 2a 20 50 e to P2 and.** P
1e1ed 31 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 1 is left pointi
1e1ee 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69 ng at the matchi
1e1ef 6e 67 20 65 6e 74 72 79 2e 0a 2a 2f 0a 2f 2a 20 ng entry..*/./*
1e1f0 4f 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 Opcode: NotFound
1e1f1 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1e1f2 2a 0a 2a 2a 20 49 66 20 50 34 3d 3d 30 20 74 68 *.** If P4==0 th
1e1f3 65 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 68 en register P3 h
1e1f4 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 olds a blob cons
1e1f5 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 tructed by MakeR
1e1f6 65 63 6f 72 64 2e 20 20 49 66 0a 2a 2a 20 50 34 ecord. If.** P4
1e1f7 3e 30 20 74 68 65 6e 20 72 65 67 69 73 74 65 72 >0 then register
1e1f8 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 P3 is the first
1e1f9 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1e1fa 20 74 68 61 74 20 66 6f 72 6d 20 61 6e 20 75 6e that form an un
1e1fb 70 61 63 6b 65 64 0a 2a 2a 20 72 65 63 6f 72 64 packed.** record
1e1fc 2e 0a 2a 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20 ..** .** Cursor
1e1fd 50 31 20 69 73 20 6f 6e 20 61 6e 20 69 6e 64 65 P1 is on an inde
1e1fe 78 20 62 74 72 65 65 2e 20 20 49 66 20 74 68 65 x btree. If the
1e1ff 20 72 65 63 6f 72 64 20 69 64 65 6e 74 69 66 69 record identifi
1e200 65 64 20 62 79 20 50 33 20 61 6e 64 20 50 34 0a ed by P3 and P4.
1e201 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 70 72 ** is not the pr
1e202 65 66 69 78 20 6f 66 20 61 6e 79 20 65 6e 74 72 efix of any entr
1e203 79 20 69 6e 20 50 31 20 74 68 65 6e 20 61 20 6a y in P1 then a j
1e204 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 ump is made to P
1e205 32 2e 20 20 49 66 20 50 31 20 0a 2a 2a 20 64 6f 2. If P1 .** do
1e206 65 73 20 63 6f 6e 74 61 69 6e 20 61 6e 20 65 6e es contain an en
1e207 74 72 79 20 77 68 6f 73 65 20 70 72 65 66 69 78 try whose prefix
1e208 20 6d 61 74 63 68 65 73 20 74 68 65 20 50 33 2f matches the P3/
1e209 50 34 20 72 65 63 6f 72 64 20 74 68 65 6e 20 63 P4 record then c
1e20a 6f 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 ontrol.** falls
1e20b 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1e20c 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1e20d 61 6e 64 20 50 31 20 69 73 20 6c 65 66 74 20 70 and P1 is left p
1e20e 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 0a 2a ointing at the.*
1e20f 2a 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 * matching entry
1e210 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1e211 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 : Found, NotExis
1e212 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a ts, IsUnique.*/.
1e213 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 case OP_NotFound
1e214 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c : /* jump,
1e215 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
1e216 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 Found: {
1e217 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1e218 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1e219 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e21a 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 20 20 69 into u.bb */. i
1e21b 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 nt alreadyExists
1e21c 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1e21d 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 pC;. int res;.
1e21e 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1e21f 2a 70 49 64 78 4b 65 79 3b 0a 20 20 55 6e 70 61 *pIdxKey;. Unpa
1e220 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 ckedRecord r;.
1e221 63 68 61 72 20 61 54 65 6d 70 52 65 63 5b 52 4f char aTempRec[RO
1e222 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 UND8(sizeof(Unpa
1e223 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73 ckedRecord)) + s
1e224 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 izeof(Mem)*3 + 7
1e225 5d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 ];.#endif /* loc
1e226 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e227 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a ed into u.bb */.
1e228 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1e229 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 66 6f EST. sqlite3_fo
1e22a 75 6e 64 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e und_count++;.#en
1e22b 64 69 66 0a 0a 20 20 75 2e 62 62 2e 61 6c 72 65 dif.. u.bb.alre
1e22c 61 64 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 adyExists = 0;.
1e22d 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1e22e 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1e22f 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1e230 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1e231 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a pe==P4_INT32 );.
1e232 20 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 u.bb.pC = p->a
1e233 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1e234 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 assert( u.bb.pC
1e235 21 3d 30 20 29 3b 0a 20 20 70 49 6e 33 20 3d 20 !=0 );. pIn3 =
1e236 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1e237 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 if( ALWAYS(u.b
1e238 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 b.pC->pCursor!=0
1e239 29 20 29 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 ) ){.. assert
1e23a 28 20 75 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 ( u.bb.pC->isTab
1e23b 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 le==0 );. if(
1e23c 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 29 7b 0a pOp->p4.i>0 ){.
1e23d 20 20 20 20 20 20 75 2e 62 62 2e 72 2e 70 4b 65 u.bb.r.pKe
1e23e 79 49 6e 66 6f 20 3d 20 75 2e 62 62 2e 70 43 2d yInfo = u.bb.pC-
1e23f 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 >pKeyInfo;.
1e240 20 75 2e 62 62 2e 72 2e 6e 46 69 65 6c 64 20 3d u.bb.r.nField =
1e241 20 28 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b (u16)pOp->p4.i;
1e242 0a 20 20 20 20 20 20 75 2e 62 62 2e 72 2e 61 4d . u.bb.r.aM
1e243 65 6d 20 3d 20 70 49 6e 33 3b 0a 20 20 20 20 20 em = pIn3;.
1e244 20 75 2e 62 62 2e 72 2e 66 6c 61 67 73 20 3d 20 u.bb.r.flags =
1e245 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f UNPACKED_PREFIX_
1e246 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 75 2e 62 MATCH;. u.b
1e247 62 2e 70 49 64 78 4b 65 79 20 3d 20 26 75 2e 62 b.pIdxKey = &u.b
1e248 62 2e 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a b.r;. }else{.
1e249 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 assert( pI
1e24a 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n3->flags & MEM_
1e24b 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 45 78 Blob );. Ex
1e24c 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a pandBlob(pIn3);.
1e24d 20 20 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b u.bb.pIdxK
1e24e 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 ey = sqlite3Vdbe
1e24f 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 75 2e 62 RecordUnpack(u.b
1e250 62 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 b.pC->pKeyInfo,
1e251 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e 7a pIn3->n, pIn3->z
1e252 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1e253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e254 20 20 20 20 20 20 20 20 20 20 75 2e 62 62 2e 61 u.bb.a
1e255 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 TempRec, sizeof(
1e256 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 29 29 3b u.bb.aTempRec));
1e257 0a 20 20 20 20 20 20 69 66 28 20 75 2e 62 62 2e . if( u.bb.
1e258 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 pIdxKey==0 ){.
1e259 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 goto no_me
1e25a 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 m;. }.
1e25b 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 2d 3e 66 u.bb.pIdxKey->f
1e25c 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 lags |= UNPACKED
1e25d 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 _PREFIX_MATCH;.
1e25e 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1e25f 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
1e260 55 6e 70 61 63 6b 65 64 28 75 2e 62 62 2e 70 43 Unpacked(u.bb.pC
1e261 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 62 2e ->pCursor, u.bb.
1e262 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 pIdxKey, 0, 0, &
1e263 75 2e 62 62 2e 72 65 73 29 3b 0a 20 20 20 20 69 u.bb.res);. i
1e264 66 28 20 70 4f 70 2d 3e 70 34 2e 69 3d 3d 30 20 f( pOp->p4.i==0
1e265 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1e266 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b VdbeDeleteUnpack
1e267 65 64 52 65 63 6f 72 64 28 75 2e 62 62 2e 70 49 edRecord(u.bb.pI
1e268 64 78 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 dxKey);. }.
1e269 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1e26a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 72 65 _OK ){. bre
1e26b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e ak;. }. u.
1e26c 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 74 73 bb.alreadyExists
1e26d 20 3d 20 28 75 2e 62 62 2e 72 65 73 3d 3d 30 29 = (u.bb.res==0)
1e26e 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e 64 ;. u.bb.pC->d
1e26f 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1e270 30 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e 0;. u.bb.pC->
1e271 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e272 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 CHE_STALE;. }.
1e273 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1e274 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 ==OP_Found ){.
1e275 20 20 69 66 28 20 75 2e 62 62 2e 61 6c 72 65 61 if( u.bb.alrea
1e276 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 dyExists ) pc =
1e277 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1e278 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21 75 else{. if( !u
1e279 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 74 .bb.alreadyExist
1e27a 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 s ) pc = pOp->p2
1e27b 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1e27c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e27d 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 32 20 IsUnique P1 P2
1e27e 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 75 P3 P4 *.**.** Cu
1e27f 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65 6e 20 rsor P1 is open
1e280 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 on an index b-tr
1e281 65 65 20 2d 20 74 68 61 74 20 69 73 20 74 6f 20 ee - that is to
1e282 73 61 79 2c 20 61 20 62 74 72 65 65 20 77 68 69 say, a btree whi
1e283 63 68 0a 2a 2a 20 6e 6f 20 64 61 74 61 20 61 6e ch.** no data an
1e284 64 20 77 68 65 72 65 20 74 68 65 20 6b 65 79 20 d where the key
1e285 61 72 65 20 72 65 63 6f 72 64 73 20 67 65 6e 65 are records gene
1e286 72 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 rated by OP_Make
1e287 52 65 63 6f 72 64 20 77 69 74 68 0a 2a 2a 20 74 Record with.** t
1e288 68 65 20 6c 69 73 74 20 66 69 65 6c 64 20 62 65 he list field be
1e289 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65 72 20 ing the integer
1e28a 52 4f 57 49 44 20 6f 66 20 74 68 65 20 65 6e 74 ROWID of the ent
1e28b 72 79 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 ry that the inde
1e28c 78 0a 2a 2a 20 65 6e 74 72 79 20 72 65 66 65 72 x.** entry refer
1e28d 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 s to..**.** The
1e28e 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 P3 register cont
1e28f 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 ains an integer
1e290 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43 record number. C
1e291 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64 20 all this record
1e292 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52 65 .** number R. Re
1e293 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68 65 gister P4 is the
1e294 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74 20 first in a set
1e295 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20 of N contiguous
1e296 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68 61 registers.** tha
1e297 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e 70 t make up an unp
1e298 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 acked index key
1e299 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 that can be used
1e29a 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31 2e with cursor P1.
1e29b 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 .** The value of
1e29c 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72 72 N can be inferr
1e29d 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 73 ed from the curs
1e29e 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20 74 or. N includes t
1e29f 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75 he rowid.** valu
1e2a0 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 e appended to th
1e2a1 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 e end of the ind
1e2a2 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73 20 ex record. This
1e2a3 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79 0a rowid value may.
1e2a4 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 ** or may not be
1e2a5 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e 0a the same as R..
1e2a6 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 **.** If any of
1e2a7 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20 the N registers
1e2a8 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 beginning with r
1e2a9 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74 61 egister P4 conta
1e2aa 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61 ins a NULL.** va
1e2ab 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 lue, jump immedi
1e2ac 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a ately to P2..**.
1e2ad 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 ** Otherwise, th
1e2ae 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 is instruction c
1e2af 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72 20 hecks if cursor
1e2b0 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 P1 contains an e
1e2b1 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74 68 ntry.** where th
1e2b2 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66 69 e first (N-1) fi
1e2b3 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20 74 elds match but t
1e2b4 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 61 he rowid value a
1e2b5 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 t the end.** of
1e2b6 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 the index entry
1e2b7 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68 65 is not R. If the
1e2b8 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65 6e re is no such en
1e2b9 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d try, control jum
1e2ba 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63 ps.** to instruc
1e2bb 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 tion P2. Otherwi
1e2bc 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f 66 se, the rowid of
1e2bd 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 the conflicting
1e2be 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 index.** entry
1e2bf 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65 67 is copied to reg
1e2c0 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f 6e ister P3 and con
1e2c1 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75 trol falls throu
1e2c2 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a gh to the next.*
1e2c3 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a * instruction..*
1e2c4 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e *.** See also: N
1e2c5 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 otFound, NotExis
1e2c6 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 ts, Found.*/.cas
1e2c7 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b e OP_IsUnique: {
1e2c8 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1e2c9 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f in3 */.#if 0 /
1e2ca 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e2cb 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e2cc 63 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20 c */. u16 ii;.
1e2cd 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1e2ce 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1e2cf 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c rsr;. u16 nFiel
1e2d0 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 d;. Mem *aMx;.
1e2d1 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1e2d2 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1e2d3 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e /* B-Tree in
1e2d4 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a dex search key *
1e2d5 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20 20 20 /. i64 R;
1e2d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e2d7 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 /* Rowid
1e2d8 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1e2d9 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69 66 20 er P3 */.#endif
1e2da 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e2db 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e2dc 62 63 20 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20 bc */.. pIn3 =
1e2dd 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1e2de 20 20 75 2e 62 63 2e 61 4d 78 20 3d 20 26 61 4d u.bc.aMx = &aM
1e2df 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 em[pOp->p4.i];.
1e2e0 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 /* Assert that
1e2e1 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 70 61 the values of pa
1e2e2 72 61 6d 65 74 65 72 73 20 50 31 20 61 6e 64 20 rameters P1 and
1e2e3 50 34 20 61 72 65 20 69 6e 20 72 61 6e 67 65 2e P4 are in range.
1e2e4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1e2e5 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e p->p4type==P4_IN
1e2e6 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 T32 );. assert(
1e2e7 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 pOp->p4.i>0 &&
1e2e8 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d pOp->p4.i<=p->nM
1e2e9 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 em );. assert(
1e2ea 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1e2eb 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 p->p1<p->nCursor
1e2ec 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 );.. /* Find t
1e2ed 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 2e he index cursor.
1e2ee 20 2a 2f 0a 20 20 75 2e 62 63 2e 70 43 78 20 3d */. u.bc.pCx =
1e2ef 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e2f0 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1e2f1 62 63 2e 70 43 78 2d 3e 64 65 66 65 72 72 65 64 bc.pCx->deferred
1e2f2 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 Moveto==0 );. u
1e2f3 2e 62 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 .bc.pCx->seekRes
1e2f4 75 6c 74 20 3d 20 30 3b 0a 20 20 75 2e 62 63 2e ult = 0;. u.bc.
1e2f5 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75 73 pCx->cacheStatus
1e2f6 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1e2f7 20 20 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 75 u.bc.pCrsr = u
1e2f8 2e 62 63 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 .bc.pCx->pCursor
1e2f9 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f ;.. /* If any o
1e2fa 66 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 f the values are
1e2fb 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20 NULL, take the
1e2fc 6a 75 6d 70 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e jump. */. u.bc.
1e2fd 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e 70 43 nField = u.bc.pC
1e2fe 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 x->pKeyInfo->nFi
1e2ff 65 6c 64 3b 0a 20 20 66 6f 72 28 75 2e 62 63 2e eld;. for(u.bc.
1e300 69 69 3d 30 3b 20 75 2e 62 63 2e 69 69 3c 75 2e ii=0; u.bc.ii<u.
1e301 62 63 2e 6e 46 69 65 6c 64 3b 20 75 2e 62 63 2e bc.nField; u.bc.
1e302 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 ii++){. if( u
1e303 2e 62 63 2e 61 4d 78 5b 75 2e 62 63 2e 69 69 5d .bc.aMx[u.bc.ii]
1e304 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c .flags & MEM_Nul
1e305 6c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 l ){. pc =
1e306 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1e307 20 20 20 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 u.bc.pCrsr =
1e308 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0;. break;.
1e309 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
1e30a 72 74 28 20 28 75 2e 62 63 2e 61 4d 78 5b 75 2e rt( (u.bc.aMx[u.
1e30b 62 63 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 bc.nField].flags
1e30c 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 & MEM_Null)==0
1e30d 29 3b 0a 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 );.. if( u.bc.p
1e30e 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f Crsr!=0 ){. /
1e30f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 * Populate the i
1e310 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e ndex search key.
1e311 20 2a 2f 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 */. u.bc.r.p
1e312 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 KeyInfo = u.bc.p
1e313 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 Cx->pKeyInfo;.
1e314 20 20 75 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 u.bc.r.nField
1e315 3d 20 75 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 = u.bc.nField +
1e316 31 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 1;. u.bc.r.fl
1e317 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 ags = UNPACKED_P
1e318 52 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 REFIX_SEARCH;.
1e319 20 20 75 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 u.bc.r.aMem =
1e31a 75 2e 62 63 2e 61 4d 78 3b 0a 0a 20 20 20 20 2f u.bc.aMx;.. /
1e31b 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 * Extract the va
1e31c 6c 75 65 20 6f 66 20 75 2e 62 63 2e 52 20 66 72 lue of u.bc.R fr
1e31d 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 om register P3.
1e31e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1e31f 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1e320 70 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e pIn3);. u.bc.
1e321 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a R = pIn3->u.i;..
1e322 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 /* Search th
1e323 65 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e 20 e B-Tree index.
1e324 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e If no conflictin
1e325 67 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75 6e g record is foun
1e326 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74 d, jump. ** t
1e327 6f 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c o P2. Otherwise,
1e328 20 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64 20 copy the rowid
1e329 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 of the conflicti
1e32a 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20 20 ng record to.
1e32b 20 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 20 ** register P3
1e32c 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 and fall through
1e32d 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 to the next ins
1e32e 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 truction. */.
1e32f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e330 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1e331 64 28 75 2e 62 63 2e 70 43 72 73 72 2c 20 26 75 d(u.bc.pCrsr, &u
1e332 2e 62 63 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e .bc.r, 0, 0, &u.
1e333 62 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 bc.pCx->seekResu
1e334 6c 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e lt);. if( (u.
1e335 62 63 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 bc.r.flags & UNP
1e336 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1e337 52 43 48 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72 RCH) || u.bc.r.r
1e338 6f 77 69 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a owid==u.bc.R ){.
1e339 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1e33a 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 p2 - 1;. }els
1e33b 65 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 e{. pIn3->u
1e33c 2e 69 20 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69 .i = u.bc.r.rowi
1e33d 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 d;. }. }. b
1e33e 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1e33f 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 de: NotExists P1
1e340 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1e341 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 Use the content
1e342 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1e343 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 as a integer key
1e344 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a . If a record .
1e345 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 ** with that key
1e346 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
1e347 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 in table of P1,
1e348 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1e349 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f .** If the reco
1e34a 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 rd does exist, t
1e34b 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 hen fall thru.
1e34c 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
1e34d 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 ft .** pointing
1e34e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 to the record if
1e34f 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a it exists..**.*
1e350 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
1e351 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 between this op
1e352 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 eration and NotF
1e353 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 ound is that thi
1e354 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 s.** operation a
1e355 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 ssumes the key i
1e356 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 s an integer and
1e357 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 that P1 is a ta
1e358 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e ble whereas.** N
1e359 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 otFound assumes
1e35a 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f key is a blob co
1e35b 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d nstructed from M
1e35c 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a akeRecord and.**
1e35d 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e P1 is an index.
1e35e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1e35f 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 Found, NotFound
1e360 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 , IsUnique.*/.ca
1e361 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a se OP_NotExists:
1e362 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1e363 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 p, in3 */.#if 0
1e364 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e365 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e366 2e 62 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .bd */. VdbeCur
1e367 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1e368 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1e369 74 20 72 65 73 3b 0a 20 20 75 36 34 20 69 4b 65 t res;. u64 iKe
1e36a 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 y;.#endif /* loc
1e36b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e36c 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a ed into u.bd */.
1e36d 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b . pIn3 = &aMem[
1e36e 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 pOp->p3];. asse
1e36f 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 rt( pIn3->flags
1e370 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 & MEM_Int );. a
1e371 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e372 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e373 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1e374 64 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b d.pC = p->apCsr[
1e375 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1e376 72 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 rt( u.bd.pC!=0 )
1e377 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 ;. assert( u.bd
1e378 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a .pC->isTable );.
1e379 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 assert( u.bd.p
1e37a 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 C->pseudoTableRe
1e37b 67 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 g==0 );. u.bd.p
1e37c 43 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e Crsr = u.bd.pC->
1e37d 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 pCursor;. if( u
1e37e 2e 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a .bd.pCrsr!=0 ){.
1e37f 20 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 u.bd.res = 0
1e380 3b 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 ;. u.bd.iKey
1e381 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn3->u.i;.
1e382 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e383 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1e384 28 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 (u.bd.pCrsr, 0,
1e385 75 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 u.bd.iKey, 0, &u
1e386 2e 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e .bd.res);. u.
1e387 62 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 bd.pC->lastRowid
1e388 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 = pIn3->u.i;.
1e389 20 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 u.bd.pC->rowid
1e38a 49 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 IsValid = u.bd.r
1e38b 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 es==0 ?1:0;.
1e38c 75 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 u.bd.pC->nullRow
1e38d 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 = 0;. u.bd.p
1e38e 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1e38f 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1e390 20 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 u.bd.pC->defer
1e391 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1e392 20 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 21 if( u.bd.res!
1e393 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d =0 ){. pc =
1e394 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1e395 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 assert( u.bd
1e396 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1e397 64 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 d==0 );. }.
1e398 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 u.bd.pC->seekR
1e399 65 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 esult = u.bd.res
1e39a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
1e39b 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 * This happens w
1e39c 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 hen an attempt t
1e39d 6f 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75 o open a read cu
1e39e 72 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 rsor on the.
1e39f 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 ** sqlite_master
1e3a0 20 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 table returns S
1e3a1 51 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 QLITE_EMPTY..
1e3a2 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 */. pc = pOp
1e3a3 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 ->p2 - 1;. as
1e3a4 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 sert( u.bd.pC->r
1e3a5 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 owidIsValid==0 )
1e3a6 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 ;. u.bd.pC->s
1e3a7 65 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 eekResult = 0;.
1e3a8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e3a9 2a 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e * Opcode: Sequen
1e3aa 63 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ce P1 P2 * * *.*
1e3ab 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 *.** Find the ne
1e3ac 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71 xt available seq
1e3ad 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 uence number for
1e3ae 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 cursor P1..** W
1e3af 72 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63 rite the sequenc
1e3b0 65 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 e number into re
1e3b1 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 gister P2..** Th
1e3b2 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 e sequence numbe
1e3b3 72 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 r on the cursor
1e3b4 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 is incremented a
1e3b5 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 fter this.** ins
1e3b6 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 truction. .*/.c
1e3b7 61 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a ase OP_Sequence:
1e3b8 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1e3b9 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1e3ba 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1e3bb 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e3bc 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e3bd 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 . assert( p->ap
1e3be 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 Csr[pOp->p1]!=0
1e3bf 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d );. pOut->u.i =
1e3c0 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e3c1 31 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 1]->seqCount++;.
1e3c2 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 break;.}.../*
1e3c3 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 Opcode: NewRowid
1e3c4 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1e3c5 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 6e .** Get a new in
1e3c6 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d teger record num
1e3c7 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 ber (a.k.a "rowi
1e3c8 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 20 d") used as the
1e3c9 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a key to a table..
1e3ca 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 ** The record nu
1e3cb 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76 mber is not prev
1e3cc 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 61 iously used as a
1e3cd 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 61 key in the data
1e3ce 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68 base.** table th
1e3cf 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 at cursor P1 poi
1e3d0 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 nts to. The new
1e3d1 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 record number i
1e3d2 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69 s written.** wri
1e3d3 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72 tten to register
1e3d4 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 P2..**.** If P3
1e3d5 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 20 >0 then P3 is a
1e3d6 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20 register in the
1e3d7 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68 root frame of th
1e3d8 69 73 20 56 44 42 45 20 74 68 61 74 20 68 6f 6c is VDBE that hol
1e3d9 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 ds .** the large
1e3da 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 67 65 st previously ge
1e3db 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e nerated record n
1e3dc 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65 umber. No new re
1e3dd 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 72 65 cord numbers are
1e3de 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 .** allowed to b
1e3df 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73 e less than this
1e3e0 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 68 69 value. When thi
1e3e1 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 73 20 s value reaches
1e3e2 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a its maximum, .**
1e3e3 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 a SQLITE_FULL e
1e3e4 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65 rror is generate
1e3e5 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 73 74 d. The P3 regist
1e3e6 65 72 20 69 73 20 75 70 64 61 74 65 64 20 77 69 er is updated wi
1e3e7 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65 th the '.** gene
1e3e8 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d rated record num
1e3e9 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63 ber. This P3 mec
1e3ea 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74 hanism is used t
1e3eb 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 o help implement
1e3ec 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52 the.** AUTOINCR
1e3ed 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a EMENT feature..*
1e3ee 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77 /.case OP_NewRow
1e3ef 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 id: {
1e3f0 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1e3f1 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 se */.#if 0 /*
1e3f2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e3f3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 moved into u.be
1e3f4 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 */. i64 v;
1e3f5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1e3f6 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a he new rowid */.
1e3f7 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e3f8 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 ; /* Curs
1e3f9 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 or of table to g
1e3fa 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 et the new rowid
1e3fb 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 */. int res;
1e3fc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e3fd 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c Result of an sql
1e3fe 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 ite3BtreeLast()
1e3ff 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 */. int cnt;
1e400 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1e401 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 ounter to limit
1e402 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
1e403 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 arches */. Mem
1e404 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *pMem;
1e405 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
1e406 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 olding largest r
1e407 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 owid for AUTOINC
1e408 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65 REMENT */. Vdbe
1e409 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 Frame *pFrame;
1e40a 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 /* Root frame
1e40b 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64 of VDBE */.#end
1e40c 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e40d 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e40e 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65 u.be */.. u.be
1e40f 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72 .v = 0;. u.be.r
1e410 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 es = 0;. assert
1e411 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e412 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e413 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20 or );. u.be.pC
1e414 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e415 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e416 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 .be.pC!=0 );. i
1e417 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e 70 43 f( NEVER(u.be.pC
1e418 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b ->pCursor==0) ){
1e419 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f . /* The zero
1e41a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
1e41b 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 above is all tha
1e41c 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 t is needed */.
1e41d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
1e41e 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 he next rowid or
1e41f 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 record number (
1e420 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 different terms
1e421 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 for the same.
1e422 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 ** thing) is ob
1e423 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d tained in a two-
1e424 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a step algorithm..
1e425 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 **. ** Fi
1e426 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 rst we attempt t
1e427 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 o find the large
1e428 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 st existing rowi
1e429 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 d and add one.
1e42a 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 ** to that. B
1e42b 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 ut if the larges
1e42c 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
1e42d 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 is already the
1e42e 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 maximum. ** p
1e42f 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c ositive integer,
1e430 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c we have to fall
1e431 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1e432 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 second. ** pr
1e433 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f obabilistic algo
1e434 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 rithm. **.
1e435 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 ** The second a
1e436 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 lgorithm is to s
1e437 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 elect a rowid at
1e438 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 random and see
1e439 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 if. ** it alr
1e43a 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 eady exists in t
1e43b 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 he table. If it
1e43c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c does not exist,
1e43d 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 we have. **
1e43e 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 succeeded. If t
1e43f 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 he random rowid
1e440 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 does exist, we s
1e441 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a elect a new one.
1e442 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 ** and try a
1e443 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20 gain, up to 100
1e444 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 times.. */.
1e445 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 2e 70 assert( u.be.p
1e446 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1e447 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a u.be.cnt = 0;.
1e448 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33 .#ifdef SQLITE_3
1e449 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 2BIT_ROWID.# d
1e44a 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1e44b 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0x7fffffff.#else
1e44c 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d . /* Some com
1e44d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 pilers complain
1e44e 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 about constants
1e44f 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66 of the form 0x7f
1e450 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a ffffffffffffff..
1e451 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f ** Others co
1e452 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 mplain about 0x7
1e453 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 ffffffffffffffff
1e454 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 LL. The followi
1e455 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 ng macro seems.
1e456 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 ** to provide
1e457 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 the constant wh
1e458 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 ile making all c
1e459 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a ompilers happy..
1e45a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e */.# defin
1e45b 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36 e MAX_ROWID (i6
1e45c 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66 4)( (((u64)0x7ff
1e45d 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 fffff)<<32) | (u
1e45e 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a 64)0xffffffff ).
1e45f 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
1e460 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e !u.be.pC->useRan
1e461 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1e462 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74 u.be.v = sqlit
1e463 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 e3BtreeGetCached
1e464 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 Rowid(u.be.pC->p
1e465 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 Cursor);. i
1e466 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a f( u.be.v==0 ){.
1e467 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1e468 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e ite3BtreeLast(u.
1e469 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 be.pC->pCursor,
1e46a 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 &u.be.res);.
1e46b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1e46c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1e46d 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1e46e 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1e46f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1e470 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20 ( u.be.res ){.
1e471 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d u.be.v =
1e472 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 1;. }els
1e473 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
1e474 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1e475 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1e476 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 .be.pC->pCursor)
1e477 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 );. rc
1e478 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1e479 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e eySize(u.be.pC->
1e47a 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76 pCursor, &u.be.v
1e47b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
1e47c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1e47d 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f OK ); /* Canno
1e47e 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67 t fail following
1e47f 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a BtreeLast() */.
1e480 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e if( u.
1e481 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 be.v==MAX_ROWID
1e482 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
1e483 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f .be.pC->useRando
1e484 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 mRowid = 1;.
1e485 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1e486 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b u.be.v+
1e487 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 +;. }.
1e488 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1e489 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1e48a 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1e48b 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f ENT. if( pO
1e48c 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20 p->p3 ){.
1e48d 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 /* Assert that
1e48e 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 P3 is a valid me
1e48f 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 mory cell. */.
1e490 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1e491 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 p->p3>0 );.
1e492 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 if( p->pFrame
1e493 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
1e494 72 28 75 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d r(u.be.pFrame=p-
1e495 3e 70 46 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46 >pFrame; u.be.pF
1e496 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 rame->pParent; u
1e497 2e 62 65 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e .be.pFrame=u.be.
1e498 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 pFrame->pParent)
1e499 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 ;. /* A
1e49a 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 73 ssert that P3 is
1e49b 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 a valid memory
1e49c 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 cell. */.
1e49d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e49e 70 33 3c 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d p3<=u.be.pFrame-
1e49f 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 >nMem );.
1e4a0 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 u.be.pMem = &
1e4a1 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 u.be.pFrame->aMe
1e4a2 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1e4a3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e4a4 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 /* Assert t
1e4a5 68 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 hat P3 is a vali
1e4a6 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a d memory cell. *
1e4a7 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 /. asse
1e4a8 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e rt( pOp->p3<=p->
1e4a9 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 nMem );.
1e4aa 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 61 u.be.pMem = &a
1e4ab 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1e4ac 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
1e4ad 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1e4ae 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 65 2e 70 4d pOp->p3, u.be.pM
1e4af 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c em);. sql
1e4b0 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
1e4b1 65 72 69 66 79 28 75 2e 62 65 2e 70 4d 65 6d 29 erify(u.be.pMem)
1e4b2 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
1e4b3 28 20 28 75 2e 62 65 2e 70 4d 65 6d 2d 3e 66 6c ( (u.be.pMem->fl
1e4b4 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d ags & MEM_Int)!=
1e4b5 30 20 29 3b 20 20 2f 2a 20 6d 65 6d 28 50 33 29 0 ); /* mem(P3)
1e4b6 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 holds an intege
1e4b7 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 r */. if(
1e4b8 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 3d u.be.pMem->u.i=
1e4b9 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c 20 75 2e =MAX_ROWID || u.
1e4ba 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d be.pC->useRandom
1e4bb 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
1e4bc 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 rc = SQLITE_F
1e4bd 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 ULL;. g
1e4be 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e4bf 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 _error;.
1e4c0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e }. if( u.
1e4c1 62 65 2e 76 3c 75 2e 62 65 2e 70 4d 65 6d 2d 3e be.v<u.be.pMem->
1e4c2 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20 20 20 u.i+1 ){.
1e4c3 20 20 20 75 2e 62 65 2e 76 20 3d 20 75 2e 62 65 u.be.v = u.be
1e4c4 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a .pMem->u.i + 1;.
1e4c5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e4c6 20 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 u.be.pMem->u.i
1e4c7 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 20 20 20 = u.be.v;.
1e4c8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 }.#endif..
1e4c9 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
1e4ca 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 65 CachedRowid(u.be
1e4cb 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e .pC->pCursor, u.
1e4cc 62 65 2e 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f be.v<MAX_ROWID ?
1e4cd 20 75 2e 62 65 2e 76 2b 31 20 3a 20 30 29 3b 0a u.be.v+1 : 0);.
1e4ce 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e }. if( u.
1e4cf 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d be.pC->useRandom
1e4d0 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 61 Rowid ){. a
1e4d1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d ssert( pOp->p3==
1e4d2 30 20 29 3b 20 20 2f 2a 20 57 65 20 63 61 6e 6e 0 ); /* We cann
1e4d3 6f 74 20 62 65 20 69 6e 20 72 61 6e 64 6f 6d 20 ot be in random
1e4d4 72 6f 77 69 64 20 6d 6f 64 65 20 69 66 20 74 68 rowid mode if th
1e4d5 69 73 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 is is.
1e4d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e4d7 20 20 20 2a 2a 20 61 6e 20 41 55 54 4f 49 4e 43 ** an AUTOINC
1e4d8 52 45 4d 45 4e 54 20 74 61 62 6c 65 2e 20 2a 2f REMENT table. */
1e4d9 0a 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 . u.be.v =
1e4da 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 db->lastRowid;.
1e4db 20 20 20 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 u.be.cnt =
1e4dc 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 0;. do{.
1e4dd 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 63 6e if( u.be.cn
1e4de 74 3d 3d 30 20 26 26 20 28 75 2e 62 65 2e 76 26 t==0 && (u.be.v&
1e4df 30 78 66 66 66 66 66 66 29 3d 3d 75 2e 62 65 2e 0xffffff)==u.be.
1e4e0 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 v ){. u
1e4e1 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 20 .be.v++;.
1e4e2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1e4e3 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
1e4e4 6e 65 73 73 28 73 69 7a 65 6f 66 28 75 2e 62 65 ness(sizeof(u.be
1e4e5 2e 76 29 2c 20 26 75 2e 62 65 2e 76 29 3b 0a 20 .v), &u.be.v);.
1e4e6 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 if( u.b
1e4e7 65 2e 63 6e 74 3c 35 20 29 20 75 2e 62 65 2e 76 e.cnt<5 ) u.be.v
1e4e8 20 26 3d 20 30 78 66 66 66 66 66 66 3b 0a 20 20 &= 0xffffff;.
1e4e9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1e4ea 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e4eb 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1e4ec 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.be.pC->pCursor
1e4ed 2c 20 30 2c 20 28 75 36 34 29 75 2e 62 65 2e 76 , 0, (u64)u.be.v
1e4ee 2c 20 30 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b , 0, &u.be.res);
1e4ef 0a 20 20 20 20 20 20 20 20 75 2e 62 65 2e 63 6e . u.be.cn
1e4f0 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c t++;. }whil
1e4f1 65 28 20 75 2e 62 65 2e 63 6e 74 3c 31 30 30 20 e( u.be.cnt<100
1e4f2 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
1e4f3 20 26 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 && u.be.res==0
1e4f4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
1e4f5 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e =SQLITE_OK && u.
1e4f6 62 65 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 be.res==0 ){.
1e4f7 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1e4f8 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 _FULL;. g
1e4f9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e4fa 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a _error;. }.
1e4fb 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 65 2e 70 }. u.be.p
1e4fc 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1e4fd 3d 20 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 = 0;. u.be.pC
1e4fe 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1e4ff 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70 = 0;. u.be.p
1e500 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1e501 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1e502 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 }. pOut->u.i =
1e503 75 2e 62 65 2e 76 3b 0a 20 20 62 72 65 61 6b 3b u.be.v;. break;
1e504 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1e505 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20 50 nsert P1 P2 P3 P
1e506 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 4 P5.**.** Write
1e507 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 an entry into t
1e508 68 65 20 74 61 62 6c 65 20 6f 66 20 63 75 72 73 he table of curs
1e509 6f 72 20 50 31 2e 20 20 41 20 6e 65 77 20 65 6e or P1. A new en
1e50a 74 72 79 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 try is.** create
1e50b 64 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 d if it doesn't
1e50c 61 6c 72 65 61 64 79 20 65 78 69 73 74 20 6f 72 already exist or
1e50d 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 61 6e the data for an
1e50e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 65 6e 74 existing.** ent
1e50f 72 79 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 ry is overwritte
1e510 6e 2e 20 20 54 68 65 20 64 61 74 61 20 69 73 20 n. The data is
1e511 74 68 65 20 76 61 6c 75 65 20 4d 45 4d 5f 42 6c the value MEM_Bl
1e512 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 ob stored in reg
1e513 69 73 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 ister.** number
1e514 50 32 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 P2. The key is s
1e515 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1e516 72 20 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 r P3. The key mu
1e517 73 74 0a 2a 2a 20 62 65 20 61 20 4d 45 4d 5f 49 st.** be a MEM_I
1e518 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 nt..**.** If the
1e519 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 OPFLAG_NCHANGE
1e51a 66 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 flag of P5 is se
1e51b 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 t, then the row
1e51c 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a change count is.
1e51d 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 ** incremented (
1e51e 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 otherwise not).
1e51f 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c If the OPFLAG_L
1e520 41 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 ASTROWID flag of
1e521 20 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 P5 is set,.** t
1e522 68 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f hen rowid is sto
1e523 72 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 red for subseque
1e524 6e 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 nt return by the
1e525 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 .** sqlite3_last
1e526 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 _insert_rowid()
1e527 66 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 function (otherw
1e528 69 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 ise it is unmodi
1e529 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 fied)..**.** If
1e52a 74 68 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 the OPFLAG_USESE
1e52b 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 6f 66 EKRESULT flag of
1e52c 20 50 35 20 69 73 20 73 65 74 20 61 6e 64 20 69 P5 is set and i
1e52d 66 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a f the result of.
1e52e 2a 2a 20 74 68 65 20 6c 61 73 74 20 73 65 65 6b ** the last seek
1e52f 20 6f 70 65 72 61 74 69 6f 6e 20 28 4f 50 5f 4e operation (OP_N
1e530 6f 74 45 78 69 73 74 73 29 20 77 61 73 20 61 20 otExists) was a
1e531 73 75 63 63 65 73 73 2c 20 74 68 65 6e 20 74 68 success, then th
1e532 69 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 is.** operation
1e533 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 will not attempt
1e534 20 74 6f 20 66 69 6e 64 20 74 68 65 20 61 70 70 to find the app
1e535 72 6f 70 72 69 61 74 65 20 72 6f 77 20 62 65 66 ropriate row bef
1e536 6f 72 65 20 64 6f 69 6e 67 0a 2a 2a 20 74 68 65 ore doing.** the
1e537 20 69 6e 73 65 72 74 20 62 75 74 20 77 69 6c 6c insert but will
1e538 20 69 6e 73 74 65 61 64 20 6f 76 65 72 77 72 69 instead overwri
1e539 74 65 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 te the row that
1e53a 74 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a the cursor is.**
1e53b 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1e53c 69 6e 67 20 74 6f 2e 20 20 50 72 65 73 75 6d 61 ing to. Presuma
1e53d 62 6c 79 2c 20 74 68 65 20 70 72 69 6f 72 20 4f bly, the prior O
1e53e 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 70 63 6f P_NotExists opco
1e53f 64 65 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 de.** has alread
1e540 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 74 68 65 y positioned the
1e541 20 63 75 72 73 6f 72 20 63 6f 72 72 65 63 74 6c cursor correctl
1e542 79 2e 20 20 54 68 69 73 20 69 73 20 61 6e 20 6f y. This is an o
1e543 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 ptimization.** t
1e544 68 61 74 20 62 6f 6f 73 74 73 20 70 65 72 66 6f hat boosts perfo
1e545 72 6d 61 6e 63 65 20 62 79 20 61 76 6f 69 64 69 rmance by avoidi
1e546 6e 67 20 72 65 64 75 6e 64 61 6e 74 20 73 65 65 ng redundant see
1e547 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ks..**.** If the
1e548 20 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 OPFLAG_ISUPDATE
1e549 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
1e54a 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 en this opcode i
1e54b 73 20 70 61 72 74 20 6f 66 20 61 6e 0a 2a 2a 20 s part of an.**
1e54c 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e UPDATE operation
1e54d 2e 20 20 4f 74 68 65 72 77 69 73 65 20 28 69 66 . Otherwise (if
1e54e 20 74 68 65 20 66 6c 61 67 20 69 73 20 63 6c 65 the flag is cle
1e54f 61 72 29 20 74 68 65 6e 20 74 68 69 73 20 6f 70 ar) then this op
1e550 63 6f 64 65 0a 2a 2a 20 69 73 20 70 61 72 74 20 code.** is part
1e551 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 70 65 of an INSERT ope
1e552 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 69 66 ration. The dif
1e553 66 65 72 65 6e 63 65 20 69 73 20 6f 6e 6c 79 20 ference is only
1e554 69 6d 70 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 important to.**
1e555 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e the update hook.
1e556 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 .**.** Parameter
1e557 20 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f P4 may point to
1e558 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 a string contai
1e559 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d 6e ning the table-n
1e55a 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 ame, or.** may b
1e55b 65 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 e NULL. If it is
1e55c 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 not NULL, then
1e55d 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 the update-hook
1e55e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55 70 .** (sqlite3.xUp
1e55f 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69 73 dateCallback) is
1e560 20 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 invoked followi
1e561 6e 67 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 ng a successful
1e562 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 insert..**.** (W
1e563 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 ARNING/TODO: If
1e564 50 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d 63 P1 is a pseudo-c
1e565 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 ursor and P2 is
1e566 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 dynamically.** a
1e567 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 6f llocated, then o
1e568 77 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 69 wnership of P2 i
1e569 73 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 6f s transferred to
1e56a 20 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 73 the pseudo-curs
1e56b 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 74 or.** and regist
1e56c 65 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 70 er P2 becomes ep
1e56d 68 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 65 hemeral. If the
1e56e 20 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e 67 cursor is chang
1e56f 65 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 ed, the.** value
1e570 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 of register P2
1e571 77 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67 65 will then change
1e572 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 . Make sure thi
1e573 73 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 s does not.** ca
1e574 75 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 use any problems
1e575 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e .).**.** This in
1e576 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 struction only w
1e577 6f 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20 orks on tables.
1e578 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 The equivalent
1e579 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 instruction.** f
1e57a 6f 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f 50 or indices is OP
1e57b 5f 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 2f _IdxInsert..*/./
1e57c 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 * Opcode: Insert
1e57d 49 6e 74 20 50 31 20 50 32 20 50 33 20 50 34 20 Int P1 P2 P3 P4
1e57e 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1e57f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 rks exactly like
1e580 20 4f 50 5f 49 6e 73 65 72 74 20 65 78 63 65 70 OP_Insert excep
1e581 74 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 69 t that the key i
1e582 73 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 s the.** integer
1e583 20 76 61 6c 75 65 20 50 33 2c 20 6e 6f 74 20 74 value P3, not t
1e584 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
1e585 69 6e 74 65 67 65 72 20 73 74 6f 72 65 64 20 69 integer stored i
1e586 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1e587 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 /.case OP_Insert
1e588 3a 20 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 : .case OP_Inser
1e589 74 49 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f tInt: {.#if 0 /
1e58a 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e58b 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e58c 66 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 f */. Mem *pDat
1e58d 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 a; /* MEM
1e58e 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 cell holding dat
1e58f 61 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 a for the record
1e590 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 to be inserted
1e591 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 */. Mem *pKey;
1e592 20 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 /* MEM ce
1e593 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 ll holding key
1e594 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a for the record *
1e595 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 /. i64 iKey;
1e596 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 /* The int
1e597 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 eger ROWID or ke
1e598 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 y for the record
1e599 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 to be inserted
1e59a 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
1e59b 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 *pC; /* Cursor
1e59c 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 to table into w
1e59d 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 hich insert is w
1e59e 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 ritten */. int
1e59f 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a nZero; /*
1e5a0 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d Number of zero-
1e5a1 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 bytes to append
1e5a2 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 */. int seekRes
1e5a3 75 6c 74 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 ult; /* Result
1e5a4 20 6f 66 20 70 72 69 6f 72 20 73 65 65 6b 20 6f of prior seek o
1e5a5 72 20 30 20 69 66 20 6e 6f 20 55 53 45 53 45 45 r 0 if no USESEE
1e5a6 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a KRESULT flag */.
1e5a7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
1e5a8 62 3b 20 20 2f 2a 20 64 61 74 61 62 61 73 65 20 b; /* database
1e5a9 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 name - used by t
1e5aa 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a he update hook *
1e5ab 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1e5ac 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e zTbl; /* Table n
1e5ad 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 ame - used by th
1e5ae 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f e opdate hook */
1e5af 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 . int op;
1e5b0 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 /* Opcode f
1e5b1 6f 72 20 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 or update hook:
1e5b2 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 6f 72 SQLITE_UPDATE or
1e5b3 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a SQLITE_INSERT *
1e5b4 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 /.#endif /* loca
1e5b5 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e5b6 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a d into u.bf */..
1e5b7 20 20 75 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 u.bf.pData = &
1e5b8 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1e5b9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1e5ba 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1e5bb 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 ->nCursor );. u
1e5bc 2e 62 66 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .bf.pC = p->apCs
1e5bd 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1e5be 73 65 72 74 28 20 75 2e 62 66 2e 70 43 21 3d 30 sert( u.bf.pC!=0
1e5bf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1e5c0 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d bf.pC->pCursor!=
1e5c1 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1e5c2 2e 62 66 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 .bf.pC->pseudoTa
1e5c3 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 bleReg==0 );. a
1e5c4 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e ssert( u.bf.pC->
1e5c5 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 isTable );. REG
1e5c6 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1e5c7 3e 70 32 2c 20 75 2e 62 66 2e 70 44 61 74 61 29 >p2, u.bf.pData)
1e5c8 3b 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 ;.. if( pOp->op
1e5c9 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20 code==OP_Insert
1e5ca 29 7b 0a 20 20 20 20 75 2e 62 66 2e 70 4b 65 79 ){. u.bf.pKey
1e5cb 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1e5cc 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 ];. assert( u
1e5cd 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 20 .bf.pKey->flags
1e5ce 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 & MEM_Int );.
1e5cf 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1e5d0 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 4b pOp->p3, u.bf.pK
1e5d1 65 79 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b ey);. u.bf.iK
1e5d2 65 79 20 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e ey = u.bf.pKey->
1e5d3 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 u.i;. }else{.
1e5d4 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f assert( pOp->o
1e5d5 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 pcode==OP_Insert
1e5d6 49 6e 74 20 29 3b 0a 20 20 20 20 75 2e 62 66 2e Int );. u.bf.
1e5d7 69 4b 65 79 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a iKey = pOp->p3;.
1e5d8 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e }.. if( pOp->
1e5d9 70 35 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 p5 & OPFLAG_NCHA
1e5da 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 NGE ) p->nChange
1e5db 2b 2b 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 ++;. if( pOp->p
1e5dc 35 20 26 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 5 & OPFLAG_LASTR
1e5dd 4f 57 49 44 20 29 20 64 62 2d 3e 6c 61 73 74 52 OWID ) db->lastR
1e5de 6f 77 69 64 20 3d 20 75 2e 62 66 2e 69 4b 65 79 owid = u.bf.iKey
1e5df 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44 61 ;. if( u.bf.pDa
1e5e0 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ta->flags & MEM_
1e5e1 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62 66 Null ){. u.bf
1e5e2 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 .pData->z = 0;.
1e5e3 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e u.bf.pData->n
1e5e4 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
1e5e5 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e assert( u.bf.
1e5e6 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 pData->flags & (
1e5e7 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 MEM_Blob|MEM_Str
1e5e8 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 2e ) );. }. u.bf.
1e5e9 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 70 seekResult = ((p
1e5ea 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1e5eb 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f USESEEKRESULT) ?
1e5ec 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52 65 u.bf.pC->seekRe
1e5ed 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 sult : 0);. if(
1e5ee 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 u.bf.pData->fla
1e5ef 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b gs & MEM_Zero ){
1e5f0 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 . u.bf.nZero
1e5f1 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 75 2e = u.bf.pData->u.
1e5f2 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a nZero;. }else{.
1e5f3 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d u.bf.nZero =
1e5f4 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0;. }. sqlite
1e5f5 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 3BtreeSetCachedR
1e5f6 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e 70 43 owid(u.bf.pC->pC
1e5f7 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 ursor, 0);. rc
1e5f8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e = sqlite3BtreeIn
1e5f9 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e 70 43 sert(u.bf.pC->pC
1e5fa 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 2e 69 ursor, 0, u.bf.i
1e5fb 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
1e5fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
1e5fd 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e .bf.pData->z, u.
1e5fe 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62 bf.pData->n, u.b
1e5ff 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 f.nZero,.
1e600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e601 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 pOp->p5 & OPF
1e602 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62 66 LAG_APPEND, u.bf
1e603 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b .seekResult. );
1e604 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 69 . u.bf.pC->rowi
1e605 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1e606 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bf.pC->deferre
1e607 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 dMoveto = 0;. u
1e608 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 .bf.pC->cacheSta
1e609 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1e60a 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 E;.. /* Invoke
1e60b 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 the update-hook
1e60c 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a if required. */.
1e60d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1e60e 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 _OK && db->xUpda
1e60f 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f teCallback && pO
1e610 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75 p->p4.z ){. u
1e611 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44 .bf.zDb = db->aD
1e612 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 5d 2e b[u.bf.pC->iDb].
1e613 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 66 2e zName;. u.bf.
1e614 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a zTbl = pOp->p4.z
1e615 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 3d 20 ;. u.bf.op =
1e616 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c ((pOp->p5 & OPFL
1e617 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53 AG_ISUPDATE) ? S
1e618 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53 QLITE_UPDATE : S
1e619 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 QLITE_INSERT);.
1e61a 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e assert( u.bf.
1e61b 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 pC->isTable );.
1e61c 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 db->xUpdateCa
1e61d 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 llback(db->pUpda
1e61e 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 2c 20 teArg, u.bf.op,
1e61f 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a u.bf.zDb, u.bf.z
1e620 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 29 3b Tbl, u.bf.iKey);
1e621 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e622 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a f.pC->iDb>=0 );.
1e623 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1e624 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 /* Opcode: Delet
1e625 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a e P1 P2 * P4 *.*
1e626 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 *.** Delete the
1e627 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20 record at which
1e628 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 the P1 cursor is
1e629 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1e62a 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ing..**.** The c
1e62b 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 ursor will be le
1e62c 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 ft pointing at e
1e62d 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f ither the next o
1e62e 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a r the previous.*
1e62f 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 * record in the
1e630 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20 table. If it is
1e631 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
1e632 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 the next record
1e633 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 , then.** the ne
1e634 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 xt Next instruct
1e635 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f ion will be a no
1e636 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69 -op. Hence it i
1e637 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a s OK to delete.*
1e638 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 * a record from
1e639 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c within an Next l
1e63a 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 oop..**.** If th
1e63b 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 e OPFLAG_NCHANGE
1e63c 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73 flag of P2 is s
1e63d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 et, then the row
1e63e 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 change count is
1e63f 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 .** incremented
1e640 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e (otherwise not).
1e641 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e .**.** P1 must n
1e642 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62 ot be pseudo-tab
1e643 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62 le. It has to b
1e644 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77 e a real table w
1e645 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 ith.** multiple
1e646 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 rows..**.** If P
1e647 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 4 is not NULL, t
1e648 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61 hen it is the na
1e649 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1e64a 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f that P1 is.** po
1e64b 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 inting to. The
1e64c 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c update hook will
1e64d 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20 be invoked, if
1e64e 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66 it exists..** If
1e64f 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 P4 is not NULL
1e650 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73 then the P1 curs
1e651 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 or must have bee
1e652 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 n positioned.**
1e653 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e using OP_NotFoun
1e654 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b d prior to invok
1e655 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e ing this opcode.
1e656 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 .*/.case OP_Dele
1e657 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 te: {.#if 0 /*
1e658 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e659 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 moved into u.bg
1e65a 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 */. i64 iKey;.
1e65b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e65c 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e65d 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e65e 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20 into u.bg */..
1e65f 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 3b 0a u.bg.iKey = 0;.
1e660 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e661 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e662 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e663 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bg.pC = p->apC
1e664 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e665 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21 3d ssert( u.bg.pC!=
1e666 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1e667 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .bg.pC->pCursor!
1e668 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 =0 ); /* Only v
1e669 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61 alid for real ta
1e66a 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74 bles, no pseudot
1e66b 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 ables */.. /* I
1e66c 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f f the update-hoo
1e66d 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 k will be invoke
1e66e 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b 65 79 d, set u.bg.iKey
1e66f 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 to the rowid of
1e670 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 the. ** row be
1e671 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a ing deleted.. *
1e672 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 /. if( db->xUpd
1e673 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 ateCallback && p
1e674 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 Op->p4.z ){.
1e675 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d assert( u.bg.pC-
1e676 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1e677 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d assert( u.bg.pC-
1e678 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 3b >rowidIsValid );
1e679 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 73 /* lastRowid s
1e67a 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 4f et by previous O
1e67b 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 P_NotFound */.
1e67c 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 75 2e u.bg.iKey = u.
1e67d 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 bg.pC->lastRowid
1e67e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
1e67f 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 65 OP_Delete opcode
1e680 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20 always follows
1e681 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 an OP_NotExists
1e682 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20 or OP_Last or.
1e683 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20 ** OP_Column on
1e684 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 77 the same table w
1e685 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65 72 ithout any inter
1e686 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e vening operation
1e687 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68 s that. ** migh
1e688 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69 t move or invali
1e689 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 2e date the cursor.
1e68a 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20 75 Hence cursor u
1e68b 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 79 73 .bg.pC is always
1e68c 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74 pointing. ** t
1e68d 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 o the row to be
1e68e 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20 deleted and the
1e68f 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1e690 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61 74 rMoveto() operat
1e691 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69 ion. ** below i
1e692 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70 s always a no-op
1e693 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c and cannot fail
1e694 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20 69 . We will run i
1e695 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68 t anyhow, though
1e696 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64 20 ,. ** to guard
1e697 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 63 against future c
1e698 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f hanges to the co
1e699 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20 de generator..
1e69a 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e **/. assert( u.
1e69b 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d bg.pC->deferredM
1e69c 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 oveto==0 );. rc
1e69d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
1e69e 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 67 2e rsorMoveto(u.bg.
1e69f 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 pC);. if( NEVER
1e6a0 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 (rc!=SQLITE_OK)
1e6a1 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1e6a2 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71 _to_error;.. sq
1e6a3 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
1e6a4 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70 43 hedRowid(u.bg.pC
1e6a5 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 ->pCursor, 0);.
1e6a6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e6a7 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e 70 43 eeDelete(u.bg.pC
1e6a8 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e ->pCursor);. u.
1e6a9 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bg.pC->cacheStat
1e6aa 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e6ab 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 ;.. /* Invoke t
1e6ac 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 he update-hook i
1e6ad 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 f required. */.
1e6ae 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e6af 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 OK && db->xUpdat
1e6b0 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 eCallback && pOp
1e6b1 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f ->p4.z ){. co
1e6b2 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 nst char *zDb =
1e6b3 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 43 2d db->aDb[u.bg.pC-
1e6b4 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 >iDb].zName;.
1e6b5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 const char *zTb
1e6b6 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 l = pOp->p4.z;.
1e6b7 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 db->xUpdateCa
1e6b8 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 llback(db->pUpda
1e6b9 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 teArg, SQLITE_DE
1e6ba 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c LETE, zDb, zTbl,
1e6bb 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 20 20 u.bg.iKey);.
1e6bc 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 assert( u.bg.pC
1e6bd 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a ->iDb>=0 );. }.
1e6be 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 if( pOp->p2 &
1e6bf 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 OPFLAG_NCHANGE )
1e6c0 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1e6c1 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63 break;.}./* Opc
1e6c2 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 ode: ResetCount
1e6c3 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 * * * * *.**.**
1e6c4 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 The value of the
1e6c5 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1e6c6 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65 is copied to the
1e6c7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1e6c8 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 .** change count
1e6c9 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20 er (returned by
1e6ca 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1e6cb 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e to sqlite3_chan
1e6cc 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20 ges())..** Then
1e6cd 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c the VMs internal
1e6ce 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1e6cf 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20 resets to 0..**
1e6d0 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
1e6d1 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 trigger programs
1e6d2 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 ..*/.case OP_Res
1e6d3 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c etCount: {. sql
1e6d4 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 ite3VdbeSetChang
1e6d5 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 es(db, p->nChang
1e6d6 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 e);. p->nChange
1e6d7 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1e6d8 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 ../* Opcode: Row
1e6d9 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a Data P1 P2 * * *
1e6da 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 .**.** Write int
1e6db 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 o register P2 th
1e6dc 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 e complete row d
1e6dd 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 ata for cursor P
1e6de 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 1..** There is n
1e6df 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e o interpretation
1e6e0 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a of the data. .
1e6e1 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f ** It is just co
1e6e2 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 pied onto the P2
1e6e3 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c register exactl
1e6e4 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 y as .** it is f
1e6e5 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 ound in the data
1e6e6 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
1e6e7 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f If the P1 curso
1e6e8 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 r must be pointi
1e6e9 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f ng to a valid ro
1e6ea 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f w (not a NULL ro
1e6eb 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 w).** of a real
1e6ec 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 table, not a pse
1e6ed 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a udo-table..*/./*
1e6ee 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 Opcode: RowKey
1e6ef 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1e6f0 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 * Write into reg
1e6f1 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d ister P2 the com
1e6f2 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f plete row key fo
1e6f3 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 r cursor P1..**
1e6f4 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 There is no inte
1e6f5 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 rpretation of th
1e6f6 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 e data. .** The
1e6f7 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f key is copied o
1e6f8 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 nto the P3 regis
1e6f9 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a ter exactly as .
1e6fa 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 ** it is found i
1e6fb 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1e6fc 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ile..**.** If th
1e6fd 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 e P1 cursor must
1e6fe 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 be pointing to
1e6ff 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 a valid row (not
1e700 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 a NULL row).**
1e701 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c of a real table,
1e702 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 not a pseudo-ta
1e703 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ble..*/.case OP_
1e704 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f RowKey:.case OP_
1e705 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20 30 RowData: {.#if 0
1e706 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e707 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e708 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.bh */. VdbeCu
1e709 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 rsor *pC;. BtCu
1e70a 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 rsor *pCrsr;. u
1e70b 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b 32 n;. i64 n64;
1e70c 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e70d 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e70e 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20 into u.bh */..
1e70f 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f pOut = &aMem[pO
1e710 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f p->p2];.. /* No
1e711 74 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61 te that RowKey a
1e712 6e 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72 nd RowData are r
1e713 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68 eally exactly th
1e714 65 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69 e same instructi
1e715 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 on */. assert(
1e716 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1e717 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 p->p1<p->nCursor
1e718 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20 );. u.bh.pC =
1e719 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1e71a 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 ];. assert( u.b
1e71b 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c h.pC->isTable ||
1e71c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1e71d 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20 61 73 73 _RowKey );. ass
1e71e 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 ert( u.bh.pC->is
1e71f 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 Index || pOp->op
1e720 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 code==OP_RowData
1e721 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1e722 62 68 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 bh.pC!=0 );. as
1e723 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 6e sert( u.bh.pC->n
1e724 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 ullRow==0 );. a
1e725 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e ssert( u.bh.pC->
1e726 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d pseudoTableReg==
1e727 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1e728 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .bh.pC->pCursor!
1e729 3d 30 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 72 =0 );. u.bh.pCr
1e72a 73 72 20 3d 20 75 2e 62 68 2e 70 43 2d 3e 70 43 sr = u.bh.pC->pC
1e72b 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 ursor;. assert(
1e72c 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1e72d 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 68 2e sorIsValid(u.bh.
1e72e 70 43 72 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 pCrsr) );.. /*
1e72f 54 68 65 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e The OP_RowKey an
1e730 64 20 4f 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 d OP_RowData opc
1e731 6f 64 65 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c odes always foll
1e732 6f 77 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 ow OP_NotExists
1e733 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e or. ** OP_Rewin
1e734 64 2f 4f 70 5f 4e 65 78 74 20 77 69 74 68 20 6e d/Op_Next with n
1e735 6f 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e o intervening in
1e736 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20 structions that
1e737 6d 69 67 68 74 20 69 6e 76 61 6c 69 64 61 74 65 might invalidate
1e738 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 . ** the cursor
1e739 2e 20 20 48 65 6e 63 65 20 74 68 65 20 66 6f 6c . Hence the fol
1e73a 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 64 lowing sqlite3Vd
1e73b 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 beCursorMoveto()
1e73c 20 63 61 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a call is always.
1e73d 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 ** a no-op and
1e73e 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e can never fail.
1e73f 20 20 42 75 74 20 77 65 20 6c 65 61 76 65 20 69 But we leave i
1e740 74 20 69 6e 20 70 6c 61 63 65 20 61 73 20 61 20 t in place as a
1e741 73 61 66 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 safety.. */. a
1e742 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e ssert( u.bh.pC->
1e743 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d deferredMoveto==
1e744 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 0 );. rc = sqli
1e745 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 te3VdbeCursorMov
1e746 65 74 6f 28 75 2e 62 68 2e 70 43 29 3b 0a 20 20 eto(u.bh.pC);.
1e747 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 if( NEVER(rc!=SQ
1e748 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 LITE_OK) ) goto
1e749 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1e74a 6f 72 3b 0a 0a 20 20 69 66 28 20 75 2e 62 68 2e or;.. if( u.bh.
1e74b 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 pC->isIndex ){.
1e74c 20 20 20 61 73 73 65 72 74 28 20 21 75 2e 62 68 assert( !u.bh
1e74d 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a .pC->isTable );.
1e74e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e74f 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 BtreeKeySize(u.b
1e750 68 2e 70 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e h.pCrsr, &u.bh.n
1e751 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 64);. assert(
1e752 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1e753 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 ; /* True bec
1e754 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f ause of CursorMo
1e755 76 65 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 veto() call abov
1e756 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 e */. if( u.b
1e757 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 h.n64>db->aLimit
1e758 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
1e759 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 NGTH] ){. g
1e75a 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 oto too_big;.
1e75b 20 7d 0a 20 20 20 20 75 2e 62 68 2e 6e 20 3d 20 }. u.bh.n =
1e75c 28 75 33 32 29 75 2e 62 68 2e 6e 36 34 3b 0a 20 (u32)u.bh.n64;.
1e75d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
1e75e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
1e75f 61 53 69 7a 65 28 75 2e 62 68 2e 70 43 72 73 72 aSize(u.bh.pCrsr
1e760 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a 20 20 20 20 , &u.bh.n);.
1e761 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1e762 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 44 TE_OK ); /* D
1e763 61 74 61 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 ataSize() cannot
1e764 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 69 66 28 fail */. if(
1e765 20 75 2e 62 68 2e 6e 3e 28 75 33 32 29 64 62 2d u.bh.n>(u32)db-
1e766 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1e767 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1e768 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 goto too_b
1e769 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ig;. }. }.
1e76a 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
1e76b 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 75 2e 62 emGrow(pOut, u.b
1e76c 68 2e 6e 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 h.n, 0) ){. g
1e76d 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1e76e 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 75 2e 62 68 pOut->n = u.bh
1e76f 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 .n;. MemSetType
1e770 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 Flag(pOut, MEM_B
1e771 6c 6f 62 29 3b 0a 20 20 69 66 28 20 75 2e 62 68 lob);. if( u.bh
1e772 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a .pC->isIndex ){.
1e773 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e774 42 74 72 65 65 4b 65 79 28 75 2e 62 68 2e 70 43 BtreeKey(u.bh.pC
1e775 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 rsr, 0, u.bh.n,
1e776 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73 pOut->z);. }els
1e777 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e{. rc = sqli
1e778 74 65 33 42 74 72 65 65 44 61 74 61 28 75 2e 62 te3BtreeData(u.b
1e779 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 h.pCrsr, 0, u.bh
1e77a 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 .n, pOut->z);.
1e77b 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 }. pOut->enc =
1e77c 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a SQLITE_UTF8; /*
1e77d 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f In case the blo
1e77e 62 20 69 73 20 65 76 65 72 20 63 61 73 74 20 74 b is ever cast t
1e77f 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 o text */. UPDA
1e780 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1e781 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pOut);. break;.
1e782 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f }../* Opcode: Ro
1e783 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a wid P1 P2 * * *.
1e784 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 **.** Store in r
1e785 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e egister P2 an in
1e786 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
1e787 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 61 he key of the ta
1e788 62 6c 65 20 65 6e 74 72 79 20 74 68 61 74 0a 2a ble entry that.*
1e789 2a 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c * P1 is currentl
1e78a 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a y point to..**.*
1e78b 2a 20 50 31 20 63 61 6e 20 62 65 20 65 69 74 68 * P1 can be eith
1e78c 65 72 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 er an ordinary t
1e78d 61 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 able or a virtua
1e78e 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 72 65 20 l table. There
1e78f 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 used to.** be a
1e790 73 65 70 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 separate OP_VRow
1e791 69 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 id opcode for us
1e792 65 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 e with virtual t
1e793 61 62 6c 65 73 2c 20 62 75 74 20 74 68 69 73 0a ables, but this.
1e794 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f ** one opcode no
1e795 77 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 w works for both
1e796 20 74 61 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f table types..*/
1e797 0a 63 61 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 .case OP_Rowid:
1e798 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1e799 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1e79a 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f ease */.#if 0 /
1e79b 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e79c 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e79d 69 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f i */. VdbeCurso
1e79e 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a r *pC;. i64 v;.
1e79f 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1e7a0 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 pVtab;. const s
1e7a1 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
1e7a2 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f Module;.#endif /
1e7a3 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e7a4 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e7a5 69 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 i */.. assert(
1e7a6 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1e7a7 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 p->p1<p->nCursor
1e7a8 20 29 3b 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20 );. u.bi.pC =
1e7a9 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1e7aa 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 ];. assert( u.b
1e7ab 69 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 i.pC!=0 );. ass
1e7ac 65 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 70 73 ert( u.bi.pC->ps
1e7ad 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 eudoTableReg==0
1e7ae 29 3b 0a 20 20 69 66 28 20 75 2e 62 69 2e 70 43 );. if( u.bi.pC
1e7af 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 ->nullRow ){.
1e7b0 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1e7b1 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 62 72 65 EM_Null;. bre
1e7b2 61 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ak;. }else if(
1e7b3 75 2e 62 69 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bi.pC->deferre
1e7b4 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20 20 75 dMoveto ){. u
1e7b5 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d .bi.v = u.bi.pC-
1e7b6 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 0a 23 >movetoTarget;.#
1e7b7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1e7b8 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1e7b9 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 69 }else if( u.bi
1e7ba 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 .pC->pVtabCursor
1e7bb 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 ){. u.bi.pVt
1e7bc 61 62 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 70 56 ab = u.bi.pC->pV
1e7bd 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 tabCursor->pVtab
1e7be 3b 0a 20 20 20 20 75 2e 62 69 2e 70 4d 6f 64 75 ;. u.bi.pModu
1e7bf 6c 65 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d le = u.bi.pVtab-
1e7c0 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 61 73 >pModule;. as
1e7c1 73 65 72 74 28 20 75 2e 62 69 2e 70 4d 6f 64 75 sert( u.bi.pModu
1e7c2 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20 le->xRowid );.
1e7c3 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1e7c4 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1e7c5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1e7c6 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d 20 isuse;. rc =
1e7c7 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 u.bi.pModule->xR
1e7c8 6f 77 69 64 28 75 2e 62 69 2e 70 43 2d 3e 70 56 owid(u.bi.pC->pV
1e7c9 74 61 62 43 75 72 73 6f 72 2c 20 26 75 2e 62 69 tabCursor, &u.bi
1e7ca 2e 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 .v);. sqlite3
1e7cb 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1e7cc 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1e7cd 45 72 72 4d 73 67 20 3d 20 75 2e 62 69 2e 70 56 ErrMsg = u.bi.pV
1e7ce 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 tab->zErrMsg;.
1e7cf 20 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a 45 u.bi.pVtab->zE
1e7d0 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 rrMsg = 0;. i
1e7d1 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1e7d2 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1e7d3 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1e7d4 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c e;.#endif /* SQL
1e7d5 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1e7d6 54 41 42 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 65 TABLE */. }else
1e7d7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e {. assert( u.
1e7d8 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d bi.pC->pCursor!=
1e7d9 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 0 );. rc = sq
1e7da 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
1e7db 6f 76 65 74 6f 28 75 2e 62 69 2e 70 43 29 3b 0a oveto(u.bi.pC);.
1e7dc 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
1e7dd 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1e7de 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 2e rror;. if( u.
1e7df 62 69 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 bi.pC->rowidIsVa
1e7e0 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 lid ){. u.b
1e7e1 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6c i.v = u.bi.pC->l
1e7e2 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 7d 65 astRowid;. }e
1e7e3 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1e7e4 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 sqlite3BtreeKeyS
1e7e5 69 7a 65 28 75 2e 62 69 2e 70 43 2d 3e 70 43 75 ize(u.bi.pC->pCu
1e7e6 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a rsor, &u.bi.v);.
1e7e7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
1e7e8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 ==SQLITE_OK );
1e7e9 2f 2a 20 41 6c 77 61 79 73 20 73 6f 20 62 65 63 /* Always so bec
1e7ea 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f ause of CursorMo
1e7eb 76 65 74 6f 28 29 20 61 62 6f 76 65 20 2a 2f 0a veto() above */.
1e7ec 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74 }. }. pOut
1e7ed 2d 3e 75 2e 69 20 3d 20 75 2e 62 69 2e 76 3b 0a ->u.i = u.bi.v;.
1e7ee 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e7ef 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 pcode: NullRow P
1e7f0 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1e7f1 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
1e7f2 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 P1 to a null row
1e7f3 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e . Any OP_Column
1e7f4 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 operations.** t
1e7f5 68 61 74 20 6f 63 63 75 72 20 77 68 69 6c 65 20 hat occur while
1e7f6 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f 6e the cursor is on
1e7f7 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 20 77 69 the null row wi
1e7f8 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 77 72 69 ll always.** wri
1e7f9 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 te a NULL..*/.ca
1e7fa 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b se OP_NullRow: {
1e7fb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1e7fc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e7fd 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 20 20 into u.bj */.
1e7fe 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1e7ff 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e800 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e801 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 0a 20 20 into u.bj */..
1e802 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1e803 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1e804 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e >nCursor );. u.
1e805 62 6a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 bj.pC = p->apCsr
1e806 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1e807 65 72 74 28 20 75 2e 62 6a 2e 70 43 21 3d 30 20 ert( u.bj.pC!=0
1e808 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e 6e 75 );. u.bj.pC->nu
1e809 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 62 llRow = 1;. u.b
1e80a 6a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c j.pC->rowidIsVal
1e80b 69 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e id = 0;. if( u.
1e80c 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 20 29 bj.pC->pCursor )
1e80d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
1e80e 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 75 2e eeClearCursor(u.
1e80f 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b bj.pC->pCursor);
1e810 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e811 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 73 74 ./* Opcode: Last
1e812 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1e813 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 ** The next use
1e814 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 of the Rowid or
1e815 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 Column or Next i
1e816 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 nstruction for P
1e817 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 1 .** will refer
1e818 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 to the last ent
1e819 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ry in the databa
1e81a 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 se table or inde
1e81b 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 x..** If the tab
1e81c 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 le or index is e
1e81d 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 mpty and P2>0, t
1e81e 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 hen jump immedia
1e81f 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 tely to P2..** I
1e820 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 f P2 is 0 or if
1e821 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 the table or ind
1e822 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c ex is not empty,
1e823 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a fall through.**
1e824 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
1e825 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a g instruction..*
1e826 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 74 3a 20 /.case OP_Last:
1e827 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e828 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e829 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e82a 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f ved into u.bk */
1e82b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1e82c 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1e82d 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b Crsr;. int res;
1e82e 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e82f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e830 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 0a 20 into u.bk */..
1e831 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1e832 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1e833 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 ->nCursor );. u
1e834 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .bk.pC = p->apCs
1e835 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1e836 73 65 72 74 28 20 75 2e 62 6b 2e 70 43 21 3d 30 sert( u.bk.pC!=0
1e837 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 72 73 72 );. u.bk.pCrsr
1e838 20 3d 20 75 2e 62 6b 2e 70 43 2d 3e 70 43 75 72 = u.bk.pC->pCur
1e839 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 6b 2e sor;. if( u.bk.
1e83a 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20 pCrsr==0 ){.
1e83b 75 2e 62 6b 2e 72 65 73 20 3d 20 31 3b 0a 20 20 u.bk.res = 1;.
1e83c 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1e83d 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 sqlite3BtreeLast
1e83e 28 75 2e 62 6b 2e 70 43 72 73 72 2c 20 26 75 2e (u.bk.pCrsr, &u.
1e83f 62 6b 2e 72 65 73 29 3b 0a 20 20 7d 0a 20 20 75 bk.res);. }. u
1e840 2e 62 6b 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .bk.pC->nullRow
1e841 3d 20 28 75 38 29 75 2e 62 6b 2e 72 65 73 3b 0a = (u8)u.bk.res;.
1e842 20 20 75 2e 62 6b 2e 70 43 2d 3e 64 65 66 65 72 u.bk.pC->defer
1e843 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1e844 20 75 2e 62 6b 2e 70 43 2d 3e 72 6f 77 69 64 49 u.bk.pC->rowidI
1e845 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e sValid = 0;. u.
1e846 62 6b 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bk.pC->cacheStat
1e847 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e848 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e ;. if( pOp->p2>
1e849 30 20 26 26 20 75 2e 62 6b 2e 72 65 73 20 29 7b 0 && u.bk.res ){
1e84a 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1e84b 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1e84c 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 ak;.}.../* Opcod
1e84d 65 3a 20 53 6f 72 74 20 50 31 20 50 32 20 2a 20 e: Sort P1 P2 *
1e84e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f * *.**.** This o
1e84f 70 63 6f 64 65 20 64 6f 65 73 20 65 78 61 63 74 pcode does exact
1e850 6c 79 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e ly the same thin
1e851 67 20 61 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 g as OP_Rewind e
1e852 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 xcept that.** it
1e853 20 69 6e 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 increments an u
1e854 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 ndocumented glob
1e855 61 6c 20 76 61 72 69 61 62 6c 65 20 75 73 65 64 al variable used
1e856 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a for testing..**
1e857 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69 73 20 61 .** Sorting is a
1e858 63 63 6f 6d 70 6c 69 73 68 65 64 20 62 79 20 77 ccomplished by w
1e859 72 69 74 69 6e 67 20 72 65 63 6f 72 64 73 20 69 riting records i
1e85a 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e nto a sorting in
1e85b 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 dex,.** then rew
1e85c 69 6e 64 69 6e 67 20 74 68 61 74 20 69 6e 64 65 inding that inde
1e85d 78 20 61 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 x and playing it
1e85e 20 62 61 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e back from begin
1e85f 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 ning to.** end.
1e860 20 57 65 20 75 73 65 20 74 68 65 20 4f 50 5f 53 We use the OP_S
1e861 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 ort opcode inste
1e862 61 64 20 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 ad of OP_Rewind
1e863 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 to do the.** rew
1e864 69 6e 64 69 6e 67 20 73 6f 20 74 68 61 74 20 74 inding so that t
1e865 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 he global variab
1e866 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 le will be incre
1e867 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 mented and.** re
1e868 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 63 gression tests c
1e869 61 6e 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 an determine whe
1e86a 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
1e86b 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 optimizer is.**
1e86c 63 6f 72 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 correctly optimi
1e86d 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a zing out sorts..
1e86e 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a */.case OP_Sort:
1e86f 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1e870 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 p */.#ifdef SQLI
1e871 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 TE_TEST. sqlite
1e872 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 3_sort_count++;.
1e873 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 sqlite3_search
1e874 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 _count--;.#endif
1e875 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 . p->aCounter[S
1e876 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
1e877 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a _SORT-1]++;. /*
1e878 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e Fall through in
1e879 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a to OP_Rewind */.
1e87a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 }./* Opcode: Rew
1e87b 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ind P1 P2 * * *.
1e87c 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 **.** The next u
1e87d 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 se of the Rowid
1e87e 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 or Column or Nex
1e87f 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f t instruction fo
1e880 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 r P1 .** will re
1e881 66 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 fer to the first
1e882 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1e883 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1e884 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 index..** If the
1e885 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1e886 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e is empty and P2>
1e887 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 0, then jump imm
1e888 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1e889 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 ** If P2 is 0 or
1e88a 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 if the table or
1e88b 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d index is not em
1e88c 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 pty, fall throug
1e88d 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1e88e 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1e88f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 n..*/.case OP_Re
1e890 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f wind: { /
1e891 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 * jump */.#if 0
1e892 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e893 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e894 2e 62 6c 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .bl */. VdbeCur
1e895 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1e896 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1e897 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a t res;.#endif /*
1e898 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e899 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c moved into u.bl
1e89a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e89b 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e89c 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e89d 29 3b 0a 20 20 75 2e 62 6c 2e 70 43 20 3d 20 70 );. u.bl.pC = p
1e89e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e89f 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6c ;. assert( u.bl
1e8a0 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 .pC!=0 );. if(
1e8a1 28 75 2e 62 6c 2e 70 43 72 73 72 20 3d 20 75 2e (u.bl.pCrsr = u.
1e8a2 62 6c 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 21 bl.pC->pCursor)!
1e8a3 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 =0 ){. rc = s
1e8a4 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74 qlite3BtreeFirst
1e8a5 28 75 2e 62 6c 2e 70 43 72 73 72 2c 20 26 75 2e (u.bl.pCrsr, &u.
1e8a6 62 6c 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 bl.res);. u.b
1e8a7 6c 2e 70 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 l.pC->atFirst =
1e8a8 75 2e 62 6c 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 u.bl.res==0 ?1:0
1e8a9 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 64 ;. u.bl.pC->d
1e8aa 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1e8ab 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 0;. u.bl.pC->
1e8ac 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e8ad 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75 CHE_STALE;. u
1e8ae 2e 62 6c 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .bl.pC->rowidIsV
1e8af 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 alid = 0;. }els
1e8b0 65 7b 0a 20 20 20 20 75 2e 62 6c 2e 72 65 73 20 e{. u.bl.res
1e8b1 3d 20 31 3b 0a 20 20 7d 0a 20 20 75 2e 62 6c 2e = 1;. }. u.bl.
1e8b2 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 pC->nullRow = (u
1e8b3 38 29 75 2e 62 6c 2e 72 65 73 3b 0a 20 20 61 73 8)u.bl.res;. as
1e8b4 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1e8b5 26 26 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f && pOp->p2<p->nO
1e8b6 70 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 6c 2e p );. if( u.bl.
1e8b7 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 res ){. pc =
1e8b8 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1e8b9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e8ba 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 50 31 20 Opcode: Next P1
1e8bb 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 P2 * * *.**.** A
1e8bc 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20 50 31 dvance cursor P1
1e8bd 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e so that it poin
1e8be 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6b ts to the next k
1e8bf 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e 20 ey/data pair in
1e8c0 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 its.** table or
1e8c1 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 65 index. If there
1e8c2 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b 65 79 are no more key
1e8c3 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 /value pairs the
1e8c4 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a n fall through.*
1e8c5 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * to the followi
1e8c6 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 ng instruction.
1e8c7 20 42 75 74 20 69 66 20 74 68 65 20 63 75 72 73 But if the curs
1e8c8 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73 20 73 or advance was s
1e8c9 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 uccessful,.** ju
1e8ca 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1e8cb 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 o P2..**.** The
1e8cc 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 P1 cursor must b
1e8cd 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 61 62 e for a real tab
1e8ce 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f le, not a pseudo
1e8cf 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 -table..**.** Se
1e8d0 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a e also: Prev.*/.
1e8d1 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65 76 20 /* Opcode: Prev
1e8d2 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1e8d3 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73 6f 72 * Back up cursor
1e8d4 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 P1 so that it p
1e8d5 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 72 65 oints to the pre
1e8d6 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61 20 70 vious key/data p
1e8d7 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 air in its.** ta
1e8d8 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 ble or index. I
1e8d9 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 72 f there is no pr
1e8da 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c 75 65 evious key/value
1e8db 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c pairs then fall
1e8dc 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 through.** to t
1e8dd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 he following ins
1e8de 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 truction. But i
1e8df 66 20 74 68 65 20 63 75 72 73 6f 72 20 62 61 63 f the cursor bac
1e8e0 6b 75 70 20 77 61 73 20 73 75 63 63 65 73 73 66 kup was successf
1e8e1 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 ul,.** jump imme
1e8e2 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a diately to P2..*
1e8e3 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 73 *.** The P1 curs
1e8e4 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 61 or must be for a
1e8e5 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 real table, not
1e8e6 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e a pseudo-table.
1e8e7 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 65 76 .*/.case OP_Prev
1e8e8 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : /* ju
1e8e9 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 mp */.case OP_Ne
1e8ea 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 xt: { /*
1e8eb 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f jump */.#if 0 /
1e8ec 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e8ed 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e8ee 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f m */. VdbeCurso
1e8ef 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f r *pC;. BtCurso
1e8f0 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 r *pCrsr;. int
1e8f1 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c res;.#endif /* l
1e8f2 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e8f3 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a oved into u.bm *
1e8f4 2f 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 /.. CHECK_FOR_I
1e8f5 4e 54 45 52 52 55 50 54 3b 0a 20 20 61 73 73 65 NTERRUPT;. asse
1e8f6 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e8f7 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e8f8 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6d 2e 70 rsor );. u.bm.p
1e8f9 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e8fa 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 75 2e 62 ->p1];. if( u.b
1e8fb 6d 2e 70 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 m.pC==0 ){. b
1e8fc 72 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 reak; /* See ti
1e8fd 63 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 cket #2273 */.
1e8fe 7d 0a 20 20 75 2e 62 6d 2e 70 43 72 73 72 20 3d }. u.bm.pCrsr =
1e8ff 20 75 2e 62 6d 2e 70 43 2d 3e 70 43 75 72 73 6f u.bm.pC->pCurso
1e900 72 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 r;. if( u.bm.pC
1e901 72 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e rsr==0 ){. u.
1e902 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d bm.pC->nullRow =
1e903 20 31 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 1;. break;.
1e904 20 7d 0a 20 20 75 2e 62 6d 2e 72 65 73 20 3d 20 }. u.bm.res =
1e905 31 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 1;. assert( u.b
1e906 6d 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f m.pC->deferredMo
1e907 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 veto==0 );. rc
1e908 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f = pOp->opcode==O
1e909 50 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74 65 33 P_Next ? sqlite3
1e90a 42 74 72 65 65 4e 65 78 74 28 75 2e 62 6d 2e 70 BtreeNext(u.bm.p
1e90b 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29 Crsr, &u.bm.res)
1e90c 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 :.
1e90d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e90e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 sqlite3BtreePre
1e90f 76 69 6f 75 73 28 75 2e 62 6d 2e 70 43 72 73 72 vious(u.bm.pCrsr
1e910 2c 20 26 75 2e 62 6d 2e 72 65 73 29 3b 0a 20 20 , &u.bm.res);.
1e911 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 u.bm.pC->nullRow
1e912 20 3d 20 28 75 38 29 75 2e 62 6d 2e 72 65 73 3b = (u8)u.bm.res;
1e913 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 63 61 63 68 . u.bm.pC->cach
1e914 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1e915 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 75 2e 62 STALE;. if( u.b
1e916 6d 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 m.res==0 ){.
1e917 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1e918 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 ;. if( pOp->p
1e919 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 5 ) p->aCounter[
1e91a 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 pOp->p5-1]++;.#i
1e91b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1e91c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 . sqlite3_sea
1e91d 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e rch_count++;.#en
1e91e 64 69 66 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 dif. }. u.bm.p
1e91f 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1e920 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a = 0;. break;.}.
1e921 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 49 ./* Opcode: IdxI
1e922 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20 2a nsert P1 P2 P3 *
1e923 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 P5.**.** Regist
1e924 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20 53 51 er P2 holds a SQ
1e925 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 64 65 L index key made
1e926 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 4d 61 using the.** Ma
1e927 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 keRecord instruc
1e928 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f 70 63 tions. This opc
1e929 6f 64 65 20 77 72 69 74 65 73 20 74 68 61 74 20 ode writes that
1e92a 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 key.** into the
1e92b 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74 61 20 index P1. Data
1e92c 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 69 73 for the entry is
1e92d 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 nil..**.** P3 i
1e92e 73 20 61 20 66 6c 61 67 20 74 68 61 74 20 70 72 s a flag that pr
1e92f 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20 74 6f ovides a hint to
1e930 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 the b-tree laye
1e931 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 69 r that this.** i
1e932 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 nsert is likely
1e933 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 2e to be an append.
1e934 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1e935 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 ruction only wor
1e936 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 2e 20 ks for indices.
1e937 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 The equivalent
1e938 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 instruction.** f
1e939 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f 50 5f or tables is OP_
1e93a 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20 Insert..*/.case
1e93b 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 7b 20 OP_IdxInsert: {
1e93c 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f /* in2 */
1e93d 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1e93e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e93f 20 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a 20 20 into u.bn */.
1e940 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1e941 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1e942 72 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 r;. int nKey;.
1e943 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 const char *zKe
1e944 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 y;.#endif /* loc
1e945 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e946 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a ed into u.bn */.
1e947 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e948 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e949 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e94a 20 75 2e 62 6e 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bn.pC = p->ap
1e94b 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e94c 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43 21 assert( u.bn.pC!
1e94d 3d 30 20 29 3b 0a 20 20 70 49 6e 32 20 3d 20 26 =0 );. pIn2 = &
1e94e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1e94f 20 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 assert( pIn2->f
1e950 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 lags & MEM_Blob
1e951 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 73 72 20 );. u.bn.pCrsr
1e952 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 73 = u.bn.pC->pCurs
1e953 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 or;. if( ALWAYS
1e954 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d 30 29 20 (u.bn.pCrsr!=0)
1e955 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 ){. assert( u
1e956 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d .bn.pC->isTable=
1e957 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 45 =0 );. rc = E
1e958 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b xpandBlob(pIn2);
1e959 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1e95a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1e95b 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e 32 u.bn.nKey = pIn2
1e95c 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e ->n;. u.bn.
1e95d 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a zKey = pIn2->z;.
1e95e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1e95f 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 75 2e e3BtreeInsert(u.
1e960 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 6e 2e 7a bn.pCrsr, u.bn.z
1e961 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c 20 Key, u.bn.nKey,
1e962 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 "", 0, 0, pOp->p
1e963 33 2c 0a 20 20 20 20 20 20 20 20 20 20 28 28 70 3,. ((p
1e964 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1e965 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f USESEEKRESULT) ?
1e966 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 65 u.bn.pC->seekRe
1e967 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 20 20 sult : 0).
1e968 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1e969 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 65 72 72 u.bn.pC->deferr
1e96a 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1e96b 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 61 u.bn.pC->ca
1e96c 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1e96d 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 E_STALE;. }.
1e96e 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e96f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c * Opcode: IdxDel
1e970 65 74 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a ete P1 P2 P3 * *
1e971 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 .**.** The conte
1e972 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73 74 65 nt of P3 registe
1e973 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 rs starting at r
1e974 65 67 69 73 74 65 72 20 50 32 20 66 6f 72 6d 0a egister P2 form.
1e975 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 ** an unpacked i
1e976 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 73 20 6f ndex key. This o
1e977 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68 pcode removes th
1e978 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 at entry from th
1e979 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e e .** index open
1e97a 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e ed by cursor P1.
1e97b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 .*/.case OP_IdxD
1e97c 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 elete: {.#if 0
1e97d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e97e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e97f 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 bo */. VdbeCurs
1e980 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 or *pC;. BtCurs
1e981 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 or *pCrsr;. int
1e982 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 res;. Unpacked
1e983 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 Record r;.#endif
1e984 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e985 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e986 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .bo */.. assert
1e987 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 ( pOp->p3>0 );.
1e988 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1e989 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f >0 && pOp->p2+pO
1e98a 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 p->p3<=p->nMem+1
1e98b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1e98c 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1e98d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1e98e 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d ;. u.bo.pC = p-
1e98f 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e990 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f 2e . assert( u.bo.
1e991 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e pC!=0 );. u.bo.
1e992 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70 43 2d pCrsr = u.bo.pC-
1e993 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 >pCursor;. if(
1e994 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 43 72 73 ALWAYS(u.bo.pCrs
1e995 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75 2e 62 r!=0) ){. u.b
1e996 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 o.r.pKeyInfo = u
1e997 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f .bo.pC->pKeyInfo
1e998 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 69 ;. u.bo.r.nFi
1e999 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e eld = (u16)pOp->
1e99a 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 66 p3;. u.bo.r.f
1e99b 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e lags = 0;. u.
1e99c 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 bo.r.aMem = &aMe
1e99d 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1e99e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e99f 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1e9a0 75 2e 62 6f 2e 70 43 72 73 72 2c 20 26 75 2e 62 u.bo.pCrsr, &u.b
1e9a1 6f 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 6f o.r, 0, 0, &u.bo
1e9a2 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 .res);. if( r
1e9a3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1e9a4 75 2e 62 6f 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 u.bo.res==0 ){.
1e9a5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e9a6 33 42 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62 3BtreeDelete(u.b
1e9a7 6f 2e 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a o.pCrsr);. }.
1e9a8 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f assert( u.bo
1e9a9 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e9aa 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e eto==0 );. u.
1e9ab 62 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bo.pC->cacheStat
1e9ac 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e9ad 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1e9ae 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 ../* Opcode: Idx
1e9af 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a 20 Rowid P1 P2 * *
1e9b0 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e *.**.** Write in
1e9b1 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 61 to register P2 a
1e9b2 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
1e9b3 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 is the last entr
1e9b4 79 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 y in the record
1e9b5 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 at.** the end of
1e9b6 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 70 the index key p
1e9b7 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 63 75 72 ointed to by cur
1e9b8 73 6f 72 20 50 31 2e 20 20 54 68 69 73 20 69 6e sor P1. This in
1e9b9 74 65 67 65 72 20 73 68 6f 75 6c 64 20 62 65 0a teger should be.
1e9ba 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 ** the rowid of
1e9bb 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 the table entry
1e9bc 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 69 6e to which this in
1e9bd 64 65 78 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 dex entry points
1e9be 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1e9bf 3a 20 52 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63 : Rowid, MakeRec
1e9c0 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ord..*/.case OP_
1e9c1 49 64 78 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 IdxRowid: {
1e9c2 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1e9c3 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 -prerelease */.#
1e9c4 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e9c5 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e9c6 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 20 20 42 74 nto u.bp */. Bt
1e9c7 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1e9c8 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e9c9 0a 20 20 69 36 34 20 72 6f 77 69 64 3b 0a 23 65 . i64 rowid;.#e
1e9ca 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e9cb 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e9cc 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 to u.bp */.. as
1e9cd 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e9ce 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e9cf 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70 Cursor );. u.bp
1e9d0 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e9d1 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e9d2 74 28 20 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b t( u.bp.pC!=0 );
1e9d3 0a 20 20 75 2e 62 70 2e 70 43 72 73 72 20 3d 20 . u.bp.pCrsr =
1e9d4 75 2e 62 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bp.pC->pCursor
1e9d5 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 ;. pOut->flags
1e9d6 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 66 = MEM_Null;. if
1e9d7 28 20 41 4c 57 41 59 53 28 75 2e 62 70 2e 70 43 ( ALWAYS(u.bp.pC
1e9d8 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 72 rsr!=0) ){. r
1e9d9 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 c = sqlite3VdbeC
1e9da 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 70 ursorMoveto(u.bp
1e9db 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 .pC);. if( NE
1e9dc 56 45 52 28 72 63 29 20 29 20 67 6f 74 6f 20 61 VER(rc) ) goto a
1e9dd 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e9de 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 r;. assert( u
1e9df 2e 62 70 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bp.pC->deferred
1e9e0 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 Moveto==0 );.
1e9e1 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 assert( u.bp.pC
1e9e2 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a ->isTable==0 );.
1e9e3 20 20 20 20 69 66 28 20 21 75 2e 62 70 2e 70 43 if( !u.bp.pC
1e9e4 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 ->nullRow ){.
1e9e5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1e9e6 64 62 65 49 64 78 52 6f 77 69 64 28 64 62 2c 20 dbeIdxRowid(db,
1e9e7 75 2e 62 70 2e 70 43 72 73 72 2c 20 26 75 2e 62 u.bp.pCrsr, &u.b
1e9e8 70 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 p.rowid);.
1e9e9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1e9ea 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
1e9eb 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1e9ec 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rror;. }.
1e9ed 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1e9ee 75 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20 u.bp.rowid;.
1e9ef 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1e9f0 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 20 MEM_Int;. }.
1e9f1 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e9f2 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20 * Opcode: IdxGE
1e9f3 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1e9f4 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 *.** The P4 regi
1e9f5 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 ster values begi
1e9f6 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f nning with P3 fo
1e9f7 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 rm an unpacked i
1e9f8 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 ndex .** key tha
1e9f9 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 t omits the ROWI
1e9fa 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 D. Compare this
1e9fb 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e key value again
1e9fc 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a st the index .**
1e9fd 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 that P1 is curr
1e9fe 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 ently pointing t
1e9ff 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 o, ignoring the
1ea00 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 ROWID on the P1
1ea01 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 index..**.** If
1ea02 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 the P1 index ent
1ea03 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ry is greater th
1ea04 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 an or equal to t
1ea05 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 he key value.**
1ea06 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1ea07 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c Otherwise fall
1ea08 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1ea09 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
1ea0a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 ..**.** If P5 is
1ea0b 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 non-zero then t
1ea0c 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 he key value is
1ea0d 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 increased by an
1ea0e 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f epsilon .** prio
1ea0f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 r to the compari
1ea10 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 son. This make
1ea11 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 the opcode work
1ea12 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 70 like IdxGT excep
1ea13 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65 t.** that if the
1ea14 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74 key from regist
1ea15 65 72 20 50 33 20 69 73 20 61 20 70 72 65 66 69 er P3 is a prefi
1ea16 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20 x of the key in
1ea17 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 the cursor,.** t
1ea18 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c he result is fal
1ea19 73 65 20 77 68 65 72 65 61 73 20 69 74 20 77 6f se whereas it wo
1ea1a 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 68 uld be true with
1ea1b 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 IdxGT..*/./* Op
1ea1c 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50 code: IdxLT P1 P
1ea1d 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 2 P3 * P5.**.**
1ea1e 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 The P4 register
1ea1f 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 values beginning
1ea20 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e with P3 form an
1ea21 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 unpacked index
1ea22 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 .** key that omi
1ea23 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 ts the ROWID. C
1ea24 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 ompare this key
1ea25 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 value against th
1ea26 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 e index .** that
1ea27 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 P1 is currently
1ea28 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 pointing to, ig
1ea29 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 noring the ROWID
1ea2a 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 on the P1 index
1ea2b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 ..**.** If the P
1ea2c 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 1 index entry is
1ea2d 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b less than the k
1ea2e 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 ey value then ju
1ea2f 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 mp to P2..** Oth
1ea30 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f erwise fall thro
1ea31 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ugh to the next
1ea32 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a instruction..**.
1ea33 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d ** If P5 is non-
1ea34 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 zero then the ke
1ea35 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 y value is incre
1ea36 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c ased by an epsil
1ea37 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 on prior .** to
1ea38 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 the comparison.
1ea39 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20 This makes the
1ea3a 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 opcode work like
1ea3b 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 IdxLE..*/.case
1ea3c 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20 OP_IdxLT:
1ea3d 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 /* jump */.ca
1ea3e 73 65 20 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 se OP_IdxGE: {
1ea3f 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f /* jump */
1ea40 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ea41 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ea42 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 into u.bq */.
1ea43 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1ea44 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 int res;. Unp
1ea45 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 ackedRecord r;.#
1ea46 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1ea47 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ea48 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 nto u.bq */.. a
1ea49 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1ea4a 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1ea4b 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1ea4c 71 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b q.pC = p->apCsr[
1ea4d 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1ea4e 72 74 28 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 rt( u.bq.pC!=0 )
1ea4f 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 ;. if( ALWAYS(u
1ea50 2e 62 71 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .bq.pC->pCursor!
1ea51 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 =0) ){. asser
1ea52 74 28 20 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 t( u.bq.pC->defe
1ea53 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1ea54 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1ea55 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e ->p5==0 || pOp->
1ea56 70 35 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 p5==1 );. ass
1ea57 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1ea58 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 ==P4_INT32 );.
1ea59 20 20 75 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 u.bq.r.pKeyInf
1ea5a 6f 20 3d 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 o = u.bq.pC->pKe
1ea5b 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e yInfo;. u.bq.
1ea5c 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 r.nField = (u16)
1ea5d 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 pOp->p4.i;. i
1ea5e 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 f( pOp->p5 ){.
1ea5f 20 20 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 u.bq.r.flags
1ea60 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 = UNPACKED_INCR
1ea61 4b 45 59 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 KEY | UNPACKED_I
1ea62 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 GNORE_ROWID;.
1ea63 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e }else{. u.
1ea64 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 bq.r.flags = UNP
1ea65 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 ACKED_IGNORE_ROW
1ea66 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e ID;. }. u.
1ea67 62 71 2e 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 bq.r.aMem = &aMe
1ea68 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1ea69 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1ea6a 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e IdxKeyCompare(u.
1ea6b 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20 bq.pC, &u.bq.r,
1ea6c 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 20 &u.bq.res);.
1ea6d 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1ea6e 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 =OP_IdxLT ){.
1ea6f 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75 u.bq.res = -u
1ea70 2e 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c .bq.res;. }el
1ea71 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
1ea72 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
1ea73 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20 P_IdxGE );.
1ea74 20 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20 u.bq.res++;.
1ea75 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 2e }. if( u.bq.
1ea76 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 res>0 ){. p
1ea77 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 c = pOp->p2 - 1
1ea78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1ea79 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ea7a 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 e: Destroy P1 P2
1ea7b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 P3 * *.**.** De
1ea7c 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 lete an entire d
1ea7d 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1ea7e 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1ea7f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 t page in the da
1ea80 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 tabase.** file i
1ea81 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a s given by P1..*
1ea82 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 *.** The table b
1ea83 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 eing destroyed i
1ea84 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 s in the main da
1ea85 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 tabase file if P
1ea86 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3==0. If.** P3=
1ea87 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c =1 then the tabl
1ea88 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 e to be clear is
1ea89 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 in the auxiliar
1ea8a 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a y database file.
1ea8b 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 ** that is used
1ea8c 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 to store tables
1ea8d 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 create using CRE
1ea8e 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 ATE TEMPORARY TA
1ea8f 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 BLE..**.** If AU
1ea90 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 TOVACUUM is enab
1ea91 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 led then it is p
1ea92 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f ossible that ano
1ea93 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a ther root page.*
1ea94 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 * might be moved
1ea95 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 into the newly
1ea96 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 deleted root pag
1ea97 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 e in order to ke
1ea98 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 ep all.** root p
1ea99 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 ages contiguous
1ea9a 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
1ea9b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1ea9c 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a . The former.**
1ea9d 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f value of the ro
1ea9e 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 ot page that mov
1ea9f 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 ed - its value b
1eaa0 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f efore the move o
1eaa1 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 ccurred -.** is
1eaa2 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1eaa3 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 er P2. If no pa
1eaa4 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 ge .** movement
1eaa5 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65 was required (be
1eaa6 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 cause the table
1eaa7 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 being dropped wa
1eaa8 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 s already .** th
1eaa9 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 e last one in th
1eaaa 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e e database) then
1eaab 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 a zero is store
1eaac 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 d in register P2
1eaad 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 ..** If AUTOVACU
1eaae 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 UM is disabled t
1eaaf 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 hen a zero is st
1eab0 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
1eab1 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1eab2 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 lso: Clear.*/.ca
1eab3 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b se OP_Destroy: {
1eab4 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1eab5 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1eab6 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1eab7 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1eab8 75 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d u.br */. int iM
1eab9 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 oved;. int iCnt
1eaba 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b ;. Vdbe *pVdbe;
1eabb 0a 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64 . int iDb;.#end
1eabc 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1eabd 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1eabe 20 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66 u.br */.#ifndef
1eabf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1eac0 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72 TUALTABLE. u.br
1eac1 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 .iCnt = 0;. for
1eac2 28 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e (u.br.pVdbe=db->
1eac3 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 pVdbe; u.br.pVdb
1eac4 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d 20 e; u.br.pVdbe =
1eac5 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78 u.br.pVdbe->pNex
1eac6 74 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 72 t){. if( u.br
1eac7 2e 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 .pVdbe->magic==V
1eac8 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 DBE_MAGIC_RUN &&
1eac9 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56 u.br.pVdbe->inV
1eaca 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 tabMethod<2 && u
1eacb 2e 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 .br.pVdbe->pc>=0
1eacc 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 69 ){. u.br.i
1eacd 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d Cnt++;. }. }
1eace 0a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43 .#else. u.br.iC
1eacf 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 nt = db->activeV
1ead0 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 dbeCnt;.#endif.
1ead1 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1ead2 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 66 28 20 75 EM_Null;. if( u
1ead3 2e 62 72 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 20 .br.iCnt>1 ){.
1ead4 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f rc = SQLITE_LO
1ead5 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65 72 72 CKED;. p->err
1ead6 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 orAction = OE_Ab
1ead7 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ort;. }else{.
1ead8 20 20 75 2e 62 72 2e 69 44 62 20 3d 20 70 4f 70 u.br.iDb = pOp
1ead9 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72 74 ->p3;. assert
1eada 28 20 75 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 29 ( u.br.iCnt==1 )
1eadb 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1eadc 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1eadd 3c 3c 75 2e 62 72 2e 69 44 62 29 29 21 3d 30 20 <<u.br.iDb))!=0
1eade 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1eadf 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c te3BtreeDropTabl
1eae0 65 28 64 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 e(db->aDb[u.br.i
1eae1 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 Db].pBt, pOp->p1
1eae2 2c 20 26 75 2e 62 72 2e 69 4d 6f 76 65 64 29 3b , &u.br.iMoved);
1eae3 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1eae4 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1eae5 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 pOut->u.i = u.br
1eae6 2e 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 .iMoved;.#ifndef
1eae7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1eae8 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
1eae9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1eaea 20 75 2e 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 u.br.iMoved!=0
1eaeb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1eaec 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 RootPageMoved(&d
1eaed 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d b->aDb[u.br.iDb]
1eaee 2c 20 75 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 , u.br.iMoved, p
1eaef 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 20 20 72 Op->p1);. r
1eaf0 65 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c esetSchemaOnFaul
1eaf1 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e t = 1;. }.#en
1eaf2 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b dif. }. break;
1eaf3 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
1eaf4 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a lear P1 P2 P3.**
1eaf5 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 .** Delete all c
1eaf6 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 ontents of the d
1eaf7 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1eaf8 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1eaf9 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 t page.** in the
1eafa 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1eafb 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20 s given by P1.
1eafc 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 But, unlike Dest
1eafd 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 roy, do not.** r
1eafe 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 emove the table
1eaff 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 or index from th
1eb00 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1eb01 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 .**.** The table
1eb02 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20 being clear is
1eb03 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 in the main data
1eb04 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d base file if P2=
1eb05 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 =0. If.** P2==1
1eb06 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 then the table
1eb07 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 to be clear is i
1eb08 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 n the auxiliary
1eb09 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
1eb0a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f that is used to
1eb0b 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 store tables cr
1eb0c 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 eate using CREAT
1eb0d 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c E TEMPORARY TABL
1eb0e 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 E..**.** If the
1eb0f 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d P3 value is non-
1eb10 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 zero, then the t
1eb11 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f able referred to
1eb12 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 must be an.** i
1eb13 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20 ntkey table (an
1eb14 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 SQL table, not a
1eb15 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 n index). In thi
1eb16 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63 s case the row c
1eb17 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 hange .** count
1eb18 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 is incremented b
1eb19 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 y the number of
1eb1a 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c rows in the tabl
1eb1b 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e e being cleared.
1eb1c 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72 .** If P3 is gr
1eb1d 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c eater than zero,
1eb1e 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
1eb1f 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1eb20 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f er P3 is.** also
1eb21 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 incremented by
1eb22 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
1eb23 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ws in the table
1eb24 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a being cleared..*
1eb25 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 *.** See also: D
1eb26 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f estroy.*/.case O
1eb27 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 P_Clear: {.#if 0
1eb28 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1eb29 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1eb2a 75 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 u.bs */. int nC
1eb2b 68 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a hange;.#endif /*
1eb2c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1eb2d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 moved into u.bs
1eb2e 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61 */.. u.bs.nCha
1eb2f 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 nge = 0;. asser
1eb30 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1eb31 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 & (1<<pOp->p2))
1eb32 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 !=0 );. rc = sq
1eb33 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
1eb34 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e able(. db->
1eb35 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 aDb[pOp->p2].pBt
1eb36 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d , pOp->p1, (pOp-
1eb37 3e 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61 >p3 ? &u.bs.nCha
1eb38 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 nge : 0). );.
1eb39 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 if( pOp->p3 ){.
1eb3a 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d p->nChange +=
1eb3b 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 u.bs.nChange;.
1eb3c 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 if( pOp->p3>0
1eb3d 20 29 7b 0a 20 20 20 20 20 20 61 4d 65 6d 5b 70 ){. aMem[p
1eb3e 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 75 Op->p3].u.i += u
1eb3f 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 .bs.nChange;.
1eb40 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1eb41 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 }../* Opcode: Cr
1eb42 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32 20 eateTable P1 P2
1eb43 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f * * *.**.** Allo
1eb44 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c 65 cate a new table
1eb45 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 in the main dat
1eb46 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 abase file if P1
1eb47 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a ==0 or in the.**
1eb48 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 auxiliary datab
1eb49 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d ase file if P1==
1eb4a 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61 63 1 or in an attac
1eb4b 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66 0a hed database if.
1eb4c 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65 20 ** P1>1. Write
1eb4d 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 the root page nu
1eb4e 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 mber of the new
1eb4f 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 table into.** re
1eb50 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20 gister P2.**.**
1eb51 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 The difference b
1eb52 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20 61 etween a table a
1eb53 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 nd an index is t
1eb54 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 his: A table mu
1eb55 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 st.** have a 4-b
1eb56 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 yte integer key
1eb57 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72 62 and can have arb
1eb58 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41 6e itrary data. An
1eb59 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e index.** has an
1eb5a 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20 62 arbitrary key b
1eb5b 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a ut no data..**.*
1eb5c 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 * See also: Crea
1eb5d 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 teIndex.*/./* Op
1eb5e 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64 65 code: CreateInde
1eb5f 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a x P1 P2 * * *.**
1eb60 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
1eb61 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 ew index in the
1eb62 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1eb63 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 le if P1==0 or i
1eb64 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 n the.** auxilia
1eb65 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ry database file
1eb66 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 if P1==1 or in
1eb67 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 an attached data
1eb68 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e base if.** P1>1.
1eb69 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f 74 Write the root
1eb6a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
1eb6b 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e the new table in
1eb6c 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 to.** register P
1eb6d 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63 2..**.** See doc
1eb6e 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 umentation on OP
1eb6f 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f 72 _CreateTable for
1eb70 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
1eb71 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 rmation..*/.case
1eb72 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 3a OP_CreateIndex:
1eb73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1eb74 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1eb75 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 /.case OP_Create
1eb76 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 Table: {
1eb77 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c /* out2-prerel
1eb78 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f ease */.#if 0 /
1eb79 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eb7a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1eb7b 74 20 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b t */. int pgno;
1eb7c 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 . int flags;.
1eb7d 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 Db *pDb;.#endif
1eb7e 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1eb7f 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1eb80 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 74 2e 70 67 bt */.. u.bt.pg
1eb81 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 no = 0;. assert
1eb82 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1eb83 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1eb84 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1eb85 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1eb86 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1eb87 0a 20 20 75 2e 62 74 2e 70 44 62 20 3d 20 26 64 . u.bt.pDb = &d
1eb88 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b b->aDb[pOp->p1];
1eb89 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 74 2e . assert( u.bt.
1eb8a 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 pDb->pBt!=0 );.
1eb8b 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1eb8c 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 ==OP_CreateTable
1eb8d 20 29 7b 0a 20 20 20 20 2f 2a 20 75 2e 62 74 2e ){. /* u.bt.
1eb8e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e flags = BTREE_IN
1eb8f 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 75 2e 62 TKEY; */. u.b
1eb90 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f t.flags = BTREE_
1eb91 4c 45 41 46 44 41 54 41 7c 42 54 52 45 45 5f 49 LEAFDATA|BTREE_I
1eb92 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a NTKEY;. }else{.
1eb93 20 20 20 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d u.bt.flags =
1eb94 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b BTREE_ZERODATA;
1eb95 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 . }. rc = sqli
1eb96 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
1eb97 62 6c 65 28 75 2e 62 74 2e 70 44 62 2d 3e 70 42 ble(u.bt.pDb->pB
1eb98 74 2c 20 26 75 2e 62 74 2e 70 67 6e 6f 2c 20 75 t, &u.bt.pgno, u
1eb99 2e 62 74 2e 66 6c 61 67 73 29 3b 0a 20 20 70 4f .bt.flags);. pO
1eb9a 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 74 2e 70 ut->u.i = u.bt.p
1eb9b 67 6e 6f 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a gno;. break;.}.
1eb9c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 ./* Opcode: Pars
1eb9d 65 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a 20 eSchema P1 P2 *
1eb9e 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 P4 *.**.** Read
1eb9f 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65 6e and parse all en
1eba0 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20 53 tries from the S
1eba1 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 QLITE_MASTER tab
1eba2 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 le of database P
1eba3 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 20 1.** that match
1eba4 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
1eba5 20 50 34 2e 20 20 50 32 20 69 73 20 74 68 65 20 P4. P2 is the
1eba6 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20 20 "force" flag.
1eba7 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 Always do.** the
1eba8 20 70 61 72 73 69 6e 67 20 69 66 20 50 32 20 69 parsing if P2 i
1eba9 73 20 74 72 75 65 2e 20 20 49 66 20 50 32 20 69 s true. If P2 i
1ebaa 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 s false, then th
1ebab 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 0a is routine is a.
1ebac 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 ** no-op if the
1ebad 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 schema is not cu
1ebae 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 rrently loaded.
1ebaf 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
1ebb0 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c if P2.** is fal
1ebb1 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 4d se, the SQLITE_M
1ebb2 41 53 54 45 52 20 74 61 62 6c 65 20 69 73 20 6f ASTER table is o
1ebb3 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20 74 68 nly parsed if th
1ebb4 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a e rest of the.**
1ebb5 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61 schema is alrea
1ebb6 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 dy loaded into t
1ebb7 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e he symbol table.
1ebb8 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1ebb9 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 de invokes the p
1ebba 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 65 20 arser to create
1ebbb 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61 a new virtual ma
1ebbc 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 chine,.** then r
1ebbd 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72 74 uns the new virt
1ebbe 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 ual machine. It
1ebbf 20 69 73 20 74 68 75 73 20 61 20 72 65 2d 65 6e is thus a re-en
1ebc0 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f trant opcode..*/
1ebc1 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63 .case OP_ParseSc
1ebc2 68 65 6d 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f hema: {.#if 0 /
1ebc3 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ebc4 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1ebc5 75 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a u */. int iDb;.
1ebc6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d const char *zM
1ebc7 61 73 74 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a aster;. char *z
1ebc8 53 71 6c 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 Sql;. InitData
1ebc9 69 6e 69 74 44 61 74 61 3b 0a 23 65 6e 64 69 66 initData;.#endif
1ebca 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ebcb 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ebcc 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e 62 75 2e 69 .bu */.. u.bu.i
1ebcd 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 Db = pOp->p1;.
1ebce 61 73 73 65 72 74 28 20 75 2e 62 75 2e 69 44 62 assert( u.bu.iDb
1ebcf 3e 3d 30 20 26 26 20 75 2e 62 75 2e 69 44 62 3c >=0 && u.bu.iDb<
1ebd0 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a db->nDb );.. /*
1ebd1 20 49 66 20 70 4f 70 2d 3e 70 32 20 69 73 20 30 If pOp->p2 is 0
1ebd2 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f , then this opco
1ebd3 64 65 20 69 73 20 62 65 69 6e 67 20 65 78 65 63 de is being exec
1ebd4 75 74 65 64 20 74 6f 20 72 65 61 64 20 61 0a 20 uted to read a.
1ebd5 20 2a 2a 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 ** single row,
1ebd6 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 for example the
1ebd7 72 6f 77 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e row correspondin
1ebd8 67 20 74 6f 20 61 20 6e 65 77 20 69 6e 64 65 78 g to a new index
1ebd9 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62 79 . ** created by
1ebda 20 74 68 69 73 20 56 44 42 45 2c 20 66 72 6f 6d this VDBE, from
1ebdb 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 the sqlite_mast
1ebdc 65 72 20 74 61 62 6c 65 2e 20 49 74 20 6f 6e 6c er table. It onl
1ebdd 79 0a 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 y. ** does this
1ebde 20 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f if the correspo
1ebdf 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 nding in-memory
1ebe0 73 63 68 65 6d 61 20 69 73 20 63 75 72 72 65 6e schema is curren
1ebe1 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 2e tly. ** loaded.
1ebe2 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
1ebe3 6e 65 77 20 69 6e 64 65 78 20 64 65 66 69 6e 69 new index defini
1ebe4 74 69 6f 6e 20 63 61 6e 20 62 65 20 6c 6f 61 64 tion can be load
1ebe5 65 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 ed along. ** wi
1ebe6 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 th the rest of t
1ebe7 68 65 20 73 63 68 65 6d 61 20 77 68 65 6e 20 69 he schema when i
1ebe8 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 20 t is required..
1ebe9 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 **. ** Althoug
1ebea 68 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 h the mutex on t
1ebeb 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 he BtShared obje
1ebec 63 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f ct that correspo
1ebed 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 64 61 74 61 nds to. ** data
1ebee 62 61 73 65 20 75 2e 62 75 2e 69 44 62 20 28 74 base u.bu.iDb (t
1ebef 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 he database cont
1ebf0 61 69 6e 69 6e 67 20 74 68 65 20 73 71 6c 69 74 aining the sqlit
1ebf1 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 e_master table.
1ebf2 20 2a 2a 20 72 65 61 64 20 62 79 20 74 68 69 73 ** read by this
1ebf3 20 69 6e 73 74 72 75 63 74 69 6f 6e 29 20 69 73 instruction) is
1ebf4 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 2c currently held,
1ebf5 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 it is necessary
1ebf6 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 to. ** obtain
1ebf7 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 the mutexes on a
1ebf8 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 ll attached data
1ebf9 62 61 73 65 73 20 62 65 66 6f 72 65 20 63 68 65 bases before che
1ebfa 63 6b 69 6e 67 20 69 66 0a 20 20 2a 2a 20 74 68 cking if. ** th
1ebfb 65 20 73 63 68 65 6d 61 20 6f 66 20 75 2e 62 75 e schema of u.bu
1ebfc 2e 69 44 62 20 69 73 20 6c 6f 61 64 65 64 2e 20 .iDb is loaded.
1ebfd 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 2c This is because,
1ebfe 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
1ebff 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 . ** the sqlite
1ec00 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 62 65 3_exec() call be
1ec01 6c 6f 77 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c low, SQLite will
1ec02 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a 20 73 71 6c invoke. ** sql
1ec03 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c ite3BtreeEnterAl
1ec04 6c 28 29 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 l(). If all mute
1ec05 78 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 xes are not alre
1ec06 61 64 79 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 ady held, the.
1ec07 2a 2a 20 75 2e 62 75 2e 69 44 62 20 6d 75 74 65 ** u.bu.iDb mute
1ec08 78 20 6d 61 79 20 62 65 20 74 65 6d 70 6f 72 61 x may be tempora
1ec09 72 69 6c 79 20 72 65 6c 65 61 73 65 64 20 74 6f rily released to
1ec0a 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e avoid deadlock.
1ec0b 20 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 If. ** this ha
1ec0c 70 70 65 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 ppens, then some
1ec0d 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 61 other thread ma
1ec0e 79 20 64 65 6c 65 74 65 20 74 68 65 20 69 6e 2d y delete the in-
1ec0f 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 73 63 68 65 memory. ** sche
1ec10 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20 75 ma of database u
1ec11 2e 62 75 2e 69 44 62 20 62 65 66 6f 72 65 20 74 .bu.iDb before t
1ec12 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1ec13 20 72 75 6e 73 2e 20 54 68 65 20 73 63 68 65 6d runs. The schem
1ec14 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 a. ** will not
1ec15 62 65 20 72 65 6c 6f 61 64 65 64 20 62 65 63 75 be reloaded becu
1ec16 61 73 65 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 ase the db->init
1ec17 2e 62 75 73 79 20 66 6c 61 67 20 69 73 20 73 65 .busy flag is se
1ec18 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e t. This. ** can
1ec19 20 72 65 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f result in a "no
1ec1a 20 73 75 63 68 20 74 61 62 6c 65 3a 20 73 71 6c such table: sql
1ec1b 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 ite_master" or "
1ec1c 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64 malformed. ** d
1ec1d 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 22 20 atabase schema"
1ec1e 65 72 72 6f 72 20 62 65 69 6e 67 20 72 65 74 75 error being retu
1ec1f 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 rned to the user
1ec20 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
1ec21 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
1ec22 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b dsMutex(db->aDb[
1ec23 75 2e 62 75 2e 69 44 62 5d 2e 70 42 74 29 20 29 u.bu.iDb].pBt) )
1ec24 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1ec25 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 EnterAll(db);.
1ec26 69 66 28 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44 if( pOp->p2 || D
1ec27 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c bHasProperty(db,
1ec28 20 75 2e 62 75 2e 69 44 62 2c 20 44 42 5f 53 63 u.bu.iDb, DB_Sc
1ec29 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 hemaLoaded) ){.
1ec2a 20 20 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 20 u.bu.zMaster
1ec2b 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 75 = SCHEMA_TABLE(u
1ec2c 2e 62 75 2e 69 44 62 29 3b 0a 20 20 20 20 75 2e .bu.iDb);. u.
1ec2d 62 75 2e 69 6e 69 74 44 61 74 61 2e 64 62 20 3d bu.initData.db =
1ec2e 20 64 62 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e db;. u.bu.in
1ec2f 69 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 itData.iDb = pOp
1ec30 2d 3e 70 31 3b 0a 20 20 20 20 75 2e 62 75 2e 69 ->p1;. u.bu.i
1ec31 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 nitData.pzErrMsg
1ec32 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a = &p->zErrMsg;.
1ec33 20 20 20 20 75 2e 62 75 2e 7a 53 71 6c 20 3d 20 u.bu.zSql =
1ec34 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1ec35 62 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43 b,. "SELEC
1ec36 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 T name, rootpage
1ec37 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e , sql FROM '%q'.
1ec38 25 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20 %s WHERE %s",.
1ec39 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 75 2e 62 db->aDb[u.b
1ec3a 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75 2e u.iDb].zName, u.
1ec3b 62 75 2e 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d bu.zMaster, pOp-
1ec3c 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 >p4.z);. if(
1ec3d 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30 20 29 7b 0a u.bu.zSql==0 ){.
1ec3e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1ec3f 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c E_NOMEM;. }el
1ec40 73 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 se{. (void)
1ec41 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1ec42 28 64 62 29 3b 0a 20 20 20 20 20 20 61 73 73 65 (db);. asse
1ec43 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 rt( db->init.bus
1ec44 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62 y==0 );. db
1ec45 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b ->init.busy = 1;
1ec46 0a 20 20 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 . u.bu.init
1ec47 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 Data.rc = SQLITE
1ec48 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 _OK;. asser
1ec49 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 t( !db->mallocFa
1ec4a 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 63 iled );. rc
1ec4b 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 = sqlite3_exec(
1ec4c 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 2c 20 73 db, u.bu.zSql, s
1ec4d 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 qlite3InitCallba
1ec4e 63 6b 2c 20 26 75 2e 62 75 2e 69 6e 69 74 44 61 ck, &u.bu.initDa
1ec4f 74 61 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 ta, 0);. if
1ec50 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1ec51 29 20 72 63 20 3d 20 75 2e 62 75 2e 69 6e 69 74 ) rc = u.bu.init
1ec52 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 Data.rc;. s
1ec53 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ec54 20 75 2e 62 75 2e 7a 53 71 6c 29 3b 0a 20 20 20 u.bu.zSql);.
1ec55 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 db->init.busy
1ec56 20 3d 20 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 = 0;. (voi
1ec57 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
1ec58 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d n(db);. }. }
1ec59 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
1ec5a 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 eaveAll(db);. i
1ec5b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f f( rc==SQLITE_NO
1ec5c 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 MEM ){. goto
1ec5d 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 no_mem;. }. br
1ec5e 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 eak;.}..#if !def
1ec5f 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1ec60 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a 20 4f 70 63 _ANALYZE)./* Opc
1ec61 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 ode: LoadAnalysi
1ec62 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a s P1 * * * *.**.
1ec63 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c 69 ** Read the sqli
1ec64 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 te_stat1 table f
1ec65 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20 61 or database P1 a
1ec66 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74 nd load the cont
1ec67 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 ent.** of that t
1ec68 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e able into the in
1ec69 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61 73 ternal index has
1ec6a 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 77 h table. This w
1ec6b 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68 65 ill cause.** the
1ec6c 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65 20 analysis to be
1ec6d 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61 72 used when prepar
1ec6e 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75 65 ing all subseque
1ec6f 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 nt queries..*/.c
1ec70 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 ase OP_LoadAnaly
1ec71 73 69 73 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 sis: {. assert(
1ec72 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1ec73 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 Op->p1<db->nDb )
1ec74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1ec75 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c AnalysisLoad(db,
1ec76 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 62 72 65 pOp->p1);. bre
1ec77 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f ak; .}.#endif /
1ec78 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 * !defined(SQLIT
1ec79 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 E_OMIT_ANALYZE)
1ec7a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 */../* Opcode: D
1ec7b 72 6f 70 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 ropTable P1 * *
1ec7c 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 P4 *.**.** Remov
1ec7d 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 e the internal (
1ec7e 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 in-memory) data
1ec7f 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 structures that
1ec80 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 describe.** the
1ec81 74 61 62 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 table named P4 i
1ec82 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 n database P1.
1ec83 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 This is called a
1ec84 66 74 65 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 fter a table.**
1ec85 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 is dropped in or
1ec86 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 der to keep the
1ec87 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 internal represe
1ec88 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a ntation of the.*
1ec89 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 * schema consist
1ec8a 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 ent with what is
1ec8b 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 on disk..*/.cas
1ec8c 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 e OP_DropTable:
1ec8d 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e {. sqlite3Unlin
1ec8e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 kAndDeleteTable(
1ec8f 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 db, pOp->p1, pOp
1ec90 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b ->p4.z);. break
1ec91 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ec92 44 72 6f 70 49 6e 64 65 78 20 50 31 20 2a 20 2a DropIndex P1 * *
1ec93 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f P4 *.**.** Remo
1ec94 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 ve the internal
1ec95 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 (in-memory) data
1ec96 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 structures that
1ec97 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 describe.** the
1ec98 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 50 34 20 index named P4
1ec99 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 in database P1.
1ec9a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 This is called
1ec9b 61 66 74 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a after an index.*
1ec9c 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e 20 * is dropped in
1ec9d 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 order to keep th
1ec9e 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 e internal repre
1ec9f 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
1eca0 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 .** schema consi
1eca1 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 20 stent with what
1eca2 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 is on disk..*/.c
1eca3 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 ase OP_DropIndex
1eca4 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c : {. sqlite3Unl
1eca5 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 inkAndDeleteInde
1eca6 78 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 x(db, pOp->p1, p
1eca7 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 Op->p4.z);. bre
1eca8 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1eca9 3a 20 44 72 6f 70 54 72 69 67 67 65 72 20 50 31 : DropTrigger P1
1ecaa 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1ecab 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 Remove the inter
1ecac 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 nal (in-memory)
1ecad 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
1ecae 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a that describe.**
1ecaf 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d the trigger nam
1ecb0 65 64 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 ed P4 in databas
1ecb1 65 20 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 e P1. This is c
1ecb2 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20 74 72 alled after a tr
1ecb3 69 67 67 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 igger.** is drop
1ecb4 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ped in order to
1ecb5 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 6e 61 keep the interna
1ecb6 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e l representation
1ecb7 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d of the.** schem
1ecb8 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 a consistent wit
1ecb9 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 69 73 h what is on dis
1ecba 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 k..*/.case OP_Dr
1ecbb 6f 70 54 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 opTrigger: {. s
1ecbc 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
1ecbd 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c eleteTrigger(db,
1ecbe 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1ecbf 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 4.z);. break;.}
1ecc0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1ecc1 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
1ecc2 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 _CHECK./* Opcode
1ecc3 3a 20 49 6e 74 65 67 72 69 74 79 43 6b 20 50 31 : IntegrityCk P1
1ecc4 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a P2 P3 * P5.**.*
1ecc5 2a 20 44 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 * Do an analysis
1ecc6 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 6c of the currentl
1ecc7 79 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e y open database.
1ecc8 20 20 53 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 Store in.** re
1ecc9 67 69 73 74 65 72 20 50 31 20 74 68 65 20 74 65 gister P1 the te
1ecca 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d xt of an error m
1eccb 65 73 73 61 67 65 20 64 65 73 63 72 69 62 69 6e essage describin
1eccc 67 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a g any problems..
1eccd 2a 2a 20 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d ** If no problem
1ecce 73 20 61 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f s are found, sto
1eccf 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 re a NULL in reg
1ecd0 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 ister P1..**.**
1ecd1 54 68 65 20 72 65 67 69 73 74 65 72 20 50 33 20 The register P3
1ecd2 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 78 contains the max
1ecd3 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 imum number of a
1ecd4 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a llowed errors..*
1ecd5 2a 20 41 74 20 6d 6f 73 74 20 72 65 67 28 50 33 * At most reg(P3
1ecd6 29 20 65 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 ) errors will be
1ecd7 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e reported..** In
1ecd8 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 other words, th
1ecd9 65 20 61 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 e analysis stops
1ecda 20 61 73 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 as soon as reg(
1ecdb 50 31 29 20 65 72 72 6f 72 73 20 61 72 65 20 0a P1) errors are .
1ecdc 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67 28 50 31 ** seen. Reg(P1
1ecdd 29 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 ) is updated wit
1ecde 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 h the number of
1ecdf 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 errors remaining
1ece0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 ..**.** The root
1ece1 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 page numbers of
1ece2 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 all tables in t
1ece3 68 65 20 64 61 74 61 62 61 73 65 20 61 72 65 20 he database are
1ece4 69 6e 74 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 integer.** store
1ece5 64 20 69 6e 20 72 65 67 28 50 31 29 2c 20 72 65 d in reg(P1), re
1ece6 67 28 50 31 2b 31 29 2c 20 72 65 67 28 50 31 2b g(P1+1), reg(P1+
1ece7 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 2), .... There
1ece8 61 72 65 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a are P2 tables.**
1ece9 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 total..**.** If
1ecea 20 50 35 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c P5 is not zero,
1eceb 20 74 68 65 20 63 68 65 63 6b 20 69 73 20 64 6f the check is do
1ecec 6e 65 20 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 ne on the auxili
1eced 61 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ary database.**
1ecee 66 69 6c 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 file, not the ma
1ecef 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
1ecf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1ecf1 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 ode is used to i
1ecf2 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 mplement the int
1ecf3 65 67 72 69 74 79 5f 63 68 65 63 6b 20 70 72 61 egrity_check pra
1ecf4 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f gma..*/.case OP_
1ecf5 49 6e 74 65 67 72 69 74 79 43 6b 3a 20 7b 0a 23 IntegrityCk: {.#
1ecf6 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ecf7 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ecf8 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 20 20 69 6e nto u.bv */. in
1ecf9 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a t nRoot; /*
1ecfa 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 Number of table
1ecfb 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 s to check. (Nu
1ecfc 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 mber of root pag
1ecfd 65 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 es.) */. int *a
1ecfe 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 Root; /* Arr
1ecff 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e ay of rootpage n
1ed00 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 umbers for table
1ed01 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 s to be checked
1ed02 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 */. int j;
1ed03 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1ed04 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 nter */. int nE
1ed05 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d rr; /* Num
1ed06 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 ber of errors re
1ed07 70 6f 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 ported */. char
1ed08 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 *z; /* T
1ed09 65 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 ext of the error
1ed0a 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d report */. Mem
1ed0b 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 *pnErr; /*
1ed0c 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 Register keeping
1ed0d 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 track of errors
1ed0e 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 23 65 remaining */.#e
1ed0f 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ed10 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ed11 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a 20 20 75 2e to u.bv */.. u.
1ed12 62 76 2e 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e bv.nRoot = pOp->
1ed13 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e p2;. assert( u.
1ed14 62 76 2e 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 bv.nRoot>0 );.
1ed15 75 2e 62 76 2e 61 52 6f 6f 74 20 3d 20 73 71 6c u.bv.aRoot = sql
1ed16 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
1ed17 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a db, sizeof(int)*
1ed18 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b 31 29 20 29 (u.bv.nRoot+1) )
1ed19 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e 61 52 6f ;. if( u.bv.aRo
1ed1a 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ot==0 ) goto no_
1ed1b 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 mem;. assert( p
1ed1c 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d Op->p3>0 && pOp-
1ed1d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1ed1e 20 20 75 2e 62 76 2e 70 6e 45 72 72 20 3d 20 26 u.bv.pnErr = &
1ed1f 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1ed20 20 61 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70 assert( (u.bv.p
1ed21 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 nErr->flags & ME
1ed22 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 M_Int)!=0 );. a
1ed23 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 ssert( (u.bv.pnE
1ed24 72 72 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d rr->flags & (MEM
1ed25 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d _Str|MEM_Blob))=
1ed26 3d 30 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 =0 );. pIn1 = &
1ed27 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1ed28 20 66 6f 72 28 75 2e 62 76 2e 6a 3d 30 3b 20 75 for(u.bv.j=0; u
1ed29 2e 62 76 2e 6a 3c 75 2e 62 76 2e 6e 52 6f 6f 74 .bv.j<u.bv.nRoot
1ed2a 3b 20 75 2e 62 76 2e 6a 2b 2b 29 7b 0a 20 20 20 ; u.bv.j++){.
1ed2b 20 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 u.bv.aRoot[u.bv
1ed2c 2e 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 .j] = (int)sqlit
1ed2d 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 26 e3VdbeIntValue(&
1ed2e 70 49 6e 31 5b 75 2e 62 76 2e 6a 5d 29 3b 0a 20 pIn1[u.bv.j]);.
1ed2f 20 7d 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b }. u.bv.aRoot[
1ed30 75 2e 62 76 2e 6a 5d 20 3d 20 30 3b 0a 20 20 61 u.bv.j] = 0;. a
1ed31 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 ssert( pOp->p5<d
1ed32 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 b->nDb );. asse
1ed33 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1ed34 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 k & (1<<pOp->p5)
1ed35 29 21 3d 30 20 29 3b 0a 20 20 75 2e 62 76 2e 7a )!=0 );. u.bv.z
1ed36 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 = sqlite3BtreeI
1ed37 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64 62 ntegrityCheck(db
1ed38 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e 70 ->aDb[pOp->p5].p
1ed39 42 74 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 2c 20 Bt, u.bv.aRoot,
1ed3a 75 2e 62 76 2e 6e 52 6f 6f 74 2c 0a 20 20 20 20 u.bv.nRoot,.
1ed3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ed3c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e (in
1ed3d 74 29 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 2e t)u.bv.pnErr->u.
1ed3e 69 2c 20 26 75 2e 62 76 2e 6e 45 72 72 29 3b 0a i, &u.bv.nErr);.
1ed3f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ed40 64 62 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 29 3b db, u.bv.aRoot);
1ed41 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 . u.bv.pnErr->u
1ed42 2e 69 20 2d 3d 20 75 2e 62 76 2e 6e 45 72 72 3b .i -= u.bv.nErr;
1ed43 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1ed44 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a mSetNull(pIn1);.
1ed45 20 20 69 66 28 20 75 2e 62 76 2e 6e 45 72 72 3d if( u.bv.nErr=
1ed46 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 =0 ){. assert
1ed47 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 3b 0a 20 ( u.bv.z==0 );.
1ed48 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 76 2e }else if( u.bv.
1ed49 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f z==0 ){. goto
1ed4a 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73 65 no_mem;. }else
1ed4b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1ed4c 65 4d 65 6d 53 65 74 53 74 72 28 70 49 6e 31 2c eMemSetStr(pIn1,
1ed4d 20 75 2e 62 76 2e 7a 2c 20 2d 31 2c 20 53 51 4c u.bv.z, -1, SQL
1ed4e 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65 ITE_UTF8, sqlite
1ed4f 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20 55 3_free);. }. U
1ed50 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1ed51 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 ZE(pIn1);. sqli
1ed52 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1ed53 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f oding(pIn1, enco
1ed54 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a ding);. break;.
1ed55 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1ed56 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 TE_OMIT_INTEGRIT
1ed57 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f Y_CHECK */../* O
1ed58 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 41 64 64 pcode: RowSetAdd
1ed59 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1ed5a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 69 6e ** Insert the in
1ed5b 74 65 67 65 72 20 76 61 6c 75 65 20 68 65 6c 64 teger value held
1ed5c 20 62 79 20 72 65 67 69 73 74 65 72 20 50 32 20 by register P2
1ed5d 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 6e 20 69 into a boolean i
1ed5e 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 69 6e 20 ndex.** held in
1ed5f 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a register P1..**.
1ed60 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 ** An assertion
1ed61 66 61 69 6c 73 20 69 66 20 50 32 20 69 73 20 6e fails if P2 is n
1ed62 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a ot an integer..*
1ed63 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 /.case OP_RowSet
1ed64 41 64 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 Add: { /*
1ed65 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a 20 20 70 49 in1, in2 */. pI
1ed66 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n1 = &aMem[pOp->
1ed67 70 31 5d 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 p1];. pIn2 = &a
1ed68 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1ed69 61 73 73 65 72 74 28 20 28 70 49 6e 32 2d 3e 66 assert( (pIn2->f
1ed6a 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 lags & MEM_Int)!
1ed6b 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e =0 );. if( (pIn
1ed6c 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 1->flags & MEM_R
1ed6d 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 owSet)==0 ){.
1ed6e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1ed6f 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a etRowSet(pIn1);.
1ed70 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 if( (pIn1->f
1ed71 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
1ed72 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f t)==0 ) goto no_
1ed73 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 mem;. }. sqlit
1ed74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 e3RowSetInsert(p
1ed75 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 In1->u.pRowSet,
1ed76 70 49 6e 32 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 pIn2->u.i);. br
1ed77 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ed78 65 3a 20 52 6f 77 53 65 74 52 65 61 64 20 50 31 e: RowSetRead P1
1ed79 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1ed7a 20 45 78 74 72 61 63 74 20 74 68 65 20 73 6d 61 Extract the sma
1ed7b 6c 6c 65 73 74 20 76 61 6c 75 65 20 66 72 6f 6d llest value from
1ed7c 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 boolean index P
1ed7d 31 20 61 6e 64 20 70 75 74 20 74 68 61 74 20 76 1 and put that v
1ed7e 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 alue into.** reg
1ed7f 69 73 74 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 ister P3. Or, i
1ed80 66 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 f boolean index
1ed81 50 31 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 P1 is initially
1ed82 65 6d 70 74 79 2c 20 6c 65 61 76 65 20 50 33 0a empty, leave P3.
1ed83 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 ** unchanged and
1ed84 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 jump to instruc
1ed85 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 tion P2..*/.case
1ed86 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 OP_RowSetRead:
1ed87 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c { /* jump,
1ed88 20 69 6e 31 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 in1, out3 */.#i
1ed89 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ed8a 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ed8b 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 69 36 34 to u.bw */. i64
1ed8c 20 76 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 val;.#endif /*
1ed8d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ed8e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 moved into u.bw
1ed8f 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 */. CHECK_FOR_I
1ed90 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 49 6e 31 NTERRUPT;. pIn1
1ed91 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 = &aMem[pOp->p1
1ed92 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e ];. if( (pIn1->
1ed93 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1ed94 65 74 29 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c et)==0. || sql
1ed95 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 70 ite3RowSetNext(p
1ed96 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 In1->u.pRowSet,
1ed97 26 75 2e 62 77 2e 76 61 6c 29 3d 3d 30 0a 20 20 &u.bw.val)==0.
1ed98 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6f ){. /* The bo
1ed99 6f 6c 65 61 6e 20 69 6e 64 65 78 20 69 73 20 65 olean index is e
1ed9a 6d 70 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 mpty */. sqli
1ed9b 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1ed9c 6c 28 70 49 6e 31 29 3b 0a 20 20 20 20 70 63 20 l(pIn1);. pc
1ed9d 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1ed9e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 }else{. /* A
1ed9f 20 76 61 6c 75 65 20 77 61 73 20 70 75 6c 6c 65 value was pulle
1eda0 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 d from the index
1eda1 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
1eda2 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 dbeMemSetInt64(&
1eda3 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2c 20 75 aMem[pOp->p3], u
1eda4 2e 62 77 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 .bw.val);. }.
1eda5 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1eda6 6f 64 65 3a 20 52 6f 77 53 65 74 54 65 73 74 20 ode: RowSetTest
1eda7 50 31 20 50 32 20 50 33 20 50 34 0a 2a 2a 0a 2a P1 P2 P3 P4.**.*
1eda8 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 69 73 * Register P3 is
1eda9 20 61 73 73 75 6d 65 64 20 74 6f 20 68 6f 6c 64 assumed to hold
1edaa 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 a 64-bit intege
1edab 72 20 76 61 6c 75 65 2e 20 49 66 20 72 65 67 69 r value. If regi
1edac 73 74 65 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 61 ster P1.** conta
1edad 69 6e 73 20 61 20 52 6f 77 53 65 74 20 6f 62 6a ins a RowSet obj
1edae 65 63 74 20 61 6e 64 20 74 68 61 74 20 52 6f 77 ect and that Row
1edaf 53 65 74 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 Set object conta
1edb0 69 6e 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 ins.** the value
1edb1 20 68 65 6c 64 20 69 6e 20 50 33 2c 20 6a 75 6d held in P3, jum
1edb2 70 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 p to register P2
1edb3 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e 73 . Otherwise, ins
1edb4 65 72 74 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 ert the.** integ
1edb5 65 72 20 69 6e 20 50 33 20 69 6e 74 6f 20 74 68 er in P3 into th
1edb6 65 20 52 6f 77 53 65 74 20 61 6e 64 20 63 6f 6e e RowSet and con
1edb7 74 69 6e 75 65 20 6f 6e 20 74 6f 20 74 68 65 0a tinue on to the.
1edb8 2a 2a 20 6e 65 78 74 20 6f 70 63 6f 64 65 2e 0a ** next opcode..
1edb9 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 **.** The RowSet
1edba 20 6f 62 6a 65 63 74 20 69 73 20 6f 70 74 69 6d object is optim
1edbb 69 7a 65 64 20 66 6f 72 20 74 68 65 20 63 61 73 ized for the cas
1edbc 65 20 77 68 65 72 65 20 73 75 63 63 65 73 73 69 e where successi
1edbd 76 65 20 73 65 74 73 0a 2a 2a 20 6f 66 20 69 6e ve sets.** of in
1edbe 74 65 67 65 72 73 2c 20 77 68 65 72 65 20 65 61 tegers, where ea
1edbf 63 68 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 ch set contains
1edc0 6e 6f 20 64 75 70 6c 69 63 61 74 65 73 2e 20 45 no duplicates. E
1edc1 61 63 68 20 73 65 74 0a 2a 2a 20 6f 66 20 76 61 ach set.** of va
1edc2 6c 75 65 73 20 69 73 20 69 64 65 6e 74 69 66 69 lues is identifi
1edc3 65 64 20 62 79 20 61 20 75 6e 69 71 75 65 20 50 ed by a unique P
1edc4 34 20 76 61 6c 75 65 2e 20 54 68 65 20 66 69 72 4 value. The fir
1edc5 73 74 20 73 65 74 0a 2a 2a 20 6d 75 73 74 20 68 st set.** must h
1edc6 61 76 65 20 50 34 3d 3d 30 2c 20 74 68 65 20 66 ave P4==0, the f
1edc7 69 6e 61 6c 20 73 65 74 20 50 34 3d 2d 31 2e 20 inal set P4=-1.
1edc8 20 50 34 20 6d 75 73 74 20 62 65 20 65 69 74 68 P4 must be eith
1edc9 65 72 20 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e 2d er -1 or.** non-
1edca 6e 65 67 61 74 69 76 65 2e 20 20 46 6f 72 20 6e negative. For n
1edcb 6f 6e 2d 6e 65 67 61 74 69 76 65 20 76 61 6c 75 on-negative valu
1edcc 65 73 20 6f 66 20 50 34 20 6f 6e 6c 79 20 74 68 es of P4 only th
1edcd 65 20 6c 6f 77 65 72 20 34 0a 2a 2a 20 62 69 74 e lower 4.** bit
1edce 73 20 61 72 65 20 73 69 67 6e 69 66 69 63 61 6e s are significan
1edcf 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c t..**.** This al
1edd0 6c 6f 77 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f lows optimizatio
1edd1 6e 73 3a 20 28 61 29 20 77 68 65 6e 20 50 34 3d ns: (a) when P4=
1edd2 3d 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e =0 there is no n
1edd3 65 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 eed to test.** t
1edd4 68 65 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 he rowset object
1edd5 20 66 6f 72 20 50 33 2c 20 61 73 20 69 74 20 69 for P3, as it i
1edd6 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 s guaranteed not
1edd7 20 74 6f 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0a to contain it,.
1edd8 2a 2a 20 28 62 29 20 77 68 65 6e 20 50 34 3d 3d ** (b) when P4==
1edd9 2d 31 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e -1 there is no n
1edda 65 65 64 20 74 6f 20 69 6e 73 65 72 74 20 74 68 eed to insert th
1eddb 65 20 76 61 6c 75 65 2c 20 61 73 20 69 74 20 77 e value, as it w
1eddc 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 ill.** never be
1eddd 74 65 73 74 65 64 20 66 6f 72 2c 20 61 6e 64 20 tested for, and
1edde 28 63 29 20 77 68 65 6e 20 61 20 76 61 6c 75 65 (c) when a value
1eddf 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 that is part of
1ede0 20 73 65 74 20 58 20 69 73 0a 2a 2a 20 69 6e 73 set X is.** ins
1ede1 65 72 74 65 64 2c 20 74 68 65 72 65 20 69 73 20 erted, there is
1ede2 6e 6f 20 6e 65 65 64 20 74 6f 20 73 65 61 72 63 no need to searc
1ede3 68 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 h to see if the
1ede4 73 61 6d 65 20 76 61 6c 75 65 20 77 61 73 0a 2a same value was.*
1ede5 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 * previously ins
1ede6 65 72 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 erted as part of
1ede7 20 73 65 74 20 58 20 28 6f 6e 6c 79 20 69 66 20 set X (only if
1ede8 69 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c it was previousl
1ede9 79 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 61 73 y.** inserted as
1edea 20 70 61 72 74 20 6f 66 20 73 6f 6d 65 20 6f 74 part of some ot
1edeb 68 65 72 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 73 her set)..*/.cas
1edec 65 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 3a e OP_RowSetTest:
1eded 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1edee 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1edef 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 in1, in3 */.#if
1edf0 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1edf1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1edf2 20 75 2e 62 78 20 2a 2f 0a 20 20 69 6e 74 20 69 u.bx */. int i
1edf3 53 65 74 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 Set;. int exist
1edf4 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 s;.#endif /* loc
1edf5 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1edf6 65 64 20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a ed into u.bx */.
1edf7 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b . pIn1 = &aMem[
1edf8 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 pOp->p1];. pIn3
1edf9 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1edfa 5d 3b 0a 20 20 75 2e 62 78 2e 69 53 65 74 20 3d ];. u.bx.iSet =
1edfb 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 pOp->p4.i;. as
1edfc 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 sert( pIn3->flag
1edfd 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 s&MEM_Int );..
1edfe 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 /* If there is a
1edff 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 nything other th
1ee00 61 6e 20 61 20 72 6f 77 73 65 74 20 6f 62 6a 65 an a rowset obje
1ee01 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c ct in memory cel
1ee02 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 l P1,. ** delet
1ee03 65 20 69 74 20 6e 6f 77 20 61 6e 64 20 69 6e 69 e it now and ini
1ee04 74 69 61 6c 69 7a 65 20 50 31 20 77 69 74 68 20 tialize P1 with
1ee05 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 74 0a an empty rowset.
1ee06 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 */. if( (pIn1
1ee07 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
1ee08 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 wSet)==0 ){.
1ee09 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1ee0a 74 52 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 tRowSet(pIn1);.
1ee0b 20 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c if( (pIn1->fl
1ee0c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
1ee0d 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d )==0 ) goto no_m
1ee0e 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 em;. }.. asser
1ee0f 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1ee10 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 P4_INT32 );. as
1ee11 73 65 72 74 28 20 75 2e 62 78 2e 69 53 65 74 3d sert( u.bx.iSet=
1ee12 3d 2d 31 20 7c 7c 20 75 2e 62 78 2e 69 53 65 74 =-1 || u.bx.iSet
1ee13 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 >=0 );. if( u.b
1ee14 78 2e 69 53 65 74 20 29 7b 0a 20 20 20 20 75 2e x.iSet ){. u.
1ee15 62 78 2e 65 78 69 73 74 73 20 3d 20 73 71 6c 69 bx.exists = sqli
1ee16 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 70 49 te3RowSetTest(pI
1ee17 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 0a 20 n1->u.pRowSet,.
1ee18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 75 (u
1ee1a 38 29 28 75 2e 62 78 2e 69 53 65 74 3e 3d 30 20 8)(u.bx.iSet>=0
1ee1b 3f 20 75 2e 62 78 2e 69 53 65 74 20 26 20 30 78 ? u.bx.iSet & 0x
1ee1c 66 20 3a 20 30 78 66 66 29 2c 0a 20 20 20 20 20 f : 0xff),.
1ee1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ee1e 20 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e pIn3->
1ee1f 75 2e 69 29 3b 0a 20 20 20 20 69 66 28 20 75 2e u.i);. if( u.
1ee20 62 78 2e 65 78 69 73 74 73 20 29 7b 0a 20 20 20 bx.exists ){.
1ee21 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1ee22 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b - 1;. break
1ee23 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
1ee24 28 20 75 2e 62 78 2e 69 53 65 74 3e 3d 30 20 29 ( u.bx.iSet>=0 )
1ee25 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 {. sqlite3Row
1ee26 53 65 74 49 6e 73 65 72 74 28 70 49 6e 31 2d 3e SetInsert(pIn1->
1ee27 75 2e 70 52 6f 77 53 65 74 2c 20 70 49 6e 33 2d u.pRowSet, pIn3-
1ee28 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20 20 62 72 65 >u.i);. }. bre
1ee29 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 ak;.}...#ifndef
1ee2a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1ee2b 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 GER../* Opcode:
1ee2c 50 72 6f 67 72 61 6d 20 50 31 20 50 32 20 50 33 Program P1 P2 P3
1ee2d 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 P4 *.**.** Exec
1ee2e 75 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20 ute the trigger
1ee2f 70 72 6f 67 72 61 6d 20 70 61 73 73 65 64 20 61 program passed a
1ee30 73 20 50 34 20 28 74 79 70 65 20 50 34 5f 53 55 s P4 (type P4_SU
1ee31 42 50 52 4f 47 52 41 4d 29 2e 20 0a 2a 2a 0a 2a BPROGRAM). .**.*
1ee32 2a 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 74 68 * P1 contains th
1ee33 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 e address of the
1ee34 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 memory cell tha
1ee35 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 t contains the f
1ee36 69 72 73 74 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 irst memory .**
1ee37 63 65 6c 6c 20 69 6e 20 61 6e 20 61 72 72 61 79 cell in an array
1ee38 20 6f 66 20 76 61 6c 75 65 73 20 75 73 65 64 20 of values used
1ee39 61 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 as arguments to
1ee3a 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e the sub-program.
1ee3b 20 50 32 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 P2 .** contains
1ee3c 20 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 the address to
1ee3d 6a 75 6d 70 20 74 6f 20 69 66 20 74 68 65 20 73 jump to if the s
1ee3e 75 62 2d 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 ub-program throw
1ee3f 73 20 61 6e 20 49 47 4e 4f 52 45 20 0a 2a 2a 20 s an IGNORE .**
1ee40 65 78 63 65 70 74 69 6f 6e 20 75 73 69 6e 67 20 exception using
1ee41 74 68 65 20 52 41 49 53 45 28 29 20 66 75 6e 63 the RAISE() func
1ee42 74 69 6f 6e 2e 20 52 65 67 69 73 74 65 72 20 50 tion. Register P
1ee43 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 3 contains the a
1ee44 64 64 72 65 73 73 20 0a 2a 2a 20 6f 66 20 61 20 ddress .** of a
1ee45 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 memory cell in t
1ee46 68 69 73 20 28 74 68 65 20 70 61 72 65 6e 74 29 his (the parent)
1ee47 20 56 4d 20 74 68 61 74 20 69 73 20 75 73 65 64 VM that is used
1ee48 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 to allocate the
1ee49 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 .** memory requ
1ee4a 69 72 65 64 20 62 79 20 74 68 65 20 73 75 62 2d ired by the sub-
1ee4b 76 64 62 65 20 61 74 20 72 75 6e 74 69 6d 65 2e vdbe at runtime.
1ee4c 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 .**.** P4 is a p
1ee4d 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 4d ointer to the VM
1ee4e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1ee4f 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
1ee50 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 6f 67 .*/.case OP_Prog
1ee51 72 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a ram: { /*
1ee52 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 jump */.#if 0
1ee53 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ee54 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ee55 62 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d by */. int nMem
1ee56 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1ee57 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d /* Number of mem
1ee58 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 66 6f ory registers fo
1ee59 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
1ee5a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 . int nByte;
1ee5b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1ee5c 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 tes of runtime s
1ee5d 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f pace required fo
1ee5e 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
1ee5f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 . Mem *pRt;
1ee60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1ee61 67 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 gister to alloca
1ee62 74 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 te runtime space
1ee63 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b */. Mem *pMem;
1ee64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ee65 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 Used to iterate
1ee66 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 through memory
1ee67 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a cells */. Mem *
1ee68 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 pEnd;
1ee69 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 /* Last memor
1ee6a 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 y cell in new ar
1ee6b 72 61 79 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 ray */. VdbeFra
1ee6c 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 me *pFrame;
1ee6d 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61 /* New vdbe fra
1ee6e 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e me to execute in
1ee6f 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d */. SubProgram
1ee70 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a *pProgram; /*
1ee71 20 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 Sub-program to
1ee72 65 78 65 63 75 74 65 20 2a 2f 0a 20 20 76 6f 69 execute */. voi
1ee73 64 20 2a 74 3b 20 20 20 20 20 20 20 20 20 20 20 d *t;
1ee74 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 /* Token id
1ee75 65 6e 74 69 66 79 69 6e 67 20 74 72 69 67 67 65 entifying trigge
1ee76 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c r */.#endif /* l
1ee77 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ee78 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a oved into u.by *
1ee79 2f 0a 0a 20 20 75 2e 62 79 2e 70 50 72 6f 67 72 /.. u.by.pProgr
1ee7a 61 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 am = pOp->p4.pPr
1ee7b 6f 67 72 61 6d 3b 0a 20 20 75 2e 62 79 2e 70 52 ogram;. u.by.pR
1ee7c 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 t = &aMem[pOp->p
1ee7d 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 3];. assert( u.
1ee7e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 by.pProgram->nOp
1ee7f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 >0 );.. /* If t
1ee80 68 65 20 70 35 20 66 6c 61 67 20 69 73 20 63 6c he p5 flag is cl
1ee81 65 61 72 2c 20 74 68 65 6e 20 72 65 63 75 72 73 ear, then recurs
1ee82 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ive invocation o
1ee83 66 20 74 72 69 67 67 65 72 73 20 69 73 0a 20 20 f triggers is.
1ee84 2a 2a 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 ** disabled for
1ee85 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 backwards compat
1ee86 69 62 69 6c 69 74 79 20 28 70 35 20 69 73 20 73 ibility (p5 is s
1ee87 65 74 20 69 66 20 74 68 69 73 20 73 75 62 2d 70 et if this sub-p
1ee88 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 69 73 20 72 rogram. ** is r
1ee89 65 61 6c 6c 79 20 61 20 74 72 69 67 67 65 72 2c eally a trigger,
1ee8a 20 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e 20 6b not a foreign k
1ee8b 65 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64 20 74 ey action, and t
1ee8c 68 65 20 66 6c 61 67 20 73 65 74 0a 20 20 2a 2a he flag set. **
1ee8d 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 and cleared by
1ee8e 74 68 65 20 22 50 52 41 47 4d 41 20 72 65 63 75 the "PRAGMA recu
1ee8f 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 22 20 rsive_triggers"
1ee90 63 6f 6d 6d 61 6e 64 20 69 73 20 63 6c 65 61 72 command is clear
1ee91 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 ).. **. ** It
1ee92 69 73 20 72 65 63 75 72 73 69 76 65 20 69 6e 76 is recursive inv
1ee93 6f 63 61 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 ocation of trigg
1ee94 65 72 73 2c 20 61 74 20 74 68 65 20 53 51 4c 20 ers, at the SQL
1ee95 6c 65 76 65 6c 2c 20 74 68 61 74 20 69 73 0a 20 level, that is.
1ee96 20 2a 2a 20 64 69 73 61 62 6c 65 64 2e 20 49 6e ** disabled. In
1ee97 20 73 6f 6d 65 20 63 61 73 65 73 20 61 20 73 69 some cases a si
1ee98 6e 67 6c 65 20 74 72 69 67 67 65 72 20 6d 61 79 ngle trigger may
1ee99 20 67 65 6e 65 72 61 74 65 20 6d 6f 72 65 20 74 generate more t
1ee9a 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a 20 53 75 62 han one. ** Sub
1ee9b 50 72 6f 67 72 61 6d 20 28 69 66 20 74 68 65 20 Program (if the
1ee9c 74 72 69 67 67 65 72 20 6d 61 79 20 62 65 20 65 trigger may be e
1ee9d 78 65 63 75 74 65 64 20 77 69 74 68 20 6d 6f 72 xecuted with mor
1ee9e 65 20 74 68 61 6e 20 6f 6e 65 20 64 69 66 66 65 e than one diffe
1ee9f 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e 20 43 4f 4e rent. ** ON CON
1eea0 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 29 FLICT algorithm)
1eea1 2e 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 . SubProgram str
1eea2 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 uctures associat
1eea3 65 64 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 73 ed with a. ** s
1eea4 69 6e 67 6c 65 20 74 72 69 67 67 65 72 20 61 6c ingle trigger al
1eea5 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 l have the same
1eea6 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 53 75 value for the Su
1eea7 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 0a 20 bProgram.token.
1eea8 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 20 20 2a ** variable. *
1eea9 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 /. if( pOp->p5
1eeaa 29 7b 0a 20 20 20 20 75 2e 62 79 2e 74 20 3d 20 ){. u.by.t =
1eeab 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 u.by.pProgram->t
1eeac 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 75 2e oken;. for(u.
1eead 62 79 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 by.pFrame=p->pFr
1eeae 61 6d 65 3b 20 75 2e 62 79 2e 70 46 72 61 6d 65 ame; u.by.pFrame
1eeaf 20 26 26 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d && u.by.pFrame-
1eeb0 3e 74 6f 6b 65 6e 21 3d 75 2e 62 79 2e 74 3b 20 >token!=u.by.t;
1eeb1 75 2e 62 79 2e 70 46 72 61 6d 65 3d 75 2e 62 79 u.by.pFrame=u.by
1eeb2 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 .pFrame->pParent
1eeb3 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 79 2e );. if( u.by.
1eeb4 70 46 72 61 6d 65 20 29 20 62 72 65 61 6b 3b 0a pFrame ) break;.
1eeb5 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 6e 46 }.. if( p->nF
1eeb6 72 61 6d 65 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 rame>=db->aLimit
1eeb7 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 [SQLITE_LIMIT_TR
1eeb8 49 47 47 45 52 5f 44 45 50 54 48 5d 20 29 7b 0a IGGER_DEPTH] ){.
1eeb9 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1eeba 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 ERROR;. sqlit
1eebb 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1eebc 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 74 6f zErrMsg, db, "to
1eebd 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 o many levels of
1eebe 20 74 72 69 67 67 65 72 20 72 65 63 75 72 73 69 trigger recursi
1eebf 6f 6e 22 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b on");. break;
1eec0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73 . }.. /* Regis
1eec1 74 65 72 20 75 2e 62 79 2e 70 52 74 20 69 73 20 ter u.by.pRt is
1eec2 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
1eec3 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 e memory require
1eec4 64 20 74 6f 20 73 61 76 65 20 74 68 65 20 73 74 d to save the st
1eec5 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ate. ** of the
1eec6 63 75 72 72 65 6e 74 20 70 72 6f 67 72 61 6d 2c current program,
1eec7 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 and the memory
1eec8 72 65 71 75 69 72 65 64 20 61 74 20 72 75 6e 74 required at runt
1eec9 69 6d 65 20 74 6f 20 65 78 65 63 75 74 65 0a 20 ime to execute.
1eeca 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 ** the trigger
1eecb 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 69 73 program. If this
1eecc 20 74 72 69 67 67 65 72 20 68 61 73 20 62 65 65 trigger has bee
1eecd 6e 20 66 69 72 65 64 20 62 65 66 6f 72 65 2c 20 n fired before,
1eece 74 68 65 6e 20 75 2e 62 79 2e 70 52 74 0a 20 20 then u.by.pRt.
1eecf 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 61 6c ** is already al
1eed0 6c 6f 63 61 74 65 64 2e 20 4f 74 68 65 72 77 69 located. Otherwi
1eed1 73 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 69 se, it must be i
1eed2 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a nitialized. */.
1eed3 20 20 69 66 28 20 28 75 2e 62 79 2e 70 52 74 2d if( (u.by.pRt-
1eed4 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 >flags&MEM_Frame
1eed5 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 )==0 ){. /* S
1eed6 75 62 50 72 6f 67 72 61 6d 2e 6e 4d 65 6d 20 69 ubProgram.nMem i
1eed7 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d s set to the num
1eed8 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 ber of memory ce
1eed9 6c 6c 73 20 75 73 65 64 20 62 79 20 74 68 65 0a lls used by the.
1eeda 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 73 ** program s
1eedb 74 6f 72 65 64 20 69 6e 20 53 75 62 50 72 6f 67 tored in SubProg
1eedc 72 61 6d 2e 61 4f 70 2e 20 41 73 20 77 65 6c 6c ram.aOp. As well
1eedd 20 61 73 20 74 68 65 73 65 2c 20 6f 6e 65 20 6d as these, one m
1eede 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 63 65 6c emory. ** cel
1eedf 6c 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f l is required fo
1eee0 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 75 73 r each cursor us
1eee1 65 64 20 62 79 20 74 68 65 20 70 72 6f 67 72 61 ed by the progra
1eee2 6d 2e 20 53 65 74 20 6c 6f 63 61 6c 0a 20 20 20 m. Set local.
1eee3 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 75 2e 62 ** variable u.b
1eee4 79 2e 6e 4d 65 6d 20 28 61 6e 64 20 6c 61 74 65 y.nMem (and late
1eee5 72 2c 20 56 64 62 65 46 72 61 6d 65 2e 6e 43 68 r, VdbeFrame.nCh
1eee6 69 6c 64 4d 65 6d 29 20 74 6f 20 74 68 69 73 20 ildMem) to this
1eee7 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 value.. */.
1eee8 20 20 75 2e 62 79 2e 6e 4d 65 6d 20 3d 20 75 2e u.by.nMem = u.
1eee9 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 by.pProgram->nMe
1eeea 6d 20 2b 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 m + u.by.pProgra
1eeeb 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e 62 m->nCsr;. u.b
1eeec 79 2e 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 y.nByte = ROUND8
1eeed 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 6d (sizeof(VdbeFram
1eeee 65 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 e)).
1eeef 20 20 2b 20 75 2e 62 79 2e 6e 4d 65 6d 20 2a 20 + u.by.nMem *
1eef0 73 69 7a 65 6f 66 28 4d 65 6d 29 0a 20 20 20 20 sizeof(Mem).
1eef1 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e 62 79 + u.by
1eef2 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 .pProgram->nCsr
1eef3 2a 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 * sizeof(VdbeCur
1eef4 73 6f 72 20 2a 29 3b 0a 20 20 20 20 75 2e 62 79 sor *);. u.by
1eef5 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c 69 74 65 .pFrame = sqlite
1eef6 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 3DbMallocZero(db
1eef7 2c 20 75 2e 62 79 2e 6e 42 79 74 65 29 3b 0a 20 , u.by.nByte);.
1eef8 20 20 20 69 66 28 20 21 75 2e 62 79 2e 70 46 72 if( !u.by.pFr
1eef9 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ame ){. got
1eefa 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a o no_mem;. }.
1eefb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1eefc 65 6d 52 65 6c 65 61 73 65 28 75 2e 62 79 2e 70 emRelease(u.by.p
1eefd 52 74 29 3b 0a 20 20 20 20 75 2e 62 79 2e 70 52 Rt);. u.by.pR
1eefe 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 46 t->flags = MEM_F
1eeff 72 61 6d 65 3b 0a 20 20 20 20 75 2e 62 79 2e 70 rame;. u.by.p
1ef00 52 74 2d 3e 75 2e 70 46 72 61 6d 65 20 3d 20 75 Rt->u.pFrame = u
1ef01 2e 62 79 2e 70 46 72 61 6d 65 3b 0a 0a 20 20 20 .by.pFrame;..
1ef02 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 76 20 u.by.pFrame->v
1ef03 3d 20 70 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 = p;. u.by.pF
1ef04 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 rame->nChildMem
1ef05 3d 20 75 2e 62 79 2e 6e 4d 65 6d 3b 0a 20 20 20 = u.by.nMem;.
1ef06 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 u.by.pFrame->nC
1ef07 68 69 6c 64 43 73 72 20 3d 20 75 2e 62 79 2e 70 hildCsr = u.by.p
1ef08 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 Program->nCsr;.
1ef09 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e u.by.pFrame->
1ef0a 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 75 2e 62 pc = pc;. u.b
1ef0b 79 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 20 3d y.pFrame->aMem =
1ef0c 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 20 20 75 2e p->aMem;. u.
1ef0d 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 by.pFrame->nMem
1ef0e 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 75 = p->nMem;. u
1ef0f 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 61 70 43 73 .by.pFrame->apCs
1ef10 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0a 20 20 r = p->apCsr;.
1ef11 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e u.by.pFrame->n
1ef12 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43 75 72 Cursor = p->nCur
1ef13 73 6f 72 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 sor;. u.by.pF
1ef14 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70 2d 3e 61 rame->aOp = p->a
1ef15 4f 70 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 Op;. u.by.pFr
1ef16 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 2d 3e 6e 4f ame->nOp = p->nO
1ef17 70 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 p;. u.by.pFra
1ef18 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 75 2e 62 79 me->token = u.by
1ef19 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e .pProgram->token
1ef1a 3b 0a 0a 20 20 20 20 75 2e 62 79 2e 70 45 6e 64 ;.. u.by.pEnd
1ef1b 20 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d = &VdbeFrameMem
1ef1c 28 75 2e 62 79 2e 70 46 72 61 6d 65 29 5b 75 2e (u.by.pFrame)[u.
1ef1d 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c by.pFrame->nChil
1ef1e 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 6f 72 28 75 dMem];. for(u
1ef1f 2e 62 79 2e 70 4d 65 6d 3d 56 64 62 65 46 72 61 .by.pMem=VdbeFra
1ef20 6d 65 4d 65 6d 28 75 2e 62 79 2e 70 46 72 61 6d meMem(u.by.pFram
1ef21 65 29 3b 20 75 2e 62 79 2e 70 4d 65 6d 21 3d 75 e); u.by.pMem!=u
1ef22 2e 62 79 2e 70 45 6e 64 3b 20 75 2e 62 79 2e 70 .by.pEnd; u.by.p
1ef23 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e Mem++){. u.
1ef24 62 79 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d by.pMem->flags =
1ef25 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
1ef26 20 75 2e 62 79 2e 70 4d 65 6d 2d 3e 64 62 20 3d u.by.pMem->db =
1ef27 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c db;. }. }el
1ef28 73 65 7b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 se{. u.by.pFr
1ef29 61 6d 65 20 3d 20 75 2e 62 79 2e 70 52 74 2d 3e ame = u.by.pRt->
1ef2a 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20 61 73 u.pFrame;. as
1ef2b 73 65 72 74 28 20 75 2e 62 79 2e 70 50 72 6f 67 sert( u.by.pProg
1ef2c 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 62 79 2e 70 ram->nMem+u.by.p
1ef2d 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 Program->nCsr==u
1ef2e 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 .by.pFrame->nChi
1ef2f 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61 73 73 ldMem );. ass
1ef30 65 72 74 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 ert( u.by.pProgr
1ef31 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 79 2e 70 am->nCsr==u.by.p
1ef32 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 Frame->nChildCsr
1ef33 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1ef34 70 63 3d 3d 75 2e 62 79 2e 70 46 72 61 6d 65 2d pc==u.by.pFrame-
1ef35 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 70 2d >pc );. }.. p-
1ef36 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e 62 >nFrame++;. u.b
1ef37 79 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e y.pFrame->pParen
1ef38 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 t = p->pFrame;.
1ef39 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6c 61 u.by.pFrame->la
1ef3a 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61 stRowid = db->la
1ef3b 73 74 52 6f 77 69 64 3b 0a 20 20 75 2e 62 79 2e stRowid;. u.by.
1ef3c 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 20 pFrame->nChange
1ef3d 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 = p->nChange;.
1ef3e 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a p->nChange = 0;.
1ef3f 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 75 2e p->pFrame = u.
1ef40 62 79 2e 70 46 72 61 6d 65 3b 0a 20 20 70 2d 3e by.pFrame;. p->
1ef41 61 4d 65 6d 20 3d 20 61 4d 65 6d 20 3d 20 26 56 aMem = aMem = &V
1ef42 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 79 dbeFrameMem(u.by
1ef43 2e 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 .pFrame)[-1];.
1ef44 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 62 79 2e 70 p->nMem = u.by.p
1ef45 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d Frame->nChildMem
1ef46 3b 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d ;. p->nCursor =
1ef47 20 28 75 31 36 29 75 2e 62 79 2e 70 46 72 61 6d (u16)u.by.pFram
1ef48 65 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 e->nChildCsr;.
1ef49 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 62 65 p->apCsr = (Vdbe
1ef4a 43 75 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b Cursor **)&aMem[
1ef4b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d p->nMem+1];. p-
1ef4c 3e 61 4f 70 20 3d 20 61 4f 70 20 3d 20 75 2e 62 >aOp = aOp = u.b
1ef4d 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b y.pProgram->aOp;
1ef4e 0a 20 20 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 79 . p->nOp = u.by
1ef4f 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a .pProgram->nOp;.
1ef50 20 20 70 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 pc = -1;.. br
1ef51 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ef52 65 3a 20 50 61 72 61 6d 20 50 31 20 50 32 20 2a e: Param P1 P2 *
1ef53 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 * *.**.** This
1ef54 6f 70 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 opcode is only e
1ef55 76 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 73 ver present in s
1ef56 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c ub-programs call
1ef57 65 64 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 4f ed via the .** O
1ef58 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 P_Program instru
1ef59 63 74 69 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61 ction. Copy a va
1ef5a 6c 75 65 20 63 75 72 72 65 6e 74 6c 79 20 73 74 lue currently st
1ef5b 6f 72 65 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79 ored in a memory
1ef5c 20 0a 2a 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65 .** cell of the
1ef5d 20 63 61 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74 calling (parent
1ef5e 29 20 66 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20 ) frame to cell
1ef5f 50 32 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e P2 in the curren
1ef60 74 20 66 72 61 6d 65 73 20 0a 2a 2a 20 61 64 64 t frames .** add
1ef61 72 65 73 73 20 73 70 61 63 65 2e 20 54 68 69 73 ress space. This
1ef62 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67 is used by trig
1ef63 67 65 72 20 70 72 6f 67 72 61 6d 73 20 74 6f 20 ger programs to
1ef64 61 63 63 65 73 73 20 74 68 65 20 6e 65 77 2e 2a access the new.*
1ef65 20 0a 2a 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 .** and old.* v
1ef66 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 alues..**.** The
1ef67 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 address of the
1ef68 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65 cell in the pare
1ef69 6e 74 20 66 72 61 6d 65 20 69 73 20 64 65 74 65 nt frame is dete
1ef6a 72 6d 69 6e 65 64 20 62 79 20 61 64 64 69 6e 67 rmined by adding
1ef6b 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 .** the value of
1ef6c 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e 74 the P1 argument
1ef6d 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
1ef6e 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e 74 the P1 argument
1ef6f 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 to the.** calli
1ef70 6e 67 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e ng OP_Program in
1ef71 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 struction..*/.ca
1ef72 73 65 20 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 20 se OP_Param: {
1ef73 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1ef74 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 -prerelease */.#
1ef75 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ef76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ef77 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 56 64 nto u.bz */. Vd
1ef78 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1ef79 0a 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e . Mem *pIn;.#en
1ef7a 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1ef7b 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ef7c 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 75 2e 62 7a o u.bz */. u.bz
1ef7d 2e 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 .pFrame = p->pFr
1ef7e 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e 70 49 6e 20 ame;. u.bz.pIn
1ef7f 3d 20 26 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e = &u.bz.pFrame->
1ef80 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 aMem[pOp->p1 + u
1ef81 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b .bz.pFrame->aOp[
1ef82 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 5d u.bz.pFrame->pc]
1ef83 2e 70 31 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 .p1];. sqlite3V
1ef84 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
1ef85 79 28 70 4f 75 74 2c 20 75 2e 62 7a 2e 70 49 6e y(pOut, u.bz.pIn
1ef86 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 , MEM_Ephem);.
1ef87 62 72 65 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 break;.}..#endif
1ef88 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 /* #ifndef SQLI
1ef89 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 TE_OMIT_TRIGGER
1ef8a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
1ef8b 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
1ef8c 4b 45 59 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 KEY./* Opcode: F
1ef8d 6b 43 6f 75 6e 74 65 72 20 50 31 20 50 32 20 2a kCounter P1 P2 *
1ef8e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 * *.**.** Incre
1ef8f 6d 65 6e 74 20 61 20 22 63 6f 6e 73 74 72 61 69 ment a "constrai
1ef90 6e 74 20 63 6f 75 6e 74 65 72 22 20 62 79 20 50 nt counter" by P
1ef91 32 20 28 50 32 20 6d 61 79 20 62 65 20 6e 65 67 2 (P2 may be neg
1ef92 61 74 69 76 65 20 6f 72 20 70 6f 73 69 74 69 76 ative or positiv
1ef93 65 29 2e 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 e)..** If P1 is
1ef94 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 64 61 non-zero, the da
1ef95 74 61 62 61 73 65 20 63 6f 6e 73 74 72 61 69 6e tabase constrain
1ef96 74 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 t counter is inc
1ef97 72 65 6d 65 6e 74 65 64 20 0a 2a 2a 20 28 64 65 remented .** (de
1ef98 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b ferred foreign k
1ef99 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e ey constraints).
1ef9a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 Otherwise, if P
1ef9b 31 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 0a 1 is zero, the .
1ef9c 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 ** statement cou
1ef9d 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e nter is incremen
1ef9e 74 65 64 20 28 69 6d 6d 65 64 69 61 74 65 20 66 ted (immediate f
1ef9f 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 oreign key const
1efa0 72 61 69 6e 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 raints)..*/.case
1efa1 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b OP_FkCounter: {
1efa2 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 . if( pOp->p1 )
1efa3 7b 0a 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 {. db->nDefer
1efa4 72 65 64 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e redCons += pOp->
1efa5 70 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 p2;. }else{.
1efa6 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e p->nFkConstrain
1efa7 74 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 t += pOp->p2;.
1efa8 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1efa9 20 4f 70 63 6f 64 65 3a 20 46 6b 49 66 5a 65 72 Opcode: FkIfZer
1efaa 6f 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a o P1 P2 * * *.**
1efab 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1efac 74 65 73 74 73 20 69 66 20 61 20 66 6f 72 65 69 tests if a forei
1efad 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1efae 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 63 75 72 t-counter is cur
1efaf 72 65 6e 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 rently zero..**
1efb0 49 66 20 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 If so, jump to i
1efb1 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f nstruction P2. O
1efb2 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 therwise, fall t
1efb3 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1efb4 78 74 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 xt .** instructi
1efb5 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 on..**.** If P1
1efb6 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1efb7 6e 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 n the jump is ta
1efb8 6b 65 6e 20 69 66 20 74 68 65 20 64 61 74 61 62 ken if the datab
1efb9 61 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 ase constraint-c
1efba 6f 75 6e 74 65 72 0a 2a 2a 20 69 73 20 7a 65 72 ounter.** is zer
1efbb 6f 20 28 74 68 65 20 6f 6e 65 20 74 68 61 74 20 o (the one that
1efbc 63 6f 75 6e 74 73 20 64 65 66 65 72 72 65 64 20 counts deferred
1efbd 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
1efbe 74 69 6f 6e 73 29 2e 20 49 66 20 50 31 20 69 73 tions). If P1 is
1efbf 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75 .** zero, the ju
1efc0 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 20 74 mp is taken if t
1efc1 68 65 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e he statement con
1efc2 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 straint-counter
1efc3 69 73 20 7a 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 is zero.** (imme
1efc4 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 diate foreign ke
1efc5 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f y constraint vio
1efc6 6c 61 74 69 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 lations)..*/.cas
1efc7 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b e OP_FkIfZero: {
1efc8 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
1efc9 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 */. if( pOp->p
1efca 31 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 1 ){. if( db-
1efcb 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d >nDeferredCons==
1efcc 30 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 0 ) pc = pOp->p2
1efcd 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 -1;. }else{.
1efce 20 69 66 28 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 if( p->nFkConst
1efcf 72 61 69 6e 74 3d 3d 30 20 29 20 70 63 20 3d 20 raint==0 ) pc =
1efd0 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 pOp->p2-1;. }.
1efd1 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1efd2 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 /* #ifndef SQLI
1efd3 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
1efd4 4b 45 59 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 KEY */..#ifndef
1efd5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1efd6 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 INCREMENT./* Opc
1efd7 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 ode: MemMax P1 P
1efd8 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 2 * * *.**.** P1
1efd9 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20 69 is a register i
1efda 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 n the root frame
1efdb 20 6f 66 20 74 68 69 73 20 56 4d 20 28 74 68 65 of this VM (the
1efdc 20 72 6f 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a root frame is.*
1efdd 2a 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d * different from
1efde 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 the current fra
1efdf 6d 65 20 69 66 20 74 68 69 73 20 69 6e 73 74 72 me if this instr
1efe0 75 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 uction is being
1efe1 65 78 65 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 executed.** with
1efe2 69 6e 20 61 20 73 75 62 2d 70 72 6f 67 72 61 6d in a sub-program
1efe3 29 2e 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 ). Set the value
1efe4 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
1efe5 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f to the maximum o
1efe6 66 20 0a 2a 2a 20 69 74 73 20 63 75 72 72 65 6e f .** its curren
1efe7 74 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 t value and the
1efe8 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1efe9 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 r P2..**.** This
1efea 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 72 instruction thr
1efeb 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 ows an error if
1efec 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
1efed 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 is not initially
1efee 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a .** an integer..
1efef 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 */.case OP_MemMa
1eff0 78 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 x: { /* i
1eff1 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 n2 */.#if 0 /*
1eff2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1eff3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 moved into u.ca
1eff4 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a */. Mem *pIn1;.
1eff5 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 VdbeFrame *pFr
1eff6 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c ame;.#endif /* l
1eff7 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1eff8 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a oved into u.ca *
1eff9 2f 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d /. if( p->pFram
1effa 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 e ){. for(u.c
1effb 61 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 a.pFrame=p->pFra
1effc 6d 65 3b 20 75 2e 63 61 2e 70 46 72 61 6d 65 2d me; u.ca.pFrame-
1effd 3e 70 50 61 72 65 6e 74 3b 20 75 2e 63 61 2e 70 >pParent; u.ca.p
1effe 46 72 61 6d 65 3d 75 2e 63 61 2e 70 46 72 61 6d Frame=u.ca.pFram
1efff 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 e->pParent);.
1f000 20 75 2e 63 61 2e 70 49 6e 31 20 3d 20 26 75 2e u.ca.pIn1 = &u.
1f001 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b ca.pFrame->aMem[
1f002 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 pOp->p1];. }els
1f003 65 7b 0a 20 20 20 20 75 2e 63 61 2e 70 49 6e 31 e{. u.ca.pIn1
1f004 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 = &aMem[pOp->p1
1f005 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 ];. }. sqlite3
1f006 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
1f007 79 28 75 2e 63 61 2e 70 49 6e 31 29 3b 0a 20 20 y(u.ca.pIn1);.
1f008 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn2 = &aMem[pOp
1f009 2d 3e 70 32 5d 3b 0a 20 20 73 71 6c 69 74 65 33 ->p2];. sqlite3
1f00a 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
1f00b 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 20 75 y(pIn2);. if( u
1f00c 2e 63 61 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 .ca.pIn1->u.i<pI
1f00d 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e n2->u.i){. u.
1f00e 63 61 2e 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 ca.pIn1->u.i = p
1f00f 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 In2->u.i;. }.
1f010 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1f011 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 /* SQLITE_OMIT_A
1f012 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a UTOINCREMENT */.
1f013 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f ./* Opcode: IfPo
1f014 73 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a s P1 P2 * * *.**
1f015 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1f016 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 of register P1
1f017 69 73 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c is 1 or greater,
1f018 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a jump to P2..**.
1f019 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c ** It is illegal
1f01a 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 to use this ins
1f01b 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 truction on a re
1f01c 67 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 gister that does
1f01d 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 .** not contain
1f01e 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 an integer. An
1f01f 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1f020 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 will result if y
1f021 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 ou try..*/.case
1f022 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 OP_IfPos: {
1f023 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 /* jump, in1
1f024 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1f025 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1f026 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1f027 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 s&MEM_Int );. i
1f028 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 f( pIn1->u.i>0 )
1f029 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d {. pc = pOp-
1f02a 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1f02b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1f02c 64 65 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 de: IfNeg P1 P2
1f02d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 * * *.**.** If t
1f02e 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 he value of regi
1f02f 73 74 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 ster P1 is less
1f030 74 68 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 than zero, jump
1f031 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 to P2. .**.** It
1f032 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 is illegal to u
1f033 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 se this instruct
1f034 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 ion on a registe
1f035 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e r that does.** n
1f036 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e ot contain an in
1f037 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 teger. An asser
1f038 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 tion fault will
1f039 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 result if you tr
1f03a 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 y..*/.case OP_If
1f03b 4e 65 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a Neg: { /*
1f03c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 jump, in1 */.
1f03d 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1f03e 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1f03f 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d pIn1->flags&MEM
1f040 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 _Int );. if( pI
1f041 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 n1->u.i<0 ){.
1f042 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1f043 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1f044 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 .}../* Opcode: I
1f045 66 5a 65 72 6f 20 50 31 20 50 32 20 50 33 20 2a fZero P1 P2 P3 *
1f046 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 *.**.** The reg
1f047 69 73 74 65 72 20 50 31 20 6d 75 73 74 20 63 6f ister P1 must co
1f048 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 ntain an integer
1f049 2e 20 20 41 64 64 20 6c 69 74 65 72 61 6c 20 50 . Add literal P
1f04a 33 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 3 to the.** valu
1f04b 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1f04c 2e 20 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 . If the result
1f04d 20 69 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a is exactly 0, j
1f04e 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a ump to P2. .**.*
1f04f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 * It is illegal
1f050 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 to use this inst
1f051 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 ruction on a reg
1f052 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a ister that does.
1f053 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 ** not contain a
1f054 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 n integer. An a
1f055 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 ssertion fault w
1f056 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f ill result if yo
1f057 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f u try..*/.case O
1f058 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 P_IfZero: {
1f059 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 /* jump, in1
1f05a 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1f05b 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 m[pOp->p1];. as
1f05c 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1f05d 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 s&MEM_Int );. p
1f05e 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d In1->u.i += pOp-
1f05f 3e 70 33 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d >p3;. if( pIn1-
1f060 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 >u.i==0 ){.
1f061 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1f062 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1f063 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 ../* Opcode: Agg
1f064 53 74 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 Step * P2 P3 P4
1f065 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 P5.**.** Execute
1f066 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 the step functi
1f067 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 on for an aggreg
1f068 61 74 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e ate. The.** fun
1f069 63 74 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 ction has P5 arg
1f06a 75 6d 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 uments. P4 is
1f06b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1f06c 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 FuncDef.** stru
1f06d 63 74 75 72 65 20 74 68 61 74 20 73 70 65 63 69 cture that speci
1f06e 66 69 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f fies the functio
1f06f 6e 2e 20 20 55 73 65 20 72 65 67 69 73 74 65 72 n. Use register
1f070 0a 2a 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 .** P3 as the ac
1f071 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a cumulator..**.**
1f072 20 54 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 The P5 argument
1f073 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d s are taken from
1f074 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 register P2 and
1f075 20 69 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f its.** successo
1f076 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 rs..*/.case OP_A
1f077 67 67 53 74 65 70 3a 20 7b 0a 23 69 66 20 30 20 ggStep: {.#if 0
1f078 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f079 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f07a 2e 63 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a .cb */. int n;.
1f07b 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a int i;. Mem *
1f07c 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 pMem;. Mem *pRe
1f07d 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c;. sqlite3_con
1f07e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 text ctx;. sqli
1f07f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 te3_value **apVa
1f080 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 l;.#endif /* loc
1f081 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f082 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a ed into u.cb */.
1f083 0a 20 20 75 2e 63 62 2e 6e 20 3d 20 70 4f 70 2d . u.cb.n = pOp-
1f084 3e 70 35 3b 0a 20 20 61 73 73 65 72 74 28 20 75 >p5;. assert( u
1f085 2e 63 62 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e .cb.n>=0 );. u.
1f086 63 62 2e 70 52 65 63 20 3d 20 26 61 4d 65 6d 5b cb.pRec = &aMem[
1f087 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 62 pOp->p2];. u.cb
1f088 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 .apVal = p->apAr
1f089 67 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 g;. assert( u.c
1f08a 62 2e 61 70 56 61 6c 20 7c 7c 20 75 2e 63 62 2e b.apVal || u.cb.
1f08b 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e n==0 );. for(u.
1f08c 63 62 2e 69 3d 30 3b 20 75 2e 63 62 2e 69 3c 75 cb.i=0; u.cb.i<u
1f08d 2e 63 62 2e 6e 3b 20 75 2e 63 62 2e 69 2b 2b 2c .cb.n; u.cb.i++,
1f08e 20 75 2e 63 62 2e 70 52 65 63 2b 2b 29 7b 0a 20 u.cb.pRec++){.
1f08f 20 20 20 75 2e 63 62 2e 61 70 56 61 6c 5b 75 2e u.cb.apVal[u.
1f090 63 62 2e 69 5d 20 3d 20 75 2e 63 62 2e 70 52 65 cb.i] = u.cb.pRe
1f091 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 c;. sqlite3Vd
1f092 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 beMemStoreType(u
1f093 2e 63 62 2e 70 52 65 63 29 3b 0a 20 20 7d 0a 20 .cb.pRec);. }.
1f094 20 75 2e 63 62 2e 63 74 78 2e 70 46 75 6e 63 20 u.cb.ctx.pFunc
1f095 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
1f096 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1f097 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1f098 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e =p->nMem );. u.
1f099 63 62 2e 63 74 78 2e 70 4d 65 6d 20 3d 20 75 2e cb.ctx.pMem = u.
1f09a 63 62 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b cb.pMem = &aMem[
1f09b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 62 pOp->p3];. u.cb
1f09c 2e 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e .pMem->n++;. u.
1f09d 63 62 2e 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d cb.ctx.s.flags =
1f09e 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 MEM_Null;. u.c
1f09f 62 2e 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 b.ctx.s.z = 0;.
1f0a0 20 75 2e 63 62 2e 63 74 78 2e 73 2e 7a 4d 61 6c u.cb.ctx.s.zMal
1f0a1 6c 6f 63 20 3d 20 30 3b 0a 20 20 75 2e 63 62 2e loc = 0;. u.cb.
1f0a2 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a ctx.s.xDel = 0;.
1f0a3 20 20 75 2e 63 62 2e 63 74 78 2e 73 2e 64 62 20 u.cb.ctx.s.db
1f0a4 3d 20 64 62 3b 0a 20 20 75 2e 63 62 2e 63 74 78 = db;. u.cb.ctx
1f0a5 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 .isError = 0;.
1f0a6 75 2e 63 62 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d u.cb.ctx.pColl =
1f0a7 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 62 2e 63 0;. if( u.cb.c
1f0a8 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 tx.pFunc->flags
1f0a9 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 & SQLITE_FUNC_NE
1f0aa 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 EDCOLL ){. as
1f0ab 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 sert( pOp>p->aOp
1f0ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1f0ad 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d pOp[-1].p4type==
1f0ae 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 P4_COLLSEQ );.
1f0af 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 assert( pOp[-1
1f0b0 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c ].opcode==OP_Col
1f0b1 6c 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 63 62 lSeq );. u.cb
1f0b2 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 .ctx.pColl = pOp
1f0b3 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 [-1].p4.pColl;.
1f0b4 20 7d 0a 20 20 28 75 2e 63 62 2e 63 74 78 2e 70 }. (u.cb.ctx.p
1f0b5 46 75 6e 63 2d 3e 78 53 74 65 70 29 28 26 75 2e Func->xStep)(&u.
1f0b6 63 62 2e 63 74 78 2c 20 75 2e 63 62 2e 6e 2c 20 cb.ctx, u.cb.n,
1f0b7 75 2e 63 62 2e 61 70 56 61 6c 29 3b 0a 20 20 69 u.cb.apVal);. i
1f0b8 66 28 20 75 2e 63 62 2e 63 74 78 2e 69 73 45 72 f( u.cb.ctx.isEr
1f0b9 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ror ){. sqlit
1f0ba 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1f0bb 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1f0bc 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ", sqlite3_value
1f0bd 5f 74 65 78 74 28 26 75 2e 63 62 2e 63 74 78 2e _text(&u.cb.ctx.
1f0be 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e s));. rc = u.
1f0bf 63 62 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a cb.ctx.isError;.
1f0c0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
1f0c1 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 eMemRelease(&u.c
1f0c2 62 2e 63 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 b.ctx.s);. brea
1f0c3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1f0c4 20 41 67 67 46 69 6e 61 6c 20 50 31 20 50 32 20 AggFinal P1 P2
1f0c5 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 * P4 *.**.** Exe
1f0c6 63 75 74 65 20 74 68 65 20 66 69 6e 61 6c 69 7a cute the finaliz
1f0c7 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 er function for
1f0c8 61 6e 20 61 67 67 72 65 67 61 74 65 2e 20 20 50 an aggregate. P
1f0c9 31 20 69 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 1 is.** the memo
1f0ca 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 ry location that
1f0cb 20 69 73 20 74 68 65 20 61 63 63 75 6d 75 6c 61 is the accumula
1f0cc 74 6f 72 20 66 6f 72 20 74 68 65 20 61 67 67 72 tor for the aggr
1f0cd 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 egate..**.** P2
1f0ce 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
1f0cf 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 arguments that
1f0d0 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f the step functio
1f0d1 6e 20 74 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 n takes and.** P
1f0d2 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
1f0d3 6f 20 74 68 65 20 46 75 6e 63 44 65 66 20 66 6f o the FuncDef fo
1f0d4 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e r this function.
1f0d5 20 20 54 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 The P2.** argu
1f0d6 6d 65 6e 74 20 69 73 20 6e 6f 74 20 75 73 65 64 ment is not used
1f0d7 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 2e by this opcode.
1f0d8 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 It is only the
1f0d9 72 65 20 74 6f 20 64 69 73 61 6d 62 69 67 75 61 re to disambigua
1f0da 74 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 te.** functions
1f0db 74 68 61 74 20 63 61 6e 20 74 61 6b 65 20 76 61 that can take va
1f0dc 72 79 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 rying numbers of
1f0dd 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 arguments. The
1f0de 0a 2a 2a 20 50 34 20 61 72 67 75 6d 65 6e 74 20 .** P4 argument
1f0df 69 73 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 is only needed f
1f0e0 6f 72 20 74 68 65 20 64 65 67 65 6e 65 72 61 74 or the degenerat
1f0e1 65 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 e case where.**
1f0e2 74 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f the step functio
1f0e3 6e 20 77 61 73 20 6e 6f 74 20 70 72 65 76 69 6f n was not previo
1f0e4 75 73 6c 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a usly called..*/.
1f0e5 63 61 73 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c case OP_AggFinal
1f0e6 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1f0e7 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1f0e8 76 65 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f ved into u.cc */
1f0e9 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 . Mem *pMem;.#e
1f0ea 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1f0eb 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f0ec 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 61 73 73 to u.cc */. ass
1f0ed 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 ert( pOp->p1>0 &
1f0ee 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d & pOp->p1<=p->nM
1f0ef 65 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 70 4d 65 em );. u.cc.pMe
1f0f0 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 m = &aMem[pOp->p
1f0f1 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 1];. assert( (u
1f0f2 2e 63 63 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 .cc.pMem->flags
1f0f3 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d & ~(MEM_Null|MEM
1f0f4 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 _Agg))==0 );. r
1f0f5 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1f0f6 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 63 2e emFinalize(u.cc.
1f0f7 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 pMem, pOp->p4.pF
1f0f8 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 unc);. if( rc )
1f0f9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
1f0fa 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1f0fb 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1f0fc 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1f0fd 28 75 2e 63 63 2e 70 4d 65 6d 29 29 3b 0a 20 20 (u.cc.pMem));.
1f0fe 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 }. sqlite3VdbeC
1f0ff 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e hangeEncoding(u.
1f100 63 63 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e cc.pMem, encodin
1f101 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 g);. UPDATE_MAX
1f102 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 63 2e 70 _BLOBSIZE(u.cc.p
1f103 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 Mem);. if( sqli
1f104 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1f105 28 75 2e 63 63 2e 70 4d 65 6d 29 20 29 7b 0a 20 (u.cc.pMem) ){.
1f106 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1f107 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1f108 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1f109 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
1f10a 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 M) && !defined(S
1f10b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 QLITE_OMIT_ATTAC
1f10c 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 H)./* Opcode: Va
1f10d 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a cuum * * * * *.*
1f10e 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 *.** Vacuum the
1f10f 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e entire database.
1f110 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 This opcode wi
1f111 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 ll cause other v
1f112 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e irtual.** machin
1f113 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 es to be created
1f114 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 and run. It ma
1f115 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 y not be called
1f116 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 from within.** a
1f117 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f transaction..*/
1f118 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a .case OP_Vacuum:
1f119 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 {. if( sqlite3
1f11a 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1f11b 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f11c 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 o_misuse; . rc
1f11d 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 = sqlite3RunVacu
1f11e 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 um(&p->zErrMsg,
1f11f 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 db);. if( sqlit
1f120 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1f121 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1f122 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 to_misuse;. bre
1f123 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ak;.}.#endif..#i
1f124 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
1f125 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
1f126 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e M)./* Opcode: In
1f127 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a crVacuum P1 P2 *
1f128 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f * *.**.** Perfo
1f129 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 rm a single step
1f12a 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e of the incremen
1f12b 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 tal vacuum proce
1f12c 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 dure on.** the P
1f12d 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 1 database. If t
1f12e 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 he vacuum has fi
1f12f 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 nished, jump to
1f130 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 instruction.** P
1f131 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 2. Otherwise, fa
1f132 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1f133 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1f134 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 on..*/.case OP_I
1f135 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 ncrVacuum: {
1f136 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 /* jump */.#
1f137 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1f138 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f139 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 42 74 nto u.cd */. Bt
1f13a 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 ree *pBt;.#endif
1f13b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f13c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f13d 2e 63 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .cd */.. assert
1f13e 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1f13f 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1f140 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1f141 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1f142 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1f143 0a 20 20 75 2e 63 64 2e 70 42 74 20 3d 20 64 62 . u.cd.pBt = db
1f144 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1f145 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 Bt;. rc = sqlit
1f146 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 e3BtreeIncrVacuu
1f147 6d 28 75 2e 63 64 2e 70 42 74 29 3b 0a 20 20 69 m(u.cd.pBt);. i
1f148 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f f( rc==SQLITE_DO
1f149 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 NE ){. pc = p
1f14a 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1f14b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f14c 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1f14d 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 endif../* Opcode
1f14e 3a 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20 : Expire P1 * *
1f14f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 * *.**.** Cause
1f150 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 precompiled stat
1f151 65 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 ements to become
1f152 20 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70 expired. An exp
1f153 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ired statement.*
1f154 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 * fails with an
1f155 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 error code of SQ
1f156 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69 LITE_SCHEMA if i
1f157 74 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74 t is ever execut
1f158 65 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 ed .** (via sqli
1f159 74 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 te3_step())..**
1f15a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20 .** If P1 is 0,
1f15b 74 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 then all SQL sta
1f15c 74 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 tements become e
1f15d 78 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73 xpired. If P1 is
1f15e 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 non-zero,.** th
1f15f 65 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 en only the curr
1f160 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 ently executing
1f161 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66 statement is aff
1f162 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 ected. .*/.case
1f163 4f 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 OP_Expire: {. i
1f164 66 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 f( !pOp->p1 ){.
1f165 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 sqlite3Expire
1f166 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1f167 74 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b ts(db);. }else{
1f168 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 . p->expired
1f169 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b = 1;. }. break
1f16a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1f16b 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1f16c 43 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a CACHE./* Opcode:
1f16d 20 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 TableLock P1 P2
1f16e 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f P3 P4 *.**.** O
1f16f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 btain a lock on
1f170 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 a particular tab
1f171 6c 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 le. This instruc
1f172 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 tion is only use
1f173 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 d when.** the sh
1f174 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 ared-cache featu
1f175 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a re is enabled. .
1f176 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1f177 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
1f178 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 abase in sqlite3
1f179 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 .aDb[] of the da
1f17a 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 tabase.** on whi
1f17b 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 ch the lock is a
1f17c 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 cquired. A read
1f17d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
1f17e 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 if P3==0 or.**
1f17f 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 a write lock if
1f180 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 P3==1..**.** P2
1f181 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f contains the roo
1f182 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 t-page of the ta
1f183 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a ble to lock..**.
1f184 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 ** P4 contains a
1f185 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1f186 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1f187 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 e being locked.
1f188 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 This is only.**
1f189 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 used to generate
1f18a 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1f18b 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 e if the lock ca
1f18c 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 nnot be obtained
1f18d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 ..*/.case OP_Tab
1f18e 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 leLock: {. u8 i
1f18f 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 sWriteLock = (u8
1f190 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 )pOp->p3;. if(
1f191 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 isWriteLock || 0
1f192 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c ==(db->flags&SQL
1f193 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 ITE_ReadUncommit
1f194 74 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 ted) ){. int
1f195 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 p1 = pOp->p1; .
1f196 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 assert( p1>=0
1f197 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 && p1<db->nDb )
1f198 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
1f199 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 ->btreeMask & (1
1f19a 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 <<p1))!=0 );.
1f19b 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 assert( isWrite
1f19c 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 Lock==0 || isWri
1f19d 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 teLock==1 );.
1f19e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1f19f 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e eeLockTable(db->
1f1a0 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 aDb[p1].pBt, pOp
1f1a1 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 ->p2, isWriteLoc
1f1a2 6b 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26 k);. if( (rc&
1f1a3 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 0xFF)==SQLITE_LO
1f1a4 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f CKED ){. co
1f1a5 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f nst char *z = pO
1f1a6 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 p->p4.z;. s
1f1a7 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1f1a8 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1f1a9 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 "database table
1f1aa 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c is locked: %s",
1f1ab 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 z);. }. }.
1f1ac 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f1ad 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1f1ae 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a SHARED_CACHE */.
1f1af 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f1b0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f1b1 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 E./* Opcode: VBe
1f1b2 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a gin * * * P4 *.*
1f1b3 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 *.** P4 may be a
1f1b4 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 pointer to an s
1f1b5 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1f1b6 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 cture. If so, ca
1f1b7 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 ll the .** xBegi
1f1b8 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 n method for tha
1f1b9 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 t table..**.** A
1f1ba 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20 lso, whether or
1f1bb 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63 not P4 is set, c
1f1bc 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 heck that this i
1f1bd 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c s not being call
1f1be 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 ed from.** withi
1f1bf 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 n a callback to
1f1c0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
1f1c1 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 xSync() method.
1f1c2 49 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72 If it is, the er
1f1c3 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c ror.** code will
1f1c4 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 be set to SQLIT
1f1c5 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 E_LOCKED..*/.cas
1f1c6 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 e OP_VBegin: {.#
1f1c7 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1f1c8 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f1c9 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 56 54 nto u.ce */. VT
1f1ca 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e able *pVTab;.#en
1f1cb 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1f1cc 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1f1cd 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 75 2e 63 65 o u.ce */. u.ce
1f1ce 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 .pVTab = pOp->p4
1f1cf 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 .pVtab;. rc = s
1f1d0 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 qlite3VtabBegin(
1f1d1 64 62 2c 20 75 2e 63 65 2e 70 56 54 61 62 29 3b db, u.ce.pVTab);
1f1d2 0a 20 20 69 66 28 20 75 2e 63 65 2e 70 56 54 61 . if( u.ce.pVTa
1f1d3 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
1f1d4 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1f1d5 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a rrMsg);. p->z
1f1d6 45 72 72 4d 73 67 20 3d 20 75 2e 63 65 2e 70 56 ErrMsg = u.ce.pV
1f1d7 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 Tab->pVtab->zErr
1f1d8 4d 73 67 3b 0a 20 20 20 20 75 2e 63 65 2e 70 56 Msg;. u.ce.pV
1f1d9 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 Tab->pVtab->zErr
1f1da 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 Msg = 0;. }. b
1f1db 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1f1dc 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1f1dd 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1f1de 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f1df 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1f1e0 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 /* Opcode: VCrea
1f1e1 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a te P1 * * P4 *.*
1f1e2 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e *.** P4 is the n
1f1e3 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c ame of a virtual
1f1e4 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 table in databa
1f1e5 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 se P1. Call the
1f1e6 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a xCreate method.*
1f1e7 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 * for that table
1f1e8 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 ..*/.case OP_VCr
1f1e9 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 eate: {. rc = s
1f1ea 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 qlite3VtabCallCr
1f1eb 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 eate(db, pOp->p1
1f1ec 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d , pOp->p4.z, &p-
1f1ed 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 >zErrMsg);. bre
1f1ee 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1f1ef 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1f1f0 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 UALTABLE */..#if
1f1f1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f1f2 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a _VIRTUALTABLE./*
1f1f3 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f Opcode: VDestro
1f1f4 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a y P1 * * P4 *.**
1f1f5 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 .** P4 is the na
1f1f6 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 me of a virtual
1f1f7 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 table in databas
1f1f8 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 e P1. Call the
1f1f9 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a xDestroy method.
1f1fa 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 ** of that table
1f1fb 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 ..*/.case OP_VDe
1f1fc 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e stroy: {. p->in
1f1fd 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a VtabMethod = 2;.
1f1fe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 rc = sqlite3Vt
1f1ff 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 abCallDestroy(db
1f200 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
1f201 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 p4.z);. p->inVt
1f202 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1f203 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1f204 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 /* SQLITE_OMIT_V
1f205 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a IRTUALTABLE */..
1f206 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f207 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f208 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 ./* Opcode: VOpe
1f209 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a n P1 * * P4 *.**
1f20a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1f20b 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c ter to a virtual
1f20c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 table object, a
1f20d 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 n sqlite3_vtab s
1f20e 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 tructure..** P1
1f20f 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 is a cursor numb
1f210 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 er. This opcode
1f211 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 opens a cursor
1f212 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a to the virtual.*
1f213 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 * table and stor
1f214 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 es that cursor i
1f215 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 n P1..*/.case OP
1f216 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 _VOpen: {.#if 0
1f217 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f218 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f219 2e 63 66 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .cf */. VdbeCur
1f21a 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c sor *pCur;. sql
1f21b 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1f21c 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 *pVtabCursor;.
1f21d 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1f21e 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f Vtab;. sqlite3_
1f21f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1f220 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f221 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f222 20 69 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 0a 20 into u.cf */..
1f223 20 75 2e 63 66 2e 70 43 75 72 20 3d 20 30 3b 0a u.cf.pCur = 0;.
1f224 20 20 75 2e 63 66 2e 70 56 74 61 62 43 75 72 73 u.cf.pVtabCurs
1f225 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 66 2e 70 or = 0;. u.cf.p
1f226 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Vtab = pOp->p4.p
1f227 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 Vtab->pVtab;. u
1f228 2e 63 66 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 .cf.pModule = (s
1f229 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 qlite3_module *)
1f22a 75 2e 63 66 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 u.cf.pVtab->pMod
1f22b 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e ule;. assert(u.
1f22c 63 66 2e 70 56 74 61 62 20 26 26 20 75 2e 63 66 cf.pVtab && u.cf
1f22d 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 .pModule);. if(
1f22e 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 sqlite3SafetyOf
1f22f 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f f(db) ) goto abo
1f230 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1f231 3b 0a 20 20 72 63 20 3d 20 75 2e 63 66 2e 70 4d ;. rc = u.cf.pM
1f232 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 odule->xOpen(u.c
1f233 66 2e 70 56 74 61 62 2c 20 26 75 2e 63 66 2e 70 f.pVtab, &u.cf.p
1f234 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 VtabCursor);. s
1f235 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f236 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1f237 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1f238 66 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 f.pVtab->zErrMsg
1f239 3b 0a 20 20 75 2e 63 66 2e 70 56 74 61 62 2d 3e ;. u.cf.pVtab->
1f23a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 zErrMsg = 0;. i
1f23b 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1f23c 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1f23d 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f23e 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f e;. if( SQLITE_
1f23f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a OK==rc ){. /*
1f240 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 Initialize sqli
1f241 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1f242 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 base class */.
1f243 20 20 75 2e 63 66 2e 70 56 74 61 62 43 75 72 73 u.cf.pVtabCurs
1f244 6f 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 66 or->pVtab = u.cf
1f245 2e 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 .pVtab;.. /*
1f246 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 Initialise vdbe
1f247 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f cursor object */
1f248 0a 20 20 20 20 75 2e 63 66 2e 70 43 75 72 20 3d . u.cf.pCur =
1f249 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 allocateCursor(
1f24a 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d p, pOp->p1, 0, -
1f24b 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75 1, 0);. if( u
1f24c 2e 63 66 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 .cf.pCur ){.
1f24d 20 20 75 2e 63 66 2e 70 43 75 72 2d 3e 70 56 74 u.cf.pCur->pVt
1f24e 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 66 2e abCursor = u.cf.
1f24f 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 pVtabCursor;.
1f250 20 20 20 75 2e 63 66 2e 70 43 75 72 2d 3e 70 4d u.cf.pCur->pM
1f251 6f 64 75 6c 65 20 3d 20 75 2e 63 66 2e 70 56 74 odule = u.cf.pVt
1f252 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d abCursor->pVtab-
1f253 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 >pModule;. }e
1f254 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d lse{. db->m
1f255 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
1f256 0a 20 20 20 20 20 20 75 2e 63 66 2e 70 4d 6f 64 . u.cf.pMod
1f257 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 66 ule->xClose(u.cf
1f258 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 .pVtabCursor);.
1f259 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1f25a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1f25b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f25c 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1f25d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1f25e 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f IRTUALTABLE./* O
1f25f 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 pcode: VFilter P
1f260 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1f261 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f ** P1 is a curso
1f262 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56 r opened using V
1f263 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 Open. P2 is an
1f264 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 address to jump
1f265 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c to if.** the fil
1f266 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74 tered result set
1f267 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a is empty..**.**
1f268 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55 P4 is either NU
1f269 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 LL or a string t
1f26a 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 hat was generate
1f26b 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e d by the xBestIn
1f26c 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 dex.** method of
1f26d 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 the module. Th
1f26e 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e e interpretation
1f26f 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e of the P4 strin
1f270 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 g is left.** to
1f271 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 the module imple
1f272 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a mentation..**.**
1f273 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 This opcode inv
1f274 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72 okes the xFilter
1f275 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 method on the v
1f276 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65 irtual table spe
1f277 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e cified.** by P1.
1f278 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75 The integer qu
1f279 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 ery plan paramet
1f27a 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73 er to xFilter is
1f27b 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1f27c 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 ter.** P3. Regis
1f27d 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20 ter P3+1 stores
1f27e 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74 the argc paramet
1f27f 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20 er to be passed
1f280 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 to the.** xFilte
1f281 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74 r method. Regist
1f282 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 ers P3+2..P3+1+a
1f283 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63 rgc are the argc
1f284 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 .** additional p
1f285 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 arameters which
1f286 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a are passed to.**
1f287 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 xFilter as argv
1f288 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20 . Register P3+2
1f289 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 becomes argv[0]
1f28a 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78 when passed to x
1f28b 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 Filter..**.** A
1f28c 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 jump is made to
1f28d 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 P2 if the result
1f28e 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 set after filte
1f28f 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d ring would be em
1f290 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f pty..*/.case OP_
1f291 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 VFilter: { /*
1f292 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f jump */.#if 0 /
1f293 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1f294 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1f295 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b g */. int nArg;
1f296 0a 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 . int iQuery;.
1f297 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
1f298 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1f299 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 Mem *pQuery;.
1f29a 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 Mem *pArgc;. s
1f29b 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
1f29c 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b or *pVtabCursor;
1f29d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 . sqlite3_vtab
1f29e 2a 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75 *pVtab;. VdbeCu
1f29f 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e rsor *pCur;. in
1f2a0 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a t res;. int i;.
1f2a1 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 Mem **apArg;.#
1f2a2 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1f2a3 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f2a4 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 75 nto u.cg */.. u
1f2a5 2e 63 67 2e 70 51 75 65 72 79 20 3d 20 26 61 4d .cg.pQuery = &aM
1f2a6 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 em[pOp->p3];. u
1f2a7 2e 63 67 2e 70 41 72 67 63 20 3d 20 26 75 2e 63 .cg.pArgc = &u.c
1f2a8 67 2e 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75 g.pQuery[1];. u
1f2a9 2e 63 67 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 .cg.pCur = p->ap
1f2aa 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1f2ab 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1f2ac 4f 70 2d 3e 70 33 2c 20 75 2e 63 67 2e 70 51 75 Op->p3, u.cg.pQu
1f2ad 65 72 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ery);. assert(
1f2ae 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 u.cg.pCur->pVtab
1f2af 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 63 67 Cursor );. u.cg
1f2b0 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75 .pVtabCursor = u
1f2b1 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 .cg.pCur->pVtabC
1f2b2 75 72 73 6f 72 3b 0a 20 20 75 2e 63 67 2e 70 56 ursor;. u.cg.pV
1f2b3 74 61 62 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 tab = u.cg.pVtab
1f2b4 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 Cursor->pVtab;.
1f2b5 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 u.cg.pModule =
1f2b6 75 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 u.cg.pVtab->pMod
1f2b7 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20 ule;.. /* Grab
1f2b8 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 the index number
1f2b9 20 61 6e 64 20 61 72 67 63 20 70 61 72 61 6d 65 and argc parame
1f2ba 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 ters */. assert
1f2bb 28 20 28 75 2e 63 67 2e 70 51 75 65 72 79 2d 3e ( (u.cg.pQuery->
1f2bc 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d flags&MEM_Int)!=
1f2bd 30 20 26 26 20 75 2e 63 67 2e 70 41 72 67 63 2d 0 && u.cg.pArgc-
1f2be 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 >flags==MEM_Int
1f2bf 29 3b 0a 20 20 75 2e 63 67 2e 6e 41 72 67 20 3d );. u.cg.nArg =
1f2c0 20 28 69 6e 74 29 75 2e 63 67 2e 70 41 72 67 63 (int)u.cg.pArgc
1f2c1 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 67 2e 69 51 ->u.i;. u.cg.iQ
1f2c2 75 65 72 79 20 3d 20 28 69 6e 74 29 75 2e 63 67 uery = (int)u.cg
1f2c3 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 .pQuery->u.i;..
1f2c4 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 /* Invoke the x
1f2c5 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f Filter method */
1f2c6 0a 20 20 7b 0a 20 20 20 20 75 2e 63 67 2e 72 65 . {. u.cg.re
1f2c7 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 63 67 2e s = 0;. u.cg.
1f2c8 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 apArg = p->apArg
1f2c9 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 67 2e 69 ;. for(u.cg.i
1f2ca 20 3d 20 30 3b 20 75 2e 63 67 2e 69 3c 75 2e 63 = 0; u.cg.i<u.c
1f2cb 67 2e 6e 41 72 67 3b 20 75 2e 63 67 2e 69 2b 2b g.nArg; u.cg.i++
1f2cc 29 7b 0a 20 20 20 20 20 20 75 2e 63 67 2e 61 70 ){. u.cg.ap
1f2cd 41 72 67 5b 75 2e 63 67 2e 69 5d 20 3d 20 26 75 Arg[u.cg.i] = &u
1f2ce 2e 63 67 2e 70 41 72 67 63 5b 75 2e 63 67 2e 69 .cg.pArgc[u.cg.i
1f2cf 2b 31 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 +1];. sqlit
1f2d0 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 e3VdbeMemStoreTy
1f2d1 70 65 28 75 2e 63 67 2e 61 70 41 72 67 5b 75 2e pe(u.cg.apArg[u.
1f2d2 63 67 2e 69 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20 cg.i]);. }..
1f2d3 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1f2d4 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1f2d5 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f2d6 6d 69 73 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 misuse;. p->i
1f2d7 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b nVtabMethod = 1;
1f2d8 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 . rc = u.cg.p
1f2d9 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28 Module->xFilter(
1f2da 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 u.cg.pVtabCursor
1f2db 2c 20 75 2e 63 67 2e 69 51 75 65 72 79 2c 20 70 , u.cg.iQuery, p
1f2dc 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 63 67 2e 6e Op->p4.z, u.cg.n
1f2dd 41 72 67 2c 20 75 2e 63 67 2e 61 70 41 72 67 29 Arg, u.cg.apArg)
1f2de 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d ;. p->inVtabM
1f2df 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 ethod = 0;. s
1f2e0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f2e1 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1f2e2 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 p->zErrMsg = u
1f2e3 2e 63 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .cg.pVtab->zErrM
1f2e4 73 67 3b 0a 20 20 20 20 75 2e 63 67 2e 70 56 74 sg;. u.cg.pVt
1f2e5 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1f2e6 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1f2e7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1f2e8 75 2e 63 67 2e 72 65 73 20 3d 20 75 2e 63 67 2e u.cg.res = u.cg.
1f2e9 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e pModule->xEof(u.
1f2ea 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b cg.pVtabCursor);
1f2eb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
1f2ec 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1f2ed 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1f2ee 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a due_to_misuse;..
1f2ef 20 20 20 20 69 66 28 20 75 2e 63 67 2e 72 65 73 if( u.cg.res
1f2f0 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 ){. pc = p
1f2f1 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1f2f2 7d 0a 20 20 7d 0a 20 20 75 2e 63 67 2e 70 43 75 }. }. u.cg.pCu
1f2f3 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a r->nullRow = 0;.
1f2f4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1f2f5 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1f2f6 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1f2f7 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1f2f8 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1f2f9 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1f2fa 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 Column P1 P2 P3
1f2fb 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 * *.**.** Store
1f2fc 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1f2fd 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 P2-th column of
1f2fe 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74 .** the row of t
1f2ff 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
1f300 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 that the .** P1
1f301 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 cursor is point
1f302 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 ing to into regi
1f303 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 ster P3..*/.case
1f304 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 OP_VColumn: {.#
1f305 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1f306 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f307 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 20 20 73 71 nto u.ch */. sq
1f308 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1f309 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 b;. const sqlit
1f30a 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1f30b 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 le;. Mem *pDest
1f30c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ;. sqlite3_cont
1f30d 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65 ext sContext;.#e
1f30e 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1f30f 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f310 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 56 64 to u.ch */.. Vd
1f311 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d beCursor *pCur =
1f312 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1f313 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 1];. assert( pC
1f314 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 ur->pVtabCursor
1f315 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1f316 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1f317 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1f318 75 2e 63 68 2e 70 44 65 73 74 20 3d 20 26 61 4d u.ch.pDest = &aM
1f319 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 em[pOp->p3];. i
1f31a 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 f( pCur->nullRow
1f31b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1f31c 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e dbeMemSetNull(u.
1f31d 63 68 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 62 ch.pDest);. b
1f31e 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 68 reak;. }. u.ch
1f31f 2e 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 .pVtab = pCur->p
1f320 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 VtabCursor->pVta
1f321 62 3b 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c b;. u.ch.pModul
1f322 65 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e e = u.ch.pVtab->
1f323 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 pModule;. asser
1f324 74 28 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 2d t( u.ch.pModule-
1f325 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 >xColumn );. me
1f326 6d 73 65 74 28 26 75 2e 63 68 2e 73 43 6f 6e 74 mset(&u.ch.sCont
1f327 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 ext, 0, sizeof(u
1f328 2e 63 68 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a .ch.sContext));.
1f329 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 . /* The output
1f32a 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 cell may alread
1f32b 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72 20 y have a buffer
1f32c 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a allocated. Move.
1f32d 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 ** the current
1f32e 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 contents to u.c
1f32f 68 2e 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 h.sContext.s so
1f330 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72 in case the user
1f331 2d 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 -function. ** c
1f332 61 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 an use the alrea
1f333 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 dy allocated buf
1f334 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 fer instead of a
1f335 6c 6c 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a llocating a. **
1f336 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 new one.. */.
1f337 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
1f338 6f 76 65 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 ove(&u.ch.sConte
1f339 78 74 2e 73 2c 20 75 2e 63 68 2e 70 44 65 73 74 xt.s, u.ch.pDest
1f33a 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 );. MemSetTypeF
1f33b 6c 61 67 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 lag(&u.ch.sConte
1f33c 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b xt.s, MEM_Null);
1f33d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 .. if( sqlite3S
1f33e 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1f33f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1f340 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 _misuse;. rc =
1f341 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 u.ch.pModule->xC
1f342 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 olumn(pCur->pVta
1f343 62 43 75 72 73 6f 72 2c 20 26 75 2e 63 68 2e 73 bCursor, &u.ch.s
1f344 43 6f 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 Context, pOp->p2
1f345 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1f346 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1f347 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
1f348 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a = u.ch.pVtab->z
1f349 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 68 2e 70 ErrMsg;. u.ch.p
1f34a 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1f34b 30 3b 0a 20 20 69 66 28 20 75 2e 63 68 2e 73 43 0;. if( u.ch.sC
1f34c 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29 ontext.isError )
1f34d 7b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 68 2e {. rc = u.ch.
1f34e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 sContext.isError
1f34f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 ;. }.. /* Copy
1f350 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
1f351 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 he function to t
1f352 68 65 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 he P3 register.
1f353 57 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 We. ** do this
1f354 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
1f355 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 ether or not an
1f356 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 error occurred t
1f357 6f 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a o ensure any. *
1f358 2a 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 * dynamic alloca
1f359 74 69 6f 6e 20 69 6e 20 75 2e 63 68 2e 73 43 6f tion in u.ch.sCo
1f35a 6e 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 ntext.s (a Mem s
1f35b 74 72 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 truct) is relea
1f35c 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 sed.. */. sqli
1f35d 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1f35e 6f 64 69 6e 67 28 26 75 2e 63 68 2e 73 43 6f 6e oding(&u.ch.sCon
1f35f 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 text.s, encoding
1f360 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1f361 4d 65 6d 4d 6f 76 65 28 75 2e 63 68 2e 70 44 65 MemMove(u.ch.pDe
1f362 73 74 2c 20 26 75 2e 63 68 2e 73 43 6f 6e 74 65 st, &u.ch.sConte
1f363 78 74 2e 73 29 3b 0a 20 20 52 45 47 49 53 54 45 xt.s);. REGISTE
1f364 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1f365 20 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 20 20 u.ch.pDest);.
1f366 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1f367 49 5a 45 28 75 2e 63 68 2e 70 44 65 73 74 29 3b IZE(u.ch.pDest);
1f368 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 .. if( sqlite3S
1f369 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 afetyOn(db) ){.
1f36a 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1f36b 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d e_to_misuse;. }
1f36c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1f36d 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 68 beMemTooBig(u.ch
1f36e 2e 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 .pDest) ){. g
1f36f 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d oto too_big;. }
1f370 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1f371 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1f372 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1f373 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1f374 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1f375 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1f376 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a Next P1 P2 * * *
1f377 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 .**.** Advance v
1f378 69 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 irtual table P1
1f379 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 to the next row
1f37a 69 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 in its result se
1f37b 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f t and.** jump to
1f37c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e instruction P2.
1f37d 20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 Or, if the vir
1f37e 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 tual table has r
1f37f 65 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e eached.** the en
1f380 64 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 d of its result
1f381 73 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 set, then fall t
1f382 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1f383 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a xt instruction..
1f384 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 */.case OP_VNext
1f385 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f : { /* jump */
1f386 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1f387 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f388 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 20 into u.ci */.
1f389 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1f38a 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c tab;. const sql
1f38b 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1f38c 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b dule;. int res;
1f38d 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1f38e 43 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c Cur;.#endif /* l
1f38f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f390 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a oved into u.ci *
1f391 2f 0a 0a 20 20 75 2e 63 69 2e 72 65 73 20 3d 20 /.. u.ci.res =
1f392 30 3b 0a 20 20 75 2e 63 69 2e 70 43 75 72 20 3d 0;. u.ci.pCur =
1f393 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1f394 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1f395 63 69 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 ci.pCur->pVtabCu
1f396 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e rsor );. if( u.
1f397 63 69 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 ci.pCur->nullRow
1f398 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 ){. break;.
1f399 20 7d 0a 20 20 75 2e 63 69 2e 70 56 74 61 62 20 }. u.ci.pVtab
1f39a 3d 20 75 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74 = u.ci.pCur->pVt
1f39b 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b abCursor->pVtab;
1f39c 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 20 . u.ci.pModule
1f39d 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 70 4d = u.ci.pVtab->pM
1f39e 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 odule;. assert(
1f39f 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.ci.pModule->x
1f3a0 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e Next );.. /* In
1f3a1 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 voke the xNext()
1f3a2 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d method of the m
1f3a3 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 odule. There is
1f3a4 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 no way for the.
1f3a5 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 ** underlying i
1f3a6 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f mplementation to
1f3a7 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
1f3a8 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 if one occurs d
1f3a9 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 uring. ** xNext
1f3aa 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 (). Instead, if
1f3ab 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
1f3ac 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 true is returne
1f3ad 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 d (indicating th
1f3ae 61 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 at. ** data is
1f3af 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 available) and t
1f3b0 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 he error code re
1f3b1 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c turned when xCol
1f3b2 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 umn or. ** some
1f3b3 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 other method is
1f3b4 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e next invoked on
1f3b5 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61 the save virtua
1f3b6 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a l table cursor..
1f3b7 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
1f3b8 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1f3b9 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1f3ba 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d _to_misuse;. p-
1f3bb 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 >inVtabMethod =
1f3bc 31 3b 0a 20 20 72 63 20 3d 20 75 2e 63 69 2e 70 1;. rc = u.ci.p
1f3bd 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e Module->xNext(u.
1f3be 63 69 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 ci.pCur->pVtabCu
1f3bf 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 rsor);. p->inVt
1f3c0 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1f3c1 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1f3c2 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1f3c3 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e p->zErrMsg = u.
1f3c4 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 ci.pVtab->zErrMs
1f3c5 67 3b 0a 20 20 75 2e 63 69 2e 70 56 74 61 62 2d g;. u.ci.pVtab-
1f3c6 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1f3c7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1f3c8 4b 20 29 7b 0a 20 20 20 20 75 2e 63 69 2e 72 65 K ){. u.ci.re
1f3c9 73 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 s = u.ci.pModule
1f3ca 2d 3e 78 45 6f 66 28 75 2e 63 69 2e 70 43 75 72 ->xEof(u.ci.pCur
1f3cb 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a ->pVtabCursor);.
1f3cc 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1f3cd 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1f3ce 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f3cf 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 o_misuse;.. if(
1f3d0 20 21 75 2e 63 69 2e 72 65 73 20 29 7b 0a 20 20 !u.ci.res ){.
1f3d1 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 /* If there is
1f3d2 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 data, jump to P
1f3d3 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 2 */. pc = pO
1f3d4 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 p->p2 - 1;. }.
1f3d5 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f3d6 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1f3d7 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1f3d8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f3d9 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f3da 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 E./* Opcode: VRe
1f3db 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a name P1 * * P4 *
1f3dc 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 .**.** P4 is a p
1f3dd 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 ointer to a virt
1f3de 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 ual table object
1f3df 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 , an sqlite3_vta
1f3e0 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 b structure..**
1f3e1 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f This opcode invo
1f3e2 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f kes the correspo
1f3e3 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 nding xRename me
1f3e4 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 0a thod. The value.
1f3e5 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ** in register P
1f3e6 31 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 1 is passed as t
1f3e7 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e he zName argumen
1f3e8 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 t to the xRename
1f3e9 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 method..*/.case
1f3ea 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 OP_VRename: {.#
1f3eb 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1f3ec 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f3ed 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 71 nto u.cj */. sq
1f3ee 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1f3ef 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b b;. Mem *pName;
1f3f0 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f3f1 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f3f2 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 0a 20 into u.cj */..
1f3f3 20 75 2e 63 6a 2e 70 56 74 61 62 20 3d 20 70 4f u.cj.pVtab = pO
1f3f4 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 p->p4.pVtab->pVt
1f3f5 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 4e 61 6d 65 ab;. u.cj.pName
1f3f6 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 = &aMem[pOp->p1
1f3f7 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 ];. assert( u.c
1f3f8 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 j.pVtab->pModule
1f3f9 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 20 52 ->xRename );. R
1f3fa 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1f3fb 70 2d 3e 70 31 2c 20 75 2e 63 6a 2e 70 4e 61 6d p->p1, u.cj.pNam
1f3fc 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e e);. assert( u.
1f3fd 63 6a 2e 70 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 cj.pName->flags
1f3fe 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 69 & MEM_Str );. i
1f3ff 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1f400 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 Off(db) ) goto a
1f401 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1f402 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e se;. rc = u.cj.
1f403 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e pVtab->pModule->
1f404 78 52 65 6e 61 6d 65 28 75 2e 63 6a 2e 70 56 74 xRename(u.cj.pVt
1f405 61 62 2c 20 75 2e 63 6a 2e 70 4e 61 6d 65 2d 3e ab, u.cj.pName->
1f406 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 z);. sqlite3DbF
1f407 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1f408 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 sg);. p->zErrMs
1f409 67 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e g = u.cj.pVtab->
1f40a 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e zErrMsg;. u.cj.
1f40b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1f40c 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 0;. if( sqlite
1f40d 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1f40e 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f40f 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 o_misuse;.. bre
1f410 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ak;.}.#endif..#i
1f411 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f412 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1f413 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 74 * Opcode: VUpdat
1f414 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a e P1 P2 P3 P4 *.
1f415 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f **.** P4 is a po
1f416 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 inter to a virtu
1f417 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c al table object,
1f418 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 an sqlite3_vtab
1f419 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 structure..** T
1f41a 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b his opcode invok
1f41b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e es the correspon
1f41c 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 74 ding xUpdate met
1f41d 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a hod. P2 values.*
1f41e 2a 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 * are contiguous
1f41f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 memory cells st
1f420 61 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f 20 arting at P3 to
1f421 70 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 64 pass to the xUpd
1f422 61 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 ate .** invocati
1f423 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 6e on. The value in
1f424 20 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 32 register (P3+P2
1f425 2d 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 -1) corresponds
1f426 74 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 to the .** p2th
1f427 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 element of the a
1f428 72 67 76 20 61 72 72 61 79 20 70 61 73 73 65 64 rgv array passed
1f429 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a to xUpdate..**.
1f42a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d ** The xUpdate m
1f42b 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 ethod will do a
1f42c 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 DELETE or an INS
1f42d 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 ERT or both..**
1f42e 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d The argv[0] elem
1f42f 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 65 ent (which corre
1f430 73 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 sponds to memory
1f431 20 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 cell P3).** is
1f432 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 72 the rowid of a r
1f433 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 ow to delete. I
1f434 66 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c f argv[0] is NUL
1f435 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 L then no .** de
1f436 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 letion occurs.
1f437 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d The argv[1] elem
1f438 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 ent is the rowid
1f439 20 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 of the new .**
1f43a 72 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 62 row. This can b
1f43b 65 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 e NULL to have t
1f43c 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
1f43d 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 20 select the new
1f43e 0a 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 74 .** rowid for it
1f43f 73 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 65 self. The subse
1f440 71 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 quent elements i
1f441 6e 20 74 68 65 20 61 72 72 61 79 20 61 72 65 20 n the array are
1f442 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 6f .** the values o
1f443 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1f444 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 new row..**.**
1f445 49 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f If P2==1 then no
1f446 20 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 6f insert is perfo
1f447 72 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 rmed. argv[0] i
1f448 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a s the rowid of.*
1f449 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 * a row to delet
1f44a 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 e..**.** P1 is a
1f44b 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 boolean flag. I
1f44c 66 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 f it is set to t
1f44d 72 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 64 rue and the xUpd
1f44e 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 ate call.** is s
1f44f 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 uccessful, then
1f450 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
1f451 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 ed by sqlite3_la
1f452 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
1f453 29 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 ) .** is set to
1f454 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1f455 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 rowid for the r
1f456 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 ow just inserted
1f457 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 ..*/.case OP_VUp
1f458 64 61 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f date: {.#if 0 /
1f459 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1f45a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1f45b 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 k */. sqlite3_v
1f45c 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 tab *pVtab;. sq
1f45d 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1f45e 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72 odule;. int nAr
1f45f 67 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 g;. int i;. sq
1f460 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69 64 lite_int64 rowid
1f461 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b ;. Mem **apArg;
1f462 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a 23 65 6e 64 . Mem *pX;.#end
1f463 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1f464 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f465 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 75 2e 63 6b u.ck */.. u.ck
1f466 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 .pVtab = pOp->p4
1f467 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 .pVtab->pVtab;.
1f468 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 20 3d 20 u.ck.pModule =
1f469 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 (sqlite3_module
1f46a 2a 29 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d *)u.ck.pVtab->pM
1f46b 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 6b 2e 6e 41 odule;. u.ck.nA
1f46c 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 rg = pOp->p2;.
1f46d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1f46e 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a ype==P4_VTAB );.
1f46f 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 63 if( ALWAYS(u.c
1f470 6b 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 k.pModule->xUpda
1f471 74 65 29 20 29 7b 0a 20 20 20 20 75 2e 63 6b 2e te) ){. u.ck.
1f472 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 apArg = p->apArg
1f473 3b 0a 20 20 20 20 75 2e 63 6b 2e 70 58 20 3d 20 ;. u.ck.pX =
1f474 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1f475 20 20 20 20 66 6f 72 28 75 2e 63 6b 2e 69 3d 30 for(u.ck.i=0
1f476 3b 20 75 2e 63 6b 2e 69 3c 75 2e 63 6b 2e 6e 41 ; u.ck.i<u.ck.nA
1f477 72 67 3b 20 75 2e 63 6b 2e 69 2b 2b 29 7b 0a 20 rg; u.ck.i++){.
1f478 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f479 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 2e 63 MemStoreType(u.c
1f47a 6b 2e 70 58 29 3b 0a 20 20 20 20 20 20 75 2e 63 k.pX);. u.c
1f47b 6b 2e 61 70 41 72 67 5b 75 2e 63 6b 2e 69 5d 20 k.apArg[u.ck.i]
1f47c 3d 20 75 2e 63 6b 2e 70 58 3b 0a 20 20 20 20 20 = u.ck.pX;.
1f47d 20 75 2e 63 6b 2e 70 58 2b 2b 3b 0a 20 20 20 20 u.ck.pX++;.
1f47e 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1f47f 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1f480 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1f481 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 to_misuse;. r
1f482 63 20 3d 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 c = u.ck.pModule
1f483 2d 3e 78 55 70 64 61 74 65 28 75 2e 63 6b 2e 70 ->xUpdate(u.ck.p
1f484 56 74 61 62 2c 20 75 2e 63 6b 2e 6e 41 72 67 2c Vtab, u.ck.nArg,
1f485 20 75 2e 63 6b 2e 61 70 41 72 67 2c 20 26 75 2e u.ck.apArg, &u.
1f486 63 6b 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 ck.rowid);. s
1f487 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f488 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1f489 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 p->zErrMsg = u
1f48a 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .ck.pVtab->zErrM
1f48b 73 67 3b 0a 20 20 20 20 75 2e 63 6b 2e 70 56 74 sg;. u.ck.pVt
1f48c 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1f48d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1f48e 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1f48f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1f490 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 69 66 28 _misuse;. if(
1f491 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1f492 26 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 & pOp->p1 ){.
1f493 20 20 20 61 73 73 65 72 74 28 20 75 2e 63 6b 2e assert( u.ck.
1f494 6e 41 72 67 3e 31 20 26 26 20 75 2e 63 6b 2e 61 nArg>1 && u.ck.a
1f495 70 41 72 67 5b 30 5d 20 26 26 20 28 75 2e 63 6b pArg[0] && (u.ck
1f496 2e 61 70 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 .apArg[0]->flags
1f497 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 &MEM_Null) );.
1f498 20 20 20 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 db->lastRowi
1f499 64 20 3d 20 75 2e 63 6b 2e 72 6f 77 69 64 3b 0a d = u.ck.rowid;.
1f49a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 }. p->nCh
1f49b 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 ange++;. }. br
1f49c 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1f49d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f49e 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 TUALTABLE */..#i
1f49f 66 6e 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d fndef SQLITE_OM
1f4a0 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 IT_PAGER_PRAGMAS
1f4a1 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 ./* Opcode: Page
1f4a2 63 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 count P1 P2 * *
1f4a3 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 *.**.** Write th
1f4a4 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 e current number
1f4a5 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 of pages in dat
1f4a6 61 62 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f abase P1 to memo
1f4a7 72 79 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 ry cell P2..*/.c
1f4a8 61 73 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 ase OP_Pagecount
1f4a9 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1f4aa 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1f4ab 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1f4ac 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f4ad 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a oved into u.cl *
1f4ae 2f 0a 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e /. int p1;. in
1f4af 74 20 6e 50 61 67 65 3b 0a 20 20 50 61 67 65 72 t nPage;. Pager
1f4b0 20 2a 70 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 *pPager;.#endif
1f4b1 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f4b2 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f4b3 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e 63 6c 2e 70 .cl */.. u.cl.p
1f4b4 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1f4b5 2e 63 6c 2e 70 50 61 67 65 72 20 3d 20 73 71 6c .cl.pPager = sql
1f4b6 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64 ite3BtreePager(d
1f4b7 62 2d 3e 61 44 62 5b 75 2e 63 6c 2e 70 31 5d 2e b->aDb[u.cl.p1].
1f4b8 70 42 74 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c pBt);. rc = sql
1f4b9 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
1f4ba 6e 74 28 75 2e 63 6c 2e 70 50 61 67 65 72 2c 20 nt(u.cl.pPager,
1f4bb 26 75 2e 63 6c 2e 6e 50 61 67 65 29 3b 0a 20 20 &u.cl.nPage);.
1f4bc 2f 2a 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 /* OP_Pagecount
1f4bd 69 73 20 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 is always called
1f4be 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 from within a r
1f4bf 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ead transaction.
1f4c0 20 20 54 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 The. ** page
1f4c1 63 6f 75 6e 74 20 68 61 73 20 61 6c 72 65 61 64 count has alread
1f4c2 79 20 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 y been successfu
1f4c3 6c 6c 79 20 72 65 61 64 20 61 6e 64 20 63 61 63 lly read and cac
1f4c4 68 65 64 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a hed. So the. *
1f4c5 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 * sqlite3PagerPa
1f4c6 67 65 63 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 gecount() call a
1f4c7 62 6f 76 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c bove cannot fail
1f4c8 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 . */. if( ALWAY
1f4c9 53 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 S(rc==SQLITE_OK)
1f4ca 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e ){. pOut->u.
1f4cb 69 20 3d 20 75 2e 63 6c 2e 6e 50 61 67 65 3b 0a i = u.cl.nPage;.
1f4cc 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1f4cd 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
1f4ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1f4cf 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 ./* Opcode: Trac
1f4d0 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a e * * * P4 *.**.
1f4d1 2a 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 73 ** If tracing is
1f4d2 20 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 65 enabled (by the
1f4d3 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 sqlite3_trace()
1f4d4 29 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 ) interface, the
1f4d5 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73 n.** the UTF-8 s
1f4d6 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 tring contained
1f4d7 69 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 64 in P4 is emitted
1f4d8 20 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 61 on the trace ca
1f4d9 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 llback..*/.case
1f4da 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 23 69 66 20 OP_Trace: {.#if
1f4db 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f4dc 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f4dd 20 75 2e 63 6d 20 2a 2f 0a 20 20 63 68 61 72 20 u.cm */. char
1f4de 2a 7a 54 72 61 63 65 3b 0a 23 65 6e 64 69 66 20 *zTrace;.#endif
1f4df 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f4e0 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f4e1 63 6d 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 7a 54 cm */.. u.cm.zT
1f4e2 72 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e race = (pOp->p4.
1f4e3 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 z ? pOp->p4.z :
1f4e4 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 p->zSql);. if(
1f4e5 75 2e 63 6d 2e 7a 54 72 61 63 65 20 29 7b 0a 20 u.cm.zTrace ){.
1f4e6 20 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63 if( db->xTrac
1f4e7 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 e ){. char
1f4e8 2a 7a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 *z = sqlite3Vdbe
1f4e9 45 78 70 61 6e 64 53 71 6c 28 70 2c 20 75 2e 63 ExpandSql(p, u.c
1f4ea 6d 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 20 m.zTrace);.
1f4eb 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e db->xTrace(db->
1f4ec 70 54 72 61 63 65 41 72 67 2c 20 7a 29 3b 0a 20 pTraceArg, z);.
1f4ed 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1f4ee 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 7d ee(db, z);. }
1f4ef 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1f4f0 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 62 EBUG. if( (db
1f4f1 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1f4f2 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 7b _SqlTrace)!=0 ){
1f4f3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 . sqlite3De
1f4f4 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 bugPrintf("SQL-t
1f4f5 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 race: %s\n", u.c
1f4f6 6d 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d m.zTrace);. }
1f4f7 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1f4f8 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 E_DEBUG */. }.
1f4f9 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f4fa 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f .../* Opcode: No
1f4fb 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a op * * * * *.**.
1f4fc 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 ** Do nothing.
1f4fd 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1f4fe 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c is often useful
1f4ff 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 as a jump.** de
1f500 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a stination..*/./*
1f501 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 .** The magic Ex
1f502 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 plain opcode are
1f503 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 only inserted w
1f504 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 hen explain==2 (
1f505 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 which.** is to s
1f506 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c ay when the EXPL
1f507 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 AIN QUERY PLAN s
1f508 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a yntax is used.).
1f509 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 ** This opcode r
1f50a 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 ecords informati
1f50b 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 on from the opti
1f50c 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 mizer. It is th
1f50d 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 e.** the same as
1f50e 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 a no-op. This
1f50f 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 opcodesnever app
1f510 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 ears in a real V
1f511 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 M program..*/.de
1f512 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 fault: {
1f513 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 /* This is rea
1f514 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 lly OP_Noop and
1f515 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 OP_Explain */.
1f516 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a break;.}../*****
1f517 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f518 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f519 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f51a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f51b 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
1f51c 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 cases of the swi
1f51d 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 tch statement ab
1f51e 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 ove this line sh
1f51f 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 ould all be inde
1f520 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 nted.** by 6 spa
1f521 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 ces. But the le
1f522 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 ft-most 6 spaces
1f523 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 have been remov
1f524 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 ed to improve th
1f525 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 e.** readability
1f526 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 . From this poi
1f527 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 nt on down, the
1f528 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 normal indentati
1f529 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 on rules are.**
1f52a 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a restored..******
1f52b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f52c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f52d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f52e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f52f 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a *******/. }..
1f530 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1f531 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 ILE. {.
1f532 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 u64 elapsed = sq
1f533 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 lite3Hwtime() -
1f534 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 start;. pOp
1f535 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 ->cycles += elap
1f536 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e sed;. pOp->
1f537 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 cnt++;.#if 0.
1f538 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
1f539 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 out, "%10llu ",
1f53a 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 elapsed);.
1f53b 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1f53c 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 ntOp(stdout, ori
1f53d 67 50 63 2c 20 26 61 4f 70 5b 6f 72 69 67 50 63 gPc, &aOp[origPc
1f53e 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d ]);.#endif. }
1f53f 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
1f540 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f The following co
1f541 64 65 20 61 64 64 73 20 6e 6f 74 68 69 6e 67 20 de adds nothing
1f542 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 75 to the actual fu
1f543 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20 nctionality.
1f544 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 ** of the progra
1f545 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 68 m. It is only h
1f546 65 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20 ere for testing
1f547 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e 0a 20 and debugging..
1f548 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 ** On the oth
1f549 65 72 20 68 61 6e 64 2c 20 69 74 20 64 6f 65 73 er hand, it does
1f54a 20 62 75 72 6e 20 43 50 55 20 63 79 63 6c 65 73 burn CPU cycles
1f54b 20 65 76 65 72 79 20 74 69 6d 65 20 74 68 72 6f every time thro
1f54c 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 ugh. ** the e
1f54d 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 valuator loop.
1f54e 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 76 65 20 So we can leave
1f54f 69 74 20 6f 75 74 20 77 68 65 6e 20 4e 44 45 42 it out when NDEB
1f550 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 UG is defined..
1f551 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 */.#ifndef ND
1f552 45 42 55 47 0a 20 20 20 20 61 73 73 65 72 74 28 EBUG. assert(
1f553 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 3c 70 2d pc>=-1 && pc<p-
1f554 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 65 66 20 >nOp );..#ifdef
1f555 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1f556 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b if( p->trace ){
1f557 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 30 . if( rc!=0
1f558 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 ) fprintf(p->tr
1f559 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63 ace,"rc=%d\n",rc
1f55a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 );. if( pOp
1f55b 2d 3e 6f 70 66 6c 61 67 73 20 26 20 28 4f 50 46 ->opflags & (OPF
1f55c 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 LG_OUT2_PRERELEA
1f55d 53 45 7c 4f 50 46 4c 47 5f 4f 55 54 32 29 20 29 SE|OPFLG_OUT2) )
1f55e 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74 {. regist
1f55f 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 erTrace(p->trace
1f560 2c 20 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d , pOp->p2, &aMem
1f561 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 [pOp->p2]);.
1f562 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f }. if( pO
1f563 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 p->opflags & OPF
1f564 4c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 LG_OUT3 ){.
1f565 20 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 registerTrace
1f566 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e (p->trace, pOp->
1f567 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 p3, &aMem[pOp->p
1f568 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 3]);. }.
1f569 20 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 }.#endif /* SQ
1f56a 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 LITE_DEBUG */.#e
1f56b 6e 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 ndif /* NDEBUG
1f56c 2a 2f 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 */. } /* The e
1f56d 6e 64 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b nd of the for(;;
1f56e 29 20 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 ) loop the loops
1f56f 20 74 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 through opcodes
1f570 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 */.. /* If we
1f571 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 reach this point
1f572 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 , it means that
1f573 65 78 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e execution is fin
1f574 69 73 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 ished with. **
1f575 61 6e 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 an error of some
1f576 20 6b 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 kind.. */.vdbe
1f577 5f 65 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 _error_halt:. a
1f578 73 73 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 ssert( rc );. p
1f579 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c ->rc = rc;. sql
1f57a 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1f57b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1f57c 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 E_IOERR_NOMEM )
1f57d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1f57e 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c = 1;. rc = SQL
1f57f 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 ITE_ERROR;. if(
1f580 20 72 65 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 resetSchemaOnFa
1f581 75 6c 74 20 29 20 73 71 6c 69 74 65 33 52 65 73 ult ) sqlite3Res
1f582 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 etInternalSchema
1f583 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 (db, 0);.. /* T
1f584 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 his is the only
1f585 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 way out of this
1f586 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 procedure. We h
1f587 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 ave to. ** rele
1f588 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 ase the mutexes
1f589 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77 on btrees that w
1f58a 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20 ere acquired at
1f58b 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f the. ** top. */
1f58c 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 .vdbe_return:.
1f58d 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
1f58e 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e xArrayLeave(&p->
1f58f 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 aMutex);. retur
1f590 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 n rc;.. /* Jump
1f591 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 to here if a st
1f592 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 ring or blob lar
1f593 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f ger than SQLITE_
1f594 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 MAX_LENGTH. **
1f595 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
1f596 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 */.too_big:.
1f597 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1f598 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1f599 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f , "string or blo
1f59a 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 b too big");. r
1f59b 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 c = SQLITE_TOOBI
1f59c 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 G;. goto vdbe_e
1f59d 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1f59e 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 Jump to here if
1f59f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1f5a0 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a s.. */.no_mem:.
1f5a1 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1f5a2 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 ed = 1;. sqlite
1f5a3 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1f5a4 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 ErrMsg, db, "out
1f5a5 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 of memory");.
1f5a6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1f5a7 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 M;. goto vdbe_e
1f5a8 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1f5a9 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f Jump to here fo
1f5aa 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 r an SQLITE_MISU
1f5ab 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 SE error.. */.a
1f5ac 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1f5ad 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 se:. rc = SQLIT
1f5ae 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 E_MISUSE;. /* F
1f5af 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 all thru into ab
1f5b0 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1f5b1 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 */.. /* Jump t
1f5b2 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f o here for any o
1f5b3 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 ther kind of fat
1f5b4 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 al error. The "
1f5b5 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a rc" variable. *
1f5b6 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 * should hold th
1f5b7 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a e error number..
1f5b8 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 */.abort_due_t
1f5b9 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 o_error:. asser
1f5ba 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 t( p->zErrMsg==0
1f5bb 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 );. if( db->ma
1f5bc 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 llocFailed ) rc
1f5bd 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1f5be 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f5bf 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a _IOERR_NOMEM ){.
1f5c0 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1f5c1 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1f5c2 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1f5c3 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a te3ErrStr(rc));.
1f5c4 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f }. goto vdbe_
1f5c5 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f error_halt;.. /
1f5c6 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 * Jump to here i
1f5c7 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e f the sqlite3_in
1f5c8 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65 terrupt() API se
1f5c9 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 ts the interrupt
1f5ca 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f . ** flag.. */
1f5cb 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e .abort_due_to_in
1f5cc 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 terrupt:. asser
1f5cd 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 t( db->u1.isInte
1f5ce 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 rrupted );. rc
1f5cf 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1f5d0 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 PT;. p->rc = rc
1f5d1 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 ;. sqlite3SetSt
1f5d2 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1f5d3 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1f5d4 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a te3ErrStr(rc));.
1f5d5 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f goto vdbe_erro
1f5d6 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a r_halt;.}../****
1f5d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1f5d8 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a f vdbe.c *******
1f5d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f5da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f5db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1f5dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1f5dd 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 file vdbeblob.c
1f5de 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1f5df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1f5e1 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2007 May 1.**.*
1f5e2 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1f5e3 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1f5e4 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1f5e5 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1f5e6 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1f5e7 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1f5e8 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1f5e9 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1f5ea 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1f5eb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1f5ec 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1f5ed 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1f5ee 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1f5ef 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1f5f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1f5f1 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1f5f2 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1f5f3 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1f5f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f5f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f5f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f5f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
1f5f8 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
1f5f9 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f ins code used to
1f5fa 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 implement incre
1f5fb 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e mental BLOB I/O.
1f5fc 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 .*/...#ifndef SQ
1f5fd 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
1f5fe 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 OB../*.** Valid
1f5ff 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 sqlite3_blob* ha
1f600 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 ndles point to I
1f601 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 74 75 72 ncrblob structur
1f602 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 es..*/.typedef s
1f603 74 72 75 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 truct Incrblob I
1f604 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 ncrblob;.struct
1f605 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 Incrblob {. int
1f606 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
1f607 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
1f608 22 66 6c 61 67 73 22 20 70 61 73 73 65 64 20 74 "flags" passed t
1f609 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f o sqlite3_blob_o
1f60a 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e pen() */. int n
1f60b 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 Byte;
1f60c 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 /* Size of op
1f60d 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 en blob, in byte
1f60e 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 s */. int iOffs
1f60f 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
1f610 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 * Byte offset of
1f611 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 blob in cursor
1f612 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 data */. BtCurs
1f613 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 or *pCsr;
1f614 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
1f615 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 ting at blob row
1f616 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 */. sqlite3_st
1f617 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a mt *pStmt; /*
1f618 20 53 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 Statement holdi
1f619 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a ng cursor open *
1f61a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b /. sqlite3 *db;
1f61b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1f61c 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 he associated da
1f61d 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a tabase */.};../*
1f61e 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 .** Open a blob
1f61f 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 handle..*/.SQLIT
1f620 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1f621 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 3_blob_open(. s
1f622 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 20 20 qlite3* db,
1f623 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
1f624 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1f625 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 n */. const cha
1f626 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20 20 2f r *zDb, /
1f627 2a 20 54 68 65 20 61 74 74 61 63 68 65 64 20 64 * The attached d
1f628 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 atabase containi
1f629 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 ng the blob */.
1f62a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 const char *zTa
1f62b 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 ble, /* The
1f62c 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 table containing
1f62d 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 the blob */. c
1f62e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 onst char *zColu
1f62f 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 20 63 6f mn, /* The co
1f630 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 lumn containing
1f631 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 the blob */. sq
1f632 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c lite_int64 iRow,
1f633 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 /* The row
1f634 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1f635 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c glob */. int fl
1f636 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
1f637 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 /* True -> rea
1f638 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2c 20 d/write access,
1f639 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e false -> read-on
1f63a 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ly */. sqlite3_
1f63b 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 blob **ppBlob
1f63c 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 /* Handle for ac
1f63d 63 65 73 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 cessing the blob
1f63e 20 72 65 74 75 72 6e 65 64 20 68 65 72 65 20 2a returned here *
1f63f 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 /.){. int nAtte
1f640 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 mpt = 0;. int i
1f641 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
1f642 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a /* Index of z
1f643 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 Column in row-re
1f644 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 cord */.. /* Th
1f645 69 73 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 is VDBE program
1f646 73 65 65 6b 73 20 61 20 62 74 72 65 65 20 63 75 seeks a btree cu
1f647 72 73 6f 72 20 74 6f 20 74 68 65 20 69 64 65 6e rsor to the iden
1f648 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f tified . ** db/
1f649 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e table/row entry.
1f64a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 The reason for
1f64b 75 73 69 6e 67 20 61 20 76 64 62 65 20 70 72 6f using a vdbe pro
1f64c 67 72 61 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a gram instead. *
1f64d 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 63 6f 64 * of writing cod
1f64e 65 20 74 6f 20 75 73 65 20 74 68 65 20 62 2d 74 e to use the b-t
1f64f 72 65 65 20 6c 61 79 65 72 20 64 69 72 65 63 74 ree layer direct
1f650 6c 79 20 69 73 20 74 68 61 74 20 74 68 65 0a 20 ly is that the.
1f651 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d ** vdbe program
1f652 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e will take advan
1f653 74 61 67 65 20 6f 66 20 74 68 65 20 76 61 72 69 tage of the vari
1f654 6f 75 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ous transaction,
1f655 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e . ** locking an
1f656 64 20 65 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 d error handling
1f657 20 69 6e 66 72 61 73 74 72 75 63 74 75 72 65 20 infrastructure
1f658 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 built into the v
1f659 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 dbe.. **. ** A
1f65a 66 74 65 72 20 73 65 65 6b 69 6e 67 20 74 68 65 fter seeking the
1f65b 20 63 75 72 73 6f 72 2c 20 74 68 65 20 76 64 62 cursor, the vdb
1f65c 65 20 65 78 65 63 75 74 65 73 20 61 6e 20 4f 50 e executes an OP
1f65d 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a _ResultRow.. **
1f65e 20 43 6f 64 65 20 65 78 74 65 72 6e 61 6c 20 74 Code external t
1f65f 6f 20 74 68 65 20 56 64 62 65 20 74 68 65 6e 20 o the Vdbe then
1f660 22 62 6f 72 72 6f 77 73 22 20 74 68 65 20 62 2d "borrows" the b-
1f661 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e 64 0a tree cursor and.
1f662 20 20 2a 2a 20 75 73 65 73 20 69 74 20 74 6f 20 ** uses it to
1f663 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c implement the bl
1f664 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f ob_read(), blob_
1f665 77 72 69 74 65 28 29 20 61 6e 64 20 0a 20 20 2a write() and . *
1f666 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 * blob_bytes() f
1f667 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 unctions.. **.
1f668 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f ** The sqlite3_
1f669 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e blob_close() fun
1f66a 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 ction finalizes
1f66b 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d the vdbe program
1f66c 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f ,. ** which clo
1f66d 73 65 73 20 74 68 65 20 62 2d 74 72 65 65 20 63 ses the b-tree c
1f66e 75 72 73 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 ursor and (possi
1f66f 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 bly) commits the
1f670 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 . ** transacti
1f671 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 on.. */. stati
1f672 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 c const VdbeOpLi
1f673 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 st openBlob[] =
1f674 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 {. {OP_Transa
1f675 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c ction, 0, 0, 0},
1f676 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 /* 0: Start
1f677 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a a transaction *
1f678 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 /. {OP_Verify
1f679 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d Cookie, 0, 0, 0}
1f67a 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b , /* 1: Check
1f67b 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b the schema cook
1f67c 69 65 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 ie */. {OP_Ta
1f67d 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 bleLock, 0, 0, 0
1f67e 7d 2c 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 }, /* 2: A
1f67f 63 71 75 69 72 65 20 61 20 72 65 61 64 20 6f 72 cquire a read or
1f680 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a write lock */..
1f681 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 /* One of th
1f682 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 e following two
1f683 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 73 20 instructions is
1f684 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e 20 4f replaced by an O
1f685 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b P_Noop. */. {
1f686 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 OP_OpenRead, 0,
1f687 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 0, 0}, /*
1f688 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 3: Open cursor
1f689 30 20 66 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0 for reading */
1f68a 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 . {OP_OpenWri
1f68b 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 te, 0, 0, 0},
1f68c 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 /* 4: Open c
1f68d 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 ursor 0 for read
1f68e 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b /write */.. {
1f68f 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 OP_Variable, 1,
1f690 31 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 1, 1}, /*
1f691 20 35 3a 20 50 75 73 68 20 74 68 65 20 72 6f 77 5: Push the row
1f692 69 64 20 74 6f 20 74 68 65 20 73 74 61 63 6b 20 id to the stack
1f693 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 */. {OP_NotEx
1f694 69 73 74 73 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 ists, 0, 9, 1},
1f695 20 20 20 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b /* 6: Seek
1f696 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 the cursor */.
1f697 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 {OP_Column, 0
1f698 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 , 0, 1},
1f699 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b /* 7 */. {
1f69a 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c OP_ResultRow, 1,
1f69b 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 0, 0}, /*
1f69c 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 8 */. {OP_C
1f69d 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 lose, 0, 0, 0},
1f69e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 39 20 20 /* 9
1f69f 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c */. {OP_Halt,
1f6a0 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 0, 0, 0},
1f6a1 20 20 20 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 /* 10 */.
1f6a2 20 7d 3b 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d };.. Vdbe *v =
1f6a3 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 0;. int rc = S
1f6a4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 QLITE_OK;. char
1f6a5 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 54 61 *zErr = 0;. Ta
1f6a6 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 50 61 72 ble *pTab;. Par
1f6a7 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 2a se *pParse;.. *
1f6a8 70 70 42 6c 6f 62 20 3d 20 30 3b 0a 20 20 73 71 ppBlob = 0;. sq
1f6a9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1f6aa 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 r(db->mutex);.
1f6ab 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33 pParse = sqlite3
1f6ac 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 64 62 StackAllocRaw(db
1f6ad 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 , sizeof(*pParse
1f6ae 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 ));. if( pParse
1f6af 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
1f6b0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1f6b1 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e goto blob_open
1f6b2 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b _out;. }. do {
1f6b3 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 72 . memset(pPar
1f6b4 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 se, 0, sizeof(Pa
1f6b5 72 73 65 29 29 3b 0a 20 20 20 20 70 50 61 72 73 rse));. pPars
1f6b6 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 e->db = db;..
1f6b7 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1f6b8 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 tyOn(db) ){.
1f6b9 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f6ba 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 db, zErr);.
1f6bb 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
1f6bc 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 e(db, pParse);.
1f6bd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
1f6be 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 ex_leave(db->mut
1f6bf 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ex);. retur
1f6c0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
1f6c1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 . }.. sqli
1f6c2 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1f6c3 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62 20 3d (db);. pTab =
1f6c4 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 sqlite3LocateTa
1f6c5 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a ble(pParse, 0, z
1f6c6 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20 Table, zDb);.
1f6c7 20 69 66 28 20 70 54 61 62 20 26 26 20 49 73 56 if( pTab && IsV
1f6c8 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a irtual(pTab) ){.
1f6c9 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a pTab = 0;.
1f6ca 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1f6cb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 orMsg(pParse, "c
1f6cc 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 annot open virtu
1f6cd 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a al table: %s", z
1f6ce 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 Table);. }.#i
1f6cf 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f6d0 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28 20 70 T_VIEW. if( p
1f6d1 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 65 Tab && pTab->pSe
1f6d2 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 70 54 lect ){. pT
1f6d3 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 ab = 0;. sq
1f6d4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
1f6d5 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 arse, "cannot op
1f6d6 65 6e 20 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 en view: %s", zT
1f6d7 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e able);. }.#en
1f6d8 64 69 66 0a 20 20 20 20 69 66 28 20 21 70 54 61 dif. if( !pTa
1f6d9 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 b ){. if( p
1f6da 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 Parse->zErrMsg )
1f6db 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1f6dc 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 3DbFree(db, zErr
1f6dd 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 );. zErr
1f6de 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 = pParse->zErrMs
1f6df 67 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 g;. pPars
1f6e0 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a e->zErrMsg = 0;.
1f6e1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
1f6e2 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
1f6e3 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c . (void)sql
1f6e4 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1f6e5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f6e6 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 BtreeLeaveAll(db
1f6e7 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c );. goto bl
1f6e8 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ob_open_out;.
1f6e9 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 }.. /* Now s
1f6ea 65 61 72 63 68 20 70 54 61 62 20 66 6f 72 20 74 earch pTab for t
1f6eb 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e he exact column.
1f6ec 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c */. for(iCol
1f6ed 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d =0; iCol < pTab-
1f6ee 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b >nCol; iCol++) {
1f6ef 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1f6f0 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e e3StrICmp(pTab->
1f6f1 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 aCol[iCol].zName
1f6f2 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b , zColumn)==0 ){
1f6f3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1f6f4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1f6f5 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 if( iCol==pTab
1f6f6 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 ->nCol ){.
1f6f7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1f6f8 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a , zErr);. z
1f6f9 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Err = sqlite3MPr
1f6fa 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 intf(db, "no suc
1f6fb 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 h column: \"%s\"
1f6fc 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 ", zColumn);.
1f6fd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1f6fe 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 RROR;. (voi
1f6ff 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f d)sqlite3SafetyO
1f700 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 ff(db);. sq
1f701 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 lite3BtreeLeaveA
1f702 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f ll(db);. go
1f703 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1f704 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
1f705 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1f706 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 being opened for
1f707 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 writing, check
1f708 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 that the. **
1f709 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e column is not in
1f70a 64 65 78 65 64 2c 20 61 6e 64 20 74 68 61 74 20 dexed, and that
1f70b 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f it is not part o
1f70c 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e f a foreign key.
1f70d 20 0a 20 20 20 20 2a 2a 20 49 74 20 69 73 20 61 . ** It is a
1f70e 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c 65 73 gainst the rules
1f70f 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d to open a colum
1f710 6e 20 74 6f 20 77 68 69 63 68 20 65 69 74 68 65 n to which eithe
1f711 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a r of these. *
1f712 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73 20 61 * descriptions a
1f713 70 70 6c 69 65 73 20 66 6f 72 20 77 72 69 74 69 pplies for writi
1f714 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 ng. */. if(
1f715 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 63 flags ){. c
1f716 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 61 75 6c onst char *zFaul
1f717 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 49 6e 64 t = 0;. Ind
1f718 65 78 20 2a 70 49 64 78 3b 0a 23 69 66 6e 64 65 ex *pIdx;.#ifnde
1f719 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f f SQLITE_OMIT_FO
1f71a 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 20 REIGN_KEY.
1f71b 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 if( db->flags&SQ
1f71c 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 LITE_ForeignKeys
1f71d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 ){. /* C
1f71e 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 63 6f heck that the co
1f71f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 70 61 72 74 lumn is not part
1f720 20 6f 66 20 61 6e 20 46 4b 20 63 68 69 6c 64 20 of an FK child
1f721 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 key definition.
1f722 49 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 It. ** is
1f723 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 not necessary t
1f724 6f 20 63 68 65 63 6b 20 69 66 20 69 74 20 69 73 o check if it is
1f725 20 70 61 72 74 20 6f 66 20 61 20 70 61 72 65 6e part of a paren
1f726 74 20 6b 65 79 2c 20 61 73 20 70 61 72 65 6e 74 t key, as parent
1f727 0a 20 20 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 . ** key
1f728 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 columns must be
1f729 69 6e 64 65 78 65 64 2e 20 54 68 65 20 63 68 65 indexed. The che
1f72a 63 6b 20 62 65 6c 6f 77 20 77 69 6c 6c 20 70 69 ck below will pi
1f72b 63 6b 20 75 70 20 74 68 69 73 20 0a 20 20 20 20 ck up this .
1f72c 20 20 20 20 2a 2a 20 63 61 73 65 2e 20 20 2a 2f ** case. */
1f72d 0a 20 20 20 20 20 20 20 20 46 4b 65 79 20 2a 70 . FKey *p
1f72e 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 66 6f FKey;. fo
1f72f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 r(pFKey=pTab->pF
1f730 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 Key; pFKey; pFKe
1f731 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 y=pFKey->pNextFr
1f732 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 om){. i
1f733 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 nt j;.
1f734 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 for(j=0; j<pFKey
1f735 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 ->nCol; j++){.
1f736 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 46 if( pF
1f737 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 Key->aCol[j].iFr
1f738 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 om==iCol ){.
1f739 20 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c 74 zFault
1f73a 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 22 = "foreign key"
1f73b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1f73c 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1f73d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 }. }.#e
1f73e 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 70 ndif. for(p
1f73f 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 Idx=pTab->pIndex
1f740 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 ; pIdx; pIdx=pId
1f741 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 x->pNext){.
1f742 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 int j;.
1f743 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 for(j=0; j<pId
1f744 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 x->nColumn; j++)
1f745 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1f746 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a pIdx->aiColumn[j
1f747 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 ]==iCol ){.
1f748 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 zFault =
1f749 22 69 6e 64 65 78 65 64 22 3b 0a 20 20 20 20 20 "indexed";.
1f74a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1f74b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1f74c 66 28 20 7a 46 61 75 6c 74 20 29 7b 0a 20 20 20 f( zFault ){.
1f74d 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1f74e 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 ee(db, zErr);.
1f74f 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c zErr = sql
1f750 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 ite3MPrintf(db,
1f751 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 25 73 20 "cannot open %s
1f752 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 column for writi
1f753 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b 0a 20 20 ng", zFault);.
1f754 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1f755 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 E_ERROR;.
1f756 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1f757 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 fetyOff(db);.
1f758 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1f759 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
1f75a 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 goto blob
1f75b 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 _open_out;.
1f75c 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 }. }.. v
1f75d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 = sqlite3VdbeCre
1f75e 61 74 65 28 64 62 29 3b 0a 20 20 20 20 69 66 28 ate(db);. if(
1f75f 20 76 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 v ){. int
1f760 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 iDb = sqlite3Sch
1f761 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 emaToIndex(db, p
1f762 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 Tab->pSchema);.
1f763 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f764 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a AddOpList(v, siz
1f765 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 eof(openBlob)/si
1f766 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29 zeof(VdbeOpList)
1f767 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 , openBlob);.
1f768 20 20 20 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 flags = !!fla
1f769 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
1f76a 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 /* flags = (
1f76b 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 flags ? 1 : 0);
1f76c 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e */.. /* Con
1f76d 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 72 figure the OP_Tr
1f76e 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 ansaction */.
1f76f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f770 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44 62 angeP1(v, 0, iDb
1f771 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f772 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 VdbeChangeP2(v,
1f773 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 0, flags);..
1f774 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 /* Configure t
1f775 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b he OP_VerifyCook
1f776 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 ie */. sqli
1f777 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 te3VdbeChangeP1(
1f778 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 v, 1, iDb);.
1f779 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f77a 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54 61 62 ngeP2(v, 1, pTab
1f77b 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d ->pSchema->schem
1f77c 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 a_cookie);..
1f77d 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 /* Make sure a
1f77e 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f mutex is held o
1f77f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 n the table to b
1f780 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 e accessed */.
1f781 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 sqlite3VdbeU
1f782 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 sesBtree(v, iDb)
1f783 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e ; .. /* Con
1f784 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 61 figure the OP_Ta
1f785 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 bleLock instruct
1f786 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c ion */. sql
1f787 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 ite3VdbeChangeP1
1f788 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 (v, 2, iDb);.
1f789 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f78a 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 70 54 61 angeP2(v, 2, pTa
1f78b 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 b->tnum);.
1f78c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f78d 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 eP3(v, 2, flags)
1f78e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f78f 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 32 dbeChangeP4(v, 2
1f790 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 , pTab->zName, P
1f791 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 4_TRANSIENT);..
1f792 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 /* Remove e
1f793 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 ither the OP_Ope
1f794 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 nWrite or OpenRe
1f795 61 64 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a ad. Set the P2 .
1f796 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 ** paramet
1f797 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 er of the other
1f798 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 to pTab->tnum.
1f799 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1f79a 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 VdbeChangeToNoop
1f79b 28 76 2c 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 (v, 4 - flags, 1
1f79c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f79d 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 VdbeChangeP2(v,
1f79e 33 20 2b 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3 + flags, pTab-
1f79f 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 >tnum);. sq
1f7a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1f7a1 33 28 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 3(v, 3 + flags,
1f7a2 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 iDb);.. /*
1f7a3 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 6e 75 Configure the nu
1f7a4 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e mber of columns.
1f7a5 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 Configure the c
1f7a6 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a ursor to. *
1f7a7 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 * think that the
1f7a8 20 74 61 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d table has one m
1f7a9 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 ore column than
1f7aa 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 it really.
1f7ab 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 ** does. An OP_C
1f7ac 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 olumn to retriev
1f7ad 65 20 74 68 69 73 20 69 6d 61 67 69 6e 61 72 79 e this imaginary
1f7ae 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 column will.
1f7af 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 ** always ret
1f7b0 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e urn an SQL NULL.
1f7b1 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c 20 This is useful
1f7b2 62 65 63 61 75 73 65 20 69 74 20 6d 65 61 6e 73 because it means
1f7b3 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63 61 6e . ** we can
1f7b4 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d invoke OP_Colum
1f7b5 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 n to fill in the
1f7b6 20 76 64 62 65 20 63 75 72 73 6f 72 73 20 74 79 vdbe cursors ty
1f7b7 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 pe . ** and
1f7b8 20 6f 66 66 73 65 74 20 63 61 63 68 65 20 77 69 offset cache wi
1f7b9 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20 61 6e thout causing an
1f7ba 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 y IO.. */.
1f7bb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f7bc 43 68 61 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c ChangeP4(v, 3+fl
1f7bd 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f ags, SQLITE_INT_
1f7be 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f TO_PTR(pTab->nCo
1f7bf 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a l+1),P4_INT32);.
1f7c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f7c1 65 43 68 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 eChangeP2(v, 7,
1f7c2 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 pTab->nCol);.
1f7c3 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c if( !db->mall
1f7c4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1f7c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1f7c6 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c 20 31 akeReady(v, 1, 1
1f7c7 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 , 1, 0, 0, 0);.
1f7c8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1f7c9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1f7ca 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
1f7cb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 rc = sqlite3S
1f7cc 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 afetyOff(db);.
1f7cd 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d if( NEVER(rc!=
1f7ce 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 SQLITE_OK) || db
1f7cf 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1f7d0 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f {. goto blo
1f7d1 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 b_open_out;.
1f7d2 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 }.. sqlite3_b
1f7d3 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 ind_int64((sqlit
1f7d4 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 e3_stmt *)v, 1,
1f7d5 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 iRow);. rc =
1f7d6 73 71 6c 69 74 65 33 5f 73 74 65 70 28 28 73 71 sqlite3_step((sq
1f7d7 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b lite3_stmt *)v);
1f7d8 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1f7d9 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 ITE_ROW ){.
1f7da 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 nAttempt++;.
1f7db 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1f7dc 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 finalize((sqlite
1f7dd 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 3_stmt *)v);.
1f7de 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f7df 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 (db, zErr);.
1f7e0 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 zErr = sqlite3
1f7e1 4d 50 72 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 MPrintf(db, sqli
1f7e2 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b te3_errmsg(db));
1f7e3 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 . v = 0;.
1f7e4 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e }. } while( n
1f7e5 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72 63 3d Attempt<5 && rc=
1f7e6 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 =SQLITE_SCHEMA )
1f7e7 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
1f7e8 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f ITE_ROW ){. /
1f7e9 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 * The row-record
1f7ea 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 has been opened
1f7eb 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 successfully. C
1f7ec 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 heck that the.
1f7ed 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 ** column in q
1f7ee 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 uestion contains
1f7ef 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e text or a blob.
1f7f0 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a If it contains.
1f7f1 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 ** text, it
1f7f2 69 73 20 75 70 20 74 6f 20 74 68 65 20 63 61 6c is up to the cal
1f7f3 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 65 ler to get the e
1f7f4 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 ncoding right..
1f7f5 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c */. Incrbl
1f7f6 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 ob *pBlob;. u
1f7f7 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 32 type = v->apC
1f7f8 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f sr[0]->aType[iCo
1f7f9 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 79 70 l];.. if( typ
1f7fa 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 73 71 e<12 ){. sq
1f7fb 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f7fc 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 zErr);. zEr
1f7fd 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e r = sqlite3MPrin
1f7fe 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f tf(db, "cannot o
1f7ff 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74 79 70 pen value of typ
1f800 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 e %s",.
1f801 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a type==0?"null":
1f802 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a type==7?"real":
1f803 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20 20 20 "integer".
1f804 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 );. rc = S
1f805 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1f806 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 goto blob_ope
1f807 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 n_out;. }.
1f808 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c pBlob = (Incrbl
1f809 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 ob *)sqlite3DbMa
1f80a 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
1f80b 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a eof(Incrblob));.
1f80c 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c if( db->mall
1f80d 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
1f80e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1f80f 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 db, pBlob);.
1f810 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e goto blob_open
1f811 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
1f812 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 pBlob->flags = f
1f813 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d lags;. pBlob-
1f814 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 >pCsr = v->apCs
1f815 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 r[0]->pCursor;.
1f816 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 sqlite3BtreeE
1f817 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 nterCursor(pBlob
1f818 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c ->pCsr);. sql
1f819 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 ite3BtreeCacheOv
1f81a 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 erflow(pBlob->pC
1f81b 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 sr);. sqlite3
1f81c 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 BtreeLeaveCursor
1f81d 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 (pBlob->pCsr);.
1f81e 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 pBlob->pStmt
1f81f 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 = (sqlite3_stmt
1f820 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e *)v;. pBlob->
1f821 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 iOffset = v->apC
1f822 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 sr[0]->aOffset[i
1f823 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d Col];. pBlob-
1f824 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 >nByte = sqlite3
1f825 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1f826 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70 42 6c n(type);. pBl
1f827 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 ob->db = db;.
1f828 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 *ppBlob = (sqli
1f829 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 te3_blob *)pBlob
1f82a 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1f82b 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 E_OK;. }else if
1f82c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1f82d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
1f82e 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a Free(db, zErr);.
1f82f 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 zErr = sqlit
1f830 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e e3MPrintf(db, "n
1f831 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20 25 6c o such rowid: %l
1f832 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 ld", iRow);.
1f833 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1f834 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 R;. }..blob_ope
1f835 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 76 20 26 n_out:. if( v &
1f836 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b & (rc!=SQLITE_OK
1f837 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
1f838 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c iled) ){. sql
1f839 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
1f83a 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 (v);. }. sqlit
1f83b 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 e3Error(db, rc,
1f83c 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 zErr);. sqlite3
1f83d 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 DbFree(db, zErr)
1f83e 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b ;. sqlite3Stack
1f83f 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 Free(db, pParse)
1f840 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1f841 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b ApiExit(db, rc);
1f842 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
1f843 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1f844 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1f845 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
1f846 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 blob handle tha
1f847 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
1f848 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a created using.*
1f849 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f * sqlite3_blob_o
1f84a 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pen()..*/.SQLITE
1f84b 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1f84c 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 _blob_close(sqli
1f84d 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 te3_blob *pBlob)
1f84e 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 {. Incrblob *p
1f84f 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 = (Incrblob *)pB
1f850 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 lob;. int rc;.
1f851 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 sqlite3 *db;..
1f852 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 64 62 if( p ){. db
1f853 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 = p->db;. sq
1f854 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1f855 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 r(db->mutex);.
1f856 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 rc = sqlite3_f
1f857 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 inalize(p->pStmt
1f858 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
1f859 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 Free(db, p);.
1f85a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1f85b 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1f85c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
1f85d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1f85e 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1f85f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 ../*.** Perform
1f860 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 a read or write
1f861 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 operation on a b
1f862 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e lob.*/.static in
1f863 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 t blobReadWrite(
1f864 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 . sqlite3_blob
1f865 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 *pBlob, . void
1f866 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
1f867 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 int iOffset, .
1f868 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 int (*xCall)(Bt
1f869 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 Cursor*, u32, u3
1f86a 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 2, void*).){. i
1f86b 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f nt rc;. Incrblo
1f86c 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 b *p = (Incrblob
1f86d 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 *)pBlob;. Vdbe
1f86e 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a *v;. sqlite3 *
1f86f 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 db;.. if( p==0
1f870 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1f871 4d 49 53 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 MISUSE;. db = p
1f872 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f ->db;. sqlite3_
1f873 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e mutex_enter(db->
1f874 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 mutex);. v = (V
1f875 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a dbe*)p->pStmt;..
1f876 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 if( n<0 || iOf
1f877 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 fset<0 || (iOffs
1f878 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 et+n)>p->nByte )
1f879 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74 {. /* Request
1f87a 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 is out of range
1f87b 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73 . Return a trans
1f87c 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 ient error. */.
1f87d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1f87e 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 RROR;. sqlite
1f87f 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 3Error(db, SQLIT
1f880 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d E_ERROR, 0);. }
1f881 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29 else if( v==0 )
1f882 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 {. /* If ther
1f883 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e e is no statemen
1f884 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 t handle, then t
1f885 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 he blob-handle h
1f886 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 as. ** alread
1f887 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 y been invalidat
1f888 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 ed. Return SQLIT
1f889 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 E_ABORT in this
1f88a 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 case.. */.
1f88b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f rc = SQLITE_ABO
1f88c 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 RT;. }else{.
1f88d 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20 /* Call either
1f88e 42 74 72 65 65 44 61 74 61 28 29 20 6f 72 20 42 BtreeData() or B
1f88f 74 72 65 65 50 75 74 44 61 74 61 28 29 2e 20 49 treePutData(). I
1f890 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 f SQLITE_ABORT i
1f891 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 s. ** returne
1f892 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 d, clean-up the
1f893 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 statement handle
1f894 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
1f895 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 ert( db == v->db
1f896 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 );. sqlite3B
1f897 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 treeEnterCursor(
1f898 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63 p->pCsr);. rc
1f899 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 = xCall(p->pCsr
1f89a 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 , iOffset+p->iOf
1f89b 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 fset, n, z);.
1f89c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1f89d 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 veCursor(p->pCsr
1f89e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1f89f 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 QLITE_ABORT ){.
1f8a0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f8a1 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 Finalize(v);.
1f8a2 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b p->pStmt = 0;
1f8a3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1f8a4 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 db->errCode =
1f8a5 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 rc;. v->rc
1f8a6 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = rc;. }. }.
1f8a7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 rc = sqlite3Ap
1f8a8 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 iExit(db, rc);.
1f8a9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1f8aa 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1f8ab 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1f8ac 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
1f8ad 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e from a blob han
1f8ae 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1f8af 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f8b0 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 lob_read(sqlite3
1f8b1 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f _blob *pBlob, vo
1f8b2 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e id *z, int n, in
1f8b3 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 t iOffset){. re
1f8b4 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 turn blobReadWri
1f8b5 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 te(pBlob, z, n,
1f8b6 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 iOffset, sqlite3
1f8b7 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f BtreeData);.}../
1f8b8 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 *.** Write data
1f8b9 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 to a blob handle
1f8ba 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1f8bb 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1f8bc 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 _write(sqlite3_b
1f8bd 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 lob *pBlob, cons
1f8be 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e t void *z, int n
1f8bf 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a , int iOffset){.
1f8c0 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 return blobRea
1f8c1 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 dWrite(pBlob, (v
1f8c2 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 oid *)z, n, iOff
1f8c3 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 set, sqlite3Btre
1f8c4 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a ePutData);.}../*
1f8c5 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 .** Query a blob
1f8c6 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 handle for the
1f8c7 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
1f8c8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 ..**.** The Incr
1f8c9 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 blob.nByte field
1f8ca 20 69 73 20 66 69 78 65 64 20 66 6f 72 20 74 68 is fixed for th
1f8cb 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 e lifetime of th
1f8cc 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f e Incrblob.** so
1f8cd 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71 no mutex is req
1f8ce 75 69 72 65 64 20 66 6f 72 20 61 63 63 65 73 73 uired for access
1f8cf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1f8d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1f8d1 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 _bytes(sqlite3_b
1f8d2 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 lob *pBlob){. I
1f8d3 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e ncrblob *p = (In
1f8d4 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a crblob *)pBlob;.
1f8d5 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e return p ? p->
1f8d6 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 nByte : 0;.}..#e
1f8d7 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 ndif /* #ifndef
1f8d8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
1f8d9 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a BLOB */../******
1f8da 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1f8db 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a vdbeblob.c *****
1f8dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8de 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1f8df 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1f8e0 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a ile journal.c **
1f8e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8e3 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1f8e4 30 30 37 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 007 August 22.**
1f8e5 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1f8e6 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1f8e7 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1f8e8 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1f8e9 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1f8ea 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1f8eb 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1f8ec 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1f8ed 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1f8ee 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1f8ef 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1f8f0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1f8f1 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1f8f2 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1f8f3 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1f8f4 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1f8f5 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1f8f6 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1f8f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1f8fb 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 ** This file imp
1f8fc 6c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 lements a specia
1f8fd 6c 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 l kind of sqlite
1f8fe 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 3_file object us
1f8ff 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 ed.** by SQLite
1f900 74 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 to create journa
1f901 6c 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 l files if the a
1f902 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
1f903 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 mization.** is e
1f904 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 nabled..**.** Th
1f905 65 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 e distinctive ch
1f906 61 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 aracteristic of
1f907 74 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c this sqlite3_fil
1f908 65 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a e is that the.**
1f909 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 actual on disk
1f90a 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 file is created
1f90b 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 lazily. When the
1f90c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 file is created
1f90d 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 ,.** the caller
1f90e 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 specifies a buff
1f90f 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 er size for an i
1f910 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 n-memory buffer
1f911 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f to.** be used to
1f912 20 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 service read()
1f913 61 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 and write() requ
1f914 65 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c ests. The actual
1f915 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b file.** on disk
1f916 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 is not created
1f917 6f 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 or populated unt
1f918 69 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a il either:.**.**
1f919 20 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 1) The in-mem
1f91a 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 ory representati
1f91b 6f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 on grows too lar
1f91c 67 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 ge for the alloc
1f91d 61 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 ated .** bu
1f91e 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 ffer, or.** 2)
1f91f 20 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 The sqlite3Jour
1f920 6e 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 nalCreate() func
1f921 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
1f922 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1f923 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 _ENABLE_ATOMIC_W
1f924 52 49 54 45 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a RITE.../*.** A J
1f925 6f 75 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 ournalFile objec
1f926 74 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 t is a subclass
1f927 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 of sqlite3_file
1f928 75 73 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e used by.** as an
1f929 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c open file handl
1f92a 65 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 e for journal fi
1f92b 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a les..*/.struct J
1f92c 6f 75 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 ournalFile {. s
1f92d 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
1f92e 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f s *pMethod; /
1f92f 2a 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e * I/O methods on
1f930 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a journal files *
1f931 2f 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 /. int nBuf;
1f932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f933 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a /* Size of z
1f934 42 75 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a Buf[] in bytes *
1f935 2f 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 /. char *zBuf;
1f936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f937 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 /* Space to
1f938 62 75 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 buffer journal w
1f939 72 69 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 rites */. int i
1f93a 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
1f93b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d /* Am
1f93c 6f 75 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 ount of zBuf[] c
1f93d 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f urrently used */
1f93e 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 . int flags;
1f93f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f940 20 20 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 /* xOpen flag
1f941 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 s */. sqlite3_v
1f942 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 fs *pVfs;
1f943 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 /* The "r
1f944 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 eal" underlying
1f945 56 46 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 VFS */. sqlite3
1f946 5f 66 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 _file *pReal;
1f947 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1f948 22 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e "real" underlyin
1f949 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f g file descripto
1f94a 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 r */. const cha
1f94b 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 r *zJournal;
1f94c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
1f94d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
1f94e 6c 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 le */.};.typedef
1f94f 20 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 struct JournalF
1f950 69 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b ile JournalFile;
1f951 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f ../*.** If it do
1f952 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 es not already e
1f953 78 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e xists, create an
1f954 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f d populate the o
1f955 6e 2d 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 n-disk file .**
1f956 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 for JournalFile
1f957 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
1f958 20 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 createFile(Jour
1f959 6e 61 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 nalFile *p){. i
1f95a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1f95b 4b 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 K;. if( !p->pRe
1f95c 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 al ){. sqlite
1f95d 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 3_file *pReal =
1f95e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
1f95f 26 70 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 &p[1];. rc =
1f960 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d sqlite3OsOpen(p-
1f961 3e 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e >pVfs, p->zJourn
1f962 61 6c 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c al, pReal, p->fl
1f963 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 ags, 0);. if(
1f964 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1f965 7b 0a 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c {. p->pReal
1f966 20 3d 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 = pReal;.
1f967 69 66 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 if( p->iSize>0 )
1f968 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1f969 28 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 (p->iSize<=p->nB
1f96a 75 66 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 uf);. rc
1f96b 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
1f96c 28 70 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 (p->pReal, p->zB
1f96d 75 66 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 uf, p->iSize, 0)
1f96e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f96f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1f970 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
1f971 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1f972 74 69 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 tic int jrnlClos
1f973 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
1f974 70 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c pJfd){. Journal
1f975 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e File *p = (Journ
1f976 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 alFile *)pJfd;.
1f977 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b if( p->pReal ){
1f978 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
1f979 6f 73 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 ose(p->pReal);.
1f97a 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
1f97b 65 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 e(p->zBuf);. re
1f97c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1f97d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 }../*.** Read da
1f97e 74 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 ta from the file
1f97f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f980 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 jrnlRead(. sqli
1f981 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 te3_file *pJfd,
1f982 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /* The journa
1f983 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 l file from whic
1f984 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 h to read */. v
1f985 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 oid *zBuf,
1f986 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 /* Put the
1f987 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f results here */
1f988 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 . int iAmt,
1f989 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1f98a 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
1f98b 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 read */. sqlite
1f98c 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 _int64 iOfst
1f98d 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e /* Begin readin
1f98e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 g at this offset
1f98f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 */.){. int rc
1f990 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a = SQLITE_OK;. J
1f991 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1f992 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1f993 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 Jfd;. if( p->pR
1f994 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 eal ){. rc =
1f995 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d sqlite3OsRead(p-
1f996 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 >pReal, zBuf, iA
1f997 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 mt, iOfst);. }e
1f998 6c 73 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f lse if( (iAmt+iO
1f999 66 73 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b fst)>p->iSize ){
1f99a 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1f99b 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
1f99c 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 D;. }else{.
1f99d 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d memcpy(zBuf, &p-
1f99e 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 >zBuf[iOfst], iA
1f99f 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 mt);. }. retur
1f9a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 n rc;.}../*.** W
1f9a1 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
1f9a2 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1f9a3 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a int jrnlWrite(.
1f9a4 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1f9a5 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 pJfd, /* The
1f9a6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 journal file int
1f9a7 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 o which to write
1f9a8 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1f9a9 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 *zBuf, /*
1f9aa 54 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 Take data to be
1f9ab 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 written from her
1f9ac 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c e */. int iAmt,
1f9ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f9ae 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1f9af 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 to write */. s
1f9b0 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 qlite_int64 iOfs
1f9b1 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 t /* Begin w
1f9b2 72 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f riting at this o
1f9b3 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 ffset into the f
1f9b4 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ile */.){. int
1f9b5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f9b6 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1f9b7 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1f9b8 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 *)pJfd;. if( !p
1f9b9 2d 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 ->pReal && (iOfs
1f9ba 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 t+iAmt)>p->nBuf
1f9bb 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 ){. rc = crea
1f9bc 74 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 teFile(p);. }.
1f9bd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f9be 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d OK ){. if( p-
1f9bf 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 >pReal ){.
1f9c0 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
1f9c1 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 ite(p->pReal, zB
1f9c2 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 uf, iAmt, iOfst)
1f9c3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1f9c4 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 memcpy(&p->zB
1f9c5 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c uf[iOfst], zBuf,
1f9c6 20 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 iAmt);. if
1f9c7 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 ( p->iSize<(iOfs
1f9c8 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 t+iAmt) ){.
1f9c9 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 p->iSize = (i
1f9ca 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 Ofst+iAmt);.
1f9cb 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1f9cc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1f9cd 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 .** Truncate the
1f9ce 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1f9cf 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 int jrnlTruncat
1f9d0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
1f9d1 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 pJfd, sqlite_int
1f9d2 36 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 64 size){. int
1f9d3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1f9d4 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 JournalFile *p
1f9d5 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 = (JournalFile
1f9d6 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d *)pJfd;. if( p-
1f9d7 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 >pReal ){. rc
1f9d8 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e = sqlite3OsTrun
1f9d9 63 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 cate(p->pReal, s
1f9da 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ize);. }else if
1f9db 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 ( size<p->iSize
1f9dc 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 ){. p->iSize
1f9dd 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 = size;. }. re
1f9de 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1f9df 2a 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e * Sync the file.
1f9e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a .*/.static int j
1f9e1 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f rnlSync(sqlite3_
1f9e2 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 file *pJfd, int
1f9e3 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 flags){. int rc
1f9e4 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 ;. JournalFile
1f9e5 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c *p = (JournalFil
1f9e6 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 e *)pJfd;. if(
1f9e7 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 p->pReal ){.
1f9e8 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 rc = sqlite3OsSy
1f9e9 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 nc(p->pReal, fla
1f9ea 67 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 gs);. }else{.
1f9eb 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1f9ec 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1f9ed 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 c;.}../*.** Quer
1f9ee 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 y the size of th
1f9ef 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e e file in bytes.
1f9f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a .*/.static int j
1f9f1 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 rnlFileSize(sqli
1f9f2 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 te3_file *pJfd,
1f9f3 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 sqlite_int64 *pS
1f9f4 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ize){. int rc =
1f9f5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f SQLITE_OK;. Jo
1f9f6 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f9f7 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f9f8 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 fd;. if( p->pRe
1f9f9 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 al ){. rc = s
1f9fa 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
1f9fb 28 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 (p->pReal, pSize
1f9fc 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1f9fd 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 *pSize = (sqlite
1f9fe 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 _int64) p->iSize
1f9ff 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1fa00 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c c;.}../*.** Tabl
1fa01 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 e of methods for
1fa02 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c JournalFile sql
1fa03 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 ite3_file object
1fa04 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
1fa05 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ct sqlite3_io_me
1fa06 74 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c thods JournalFil
1fa07 65 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 eMethods = {. 1
1fa08 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
1fa09 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a iVersion */. j
1fa0a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a rnlClose, /*
1fa0b 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e xClose */. jrn
1fa0c 6c 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 lRead, /* x
1fa0d 52 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 Read */. jrnlWr
1fa0e 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 ite, /* xWri
1fa0f 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e te */. jrnlTrun
1fa10 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 cate, /* xTrunc
1fa11 61 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e ate */. jrnlSyn
1fa12 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 c, /* xSync
1fa13 20 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 */. jrnlFileSi
1fa14 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a ze, /* xFileSiz
1fa15 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 e */. 0,
1fa16 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a /* xLock *
1fa17 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 /. 0,
1fa18 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f /* xUnlock */
1fa19 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1fa1a 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
1fa1b 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 vedLock */. 0,
1fa1c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1fa1d 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 FileControl */.
1fa1e 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fa1f 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a /* xSectorSize *
1fa20 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 /. 0
1fa21 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 /* xDeviceCha
1fa22 72 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a racteristics */.
1fa23 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 };../* .** Open
1fa24 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a a journal file..
1fa25 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1fa26 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 E int sqlite3Jou
1fa27 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 rnalOpen(. sqli
1fa28 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 te3_vfs *pVfs,
1fa29 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 /* The VF
1fa2a 53 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 S to use for act
1fa2b 75 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a ual file I/O */.
1fa2c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
1fa2d 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ame, /*
1fa2e 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 Name of the jour
1fa2f 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 nal file */. sq
1fa30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1fa31 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 , /* Prea
1fa32 6c 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 llocated, blank
1fa33 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 file handle */.
1fa34 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
1fa35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1fa36 70 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a pening flags */.
1fa37 20 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 int nBuf
1fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1fa39 42 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62 Bytes buffered b
1fa3a 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 efore opening th
1fa3b 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a e file */.){. J
1fa3c 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 ournalFile *p =
1fa3d 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 (JournalFile *)p
1fa3e 4a 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c Jfd;. memset(p,
1fa3f 20 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 0, sqlite3Journ
1fa40 61 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 alSize(pVfs));.
1fa41 20 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 if( nBuf>0 ){.
1fa42 20 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c p->zBuf = sql
1fa43 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e ite3MallocZero(n
1fa44 42 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 Buf);. if( !p
1fa45 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 ->zBuf ){.
1fa46 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1fa47 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c MEM;. }. }el
1fa48 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 se{. return s
1fa49 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
1fa4a 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 s, zName, pJfd,
1fa4b 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 flags, 0);. }.
1fa4c 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a p->pMethod = &J
1fa4d 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 ournalFileMethod
1fa4e 73 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e s;. p->nBuf = n
1fa4f 42 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 Buf;. p->flags
1fa50 3d 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a = flags;. p->zJ
1fa51 6f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a ournal = zName;.
1fa52 20 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 p->pVfs = pVfs
1fa53 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1fa54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 E_OK;.}../*.** I
1fa55 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 f the argument p
1fa56 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 points to a Jou
1fa57 72 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 rnalFile structu
1fa58 72 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 re, and the unde
1fa59 72 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 rlying.** file h
1fa5a 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 as not yet been
1fa5b 63 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 created, create
1fa5c 69 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 it now..*/.SQLIT
1fa5d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1fa5e 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 lite3JournalCrea
1fa5f 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
1fa60 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d *p){. if( p->pM
1fa61 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c ethods!=&Journal
1fa62 46 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 FileMethods ){.
1fa63 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1fa64 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 _OK;. }. retur
1fa65 6e 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f n createFile((Jo
1fa66 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a urnalFile *)p);.
1fa67 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e }../* .** Return
1fa68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
1fa69 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f ytes required to
1fa6a 20 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c store a Journal
1fa6b 46 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 File that uses v
1fa6c 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 fs.** pVfs to cr
1fa6d 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 eate the underly
1fa6e 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 ing on-disk file
1fa6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
1fa70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1fa71 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 JournalSize(sqli
1fa72 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a te3_vfs *pVfs){.
1fa73 20 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e return (pVfs->
1fa74 73 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 szOsFile+sizeof(
1fa75 4a 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d JournalFile));.}
1fa76 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
1fa77 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
1fa78 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a journal.c ******
1fa79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fa7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fa7b 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1fa7c 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1fa7d 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 ile memjournal.c
1fa7e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1fa7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fa80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1fa81 30 30 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 008 October 7.**
1fa82 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1fa83 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1fa84 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1fa85 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1fa86 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1fa87 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1fa88 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1fa89 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1fa8a 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1fa8b 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1fa8c 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1fa8d 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1fa8e 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1fa8f 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1fa90 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1fa91 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1fa92 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1fa93 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1fa94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fa95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fa96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fa97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1fa98 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1fa99 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 tains code use t
1fa9a 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 o implement an i
1fa9b 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 n-memory rollbac
1fa9c 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 k journal..** Th
1fa9d 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c e in-memory roll
1fa9e 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 back journal is
1fa9f 75 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 used to journal
1faa0 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 transactions for
1faa1 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 .** ":memory:" d
1faa2 61 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 atabases and whe
1faa3 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f n the journal_mo
1faa4 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 de=MEMORY pragma
1faa5 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 0a 2f 2a is used..*/../*
1faa6 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
1faa7 63 65 73 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 ces to internal
1faa8 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 structures */.ty
1faa9 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
1faaa 4a 6f 75 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e Journal MemJourn
1faab 61 6c 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 al;.typedef stru
1faac 63 74 20 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c ct FilePoint Fil
1faad 65 50 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 ePoint;.typedef
1faae 73 74 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b struct FileChunk
1faaf 20 46 69 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 FileChunk;../*
1fab0 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
1fab1 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
1fab2 61 6c 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 al is allocated
1fab3 69 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 in increments of
1fab4 0a 2a 2a 20 74 68 69 73 20 6d 61 6e 79 20 62 79 .** this many by
1fab5 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 tes..**.** The s
1fab6 69 7a 65 20 63 68 6f 73 65 6e 20 69 73 20 61 20 ize chosen is a
1fab7 6c 69 74 74 6c 65 20 6c 65 73 73 20 74 68 61 6e little less than
1fab8 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e a power of two.
1fab9 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 74 That way,.** t
1faba 68 65 20 46 69 6c 65 43 68 75 6e 6b 20 6f 62 6a he FileChunk obj
1fabb 65 63 74 20 77 69 6c 6c 20 68 61 76 65 20 61 20 ect will have a
1fabc 73 69 7a 65 20 74 68 61 74 20 61 6c 6d 6f 73 74 size that almost
1fabd 20 65 78 61 63 74 6c 79 20 66 69 6c 6c 73 0a 2a exactly fills.*
1fabe 2a 20 61 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f * a power-of-two
1fabf 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 allocation. Th
1fac0 69 73 20 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73 is mimimizes was
1fac1 74 65 64 20 73 70 61 63 65 20 69 6e 20 70 6f 77 ted space in pow
1fac2 65 72 2d 6f 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d er-of-two.** mem
1fac3 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a ory allocators..
1fac4 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e */.#define JOURN
1fac5 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 28 28 69 AL_CHUNKSIZE ((i
1fac6 6e 74 29 28 31 30 32 34 2d 73 69 7a 65 6f 66 28 nt)(1024-sizeof(
1fac7 46 69 6c 65 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f FileChunk*)))../
1fac8 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 * Macro to find
1fac9 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 the minimum of t
1faca 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 wo numeric value
1facb 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 s..*/.#ifndef MI
1facc 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 N.# define MIN(x
1facd 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 ,y) ((x)<(y)?(x)
1face 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a :(y)).#endif../*
1facf 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b .** The rollback
1fad0 20 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70 journal is comp
1fad1 6f 73 65 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 osed of a linked
1fad2 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 73 list of these s
1fad3 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 tructures..*/.st
1fad4 72 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b ruct FileChunk {
1fad5 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e . FileChunk *pN
1fad6 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
1fad7 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b /* Next chunk
1fad8 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
1fad9 2a 2f 0a 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a */. u8 zChunk[J
1fada 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1fadb 5d 3b 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 ]; /* Content
1fadc 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f of this chunk */
1fadd 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e .};../*.** An in
1fade 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
1fadf 62 6a 65 63 74 20 73 65 72 76 65 73 20 61 73 20 bject serves as
1fae0 61 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 a cursor into th
1fae1 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
1fae2 61 6c 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f al..** The curso
1fae3 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 r can be either
1fae4 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 for reading or w
1fae5 72 69 74 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 riting..*/.struc
1fae6 74 20 46 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 t FilePoint {.
1fae7 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f sqlite3_int64 iO
1fae8 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1fae9 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 /* Offset from t
1faea 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
1faeb 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 the file */. Fi
1faec 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b leChunk *pChunk;
1faed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1faee 20 53 70 65 63 69 66 69 63 20 63 68 75 6e 6b 20 Specific chunk
1faef 69 6e 74 6f 20 77 68 69 63 68 20 63 75 72 73 6f into which curso
1faf0 72 20 70 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a r points */.};..
1faf1 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 75 62 63 6c /*.** This subcl
1faf2 61 73 73 20 69 73 20 61 20 73 75 62 63 6c 61 73 ass is a subclas
1faf3 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c s of sqlite3_fil
1faf4 65 2e 20 20 45 61 63 68 20 6f 70 65 6e 20 6d 65 e. Each open me
1faf5 6d 6f 72 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 mory-journal.**
1faf6 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
1faf7 66 20 74 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f f this class..*/
1faf8 0a 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e .struct MemJourn
1faf9 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 al {. sqlite3_i
1fafa 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 o_methods *pMeth
1fafb 6f 64 3b 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 od; /* Parent
1fafc 20 63 6c 61 73 73 2e 20 4d 55 53 54 20 42 45 20 class. MUST BE
1fafd 46 49 52 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43 FIRST */. FileC
1fafe 68 75 6e 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 hunk *pFirst;
1faff 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 /* He
1fb00 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 ad of in-memory
1fb01 63 68 75 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 chunk-list */.
1fb02 46 69 6c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69 FilePoint endpoi
1fb03 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nt;
1fb04 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
1fb05 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c e end of the fil
1fb06 65 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 e */. FilePoint
1fb07 20 72 65 61 64 70 6f 69 6e 74 3b 20 20 20 20 20 readpoint;
1fb08 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1fb09 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 r to the end of
1fb0a 74 68 65 20 6c 61 73 74 20 78 52 65 61 64 28 29 the last xRead()
1fb0b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
1fb0c 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 ad data from the
1fb0d 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
1fb0e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 al file. This i
1fb0f 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
1fb10 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 tion.** of the s
1fb11 71 6c 69 74 65 33 5f 76 66 73 2e 78 52 65 61 64 qlite3_vfs.xRead
1fb12 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 method..*/.stat
1fb13 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 ic int memjrnlRe
1fb14 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 ad(. sqlite3_fi
1fb15 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 le *pJfd, /*
1fb16 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
1fb17 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 from which to r
1fb18 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a ead */. void *z
1fb19 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
1fb1a 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c /* Put the resul
1fb1b 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 ts here */. int
1fb1c 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 iAmt,
1fb1d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1fb1e 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a bytes to read *
1fb1f 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 /. sqlite_int64
1fb20 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 iOfst /* Be
1fb21 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74 gin reading at t
1fb22 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b his offset */.){
1fb23 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 . MemJournal *p
1fb24 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a = (MemJournal *
1fb25 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 )pJfd;. u8 *zOu
1fb26 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 t = zBuf;. int
1fb27 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 nRead = iAmt;.
1fb28 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 int iChunkOffset
1fb29 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 ;. FileChunk *p
1fb2a 43 68 75 6e 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c Chunk;.. /* SQL
1fb2b 69 74 65 20 6e 65 76 65 72 20 74 72 69 65 73 20 ite never tries
1fb2c 74 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65 to read past the
1fb2d 20 65 6e 64 20 6f 66 20 61 20 72 6f 6c 6c 62 61 end of a rollba
1fb2e 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ck journal file
1fb2f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 4f 66 */. assert( iOf
1fb30 73 74 2b 69 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 st+iAmt<=p->endp
1fb31 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a oint.iOffset );.
1fb32 0a 20 20 69 66 28 20 70 2d 3e 72 65 61 64 70 6f . if( p->readpo
1fb33 69 6e 74 2e 69 4f 66 66 73 65 74 21 3d 69 4f 66 int.iOffset!=iOf
1fb34 73 74 20 7c 7c 20 69 4f 66 73 74 3d 3d 30 20 29 st || iOfst==0 )
1fb35 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e {. sqlite3_in
1fb36 74 36 34 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20 t64 iOff = 0;.
1fb37 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e for(pChunk=p->
1fb38 70 46 69 72 73 74 3b 20 0a 20 20 20 20 20 20 20 pFirst; .
1fb39 20 41 4c 57 41 59 53 28 70 43 68 75 6e 6b 29 20 ALWAYS(pChunk)
1fb3a 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c && (iOff+JOURNAL
1fb3b 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 _CHUNKSIZE)<=iOf
1fb3c 73 74 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75 st;. pChu
1fb3d 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 nk=pChunk->pNext
1fb3e 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 4f . ){. iO
1fb3f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 ff += JOURNAL_CH
1fb40 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 UNKSIZE;. }.
1fb41 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 }else{. pChu
1fb42 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e nk = p->readpoin
1fb43 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 t.pChunk;. }..
1fb44 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 iChunkOffset =
1fb45 28 69 6e 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 (int)(iOfst%JOUR
1fb46 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a NAL_CHUNKSIZE);.
1fb47 20 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 do {. int i
1fb48 53 70 61 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f Space = JOURNAL_
1fb49 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 CHUNKSIZE - iChu
1fb4a 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 6e nkOffset;. in
1fb4b 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 t nCopy = MIN(nR
1fb4c 65 61 64 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 ead, (JOURNAL_CH
1fb4d 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b UNKSIZE - iChunk
1fb4e 4f 66 66 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 Offset));. me
1fb4f 6d 63 70 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 mcpy(zOut, &pChu
1fb50 6e 6b 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e nk->zChunk[iChun
1fb51 6b 4f 66 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 kOffset], nCopy)
1fb52 3b 0a 20 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 ;. zOut += nC
1fb53 6f 70 79 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d opy;. nRead -
1fb54 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 69 43 = iSpace;. iC
1fb55 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a hunkOffset = 0;.
1fb56 20 20 7d 20 77 68 69 6c 65 28 20 6e 52 65 61 64 } while( nRead
1fb57 3e 3d 30 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 >=0 && (pChunk=p
1fb58 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 Chunk->pNext)!=0
1fb59 20 26 26 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 && nRead>0 );.
1fb5a 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f p->readpoint.iO
1fb5b 66 66 73 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 ffset = iOfst+iA
1fb5c 6d 74 3b 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 mt;. p->readpoi
1fb5d 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 nt.pChunk = pChu
1fb5e 6e 6b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 nk;.. return SQ
1fb5f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1fb60 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 * Write data to
1fb61 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 the file..*/.sta
1fb62 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 tic int memjrnlW
1fb63 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
1fb64 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f file *pJfd, /
1fb65 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
1fb66 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f le into which to
1fb67 20 77 72 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 write */. cons
1fb68 74 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 t void *zBuf,
1fb69 20 20 20 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 /* Take data
1fb6a 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 66 72 to be written fr
1fb6b 6f 6d 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 om here */. int
1fb6c 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 iAmt,
1fb6d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1fb6e 20 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 bytes to write
1fb6f 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 */. sqlite_int6
1fb70 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 4 iOfst /* B
1fb71 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 egin writing at
1fb72 74 68 69 73 20 6f 66 66 73 65 74 20 69 6e 74 6f this offset into
1fb73 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a the file */.){.
1fb74 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 MemJournal *p
1fb75 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 = (MemJournal *)
1fb76 70 4a 66 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 pJfd;. int nWri
1fb77 74 65 20 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 te = iAmt;. u8
1fb78 2a 7a 57 72 69 74 65 20 3d 20 28 75 38 20 2a 29 *zWrite = (u8 *)
1fb79 7a 42 75 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 zBuf;.. /* An i
1fb7a 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
1fb7b 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c file should onl
1fb7c 79 20 65 76 65 72 20 62 65 20 61 70 70 65 6e 64 y ever be append
1fb7d 65 64 20 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 ed to. Random.
1fb7e 2a 2a 20 61 63 63 65 73 73 20 77 72 69 74 65 73 ** access writes
1fb7f 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65 are not require
1fb80 64 20 62 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a d by sqlite.. *
1fb81 2f 0a 20 20 61 73 73 65 72 74 28 20 69 4f 66 73 /. assert( iOfs
1fb82 74 3d 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 t==p->endpoint.i
1fb83 4f 66 66 73 65 74 20 29 3b 0a 20 20 55 4e 55 53 Offset );. UNUS
1fb84 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 4f 66 ED_PARAMETER(iOf
1fb85 73 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e st);.. while( n
1fb86 57 72 69 74 65 3e 30 20 29 7b 0a 20 20 20 20 46 Write>0 ){. F
1fb87 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b ileChunk *pChunk
1fb88 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 = p->endpoint.p
1fb89 43 68 75 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69 Chunk;. int i
1fb8a 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 ChunkOffset = (i
1fb8b 6e 74 29 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e nt)(p->endpoint.
1fb8c 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f iOffset%JOURNAL_
1fb8d 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20 CHUNKSIZE);.
1fb8e 69 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e int iSpace = MIN
1fb8f 28 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c (nWrite, JOURNAL
1fb90 5f 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 _CHUNKSIZE - iCh
1fb91 75 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 unkOffset);..
1fb92 20 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 if( iChunkOffse
1fb93 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a t==0 ){. /*
1fb94 20 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65 New chunk is re
1fb95 71 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64 quired to extend
1fb96 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 the file. */.
1fb97 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 FileChunk *p
1fb98 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 New = sqlite3_ma
1fb99 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 lloc(sizeof(File
1fb9a 43 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 Chunk));. i
1fb9b 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 f( !pNew ){.
1fb9c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1fb9d 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 E_IOERR_NOMEM;.
1fb9e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 }. pNe
1fb9f 77 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 w->pNext = 0;.
1fba0 20 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29 if( pChunk )
1fba1 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1fba2 28 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 ( p->pFirst );.
1fba3 20 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 pChunk->p
1fba4 4e 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 Next = pNew;.
1fba5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fba6 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46 assert( !p->pF
1fba7 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 irst );.
1fba8 70 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77 p->pFirst = pNew
1fba9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1fbaa 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 p->endpoint.pChu
1fbab 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d nk = pNew;. }
1fbac 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d .. memcpy(&p-
1fbad 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b >endpoint.pChunk
1fbae 2d 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f ->zChunk[iChunkO
1fbaf 66 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 ffset], zWrite,
1fbb0 69 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 iSpace);. zWr
1fbb1 69 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 ite += iSpace;.
1fbb2 20 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 nWrite -= iSp
1fbb3 61 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 ace;. p->endp
1fbb4 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 oint.iOffset +=
1fbb5 69 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 iSpace;. }.. r
1fbb6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1fbb7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 .}../*.** Trunca
1fbb8 74 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a te the file..*/.
1fbb9 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 static int memjr
1fbba 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 nlTruncate(sqlit
1fbbb 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1fbbc 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 qlite_int64 size
1fbbd 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 ){. MemJournal
1fbbe 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c *p = (MemJournal
1fbbf 20 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 *)pJfd;. FileC
1fbc0 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 hunk *pChunk;.
1fbc1 61 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b assert(size==0);
1fbc2 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
1fbc3 54 45 52 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 TER(size);. pCh
1fbc4 75 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b unk = p->pFirst;
1fbc5 0a 20 20 77 68 69 6c 65 28 20 70 43 68 75 6e 6b . while( pChunk
1fbc6 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e ){. FileChun
1fbc7 6b 20 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b k *pTmp = pChunk
1fbc8 3b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 ;. pChunk = p
1fbc9 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 Chunk->pNext;.
1fbca 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
1fbcb 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Tmp);. }. sqli
1fbcc 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 te3MemJournalOpe
1fbcd 6e 28 70 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 n(pJfd);. retur
1fbce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1fbcf 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 /*.** Close the
1fbd0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1fbd1 69 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 int memjrnlClose
1fbd2 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1fbd3 4a 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 Jfd){. memjrnlT
1fbd4 72 75 6e 63 61 74 65 28 70 4a 66 64 2c 20 30 29 runcate(pJfd, 0)
1fbd5 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1fbd6 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
1fbd7 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a Sync the file..*
1fbd8 2a 0a 2a 2a 20 53 79 6e 63 69 6e 67 20 61 6e 20 *.** Syncing an
1fbd9 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 in-memory journa
1fbda 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 l is a no-op. A
1fbdb 6e 64 2c 20 69 6e 20 66 61 63 74 2c 20 74 68 69 nd, in fact, thi
1fbdc 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 s routine.** is
1fbdd 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 6e 20 never called in
1fbde 61 20 77 6f 72 6b 69 6e 67 20 69 6d 70 6c 65 6d a working implem
1fbdf 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 entation. This
1fbe0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
1fbe1 2a 20 65 78 69 73 74 73 20 70 75 72 65 6c 79 20 * exists purely
1fbe2 61 73 20 61 20 63 6f 6e 74 69 6e 67 65 6e 63 79 as a contingency
1fbe3 2c 20 69 6e 20 63 61 73 65 20 73 6f 6d 65 20 6d , in case some m
1fbe4 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e 20 73 6f alfunction in so
1fbe5 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 61 72 74 me other.** part
1fbe6 20 6f 66 20 53 51 4c 69 74 65 20 63 61 75 73 65 of SQLite cause
1fbe7 73 20 53 79 6e 63 20 74 6f 20 62 65 20 63 61 6c s Sync to be cal
1fbe8 6c 65 64 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a led by mistake..
1fbe9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
1fbea 6d 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 mjrnlSync(sqlite
1fbeb 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 3_file *NotUsed,
1fbec 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 20 int NotUsed2){
1fbed 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 /*NO_TEST*/.
1fbee 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1fbef 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
1fbf0 65 64 32 29 3b 20 20 20 20 20 20 20 20 20 20 20 ed2);
1fbf1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e /*N
1fbf2 4f 5f 54 45 53 54 2a 2f 0a 20 20 61 73 73 65 72 O_TEST*/. asser
1fbf3 74 28 20 30 20 29 3b 20 20 20 20 20 20 20 20 20 t( 0 );
1fbf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbf6 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 /*NO_TES
1fbf7 54 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c T*/. return SQL
1fbf8 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
1fbf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbfb 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d /*NO_TEST*/.}
1fbfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1fc00 4e 4f 5f 54 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a NO_TEST*/../*.**
1fc01 20 51 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 Query the size
1fc02 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 of the file in b
1fc03 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ytes..*/.static
1fc04 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 int memjrnlFileS
1fc05 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ize(sqlite3_file
1fc06 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 *pJfd, sqlite_i
1fc07 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 nt64 *pSize){.
1fc08 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 MemJournal *p =
1fc09 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a (MemJournal *)pJ
1fc0a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 fd;. *pSize = (
1fc0b 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 70 2d sqlite_int64) p-
1fc0c 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 >endpoint.iOffse
1fc0d 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 t;. return SQLI
1fc0e 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1fc0f 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 Table of methods
1fc10 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 for MemJournal
1fc11 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a sqlite3_file obj
1fc12 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ect..*/.static s
1fc13 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f truct sqlite3_io
1fc14 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 _methods MemJour
1fc15 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 nalMethods = {.
1fc16 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
1fc17 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a /* iVersion *
1fc18 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 /. memjrnlClose
1fc19 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 , /* xClose
1fc1a 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 */. memjrnlRead
1fc1b 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 , /* xRead
1fc1c 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 */. memjrnlWrit
1fc1d 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 e, /* xWrite
1fc1e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 */. memjrnlTru
1fc1f 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e ncate, /* xTrun
1fc20 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e cate */. memjrn
1fc21 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 lSync, /* x
1fc22 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e Sync */. memjrn
1fc23 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 lFileSize, /* x
1fc24 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c FileSize */. 0,
1fc25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc26 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c /* xLock */. 0,
1fc27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc28 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 /* xUnlock */.
1fc29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fc2a 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
1fc2b 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 vedLock */. 0,
1fc2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fc2d 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a * xFileControl *
1fc2e 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 /. 0,
1fc2f 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 /* xSector
1fc30 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 Size */. 0
1fc31 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1fc32 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
1fc33 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 stics */.};../*
1fc34 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e .** Open a journ
1fc35 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 al file..*/.SQLI
1fc36 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1fc37 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
1fc38 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 lOpen(sqlite3_fi
1fc39 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d le *pJfd){. Mem
1fc3a 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 Journal *p = (Me
1fc3b 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b mJournal *)pJfd;
1fc3c 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 . assert( EIGHT
1fc3d 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 _BYTE_ALIGNMENT(
1fc3e 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 p) );. memset(p
1fc3f 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a , 0, sqlite3MemJ
1fc40 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 ournalSize());.
1fc41 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d p->pMethod = &M
1fc42 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 emJournalMethods
1fc43 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1fc44 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 69 n true if the fi
1fc45 6c 65 2d 68 61 6e 64 6c 65 20 70 61 73 73 65 64 le-handle passed
1fc46 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 as an argument
1fc47 69 73 20 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d is .** an in-mem
1fc48 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a ory journal .*/.
1fc49 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fc4a 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a nt sqlite3IsMemJ
1fc4b 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 ournal(sqlite3_f
1fc4c 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 ile *pJfd){. re
1fc4d 74 75 72 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 turn pJfd->pMeth
1fc4e 6f 64 73 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c ods==&MemJournal
1fc4f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a Methods;.}../* .
1fc50 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1fc51 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
1fc52 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 quired to store
1fc53 61 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 a MemJournal tha
1fc54 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 t uses vfs.** pV
1fc55 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 fs to create the
1fc56 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 underlying on-d
1fc57 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 isk files..*/.SQ
1fc58 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1fc59 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
1fc5a 61 6c 53 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 alSize(void){.
1fc5b 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 return sizeof(Me
1fc5c 6d 4a 6f 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a mJournal);.}../*
1fc5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1fc5e 64 20 6f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e d of memjournal.
1fc5f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1fc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1fc62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1fc63 67 69 6e 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e gin file walker.
1fc64 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1fc65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1fc67 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 .** 2008 August
1fc68 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 16.**.** The aut
1fc69 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1fc6a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1fc6b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1fc6c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1fc6d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1fc6e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1fc6f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1fc70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1fc71 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1fc72 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1fc73 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1fc74 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1fc75 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1fc76 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1fc77 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1fc78 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1fc79 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1fc7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc7e 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
1fc7f 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 ontains routines
1fc80 20 75 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e used for walkin
1fc81 67 20 74 68 65 20 70 61 72 73 65 72 20 74 72 65 g the parser tre
1fc82 65 20 66 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 e for.** an SQL
1fc83 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 0a 0a statement..*/...
1fc84 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 /*.** Walk an ex
1fc85 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 pression tree.
1fc86 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 Invoke the callb
1fc87 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 ack once for eac
1fc88 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 h node.** of the
1fc89 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 expression, whi
1fc8a 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 le decending. (
1fc8b 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
1fc8c 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 the callback.**
1fc8d 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 is invoked befor
1fc8e 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64 e visiting child
1fc8f 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ren.).**.** The
1fc90 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
1fc91 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 m the callback s
1fc92 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 hould be one of
1fc93 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e the WRC_*.** con
1fc94 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66 stants to specif
1fc95 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 y how to proceed
1fc96 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a with the walk..
1fc97 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e **.** WRC_Con
1fc98 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69 tinue Conti
1fc99 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64 nue descending d
1fc9a 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a own the tree..**
1fc9b 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65 .** WRC_Prune
1fc9c 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 Do not
1fc9d 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 descend into chi
1fc9e 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 ld nodes. But a
1fc9f 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 llow.**
1fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 the
1fca1 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 walk to continu
1fca2 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e e with sibling n
1fca3 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 odes..**.** W
1fca4 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20 RC_Abort
1fca5 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c Do no more call
1fca6 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 backs. Unwind t
1fca7 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 he stack and.**
1fca8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fca9 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 20 return the
1fcaa 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 top-level walk c
1fcab 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 all..**.** The r
1fcac 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d eturn value from
1fcad 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1fcae 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 WRC_Abort to ab
1fcaf 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 andon the tree w
1fcb0 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 alk.** and WRC_C
1fcb1 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 ontinue to conti
1fcb2 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nue..*/.SQLITE_P
1fcb3 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1fcb4 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 e3WalkExpr(Walke
1fcb5 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
1fcb6 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 *pExpr){. int
1fcb7 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d rc;. if( pExpr=
1fcb8 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f =0 ) return WRC_
1fcb9 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 74 Continue;. test
1fcba 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f case( ExprHasPro
1fcbb 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1fcbc 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 TokenOnly) );.
1fcbd 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 61 testcase( ExprHa
1fcbe 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
1fcbf 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a EP_Reduced) );.
1fcc0 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e rc = pWalker->
1fcc1 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 xExprCallback(pW
1fcc2 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 alker, pExpr);.
1fcc3 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e if( rc==WRC_Con
1fcc4 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20 tinue.
1fcc5 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73 41 && !ExprHasA
1fcc6 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 nyProperty(pExpr
1fcc7 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 ,EP_TokenOnly) )
1fcc8 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 {. if( sqlite
1fcc9 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1fcca 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 r, pExpr->pLeft)
1fccb 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fccc 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c ort;. if( sql
1fccd 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1fcce 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 lker, pExpr->pRi
1fccf 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 ght) ) return WR
1fcd0 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 C_Abort;. if(
1fcd1 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1fcd2 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
1fcd3 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 lect) ){. i
1fcd4 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 f( sqlite3WalkSe
1fcd5 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 lect(pWalker, pE
1fcd6 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 xpr->x.pSelect)
1fcd7 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fcd8 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 rt;. }else{.
1fcd9 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fcda 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 WalkExprList(pWa
1fcdb 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 lker, pExpr->x.p
1fcdc 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 List) ) return W
1fcdd 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a RC_Abort;. }.
1fcde 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }. return rc
1fcdf 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a & WRC_Abort;.}..
1fce0 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 /*.** Call sqlit
1fce1 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 e3WalkExpr() for
1fce2 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f every expressio
1fce3 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 n in list p or u
1fce4 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 ntil.** an abort
1fce5 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65 6e request is seen
1fce6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1fce7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
1fce8 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b alkExprList(Walk
1fce9 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1fcea 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 rList *p){. int
1fceb 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 i;. struct Exp
1fcec 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
1fced 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 m;. if( p ){.
1fcee 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 for(i=p->nExpr
1fcef 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e , pItem=p->a; i>
1fcf0 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 0; i--, pItem++)
1fcf1 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
1fcf2 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1fcf3 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 ker, pItem->pExp
1fcf4 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f r) ) return WRC_
1fcf5 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d Abort;. }. }
1fcf6 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f . return WRC_Co
1fcf7 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ntinue;.}../*.**
1fcf8 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 Walk all expres
1fcf9 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 sions associated
1fcfa 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 with SELECT sta
1fcfb 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a tement p. Do.**
1fcfc 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 not invoke the
1fcfd 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 SELECT callback
1fcfe 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 on p, but do (of
1fcff 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a course) invoke.
1fd00 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c ** any expr call
1fd01 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 backs and SELECT
1fd02 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 callbacks that
1fd03 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 come from subque
1fd04 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 ries..** Return
1fd05 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 WRC_Abort or WRC
1fd06 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 _Continue..*/.SQ
1fd07 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1fd08 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
1fd09 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 ctExpr(Walker *p
1fd0a 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a Walker, Select *
1fd0b 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 p){. if( sqlite
1fd0c 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1fd0d 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 alker, p->pEList
1fd0e 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fd0f 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 bort;. if( sqli
1fd10 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c te3WalkExpr(pWal
1fd11 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 ker, p->pWhere)
1fd12 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fd13 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1fd14 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1fd15 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 alker, p->pGroup
1fd16 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 By) ) return WRC
1fd17 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 _Abort;. if( sq
1fd18 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 lite3WalkExpr(pW
1fd19 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e alker, p->pHavin
1fd1a 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f g) ) return WRC_
1fd1b 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1fd1c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 ite3WalkExprList
1fd1d 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 (pWalker, p->pOr
1fd1e 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 derBy) ) return
1fd1f 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 WRC_Abort;. if(
1fd20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
1fd21 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 (pWalker, p->pLi
1fd22 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 mit) ) return WR
1fd23 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1fd24 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1fd25 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 Walker, p->pOffs
1fd26 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 et) ) return WRC
1fd27 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e _Abort;. return
1fd28 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1fd29 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 ../*.** Walk the
1fd2a 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73 73 parse trees ass
1fd2b 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c ociated with all
1fd2c 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 subqueries in t
1fd2d 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 he.** FROM claus
1fd2e 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 e of SELECT stat
1fd2f 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 ement p. Do not
1fd30 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 invoke the sele
1fd31 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f ct.** callback o
1fd32 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f n p, but do invo
1fd33 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 ke it on each FR
1fd34 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 OM clause subque
1fd35 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 ry.** and on any
1fd36 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72 74 subqueries furt
1fd37 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 her down in the
1fd38 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a tree. Return .*
1fd39 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 * WRC_Abort or W
1fd3a 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a RC_Continue;.*/.
1fd3b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fd3c 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 nt sqlite3WalkSe
1fd3d 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 lectFrom(Walker
1fd3e 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 *pWalker, Select
1fd3f 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 *p){. SrcList
1fd40 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a *pSrc;. int i;.
1fd41 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
1fd42 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 _item *pItem;..
1fd43 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b pSrc = p->pSrc;
1fd44 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 . if( ALWAYS(pS
1fd45 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 rc) ){. for(i
1fd46 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 =pSrc->nSrc, pIt
1fd47 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b em=pSrc->a; i>0;
1fd48 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i--, pItem++){.
1fd49 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1fd4a 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 3WalkSelect(pWal
1fd4b 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c ker, pItem->pSel
1fd4c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 ect) ){.
1fd4d 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fd4e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1fd4f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 }. return WRC
1fd50 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f _Continue;.} ../
1fd51 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 *.** Call sqlite
1fd52 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 3WalkExpr() for
1fd53 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e every expression
1fd54 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 in Select state
1fd55 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b ment p..** Invok
1fd56 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c e sqlite3WalkSel
1fd57 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 ect() for subque
1fd58 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d ries in the FROM
1fd59 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f clause and.** o
1fd5a 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 n the compound s
1fd5b 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e elect chain, p->
1fd5c 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 pPrior..**.** Re
1fd5d 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 turn WRC_Continu
1fd5e 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 e under normal c
1fd5f 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 onditions. Retu
1fd60 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a rn WRC_Abort if.
1fd61 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 ** there is an a
1fd62 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a bort request..**
1fd63 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 .** If the Walke
1fd64 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 r does not have
1fd65 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 an xSelectCallba
1fd66 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 ck() then this r
1fd67 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e outine.** is a n
1fd68 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 o-op returning W
1fd69 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a RC_Continue..*/.
1fd6a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1fd6b 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 nt sqlite3WalkSe
1fd6c 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 lect(Walker *pWa
1fd6d 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1fd6e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 {. int rc;. if
1fd6f 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 ( p==0 || pWalke
1fd70 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 r->xSelectCallba
1fd71 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 ck==0 ) return W
1fd72 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 RC_Continue;. r
1fd73 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 c = WRC_Continue
1fd74 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b ;. while( p ){
1fd75 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 . rc = pWalke
1fd76 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 r->xSelectCallba
1fd77 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a ck(pWalker, p);.
1fd78 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65 if( rc ) bre
1fd79 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ak;. if( sqli
1fd7a 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
1fd7b 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 r(pWalker, p) )
1fd7c 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fd7d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1fd7e 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 3WalkSelectFrom(
1fd7f 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 pWalker, p) ) re
1fd80 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1fd81 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f p = p->pPrio
1fd82 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 r;. }. return
1fd83 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a rc & WRC_Abort;.
1fd84 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
1fd85 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 ** End of walker
1fd86 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1fd87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd89 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1fd8a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65 ** Begin file re
1fd8b 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a solve.c ********
1fd8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fd8e 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 */./*.** 2008 Au
1fd8f 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 gust 18.**.** Th
1fd90 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1fd91 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1fd92 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1fd93 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1fd94 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1fd95 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1fd96 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1fd97 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1fd98 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1fd99 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1fd9a 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1fd9b 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1fd9c 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1fd9d 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1fd9e 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1fd9f 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1fda0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1fda1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fda2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fda3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fda4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fda5 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1fda6 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1fda7 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f routines used fo
1fda8 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 r walking the pa
1fda9 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a rser tree and.**
1fdaa 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 resolve all ide
1fdab 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73 6f ntifiers by asso
1fdac 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74 ciating them wit
1fdad 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a h a particular.*
1fdae 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 * table and colu
1fdaf 6d 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 mn..*/../*.** Tu
1fdb0 72 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70 rn the pExpr exp
1fdb1 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 ression into an
1fdb2 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43 alias for the iC
1fdb3 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 ol-th column of
1fdb4 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
1fdb5 74 20 69 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a t in pEList..**.
1fdb6 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 ** If the result
1fdb7 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 set column is a
1fdb8 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 simple column r
1fdb9 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 74 eference, then t
1fdba 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d his routine.** m
1fdbb 61 6b 65 73 20 61 6e 20 65 78 61 63 74 20 63 6f akes an exact co
1fdbc 70 79 2e 20 20 42 75 74 20 66 6f 72 20 61 6e 79 py. But for any
1fdbd 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 65 other kind of e
1fdbe 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a xpression, this.
1fdbf 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 ** routine make
1fdc0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 a copy of the re
1fdc1 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20 sult set column
1fdc2 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 as the argument
1fdc3 74 6f 20 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 to the.** TK_AS
1fdc4 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 54 operator. The T
1fdc5 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 63 61 K_AS operator ca
1fdc6 75 73 65 73 20 74 68 65 20 65 78 70 72 65 73 73 uses the express
1fdc7 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61 ion to be.** eva
1fdc8 6c 75 61 74 65 64 20 6a 75 73 74 20 6f 6e 63 65 luated just once
1fdc9 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64 and then reused
1fdca 20 66 6f 72 20 65 61 63 68 20 61 6c 69 61 73 2e for each alias.
1fdcb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f .**.** The reaso
1fdcc 6e 20 66 6f 72 20 73 75 70 70 72 65 73 73 69 6e n for suppressin
1fdcd 67 20 74 68 65 20 54 4b 5f 41 53 20 74 65 72 6d g the TK_AS term
1fdce 20 77 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 when the expres
1fdcf 73 69 6f 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 sion is a simple
1fdd0 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 .** column refer
1fdd1 65 6e 63 65 20 69 73 20 73 6f 20 74 68 61 74 20 ence is so that
1fdd2 74 68 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 the column refer
1fdd3 65 6e 63 65 20 77 69 6c 6c 20 62 65 20 72 65 63 ence will be rec
1fdd4 6f 67 6e 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73 ognized as.** us
1fdd5 61 62 6c 65 20 62 79 20 69 6e 64 69 63 65 73 20 able by indices
1fdd6 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45 within the WHERE
1fdd7 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 clause processi
1fdd8 6e 67 20 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a ng logic. .**.**
1fdd9 20 48 61 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41 Hack: The TK_A
1fdda 53 20 6f 70 65 72 61 74 6f 72 20 69 73 20 69 6e S operator is in
1fddb 68 69 62 69 74 65 64 20 69 66 20 7a 54 79 70 65 hibited if zType
1fddc 5b 30 5d 3d 3d 27 47 27 2e 20 20 54 68 69 73 20 [0]=='G'. This
1fddd 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e means.** that in
1fdde 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 a GROUP BY clau
1fddf 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 se, the expressi
1fde0 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20 on is evaluated
1fde1 74 77 69 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a twice. Hence:.*
1fde2 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 *.** SELECT
1fde3 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c random()%5 AS x,
1fde4 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 count(*) FROM t
1fde5 61 62 20 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a ab GROUP BY x.**
1fde6 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e .** Is equivalen
1fde7 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 t to:.**.**
1fde8 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 SELECT random()%
1fde9 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 5 AS x, count(*)
1fdea 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 FROM tab GROUP
1fdeb 42 59 20 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a BY random()%5.**
1fdec 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f .** The result o
1fded 66 20 72 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20 f random()%5 in
1fdee 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 the GROUP BY cla
1fdef 75 73 65 20 69 73 20 70 72 6f 62 61 62 6c 79 20 use is probably
1fdf0 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f different.** fro
1fdf1 6d 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 m the result in
1fdf2 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2e 20 the result-set.
1fdf3 20 57 65 20 6d 69 67 68 74 20 66 69 78 20 74 68 We might fix th
1fdf4 69 73 20 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a is someday. Or.
1fdf5 2a 2a 20 74 68 65 6e 20 61 67 61 69 6e 2c 20 77 ** then again, w
1fdf6 65 20 6d 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a e might not....*
1fdf7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 /.static void re
1fdf8 73 6f 6c 76 65 41 6c 69 61 73 28 0a 20 20 50 61 solveAlias(. Pa
1fdf9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1fdfa 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
1fdfb 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
1fdfc 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 rList *pEList,
1fdfd 20 20 20 20 2f 2a 20 41 20 72 65 73 75 6c 74 20 /* A result
1fdfe 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f set */. int iCo
1fdff 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
1fe00 2f 2a 20 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 /* A column in t
1fe01 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 he result set.
1fe02 30 2e 2e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 0..pEList->nExpr
1fe03 2d 31 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 -1 */. Expr *pE
1fe04 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f xpr, /
1fe05 2a 20 54 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 * Transform this
1fe06 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 into an alias t
1fe07 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 o the result set
1fe08 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1fe09 20 2a 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20 *zType /*
1fe0a 22 47 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 "GROUP" or "ORDE
1fe0b 52 22 20 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 R" or "" */.){.
1fe0c 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20 Expr *pOrig;
1fe0d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 /* The i
1fe0e 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 Col-th column of
1fe0f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1fe10 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 75 70 3b */. Expr *pDup;
1fe11 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1fe12 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a opy of pOrig */.
1fe13 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
1fe14 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1fe15 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1fe16 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ion */.. assert
1fe17 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f ( iCol>=0 && iCo
1fe18 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 l<pEList->nExpr
1fe19 29 3b 0a 20 20 70 4f 72 69 67 20 3d 20 70 45 4c );. pOrig = pEL
1fe1a 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 ist->a[iCol].pEx
1fe1b 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f pr;. assert( pO
1fe1c 72 69 67 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 rig!=0 );. asse
1fe1d 72 74 28 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73 rt( pOrig->flags
1fe1e 20 26 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 29 & EP_Resolved )
1fe1f 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1fe20 3e 64 62 3b 0a 20 20 69 66 28 20 70 4f 72 69 67 >db;. if( pOrig
1fe21 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 ->op!=TK_COLUMN
1fe22 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d 27 47 27 && zType[0]!='G'
1fe23 20 29 7b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 ){. pDup = s
1fe24 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
1fe25 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 , pOrig, 0);.
1fe26 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 50 pDup = sqlite3P
1fe27 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f Expr(pParse, TK_
1fe28 41 53 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b AS, pDup, 0, 0);
1fe29 0a 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 . if( pDup==0
1fe2a 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 ) return;. i
1fe2b 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f f( pEList->a[iCo
1fe2c 6c 5d 2e 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0a l].iAlias==0 ){.
1fe2d 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b pEList->a[
1fe2e 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28 iCol].iAlias = (
1fe2f 75 31 36 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e u16)(++pParse->n
1fe30 41 6c 69 61 73 29 3b 0a 20 20 20 20 7d 0a 20 20 Alias);. }.
1fe31 20 20 70 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d pDup->iTable =
1fe32 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d pEList->a[iCol]
1fe33 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 65 6c 73 65 .iAlias;. }else
1fe34 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
1fe35 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f 49 erty(pOrig, EP_I
1fe36 6e 74 56 61 6c 75 65 29 20 7c 7c 20 70 4f 72 69 ntValue) || pOri
1fe37 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 29 g->u.zToken==0 )
1fe38 7b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c {. pDup = sql
1fe39 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
1fe3a 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 69 pOrig, 0);. i
1fe3b 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 f( pDup==0 ) ret
1fe3c 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
1fe3d 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d char *zToken =
1fe3e 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e pOrig->u.zToken
1fe3f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 ;. assert( zT
1fe40 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 70 oken!=0 );. p
1fe41 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d Orig->u.zToken =
1fe42 20 30 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 0;. pDup = s
1fe43 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
1fe44 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 , pOrig, 0);.
1fe45 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e pOrig->u.zToken
1fe46 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 = zToken;. i
1fe47 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 f( pDup==0 ) ret
1fe48 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 urn;. assert(
1fe49 20 28 70 44 75 70 2d 3e 66 6c 61 67 73 20 26 20 (pDup->flags &
1fe4a 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 (EP_Reduced|EP_T
1fe4b 6f 6b 65 6e 4f 6e 6c 79 29 29 3d 3d 30 20 29 3b okenOnly))==0 );
1fe4c 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 . pDup->flags
1fe4d 32 20 7c 3d 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 2 |= EP2_Malloce
1fe4e 64 54 6f 6b 65 6e 3b 0a 20 20 20 20 70 44 75 70 dToken;. pDup
1fe4f 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c ->u.zToken = sql
1fe50 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
1fe51 20 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 zToken);. }.
1fe52 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 if( pExpr->flags
1fe53 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 & EP_ExpCollate
1fe54 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e 70 43 ){. pDup->pC
1fe55 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f oll = pExpr->pCo
1fe56 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c ll;. pDup->fl
1fe57 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c ags |= EP_ExpCol
1fe58 6c 61 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 late;. }.. /*
1fe59 42 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 Before calling s
1fe5a 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1fe5b 28 29 2c 20 73 65 74 20 74 68 65 20 45 50 5f 53 (), set the EP_S
1fe5c 74 61 74 69 63 20 66 6c 61 67 2e 20 54 68 69 73 tatic flag. This
1fe5d 20 0a 20 20 2a 2a 20 70 72 65 76 65 6e 74 73 20 . ** prevents
1fe5e 45 78 70 72 44 65 6c 65 74 65 28 29 20 66 72 6f ExprDelete() fro
1fe5f 6d 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 45 m deleting the E
1fe60 78 70 72 20 73 74 72 75 63 74 75 72 65 20 69 74 xpr structure it
1fe61 73 65 6c 66 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 77 self,. ** allow
1fe62 69 6e 67 20 69 74 20 74 6f 20 62 65 20 72 65 70 ing it to be rep
1fe63 6f 70 75 6c 61 74 65 64 20 62 79 20 74 68 65 20 opulated by the
1fe64 6d 65 6d 63 70 79 28 29 20 6f 6e 20 74 68 65 20 memcpy() on the
1fe65 66 6f 6c 6c 6f 77 69 6e 67 20 6c 69 6e 65 2e 0a following line..
1fe66 20 20 2a 2f 0a 20 20 45 78 70 72 53 65 74 50 72 */. ExprSetPr
1fe67 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1fe68 5f 53 74 61 74 69 63 29 3b 0a 20 20 73 71 6c 69 _Static);. sqli
1fe69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1fe6a 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63 , pExpr);. memc
1fe6b 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c 20 py(pExpr, pDup,
1fe6c 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29 3b sizeof(*pExpr));
1fe6d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1fe6e 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f (db, pDup);.}../
1fe6f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e *.** Given the n
1fe70 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 ame of a column
1fe71 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e of the form X.Y.
1fe72 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 Z or Y.Z or just
1fe73 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 Z, look up.** t
1fe74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 hat name in the
1fe75 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74 61 set of source ta
1fe76 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73 74 bles in pSrcList
1fe77 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70 45 and make the pE
1fe78 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 xpr .** expressi
1fe79 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62 61 on node refer ba
1fe7a 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72 63 ck to that sourc
1fe7b 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 e column. The f
1fe7c 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 ollowing changes
1fe7d 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 .** are made to
1fe7e 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 pExpr:.**.**
1fe7f 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20 20 pExpr->iDb
1fe80 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e 64 Set the ind
1fe81 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 ex in db->aDb[]
1fe82 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1fe83 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 X.**
1fe84 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 76 (ev
1fe85 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c 69 en if X is impli
1fe86 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 ed)..** pExpr
1fe87 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20 20 ->iTable
1fe88 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73 6f Set to the curso
1fe89 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 r number for the
1fe8a 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a table obtained.
1fe8b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
1fe8c 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20 from
1fe8d 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 pSrcList..**
1fe8e 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20 20 pExpr->pTab
1fe8f 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 Points to t
1fe90 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 he Table structu
1fe91 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e 20 re of X.Y (even
1fe92 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 if.**
1fe93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20 X
1fe94 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d 70 and/or Y are imp
1fe95 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78 lied.).** pEx
1fe96 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20 pr->iColumn
1fe97 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f 6c Set to the col
1fe98 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 umn number withi
1fe99 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 n the table..**
1fe9a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 pExpr->op
1fe9b 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 54 Set to T
1fe9c 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 K_COLUMN..**
1fe9d 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20 20 pExpr->pLeft
1fe9e 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 Any express
1fe9f 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 ion this points
1fea0 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a to is deleted.**
1fea1 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 pExpr->pRigh
1fea2 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78 70 t Any exp
1fea3 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 ression this poi
1fea4 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74 65 nts to is delete
1fea5 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 44 62 d..**.** The zDb
1fea6 20 76 61 72 69 61 62 6c 65 20 69 73 20 74 68 65 variable is the
1fea7 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
1fea8 61 62 61 73 65 20 28 74 68 65 20 22 58 22 29 2e abase (the "X").
1fea9 20 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61 79 This value may
1feaa 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e be.** NULL mean
1feab 69 6e 67 20 74 68 61 74 20 6e 61 6d 65 20 69 73 ing that name is
1feac 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a of the form Y.Z
1fead 20 6f 72 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 or Z. Any avai
1feae 6c 61 62 6c 65 20 64 61 74 61 62 61 73 65 0a 2a lable database.*
1feaf 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 20 20 * can be used.
1feb0 54 68 65 20 7a 54 61 62 6c 65 20 76 61 72 69 61 The zTable varia
1feb1 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ble is the name
1feb2 6f 66 20 74 68 65 20 74 61 62 6c 65 20 28 74 68 of the table (th
1feb3 65 20 22 59 22 29 2e 20 20 54 68 69 73 0a 2a 2a e "Y"). This.**
1feb4 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 4e 55 value can be NU
1feb5 4c 4c 20 69 66 20 7a 44 62 20 69 73 20 61 6c 73 LL if zDb is als
1feb6 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 7a 54 61 62 o NULL. If zTab
1feb7 6c 65 20 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a le is NULL it.**
1feb8 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
1feb9 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65 form of the name
1feba 20 69 73 20 5a 20 61 6e 64 20 74 68 61 74 20 63 is Z and that c
1febb 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20 olumns from any
1febc 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20 table.** can be
1febd 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 used..**.** If t
1febe 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62 he name cannot b
1febf 65 20 72 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62 e resolved unamb
1fec0 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20 iguously, leave
1fec1 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1fec2 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 20 61 6e .** in pParse an
1fec3 64 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f d return WRC_Abo
1fec4 72 74 2e 20 20 52 65 74 75 72 6e 20 57 52 43 5f rt. Return WRC_
1fec5 50 72 75 6e 65 20 6f 6e 20 73 75 63 63 65 73 73 Prune on success
1fec6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1fec7 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 lookupName(. Pa
1fec8 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1fec9 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e /* The parsin
1feca 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 g context */. c
1fecb 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 onst char *zDb,
1fecc 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1fecd 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 he database cont
1fece 61 69 6e 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 aining table, or
1fecf 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 NULL */. const
1fed0 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 20 20 20 char *zTab,
1fed1 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 /* Name of table
1fed2 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 containing colu
1fed3 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 mn, or NULL */.
1fed4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f const char *zCo
1fed5 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 l, /* Name of
1fed6 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a the column. */.
1fed7 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
1fed8 4e 43 2c 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 NC, /* The na
1fed9 6d 65 20 63 6f 6e 74 65 78 74 20 75 73 65 64 20 me context used
1feda 74 6f 20 72 65 73 6f 6c 76 65 20 74 68 65 20 6e to resolve the n
1fedb 61 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ame */. Expr *p
1fedc 45 78 70 72 20 20 20 20 20 20 20 20 20 20 2f 2a Expr /*
1fedd 20 4d 61 6b 65 20 74 68 69 73 20 45 58 50 52 20 Make this EXPR
1fede 6e 6f 64 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 node point to th
1fedf 65 20 73 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d e selected colum
1fee0 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c n */.){. int i,
1fee1 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f j; /
1fee2 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 * Loop counters
1fee3 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 */. int cnt = 0
1fee4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1fee5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1fee6 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c of matching col
1fee7 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 umn names */. i
1fee8 6e 74 20 63 6e 74 54 61 62 20 3d 20 30 3b 20 20 nt cntTab = 0;
1fee9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1feea 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 /* Number of ma
1feeb 74 63 68 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d tching table nam
1feec 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 es */. sqlite3
1feed 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
1feee 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
1feef 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1fef0 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 tion */. struct
1fef1 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1fef2 49 74 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 Item; /* U
1fef3 73 65 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f se for looping o
1fef4 76 65 72 20 70 53 72 63 4c 69 73 74 20 69 74 65 ver pSrcList ite
1fef5 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 ms */. struct S
1fef6 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4d 61 rcList_item *pMa
1fef7 74 63 68 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 tch = 0; /* The
1fef8 20 6d 61 74 63 68 69 6e 67 20 70 53 72 63 4c 69 matching pSrcLi
1fef9 73 74 20 69 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d st item */. Nam
1fefa 65 43 6f 6e 74 65 78 74 20 2a 70 54 6f 70 4e 43 eContext *pTopNC
1fefb 20 3d 20 70 4e 43 3b 20 20 20 20 20 20 20 20 2f = pNC; /
1fefc 2a 20 46 69 72 73 74 20 6e 61 6d 65 63 6f 6e 74 * First namecont
1fefd 65 78 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20 ext in the list
1fefe 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 */. Schema *pSc
1feff 68 65 6d 61 20 3d 20 30 3b 20 20 20 20 20 20 20 hema = 0;
1ff00 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 /* Schema
1ff01 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
1ff02 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 72 on */. int isTr
1ff03 69 67 67 65 72 20 3d 20 30 3b 0a 0a 20 20 61 73 igger = 0;.. as
1ff04 73 65 72 74 28 20 70 4e 43 20 29 3b 20 20 20 20 sert( pNC );
1ff05 20 2f 2a 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e /* the name con
1ff06 74 65 78 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e text cannot be N
1ff07 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ULL. */. assert
1ff08 28 20 7a 43 6f 6c 20 29 3b 20 20 20 20 2f 2a 20 ( zCol ); /*
1ff09 54 68 65 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 The Z in X.Y.Z c
1ff0a 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f annot be NULL */
1ff0b 0a 20 20 61 73 73 65 72 74 28 20 7e 45 78 70 72 . assert( ~Expr
1ff0c 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
1ff0d 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e Expr, EP_TokenOn
1ff0e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 ly|EP_Reduced) )
1ff0f 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ;.. /* Initiali
1ff10 7a 65 20 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e ze the node to n
1ff11 6f 2d 6d 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 o-match */. pEx
1ff12 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b pr->iTable = -1;
1ff13 0a 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d . pExpr->pTab =
1ff14 20 30 3b 0a 20 20 45 78 70 72 53 65 74 49 72 72 0;. ExprSetIrr
1ff15 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b educible(pExpr);
1ff16 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 .. /* Start at
1ff17 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 the inner-most c
1ff18 6f 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 ontext and move
1ff19 6f 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 outward until a
1ff1a 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a match is found *
1ff1b 2f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 /. while( pNC &
1ff1c 26 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 & cnt==0 ){.
1ff1d 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
1ff1e 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 ;. SrcList *p
1ff1f 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 SrcList = pNC->p
1ff20 53 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 SrcList;.. if
1ff21 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 ( pSrcList ){.
1ff22 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 for(i=0, pIt
1ff23 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 em=pSrcList->a;
1ff24 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 i<pSrcList->nSrc
1ff25 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1ff26 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a . Table *
1ff27 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e pTab;. in
1ff28 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 t iDb;. C
1ff29 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a olumn *pCol;. .
1ff2a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
1ff2b 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 Item->pTab;.
1ff2c 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 assert( pTab
1ff2d 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 !=0 && pTab->zNa
1ff2e 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 me!=0 );.
1ff2f 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
1ff30 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
1ff31 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
1ff32 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1ff33 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a pTab->nCol>0 );.
1ff34 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 if( zTab
1ff35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
1ff36 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 ( pItem->zAlias
1ff37 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 ){. c
1ff38 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 har *zTabName =
1ff39 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 pItem->zAlias;.
1ff3a 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 if( s
1ff3b 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 qlite3StrICmp(zT
1ff3c 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 abName, zTab)!=0
1ff3d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
1ff3e 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1ff3f 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a char *
1ff40 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d zTabName = pTab-
1ff41 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 >zName;.
1ff42 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a 54 if( NEVER(zT
1ff43 61 62 4e 61 6d 65 3d 3d 30 29 20 7c 7c 20 73 71 abName==0) || sq
1ff44 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 lite3StrICmp(zTa
1ff45 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 bName, zTab)!=0
1ff46 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1ff47 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
1ff48 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1ff49 20 20 20 20 20 69 66 28 20 7a 44 62 21 3d 30 20 if( zDb!=0
1ff4a 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1ff4b 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a p(db->aDb[iDb].z
1ff4c 4e 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20 29 7b Name, zDb)!=0 ){
1ff4d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 . c
1ff4e 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 ontinue;.
1ff4f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1ff50 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1ff51 20 20 20 20 20 69 66 28 20 30 3d 3d 28 63 6e 74 if( 0==(cnt
1ff52 54 61 62 2b 2b 29 20 29 7b 0a 20 20 20 20 20 20 Tab++) ){.
1ff53 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1ff54 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 e = pItem->iCurs
1ff55 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 or;. pE
1ff56 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 xpr->pTab = pTab
1ff57 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 63 68 ;. pSch
1ff58 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 ema = pTab->pSch
1ff59 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 70 ema;. p
1ff5a 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 Match = pItem;.
1ff5b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1ff5c 20 66 6f 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 for(j=0, pCol=p
1ff5d 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 Tab->aCol; j<pTa
1ff5e 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 b->nCol; j++, pC
1ff5f 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 ol++){.
1ff60 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1ff61 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c Cmp(pCol->zName,
1ff62 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 zCol)==0 ){.
1ff63 20 20 20 20 20 20 20 20 20 49 64 4c 69 73 74 20 IdList
1ff64 2a 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 20 *pUsing;.
1ff65 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 cnt++;.
1ff66 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1ff67 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 Table = pItem->i
1ff68 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 Cursor;.
1ff69 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
1ff6a 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 = pTab;.
1ff6b 20 20 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 pMatch = pIt
1ff6c 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 em;.
1ff6d 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e pSchema = pTab->
1ff6e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 pSchema;.
1ff6f 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 /* Substitu
1ff70 74 65 20 74 68 65 20 72 6f 77 69 64 20 28 63 6f te the rowid (co
1ff71 6c 75 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68 65 lumn -1) for the
1ff72 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
1ff73 20 4b 45 59 20 2a 2f 0a 20 20 20 20 20 20 20 20 KEY */.
1ff74 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 pExpr->iColu
1ff75 6d 6e 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 mn = j==pTab->iP
1ff76 4b 65 79 20 3f 20 2d 31 20 3a 20 28 69 31 36 29 Key ? -1 : (i16)
1ff77 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 j;. i
1ff78 66 28 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e f( i<pSrcList->n
1ff79 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 Src-1 ){.
1ff7a 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d if( pItem
1ff7b 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a [1].jointype & J
1ff7c 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 T_NATURAL ){.
1ff7d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1ff7e 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 6f 63 If this match oc
1ff7f 63 75 72 72 65 64 20 69 6e 20 74 68 65 20 6c 65 curred in the le
1ff80 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6e 61 ft table of a na
1ff81 74 75 72 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20 tural join,.
1ff82 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
1ff83 68 65 6e 20 73 6b 69 70 20 74 68 65 20 72 69 67 hen skip the rig
1ff84 68 74 20 74 61 62 6c 65 20 74 6f 20 61 76 6f 69 ht table to avoi
1ff85 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 d a duplicate ma
1ff86 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 tch */.
1ff87 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a pItem++;.
1ff88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff89 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 i++;.
1ff8a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 55 }else if( (pU
1ff8b 73 69 6e 67 20 3d 20 70 49 74 65 6d 5b 31 5d 2e sing = pItem[1].
1ff8c 70 55 73 69 6e 67 29 21 3d 30 20 29 7b 0a 20 20 pUsing)!=0 ){.
1ff8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ff8e 20 49 66 20 74 68 69 73 20 6d 61 74 63 68 20 6f If this match o
1ff8f 63 63 75 72 73 20 6f 6e 20 61 20 63 6f 6c 75 6d ccurs on a colum
1ff90 6e 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 n that is in the
1ff91 20 55 53 49 4e 47 20 63 6c 61 75 73 65 0a 20 20 USING clause.
1ff92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
1ff93 20 6f 66 20 61 20 6a 6f 69 6e 2c 20 73 6b 69 70 of a join, skip
1ff94 20 74 68 65 20 73 65 61 72 63 68 20 6f 66 20 74 the search of t
1ff95 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f he right table o
1ff96 66 20 74 68 65 20 6a 6f 69 6e 0a 20 20 20 20 20 f the join.
1ff97 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f ** to
1ff98 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63 61 avoid a duplica
1ff99 74 65 20 6d 61 74 63 68 20 74 68 65 72 65 2e 20 te match there.
1ff9a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
1ff9b 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 int k;.
1ff9c 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b 3d for(k=
1ff9d 30 3b 20 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49 64 0; k<pUsing->nId
1ff9e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; k++){.
1ff9f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
1ffa0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 55 73 lite3StrICmp(pUs
1ffa1 69 6e 67 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c ing->a[k].zName,
1ffa2 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 zCol)==0 ){.
1ffa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffa4 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 pItem++;.
1ffa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 2b i+
1ffa6 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +;.
1ffa7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1ffa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ffa9 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
1ffaa 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1ffab 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1ffac 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 }. br
1ffad 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d eak;. }
1ffae 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1ffaf 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 }. }..#ifnde
1ffb0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 f SQLITE_OMIT_TR
1ffb1 49 47 47 45 52 0a 20 20 20 20 2f 2a 20 49 66 20 IGGER. /* If
1ffb2 77 65 20 68 61 76 65 20 6e 6f 74 20 61 6c 72 65 we have not alre
1ffb3 61 64 79 20 72 65 73 6f 6c 76 65 64 20 74 68 65 ady resolved the
1ffb4 20 6e 61 6d 65 2c 20 74 68 65 6e 20 6d 61 79 62 name, then mayb
1ffb5 65 20 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 e . ** it is
1ffb6 61 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a a new.* or old.*
1ffb7 20 74 72 69 67 67 65 72 20 61 72 67 75 6d 65 6e trigger argumen
1ffb8 74 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20 20 t reference.
1ffb9 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 3d 3d */. if( zDb==
1ffba 30 20 26 26 20 7a 54 61 62 21 3d 30 20 26 26 20 0 && zTab!=0 &&
1ffbb 63 6e 74 3d 3d 30 20 26 26 20 70 50 61 72 73 65 cnt==0 && pParse
1ffbc 2d 3e 70 54 72 69 67 67 65 72 54 61 62 21 3d 30 ->pTriggerTab!=0
1ffbd 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 ){. int op
1ffbe 20 3d 20 70 50 61 72 73 65 2d 3e 65 54 72 69 67 = pParse->eTrig
1ffbf 67 65 72 4f 70 3b 0a 20 20 20 20 20 20 54 61 62 gerOp;. Tab
1ffc0 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 le *pTab = 0;.
1ffc1 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d assert( op==
1ffc2 54 4b 5f 44 45 4c 45 54 45 20 7c 7c 20 6f 70 3d TK_DELETE || op=
1ffc3 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 =TK_UPDATE || op
1ffc4 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 29 3b 0a 20 ==TK_INSERT );.
1ffc5 20 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f if( op!=TK_
1ffc6 44 45 4c 45 54 45 20 26 26 20 73 71 6c 69 74 65 DELETE && sqlite
1ffc7 33 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 7a 3StrICmp("new",z
1ffc8 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 Tab) == 0 ){.
1ffc9 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1ffca 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 le = 1;.
1ffcb 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 pTab = pParse->p
1ffcc 54 72 69 67 67 65 72 54 61 62 3b 0a 20 20 20 20 TriggerTab;.
1ffcd 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 21 3d }else if( op!=
1ffce 54 4b 5f 49 4e 53 45 52 54 20 26 26 20 73 71 6c TK_INSERT && sql
1ffcf 69 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c 64 ite3StrICmp("old
1ffd0 22 2c 7a 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20 ",zTab)==0 ){.
1ffd1 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 pExpr->iTa
1ffd2 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 ble = 0;.
1ffd3 20 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e pTab = pParse->
1ffd4 70 54 72 69 67 67 65 72 54 61 62 3b 0a 20 20 20 pTriggerTab;.
1ffd5 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
1ffd6 70 54 61 62 20 29 7b 20 0a 20 20 20 20 20 20 20 pTab ){ .
1ffd7 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 int iCol;.
1ffd8 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61 pSchema = pTa
1ffd9 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 b->pSchema;.
1ffda 20 20 20 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 cntTab++;.
1ffdb 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1ffdc 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 3IsRowid(zCol) )
1ffdd 7b 0a 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c {. iCol
1ffde 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d = -1;. }
1ffdf 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1ffe0 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c for(iCol=0; iCol
1ffe1 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f <pTab->nCol; iCo
1ffe2 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 l++){.
1ffe3 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d Column *pCol =
1ffe4 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f &pTab->aCol[iCo
1ffe5 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 l];.
1ffe6 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
1ffe7 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 mp(pCol->zName,
1ffe8 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 zCol)==0 ){.
1ffe9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43 if( iC
1ffea 6f 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 ol==pTab->iPKey
1ffeb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1ffec 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 iCol = -1;.
1ffed 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1ffee 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 brea
1ffef 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d k;. }
1fff0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1fff1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1fff2 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 f( iCol<pTab->nC
1fff3 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ol ){.
1fff4 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 cnt++;.
1fff5 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 if( iCol<0 ){.
1fff6 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
1fff7 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c ->affinity = SQL
1fff8 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b ITE_AFF_INTEGER;
1fff9 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
1fffa 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 if( pExpr->iTab
1fffb 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 le==0 ){.
1fffc 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 testcase( i
1fffd 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 Col==31 );.
1fffe 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
1ffff 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 iCol==32 );.
20000 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d pParse-
20001 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20 28 69 43 6f >oldmask |= (iCo
20002 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66 66 66 l>=32 ? 0xffffff
20003 66 66 20 3a 20 28 28 28 75 33 32 29 31 29 3c 3c ff : (((u32)1)<<
20004 69 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 iCol));.
20005 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
20006 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 testcase( i
20007 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 Col==31 );.
20008 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
20009 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 iCol==32 );.
2000a 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d pParse-
2000b 3e 6e 65 77 6d 61 73 6b 20 7c 3d 20 28 69 43 6f >newmask |= (iCo
2000c 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66 66 66 l>=32 ? 0xffffff
2000d 66 66 20 3a 20 28 28 28 75 33 32 29 31 29 3c 3c ff : (((u32)1)<<
2000e 69 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 iCol));.
2000f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 }. pE
20010 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 xpr->iColumn = (
20011 69 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 i16)iCol;.
20012 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
20013 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 = pTab;.
20014 20 20 69 73 54 72 69 67 67 65 72 20 3d 20 31 3b isTrigger = 1;
20015 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20016 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 }. }.#endif
20017 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 /* !defined(SQLI
20018 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 TE_OMIT_TRIGGER)
20019 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 */.. /*.
2001a 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6e ** Perhaps the n
2001b 61 6d 65 20 69 73 20 61 20 72 65 66 65 72 65 6e ame is a referen
2001c 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49 44 0a ce to the ROWID.
2001d 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 */. if( c
2001e 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61 62 3d nt==0 && cntTab=
2001f 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49 73 52 =1 && sqlite3IsR
20020 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 owid(zCol) ){.
20021 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 cnt = 1;.
20022 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d pExpr->iColum
20023 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 n = -1;. pE
20024 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 xpr->affinity =
20025 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
20026 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ER;. }.. /
20027 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
20028 69 6e 70 75 74 20 69 73 20 6f 66 20 74 68 65 20 input is of the
20029 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 form Z (not Y.Z
2002a 6f 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 or X.Y.Z) then t
2002b 68 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a he name Z. **
2002c 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20 might refer to
2002d 61 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c an result-set al
2002e 69 61 73 2e 20 20 54 68 69 73 20 68 61 70 70 65 ias. This happe
2002f 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c ns, for example,
20030 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 when. ** we
20031 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 are resolving na
20032 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 mes in the WHERE
20033 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66 clause of the f
20034 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 ollowing command
20035 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
20036 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 SELECT a+b A
20037 53 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 S x FROM table W
20038 48 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a HERE x<10;. *
20039 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 *. ** In case
2003a 73 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70 s like this, rep
2003b 6c 61 63 65 20 70 45 78 70 72 20 77 69 74 68 20 lace pExpr with
2003c 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 a copy of the ex
2003d 70 72 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 pression that.
2003e 20 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 ** forms the r
2003f 65 73 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20 esult set entry
20040 28 22 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78 ("a+b" in the ex
20041 61 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72 ample) and retur
20042 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 n immediately..
20043 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 ** Note that
20044 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
20045 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
20046 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 should have alr
20047 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a eady been. **
20048 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65 resolved by the
20049 20 74 69 6d 65 20 74 68 65 20 57 48 45 52 45 20 time the WHERE
2004a 63 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76 clause is resolv
2004b 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ed.. */. i
2004c 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 f( cnt==0 && (pE
2004d 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 List = pNC->pELi
2004e 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d st)!=0 && zTab==
2004f 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 0 ){. for(j
20050 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 =0; j<pEList->nE
20051 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 xpr; j++){.
20052 20 20 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 char *zAs = p
20053 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d EList->a[j].zNam
20054 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a e;. if( z
20055 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 As!=0 && sqlite3
20056 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f StrICmp(zAs, zCo
20057 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
20058 20 20 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a Expr *pOrig;.
20059 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
2005a 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d ( pExpr->pLeft==
2005b 30 20 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 0 && pExpr->pRig
2005c 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 ht==0 );.
2005d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
2005e 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a ->x.pList==0 );.
2005f 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
20060 28 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 ( pExpr->x.pSele
20061 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 ct==0 );.
20062 20 20 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 pOrig = pELis
20063 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 3b 0a 20 t->a[j].pExpr;.
20064 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4e if( !pN
20065 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 26 26 20 45 C->allowAgg && E
20066 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20067 4f 72 69 67 2c 20 45 50 5f 41 67 67 29 20 29 7b Orig, EP_Agg) ){
20068 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
20069 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
2006a 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 rse, "misuse of
2006b 61 6c 69 61 73 65 64 20 61 67 67 72 65 67 61 74 aliased aggregat
2006c 65 20 25 73 22 2c 20 7a 41 73 29 3b 0a 20 20 20 e %s", zAs);.
2006d 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
2006e 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 WRC_Abort;.
2006f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
20070 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 resolveAlias(pP
20071 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c arse, pEList, j,
20072 20 70 45 78 70 72 2c 20 22 22 29 3b 0a 20 20 20 pExpr, "");.
20073 20 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a cnt = 1;.
20074 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 pMatch
20075 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
20076 61 73 73 65 72 74 28 20 7a 54 61 62 3d 3d 30 20 assert( zTab==0
20077 26 26 20 7a 44 62 3d 3d 30 20 29 3b 0a 20 20 20 && zDb==0 );.
20078 20 20 20 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b goto look
20079 75 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 20 20 upname_end;.
2007a 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 }. } .
2007b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 }.. /* Adv
2007c 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 ance to the next
2007d 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 name context.
2007e 54 68 65 20 6c 6f 6f 70 20 77 69 6c 6c 20 65 78 The loop will ex
2007f 69 74 20 77 68 65 6e 20 65 69 74 68 65 72 0a 20 it when either.
20080 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 61 20 ** we have a
20081 6d 61 74 63 68 20 28 63 6e 74 3e 30 29 20 6f 72 match (cnt>0) or
20082 20 77 68 65 6e 20 77 65 20 72 75 6e 20 6f 75 74 when we run out
20083 20 6f 66 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 of name context
20084 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
20085 28 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ( cnt==0 ){.
20086 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 pNC = pNC->pNe
20087 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 xt;. }. }..
20088 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 58 20 61 6e /*. ** If X an
20089 64 20 59 20 61 72 65 20 4e 55 4c 4c 20 28 69 6e d Y are NULL (in
2008a 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20 other words if
2008b 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 only the column
2008c 6e 61 6d 65 20 5a 20 69 73 0a 20 20 2a 2a 20 73 name Z is. ** s
2008d 75 70 70 6c 69 65 64 29 20 61 6e 64 20 74 68 65 upplied) and the
2008e 20 76 61 6c 75 65 20 6f 66 20 5a 20 69 73 20 65 value of Z is e
2008f 6e 63 6c 6f 73 65 64 20 69 6e 20 64 6f 75 62 6c nclosed in doubl
20090 65 2d 71 75 6f 74 65 73 2c 20 74 68 65 6e 0a 20 e-quotes, then.
20091 20 2a 2a 20 5a 20 69 73 20 61 20 73 74 72 69 6e ** Z is a strin
20092 67 20 6c 69 74 65 72 61 6c 20 69 66 20 69 74 20 g literal if it
20093 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 20 61 6e doesn't match an
20094 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 y column names.
20095 20 49 6e 20 74 68 61 74 0a 20 20 2a 2a 20 63 61 In that. ** ca
20096 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 se, we need to r
20097 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 eturn right away
20098 20 61 6e 64 20 6e 6f 74 20 6d 61 6b 65 20 61 6e and not make an
20099 79 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 20 2a y changes to. *
2009a 2a 20 70 45 78 70 72 2e 0a 20 20 2a 2a 0a 20 20 * pExpr.. **.
2009b 2a 2a 20 42 65 63 61 75 73 65 20 6e 6f 20 72 65 ** Because no re
2009c 66 65 72 65 6e 63 65 20 77 61 73 20 6d 61 64 65 ference was made
2009d 20 74 6f 20 6f 75 74 65 72 20 63 6f 6e 74 65 78 to outer contex
2009e 74 73 2c 20 74 68 65 20 70 4e 43 2d 3e 6e 52 65 ts, the pNC->nRe
2009f 66 0a 20 20 2a 2a 20 66 69 65 6c 64 73 20 61 72 f. ** fields ar
200a0 65 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 69 6e e not changed in
200a1 20 61 6e 79 20 63 6f 6e 74 65 78 74 2e 0a 20 20 any context..
200a2 2a 2f 0a 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 */. if( cnt==0
200a3 26 26 20 7a 54 61 62 3d 3d 30 20 26 26 20 45 78 && zTab==0 && Ex
200a4 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
200a5 78 70 72 2c 45 50 5f 44 62 6c 51 75 6f 74 65 64 xpr,EP_DblQuoted
200a6 29 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e ) ){. pExpr->
200a7 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a op = TK_STRING;.
200a8 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
200a9 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 0;. return
200aa 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a WRC_Prune;. }..
200ab 20 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 /*. ** cnt==0
200ac 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73 means there was
200ad 20 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 not match. cnt
200ae 3e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 >1 means there w
200af 65 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 ere two or. **
200b0 6d 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 45 more matches. E
200b1 69 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 61 ither way, we ha
200b2 76 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a ve an error.. *
200b3 2f 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 29 /. if( cnt!=1 )
200b4 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
200b5 20 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 *zErr;. zErr
200b6 20 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 = cnt==0 ? "no
200b7 73 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 such column" : "
200b8 61 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e ambiguous column
200b9 20 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 name";. if(
200ba 7a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c zDb ){. sql
200bb 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
200bc 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e rse, "%s: %s.%s.
200bd 25 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 %s", zErr, zDb,
200be 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 zTab, zCol);.
200bf 20 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 20 }else if( zTab
200c0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
200c1 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
200c2 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 "%s: %s.%s", zE
200c3 72 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b rr, zTab, zCol);
200c4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
200c5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
200c6 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 g(pParse, "%s: %
200c7 73 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b s", zErr, zCol);
200c8 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e . }. pTopN
200c9 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a C->nErr++;. }..
200ca 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e /* If a column
200cb 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e from a table in
200cc 20 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 66 pSrcList is ref
200cd 65 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 erenced, then re
200ce 63 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 cord. ** this f
200cf 61 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c act in the pSrcL
200d0 69 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 ist.a[].colUsed
200d1 62 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e bitmask. Column
200d2 20 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 0 causes. ** b
200d3 69 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20 it 0 to be set.
200d4 20 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 Column 1 sets b
200d5 69 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f it 1. And so fo
200d6 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 2a rth. If the. *
200d7 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 * column number
200d8 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
200d9 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 the number of bi
200da 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 73 ts in the bitmas
200db 6b 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 k. ** then set
200dc 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 the high-order b
200dd 69 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 it of the bitmas
200de 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 k.. */. if( pE
200df 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 xpr->iColumn>=0
200e0 26 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a && pMatch!=0 ){.
200e1 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 int n = pExp
200e2 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 r->iColumn;.
200e3 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 4d 53 testcase( n==BMS
200e4 2d 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e -1 );. if( n>
200e5 3d 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 6e 20 =BMS ){. n
200e6 3d 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 = BMS-1;. }.
200e7 20 20 20 61 73 73 65 72 74 28 20 70 4d 61 74 63 assert( pMatc
200e8 68 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 78 70 h->iCursor==pExp
200e9 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 r->iTable );.
200ea 20 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 65 64 pMatch->colUsed
200eb 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31 29 |= ((Bitmask)1)
200ec 3c 3c 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 <<n;. }.. /* C
200ed 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75 lean up and retu
200ee 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 rn. */. sqlite
200ef 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
200f0 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 pExpr->pLeft);.
200f1 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 pExpr->pLeft =
200f2 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 0;. sqlite3Expr
200f3 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 Delete(db, pExpr
200f4 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 70 45 78 ->pRight);. pEx
200f5 70 72 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a pr->pRight = 0;.
200f6 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28 69 pExpr->op = (i
200f7 73 54 72 69 67 67 65 72 20 3f 20 54 4b 5f 54 52 sTrigger ? TK_TR
200f8 49 47 47 45 52 20 3a 20 54 4b 5f 43 4f 4c 55 4d IGGER : TK_COLUM
200f9 4e 29 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 N);.lookupname_e
200fa 6e 64 3a 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31 nd:. if( cnt==1
200fb 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
200fc 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 pNC!=0 );. sq
200fd 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 70 50 lite3AuthRead(pP
200fe 61 72 73 65 2c 20 70 45 78 70 72 2c 20 70 53 63 arse, pExpr, pSc
200ff 68 65 6d 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c hema, pNC->pSrcL
20100 69 73 74 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63 ist);. /* Inc
20101 72 65 6d 65 6e 74 20 74 68 65 20 6e 52 65 66 20 rement the nRef
20102 76 61 6c 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d value on all nam
20103 65 20 63 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20 e contexts from
20104 54 6f 70 4e 43 20 75 70 20 74 6f 0a 20 20 20 20 TopNC up to.
20105 2a 2a 20 74 68 65 20 70 6f 69 6e 74 20 77 68 65 ** the point whe
20106 72 65 20 74 68 65 20 6e 61 6d 65 20 6d 61 74 63 re the name matc
20107 68 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 hed. */. for(
20108 3b 3b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ;;){. asser
20109 74 28 20 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a t( pTopNC!=0 );.
2010a 20 20 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 pTopNC->nR
2010b 65 66 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 ef++;. if(
2010c 70 54 6f 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 pTopNC==pNC ) br
2010d 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e eak;. pTopN
2010e 43 20 3d 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 C = pTopNC->pNex
2010f 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 t;. }. ret
20110 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
20111 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 } else {. re
20112 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
20113 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c }.}../*.** All
20114 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e ocate and return
20115 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
20116 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 6c expression to l
20117 6f 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 oad the column i
20118 43 6f 6c 0a 2a 2a 20 66 72 6f 6d 20 64 61 74 61 Col.** from data
20119 73 6f 75 72 63 65 20 69 53 72 63 20 64 61 74 61 source iSrc data
2011a 73 6f 75 72 63 65 20 69 6e 20 53 72 63 4c 69 73 source in SrcLis
2011b 74 20 70 53 72 63 2e 0a 2a 2f 0a 53 51 4c 49 54 t pSrc..*/.SQLIT
2011c 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
2011d 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c sqlite3CreateCol
2011e 75 6d 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20 umnExpr(sqlite3
2011f 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 *db, SrcList *pS
20120 72 63 2c 20 69 6e 74 20 69 53 72 63 2c 20 69 6e rc, int iSrc, in
20121 74 20 69 43 6f 6c 29 7b 0a 20 20 45 78 70 72 20 t iCol){. Expr
20122 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 *p = sqlite3Expr
20123 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 43 4f 4c Alloc(db, TK_COL
20124 55 4d 4e 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 UMN, 0, 0);. if
20125 28 20 70 20 29 7b 0a 20 20 20 20 73 74 72 75 63 ( p ){. struc
20126 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
20127 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 pItem = &pSrc->a
20128 5b 69 53 72 63 5d 3b 0a 20 20 20 20 70 2d 3e 70 [iSrc];. p->p
20129 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 Tab = pItem->pTa
2012a 62 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 b;. p->iTable
2012b 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f = pItem->iCurso
2012c 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 r;. if( p->pT
2012d 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20 ab->iPKey==iCol
2012e 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c ){. p->iCol
2012f 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 umn = -1;. }e
20130 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43 lse{. p->iC
20131 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69 olumn = (ynVar)i
20132 43 6f 6c 3b 0a 20 20 20 20 20 20 70 49 74 65 6d Col;. pItem
20133 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 ->colUsed |= ((B
20134 69 74 6d 61 73 6b 29 31 29 3c 3c 28 69 43 6f 6c itmask)1)<<(iCol
20135 3e 3d 42 4d 53 20 3f 20 42 4d 53 2d 31 20 3a 20 >=BMS ? BMS-1 :
20136 69 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 iCol);. }.
20137 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
20138 28 70 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 (p, EP_Resolved)
20139 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
2013a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
2013b 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 62 routine is callb
2013c 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 57 ack for sqlite3W
2013d 61 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a alkExpr()..**.**
2013e 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 Resolve symboli
2013f 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f c names into TK_
20140 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73 COLUMN operators
20141 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
20142 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 .** node in the
20143 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e expression tree.
20144 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20 63 6f Return 0 to co
20145 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61 72 63 ntinue the searc
20146 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72 h down.** the tr
20147 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f 72 74 ee or 2 to abort
20148 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a the tree walk..
20149 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
2014a 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65 72 72 ne also does err
2014b 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e 64 20 or checking and
2014c 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 name resolution
2014d 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 for.** function
2014e 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70 65 72 names. The oper
2014f 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65 67 61 ator for aggrega
20150 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 te functions is
20151 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b changed.** to TK
20152 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a _AGG_FUNCTION..*
20153 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
20154 6f 6c 76 65 45 78 70 72 53 74 65 70 28 57 61 6c olveExprStep(Wal
20155 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 ker *pWalker, Ex
20156 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61 pr *pExpr){. Na
20157 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 0a meContext *pNC;.
20158 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
20159 0a 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65 .. pNC = pWalke
2015a 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73 73 65 r->u.pNC;. asse
2015b 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 rt( pNC!=0 );.
2015c 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 pParse = pNC->pP
2015d 61 72 73 65 3b 0a 20 20 61 73 73 65 72 74 28 20 arse;. assert(
2015e 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 72 2d pParse==pWalker-
2015f 3e 70 50 61 72 73 65 20 29 3b 0a 0a 20 20 69 66 >pParse );.. if
20160 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 ( ExprHasAnyProp
20161 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 erty(pExpr, EP_R
20162 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74 75 72 esolved) ) retur
20163 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 45 n WRC_Prune;. E
20164 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 xprSetProperty(p
20165 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 Expr, EP_Resolve
20166 64 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 d);.#ifndef NDEB
20167 55 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e 70 53 UG. if( pNC->pS
20168 72 63 4c 69 73 74 20 26 26 20 70 4e 43 2d 3e 70 rcList && pNC->p
20169 53 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e SrcList->nAlloc>
2016a 30 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 0 ){. SrcList
2016b 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 *pSrcList = pNC
2016c 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 ->pSrcList;.
2016d 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 int i;. for(i
2016e 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c =0; i<pNC->pSrcL
2016f 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b ist->nSrc; i++){
20170 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
20171 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 SrcList->a[i].iC
20172 75 72 73 6f 72 3e 3d 30 20 26 26 20 70 53 72 63 ursor>=0 && pSrc
20173 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 List->a[i].iCurs
20174 6f 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 29 or<pParse->nTab)
20175 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
20176 69 66 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 if. switch( pEx
20177 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66 20 64 pr->op ){..#if d
20178 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
20179 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 ABLE_UPDATE_DELE
2017a 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 TE_LIMIT) && !de
2017b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
2017c 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20 20 20 T_SUBQUERY).
2017d 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c 20 6f /* The special o
2017e 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 20 6d perator TK_ROW m
2017f 65 61 6e 73 20 75 73 65 20 74 68 65 20 72 6f 77 eans use the row
20180 69 64 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 id for the first
20181 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 . ** column i
20182 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
20183 65 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 e. This is used
20184 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20 61 6e by the LIMIT an
20185 64 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a d ORDER BY. *
20186 2a 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 * clause process
20187 69 6e 67 20 6f 6e 20 55 50 44 41 54 45 20 61 6e ing on UPDATE an
20188 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 d DELETE stateme
20189 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 nts.. */.
2018a 63 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 case TK_ROW: {.
2018b 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 SrcList *pS
2018c 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 rcList = pNC->pS
2018d 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 73 74 rcList;. st
2018e 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
2018f 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 m *pItem;.
20190 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74 assert( pSrcList
20191 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 6e 53 && pSrcList->nS
20192 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 rc==1 );. p
20193 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d Item = pSrcList-
20194 3e 61 3b 20 0a 20 20 20 20 20 20 70 45 78 70 72 >a; . pExpr
20195 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e ->op = TK_COLUMN
20196 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 ;. pExpr->p
20197 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 Tab = pItem->pTa
20198 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e b;. pExpr->
20199 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e iTable = pItem->
2019a 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 iCursor;. p
2019b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 Expr->iColumn =
2019c 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d -1;. pExpr-
2019d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 >affinity = SQLI
2019e 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a TE_AFF_INTEGER;.
2019f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
201a0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 }.#endif /* def
201a1 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 ined(SQLITE_ENAB
201a2 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 LE_UPDATE_DELETE
201a3 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 _LIMIT) && !defi
201a4 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
201a5 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 20 20 SUBQUERY) */..
201a6 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e /* A lone iden
201a7 74 69 66 69 65 72 20 69 73 20 74 68 65 20 6e 61 tifier is the na
201a8 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a me of a column..
201a9 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
201aa 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20 20 72 TK_ID: {. r
201ab 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 eturn lookupName
201ac 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 70 (pParse, 0, 0, p
201ad 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 Expr->u.zToken,
201ae 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 pNC, pExpr);.
201af 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 74 }. . /* A t
201b0 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f able name and co
201b1 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49 lumn name: I
201b2 44 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61 D.ID. ** Or a
201b3 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
201b4 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 and column: ID
201b5 2e 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20 .ID.ID. */.
201b6 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b case TK_DOT: {
201b7 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
201b8 72 20 2a 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 r *zColumn;.
201b9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
201ba 61 62 6c 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73 able;. cons
201bb 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 20 t char *zDb;.
201bc 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b Expr *pRight;
201bd 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70 .. /* if( p
201be 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 72 65 SrcList==0 ) bre
201bf 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 52 69 ak; */. pRi
201c0 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 ght = pExpr->pRi
201c1 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ght;. if( p
201c2 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 Right->op==TK_ID
201c3 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 44 62 20 ){. zDb
201c4 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 54 61 = 0;. zTa
201c5 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 ble = pExpr->pLe
201c6 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 ft->u.zToken;.
201c7 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 zColumn =
201c8 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e pRight->u.zToken
201c9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
201ca 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
201cb 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f Right->op==TK_DO
201cc 54 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 44 62 T );. zDb
201cd 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d = pExpr->pLeft-
201ce 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 >u.zToken;.
201cf 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 52 69 67 zTable = pRig
201d0 68 74 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f ht->pLeft->u.zTo
201d1 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f ken;. zCo
201d2 6c 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d 3e 70 lumn = pRight->p
201d3 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b Right->u.zToken;
201d4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
201d5 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 eturn lookupName
201d6 28 70 50 61 72 73 65 2c 20 7a 44 62 2c 20 7a 54 (pParse, zDb, zT
201d7 61 62 6c 65 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 able, zColumn, p
201d8 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 NC, pExpr);.
201d9 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 }.. /* Resolv
201da 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 e function names
201db 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
201dc 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a TK_CONST_FUNC:.
201dd 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 case TK_FUNC
201de 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 TION: {. Ex
201df 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 prList *pList =
201e0 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 pExpr->x.pList;
201e1 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 /* The argume
201e2 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 nt list */.
201e3 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f int n = pList ?
201e4 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 pList->nExpr :
201e5 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 0; /* Number
201e6 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a of arguments */.
201e7 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63 int no_suc
201e8 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20 20 20 h_func = 0;
201e9 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 20 /* True if no
201ea 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 such function ex
201eb 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e ists */. in
201ec 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 t wrong_num_args
201ed 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 = 0; /* Tru
201ee 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65 e if wrong numbe
201ef 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a r of arguments *
201f0 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 5f 61 /. int is_a
201f1 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 gg = 0;
201f2 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
201f3 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 s an aggregate f
201f4 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 unction */.
201f5 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20 20 20 int auth;
201f6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
201f7 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f Authorization to
201f8 20 75 73 65 20 74 68 65 20 66 75 6e 63 74 69 6f use the functio
201f9 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e n */. int n
201fa 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Id;
201fb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
201fc 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 of characters i
201fd 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 n function name
201fe 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 */. const c
201ff 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 har *zId;
20200 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 /* The func
20201 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 tion name. */.
20202 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 FuncDef *pDe
20203 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
20204 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
20205 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74 69 6f bout the functio
20206 6e 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 6e n */. u8 en
20207 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d 3e c = ENC(pParse->
20208 64 62 29 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 db); /* The da
20209 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 tabase encoding
2020a 2a 2f 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61 */.. testca
2020b 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
2020c 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a K_CONST_FUNC );.
2020d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
2020e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
2020f 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 Expr, EP_xIsSele
20210 63 74 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 ct) );. zId
20211 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b = pExpr->u.zTok
20212 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 en;. nId =
20213 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
20214 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 44 65 66 zId);. pDef
20215 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 = sqlite3FindFu
20216 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 nction(pParse->d
20217 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 b, zId, nId, n,
20218 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 enc, 0);. i
20219 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 f( pDef==0 ){.
2021a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c pDef = sql
2021b 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e ite3FindFunction
2021c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 (pParse->db, zId
2021d 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 , nId, -1, enc,
2021e 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
2021f 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 pDef==0 ){.
20220 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e no_such_fun
20221 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d c = 1;. }
20222 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
20223 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d wrong_num_args =
20224 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1;. }.
20225 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
20226 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44 65 66 is_agg = pDef
20227 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 ->xFunc==0;.
20228 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
20229 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a TE_OMIT_AUTHORIZ
2022a 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66 28 20 ATION. if(
2022b 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 pDef ){.
2022c 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75 auth = sqlite3Au
2022d 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 thCheck(pParse,
2022e 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c SQLITE_FUNCTION,
2022f 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 0, pDef->zName,
20230 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0);. if(
20231 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b auth!=SQLITE_OK
20232 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ){. if
20233 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 ( auth==SQLITE_D
20234 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ENY ){.
20235 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
20236 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 sg(pParse, "not
20237 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20 75 73 authorized to us
20238 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c e function: %s",
20239 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
2023a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2023b 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 pDef->zName
2023c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 );. p
2023d 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 NC->nErr++;.
2023e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2023f 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
20240 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 _NULL;.
20241 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
20242 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 e;. }.
20243 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
20244 20 20 69 66 28 20 69 73 5f 61 67 67 20 26 26 20 if( is_agg &&
20245 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 !pNC->allowAgg )
20246 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
20247 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
20248 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67 , "misuse of agg
20249 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
2024a 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64 %.*s()", nId,zId
2024b 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e );. pNC->
2024c 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
2024d 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20 20 20 is_agg = 0;.
2024e 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73 }else if( no_s
2024f 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20 uch_func ){.
20250 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
20251 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 Msg(pParse, "no
20252 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25 such function: %
20253 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b .*s", nId, zId);
20254 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 . pNC->nE
20255 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 rr++;. }els
20256 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f e if( wrong_num_
20257 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20 20 20 args ){.
20258 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20259 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75 pParse,"wrong nu
2025a 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
2025b 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e s to function %.
2025c 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20 20 20 *s()",.
2025d 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 nId, zId);.
2025e 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 pNC->nErr
2025f 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ++;. }.
20260 20 20 69 66 28 20 69 73 5f 61 67 67 20 29 7b 0a if( is_agg ){.
20261 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f pExpr->o
20262 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 p = TK_AGG_FUNCT
20263 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 ION;. pNC
20264 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 ->hasAgg = 1;.
20265 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
20266 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c is_agg ) pNC->al
20267 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 lowAgg = 0;.
20268 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
20269 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 rList(pWalker, p
2026a 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 List);. if(
2026b 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 is_agg ) pNC->a
2026c 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 llowAgg = 1;.
2026d 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43 /* FIX ME: C
2026e 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e 61 66 ompute pExpr->af
2026f 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f 6e 20 finity based on
20270 74 68 65 20 65 78 70 65 63 74 65 64 20 72 65 74 the expected ret
20271 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79 70 urn. ** typ
20272 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f e of the functio
20273 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n . */.
20274 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
20275 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ne;. }.#ifnde
20276 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 f SQLITE_OMIT_SU
20277 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 BQUERY. case
20278 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 63 TK_SELECT:. c
20279 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 20 20 ase TK_EXISTS:
2027a 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
2027b 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 >op==TK_EXISTS )
2027c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 ;.#endif. cas
2027d 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 e TK_IN: {.
2027e 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 testcase( pExpr
2027f 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 ->op==TK_IN );.
20280 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
20281 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
20282 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b EP_xIsSelect) ){
20283 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 52 65 . int nRe
20284 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 f = pNC->nRef;.#
20285 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
20286 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 20 20 20 IT_CHECK.
20287 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 if( pNC->isChec
20288 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 k ){. s
20289 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
2028a 50 61 72 73 65 2c 22 73 75 62 71 75 65 72 69 65 Parse,"subquerie
2028b 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e 20 s prohibited in
2028c 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 CHECK constraint
2028d 73 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 s");. }.#
2028e 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 73 71 endif. sq
2028f 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
20290 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e pWalker, pExpr->
20291 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 x.pSelect);.
20292 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d assert( pNC-
20293 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 >nRef>=nRef );.
20294 20 20 20 20 20 20 20 69 66 28 20 6e 52 65 66 21 if( nRef!
20295 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 =pNC->nRef ){.
20296 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74 50 ExprSetP
20297 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
20298 50 5f 56 61 72 53 65 6c 65 63 74 29 3b 0a 20 20 P_VarSelect);.
20299 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
2029a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2029b 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
2029c 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 E_OMIT_CHECK.
2029d 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c case TK_VARIABL
2029e 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 E: {. if( p
2029f 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 NC->isCheck ){.
202a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
202a1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 70 rorMsg(pParse,"p
202a2 61 72 61 6d 65 74 65 72 73 20 70 72 6f 68 69 62 arameters prohib
202a3 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20 63 6f ited in CHECK co
202a4 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 nstraints");.
202a5 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
202a6 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
202a7 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 }. return (pPa
202a8 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61 rse->nErr || pPa
202a9 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rse->db->mallocF
202aa 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f ailed) ? WRC_Abo
202ab 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 rt : WRC_Continu
202ac 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 e;.}../*.** pELi
202ad 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 st is a list of
202ae 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63 expressions whic
202af 68 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 h are really the
202b0 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
202b1 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 he.** a SELECT s
202b2 74 61 74 65 6d 65 6e 74 2e 20 20 70 45 20 69 73 tatement. pE is
202b3 20 61 20 74 65 72 6d 20 69 6e 20 61 6e 20 4f 52 a term in an OR
202b4 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
202b5 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 BY clause..** Th
202b6 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
202b7 73 20 74 6f 20 73 65 65 20 69 66 20 70 45 20 69 s to see if pE i
202b8 73 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 s a simple ident
202b9 69 66 69 65 72 20 77 68 69 63 68 20 63 6f 72 72 ifier which corr
202ba 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 esponds.** to th
202bb 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 e AS-name of one
202bc 20 6f 66 20 74 68 65 20 74 65 72 6d 73 20 6f 66 of the terms of
202bd 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
202be 6c 69 73 74 2e 20 20 49 66 20 69 74 20 69 73 2c list. If it is,
202bf 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
202c0 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 return an integ
202c1 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 er between 1 and
202c2 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 N where N is th
202c3 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 e number of.** e
202c4 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45 4c 69 73 lements in pELis
202c5 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 t, corresponding
202c6 20 74 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 to the matching
202c7 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68 65 72 entry. If ther
202c8 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 e is.** no match
202c9 2c 20 6f 72 20 69 66 20 70 45 20 69 73 20 6e 6f , or if pE is no
202ca 74 20 61 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 t a simple ident
202cb 69 66 69 65 72 2c 20 74 68 65 6e 20 74 68 69 73 ifier, then this
202cc 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 routine.** retu
202cd 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 rn 0..**.** pELi
202ce 73 74 20 68 61 73 20 62 65 65 6e 20 72 65 73 6f st has been reso
202cf 6c 76 65 64 2e 20 20 70 45 20 68 61 73 20 6e 6f lved. pE has no
202d0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
202d1 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 0a resolveAsName(.
202d2 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
202d3 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
202d4 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f context for erro
202d5 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 r messages */.
202d6 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
202d7 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 65 78 , /* List of ex
202d8 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 73 63 61 pressions to sca
202d9 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 n */. Expr *pE
202da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
202db 72 65 73 73 69 6f 6e 20 77 65 20 61 72 65 20 74 ression we are t
202dc 72 79 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 2a rying to match *
202dd 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 /.){. int i;
202de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
202df 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 p counter */..
202e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
202e1 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69 66 28 (pParse);.. if(
202e2 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 pE->op==TK_ID )
202e3 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c {. char *zCol
202e4 20 3d 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b = pE->u.zToken;
202e5 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
202e6 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 pEList->nExpr; i
202e7 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 ++){. char
202e8 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 *zAs = pEList->a
202e9 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 [i].zName;.
202ea 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26 20 73 if( zAs!=0 && s
202eb 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 41 qlite3StrICmp(zA
202ec 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 s, zCol)==0 ){.
202ed 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b return i+
202ee 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
202ef 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
202f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 .}../*.** pE is
202f1 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
202f2 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 expression which
202f3 20 69 73 20 61 20 73 69 6e 67 6c 65 20 74 65 72 is a single ter
202f4 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 52 44 45 m in the.** ORDE
202f5 52 20 42 59 20 6f 66 20 61 20 63 6f 6d 70 6f 75 R BY of a compou
202f6 6e 64 20 53 45 4c 45 43 54 2e 20 20 54 68 65 20 nd SELECT. The
202f7 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 6e expression has n
202f8 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 ot been.** name
202f9 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 resolved..**.**
202fa 41 74 20 74 68 65 20 70 6f 69 6e 74 20 74 68 69 At the point thi
202fb 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
202fc 6c 65 64 2c 20 77 65 20 61 6c 72 65 61 64 79 20 led, we already
202fd 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 0a 2a 2a know that the.**
202fe 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69 ORDER BY term i
202ff 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 s not an integer
20300 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 index into the
20301 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 61 result set. Tha
20302 74 0a 2a 2a 20 63 61 73 65 20 69 73 20 68 61 6e t.** case is han
20303 64 6c 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c dled by the call
20304 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a ing routine..**.
20305 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6d 61 ** Attempt to ma
20306 74 63 68 20 70 45 20 61 67 61 69 6e 73 74 20 72 tch pE against r
20307 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e esult set column
20308 73 20 69 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f s in the left-mo
20309 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 st.** SELECT sta
2030a 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 tement. Return
2030b 74 68 65 20 69 6e 64 65 78 20 69 20 6f 66 20 74 the index i of t
2030c 68 65 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 he matching colu
2030d 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 64 mn,.** as an ind
2030e 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 65 20 63 ication to the c
2030f 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20 73 68 aller that it sh
20310 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 74 68 65 ould sort by the
20311 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a i-th column..**
20312 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 The left-most c
20313 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20 49 6e 20 olumn is 1. In
20314 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
20315 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
20316 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 69 is the.** same i
20317 6e 74 65 67 65 72 20 76 61 6c 75 65 20 74 68 61 nteger value tha
20318 74 20 77 6f 75 6c 64 20 62 65 20 75 73 65 64 20 t would be used
20319 69 6e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 in the SQL state
2031a 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63 61 74 65 ment to indicate
2031b 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a .** the column..
2031c 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 **.** If there i
2031d 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 s no match, retu
2031e 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 2d 31 rn 0. Return -1
2031f 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
20320 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 urs..*/.static i
20321 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 nt resolveOrderB
20322 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 yTermToExprList(
20323 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20324 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 , /* Parsing
20325 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 context for err
20326 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 or messages */.
20327 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
20328 2c 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 , /* The SELEC
20329 54 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 T statement with
2032a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
2032b 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a ause */. Expr *
2032c 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 pE /*
2032d 54 68 65 20 73 70 65 63 69 66 69 63 20 4f 52 44 The specific ORD
2032e 45 52 20 42 59 20 74 65 72 6d 20 2a 2f 0a 29 7b ER BY term */.){
2032f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
20330 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
20331 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c unter */. ExprL
20332 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a ist *pEList; /*
20333 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 The columns of
20334 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a the result set *
20335 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 /. NameContext
20336 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 nc; /* Name c
20337 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 73 6f 6c ontext for resol
20338 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20 20 61 73 ving pE */.. as
20339 73 65 72 74 28 20 73 71 6c 69 74 65 33 45 78 70 sert( sqlite3Exp
2033a 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 rIsInteger(pE, &
2033b 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45 4c 69 73 i)==0 );. pELis
2033c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c t = pSelect->pEL
2033d 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c ist;.. /* Resol
2033e 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 ve all names in
2033f 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 the ORDER BY ter
20340 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a m expression. *
20341 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20 /. memset(&nc,
20342 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 0, sizeof(nc));.
20343 20 20 6e 63 2e 70 50 61 72 73 65 20 3d 20 70 50 nc.pParse = pP
20344 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53 72 63 4c arse;. nc.pSrcL
20345 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 ist = pSelect->p
20346 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c 69 73 74 Src;. nc.pEList
20347 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 6e 63 2e = pEList;. nc.
20348 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 allowAgg = 1;.
20349 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 nc.nErr = 0;. i
2034a 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
2034b 65 45 78 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20 eExprNames(&nc,
2034c 70 45 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 pE) ){. sqlit
2034d 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 e3ErrorClear(pPa
2034e 72 73 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e rse);. return
2034f 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 0;. }.. /* Tr
20350 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 4f y to match the O
20351 52 44 45 52 20 42 59 20 65 78 70 72 65 73 73 69 RDER BY expressi
20352 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e 20 65 78 on against an ex
20353 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e pression. ** in
20354 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e the result set.
20355 20 20 52 65 74 75 72 6e 20 61 6e 20 31 2d 62 61 Return an 1-ba
20356 73 65 64 20 69 6e 64 65 78 20 6f 66 20 74 68 65 sed index of the
20357 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a 2a 20 72 matching. ** r
20358 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e esult-set entry.
20359 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
2035a 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 i<pEList->nExpr
2035b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
2035c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 sqlite3ExprCompa
2035d 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e re(pEList->a[i].
2035e 70 45 78 70 72 2c 20 70 45 29 20 29 7b 0a 20 20 pExpr, pE) ){.
2035f 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a return i+1;.
20360 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
20361 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 If no match, ret
20362 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75 urn 0. */. retu
20363 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 rn 0;.}../*.** G
20364 65 6e 65 72 61 74 65 20 61 6e 20 4f 52 44 45 52 enerate an ORDER
20365 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 BY or GROUP BY
20366 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 term out-of-rang
20367 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 e error..*/.stat
20368 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f ic void resolveO
20369 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 0a utOfRangeError(.
2036a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
2036b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2036c 65 72 72 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e error context in
2036d 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 to which to writ
2036e 65 20 74 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20 e the error */.
2036f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 const char *zTy
20370 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 pe, /* "ORDE
20371 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f R" or "GROUP" */
20372 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 . int i,
20373 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
20374 20 69 6e 64 65 78 20 28 31 2d 62 61 73 65 64 29 index (1-based)
20375 20 6f 66 20 74 68 65 20 74 65 72 6d 20 6f 75 74 of the term out
20376 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 of range */. i
20377 6e 74 20 6d 78 20 20 20 20 20 20 20 20 20 20 20 nt mx
20378 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 /* Largest
20379 20 70 65 72 6d 69 73 73 69 62 6c 65 20 76 61 6c permissible val
2037a 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 ue of i */.){.
2037b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2037c 70 50 61 72 73 65 2c 20 0a 20 20 20 20 22 25 72 pParse, . "%r
2037d 20 25 73 20 42 59 20 74 65 72 6d 20 6f 75 74 20 %s BY term out
2037e 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c of range - shoul
2037f 64 20 62 65 20 22 0a 20 20 20 20 22 62 65 74 77 d be ". "betw
20380 65 65 6e 20 31 20 61 6e 64 20 25 64 22 2c 20 69 een 1 and %d", i
20381 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a , zType, mx);.}.
20382 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 ./*.** Analyze t
20383 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
20384 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 se in a compound
20385 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
20386 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 t. Modify.** e
20387 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 ach term of the
20388 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
20389 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e is a constant in
2038a 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 0a teger between 1.
2038b 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e ** and N where N
2038c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
2038d 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
2038e 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
2038f 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 ..**.** ORDER BY
20390 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 terms that are
20391 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 74 65 67 already an integ
20392 65 72 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 er between 1 and
20393 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 N are.** unmodi
20394 66 69 65 64 2e 20 20 4f 52 44 45 52 20 42 59 20 fied. ORDER BY
20395 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 69 terms that are i
20396 6e 74 65 67 65 72 73 20 6f 75 74 73 69 64 65 20 ntegers outside
20397 74 68 65 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 the range of.**
20398 31 20 74 68 72 6f 75 67 68 20 4e 20 67 65 6e 65 1 through N gene
20399 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 20 20 rate an error.
2039a 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 ORDER BY terms t
2039b 68 61 74 20 61 72 65 20 65 78 70 72 65 73 73 69 hat are expressi
2039c 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 74 63 68 ons.** are match
2039d 65 64 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c ed against resul
2039e 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t set expression
2039f 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 53 45 s of compound SE
203a0 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e LECT.** beginnin
203a1 67 20 77 69 74 68 20 74 68 65 20 6c 65 66 74 2d g with the left-
203a2 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 6e 64 20 most SELECT and
203a3 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 64 20 74 working toward t
203a4 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20 41 74 20 he right..** At
203a5 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 2c the first match,
203a6 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 65 78 the ORDER BY ex
203a7 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 61 6e pression is tran
203a8 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 sformed into.**
203a9 74 68 65 20 69 6e 74 65 67 65 72 20 63 6f 6c 75 the integer colu
203aa 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a mn number..**.**
203ab 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
203ac 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 er of errors see
203ad 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
203ae 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 resolveCompound
203af 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72 73 65 OrderBy(. Parse
203b0 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
203b1 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
203b2 65 78 74 2e 20 20 4c 65 61 76 65 20 65 72 72 6f ext. Leave erro
203b3 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 r messages here
203b4 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 */. Select *pSe
203b5 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20 54 68 lect /* Th
203b6 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
203b7 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 nt containing th
203b8 65 20 4f 52 44 45 52 20 42 59 20 2a 2f 0a 29 7b e ORDER BY */.){
203b9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70 72 . int i;. Expr
203ba 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 0a List *pOrderBy;.
203bb 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
203bc 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 st;. sqlite3 *d
203bd 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44 b;. int moreToD
203be 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72 64 65 72 o = 1;.. pOrder
203bf 42 79 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4f By = pSelect->pO
203c0 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20 70 4f rderBy;. if( pO
203c1 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 rderBy==0 ) retu
203c2 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 rn 0;. db = pPa
203c3 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c rse->db;.#if SQL
203c4 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 ITE_MAX_COLUMN.
203c5 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e if( pOrderBy->n
203c6 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b Expr>db->aLimit[
203c7 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c SQLITE_LIMIT_COL
203c8 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 UMN] ){. sqli
203c9 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
203ca 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 se, "too many te
203cb 72 6d 73 20 69 6e 20 4f 52 44 45 52 20 42 59 20 rms in ORDER BY
203cc 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 72 65 clause");. re
203cd 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 turn 1;. }.#end
203ce 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c if. for(i=0; i<
203cf 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b pOrderBy->nExpr;
203d0 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f 72 64 65 i++){. pOrde
203d1 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d rBy->a[i].done =
203d2 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 0;. }. pSelec
203d3 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 t->pNext = 0;.
203d4 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e while( pSelect->
203d5 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 53 pPrior ){. pS
203d6 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 elect->pPrior->p
203d7 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a Next = pSelect;.
203d8 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 pSelect = pS
203d9 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 elect->pPrior;.
203da 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c }. while( pSel
203db 65 63 74 20 26 26 20 6d 6f 72 65 54 6f 44 6f 20 ect && moreToDo
203dc 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 ){. struct Ex
203dd 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
203de 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54 6f 44 6f em;. moreToDo
203df 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c 69 73 74 = 0;. pEList
203e0 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 = pSelect->pELi
203e1 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 st;. assert(
203e2 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 pEList!=0 );.
203e3 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d for(i=0, pItem=
203e4 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 pOrderBy->a; i<p
203e5 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 OrderBy->nExpr;
203e6 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 i++, pItem++){.
203e7 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 int iCol =
203e8 2d 31 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a -1;. Expr *
203e9 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 pE, *pDup;.
203ea 20 69 66 28 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 if( pItem->done
203eb 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
203ec 20 20 20 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 pE = pItem->p
203ed 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 Expr;. if(
203ee 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 sqlite3ExprIsInt
203ef 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 eger(pE, &iCol)
203f0 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ){. if( i
203f1 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 Col<=0 || iCol>p
203f2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a EList->nExpr ){.
203f3 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 resolv
203f4 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 eOutOfRangeError
203f5 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45 52 22 (pParse, "ORDER"
203f6 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e , i+1, pEList->n
203f7 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20 Expr);.
203f8 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
203f9 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
203fa 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d {. iCol =
203fb 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 resolveAsName(p
203fc 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 Parse, pEList, p
203fd 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 E);. if(
203fe 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 iCol==0 ){.
203ff 20 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 pDup = sqli
20400 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
20401 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 E, 0);.
20402 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 if( !db->malloc
20403 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
20404 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44 75 assert(pDu
20405 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 p);.
20406 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 iCol = resolveOr
20407 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c derByTermToExprL
20408 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c ist(pParse, pSel
20409 65 63 74 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 ect, pDup);.
2040a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2040b 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
2040c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 ete(db, pDup);.
2040d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
2040e 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e . if( iCol>
2040f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 0 ){. Col
20410 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 lSeq *pColl = pE
20411 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 ->pColl;.
20412 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 45 2d int flags = pE-
20413 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 >flags & EP_ExpC
20414 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 ollate;.
20415 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
20416 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 20 20 e(db, pE);.
20417 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 pItem->pExpr
20418 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33 45 78 = pE = sqlite3Ex
20419 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45 47 45 pr(db, TK_INTEGE
2041a 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 R, 0);. i
2041b 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74 75 72 f( pE==0 ) retur
2041c 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70 45 2d n 1;. pE-
2041d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a >pColl = pColl;.
2041e 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c 61 67 pE->flag
2041f 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 s |= EP_IntValue
20420 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 | flags;.
20421 20 20 70 45 2d 3e 75 2e 69 56 61 6c 75 65 20 3d pE->u.iValue =
20422 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 iCol;. p
20423 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 Item->iCol = (u1
20424 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 6)iCol;.
20425 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b pItem->done = 1;
20426 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
20427 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d moreToDo =
20428 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
20429 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 }. pSelect =
2042a 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a pSelect->pNext;.
2042b 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
2042c 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 <pOrderBy->nExpr
2042d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
2042e 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 pOrderBy->a[i].d
2042f 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 one==0 ){.
20430 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
20431 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52 44 45 pParse, "%r ORDE
20432 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73 20 6e R BY term does n
20433 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22 0a 20 ot match any ".
20434 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6c 75 "colu
20435 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 mn in the result
20436 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20 set", i+1);.
20437 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
20438 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
20439 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 0;.}../*.** Chec
2043a 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20 k every term in
2043b 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 the ORDER BY or
2043c 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
2043d 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74 pOrderBy of.** t
2043e 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
2043f 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20 49 66 ent pSelect. If
20440 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72 65 66 any term is ref
20441 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72 erence to a.** r
20442 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 esult set expres
20443 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72 6d 69 sion (as determi
20444 6e 65 64 20 62 79 20 74 68 65 20 45 78 70 72 4c ned by the ExprL
20445 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64 ist.a.iCol field
20446 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72 ).** then conver
20447 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e 74 6f t that term into
20448 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 a copy of the c
20449 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 orresponding res
2044a 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d ult set.** colum
2044b 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 n..**.** If any
2044c 65 72 72 6f 72 73 20 61 72 65 20 64 65 74 65 63 errors are detec
2044d 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72 72 6f ted, add an erro
2044e 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 r message to pPa
2044f 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 rse and.** retur
20450 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 n non-zero. Ret
20451 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65 urn zero if no e
20452 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e 2e 0a rrors are seen..
20453 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20454 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
20455 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
20456 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
20457 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 e, /* Par
20458 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c sing context. L
20459 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61 eave error messa
2045a 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 ges here */. Se
2045b 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 lect *pSelect,
2045c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 /* The SELEC
2045d 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 T statement cont
2045e 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61 75 73 aining the claus
2045f 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 e */. ExprList
20460 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 *pOrderBy, /*
20461 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f 72 20 The ORDER BY or
20462 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
20463 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65 64 20 to be processed
20464 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
20465 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20 22 4f *zType /* "O
20466 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 RDER" or "GROUP"
20467 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a */.){. int i;.
20468 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20469 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78 pParse->db;. Ex
2046a 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
2046b 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
2046c 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
2046d 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d if( pOrderBy==
2046e 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 0 || pParse->db-
2046f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
20470 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 return 0;.#if SQ
20471 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a LITE_MAX_COLUMN.
20472 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d 3e if( pOrderBy->
20473 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 nExpr>db->aLimit
20474 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f [SQLITE_LIMIT_CO
20475 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c LUMN] ){. sql
20476 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
20477 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 rse, "too many t
20478 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20 63 6c erms in %s BY cl
20479 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20 ause", zType);.
2047a 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
2047b 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69 73 74 .#endif. pEList
2047c 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 = pSelect->pELi
2047d 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 st;. assert( pE
2047e 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73 List!=0 ); /* s
2047f 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 qlite3SelectNew(
20480 29 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 ) guarantees thi
20481 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 s */. for(i=0,
20482 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e pItem=pOrderBy->
20483 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e a; i<pOrderBy->n
20484 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
20485 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 ++){. if( pIt
20486 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20 em->iCol ){.
20487 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f if( pItem->iCo
20488 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 l>pEList->nExpr
20489 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c ){. resol
2048a 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f veOutOfRangeErro
2048b 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c r(pParse, zType,
2048c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 i+1, pEList->nE
2048d 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 72 65 xpr);. re
2048e 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a turn 1;. }.
2048f 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 resolveAli
20490 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 as(pParse, pELis
20491 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 t, pItem->iCol-1
20492 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 , pItem->pExpr,
20493 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 zType);. }.
20494 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
20495 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 ./*.** pOrderBy
20496 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f is an ORDER BY o
20497 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 r GROUP BY claus
20498 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74 e in SELECT stat
20499 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a ement pSelect..*
2049a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65 * The Name conte
2049b 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 xt of the SELECT
2049c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 4e statement is pN
2049d 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65 69 74 C. zType is eit
2049e 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f her.** "ORDER" o
2049f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65 6e 64 r "GROUP" depend
204a0 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74 79 70 ing on which typ
204a1 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f 72 64 e of clause pOrd
204a2 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 erBy is..**.** T
204a3 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 6f his routine reso
204a4 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d 20 6f lves each term o
204a5 66 20 74 68 65 20 63 6c 61 75 73 65 20 69 6e 74 f the clause int
204a6 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e o an expression.
204a7 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64 65 72 .** If the order
204a8 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69 -by term is an i
204a9 6e 74 65 67 65 72 20 49 20 62 65 74 77 65 65 6e nteger I between
204aa 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72 65 20 1 and N (where
204ab 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 N is the.** numb
204ac 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
204ad 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
204ae 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29 20 74 of the SELECT) t
204af 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 hen the expressi
204b0 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 on.** in the res
204b1 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63 6f 70 olution is a cop
204b2 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20 72 65 y of the I-th re
204b3 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
204b4 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 ion. If.** the
204b5 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 69 73 order-by term is
204b6 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74 68 61 an identify tha
204b7 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f t corresponds to
204b8 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a the AS-name of.
204b9 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65 74 20 ** a result-set
204ba 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e expression, then
204bb 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f 6c 76 the term resolv
204bc 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 es to a copy of
204bd 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 the.** result-se
204be 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f t expression. O
204bf 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 78 therwise, the ex
204c0 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 73 6f pression is reso
204c1 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75 lved in.** the u
204c2 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69 6e 67 sual way - using
204c3 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 sqlite3ResolveE
204c4 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a xprNames()..**.*
204c5 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
204c6 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
204c7 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 r of errors. If
204c8 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 errors occur, t
204c9 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 hen.** an approp
204ca 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 riate error mess
204cb 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c 65 66 age might be lef
204cc 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20 28 4f t in pParse. (O
204cd 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63 OM errors.** exc
204ce 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 epted.).*/.stati
204cf 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 c int resolveOrd
204d0 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d erGroupBy(. Nam
204d1 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 eContext *pNC,
204d2 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 /* The name c
204d3 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 ontext of the SE
204d4 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a LECT statement *
204d5 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c /. Select *pSel
204d6 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ect, /* The
204d7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
204d8 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72 t holding pOrder
204d9 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 By */. ExprList
204da 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a *pOrderBy, /*
204db 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 An ORDER BY or
204dc 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
204dd 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 to resolve */.
204de 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 const char *zTyp
204df 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 e /* Either
204e0 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 "ORDER" or "GROU
204e1 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 P", as appropria
204e2 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 te */.){. int i
204e3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
204e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
204e5 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
204e6 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 nt iCol;
204e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
204e8 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a Column number *
204e9 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c /. struct ExprL
204ea 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
204eb 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 /* A term of
204ec 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
204ed 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a use */. Parse *
204ee 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
204ef 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
204f0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
204f1 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20 20 20 int nResult;
204f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
204f3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d * Number of term
204f4 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
204f5 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f set */.. if( pO
204f6 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65 74 75 rderBy==0 ) retu
204f7 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20 rn 0;. nResult
204f8 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 = pSelect->pELis
204f9 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50 61 72 t->nExpr;. pPar
204fa 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
204fb 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 ;. for(i=0, pIt
204fc 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 em=pOrderBy->a;
204fd 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 i<pOrderBy->nExp
204fe 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 r; i++, pItem++)
204ff 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d {. Expr *pE =
20500 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 pItem->pExpr;.
20501 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 iCol = resolv
20502 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 eAsName(pParse,
20503 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c pSelect->pEList,
20504 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20 69 43 pE);. if( iC
20505 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a ol>0 ){. /*
20506 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d 65 20 6d If an AS-name m
20507 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 6d atch is found, m
20508 61 72 6b 20 74 68 69 73 20 4f 52 44 45 52 20 42 ark this ORDER B
20509 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 65 69 6e Y column as bein
2050a 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 70 g. ** a cop
2050b 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 y of the iCol-th
2050c 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 result-set colu
2050d 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 mn. The subsequ
2050e 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 ent call to.
2050f 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 73 6f ** sqlite3Reso
20510 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 lveOrderGroupBy(
20511 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 ) will convert t
20512 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f he expression to
20513 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 a. ** copy
20514 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 of the iCol-th
20515 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 result-set expre
20516 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 ssion. */.
20517 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 pItem->iCol = (u
20518 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 16)iCol;. c
20519 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 ontinue;. }.
2051a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 if( sqlite3Ex
2051b 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c 20 prIsInteger(pE,
2051c 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 &iCol) ){.
2051d 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 /* The ORDER BY
2051e 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 term is an integ
2051f 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20 20 41 67 er constant. Ag
20520 61 69 6e 2c 20 73 65 74 20 74 68 65 20 63 6f 6c ain, set the col
20521 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d umn. ** num
20522 62 65 72 20 73 6f 20 74 68 61 74 20 73 71 6c 69 ber so that sqli
20523 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 te3ResolveOrderG
20524 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20 63 6f roupBy() will co
20525 6e 76 65 72 74 20 74 68 65 0a 20 20 20 20 20 20 nvert the.
20526 2a 2a 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d ** order-by term
20527 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 to a copy of th
20528 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 e result-set exp
20529 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 ression */.
2052a 20 69 66 28 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 if( iCol<1 ){.
2052b 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 resolveOu
2052c 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 tOfRangeError(pP
2052d 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 arse, zType, i+1
2052e 2c 20 6e 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 , nResult);.
2052f 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
20530 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 74 65 }. pIte
20531 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 m->iCol = (u16)i
20532 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 Col;. conti
20533 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 nue;. }..
20534 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 72 /* Otherwise, tr
20535 65 61 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 eat the ORDER BY
20536 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72 64 69 term as an ordi
20537 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 nary expression
20538 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 */. pItem->iC
20539 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 ol = 0;. if(
2053a 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
2053b 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 prNames(pNC, pE)
2053c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
2053d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
2053e 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 return sqlite3Re
2053f 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
20540 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 y(pParse, pSelec
20541 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a 54 79 t, pOrderBy, zTy
20542 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 pe);.}../*.** Re
20543 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 solve names in t
20544 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
20545 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c 20 6f 66 ent p and all of
20546 20 69 74 73 20 64 65 73 63 65 6e 64 65 6e 74 73 its descendents
20547 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
20548 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 resolveSelectSte
20549 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 p(Walker *pWalke
2054a 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 r, Select *p){.
2054b 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f NameContext *pO
2054c 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 uterNC; /* Cont
2054d 65 78 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ext that contain
2054e 73 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f s this SELECT */
2054f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 . NameContext s
20550 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 NC; /* Na
20551 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 me context of th
20552 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 is SELECT */. i
20553 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 nt isCompound;
20554 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
20555 66 20 70 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e f p is a compoun
20556 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20 69 6e d select */. in
20557 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 t nCompound;
20558 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
20559 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 65 72 6d of compound term
2055a 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66 s processed so f
2055b 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 ar */. Parse *p
2055c 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 Parse;
2055d 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
2055e 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 xt */. ExprList
2055f 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 *pEList;
20560 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 20 65 78 /* Result set ex
20561 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f pression list */
20562 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
20563 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
20564 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
20565 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 ExprList *pGroup
20566 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 47 By; /* The G
20567 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a ROUP BY clause *
20568 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c 65 66 /. Select *pLef
20569 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c tmost; /* L
2056a 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 eft-most of SELE
2056b 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 CT of a compound
2056c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
2056d 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b; /*
2056e 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 Database connec
2056f 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 tion */. .. as
20570 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
20571 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 if( p->selFlags
20572 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 7b & SF_Resolved ){
20573 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f . return WRC_
20574 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 Prune;. }. pOu
20575 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d terNC = pWalker-
20576 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 72 73 65 >u.pNC;. pParse
20577 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 = pWalker->pPar
20578 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 se;. db = pPars
20579 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 e->db;.. /* Nor
2057a 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 53 65 6c mally sqlite3Sel
2057b 65 63 74 45 78 70 61 6e 64 28 29 20 77 69 6c 6c ectExpand() will
2057c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 73 74 be called first
2057d 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 65 0a 20 and will have.
2057e 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 70 61 ** already expa
2057f 6e 64 65 64 20 74 68 69 73 20 53 45 4c 45 43 54 nded this SELECT
20580 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 . However, if t
20581 68 69 73 20 69 73 20 61 20 73 75 62 71 75 65 72 his is a subquer
20582 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20 61 6e y within. ** an
20583 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c expression, sql
20584 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
20585 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 65 20 63 ames() will be c
20586 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 20 61 0a alled without a.
20587 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 ** prior call
20588 74 6f 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 to sqlite3Select
20589 45 78 70 61 6e 64 28 29 2e 20 20 57 68 65 6e 20 Expand(). When
2058a 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 6c 65 that happens, le
2058b 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 t. ** sqlite3Se
2058c 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20 61 6c lectPrep() do al
2058d 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 l of the process
2058e 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45 4c ing for this SEL
2058f 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 ECT.. ** sqlite
20590 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 77 69 3SelectPrep() wi
20591 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68 20 73 ll invoke both s
20592 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 qlite3SelectExpa
20593 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a 20 74 68 nd() and. ** th
20594 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 is routine in th
20595 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 2e e correct order.
20596 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e . */. if( (p->
20597 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 selFlags & SF_Ex
20598 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b 0a 20 20 panded)==0 ){.
20599 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 sqlite3SelectP
2059a 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 rep(pParse, p, p
2059b 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 72 65 OuterNC);. re
2059c 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 turn (pParse->nE
2059d 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 rr || db->malloc
2059e 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 Failed) ? WRC_Ab
2059f 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 6e 65 3b ort : WRC_Prune;
205a0 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 . }.. isCompou
205a1 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f 72 21 3d nd = p->pPrior!=
205a2 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 0;. nCompound =
205a3 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f 73 74 20 0;. pLeftmost
205a4 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 = p;. while( p
205a5 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 ){. assert( (
205a6 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
205a7 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29 3b _Expanded)!=0 );
205a8 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
205a9 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 >selFlags & SF_R
205aa 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 3b 0a 20 esolved)==0 );.
205ab 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c p->selFlags |
205ac 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a = SF_Resolved;..
205ad 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 /* Resolve t
205ae 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 he expressions i
205af 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 n the LIMIT and
205b0 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20 OFFSET clauses.
205b1 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 61 72 65 These. ** are
205b2 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 not allowed to
205b3 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e 61 6d refer to any nam
205b4 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e 20 65 es, so pass an e
205b5 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65 78 74 mpty NameContext
205b6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d .. */. mem
205b7 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a set(&sNC, 0, siz
205b8 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 eof(sNC));. s
205b9 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 NC.pParse = pPar
205ba 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 se;. if( sqli
205bb 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
205bc 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 mes(&sNC, p->pLi
205bd 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 mit) ||.
205be 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
205bf 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d prNames(&sNC, p-
205c0 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 20 >pOffset) ){.
205c1 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
205c2 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 ort;. }. .
205c3 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65 20 /* Set up the
205c4 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 local name-conte
205c5 78 74 20 74 6f 20 70 61 73 73 20 74 6f 20 73 71 xt to pass to sq
205c6 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
205c7 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 20 20 2a Names() to. *
205c8 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20 72 65 * resolve the re
205c9 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
205ca 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 20 2a 2f ion list.. */
205cb 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 . sNC.allowAg
205cc 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e 43 2e 70 g = 1;. sNC.p
205cd 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 SrcList = p->pSr
205ce 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 c;. sNC.pNext
205cf 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 0a = pOuterNC;. .
205d0 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e /* Resolve n
205d1 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65 73 75 ames in the resu
205d2 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20 20 70 lt set. */. p
205d3 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 EList = p->pELis
205d4 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 t;. assert( p
205d5 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 EList!=0 );.
205d6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 for(i=0; i<pELis
205d7 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a t->nExpr; i++){.
205d8 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d Expr *pX =
205d9 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 pEList->a[i].pE
205da 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 xpr;. if( s
205db 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
205dc 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 58 29 rNames(&sNC, pX)
205dd 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
205de 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
205df 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
205e0 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 6c /* Recursivel
205e1 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 y resolve names
205e2 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 in all subquerie
205e3 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 s. */. for
205e4 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d (i=0; i<p->pSrc-
205e5 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nSrc; i++){.
205e6 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 struct SrcLis
205e7 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 t_item *pItem =
205e8 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a &p->pSrc->a[i];.
205e9 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d if( pItem-
205ea 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 >pSelect ){.
205eb 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
205ec 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 20 3d 20 zSavedContext =
205ed 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e pParse->zAuthCon
205ee 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 text;. if
205ef 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 ( pItem->zName )
205f0 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f pParse->zAuthCo
205f1 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a ntext = pItem->z
205f2 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71 Name;. sq
205f3 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 lite3ResolveSele
205f4 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 ctNames(pParse,
205f5 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 pItem->pSelect,
205f6 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20 20 pOuterNC);.
205f7 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 pParse->zAuth
205f8 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64 Context = zSaved
205f9 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 Context;.
205fa 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 if( pParse->nEr
205fb 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 r || db->mallocF
205fc 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 57 ailed ) return W
205fd 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 RC_Abort;.
205fe 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
205ff 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e * If there are n
20600 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 o aggregate func
20601 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 72 65 73 tions in the res
20602 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 ult-set, and no
20603 47 52 4f 55 50 20 42 59 20 0a 20 20 20 20 2a 2a GROUP BY . **
20604 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 expression, do
20605 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 not allow aggreg
20606 61 74 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 ates in any of t
20607 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 he other express
20608 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ions.. */.
20609 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c assert( (p->sel
2060a 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 Flags & SF_Aggre
2060b 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 gate)==0 );.
2060c 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 pGroupBy = p->pG
2060d 72 6f 75 70 42 79 3b 0a 20 20 20 20 69 66 28 20 roupBy;. if(
2060e 70 47 72 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e pGroupBy || sNC.
2060f 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20 hasAgg ){.
20610 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 p->selFlags |= S
20611 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 F_Aggregate;.
20612 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e }else{. sN
20613 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a C.allowAgg = 0;.
20614 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
20615 49 66 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75 If a HAVING clau
20616 73 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74 se is present, t
20617 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 hen there must b
20618 65 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 e a GROUP BY cla
20619 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 use.. */.
2061a 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 if( p->pHaving &
2061b 26 20 21 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 & !pGroupBy ){.
2061c 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
2061d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 rMsg(pParse, "a
2061e 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
2061f 69 73 20 72 65 71 75 69 72 65 64 20 62 65 66 6f is required befo
20620 72 65 20 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 re HAVING");.
20621 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
20622 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 ort;. }. .
20623 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65 78 70 /* Add the exp
20624 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 ression list to
20625 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 the name-context
20626 20 62 65 66 6f 72 65 20 70 61 72 73 69 6e 67 20 before parsing
20627 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 the. ** other
20628 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 expressions in
20629 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 the SELECT state
2062a 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 73 6f ment. This is so
2062b 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 that. ** exp
2062c 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 ressions in the
2062d 57 48 45 52 45 20 63 6c 61 75 73 65 20 28 65 74 WHERE clause (et
2062e 63 2e 29 20 63 61 6e 20 72 65 66 65 72 20 74 6f c.) can refer to
2062f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 62 79 0a expressions by.
20630 20 20 20 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 ** aliases i
20631 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
20632 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
20633 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 Minor point: If
20634 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 this is the case
20635 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 , then the expre
20636 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 ssion will be.
20637 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 ** re-evaluate
20638 64 20 66 6f 72 20 65 61 63 68 20 72 65 66 65 72 d for each refer
20639 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 ence to it..
2063a 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 */. sNC.pELis
2063b 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 t = p->pEList;.
2063c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 if( sqlite3Re
2063d 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 solveExprNames(&
2063e 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 sNC, p->pWhere)
2063f 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 ||. sqlite
20640 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
20641 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 s(&sNC, p->pHavi
20642 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ng). ){.
20643 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
20644 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a t;. }.. /*
20645 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 61 6e The ORDER BY an
20646 64 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 d GROUP BY claus
20647 65 73 20 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 es may not refer
20648 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 to terms in.
20649 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 69 65 ** outer querie
2064a 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e s . */. sN
2064b 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 C.pNext = 0;.
2064c 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 sNC.allowAgg =
2064d 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 1;.. /* Proce
2064e 73 73 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 ss the ORDER BY
2064f 63 6c 61 75 73 65 20 66 6f 72 20 73 69 6e 67 6c clause for singl
20650 65 74 6f 6e 20 53 45 4c 45 43 54 20 73 74 61 74 eton SELECT stat
20651 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 ements.. ** T
20652 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
20653 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 se for compounds
20654 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
20655 74 73 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 ts is handled.
20656 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 ** below, afte
20657 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 r all of the res
20658 75 6c 74 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c ult-sets for all
20659 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 of the elements
2065a 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 of. ** the c
2065b 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 ompound have bee
2065c 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 n resolved..
2065d 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 43 6f */. if( !isCo
2065e 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 mpound && resolv
2065f 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 eOrderGroupBy(&s
20660 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 NC, p, p->pOrder
20661 42 79 2c 20 22 4f 52 44 45 52 22 29 20 29 7b 0a By, "ORDER") ){.
20662 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
20663 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
20664 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
20665 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
20666 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
20667 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f ;. }. . /
20668 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 47 52 * Resolve the GR
20669 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 OUP BY clause.
2066a 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 At the same time
2066b 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a 20 20 20 , make sure .
2066c 20 2a 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 ** the GROUP BY
2066d 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 clause does not
2066e 20 63 6f 6e 74 61 69 6e 20 61 67 67 72 65 67 61 contain aggrega
2066f 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 te functions..
20670 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 */. if( pGr
20671 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73 oupBy ){. s
20672 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
20673 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 tem *pItem;.
20674 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 6f 6c . if( resol
20675 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 26 veOrderGroupBy(&
20676 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 sNC, p, pGroupBy
20677 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 , "GROUP") || db
20678 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
20679 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
2067a 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
2067b 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d }. for(i=
2067c 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 0, pItem=pGroupB
2067d 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 y->a; i<pGroupBy
2067e 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 ->nExpr; i++, pI
2067f 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 tem++){.
20680 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
20681 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 rty(pItem->pExpr
20682 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 , EP_Agg) ){.
20683 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
20684 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
20685 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
20686 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f ons are not allo
20687 77 65 64 20 69 6e 20 22 0a 20 20 20 20 20 20 20 wed in ".
20688 20 20 20 20 20 20 20 22 74 68 65 20 47 52 4f 55 "the GROU
20689 50 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 P BY clause");.
2068a 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
2068b 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 WRC_Abort;.
2068c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2068d 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e }.. /* Advan
2068e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 ce to the next t
2068f 65 72 6d 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f erm of the compo
20690 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 und. */. p
20691 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 = p->pPrior;.
20692 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 nCompound++;.
20693 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 }.. /* Resolve
20694 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f 6e the ORDER BY on
20695 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 a compound SELE
20696 43 54 20 61 66 74 65 72 20 61 6c 6c 20 74 65 72 CT after all ter
20697 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 ms of. ** the c
20698 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62 65 65 ompound have bee
20699 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f n resolved.. */
2069a 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e . if( isCompoun
2069b 64 20 26 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 d && resolveComp
2069c 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 50 61 72 oundOrderBy(pPar
2069d 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 se, pLeftmost) )
2069e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 {. return WRC
2069f 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 _Abort;. }.. r
206a0 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b eturn WRC_Prune;
206a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
206a2 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 outine walks an
206a3 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 expression tree
206a4 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 72 65 66 and resolves ref
206a5 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 erences to.** ta
206a6 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 ble columns and
206a7 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d result-set colum
206a8 6e 73 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 ns. At the same
206a9 20 74 69 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a time, do error.
206aa 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 ** checking on f
206ab 75 6e 63 74 69 6f 6e 20 75 73 61 67 65 20 61 6e unction usage an
206ac 64 20 73 65 74 20 61 20 66 6c 61 67 20 69 66 20 d set a flag if
206ad 61 6e 79 20 61 67 67 72 65 67 61 74 65 20 66 75 any aggregate fu
206ae 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 nctions.** are s
206af 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 een..**.** To re
206b0 73 6f 6c 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 solve table colu
206b1 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 73 20 77 mns references w
206b2 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 e look for nodes
206b3 20 28 6f 72 20 73 75 62 74 72 65 65 73 29 20 6f (or subtrees) o
206b4 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 f the .** form X
206b5 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a .Y.Z or Y.Z or j
206b6 75 73 74 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a ust Z where.**.*
206b7 2a 20 20 20 20 20 20 58 3a 20 20 20 54 68 65 20 * X: The
206b8 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 name of a databa
206b9 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 se. Ex: "main"
206ba 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a or "temp" or.**
206bb 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 the s
206bc 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 ymbolic name ass
206bd 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 54 54 41 igned to an ATTA
206be 43 48 2d 65 64 20 64 61 74 61 62 61 73 65 2e 0a CH-ed database..
206bf 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 **.** Y:
206c0 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 The name of a ta
206c1 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c ble in a FROM cl
206c2 61 75 73 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 ause. Or in a t
206c3 72 69 67 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 rigger.**
206c4 20 20 20 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 one of the s
206c5 70 65 63 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c pecial names "ol
206c6 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a d" or "new"..**.
206c7 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20 54 68 65 ** Z: The
206c8 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d name of a colum
206c9 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a n in table Y..**
206ca 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20 61 74 20 .** The node at
206cb 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 the root of the
206cc 73 75 62 74 72 65 65 20 69 73 20 6d 6f 64 69 66 subtree is modif
206cd 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ied as follows:.
206ce 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 **.** Expr.op
206cf 20 20 20 20 20 20 20 20 43 68 61 6e 67 65 64 20 Changed
206d0 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 to TK_COLUMN.**
206d1 20 20 20 45 78 70 72 2e 70 54 61 62 20 20 20 20 Expr.pTab
206d2 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 Points to the
206d3 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 Table object for
206d4 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e X.Y.** Expr.
206d5 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f iColumn The co
206d6 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e lumn index in X.
206d7 59 2e 20 20 2d 31 20 66 6f 72 20 74 68 65 20 72 Y. -1 for the r
206d8 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 owid..** Expr
206d9 2e 69 54 61 62 6c 65 20 20 20 20 54 68 65 20 56 .iTable The V
206da 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 DBE cursor numbe
206db 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a r for X.Y.**.**.
206dc 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 ** To resolve re
206dd 73 75 6c 74 2d 73 65 74 20 72 65 66 65 72 65 6e sult-set referen
206de 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 ces, look for ex
206df 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f pression nodes o
206e0 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 f the.** form Z
206e1 28 77 69 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 (with no X and Y
206e2 20 70 72 65 66 69 78 29 20 77 68 65 72 65 20 74 prefix) where t
206e3 68 65 20 5a 20 6d 61 74 63 68 65 73 20 74 68 65 he Z matches the
206e4 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 right-hand.** s
206e5 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 ize of an AS cla
206e6 75 73 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c use in the resul
206e7 74 2d 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 t-set of a SELEC
206e8 54 2e 20 20 54 68 65 20 5a 20 65 78 70 72 65 73 T. The Z expres
206e9 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 sion.** is repla
206ea 63 65 64 20 62 79 20 61 20 63 6f 70 79 20 6f 66 ced by a copy of
206eb 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 the left-hand s
206ec 69 64 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c ide of the resul
206ed 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
206ee 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 ..** Table-name
206ef 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 and function res
206f0 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f olution occurs o
206f1 6e 20 74 68 65 20 73 75 62 73 74 69 74 75 74 65 n the substitute
206f2 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 d expression.**
206f3 74 72 65 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 tree. For examp
206f4 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 le, in:.**.**
206f5 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 SELECT a+b AS
206f6 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f x, c+d AS y FRO
206f7 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 78 3b M t1 ORDER BY x;
206f8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 .**.** The "x" t
206f9 65 72 6d 20 6f 66 20 74 68 65 20 6f 72 64 65 72 erm of the order
206fa 20 62 79 20 69 73 20 72 65 70 6c 61 63 65 64 20 by is replaced
206fb 62 79 20 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 by "a+b" to rend
206fc 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 er:.**.** S
206fd 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 ELECT a+b AS x,
206fe 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 c+d AS y FROM t1
206ff 20 4f 52 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a ORDER BY a+b;.*
20700 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 *.** Function ca
20701 6c 6c 73 20 61 72 65 20 63 68 65 63 6b 65 64 20 lls are checked
20702 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
20703 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 t the function i
20704 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e s .** defined an
20705 64 20 74 68 61 74 20 74 68 65 20 63 6f 72 72 65 d that the corre
20706 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 ct number of arg
20707 75 6d 65 6e 74 73 20 61 72 65 20 73 70 65 63 69 uments are speci
20708 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 fied..** If the
20709 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 function is an a
2070a 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
2070b 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 4e 43 2d n, then the pNC-
2070c 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 >hasAgg is.** se
2070d 74 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 t and the opcode
2070e 20 69 73 20 63 68 61 6e 67 65 64 20 66 72 6f 6d is changed from
2070f 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 TK_FUNCTION to
20710 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e TK_AGG_FUNCTION.
20711 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 72 65 73 .** If an expres
20712 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 sion contains ag
20713 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
20714 73 20 74 68 65 6e 20 74 68 65 20 45 50 5f 41 67 s then the EP_Ag
20715 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e g.** property on
20716 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20717 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e is set..**.** An
20718 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
20719 73 20 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 s left in pParse
2071a 20 69 66 20 61 6e 79 74 68 69 6e 67 20 69 73 20 if anything is
2071b 61 6d 69 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 amiss. The numb
2071c 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 er.** if errors
2071d 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
2071e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
2071f 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 nt sqlite3Resolv
20720 65 45 78 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e eExprNames( . N
20721 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
20722 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 /* Namesp
20723 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 ace to resolve e
20724 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a xpressions in. *
20725 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 /. Expr *pExpr
20726 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
20727 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f he expression to
20728 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f be analyzed. */
20729 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 65 64 48 .){. int savedH
2072a 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 asAgg;. Walker
2072b 77 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d w;.. if( pExpr=
2072c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 =0 ) return 0;.#
2072d 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 if SQLITE_MAX_EX
2072e 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 PR_DEPTH>0. {.
2072f 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 Parse *pParse
20730 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a = pNC->pParse;.
20731 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 if( sqlite3E
20732 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 70 xprCheckHeight(p
20733 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 Parse, pExpr->nH
20734 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 eight+pNC->pPars
20735 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 e->nHeight) ){.
20736 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
20737 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d }. pParse-
20738 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 45 78 70 >nHeight += pExp
20739 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a r->nHeight;. }.
2073a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 64 48 61 #endif. savedHa
2073b 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 sAgg = pNC->hasA
2073c 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 gg;. pNC->hasAg
2073d 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 g = 0;. w.xExpr
2073e 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c Callback = resol
2073f 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e veExprStep;. w.
20740 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
20741 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 = resolveSelectS
20742 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 tep;. w.pParse
20743 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 = pNC->pParse;.
20744 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a w.u.pNC = pNC;.
20745 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 sqlite3WalkExp
20746 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 r(&w, pExpr);.#i
20747 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 f SQLITE_MAX_EXP
20748 52 5f 44 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d R_DEPTH>0. pNC-
20749 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 >pParse->nHeight
2074a 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 -= pExpr->nHeig
2074b 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 ht;.#endif. if(
2074c 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 pNC->nErr>0 ||
2074d 77 2e 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 w.pParse->nErr>0
2074e 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 ){. ExprSetP
2074f 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
20750 50 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 P_Error);. }.
20751 69 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 if( pNC->hasAgg
20752 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 ){. ExprSetPr
20753 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
20754 5f 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 _Agg);. }else i
20755 66 28 20 73 61 76 65 64 48 61 73 41 67 67 20 29 f( savedHasAgg )
20756 7b 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 {. pNC->hasAg
20757 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 g = 1;. }. ret
20758 75 72 6e 20 45 78 70 72 48 61 73 50 72 6f 70 65 urn ExprHasPrope
20759 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 rty(pExpr, EP_Er
2075a 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 ror);.}.../*.**
2075b 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 Resolve all name
2075c 73 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 s in all express
2075d 69 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54 ions of a SELECT
2075e 20 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 and in all.** d
2075f 65 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 ecendents of the
20760 20 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 SELECT, includi
20761 6e 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 ng compounds off
20762 20 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a of p->pPrior,.*
20763 2a 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 * subqueries in
20764 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 expressions, and
20765 20 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 subqueries used
20766 20 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a as FROM clause.
20767 2a 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 ** terms..**.**
20768 53 65 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c See sqlite3Resol
20769 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 66 6f veExprNames() fo
2076a 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 r a description
2076b 6f 66 20 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a of the kinds of.
2076c 2a 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f ** transformatio
2076d 6e 73 20 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a ns that occur..*
2076e 2a 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 *.** All SELECT
2076f 73 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c statements shoul
20770 64 20 68 61 76 65 20 62 65 65 6e 20 65 78 70 61 d have been expa
20771 6e 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 nded using.** sq
20772 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e lite3SelectExpan
20773 64 28 29 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 d() prior to inv
20774 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 oking this routi
20775 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ne..*/.SQLITE_PR
20776 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20777 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e e3ResolveSelectN
20778 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 ames(. Parse *p
20779 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f Parse, /
2077a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e * The parser con
2077b 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 text */. Select
2077c 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
2077d 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 /* The SELECT s
2077e 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 tatement being c
2077f 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 oded. */. NameC
20780 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 ontext *pOuterNC
20781 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 /* Name contex
20782 74 20 66 6f 72 20 70 61 72 65 6e 74 20 53 45 4c t for parent SEL
20783 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ECT statement */
20784 0a 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a .){. Walker w;.
20785 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
20786 29 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c );. w.xExprCall
20787 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 back = resolveEx
20788 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c prStep;. w.xSel
20789 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 ectCallback = re
2078a 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b solveSelectStep;
2078b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 . w.pParse = pP
2078c 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 arse;. w.u.pNC
2078d 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 = pOuterNC;. sq
2078e 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
2078f 26 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a &w, p);.}../****
20790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
20791 66 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a f resolve.c ****
20792 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20793 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20794 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
20795 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
20796 20 66 69 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a file expr.c ***
20797 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20798 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20799 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
2079a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
2079b 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
2079c 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
2079d 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
2079e 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
2079f 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
207a0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
207a1 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
207a2 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
207a3 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
207a4 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
207a5 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
207a6 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
207a7 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
207a8 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
207a9 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
207aa 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
207ab 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
207ac 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
207ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
207b1 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
207b2 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 ontains routines
207b3 20 75 73 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a used for analyz
207b4 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ing expressions
207b5 61 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 and.** for gener
207b6 61 74 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20 ating VDBE code
207b7 74 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 65 that evaluates e
207b8 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 xpressions in SQ
207b9 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Lite..*/../*.**
207ba 52 65 74 75 72 6e 20 74 68 65 20 27 61 66 66 69 Return the 'affi
207bb 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65 78 70 nity' of the exp
207bc 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 66 ression pExpr if
207bd 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 any..**.** If p
207be 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e Expr is a column
207bf 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f , a reference to
207c0 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e a column via an
207c1 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 'AS' alias,.**
207c2 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 or a sub-select
207c3 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 with a column as
207c4 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
207c5 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 e, then the .**
207c6 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 affinity of that
207c7 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 column is retur
207c8 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ned. Otherwise,
207c9 30 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 64 0x00 is returned
207ca 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 ,.** indicating
207cb 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 no affinity for
207cc 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a the expression..
207cd 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 **.** i.e. the W
207ce 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 HERE clause expr
207cf 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 esssions in the
207d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d following statem
207d1 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 ents all.** have
207d2 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a an affinity:.**
207d3 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 .** CREATE TABLE
207d4 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 t1(a);.** SELEC
207d5 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 T * FROM t1 WHER
207d6 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 E a;.** SELECT a
207d7 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57 48 AS b FROM t1 WH
207d8 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 ERE b;.** SELECT
207d9 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 * FROM t1 WHERE
207da 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 (select a from
207db 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 t1);.*/.SQLITE_P
207dc 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 RIVATE char sqli
207dd 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 te3ExprAffinity(
207de 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 Expr *pExpr){.
207df 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e int op = pExpr->
207e0 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b op;. if( op==TK
207e1 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 61 _SELECT ){. a
207e2 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 66 6c ssert( pExpr->fl
207e3 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65 63 74 ags&EP_xIsSelect
207e4 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 );. return s
207e5 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 qlite3ExprAffini
207e6 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c ty(pExpr->x.pSel
207e7 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 ect->pEList->a[0
207e8 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 23 69 ].pExpr);. }.#i
207e9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
207ea 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f 70 3d T_CAST. if( op=
207eb 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20 20 20 =TK_CAST ){.
207ec 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
207ed 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
207ee 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a EP_IntValue) );.
207ef 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
207f0 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 e3AffinityType(p
207f1 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b Expr->u.zToken);
207f2 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
207f3 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f ( (op==TK_AGG_CO
207f4 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 LUMN || op==TK_C
207f5 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f OLUMN || op==TK_
207f6 52 45 47 49 53 54 45 52 29 20 0a 20 20 20 26 26 REGISTER) . &&
207f7 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 0a pExpr->pTab!=0.
207f8 20 20 29 7b 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d ){. /* op==
207f9 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 TK_REGISTER && p
207fa 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 20 68 61 Expr->pTab!=0 ha
207fb 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 ppens when pExpr
207fc 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a was originally.
207fd 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 ** a TK_COLU
207fe 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76 69 MN but was previ
207ff 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20 ously evaluated
20800 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61 20 and cached in a
20801 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 register */.
20802 69 6e 74 20 6a 20 3d 20 70 45 78 70 72 2d 3e 69 int j = pExpr->i
20803 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 66 28 20 Column;. if(
20804 6a 3c 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c j<0 ) return SQL
20805 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b ITE_AFF_INTEGER;
20806 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
20807 70 72 2d 3e 70 54 61 62 20 26 26 20 6a 3c 70 45 pr->pTab && j<pE
20808 78 70 72 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 xpr->pTab->nCol
20809 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 45 );. return pE
2080a 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b xpr->pTab->aCol[
2080b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 7d j].affinity;. }
2080c 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d . return pExpr-
2080d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a >affinity;.}../*
2080e 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c .** Set the coll
2080f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 ating sequence f
20810 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 or expression pE
20811 78 70 72 20 74 6f 20 62 65 20 74 68 65 20 63 6f xpr to be the co
20812 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 llating.** seque
20813 6e 63 65 20 6e 61 6d 65 64 20 62 79 20 70 54 6f nce named by pTo
20814 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e 20 61 20 ken. Return a
20815 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
20816 65 76 69 73 65 64 20 65 78 70 72 65 73 73 69 6f evised expressio
20817 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 n..** The collat
20818 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 ing sequence is
20819 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 6c 69 marked as "expli
2081a 63 69 74 22 20 75 73 69 6e 67 20 74 68 65 20 45 cit" using the E
2081b 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 P_ExpCollate.**
2081c 66 6c 61 67 2e 20 20 41 6e 20 65 78 70 6c 69 63 flag. An explic
2081d 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 it collating seq
2081e 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76 65 72 72 uence will overr
2081f 69 64 65 20 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 ide implicit.**
20820 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
20821 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ces..*/.SQLITE_P
20822 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
20823 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 ite3ExprSetColl(
20824 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
20825 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 xpr *pExpr, Toke
20826 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 n *pCollName){.
20827 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 char *zColl = 0
20828 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
20829 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 Dequoted name of
2082a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2082b 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 nce */. CollSeq
2082c 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 *pColl;. sqlit
2082d 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
2082e 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 >db;. zColl = s
2082f 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f qlite3NameFromTo
20830 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d ken(db, pCollNam
20831 65 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 e);. if( pExpr
20832 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 && zColl ){.
20833 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c pColl = sqlite3L
20834 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 ocateCollSeq(pPa
20835 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 rse, zColl);.
20836 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 if( pColl ){.
20837 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c pExpr->pColl
20838 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 = pColl;.
20839 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 pExpr->flags |=
2083a 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 EP_ExpCollate;.
2083b 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
2083c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f e3DbFree(db, zCo
2083d 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 ll);. return pE
2083e 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 xpr;.}../*.** Re
2083f 74 75 72 6e 20 74 68 65 20 64 65 66 61 75 6c 74 turn the default
20840 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
20841 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78 70 72 nce for the expr
20842 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 ession pExpr. If
20843 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 .** there is no
20844 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f default collatio
20845 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e 20 30 n type, return 0
20846 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20847 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c ATE CollSeq *sql
20848 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 ite3ExprCollSeq(
20849 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
2084a 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 xpr *pExpr){. C
2084b 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
2084c 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 70 0;. Expr *p = p
2084d 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28 20 41 Expr;. while( A
2084e 4c 57 41 59 53 28 70 29 20 29 7b 0a 20 20 20 20 LWAYS(p) ){.
2084f 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c int op;. pCol
20850 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 l = p->pColl;.
20851 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62 72 if( pColl ) br
20852 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70 2d eak;. op = p-
20853 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e >op;. if( p->
20854 70 54 61 62 21 3d 30 20 26 26 20 28 0a 20 20 20 pTab!=0 && (.
20855 20 20 20 20 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f op==TK_AGG_
20856 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b COLUMN || op==TK
20857 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 _COLUMN || op==T
20858 4b 5f 52 45 47 49 53 54 45 52 20 7c 7c 20 6f 70 K_REGISTER || op
20859 3d 3d 54 4b 5f 54 52 49 47 47 45 52 0a 20 20 20 ==TK_TRIGGER.
2085a 20 29 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70 )){. /* op
2085b 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26 ==TK_REGISTER &&
2085c 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 p->pTab!=0 happ
2085d 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 ens when pExpr w
2085e 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 as originally.
2085f 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 ** a TK_COLU
20860 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76 69 MN but was previ
20861 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20 ously evaluated
20862 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61 20 and cached in a
20863 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 register */.
20864 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
20865 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a oll;. int j
20866 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 = p->iColumn;.
20867 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b if( j>=0 ){
20868 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
20869 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
2086a 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c b;. zColl
2086b 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c = p->pTab->aCol
2086c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 [j].zColl;.
2086d 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
2086e 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 e3FindCollSeq(db
2086f 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c , ENC(db), zColl
20870 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 45 , 0);. pE
20871 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f xpr->pColl = pCo
20872 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ll;. }.
20873 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
20874 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 if( op!=TK_CA
20875 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c ST && op!=TK_UPL
20876 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 US ){. brea
20877 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d k;. }. p =
20878 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 p->pLeft;. }.
20879 20 69 66 28 20 73 71 6c 69 74 65 33 43 68 65 63 if( sqlite3Chec
2087a 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c kCollSeq(pParse,
2087b 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 pColl) ){ .
2087c 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 pColl = 0;. }.
2087d 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d return pColl;.}
2087e 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 ../*.** pExpr is
2087f 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 an operand of a
20880 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 comparison oper
20881 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20 74 ator. aff2 is t
20882 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e he.** type affin
20883 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 ity of the other
20884 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20 operand. This
20885 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
20886 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 the.** type affi
20887 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 nity that should
20888 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 be used for the
20889 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 comparison oper
2088a 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ator..*/.SQLITE_
2088b 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c PRIVATE char sql
2088c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e ite3CompareAffin
2088d 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c ity(Expr *pExpr,
2088e 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20 63 char aff2){. c
2088f 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69 74 har aff1 = sqlit
20890 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 e3ExprAffinity(p
20891 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66 66 Expr);. if( aff
20892 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20 20 1 && aff2 ){.
20893 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20 6f /* Both sides o
20894 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e f the comparison
20895 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 are columns. If
20896 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69 63 one has numeric
20897 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 . ** affinity
20898 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68 65 , use that. Othe
20899 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66 66 rwise use no aff
2089a 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 inity.. */.
2089b 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e if( sqlite3IsN
2089c 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 umericAffinity(a
2089d 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 ff1) || sqlite3I
2089e 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 sNumericAffinity
2089f 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20 20 (aff2) ){.
208a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 return SQLITE_AF
208a1 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d F_NUMERIC;. }
208a2 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 else{. retu
208a3 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f rn SQLITE_AFF_NO
208a4 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 NE;. }. }els
208a5 65 20 69 66 28 20 21 61 66 66 31 20 26 26 20 21 e if( !aff1 && !
208a6 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e aff2 ){. /* N
208a7 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 either side of t
208a8 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 he comparison is
208a9 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 a column. Comp
208aa 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 are the. ** r
208ab 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79 2e esults directly.
208ac 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 . */. retu
208ad 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f rn SQLITE_AFF_NO
208ae 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 NE;. }else{.
208af 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73 20 /* One side is
208b0 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 a column, the ot
208b1 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 her is not. Use
208b2 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 the columns affi
208b3 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 nity. */. ass
208b4 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 ert( aff1==0 ||
208b5 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 aff2==0 );. r
208b6 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61 66 eturn (aff1 + af
208b7 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a f2);. }.}../*.*
208b8 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d * pExpr is a com
208b9 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
208ba 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 79 . Return the ty
208bb 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 pe affinity that
208bc 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 should.** be ap
208bd 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 plied to both op
208be 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 erands prior to
208bf 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 doing the compar
208c0 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ison..*/.static
208c1 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 char comparisonA
208c2 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 ffinity(Expr *pE
208c3 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66 66 xpr){. char aff
208c4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 ;. assert( pExp
208c5 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 r->op==TK_EQ ||
208c6 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e pExpr->op==TK_IN
208c7 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 || pExpr->op==T
208c8 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 K_LT ||.
208c9 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f pExpr->op==TK_
208ca 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d GT || pExpr->op=
208cb 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d =TK_GE || pExpr-
208cc 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 >op==TK_LE ||.
208cd 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f pExpr->o
208ce 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20 70 45 78 70 p==TK_NE || pExp
208cf 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 7c 7c 20 r->op==TK_IS ||
208d0 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 pExpr->op==TK_IS
208d1 4e 4f 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 NOT );. assert(
208d2 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29 3b pExpr->pLeft );
208d3 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 . aff = sqlite3
208d4 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 ExprAffinity(pEx
208d5 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 pr->pLeft);. if
208d6 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 ( pExpr->pRight
208d7 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71 6c ){. aff = sql
208d8 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e ite3CompareAffin
208d9 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67 68 ity(pExpr->pRigh
208da 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 t, aff);. }else
208db 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
208dc 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
208dd 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 IsSelect) ){.
208de 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f aff = sqlite3Co
208df 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 mpareAffinity(pE
208e0 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e xpr->x.pSelect->
208e1 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
208e2 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 pr, aff);. }els
208e3 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20 20 e if( !aff ){.
208e4 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 aff = SQLITE_A
208e5 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 FF_NONE;. }. r
208e6 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a eturn aff;.}../*
208e7 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 .** pExpr is a c
208e8 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65 73 omparison expres
208e9 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 27 sion, eg. '=', '
208ea 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63 2e <', IN(...) etc.
208eb 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74 79 .** idx_affinity
208ec 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74 79 is the affinity
208ed 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 63 of an indexed c
208ee 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74 72 olumn. Return tr
208ef 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e 64 ue.** if the ind
208f0 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74 79 ex with affinity
208f1 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d 61 idx_affinity ma
208f2 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 y be used to imp
208f3 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 6f lement.** the co
208f4 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78 70 mparison in pExp
208f5 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
208f6 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
208f7 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 IndexAffinityOk(
208f8 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 Expr *pExpr, cha
208f9 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 7b r idx_affinity){
208fa 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f . char aff = co
208fb 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 mparisonAffinity
208fc 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74 63 (pExpr);. switc
208fd 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63 61 h( aff ){. ca
208fe 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f se SQLITE_AFF_NO
208ff 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e NE:. return
20900 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 1;. case SQL
20901 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20 20 ITE_AFF_TEXT:.
20902 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f 61 return idx_a
20903 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
20904 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64 65 AFF_TEXT;. de
20905 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65 74 fault:. ret
20906 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75 6d urn sqlite3IsNum
20907 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64 78 ericAffinity(idx
20908 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d 0a _affinity);. }.
20909 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
2090a 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68 61 the P5 value tha
2090b 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
2090c 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63 6f for a binary co
2090d 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f mparison.** opco
2090e 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 de (OP_Eq, OP_Ge
2090f 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20 63 etc.) used to c
20910 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61 6e ompare pExpr1 an
20911 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 d pExpr2..*/.sta
20912 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f 6d tic u8 binaryCom
20913 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45 78 pareP5(Expr *pEx
20914 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70 72 pr1, Expr *pExpr
20915 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 2, int jumpIfNul
20916 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20 28 l){. u8 aff = (
20917 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70 72 char)sqlite3Expr
20918 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 29 Affinity(pExpr2)
20919 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73 71 ;. aff = (u8)sq
2091a 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
2091b 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 66 nity(pExpr1, aff
2091c 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e 75 ) | (u8)jumpIfNu
2091d 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66 66 ll;. return aff
2091e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
2091f 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
20920 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 he collation seq
20921 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 6c uence that shoul
20922 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 d be used by.**
20923 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 a binary compari
20924 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d son operator com
20925 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e 64 paring pLeft and
20926 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 pRight..**.** I
20927 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20 f the left hand
20928 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 expression has a
20929 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
2092a 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 69 nce type, then i
2092b 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 t is.** used. Ot
2092c 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c 6c herwise the coll
2092d 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 ation sequence f
2092e 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61 6e or the right han
2092f 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 d expression.**
20930 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 20 is used, or the
20931 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 29 default (BINARY)
20932 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 72 if neither expr
20933 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c ession has a col
20934 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a lating.** type..
20935 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 **.** Argument p
20936 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 70 Right (but not p
20937 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 6e Left) may be a n
20938 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20 ull pointer. In
20939 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74 this case,.** it
2093a 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 is not consider
2093b 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
2093c 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
2093d 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 qlite3BinaryComp
2093e 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61 areCollSeq(. Pa
2093f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 rse *pParse, .
20940 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20 Expr *pLeft, .
20941 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a Expr *pRight.){.
20942 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
20943 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 ;. assert( pLef
20944 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 t );. if( pLeft
20945 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 ->flags & EP_Exp
20946 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 Collate ){. a
20947 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 43 ssert( pLeft->pC
20948 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c oll );. pColl
20949 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b = pLeft->pColl;
2094a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 69 . }else if( pRi
2094b 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 66 ght && pRight->f
2094c 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c lags & EP_ExpCol
2094d 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 late ){. asse
2094e 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c rt( pRight->pCol
2094f 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d l );. pColl =
20950 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a pRight->pColl;.
20951 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f }else{. pCo
20952 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ll = sqlite3Expr
20953 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
20954 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20 pLeft);. if(
20955 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 !pColl ){.
20956 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 pColl = sqlite3E
20957 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 xprCollSeq(pPars
20958 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 e, pRight);.
20959 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
2095a 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 Coll;.}../*.** G
2095b 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65 72 enerate the oper
2095c 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70 61 ands for a compa
2095d 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 2e rison operation.
2095e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 Before.** gene
2095f 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 rating the code
20960 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e 64 for each operand
20961 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e 79 , set the EP_Any
20962 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 Aff.** flag on t
20963 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f he expression so
20964 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 that it will be
20965 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61 0a able to used a.
20966 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e ** cached column
20967 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73 20 value that has
20968 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65 72 previously under
20969 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69 6e gone an.** affin
2096a 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 ity change..*/.s
2096b 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 43 tatic void codeC
2096c 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 0a ompareOperands(.
2096d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
2096e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 /* Parsing a
2096f 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 nd code generati
20970 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
20971 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 Expr *pLeft,
20972 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 /* The left op
20973 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a erand */. int *
20974 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a 20 pRegLeft, /*
20975 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 6c Register where l
20976 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 eft operand is s
20977 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a tored */. int *
20978 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a 20 pFreeLeft, /*
20979 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73 74 Free this regist
2097a 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a er when done */.
2097b 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 Expr *pRight,
2097c 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 /* The right
2097d 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
2097e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20 20 t *pRegRight,
2097f 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72 /* Register wher
20980 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 e right operand
20981 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 is stored */. i
20982 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20 20 nt *pFreeRight
20983 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20 72 /* Write temp r
20984 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67 68 egister for righ
20985 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65 20 t operand there
20986 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 */.){. while( p
20987 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c Left->op==TK_UPL
20988 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c 65 US ) pLeft = pLe
20989 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c 65 ft->pLeft;. pLe
2098a 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ft->flags |= EP_
2098b 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 4c AnyAff;. *pRegL
2098c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 eft = sqlite3Exp
2098d 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
2098e 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c 65 , pLeft, pFreeLe
2098f 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 52 ft);. while( pR
20990 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c ight->op==TK_UPL
20991 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70 52 US ) pRight = pR
20992 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 ight->pLeft;. p
20993 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 Right->flags |=
20994 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 EP_AnyAff;. *pR
20995 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 egRight = sqlite
20996 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
20997 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70 46 arse, pRight, pF
20998 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a reeRight);.}../*
20999 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
2099a 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 e for a comparis
2099b 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a on operator..*/.
2099c 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 43 static int codeC
2099d 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65 20 ompare(. Parse
2099e 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 54 *pParse, /* T
2099f 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64 20 he parsing (and
209a0 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 29 code generating)
209a1 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 context */. Ex
209a2 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 pr *pLeft,
209a3 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 /* The left oper
209a4 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 and */. Expr *p
209a5 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 Right, /* Th
209a6 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 e right operand
209a7 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 2c */. int opcode,
209a8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
209a9 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65 20 mparison opcode
209aa 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69 6e */. int in1, in
209ab 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73 74 t in2, /* Regist
209ac 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72 61 er holding opera
209ad 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 nds */. int des
209ae 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 t, /* Ju
209af 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65 2e mp here if true.
209b0 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 49 */. int jumpI
209b1 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20 74 fNull /* If t
209b2 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69 74 rue, jump if eit
209b3 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e her operand is N
209b4 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ULL */.){. int
209b5 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a p5;. int addr;.
209b6 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a CollSeq *p4;..
209b7 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42 69 p4 = sqlite3Bi
209b8 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 naryCompareCollS
209b9 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 eq(pParse, pLeft
209ba 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35 20 , pRight);. p5
209bb 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 = binaryCompareP
209bc 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 5(pLeft, pRight,
209bd 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 jumpIfNull);.
209be 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 addr = sqlite3Vd
209bf 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 2d beAddOp4(pParse-
209c0 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20 >pVdbe, opcode,
209c1 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0a in2, dest, in1,.
209c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
209c3 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 (void
209c4 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 *)p4, P4_COLLSEQ
209c5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
209c6 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65 2d ChangeP5(pParse-
209c7 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29 3b >pVdbe, (u8)p5);
209c8 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51 4c . if( (p5 & SQL
209c9 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d 53 ITE_AFF_MASK)!=S
209ca 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 QLITE_AFF_NONE )
209cb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 {. sqlite3Exp
209cc 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
209cd 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 31 ange(pParse, in1
209ce 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 1);. sqlite
209cf 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 3ExprCacheAffini
209d0 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c tyChange(pParse,
209d1 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 in2, 1);. }.
209d2 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a return addr;.}..
209d3 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 #if SQLITE_MAX_E
209d4 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a XPR_DEPTH>0./*.*
209d5 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 67 * Check that arg
209d6 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 73 ument nHeight is
209d7 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
209d8 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d ual to the maxim
209d9 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e um.** expression
209da 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20 depth allowed.
209db 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65 If it is not, le
209dc 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ave an error mes
209dd 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73 sage in.** pPars
209de 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
209df 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
209e0 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
209e1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
209e2 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 nt nHeight){. i
209e3 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
209e4 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 68 K;. int mxHeigh
209e5 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e t = pParse->db->
209e6 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
209e7 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b MIT_EXPR_DEPTH];
209e8 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e 6d . if( nHeight>m
209e9 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 73 xHeight ){. s
209ea 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
209eb 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 Parse, . "
209ec 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 Expression tree
209ed 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61 is too large (ma
209ee 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 22 ximum depth %d)"
209ef 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 29 , mxHeight. )
209f0 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
209f1 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 E_ERROR;. }. r
209f2 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 eturn rc;.}../*
209f3 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 The following th
209f4 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 ree functions, h
209f5 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68 eightOfExpr(), h
209f6 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 eightOfExprList(
209f7 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f ).** and heightO
209f8 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75 fSelect(), are u
209f9 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
209fa 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 the maximum hei
209fb 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 ght.** of any ex
209fc 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 pression tree re
209fd 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 ferenced by the
209fe 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 structure passed
209ff 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 as the.** first
20a00 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
20a01 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d If this maximum
20a02 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 height is great
20a03 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 er than the curr
20a04 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 ent value pointe
20a05 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 d.** to by pnHei
20a06 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 ght, the second
20a07 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 parameter, then
20a08 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f set *pnHeight to
20a09 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a that.** value..
20a0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 */.static void h
20a0b 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72 eightOfExpr(Expr
20a0c 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 *p, int *pnHeig
20a0d 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a ht){. if( p ){.
20a0e 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 if( p->nHeig
20a0f 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a ht>*pnHeight ){.
20a10 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20 *pnHeight
20a11 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 = p->nHeight;.
20a12 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 }. }.}.static
20a13 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 void heightOfEx
20a14 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 prList(ExprList
20a15 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 *p, int *pnHeigh
20a16 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 t){. if( p ){.
20a17 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
20a18 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 r(i=0; i<p->nExp
20a19 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 r; i++){. h
20a1a 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 eightOfExpr(p->a
20a1b 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 [i].pExpr, pnHei
20a1c 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ght);. }. }.
20a1d 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 }.static void he
20a1e 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c ightOfSelect(Sel
20a1f 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 ect *p, int *pnH
20a20 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 eight){. if( p
20a21 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 ){. heightOfE
20a22 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 xpr(p->pWhere, p
20a23 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
20a24 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 ightOfExpr(p->pH
20a25 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 aving, pnHeight)
20a26 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
20a27 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e pr(p->pLimit, pn
20a28 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 Height);. hei
20a29 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 ghtOfExpr(p->pOf
20a2a 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b fset, pnHeight);
20a2b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 . heightOfExp
20a2c 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c rList(p->pEList,
20a2d 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
20a2e 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
20a2f 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e (p->pGroupBy, pn
20a30 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 Height);. hei
20a31 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d ghtOfExprList(p-
20a32 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69 >pOrderBy, pnHei
20a33 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 ght);. height
20a34 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 OfSelect(p->pPri
20a35 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 or, pnHeight);.
20a36 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 }.}../*.** Set
20a37 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 the Expr.nHeight
20a38 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 variable in the
20a39 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 structure passe
20a3a 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 d as an .** argu
20a3b 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73 ment. An express
20a3c 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c ion with no chil
20a3d 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 dren, Expr.pList
20a3e 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 or .** Expr.pSe
20a3f 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20 lect member has
20a40 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41 a height of 1. A
20a41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 ny other express
20a42 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 ion.** has a hei
20a43 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65 ght equal to the
20a44 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 maximum height
20a45 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a of any other .**
20a46 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72 referenced Expr
20a47 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 plus one..*/.st
20a48 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 65 atic void exprSe
20a49 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 29 tHeight(Expr *p)
20a4a 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 {. int nHeight
20a4b 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 = 0;. heightOfE
20a4c 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e xpr(p->pLeft, &n
20a4d 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68 Height);. heigh
20a4e 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68 tOfExpr(p->pRigh
20a4f 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 t, &nHeight);.
20a50 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
20a51 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c rty(p, EP_xIsSel
20a52 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69 67 ect) ){. heig
20a53 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78 2e htOfSelect(p->x.
20a54 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 68 pSelect, &nHeigh
20a55 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
20a56 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
20a57 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 6e t(p->x.pList, &n
20a58 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70 Height);. }. p
20a59 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69 ->nHeight = nHei
20a5a 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a ght + 1;.}../*.*
20a5b 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e * Set the Expr.n
20a5c 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 20 Height variable
20a5d 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53 65 using the exprSe
20a5e 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74 69 tHeight() functi
20a5f 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68 65 on. If.** the he
20a60 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20 ight is greater
20a61 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d than the maximum
20a62 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73 73 allowed express
20a63 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c 65 ion depth,.** le
20a64 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 ave an error in
20a65 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 pParse..*/.SQLIT
20a66 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20a67 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 qlite3ExprSetHei
20a68 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 ght(Parse *pPars
20a69 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 65 e, Expr *p){. e
20a6a 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29 3b xprSetHeight(p);
20a6b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 . sqlite3ExprCh
20a6c 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 eckHeight(pParse
20a6d 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 7d , p->nHeight);.}
20a6e 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
20a6f 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 he maximum heigh
20a70 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73 t of any express
20a71 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e ion tree referen
20a72 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 65 ced.** by the se
20a73 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70 lect statement p
20a74 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 assed as an argu
20a75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ment..*/.SQLITE_
20a76 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
20a77 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 te3SelectExprHei
20a78 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a ght(Select *p){.
20a79 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 int nHeight =
20a7a 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 6c 0;. heightOfSel
20a7b 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74 29 ect(p, &nHeight)
20a7c 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69 67 ;. return nHeig
20a7d 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 ht;.}.#else. #d
20a7e 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65 69 efine exprSetHei
20a7f 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f 2a ght(y).#endif /*
20a80 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 SQLITE_MAX_EXPR
20a81 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a _DEPTH>0 */../*.
20a82 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
20a83 69 73 20 74 68 65 20 63 6f 72 65 20 61 6c 6c 6f is the core allo
20a84 63 61 74 6f 72 20 66 6f 72 20 45 78 70 72 20 6e cator for Expr n
20a85 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 odes..**.** Cons
20a86 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 truct a new expr
20a87 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 ession node and
20a88 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
20a89 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a to it. Memory.
20a8a 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 ** for this node
20a8b 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70 54 6f and for the pTo
20a8c 6b 65 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ken argument is
20a8d 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 a single allocat
20a8e 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 ion.** obtained
20a8f 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 from sqlite3DbMa
20a90 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 61 6c lloc(). The cal
20a91 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a ling function.**
20a92 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
20a93 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 for making sure
20a94 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75 61 the node eventua
20a95 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e 0a lly gets freed..
20a96 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 **.** If dequote
20a97 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
20a98 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 74 20 he token (if it
20a99 65 78 69 73 74 73 29 20 69 73 20 64 65 71 75 6f exists) is dequo
20a9a 74 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71 75 6f ted..** If dequo
20a9b 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e 6f 20 te is false, no
20a9c 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70 65 72 dequoting is per
20a9d 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65 20 64 formance. The d
20a9e 65 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61 6d 65 eQuote.** parame
20a9f 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 69 ter is ignored i
20aa0 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c f pToken is NULL
20aa1 20 6f 72 20 69 66 20 74 68 65 20 74 6f 6b 65 6e or if the token
20aa2 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 70 70 does not.** app
20aa3 65 61 72 20 74 6f 20 62 65 20 71 75 6f 74 65 64 ear to be quoted
20aa4 2e 20 20 49 66 20 74 68 65 20 71 75 6f 74 65 73 . If the quotes
20aa5 20 77 65 72 65 20 6f 66 20 74 68 65 20 66 6f 72 were of the for
20aa6 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c 65 2d m "..." (double-
20aa7 71 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65 6e 20 quotes).** then
20aa8 74 68 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 the EP_DblQuoted
20aa9 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20 flag is set on
20aaa 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e the expression n
20aab 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 ode..**.** Speci
20aac 61 6c 20 63 61 73 65 3a 20 20 49 66 20 6f 70 3d al case: If op=
20aad 3d 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e 64 20 =TK_INTEGER and
20aae 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20 74 6f pToken points to
20aaf 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 0a 2a a string that.*
20ab0 2a 20 63 61 6e 20 62 65 20 74 72 61 6e 73 6c 61 * can be transla
20ab1 74 65 64 20 69 6e 74 6f 20 61 20 33 32 2d 62 69 ted into a 32-bi
20ab2 74 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 t integer, then
20ab3 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e 6f 74 the token is not
20ab4 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 75 2e .** stored in u.
20ab5 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65 61 64 zToken. Instead
20ab6 2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 , the integer va
20ab7 6c 75 65 73 20 69 73 20 77 72 69 74 74 65 6e 0a lues is written.
20ab8 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c 75 65 ** into u.iValue
20ab9 20 61 6e 64 20 74 68 65 20 45 50 5f 49 6e 74 56 and the EP_IntV
20aba 61 6c 75 65 20 66 6c 61 67 20 69 73 20 73 65 74 alue flag is set
20abb 2e 20 20 4e 6f 20 65 78 74 72 61 20 73 74 6f 72 . No extra stor
20abc 61 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 age.** is alloca
20abd 74 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ted to hold the
20abe 69 6e 74 65 67 65 72 20 74 65 78 74 20 61 6e 64 integer text and
20abf 20 74 68 65 20 64 65 71 75 6f 74 65 20 66 6c 61 the dequote fla
20ac0 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f g is ignored..*/
20ac1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20ac2 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
20ac3 72 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 rAlloc(. sqlite
20ac4 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
20ac5 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 /* Handle for
20ac6 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
20ac7 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75 ero() (may be nu
20ac8 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c ll) */. int op,
20ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20aca 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f /* Expression o
20acb 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 pcode */. const
20acc 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 Token *pToken,
20acd 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 /* Token argu
20ace 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20 ment. Might be
20acf 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 64 65 NULL */. int de
20ad0 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 20 quote
20ad1 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 65 71 /* True to deq
20ad2 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 uote */.){. Exp
20ad3 72 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e r *pNew;. int n
20ad4 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 69 6e 74 Extra = 0;. int
20ad5 20 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a 20 20 iValue = 0;..
20ad6 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 if( pToken ){.
20ad7 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 54 if( op!=TK_INT
20ad8 45 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e EGER || pToken->
20ad9 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 7c z==0. |
20ada 7c 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 | sqlite3GetInt3
20adb 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56 2(pToken->z, &iV
20adc 61 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 alue)==0 ){.
20add 20 20 6e 45 78 74 72 61 20 3d 20 70 54 6f 6b 65 nExtra = pToke
20ade 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 n->n+1;. }.
20adf 7d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 }. pNew = sqlit
20ae0 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
20ae1 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 2b b, sizeof(Expr)+
20ae2 6e 45 78 74 72 61 29 3b 0a 20 20 69 66 28 20 70 nExtra);. if( p
20ae3 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d New ){. pNew-
20ae4 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 >op = (u8)op;.
20ae5 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d pNew->iAgg = -
20ae6 31 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 6b 65 1;. if( pToke
20ae7 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e n ){. if( n
20ae8 45 78 74 72 61 3d 3d 30 20 29 7b 0a 20 20 20 20 Extra==0 ){.
20ae9 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 pNew->flags
20aea 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a |= EP_IntValue;.
20aeb 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e pNew->u.
20aec 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75 65 3b iValue = iValue;
20aed 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
20aee 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 int c;.
20aef 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f pNew->u.zTo
20af0 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 70 4e ken = (char*)&pN
20af1 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d ew[1];. m
20af2 65 6d 63 70 79 28 70 4e 65 77 2d 3e 75 2e 7a 54 emcpy(pNew->u.zT
20af3 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 7a 2c oken, pToken->z,
20af4 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 pToken->n);.
20af5 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f pNew->u.zTo
20af6 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d ken[pToken->n] =
20af7 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0;. if(
20af8 64 65 71 75 6f 74 65 20 26 26 20 6e 45 78 74 72 dequote && nExtr
20af9 61 3e 3d 33 20 0a 20 20 20 20 20 20 20 20 20 20 a>=3 .
20afa 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 6f 6b && ((c = pTok
20afb 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27 20 en->z[0])=='\''
20afc 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 3d 3d || c=='"' || c==
20afd 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 20 29 '[' || c=='`') )
20afe 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
20aff 74 65 33 44 65 71 75 6f 74 65 28 70 4e 65 77 2d te3Dequote(pNew-
20b00 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 >u.zToken);.
20b01 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 22 27 if( c=='"'
20b02 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c ) pNew->flags |
20b03 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 3b 0a = EP_DblQuoted;.
20b04 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
20b05 7d 0a 20 20 20 20 7d 0a 23 69 66 20 53 51 4c 49 }. }.#if SQLI
20b06 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
20b07 48 3e 30 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 48 H>0. pNew->nH
20b08 65 69 67 68 74 20 3d 20 31 3b 0a 23 65 6e 64 69 eight = 1;.#endi
20b09 66 20 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e f . }. return
20b0a 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pNew;.}../*.**
20b0b 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 65 Allocate a new e
20b0c 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 xpression node f
20b0d 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 rom a zero-termi
20b0e 6e 61 74 65 64 20 74 6f 6b 65 6e 20 74 68 61 74 nated token that
20b0f 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 has.** already
20b10 62 65 65 6e 20 64 65 71 75 6f 74 65 64 2e 0a 2a been dequoted..*
20b11 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20b12 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
20b13 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 pr(. sqlite3 *d
20b14 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
20b15 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 Handle for sqli
20b16 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
20b17 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 ) (may be null)
20b18 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
20b19 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20b1a 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 Expression opcod
20b1b 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
20b1c 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 20 20 20 2f r *zToken /
20b1d 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 * Token argument
20b1e 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c . Might be NULL
20b1f 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 78 */.){. Token x
20b20 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e ;. x.z = zToken
20b21 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e ;. x.n = zToken
20b22 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e ? sqlite3Strlen
20b23 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b 0a 30(zToken) : 0;.
20b24 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
20b25 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 6f 70 ExprAlloc(db, op
20b26 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a , &x, 0);.}../*.
20b27 2a 2a 20 41 74 74 61 63 68 20 73 75 62 74 72 65 ** Attach subtre
20b28 65 73 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 es pLeft and pRi
20b29 67 68 74 20 74 6f 20 74 68 65 20 45 78 70 72 20 ght to the Expr
20b2a 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a node pRoot..**.*
20b2b 2a 20 49 66 20 70 52 6f 6f 74 3d 3d 4e 55 4c 4c * If pRoot==NULL
20b2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 that means that
20b2d 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
20b2e 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f tion error has o
20b2f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 49 6e 20 74 ccurred..** In t
20b30 68 61 74 20 63 61 73 65 2c 20 64 65 6c 65 74 65 hat case, delete
20b31 20 74 68 65 20 73 75 62 74 72 65 65 73 20 70 4c the subtrees pL
20b32 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 0a eft and pRight..
20b33 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20b34 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
20b35 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 prAttachSubtrees
20b36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
20b37 0a 20 20 45 78 70 72 20 2a 70 52 6f 6f 74 2c 0a . Expr *pRoot,.
20b38 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 0a 20 Expr *pLeft,.
20b39 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b Expr *pRight.){
20b3a 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d 30 20 . if( pRoot==0
20b3b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 ){. assert( d
20b3c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
20b3d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 );. sqlite3Ex
20b3e 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 prDelete(db, pLe
20b3f 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ft);. sqlite3
20b40 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
20b41 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b Right);. }else{
20b42 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20 . if( pRight
20b43 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e ){. pRoot->
20b44 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b pRight = pRight;
20b45 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 . if( pRigh
20b46 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 t->flags & EP_Ex
20b47 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 pCollate ){.
20b48 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 pRoot->flags
20b49 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 |= EP_ExpCollat
20b4a 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 e;. pRoot
20b4b 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74 ->pColl = pRight
20b4c 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d ->pColl;. }
20b4d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
20b4e 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 70 52 Left ){. pR
20b4f 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 oot->pLeft = pLe
20b50 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c ft;. if( pL
20b51 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f eft->flags & EP_
20b52 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 ExpCollate ){.
20b53 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 pRoot->fla
20b54 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c gs |= EP_ExpColl
20b55 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f ate;. pRo
20b56 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 ot->pColl = pLef
20b57 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 t->pColl;.
20b58 7d 0a 20 20 20 20 7d 0a 20 20 20 20 65 78 70 72 }. }. expr
20b59 53 65 74 48 65 69 67 68 74 28 70 52 6f 6f 74 29 SetHeight(pRoot)
20b5a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
20b5b 6c 6c 6f 63 61 74 65 20 61 20 45 78 70 72 20 6e llocate a Expr n
20b5c 6f 64 65 20 77 68 69 63 68 20 6a 6f 69 6e 73 20 ode which joins
20b5d 61 73 20 6d 61 6e 79 20 61 73 20 74 77 6f 20 73 as many as two s
20b5e 75 62 74 72 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f ubtrees..**.** O
20b5f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 66 20 74 68 ne or both of th
20b60 65 20 73 75 62 74 72 65 65 73 20 63 61 6e 20 62 e subtrees can b
20b61 65 20 4e 55 4c 4c 2e 20 20 52 65 74 75 72 6e 20 e NULL. Return
20b62 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
20b63 20 6e 65 77 0a 2a 2a 20 45 78 70 72 20 6e 6f 64 new.** Expr nod
20b64 65 2e 20 20 4f 72 2c 20 69 66 20 61 6e 20 4f 4f e. Or, if an OO
20b65 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 M error occurs,
20b66 73 65 74 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e set pParse->db->
20b67 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2c 0a 2a 2a mallocFailed,.**
20b68 20 66 72 65 65 20 74 68 65 20 73 75 62 74 72 65 free the subtre
20b69 65 73 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 es and return NU
20b6a 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 LL..*/.SQLITE_PR
20b6b 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
20b6c 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72 73 te3PExpr(. Pars
20b6d 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 e *pParse,
20b6e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
20b6f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
20b70 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 op,
20b71 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
20b72 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 n opcode */. Ex
20b73 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 pr *pLeft,
20b74 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 /* Left op
20b75 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 erand */. Expr
20b76 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 *pRight,
20b77 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 /* Right oper
20b78 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 and */. const T
20b79 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 oken *pToken
20b7a 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b /* Argument tok
20b7b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 en */.){. Expr
20b7c 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 *p = sqlite3Expr
20b7d 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e 64 62 Alloc(pParse->db
20b7e 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 , op, pToken, 1)
20b7f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 ;. sqlite3ExprA
20b80 74 74 61 63 68 53 75 62 74 72 65 65 73 28 70 50 ttachSubtrees(pP
20b81 61 72 73 65 2d 3e 64 62 2c 20 70 2c 20 70 4c 65 arse->db, p, pLe
20b82 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 72 ft, pRight);. r
20b83 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
20b84 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 65 * Join two expre
20b85 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20 ssions using an
20b86 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 49 AND operator. I
20b87 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73 73 f either express
20b88 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 ion is.** NULL,
20b89 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e then just return
20b8a 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65 the other expre
20b8b 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ssion..*/.SQLITE
20b8c 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
20b8d 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 qlite3ExprAnd(sq
20b8e 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 lite3 *db, Expr
20b8f 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 52 *pLeft, Expr *pR
20b90 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c 65 ight){. if( pLe
20b91 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ft==0 ){. ret
20b92 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d 65 urn pRight;. }e
20b93 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d 3d lse if( pRight==
20b94 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
20b95 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a pLeft;. }else{.
20b96 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d Expr *pNew =
20b97 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f sqlite3ExprAllo
20b98 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 30 2c c(db, TK_AND, 0,
20b99 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
20b9a 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 ExprAttachSubtre
20b9b 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70 4c 65 es(db, pNew, pLe
20b9c 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 ft, pRight);.
20b9d 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 20 20 return pNew;.
20b9e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 }.}../*.** Const
20b9f 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 ruct a new expre
20ba0 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 ssion node for a
20ba1 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d function with m
20ba2 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d ultiple.** argum
20ba3 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
20ba4 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
20ba5 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f lite3ExprFunctio
20ba6 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c n(Parse *pParse,
20ba7 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 ExprList *pList
20ba8 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 , Token *pToken)
20ba9 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a {. Expr *pNew;.
20baa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20bab 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 pParse->db;. as
20bac 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a sert( pToken );.
20bad 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
20bae 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b ExprAlloc(db, TK
20baf 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f 6b 65 _FUNCTION, pToke
20bb0 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 4e 65 n, 1);. if( pNe
20bb1 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 w==0 ){. sqli
20bb2 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 te3ExprListDelet
20bb3 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20 2f 2a e(db, pList); /*
20bb4 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20 6c 65 Avoid memory le
20bb5 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 ak when malloc f
20bb6 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 ails */. retu
20bb7 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 rn 0;. }. pNew
20bb8 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 ->x.pList = pLis
20bb9 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 t;. assert( !Ex
20bba 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e prHasProperty(pN
20bbb 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 ew, EP_xIsSelect
20bbc 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 ) );. sqlite3Ex
20bbd 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 prSetHeight(pPar
20bbe 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 74 se, pNew);. ret
20bbf 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
20bc0 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72 69 ** Assign a vari
20bc1 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 able number to a
20bc2 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 n expression tha
20bc3 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c 64 t encodes a wild
20bc4 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f card.** in the o
20bc5 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 riginal SQL stat
20bc6 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 ement. .**.** W
20bc7 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73 74 ildcards consist
20bc8 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 ing of a single
20bc9 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 "?" are assigned
20bca 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65 6e the next sequen
20bcb 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 tial.** variable
20bcc 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 number..**.** W
20bcd 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 ildcards of the
20bce 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20 form "?nnn" are
20bcf 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75 6d assigned the num
20bd0 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d ber "nnn". We m
20bd1 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e ake.** sure "nnn
20bd2 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20 " is not too be
20bd3 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69 61 to avoid a denia
20bd4 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74 74 l of service att
20bd5 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 ack when.** the
20bd6 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f SQL statement co
20bd7 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 65 mes from an exte
20bd8 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a rnal source..**.
20bd9 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 ** Wildcards of
20bda 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 2c the form ":aaa",
20bdb 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24 61 61 "@aaa", or "$aa
20bdc 61 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 a" are assigned
20bdd 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a the same number.
20bde 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76 69 6f ** as the previo
20bdf 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 us instance of t
20be0 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72 64 he same wildcard
20be1 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20 69 73 . Or if this is
20be2 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e the first.** in
20be3 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77 69 stance of the wi
20be4 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78 74 ldcard, the next
20be5 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69 61 sequenial varia
20be6 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a ble number is.**
20be7 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 assigned..*/.SQ
20be8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20be9 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 d sqlite3ExprAss
20bea 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 ignVarNumber(Par
20beb 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
20bec 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71 6c 69 *pExpr){. sqli
20bed 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
20bee 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 ->db;. const ch
20bef 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20 70 45 ar *z;.. if( pE
20bf0 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b xpr==0 ) return;
20bf1 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 . assert( !Expr
20bf2 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
20bf3 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 Expr, EP_IntValu
20bf4 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f e|EP_Reduced|EP_
20bf5 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 TokenOnly) );.
20bf6 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f z = pExpr->u.zTo
20bf7 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 7a ken;. assert( z
20bf8 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
20bf9 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 z[0]!=0 );. if
20bfa 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 ( z[1]==0 ){.
20bfb 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 /* Wildcard of
20bfc 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 20 41 the form "?". A
20bfd 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 20 76 ssign the next v
20bfe 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a ariable number *
20bff 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 5b /. assert( z[
20c00 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20 20 70 0]=='?' );. p
20c01 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 Expr->iColumn =
20c02 28 79 6e 56 61 72 29 28 2b 2b 70 50 61 72 73 65 (ynVar)(++pParse
20c03 2d 3e 6e 56 61 72 29 3b 0a 20 20 7d 65 6c 73 65 ->nVar);. }else
20c04 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 29 if( z[0]=='?' )
20c05 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 {. /* Wildcar
20c06 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f d of the form "?
20c07 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 20 22 nnn". Convert "
20c08 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 65 67 nnn" to an integ
20c09 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73 er and. ** us
20c0a 65 20 69 74 20 61 73 20 74 68 65 20 76 61 72 69 e it as the vari
20c0b 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 able number */.
20c0c 20 20 20 69 6e 74 20 69 20 3d 20 61 74 6f 69 28 int i = atoi(
20c0d 28 63 68 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a 20 (char*)&z[1]);.
20c0e 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d pExpr->iColum
20c0f 6e 20 3d 20 28 79 6e 56 61 72 29 69 3b 0a 20 20 n = (ynVar)i;.
20c10 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30 testcase( i==0
20c11 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
20c12 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 ( i==1 );. te
20c13 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 stcase( i==db->a
20c14 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
20c15 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 IT_VARIABLE_NUMB
20c16 45 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 ER]-1 );. tes
20c17 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c tcase( i==db->aL
20c18 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
20c19 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
20c1a 52 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 3c R] );. if( i<
20c1b 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 1 || i>db->aLimi
20c1c 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 t[SQLITE_LIMIT_V
20c1d 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 ARIABLE_NUMBER]
20c1e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20c1f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
20c20 20 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 "variable numbe
20c21 72 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 r must be betwee
20c22 6e 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 n ?1 and ?%d",.
20c23 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 db->aLi
20c24 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
20c25 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
20c26 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ]);. }. if
20c27 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 ( i>pParse->nVar
20c28 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 ){. pParse
20c29 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 ->nVar = i;.
20c2a 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f }. }else{. /
20c2b 2a 20 57 69 6c 64 63 61 72 64 73 20 6c 69 6b 65 * Wildcards like
20c2c 20 22 3a 61 61 61 22 2c 20 22 24 61 61 61 22 20 ":aaa", "$aaa"
20c2d 6f 72 20 22 40 61 61 61 22 2e 20 20 52 65 75 73 or "@aaa". Reus
20c2e 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 e the same varia
20c2f 62 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ble. ** numbe
20c30 72 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 61 r as the prior a
20c31 70 70 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65 ppearance of the
20c32 20 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 same name, or i
20c33 66 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a f the name. *
20c34 2a 20 68 61 73 20 6e 65 76 65 72 20 61 70 70 65 * has never appe
20c35 61 72 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75 ared before, reu
20c36 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 se the same vari
20c37 61 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 able number.
20c38 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 */. int i;.
20c39 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d u32 n;. n =
20c3a 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
20c3b 28 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 (z);. for(i=0
20c3c 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 ; i<pParse->nVar
20c3d 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
20c3e 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 50 61 Expr *pE = pPa
20c3f 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 69 rse->apVarExpr[i
20c40 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ];. assert(
20c41 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 pE!=0 );.
20c42 69 66 28 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 75 if( memcmp(pE->u
20c43 2e 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 3d 3d .zToken, z, n)==
20c44 30 20 26 26 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 0 && pE->u.zToke
20c45 6e 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 n[n]==0 ){.
20c46 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d pExpr->iColum
20c47 6e 20 3d 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3b n = pE->iColumn;
20c48 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
20c49 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
20c4a 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d if( i>=pParse-
20c4b 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 >nVarExpr ){.
20c4c 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d pExpr->iColum
20c4d 6e 20 3d 20 28 79 6e 56 61 72 29 28 2b 2b 70 50 n = (ynVar)(++pP
20c4e 61 72 73 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 20 arse->nVar);.
20c4f 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e if( pParse->n
20c50 56 61 72 45 78 70 72 3e 3d 70 50 61 72 73 65 2d VarExpr>=pParse-
20c51 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 >nVarExprAlloc-1
20c52 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 ){. pPar
20c53 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f se->nVarExprAllo
20c54 63 20 2b 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 c += pParse->nVa
20c55 72 45 78 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b rExprAlloc + 10;
20c56 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
20c57 3e 61 70 56 61 72 45 78 70 72 20 3d 0a 20 20 20 >apVarExpr =.
20c58 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
20c59 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
20c5a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 . d
20c5b 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b,.
20c5c 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 pParse->apVarEx
20c5d 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 pr,.
20c5e 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 pParse->nVarEx
20c5f 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 prAlloc*sizeof(p
20c60 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
20c61 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20 [0]).
20c62 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 );. }.
20c63 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f if( !db->mallo
20c64 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
20c65 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 assert( pPars
20c66 65 2d 3e 61 70 56 61 72 45 78 70 72 21 3d 30 20 e->apVarExpr!=0
20c67 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 );. pPars
20c68 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 70 50 61 e->apVarExpr[pPa
20c69 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d rse->nVarExpr++]
20c6a 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 = pExpr;.
20c6b 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 }. }. } . i
20c6c 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 f( !pParse->nErr
20c6d 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 && pParse->nVar
20c6e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
20c6f 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c TE_LIMIT_VARIABL
20c70 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 E_NUMBER] ){.
20c71 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
20c72 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 (pParse, "too ma
20c73 6e 79 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 ny SQL variables
20c74 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ");. }.}../*.**
20c75 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c Recursively del
20c76 65 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f ete an expressio
20c77 6e 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 n tree..*/.SQLIT
20c78 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
20c79 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
20c7a 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 (sqlite3 *db, Ex
20c7b 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d pr *p){. if( p=
20c7c 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 =0 ) return;. i
20c7d 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 f( !ExprHasAnyPr
20c7e 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b operty(p, EP_Tok
20c7f 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 73 enOnly) ){. s
20c80 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
20c81 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a (db, p->pLeft);.
20c82 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
20c83 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 elete(db, p->pRi
20c84 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 21 45 ght);. if( !E
20c85 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20c86 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 26 26 , EP_Reduced) &&
20c87 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 (p->flags2 & EP
20c88 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 2_MallocedToken)
20c89 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c !=0 ){. sql
20c8a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20c8b 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 ->u.zToken);.
20c8c 20 7d 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 }. if( ExprH
20c8d 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 asProperty(p, EP
20c8e 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
20c8f 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
20c90 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e ctDelete(db, p->
20c91 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 x.pSelect);.
20c92 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
20c93 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
20c94 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 te(db, p->x.pLis
20c95 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 t);. }. }.
20c96 69 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 if( !ExprHasProp
20c97 65 72 74 79 28 70 2c 20 45 50 5f 53 74 61 74 69 erty(p, EP_Stati
20c98 63 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 c) ){. sqlite
20c99 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
20c9a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
20c9b 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
20c9c 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 f bytes allocate
20c9d 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 d for the expres
20c9e 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0a sion structure .
20c9f 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ** passed as the
20ca0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
20ca1 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 This is always
20ca2 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c one of EXPR_FULL
20ca3 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45 SIZE,.** EXPR_RE
20ca4 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50 DUCEDSIZE or EXP
20ca5 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e R_TOKENONLYSIZE.
20ca6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 .*/.static int e
20ca7 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 78 xprStructSize(Ex
20ca8 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 pr *p){. if( Ex
20ca9 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
20caa 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 EP_TokenOnly) )
20cab 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b return EXPR_TOK
20cac 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66 ENONLYSIZE;. if
20cad 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
20cae 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 y(p, EP_Reduced)
20caf 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 52 ) return EXPR_R
20cb0 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 EDUCEDSIZE;. re
20cb1 74 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 turn EXPR_FULLSI
20cb2 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ZE;.}../*.** The
20cb3 20 64 75 70 65 64 45 78 70 72 2a 53 69 7a 65 28 dupedExpr*Size(
20cb4 29 20 72 6f 75 74 69 6e 65 73 20 65 61 63 68 20 ) routines each
20cb5 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
20cb6 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 r of bytes requi
20cb7 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 red.** to store
20cb8 61 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78 70 a copy of an exp
20cb9 72 65 73 73 69 6f 6e 20 6f 72 20 65 78 70 72 65 ression or expre
20cba 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 54 68 65 ssion tree. The
20cbb 79 20 64 69 66 66 65 72 20 69 6e 0a 2a 2a 20 68 y differ in.** h
20cbc 6f 77 20 6d 75 63 68 20 6f 66 20 74 68 65 20 74 ow much of the t
20cbd 72 65 65 20 69 73 20 6d 65 61 73 75 72 65 64 2e ree is measured.
20cbe 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 .**.** duped
20cbf 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29 ExprStructSize()
20cc0 20 20 20 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c Size of onl
20cc1 79 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 y the Expr struc
20cc2 74 75 72 65 20 0a 2a 2a 20 20 20 20 20 64 75 70 ture .** dup
20cc3 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 29 edExprNodeSize()
20cc4 20 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20 45 Size of E
20cc5 78 70 72 20 2b 20 73 70 61 63 65 20 66 6f 72 20 xpr + space for
20cc6 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 64 75 70 token.** dup
20cc7 65 64 45 78 70 72 53 69 7a 65 28 29 20 20 20 20 edExprSize()
20cc8 20 20 20 20 20 20 20 45 78 70 72 20 2b 20 74 6f Expr + to
20cc9 6b 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63 6f ken + subtree co
20cca 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a 2a mponents.**.****
20ccb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ccc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ccd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20cce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20ccf 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
20cd0 65 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 e dupedExprStruc
20cd1 74 53 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e tSize() function
20cd2 20 72 65 74 75 72 6e 73 20 74 77 6f 20 76 61 6c returns two val
20cd3 75 65 73 20 4f 52 2d 65 64 20 74 6f 67 65 74 68 ues OR-ed togeth
20cd4 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20 74 68 65 er: .** (1) the
20cd5 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 space required
20cd6 66 6f 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68 for a copy of th
20cd7 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 e Expr structure
20cd8 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 32 only and .** (2
20cd9 29 20 74 68 65 20 45 50 5f 78 78 78 20 66 6c 61 ) the EP_xxx fla
20cda 67 73 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 gs that indicate
20cdb 20 77 68 61 74 20 74 68 65 20 73 74 72 75 63 74 what the struct
20cdc 75 72 65 20 73 69 7a 65 20 73 68 6f 75 6c 64 20 ure size should
20cdd 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 be..** The retur
20cde 6e 20 76 61 6c 75 65 73 20 69 73 20 61 6c 77 61 n values is alwa
20cdf 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a ys one of:.**.**
20ce0 20 20 20 20 20 20 45 58 50 52 5f 46 55 4c 4c 53 EXPR_FULLS
20ce1 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 IZE.** EXPR
20ce2 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 7c _REDUCEDSIZE |
20ce3 20 45 50 5f 52 65 64 75 63 65 64 0a 2a 2a 20 20 EP_Reduced.**
20ce4 20 20 20 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e EXPR_TOKENON
20ce5 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 LYSIZE | EP_Toke
20ce6 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nOnly.**.** The
20ce7 73 69 7a 65 20 6f 66 20 74 68 65 20 73 74 72 75 size of the stru
20ce8 63 74 75 72 65 20 63 61 6e 20 62 65 20 66 6f 75 cture can be fou
20ce9 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 nd by masking th
20cea 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
20ceb 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e * of this routin
20cec 65 20 77 69 74 68 20 30 78 66 66 66 2e 20 20 54 e with 0xfff. T
20ced 68 65 20 66 6c 61 67 73 20 63 61 6e 20 62 65 20 he flags can be
20cee 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 found by masking
20cef 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 the.** return v
20cf0 61 6c 75 65 20 77 69 74 68 20 45 50 5f 52 65 64 alue with EP_Red
20cf1 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c uced|EP_TokenOnl
20cf2 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 y..**.** Note th
20cf3 61 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d 45 at with flags==E
20cf4 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 74 XPRDUP_REDUCE, t
20cf5 68 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 his routines wor
20cf6 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0a ks on full-size.
20cf7 2a 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20 45 ** (unreduced) E
20cf8 78 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20 74 xpr objects as t
20cf9 68 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c 6c hey or originall
20cfa 79 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 y constructed by
20cfb 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 the parser..**
20cfc 44 75 72 69 6e 67 20 65 78 70 72 65 73 73 69 6f During expressio
20cfd 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65 78 74 72 n analysis, extr
20cfe 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 a information is
20cff 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 6d 6f computed and mo
20d00 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 65 ved into.** late
20d01 72 20 70 61 72 74 73 20 6f 66 20 74 65 68 20 45 r parts of teh E
20d02 78 70 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 xpr object and t
20d03 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d hat extra inform
20d04 61 74 69 6f 6e 20 6d 69 67 68 74 20 67 65 74 20 ation might get
20d05 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 69 chopped.** off i
20d06 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
20d07 20 69 73 20 72 65 64 75 63 65 64 2e 20 20 4e 6f is reduced. No
20d08 74 65 20 61 6c 73 6f 20 74 68 61 74 20 69 74 20 te also that it
20d09 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 74 6f does not work to
20d0a 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58 50 52 44 .** make a EXPRD
20d0b 55 50 5f 52 45 44 55 43 45 20 63 6f 70 79 20 6f UP_REDUCE copy o
20d0c 66 20 61 20 72 65 64 75 63 65 64 20 65 78 70 72 f a reduced expr
20d0d 65 73 73 69 6f 6e 2e 20 20 49 74 20 69 73 20 6f ession. It is o
20d0e 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f 20 nly legal.** to
20d0f 72 65 64 75 63 65 20 61 20 70 72 69 73 74 69 6e reduce a pristin
20d10 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 e expression tre
20d11 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65 e from the parse
20d12 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e r. The implemen
20d13 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 70 tation.** of dup
20d14 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 edExprStructSize
20d15 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 69 () contain multi
20d16 70 6c 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ple assert() sta
20d17 74 65 6d 65 6e 74 73 20 74 68 61 74 20 61 74 74 tements that att
20d18 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f 72 empt.** to enfor
20d19 63 65 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 ce this constrai
20d1a 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nt..*/.static in
20d1b 74 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 t dupedExprStruc
20d1c 74 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 tSize(Expr *p, i
20d1d 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 nt flags){. int
20d1e 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 nSize;. assert
20d1f 28 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 ( flags==EXPRDUP
20d20 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c 61 67 73 _REDUCE || flags
20d21 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f ==0 ); /* Only o
20d22 6e 65 20 66 6c 61 67 20 76 61 6c 75 65 20 61 6c ne flag value al
20d23 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66 28 20 30 lowed */. if( 0
20d24 3d 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 ==(flags&EXPRDUP
20d25 5f 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 20 _REDUCE) ){.
20d26 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c nSize = EXPR_FUL
20d27 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a LSIZE;. }else{.
20d28 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
20d29 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
20d2a 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c p, EP_TokenOnly|
20d2b 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 EP_Reduced) );.
20d2c 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
20d2d 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
20d2e 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 0a P_FromJoin) ); .
20d2f 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
20d30 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c flags2 & EP2_Mal
20d31 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 locedToken)==0 )
20d32 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 ;. assert( (p
20d33 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 49 ->flags2 & EP2_I
20d34 72 72 65 64 75 63 69 62 6c 65 29 3d 3d 30 20 29 rreducible)==0 )
20d35 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 ;. if( p->pLe
20d36 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 20 ft || p->pRight
20d37 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 || p->pColl || p
20d38 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 ->x.pList ){.
20d39 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f nSize = EXPR_
20d3a 52 45 44 55 43 45 44 53 49 5a 45 20 7c 20 45 50 REDUCEDSIZE | EP
20d3b 5f 52 65 64 75 63 65 64 3b 0a 20 20 20 20 7d 65 _Reduced;. }e
20d3c 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 lse{. nSize
20d3d 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c = EXPR_TOKENONL
20d3e 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e YSIZE | EP_Token
20d3f 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Only;. }. }.
20d40 20 20 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a return nSize;.
20d41 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
20d42 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
20d43 68 65 20 73 70 61 63 65 20 69 6e 20 62 79 74 65 he space in byte
20d44 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 s required to st
20d45 6f 72 65 20 74 68 65 20 63 6f 70 79 20 0a 2a 2a ore the copy .**
20d46 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 of the Expr str
20d47 75 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f 70 ucture and a cop
20d48 79 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75 2e y of the Expr.u.
20d49 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 zToken string (i
20d4a 66 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67 f that.** string
20d4b 20 69 73 20 64 65 66 69 6e 65 64 2e 29 0a 2a 2f is defined.).*/
20d4c 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 .static int dupe
20d4d 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 78 dExprNodeSize(Ex
20d4e 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 pr *p, int flags
20d4f 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d ){. int nByte =
20d50 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 dupedExprStruct
20d51 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 26 Size(p, flags) &
20d52 20 30 78 66 66 66 3b 0a 20 20 69 66 28 20 21 45 0xfff;. if( !E
20d53 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20d54 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 , EP_IntValue) &
20d55 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b & p->u.zToken ){
20d56 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 . nByte += sq
20d57 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d lite3Strlen30(p-
20d58 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 20 >u.zToken)+1;.
20d59 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 }. return ROUND
20d5a 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 8(nByte);.}../*.
20d5b 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
20d5c 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 mber of bytes re
20d5d 71 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 65 quired to create
20d5e 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20 a duplicate of
20d5f 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 the .** expressi
20d60 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 on passed as the
20d61 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
20d62 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
20d63 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73 ment is a.** mas
20d64 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 k containing EXP
20d65 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a RDUP_XXX flags..
20d66 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 **.** The value
20d67 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 returned include
20d68 73 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74 s space to creat
20d69 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 e a copy of the
20d6a 45 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 69 Expr struct.** i
20d6b 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75 tself and the bu
20d6c 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f ffer referred to
20d6d 20 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 by Expr.u.zToke
20d6e 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a n, if any..**.**
20d6f 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f If the EXPRDUP_
20d70 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 REDUCE flag is s
20d71 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 et, then the ret
20d72 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 urn value includ
20d73 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 es .** space to
20d74 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 78 duplicate all Ex
20d75 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 20 pr nodes in the
20d76 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 45 tree formed by E
20d77 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 6e xpr.pLeft .** an
20d78 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 61 d Expr.pRight va
20d79 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f 74 riables (but not
20d7a 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 75 for any structu
20d7b 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 6f res pointed to o
20d7c 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 20 r .** descended
20d7d 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 2e from the Expr.x.
20d7e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e pList or Expr.x.
20d7f 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c 65 pSelect variable
20d80 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e s)..*/.static in
20d81 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 t dupedExprSize(
20d82 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 Expr *p, int fla
20d83 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 gs){. int nByte
20d84 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b = 0;. if( p ){
20d85 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 70 . nByte = dup
20d86 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 edExprNodeSize(p
20d87 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 , flags);. if
20d88 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f ( flags&EXPRDUP_
20d89 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 20 REDUCE ){.
20d8a 6e 42 79 74 65 20 2b 3d 20 64 75 70 65 64 45 78 nByte += dupedEx
20d8b 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c prSize(p->pLeft,
20d8c 20 66 6c 61 67 73 29 20 2b 20 64 75 70 65 64 45 flags) + dupedE
20d8d 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 67 68 xprSize(p->pRigh
20d8e 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d t, flags);. }
20d8f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 . }. return nB
20d90 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 yte;.}../*.** Th
20d91 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 is function is s
20d92 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 imilar to sqlite
20d93 33 45 78 70 72 44 75 70 28 29 2c 20 65 78 63 65 3ExprDup(), exce
20d94 70 74 20 74 68 61 74 20 69 66 20 70 7a 42 75 66 pt that if pzBuf
20d95 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e fer .** is not N
20d96 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 ULL then *pzBuff
20d97 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f er is assumed to
20d98 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 point to a buff
20d99 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 er large enough
20d9a 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 .** to store the
20d9b 20 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 copy of express
20d9c 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 65 ion p, the copie
20d9d 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e s of p->u.zToken
20d9e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62 .** (if applicab
20d9f 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 le), and the cop
20da0 69 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c ies of the p->pL
20da1 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 eft and p->pRigh
20da2 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a t expressions,.*
20da3 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 * if any. Before
20da4 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 returning, *pzB
20da5 75 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20 uffer is set to
20da6 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 the first byte p
20da7 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 assed the.** por
20da8 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66 tion of the buff
20da9 65 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 er copied into b
20daa 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e y this function.
20dab 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 .*/.static Expr
20dac 2a 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 *exprDup(sqlite3
20dad 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 *db, Expr *p, i
20dae 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 nt flags, u8 **p
20daf 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72 zBuffer){. Expr
20db0 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 *pNew = 0;
20db1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20db2 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 /* Value to ret
20db3 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 urn */. if( p )
20db4 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 {. const int
20db5 69 73 52 65 64 75 63 65 64 20 3d 20 28 66 6c 61 isReduced = (fla
20db6 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 gs&EXPRDUP_REDUC
20db7 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c E);. u8 *zAll
20db8 6f 63 3b 0a 20 20 20 20 75 33 32 20 73 74 61 74 oc;. u32 stat
20db9 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 20 icFlag = 0;..
20dba 20 61 73 73 65 72 74 28 20 70 7a 42 75 66 66 65 assert( pzBuffe
20dbb 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63 65 r==0 || isReduce
20dbc 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 d );.. /* Fig
20dbd 75 72 65 20 6f 75 74 20 77 68 65 72 65 20 74 6f ure out where to
20dbe 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 45 write the new E
20dbf 78 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 2a xpr structure. *
20dc0 2f 0a 20 20 20 20 69 66 28 20 70 7a 42 75 66 66 /. if( pzBuff
20dc1 65 72 20 29 7b 0a 20 20 20 20 20 20 7a 41 6c 6c er ){. zAll
20dc2 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65 72 3b 0a oc = *pzBuffer;.
20dc3 20 20 20 20 20 20 73 74 61 74 69 63 46 6c 61 67 staticFlag
20dc4 20 3d 20 45 50 5f 53 74 61 74 69 63 3b 0a 20 20 = EP_Static;.
20dc5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a }else{. z
20dc6 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 Alloc = sqlite3D
20dc7 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 bMallocRaw(db, d
20dc8 75 70 65 64 45 78 70 72 53 69 7a 65 28 70 2c 20 upedExprSize(p,
20dc9 66 6c 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 flags));. }.
20dca 20 20 20 70 4e 65 77 20 3d 20 28 45 78 70 72 20 pNew = (Expr
20dcb 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 *)zAlloc;.. i
20dcc 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 f( pNew ){.
20dcd 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 /* Set nNewSize
20dce 20 74 6f 20 74 68 65 20 73 69 7a 65 20 61 6c 6c to the size all
20dcf 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 ocated for the s
20dd0 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 tructure pointed
20dd1 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 to. ** by
20dd2 70 4e 65 77 2e 20 54 68 69 73 20 69 73 20 65 69 pNew. This is ei
20dd3 74 68 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 ther EXPR_FULLSI
20dd4 5a 45 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44 ZE, EXPR_REDUCED
20dd5 53 49 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a SIZE or. **
20dd6 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 EXPR_TOKENONLYS
20dd7 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 IZE. nToken is s
20dd8 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 et to the number
20dd9 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d of bytes consum
20dda 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 ed. ** by t
20ddb 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 he copy of the p
20ddc 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e ->u.zToken strin
20ddd 67 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 g (if any)..
20dde 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 */. const
20ddf 20 75 6e 73 69 67 6e 65 64 20 6e 53 74 72 75 63 unsigned nStruc
20de0 74 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 70 tSize = dupedExp
20de1 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 66 rStructSize(p, f
20de2 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e lags);. con
20de3 73 74 20 69 6e 74 20 6e 4e 65 77 53 69 7a 65 20 st int nNewSize
20de4 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 20 26 20 = nStructSize &
20de5 30 78 66 66 66 3b 0a 20 20 20 20 20 20 69 6e 74 0xfff;. int
20de6 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 nToken;. i
20de7 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 f( !ExprHasPrope
20de8 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c rty(p, EP_IntVal
20de9 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b ue) && p->u.zTok
20dea 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 54 en ){. nT
20deb 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 oken = sqlite3St
20dec 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b rlen30(p->u.zTok
20ded 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7d en) + 1;. }
20dee 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 54 else{. nT
20def 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 oken = 0;.
20df0 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 65 }. if( isRe
20df1 64 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20 duced ){.
20df2 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 assert( ExprHas
20df3 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 Property(p, EP_R
20df4 65 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 educed)==0 );.
20df5 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c memcpy(zAl
20df6 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 loc, p, nNewSize
20df7 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
20df8 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a int nSiz
20df9 65 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 e = exprStructSi
20dfa 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d ze(p);. m
20dfb 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c emcpy(zAlloc, p,
20dfc 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 nSize);.
20dfd 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b memset(&zAlloc[
20dfe 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f nSize], 0, EXPR_
20dff 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b FULLSIZE-nSize);
20e00 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
20e01 2f 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 65 /* Set the EP_Re
20e02 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b 65 6e 4f duced, EP_TokenO
20e03 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 74 61 74 nly, and EP_Stat
20e04 69 63 20 66 6c 61 67 73 20 61 70 70 72 6f 70 72 ic flags appropr
20e05 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 iately. */.
20e06 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 3d 20 pNew->flags &=
20e07 7e 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f ~(EP_Reduced|EP_
20e08 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 61 TokenOnly|EP_Sta
20e09 74 69 63 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 tic);. pNew
20e0a 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 74 72 75 ->flags |= nStru
20e0b 63 74 53 69 7a 65 20 26 20 28 45 50 5f 52 65 64 ctSize & (EP_Red
20e0c 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c uced|EP_TokenOnl
20e0d 79 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e y);. pNew->
20e0e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74 69 63 46 flags |= staticF
20e0f 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 lag;.. /* C
20e10 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f opy the p->u.zTo
20e11 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 20 61 ken string, if a
20e12 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ny. */. if(
20e13 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 nToken ){.
20e14 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 char *zToken
20e15 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e = pNew->u.zToken
20e16 20 3d 20 28 63 68 61 72 2a 29 26 7a 41 6c 6c 6f = (char*)&zAllo
20e17 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 20 c[nNewSize];.
20e18 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 54 6f 6b memcpy(zTok
20e19 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c en, p->u.zToken,
20e1a 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 nToken);.
20e1b 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d }.. if( 0==
20e1c 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d ((p->flags|pNew-
20e1d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54 6f 6b >flags) & EP_Tok
20e1e 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 enOnly) ){.
20e1f 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 /* Fill in th
20e20 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 e pNew->x.pSelec
20e21 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 t or pNew->x.pLi
20e22 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 st member. */.
20e23 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 if( ExprHa
20e24 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f sProperty(p, EP_
20e25 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
20e26 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e pNew->x.
20e27 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 pSelect = sqlite
20e28 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 3SelectDup(db, p
20e29 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 ->x.pSelect, isR
20e2a 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 educed);.
20e2b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
20e2c 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 pNew->x.pList
20e2d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 = sqlite3ExprLis
20e2e 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c tDup(db, p->x.pL
20e2f 69 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b ist, isReduced);
20e30 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20e31 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c }.. /* Fil
20e32 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 l in pNew->pLeft
20e33 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 and pNew->pRigh
20e34 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 t. */. if(
20e35 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 ExprHasAnyProper
20e36 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 ty(pNew, EP_Redu
20e37 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ced|EP_TokenOnly
20e38 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 6c ) ){. zAl
20e39 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45 78 70 72 loc += dupedExpr
20e3a 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 NodeSize(p, flag
20e3b 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 s);. if(
20e3c 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20e3d 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64 pNew, EP_Reduced
20e3e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
20e3f 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 65 78 70 New->pLeft = exp
20e40 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 rDup(db, p->pLef
20e41 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 t, EXPRDUP_REDUC
20e42 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 E, &zAlloc);.
20e43 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 pNew->pRi
20e44 67 68 74 20 3d 20 65 78 70 72 44 75 70 28 64 62 ght = exprDup(db
20e45 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 45 58 50 , p->pRight, EXP
20e46 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 RDUP_REDUCE, &zA
20e47 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 7d lloc);. }
20e48 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 7a 42 . if( pzB
20e49 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 20 uffer ){.
20e4a 20 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a *pzBuffer = z
20e4b 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 7d Alloc;. }
20e4c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
20e4d 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 pNew->flag
20e4e 73 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 s2 = 0;.
20e4f 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 if( !ExprHasAnyP
20e50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f roperty(p, EP_To
20e51 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 kenOnly) ){.
20e52 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 pNew->pLef
20e53 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 t = sqlite3ExprD
20e54 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c up(db, p->pLeft,
20e55 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 0);. p
20e56 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 New->pRight = sq
20e57 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20e58 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a p->pRight, 0);.
20e59 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
20e5a 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }.. }. }. r
20e5b 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
20e5c 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
20e5d 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 ng group of rout
20e5e 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 ines make deep c
20e5f 6f 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 opies of express
20e60 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 ions,.** express
20e61 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 ion lists, ID li
20e62 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 sts, and select
20e63 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 statements. The
20e64 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 copies can.** b
20e65 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65 e deleted (by be
20e66 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 ing passed to th
20e67 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e eir respective .
20e68 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69 ..Delete() routi
20e69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 nes).** without
20e6a 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72 effecting the or
20e6b 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 iginals..**.** T
20e6c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 he expression li
20e6d 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 st, ID, and sour
20e6e 63 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20 ce lists return
20e6f 62 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 by sqlite3ExprLi
20e70 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 stDup(),.** sqli
20e71 74 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20 te3IdListDup(),
20e72 61 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 and sqlite3SrcLi
20e73 73 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 stDup() can not
20e74 62 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e be further expan
20e75 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 ded .** by subse
20e76 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 quent calls to s
20e77 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 qlite*ListAppend
20e78 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a () routines..**.
20e79 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68 ** Any tables th
20e7a 61 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d at the SrcList m
20e7b 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 ight point to ar
20e7c 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 e not duplicated
20e7d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 ..**.** The flag
20e7e 73 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 s parameter cont
20e7f 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 ains a combinati
20e80 6f 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 55 on of the EXPRDU
20e81 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 20 P_XXX flags..**
20e82 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 52 If the EXPRDUP_R
20e83 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 65 EDUCE flag is se
20e84 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 75 t, then the stru
20e85 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 69 cture returned i
20e86 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 s a.** truncated
20e87 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
20e88 75 73 75 61 6c 20 45 78 70 72 20 73 74 72 75 63 usual Expr struc
20e89 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 ture that will b
20e8a 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 e stored as.** p
20e8b 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 art of the in-me
20e8c 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 mory representat
20e8d 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62 ion of the datab
20e8e 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 ase schema..*/.S
20e8f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
20e90 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 pr *sqlite3ExprD
20e91 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
20e92 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 Expr *p, int fla
20e93 67 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 gs){. return ex
20e94 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 prDup(db, p, fla
20e95 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 gs, 0);.}.SQLITE
20e96 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
20e97 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
20e98 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 stDup(sqlite3 *d
20e99 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 b, ExprList *p,
20e9a 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 78 int flags){. Ex
20e9b 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 prList *pNew;.
20e9c 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
20e9d 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f item *pItem, *pO
20e9e 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b ldItem;. int i;
20e9f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
20ea0 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d turn 0;. pNew =
20ea1 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20ea2 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a Raw(db, sizeof(*
20ea3 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70 pNew) );. if( p
20ea4 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 New==0 ) return
20ea5 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 72 0;. pNew->iECur
20ea6 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d sor = 0;. pNew-
20ea7 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e >nExpr = pNew->n
20ea8 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72 Alloc = p->nExpr
20ea9 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49 ;. pNew->a = pI
20eaa 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d tem = sqlite3DbM
20eab 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 2d allocRaw(db, p-
20eac 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d >nExpr*sizeof(p-
20ead 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 >a[0]) );. if(
20eae 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 pItem==0 ){.
20eaf 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20eb0 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 , pNew);. ret
20eb1 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f urn 0;. } . pO
20eb2 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 ldItem = p->a;.
20eb3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
20eb4 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d Expr; i++, pItem
20eb5 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b ++, pOldItem++){
20eb6 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 45 . Expr *pOldE
20eb7 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e xpr = pOldItem->
20eb8 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d pExpr;. pItem
20eb9 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 ->pExpr = sqlite
20eba 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 3ExprDup(db, pOl
20ebb 64 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 dExpr, flags);.
20ebc 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 pItem->zName
20ebd 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
20ebe 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20ebf 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 zName);. pIte
20ec0 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 m->zSpan = sqlit
20ec1 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
20ec2 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b OldItem->zSpan);
20ec3 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 . pItem->sort
20ec4 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d Order = pOldItem
20ec5 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 ->sortOrder;.
20ec6 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 pItem->done = 0
20ec7 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f ;. pItem->iCo
20ec8 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 l = pOldItem->iC
20ec9 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 ol;. pItem->i
20eca 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d Alias = pOldItem
20ecb 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 ->iAlias;. }.
20ecc 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
20ecd 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 /*.** If cursors
20ece 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 , triggers, view
20ecf 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 s and subqueries
20ed0 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 are all omitted
20ed1 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 from.** the bui
20ed2 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 ld, then none of
20ed3 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 the following r
20ed4 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20 outines, except
20ed5 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 for .** sqlite3S
20ed6 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20 electDup(), can
20ed7 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 be called. sqlit
20ed8 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73 e3SelectDup() is
20ed9 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 sometimes.** ca
20eda 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c lled with a NULL
20edb 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 argument..*/.#i
20edc 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
20edd 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 E_OMIT_VIEW) ||
20ede 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
20edf 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a OMIT_TRIGGER) \.
20ee0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
20ee1 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
20ee2 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 Y).SQLITE_PRIVAT
20ee3 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 E SrcList *sqlit
20ee4 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c e3SrcListDup(sql
20ee5 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 ite3 *db, SrcLis
20ee6 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 t *p, int flags)
20ee7 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 {. SrcList *pNe
20ee8 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e w;. int i;. in
20ee9 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 70 t nByte;. if( p
20eea 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
20eeb 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 nByte = sizeof
20eec 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e (*p) + (p->nSrc>
20eed 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 0 ? sizeof(p->a[
20eee 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31 0]) * (p->nSrc-1
20eef 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d ) : 0);. pNew =
20ef0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
20ef1 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29 3b Raw(db, nByte );
20ef2 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 . if( pNew==0 )
20ef3 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 return 0;. pNe
20ef4 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e w->nSrc = pNew->
20ef5 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 nAlloc = p->nSrc
20ef6 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
20ef7 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 ->nSrc; i++){.
20ef8 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
20ef9 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 _item *pNewItem
20efa 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 = &pNew->a[i];.
20efb 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 struct SrcLis
20efc 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d t_item *pOldItem
20efd 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 = &p->a[i];.
20efe 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 Table *pTab;.
20eff 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74 pNewItem->zDat
20f00 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 abase = sqlite3D
20f01 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
20f02 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 Item->zDatabase)
20f03 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
20f04 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 zName = sqlite3D
20f05 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 bStrDup(db, pOld
20f06 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 Item->zName);.
20f07 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69 pNewItem->zAli
20f08 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 as = sqlite3DbSt
20f09 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 rDup(db, pOldIte
20f0a 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 m->zAlias);.
20f0b 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 pNewItem->jointy
20f0c 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a pe = pOldItem->j
20f0d 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 ointype;. pNe
20f0e 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d wItem->iCursor =
20f0f 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 pOldItem->iCurs
20f10 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d or;. pNewItem
20f11 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 ->isPopulated =
20f12 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 pOldItem->isPopu
20f13 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 lated;. pNewI
20f14 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 tem->zIndex = sq
20f15 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
20f16 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64 , pOldItem->zInd
20f17 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 ex);. pNewIte
20f18 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 m->notIndexed =
20f19 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 pOldItem->notInd
20f1a 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 exed;. pNewIt
20f1b 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c em->pIndex = pOl
20f1c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a 20 dItem->pIndex;.
20f1d 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 74 pTab = pNewIt
20f1e 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49 em->pTab = pOldI
20f1f 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 tem->pTab;. i
20f20 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 f( pTab ){.
20f21 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 pTab->nRef++;.
20f22 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74 65 }. pNewIte
20f23 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c m->pSelect = sql
20f24 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 ite3SelectDup(db
20f25 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c , pOldItem->pSel
20f26 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 ect, flags);.
20f27 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d pNewItem->pOn =
20f28 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 sqlite3ExprDup(
20f29 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f db, pOldItem->pO
20f2a 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 n, flags);. p
20f2b 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 NewItem->pUsing
20f2c 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 = sqlite3IdListD
20f2d 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d up(db, pOldItem-
20f2e 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70 4e >pUsing);. pN
20f2f 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 ewItem->colUsed
20f30 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c 55 = pOldItem->colU
20f31 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 sed;. }. retur
20f32 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 n pNew;.}.SQLITE
20f33 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 _PRIVATE IdList
20f34 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 *sqlite3IdListDu
20f35 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 p(sqlite3 *db, I
20f36 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c dList *p){. IdL
20f37 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 ist *pNew;. int
20f38 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 i;. if( p==0 )
20f39 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 return 0;. pNe
20f3a 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
20f3b 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f locRaw(db, sizeo
20f3c 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 f(*pNew) );. if
20f3d 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 ( pNew==0 ) retu
20f3e 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 49 rn 0;. pNew->nI
20f3f 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 d = pNew->nAlloc
20f40 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e 65 = p->nId;. pNe
20f41 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62 w->a = sqlite3Db
20f42 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 2d MallocRaw(db, p-
20f43 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 >nId*sizeof(p->a
20f44 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 4e [0]) );. if( pN
20f45 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 ew->a==0 ){.
20f46 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20f47 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 , pNew);. ret
20f48 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 urn 0;. }. for
20f49 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20 (i=0; i<p->nId;
20f4a 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 i++){. struct
20f4b 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e IdList_item *pN
20f4c 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e ewItem = &pNew->
20f4d 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 a[i];. struct
20f4e 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f IdList_item *pO
20f4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 ldItem = &p->a[i
20f50 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d ];. pNewItem-
20f51 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 >zName = sqlite3
20f52 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c DbStrDup(db, pOl
20f53 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 dItem->zName);.
20f54 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 64 78 pNewItem->idx
20f55 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 78 = pOldItem->idx
20f56 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
20f57 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 New;.}.SQLITE_PR
20f58 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 IVATE Select *sq
20f59 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 lite3SelectDup(s
20f5a 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 qlite3 *db, Sele
20f5b 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 ct *p, int flags
20f5c 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 ){. Select *pNe
20f5d 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 w;. if( p==0 )
20f5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 return 0;. pNew
20f5f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
20f60 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 ocRaw(db, sizeof
20f61 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e (*p) );. if( pN
20f62 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 ew==0 ) return 0
20f63 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 ;. pNew->pEList
20f64 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 = sqlite3ExprLi
20f65 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c stDup(db, p->pEL
20f66 69 73 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 ist, flags);. p
20f67 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 New->pSrc = sqli
20f68 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 te3SrcListDup(db
20f69 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 , p->pSrc, flags
20f6a 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 );. pNew->pWher
20f6b 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 e = sqlite3ExprD
20f6c 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 up(db, p->pWhere
20f6d 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 , flags);. pNew
20f6e 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c ->pGroupBy = sql
20f6f 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 ite3ExprListDup(
20f70 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c db, p->pGroupBy,
20f71 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d flags);. pNew-
20f72 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 >pHaving = sqlit
20f73 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d e3ExprDup(db, p-
20f74 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 >pHaving, flags)
20f75 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 ;. pNew->pOrder
20f76 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 By = sqlite3Expr
20f77 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 ListDup(db, p->p
20f78 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b OrderBy, flags);
20f79 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d . pNew->op = p-
20f7a 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 >op;. pNew->pPr
20f7b 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c ior = sqlite3Sel
20f7c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 ectDup(db, p->pP
20f7d 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 rior, flags);.
20f7e 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 pNew->pLimit = s
20f7f 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
20f80 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 , p->pLimit, fla
20f81 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 gs);. pNew->pOf
20f82 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78 fset = sqlite3Ex
20f83 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 prDup(db, p->pOf
20f84 66 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 fset, flags);.
20f85 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 pNew->iLimit = 0
20f86 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 ;. pNew->iOffse
20f87 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 t = 0;. pNew->s
20f88 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c elFlags = p->sel
20f89 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73 Flags & ~SF_Uses
20f8a 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 Ephemeral;. pNe
20f8b 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 w->pRightmost =
20f8c 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 0;. pNew->addrO
20f8d 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b penEphm[0] = -1;
20f8e 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 . pNew->addrOpe
20f8f 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 nEphm[1] = -1;.
20f90 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 pNew->addrOpenE
20f91 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 phm[2] = -1;. r
20f92 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 eturn pNew;.}.#e
20f93 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 lse.SQLITE_PRIVA
20f94 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 TE Select *sqlit
20f95 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 e3SelectDup(sqli
20f96 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 te3 *db, Select
20f97 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a *p, int flags){.
20f98 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 assert( p==0 )
20f99 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
20f9a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 #endif.../*.** A
20f9b 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 dd a new element
20f9c 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 to the end of a
20f9d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 n expression lis
20f9e 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a t. If pList is.
20f9f 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c ** initially NUL
20fa0 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 L, then create a
20fa1 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 new expression
20fa2 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 list..**.** If a
20fa3 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
20fa4 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c on error occurs,
20fa5 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 the entire list
20fa6 20 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a is freed and.**
20fa7 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
20fa8 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20 d. If non-NULL
20fa9 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 is returned, the
20faa 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 n it is guarante
20fab 65 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e ed.** that the n
20fac 65 77 20 65 6e 74 72 79 20 77 61 73 20 73 75 63 ew entry was suc
20fad 63 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e 64 cessfully append
20fae 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
20faf 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a IVATE ExprList *
20fb0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 sqlite3ExprListA
20fb1 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 2a ppend(. Parse *
20fb2 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
20fb3 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20fb4 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ext */. ExprLis
20fb5 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 t *pList,
20fb6 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 /* List to whic
20fb7 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67 h to append. Mig
20fb8 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 ht be NULL */.
20fb9 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 Expr *pExpr
20fba 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
20fbb 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 65 ssion to be appe
20fbc 6e 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20 4e nded. Might be N
20fbd 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 ULL */.){. sqli
20fbe 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
20fbf 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 ->db;. if( pLis
20fc0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 t==0 ){. pLis
20fc1 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
20fc2 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
20fc3 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a of(ExprList) );.
20fc4 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 if( pList==0
20fc5 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
20fc6 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
20fc7 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
20fc8 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d nAlloc==0 );. }
20fc9 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41 . if( pList->nA
20fca 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78 lloc<=pList->nEx
20fcb 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 pr ){. struct
20fcc 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
20fcd 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 a;. int n = p
20fce 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b List->nAlloc*2 +
20fcf 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 4;. a = sqli
20fd0 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c te3DbRealloc(db,
20fd1 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a pList->a, n*siz
20fd2 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 eof(pList->a[0])
20fd3 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 20 );. if( a==0
20fd4 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f ){. goto no
20fd5 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _mem;. }.
20fd6 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 pList->a = a;.
20fd7 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 pList->nAlloc
20fd8 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
20fd9 63 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69 7a cSize(db, a)/siz
20fda 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 eof(a[0]);. }.
20fdb 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
20fdc 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 31 20 a!=0 );. if( 1
20fdd 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 ){. struct Ex
20fde 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
20fdf 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 em = &pList->a[p
20fe0 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a List->nExpr++];.
20fe1 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d memset(pItem
20fe2 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 , 0, sizeof(*pIt
20fe3 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d em));. pItem-
20fe4 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a >pExpr = pExpr;.
20fe5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 }. return pLi
20fe6 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 st;..no_mem:
20fe7 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 . /* Avoid lea
20fe8 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d king memory if m
20fe9 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64 alloc has failed
20fea 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 . */. sqlite3Ex
20feb 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 prDelete(db, pEx
20fec 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 pr);. sqlite3Ex
20fed 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
20fee 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 pList);. retur
20fef 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 n 0;.}../*.** Se
20ff0 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 t the ExprList.a
20ff1 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e 74 [].zName element
20ff2 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
20ff3 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 6d ently added item
20ff4 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 65 .** on the expre
20ff5 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a ssion list..**.*
20ff6 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 65 * pList might be
20ff7 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 NULL following
20ff8 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 an OOM error. B
20ff9 75 74 20 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20 ut pName should
20ffa 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c never be.** NULL
20ffb 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 . If a memory a
20ffc 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
20ffd 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d the pParse->db-
20ffe 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c >mallocFailed fl
20fff 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f ag.** is set..*/
21000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21001 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
21002 4c 69 73 74 53 65 74 4e 61 6d 65 28 0a 20 20 50 ListSetName(. P
21003 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
21004 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
21005 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 g context */. E
21006 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 xprList *pList,
21007 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 /* List t
21008 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74 o which to add t
21009 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 6f he span. */. To
2100a 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20 ken *pName,
2100b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 74 6f /* Name to
2100c 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 be added */. i
2100d 6e 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 20 nt dequote
2100e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
2100f 6f 20 63 61 75 73 65 20 74 68 65 20 6e 61 6d 65 o cause the name
21010 20 74 6f 20 62 65 20 64 65 71 75 6f 74 65 64 20 to be dequoted
21011 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 */.){. assert(
21012 70 4c 69 73 74 21 3d 30 20 7c 7c 20 70 50 61 72 pList!=0 || pPar
21013 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
21014 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 iled!=0 );. if(
21015 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 pList ){. st
21016 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
21017 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 61 em *pItem;. a
21018 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 ssert( pList->nE
21019 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 70 49 74 xpr>0 );. pIt
2101a 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 em = &pList->a[p
2101b 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a List->nExpr-1];.
2101c 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 assert( pIte
2101d 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 m->zName==0 );.
2101e 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 pItem->zName
2101f 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 = sqlite3DbStrND
21020 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 up(pParse->db, p
21021 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e Name->z, pName->
21022 6e 29 3b 0a 20 20 20 20 69 66 28 20 64 65 71 75 n);. if( dequ
21023 6f 74 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e ote && pItem->zN
21024 61 6d 65 20 29 20 73 71 6c 69 74 65 33 44 65 71 ame ) sqlite3Deq
21025 75 6f 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d uote(pItem->zNam
21026 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a e);. }.}../*.**
21027 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 73 Set the ExprLis
21028 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 6d t.a[].zSpan elem
21029 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 ent of the most
2102a 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69 recently added i
2102b 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 tem.** on the ex
2102c 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a pression list..*
2102d 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 *.** pList might
2102e 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 be NULL followi
2102f 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e ng an OOM error.
21030 20 20 42 75 74 20 70 53 70 61 6e 20 73 68 6f 75 But pSpan shou
21031 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e ld never be.** N
21032 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 ULL. If a memor
21033 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
21034 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e ls, the pParse->
21035 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21036 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e flag.** is set.
21037 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
21038 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
21039 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 0a xprListSetSpan(.
2103a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
2103b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
2103c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
2103d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
2103e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 t, /* Lis
2103f 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64 t to which to ad
21040 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 d the span. */.
21041 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e ExprSpan *pSpan
21042 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
21043 73 70 61 6e 20 74 6f 20 62 65 20 61 64 64 65 64 span to be added
21044 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
21045 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 *db = pParse->d
21046 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 b;. assert( pLi
21047 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c st!=0 || db->mal
21048 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a locFailed!=0 );.
21049 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 if( pList ){.
2104a 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
2104b 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
2104c 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 &pList->a[pList
2104d 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 ->nExpr-1];.
2104e 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e assert( pList->n
2104f 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61 73 Expr>0 );. as
21050 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 sert( db->malloc
21051 46 61 69 6c 65 64 20 7c 7c 20 70 49 74 65 6d 2d Failed || pItem-
21052 3e 70 45 78 70 72 3d 3d 70 53 70 61 6e 2d 3e 70 >pExpr==pSpan->p
21053 45 78 70 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 Expr );. sqli
21054 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 te3DbFree(db, pI
21055 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 tem->zSpan);.
21056 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 pItem->zSpan =
21057 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 sqlite3DbStrNDup
21058 28 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70 61 (db, (char*)pSpa
21059 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 n->zStart,.
2105a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2105b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
2105c 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 int)(pSpan->zEnd
2105d 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 - pSpan->zStart
2105e 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ));. }.}../*.**
2105f 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
21060 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 on list pEList c
21061 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 ontains more tha
21062 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 n iLimit element
21063 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 s,.** leave an e
21064 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 rror message in
21065 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 pParse..*/.SQLIT
21066 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21067 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 qlite3ExprListCh
21068 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 eckLength(. Par
21069 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 se *pParse,. Ex
2106a 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a prList *pEList,.
2106b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f const char *zO
2106c 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d bject.){. int m
2106d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e x = pParse->db->
2106e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
2106f 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 MIT_COLUMN];. t
21070 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 estcase( pEList
21071 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 && pEList->nExpr
21072 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 ==mx );. testca
21073 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 se( pEList && pE
21074 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b List->nExpr==mx+
21075 31 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 1 );. if( pELis
21076 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 t && pEList->nEx
21077 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c pr>mx ){. sql
21078 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
21079 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 rse, "too many c
2107a 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a olumns in %s", z
2107b 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a Object);. }.}..
2107c 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 /*.** Delete an
2107d 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f entire expressio
2107e 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 n list..*/.SQLIT
2107f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21080 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 qlite3ExprListDe
21081 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 lete(sqlite3 *db
21082 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 , ExprList *pLis
21083 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 t){. int i;. s
21084 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
21085 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 tem *pItem;. if
21086 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 ( pList==0 ) ret
21087 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
21088 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 List->a!=0 || (p
21089 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 List->nExpr==0 &
2108a 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d & pList->nAlloc=
2108b 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 =0) );. assert(
2108c 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 pList->nExpr<=p
2108d 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a List->nAlloc );.
2108e 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 for(pItem=pLis
2108f 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 t->a, i=0; i<pLi
21090 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 st->nExpr; i++,
21091 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 pItem++){. sq
21092 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
21093 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 db, pItem->pExpr
21094 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 );. sqlite3Db
21095 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e Free(db, pItem->
21096 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 zName);. sqli
21097 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 te3DbFree(db, pI
21098 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 7d tem->zSpan);. }
21099 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
2109a 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a (db, pList->a);.
2109b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
2109c 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f db, pList);.}../
2109d 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
2109e 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 63 nes are Walker c
2109f 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65 allbacks. Walke
210a0 72 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 6e r.u.pi is a poin
210a1 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 ter.** to an int
210a2 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f 75 eger. These rou
210a3 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b 69 tines are checki
210a4 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e ng an expression
210a5 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69 74 to see.** if it
210a6 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e 20 is a constant.
210a7 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 Set *Walker.u.p
210a8 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 78 i to 0 if the ex
210a9 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e pression is.** n
210aa 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a ot constant..**.
210ab 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61 63 ** These callbac
210ac 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 k routines are u
210ad 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
210ae 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
210af 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 **.** sqlite
210b0 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 3ExprIsConstant(
210b1 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
210b2 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f ExprIsConstantNo
210b3 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20 73 tJoin().** s
210b4 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
210b5 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 29 tantOrFunction()
210b6 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e .**.*/.static in
210b7 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 t exprNodeIsCons
210b8 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 tant(Walker *pWa
210b9 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 lker, Expr *pExp
210ba 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57 61 r){.. /* If pWa
210bb 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20 74 lker->u.i is 3 t
210bc 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 hen any term of
210bd 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
210be 68 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 hat comes from.
210bf 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 ** the ON or US
210c0 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 ING clauses of a
210c1 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 69 join disqualifi
210c2 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f es the expressio
210c3 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e n. ** from bein
210c4 67 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e g considered con
210c5 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 stant. */. if(
210c6 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 pWalker->u.i==3
210c7 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f && ExprHasAnyPro
210c8 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
210c9 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 FromJoin) ){.
210ca 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 pWalker->u.i =
210cb 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 0;. return WR
210cc 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 C_Abort;. }..
210cd 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f switch( pExpr->o
210ce 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 p ){. /* Cons
210cf 69 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 ider functions t
210d0 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 66 o be constant if
210d1 20 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75 6d all their argum
210d2 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61 6e ents are constan
210d3 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57 61 t. ** and pWa
210d4 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a lker->u.i==2 */.
210d5 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 case TK_FUNC
210d6 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 20 TION:. if(
210d7 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 pWalker->u.i==2
210d8 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 ) return 0;.
210d9 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
210da 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b h */. case TK
210db 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _ID:. case TK
210dc 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 _COLUMN:. cas
210dd 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f e TK_AGG_FUNCTIO
210de 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 N:. case TK_A
210df 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 20 GG_COLUMN:.
210e0 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 testcase( pExpr
210e1 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 ->op==TK_ID );.
210e2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
210e3 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c Expr->op==TK_COL
210e4 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 UMN );. tes
210e5 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
210e6 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f ==TK_AGG_FUNCTIO
210e7 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 N );. testc
210e8 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
210e9 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b TK_AGG_COLUMN );
210ea 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e . pWalker->
210eb 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 u.i = 0;. r
210ec 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
210ed 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
210ee 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
210ef 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 xpr->op==TK_SELE
210f0 43 54 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e CT ); /* selectN
210f1 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 odeIsConstant wi
210f2 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 ll disallow */.
210f3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
210f4 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 Expr->op==TK_EXI
210f5 53 54 53 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 STS ); /* select
210f6 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 NodeIsConstant w
210f7 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a ill disallow */.
210f8 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
210f9 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d _Continue;. }.}
210fa 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 .static int sele
210fb 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 ctNodeIsConstant
210fc 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
210fd 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 , Select *NotUse
210fe 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
210ff 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
21100 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 . pWalker->u.i
21101 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 = 0;. return WR
21102 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 C_Abort;.}.stati
21103 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 c int exprIsCons
21104 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 t(Expr *p, int i
21105 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b nitFlag){. Walk
21106 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 er w;. w.u.i =
21107 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 initFlag;. w.xE
21108 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 xprCallback = ex
21109 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 prNodeIsConstant
2110a 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c ;. w.xSelectCal
2110b 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f lback = selectNo
2110c 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 deIsConstant;.
2110d 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
2110e 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e &w, p);. return
2110f 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a w.u.i;.}../*.**
21110 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 Walk an express
21111 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 ion tree. Retur
21112 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 n 1 if the expre
21113 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e ssion is constan
21114 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 t.** and 0 if it
21115 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 involves variab
21116 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 les or function
21117 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 calls..**.** For
21118 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
21119 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
2111a 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 a double-quoted
2111b 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 string (ex: "abc
2111c 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 ").** is conside
2111d 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 red a variable b
2111e 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 ut a single-quot
2111f 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 ed string (ex: '
21120 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f abc') is.** a co
21121 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 nstant..*/.SQLIT
21122 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
21123 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
21124 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 ant(Expr *p){.
21125 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e return exprIsCon
21126 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a st(p, 1);.}../*.
21127 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 ** Walk an expre
21128 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 ssion tree. Ret
21129 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 urn 1 if the exp
2112a 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 ression is const
2112b 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 ant.** that does
2112c 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 no originate fr
2112d 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 om the ON or USI
2112e 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 NG clauses of a
2112f 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 join..** Return
21130 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 0 if it involves
21131 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 variables or fu
21132 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 nction calls or
21133 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e terms from.** an
21134 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 ON or USING cla
21135 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 use..*/.SQLITE_P
21136 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
21137 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
21138 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 NotJoin(Expr *p)
21139 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 {. return exprI
2113a 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a sConst(p, 3);.}.
2113b 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
2113c 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
2113d 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
2113e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 expression is c
2113f 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 onstant.** or a
21140 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 function call wi
21141 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 th constant argu
21142 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 ments. Return a
21143 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a nd 0 if there.**
21144 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c are any variabl
21145 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 es..**.** For th
21146 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 e purposes of th
21147 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 is function, a d
21148 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 ouble-quoted str
21149 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a ing (ex: "abc").
2114a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
2114b 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 a variable but
2114c 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 a single-quoted
2114d 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 string (ex: 'abc
2114e 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 ') is.** a const
2114f 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ant..*/.SQLITE_P
21150 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
21151 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
21152 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 OrFunction(Expr
21153 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 *p){. return ex
21154 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b prIsConst(p, 2);
21155 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
21156 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f expression p co
21157 64 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 des a constant i
21158 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 nteger that is s
21159 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 mall enough.** t
2115a 6f 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 o fit in a 32-bi
2115b 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 t integer, retur
2115c 6e 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 n 1 and put the
2115d 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 value of the int
2115e 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c eger.** in *pVal
2115f 75 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 ue. If the expr
21160 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e ession is not an
21161 20 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 integer or if i
21162 74 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 t is too big.**
21163 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e to fit in a sign
21164 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 ed 32-bit intege
21165 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 r, return 0 and
21166 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e leave *pValue un
21167 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 changed..*/.SQLI
21168 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
21169 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
2116a 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 ger(Expr *p, int
2116b 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 *pValue){. int
2116c 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 rc = 0;. if( p
2116d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 ->flags & EP_Int
2116e 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 Value ){. *pV
2116f 61 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c alue = p->u.iVal
21170 75 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ue;. return 1
21171 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 ;. }. switch(
21172 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 p->op ){. cas
21173 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a e TK_INTEGER: {.
21174 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
21175 65 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e e3GetInt32(p->u.
21176 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 65 29 3b zToken, pValue);
21177 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
21178 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 c==0 );. br
21179 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
2117a 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a ase TK_UPLUS: {.
2117b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
2117c 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
2117d 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 p->pLeft, pValue
2117e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
2117f 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21180 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 K_UMINUS: {.
21181 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 int v;. i
21182 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
21183 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 Integer(p->pLeft
21184 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20 , &v) ){.
21185 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 *pValue = -v;.
21186 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 rc = 1;.
21187 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
21188 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
21189 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 fault: break;.
2118a 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 }. if( rc ){.
2118b 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 assert( ExprHa
2118c 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 sAnyProperty(p,
2118d 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f EP_Reduced|EP_To
2118e 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 20 20 20 kenOnly).
2118f 20 20 20 20 20 20 20 20 7c 7c 20 28 70 2d 3e 66 || (p->f
21190 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c lags2 & EP2_Mall
21191 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b ocedToken)==0 );
21192 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f . p->op = TK_
21193 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e INTEGER;. p->
21194 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 flags |= EP_IntV
21195 61 6c 75 65 3b 0a 20 20 20 20 70 2d 3e 75 2e 69 alue;. p->u.i
21196 56 61 6c 75 65 20 3d 20 2a 70 56 61 6c 75 65 3b Value = *pValue;
21197 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
21198 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
21199 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 72 65 n FALSE if there
2119a 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 is no chance th
2119b 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f at the expressio
2119c 6e 20 63 61 6e 20 62 65 20 4e 55 4c 4c 2e 0a 2a n can be NULL..*
2119d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 *.** If the expr
2119e 65 73 73 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 ession might be
2119f 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 65 NULL or if the e
211a0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 6f 6f xpression is too
211a1 20 63 6f 6d 70 6c 65 78 0a 2a 2a 20 74 6f 20 74 complex.** to t
211a2 65 6c 6c 20 72 65 74 75 72 6e 20 54 52 55 45 2e ell return TRUE.
211a3 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .**.** This ro
211a4 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 61 73 utine is used as
211a5 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
211a6 2c 20 74 6f 20 73 6b 69 70 20 4f 50 5f 49 73 4e , to skip OP_IsN
211a7 75 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 77 ull opcodes.** w
211a8 68 65 6e 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 hen we know that
211a9 20 61 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 a value cannot
211aa 62 65 20 4e 55 4c 4c 2e 20 20 48 65 6e 63 65 2c be NULL. Hence,
211ab 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76 a false positiv
211ac 65 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 e.** (returning
211ad 54 52 55 45 20 77 68 65 6e 20 69 6e 20 66 61 63 TRUE when in fac
211ae 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e t the expression
211af 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 4e 55 can never be NU
211b0 4c 4c 29 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 LL) might.** be
211b1 61 20 73 6d 61 6c 6c 20 70 65 72 66 6f 72 6d 61 a small performa
211b2 6e 63 65 20 68 69 74 20 62 75 74 20 69 73 20 6f nce hit but is o
211b3 74 68 65 72 77 69 73 65 20 68 61 72 6d 6c 65 73 therwise harmles
211b4 73 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 s. On the other
211b5 0a 2a 2a 20 68 61 6e 64 2c 20 61 20 66 61 6c 73 .** hand, a fals
211b6 65 20 6e 65 67 61 74 69 76 65 20 28 72 65 74 75 e negative (retu
211b7 72 6e 69 6e 67 20 46 41 4c 53 45 20 77 68 65 6e rning FALSE when
211b8 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 75 6c the result coul
211b9 64 20 62 65 20 4e 55 4c 4c 29 0a 2a 2a 20 77 69 d be NULL).** wi
211ba 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 73 75 6c 74 ll likely result
211bb 20 69 6e 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 in an incorrect
211bc 20 61 6e 73 77 65 72 2e 20 20 53 6f 20 77 68 65 answer. So whe
211bd 6e 20 69 6e 20 64 6f 75 62 74 2c 20 72 65 74 75 n in doubt, retu
211be 72 6e 0a 2a 2a 20 54 52 55 45 2e 0a 2a 2f 0a 53 rn.** TRUE..*/.S
211bf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
211c0 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 6e t sqlite3ExprCan
211c1 42 65 4e 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 BeNull(const Exp
211c2 72 20 2a 70 29 7b 0a 20 20 75 38 20 6f 70 3b 0a r *p){. u8 op;.
211c3 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d while( p->op==
211c4 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f TK_UPLUS || p->o
211c5 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20 p==TK_UMINUS ){
211c6 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0a p = p->pLeft; }.
211c7 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 op = p->op;.
211c8 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 if( op==TK_REGIS
211c9 54 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70 TER ) op = p->op
211ca 32 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 2;. switch( op
211cb 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 ){. case TK_I
211cc 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 NTEGER:. case
211cd 20 54 4b 5f 53 54 52 49 4e 47 3a 0a 20 20 20 20 TK_STRING:.
211ce 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 case TK_FLOAT:.
211cf 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a case TK_BLOB:
211d0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
211d1 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
211d2 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
211d3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
211d4 61 74 65 20 61 6e 20 4f 50 5f 49 73 4e 75 6c 6c ate an OP_IsNull
211d5 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
211d6 74 20 74 65 73 74 73 20 72 65 67 69 73 74 65 72 t tests register
211d7 20 69 52 65 67 20 61 6e 64 20 6a 75 6d 70 73 0a iReg and jumps.
211d8 2a 2a 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 ** to location i
211d9 44 65 73 74 20 69 66 20 74 68 65 20 76 61 6c 75 Dest if the valu
211da 65 20 69 6e 20 69 52 65 67 20 69 73 20 4e 55 4c e in iReg is NUL
211db 4c 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 6e L. The value in
211dc 20 69 52 65 67 20 0a 2a 2a 20 77 61 73 20 63 6f iReg .** was co
211dd 6d 70 75 74 65 64 20 62 79 20 70 45 78 70 72 2e mputed by pExpr.
211de 20 20 49 66 20 77 65 20 63 61 6e 20 6c 6f 6f 6b If we can look
211df 20 61 74 20 70 45 78 70 72 20 61 74 20 63 6f 6d at pExpr at com
211e0 70 69 6c 65 2d 74 69 6d 65 20 61 6e 64 0a 2a 2a pile-time and.**
211e1 20 64 65 74 65 72 6d 69 6e 65 20 74 68 61 74 20 determine that
211e2 69 74 20 63 61 6e 20 6e 65 76 65 72 20 67 65 6e it can never gen
211e3 65 72 61 74 65 20 61 20 4e 55 4c 4c 2c 20 74 68 erate a NULL, th
211e4 65 6e 20 74 68 65 20 4f 50 5f 49 73 4e 75 6c 6c en the OP_IsNull
211e5 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 63 61 operation.** ca
211e6 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f n be omitted..*/
211e7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
211e8 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
211e9 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 0a CodeIsNullJump(.
211ea 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 Vdbe *v,
211eb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
211ec 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 E under construc
211ed 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tion */. const
211ee 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 2f 2a Expr *pExpr, /*
211ef 20 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 20 4f Only generate O
211f0 50 5f 49 73 4e 75 6c 6c 20 69 66 20 74 68 69 73 P_IsNull if this
211f1 20 65 78 70 72 20 63 61 6e 20 62 65 20 4e 55 4c expr can be NUL
211f2 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c L */. int iReg,
211f3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
211f4 73 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 st the value in
211f5 74 68 69 73 20 72 65 67 69 73 74 65 72 20 66 6f this register fo
211f6 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 r NULL */. int
211f7 69 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20 iDest
211f8 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 /* Jump here if
211f9 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6c the value is nul
211fa 6c 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 73 71 l */.){. if( sq
211fb 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e 75 lite3ExprCanBeNu
211fc 6c 6c 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 ll(pExpr) ){.
211fd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
211fe 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c p2(v, OP_IsNull,
211ff 20 69 52 65 67 2c 20 69 44 65 73 74 29 3b 0a 20 iReg, iDest);.
21200 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
21201 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67 rn TRUE if the g
21202 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 iven expression
21203 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 77 68 is a constant wh
21204 69 63 68 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20 ich would be.**
21205 75 6e 63 68 61 6e 67 65 64 20 62 79 20 4f 50 5f unchanged by OP_
21206 41 66 66 69 6e 69 74 79 20 77 69 74 68 20 74 68 Affinity with th
21207 65 20 61 66 66 69 6e 69 74 79 20 67 69 76 65 6e e affinity given
21208 20 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a in the second.*
21209 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a * argument..**.*
2120a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
2120b 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
2120c 69 6e 65 20 69 66 20 74 68 65 20 4f 50 5f 41 66 ine if the OP_Af
2120d 66 69 6e 69 74 79 20 6f 70 65 72 61 74 69 6f 6e finity operation
2120e 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 .** can be omitt
2120f 65 64 2e 20 20 57 68 65 6e 20 69 6e 20 64 6f 75 ed. When in dou
21210 62 74 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e bt return FALSE.
21211 20 20 41 20 66 61 6c 73 65 20 6e 65 67 61 74 69 A false negati
21212 76 65 0a 2a 2a 20 69 73 20 68 61 72 6d 6c 65 73 ve.** is harmles
21213 73 2e 20 20 41 20 66 61 6c 73 65 20 70 6f 73 69 s. A false posi
21214 74 69 76 65 2c 20 68 6f 77 65 76 65 72 2c 20 63 tive, however, c
21215 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 an result in the
21216 20 77 72 6f 6e 67 0a 2a 2a 20 61 6e 73 77 65 72 wrong.** answer
21217 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21218 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
21219 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 6e 69 xprNeedsNoAffini
2121a 74 79 43 68 61 6e 67 65 28 63 6f 6e 73 74 20 45 tyChange(const E
2121b 78 70 72 20 2a 70 2c 20 63 68 61 72 20 61 66 66 xpr *p, char aff
2121c 29 7b 0a 20 20 75 38 20 6f 70 3b 0a 20 20 69 66 ){. u8 op;. if
2121d 28 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 ( aff==SQLITE_AF
2121e 46 5f 4e 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 F_NONE ) return
2121f 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 1;. while( p->o
21220 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 p==TK_UPLUS || p
21221 2d 3e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 ->op==TK_UMINUS
21222 29 7b 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b ){ p = p->pLeft;
21223 20 7d 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b }. op = p->op;
21224 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 . if( op==TK_RE
21225 47 49 53 54 45 52 20 29 20 6f 70 20 3d 20 70 2d GISTER ) op = p-
21226 3e 6f 70 32 3b 0a 20 20 73 77 69 74 63 68 28 20 >op2;. switch(
21227 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
21228 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 K_INTEGER: {.
21229 20 20 20 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 return aff==S
2122a 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
2122b 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 R || aff==SQLITE
2122c 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 _AFF_NUMERIC;.
2122d 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
2122e 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 FLOAT: {. r
2122f 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49 54 eturn aff==SQLIT
21230 45 5f 41 46 46 5f 52 45 41 4c 20 7c 7c 20 61 66 E_AFF_REAL || af
21231 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 f==SQLITE_AFF_NU
21232 4d 45 52 49 43 3b 0a 20 20 20 20 7d 0a 20 20 20 MERIC;. }.
21233 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a case TK_STRING:
21234 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 {. return
21235 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f aff==SQLITE_AFF_
21236 54 45 58 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 TEXT;. }.
21237 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a case TK_BLOB: {.
21238 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
21239 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
2123a 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 K_COLUMN: {.
2123b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 54 61 assert( p->iTa
2123c 62 6c 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 70 20 ble>=0 ); /* p
2123d 63 61 6e 6e 6f 74 20 62 65 20 70 61 72 74 20 6f cannot be part o
2123e 66 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 f a CHECK constr
2123f 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 aint */. re
21240 74 75 72 6e 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3c turn p->iColumn<
21241 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 0. && (
21242 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f aff==SQLITE_AFF_
21243 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 3d 3d INTEGER || aff==
21244 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
21245 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 IC);. }. d
21246 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
21247 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
21248 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
21249 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 urn TRUE if the
2124a 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 20 given string is
2124b 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 a row-id column
2124c 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f name..*/.SQLITE_
2124d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
2124e 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 te3IsRowid(const
2124f 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 char *z){. if(
21250 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
21251 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 z, "_ROWID_")==0
21252 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 ) return 1;. i
21253 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
21254 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 p(z, "ROWID")==0
21255 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 ) return 1;. i
21256 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
21257 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 p(z, "OID")==0 )
21258 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 return 1;. ret
21259 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
2125a 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 77 Return true if w
2125b 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74 68 e are able to th
2125c 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70 e IN operator op
2125d 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0a timization on a.
2125e 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 ** query of the
2125f 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 form.**.**
21260 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e x IN (SELECT ..
21261 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 74 .).**.** Where t
21262 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 6c 61 he SELECT... cla
21263 75 73 65 20 69 73 20 61 73 20 73 70 65 63 69 66 use is as specif
21264 69 65 64 20 62 79 20 74 68 65 20 70 61 72 61 6d ied by the param
21265 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a 2a 20 eter to this.**
21266 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 routine..**.** T
21267 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 he Select object
21268 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 20 61 passed in has a
21269 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65 70 lready been prep
2126a 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e 6f 0a rocessed and no.
2126b 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 20 62 ** errors have b
2126c 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 69 een found..*/.#i
2126d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
2126e 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 74 69 T_SUBQUERY.stati
2126f 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 61 74 c int isCandidat
21270 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 63 74 eForInOpt(Select
21271 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 *p){. SrcList
21272 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c 69 73 *pSrc;. ExprLis
21273 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 61 62 t *pEList;. Tab
21274 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 28 20 le *pTab;. if(
21275 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
21276 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21277 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64 /* right-hand
21278 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20 53 side of IN is S
21279 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 ELECT */. if( p
2127a 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75 72 ->pPrior ) retur
2127b 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 n 0;
2127c 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 6f /* Not a compo
2127d 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 und SELECT */.
2127e 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 if( p->selFlags
2127f 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 & (SF_Distinct|S
21280 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b 0a F_Aggregate) ){.
21281 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 testcase( (p
21282 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 ->selFlags & (SF
21283 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 _Distinct|SF_Agg
21284 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 regate))==SF_Dis
21285 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 65 73 tinct );. tes
21286 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c tcase( (p->selFl
21287 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e ags & (SF_Distin
21288 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 ct|SF_Aggregate)
21289 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65 20 )==SF_Aggregate
2128a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
2128b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 54 20 /* No DISTINCT
2128c 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f 20 61 keyword and no a
2128d 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
2128e 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 ns */. }. asse
2128f 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d rt( p->pGroupBy=
21290 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 =0 );
21291 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 52 4f /* Has no GRO
21292 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a UP BY clause */.
21293 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 if( p->pLimit
21294 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
21295 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 20 /* Has
21296 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 no LIMIT clause
21297 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e */. assert( p->
21298 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 pOffset==0 );
21299 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
2129a 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 6e 6f o LIMIT means no
2129b 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 66 28 OFFSET */. if(
2129c 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 p->pWhere ) ret
2129d 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 urn 0;
2129e 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 /* Has no WH
2129f 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ERE clause */.
212a0 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a pSrc = p->pSrc;.
212a1 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 3d assert( pSrc!=
212a2 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 0 );. if( pSrc-
212a3 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 72 >nSrc!=1 ) retur
212a4 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a n 0; /*
212a5 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 Single term in
212a6 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 FROM clause */.
212a7 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e if( pSrc->a[0].
212a8 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e pSelect ) return
212a9 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 0; /* FROM
212aa 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65 72 is not a subquer
212ab 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 70 y or view */. p
212ac 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d Tab = pSrc->a[0]
212ad 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45 56 .pTab;. if( NEV
212ae 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 65 ER(pTab==0) ) re
212af 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 turn 0;. assert
212b0 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d ( pTab->pSelect=
212b1 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 =0 );
212b2 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 /* FROM clause
212b3 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f is not a view */
212b4 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c . if( IsVirtual
212b5 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 (pTab) ) return
212b6 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 0; /* FRO
212b7 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20 76 M clause not a v
212b8 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a irtual table */.
212b9 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 pEList = p->pE
212ba 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 List;. if( pELi
212bb 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 st->nExpr!=1 ) r
212bc 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 2f eturn 0; /
212bd 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 * One column in
212be 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a the result set *
212bf 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e /. if( pEList->
212c0 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d a[0].pExpr->op!=
212c1 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 TK_COLUMN ) retu
212c2 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20 rn 0; /* Result
212c3 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 is a column */.
212c4 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
212c5 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
212c6 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a IT_SUBQUERY */..
212c7 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
212c8 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 ion is used by t
212c9 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
212ca 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e n of the IN (...
212cb 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49 ) operator..** I
212cc 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66 69 t's job is to fi
212cd 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20 62 nd or create a b
212ce 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 -tree structure
212cf 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 that may be used
212d0 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74 65 .** either to te
212d1 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 st for membershi
212d2 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20 73 p of the (...) s
212d3 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74 65 et or to iterate
212d4 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20 through.** its
212d5 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e members, skippin
212d6 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a g duplicates..**
212d7 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66 .** The index of
212d8 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 65 6e the cursor open
212d9 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 ed on the b-tree
212da 20 28 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 (database table
212db 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 , database index
212dc 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c .** or ephermal
212dd 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 65 table) is store
212de 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 d in pX->iTable
212df 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 before this func
212e0 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a tion returns..**
212e1 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 The returned va
212e2 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 lue of this func
212e3 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 20 74 tion indicates t
212e4 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 2c 20 he b-tree type,
212e5 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
212e6 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 * IN_INDEX_ROW
212e7 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 ID - The cursor
212e8 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 was opened on a
212e9 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a database table..
212ea 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e ** IN_INDEX_IN
212eb 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f 72 DEX - The cursor
212ec 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 was opened on a
212ed 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 2e database index.
212ee 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 .** IN_INDEX_E
212ef 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 6f PH - The curso
212f0 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
212f1 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 61 a specially crea
212f2 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 ted and.**
212f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6f po
212f4 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d 61 pulated epherema
212f5 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 l table..**.** A
212f6 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72 65 n existing b-tre
212f7 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 e may only be us
212f8 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 ed if the SELECT
212f9 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c is of the simpl
212fa 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a e.** form:.**.**
212fb 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c SELECT <col
212fc 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 umn> FROM <table
212fd 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 >.**.** If the p
212fe 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 rNotFound parame
212ff 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 74 ter is 0, then t
21300 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 62 he b-tree will b
21301 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 e used to iterat
21302 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 e.** through the
21303 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b set members, sk
21304 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 ipping any dupli
21305 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 cates. In this c
21306 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 ase an.** ephere
21307 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 mal table must b
21308 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 e used unless th
21309 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 e selected <colu
2130a 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 mn> is guarantee
2130b 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 d.** to be uniqu
2130c 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 e - either becau
2130d 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 se it is an INTE
2130e 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
2130f 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 20 55 or it.** has a U
21310 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 NIQUE constraint
21311 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 65 78 or UNIQUE index
21312 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
21313 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 rNotFound parame
21314 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 ter is not 0, th
21315 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77 69 en the b-tree wi
21316 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 ll be used .** f
21317 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62 or fast set memb
21318 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e ership tests. In
21319 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70 this case an ep
2131a 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 heremal table mu
2131b 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 st .** be used u
2131c 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 nless <column> i
2131d 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 s an INTEGER PRI
2131e 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 MARY KEY or an i
2131f 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 ndex can .** be
21320 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 found with <colu
21321 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d mn> as its left-
21322 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a most column..**.
21323 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74 72 ** When the b-tr
21324 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 ee is being used
21325 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 for membership
21326 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c 69 tests, the calli
21327 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6e ng function.** n
21328 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 eeds to know whe
21329 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
2132a 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
2132b 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a ns an SQL NULL .
2132c 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 ** value in orde
2132d 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 r to correctly e
2132e 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 69 valuate expressi
2132f 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 ons like "X IN (
21330 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 Y, Z)"..** If th
21331 65 72 65 20 69 73 20 61 6e 79 20 63 68 61 6e 63 ere is any chanc
21332 65 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e 29 e that the (...)
21333 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 might contain a
21334 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a NULL value at.*
21335 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 * runtime, then
21336 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 6c a register is al
21337 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 located and the
21338 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 register number
21339 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 written.** to *p
2133a 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 rNotFound. If th
2133b 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 ere is no chance
2133c 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e 29 20 that the (...)
2133d 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 contains a.** NU
2133e 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a LL value, then *
2133f 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 prNotFound is le
21340 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a ft unchanged..**
21341 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 .** If a registe
21342 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 r is allocated a
21343 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 nd its location
21344 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 stored in *prNot
21345 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 Found, then.** i
21346 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 ts initial value
21347 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 is NULL. If th
21348 65 20 28 2e 2e 2e 29 20 64 6f 65 73 20 6e 6f 74 e (...) does not
21349 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 remain constant
2134a 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72 61 .** for the dura
2134b 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 72 tion of the quer
2134c 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 4c 45 y (i.e. the SELE
2134d 43 54 20 77 69 74 68 69 6e 20 74 68 65 20 28 2e CT within the (.
2134e 2e 2e 29 0a 2a 2a 20 69 73 20 61 20 63 6f 72 72 ..).** is a corr
2134f 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 29 elated subquery)
21350 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
21351 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 of the allocated
21352 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 register is.**
21353 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 reset to NULL ea
21354 63 68 20 74 69 6d 65 20 74 68 65 20 73 75 62 71 ch time the subq
21355 75 65 72 79 20 69 73 20 72 65 72 75 6e 2e 20 54 uery is rerun. T
21356 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a his allows the.*
21357 2a 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 * caller to use
21358 76 64 62 65 20 63 6f 64 65 20 65 71 75 69 76 61 vdbe code equiva
21359 6c 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c lent to the foll
2135a 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 owing:.**.** i
2135b 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c f( register==NUL
2135c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f L ){.** has_
2135d 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 null = <test if
2135e 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 data structure c
2135f 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a ontains null>.**
21360 20 20 20 20 20 72 65 67 69 73 74 65 72 20 3d 20 register =
21361 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 1.** }.**.** i
21362 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 n order to avoid
21363 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 running the <te
21364 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 st if data struc
21365 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 ture contains nu
21366 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 ll>.** test more
21367 20 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20 6e often than is n
21368 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 ecessary..*/.#if
21369 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2136a 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 _SUBQUERY.SQLITE
2136b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
2136c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 ite3FindInIndex(
2136d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
2136e 78 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 xpr *pX, int *pr
2136f 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c NotFound){. Sel
21370 65 63 74 20 2a 70 3b 20 20 20 20 20 20 20 20 20 ect *p;
21371 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21372 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 /* SELECT to
21373 74 68 65 20 72 69 67 68 74 20 6f 66 20 49 4e 20 the right of IN
21374 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e operator */. in
21375 74 20 65 54 79 70 65 20 3d 20 30 3b 20 20 20 20 t eType = 0;
21376 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21377 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 /* Type of R
21378 48 53 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 HS table. IN_IND
21379 45 58 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 EX_* */. int iT
2137a 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 ab = pParse->nTa
2137b 62 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 b++;
2137c 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 68 65 /* Cursor of the
2137d 20 52 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 RHS table */.
2137e 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 int mustBeUnique
2137f 20 3d 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d = (prNotFound==
21380 30 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 0); /* True if
21381 20 52 48 53 20 6d 75 73 74 20 62 65 20 75 6e 69 RHS must be uni
21382 71 75 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 que */.. assert
21383 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 ( pX->op==TK_IN
21384 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 );.. /* Check t
21385 6f 20 73 65 65 20 69 66 20 61 6e 20 65 78 69 73 o see if an exis
21386 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69 6e ting table or in
21387 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 dex can be used
21388 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66 79 20 to. ** satisfy
21389 74 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 the query. This
2138a 20 69 73 20 70 72 65 66 65 72 61 62 6c 65 20 74 is preferable t
2138b 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 6e o generating a n
2138c 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d 65 72 ew . ** ephemer
2138d 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 al table.. */.
2138e 20 70 20 3d 20 28 45 78 70 72 48 61 73 50 72 6f p = (ExprHasPro
2138f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73 perty(pX, EP_xIs
21390 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e Select) ? pX->x.
21391 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20 pSelect : 0);.
21392 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 72 73 if( ALWAYS(pPars
21393 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20 69 e->nErr==0) && i
21394 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f sCandidateForInO
21395 70 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71 6c pt(p) ){. sql
21396 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
21397 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 e->db;
21398 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
21399 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
2139a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 Expr *pExpr =
2139b 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e p->pEList->a[0].
2139c 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 78 70 72 pExpr; /* Expr
2139d 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 ession <column>
2139e 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 */. int iCol
2139f 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
213a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
213a1 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e Index of column
213a2 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 <column> */.
213a3 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 Vdbe *v = sqlit
213a4 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 e3GetVdbe(pParse
213a5 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 ); /* Virtu
213a6 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 al machine being
213a7 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 54 61 coded */. Ta
213a8 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 ble *pTab = p->p
213a9 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 20 Src->a[0].pTab;
213aa 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 3c 74 /* Table <t
213ab 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 6e able>. */. in
213ac 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 t iDb;
213ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213ae 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
213af 20 69 64 78 20 66 6f 72 20 70 54 61 62 20 2a 2f idx for pTab */
213b0 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f 64 65 . . /* Code
213b1 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f an OP_VerifyCoo
213b2 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 6c 65 kie and OP_Table
213b3 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c 65 3e Lock for <table>
213b4 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 . */. iDb = s
213b5 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e qlite3SchemaToIn
213b6 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 dex(db, pTab->pS
213b7 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69 chema);. sqli
213b8 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 te3CodeVerifySch
213b9 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 ema(pParse, iDb)
213ba 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 ;. sqlite3Tab
213bb 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 leLock(pParse, i
213bc 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 Db, pTab->tnum,
213bd 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0, pTab->zName);
213be 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 75 .. /* This fu
213bf 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 nction is only c
213c0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f 20 70 alled from two p
213c1 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 20 63 laces. In both c
213c2 61 73 65 73 20 74 68 65 20 76 64 62 65 0a 20 20 ases the vdbe.
213c3 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 ** has already
213c4 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e been allocated.
213c5 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c 69 74 So assume sqlit
213c6 65 33 47 65 74 56 64 62 65 28 29 20 69 73 20 61 e3GetVdbe() is a
213c7 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 75 63 lways. ** suc
213c8 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a 20 20 cessful here..
213c9 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
213ca 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c v);. if( iCol
213cb 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 <0 ){. int
213cc 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d iMem = ++pParse-
213cd 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 >nMem;. int
213ce 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 20 20 69 iAddr;.. i
213cf 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 Addr = sqlite3Vd
213d0 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 beAddOp1(v, OP_I
213d1 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 f, iMem);.
213d2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
213d3 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
213d4 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 1, iMem);..
213d5 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 sqlite3OpenTab
213d6 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c le(pParse, iTab,
213d7 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f iDb, pTab, OP_O
213d8 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 penRead);.
213d9 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 eType = IN_INDEX
213da 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 _ROWID;.. s
213db 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
213dc 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 re(v, iAddr);.
213dd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 }else{. I
213de 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 ndex *pIdx;
213df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213e0 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 /* Iterator
213e1 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 variable */..
213e2 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 /* The collat
213e3 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65 ion sequence use
213e4 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61 72 69 d by the compari
213e5 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 son. If an index
213e6 20 69 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 is to. **
213e7 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 be used in place
213e8 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c 65 of a temp-table
213e9 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 64 , it must be ord
213ea 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a 20 ered according.
213eb 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 ** to this
213ec 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
213ed 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f ce. */. Co
213ee 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71 llSeq *pReq = sq
213ef 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 lite3BinaryCompa
213f0 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 reCollSeq(pParse
213f1 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 , pX->pLeft, pEx
213f2 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 pr);.. /* C
213f3 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66 heck that the af
213f4 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c finity that will
213f5 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66 be used to perf
213f6 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a orm the . *
213f7 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 * comparison is
213f8 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 the same as the
213f9 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 affinity of the
213fa 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 column. If.
213fb 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 ** it is not, i
213fc 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
213fd 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 e to use any ind
213fe 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ex.. */.
213ff 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f char aff = co
21400 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 mparisonAffinity
21401 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 (pX);. int
21402 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70 affinity_ok = (p
21403 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e Tab->aCol[iCol].
21404 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61 affinity==aff||a
21405 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
21406 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72 ONE);.. for
21407 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 (pIdx=pTab->pInd
21408 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79 70 ex; pIdx && eTyp
21409 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74 79 e==0 && affinity
2140a 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e _ok; pIdx=pIdx->
2140b 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 pNext){.
2140c 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c if( (pIdx->aiCol
2140d 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20 umn[0]==iCol).
2140e 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 && sqlite
2140f 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 3FindCollSeq(db,
21410 20 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e ENC(db), pIdx->
21411 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 3d 3d 70 azColl[0], 0)==p
21412 52 65 71 0a 20 20 20 20 20 20 20 20 20 26 26 20 Req. &&
21413 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65 20 7c (!mustBeUnique |
21414 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e | (pIdx->nColumn
21415 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 ==1 && pIdx->onE
21416 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a rror!=OE_None)).
21417 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
21418 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 int iMem =
21419 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
2141a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 41 int iA
2141b 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 63 ddr;. c
2141c 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 har *pKey;. .
2141d 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20 28 pKey = (
2141e 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49 6e char *)sqlite3In
2141f 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 dexKeyinfo(pPars
21420 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 e, pIdx);.
21421 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 iAddr = sqli
21422 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
21423 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 OP_If, iMem);.
21424 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21425 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21426 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 _Integer, 1, iMe
21427 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 m);. .
21428 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21429 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 p4(v, OP_OpenRea
2142a 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 d, iTab, pIdx->t
2142b 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 num, iDb,.
2142c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2142d 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 34 pKey,P4
2142e 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
2142f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 );. Vdb
21430 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 eComment((v, "%s
21431 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 ", pIdx->zName))
21432 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 70 ;. eTyp
21433 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 e = IN_INDEX_IND
21434 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 EX;.. s
21435 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
21436 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 re(v, iAddr);.
21437 20 20 20 20 20 20 20 20 69 66 28 20 70 72 4e 6f if( prNo
21438 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 62 2d tFound && !pTab-
21439 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e >aCol[iCol].notN
2143a 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ull ){.
2143b 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d *prNotFound =
2143c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
2143d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2143e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
2143f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
21440 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 eType==0 ){.
21441 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66 6f 75 /* Could not fou
21442 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 nd an existing t
21443 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 6f able or index to
21444 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53 20 use as the RHS
21445 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20 57 b-tree.. ** W
21446 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20 67 e will have to g
21447 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65 6d enerate an ephem
21448 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64 6f eral table to do
21449 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a 2f the job.. */
2144a 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76 . int rMayHav
2144b 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 65 eNull = 0;. e
2144c 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f Type = IN_INDEX_
2144d 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72 4e EPH;. if( prN
2144e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 otFound ){.
2144f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 72 *prNotFound = r
21450 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b 2b MayHaveNull = ++
21451 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
21452 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d 3e }else if( pX->
21453 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 pLeft->iColumn<0
21454 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 && !ExprHasAnyP
21455 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 roperty(pX, EP_x
21456 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 IsSelect) ){.
21457 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e eType = IN_IN
21458 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d DEX_ROWID;. }
21459 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 . sqlite3Code
2145a 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 Subselect(pParse
2145b 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e 75 , pX, rMayHaveNu
2145c 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e ll, eType==IN_IN
2145d 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d 65 DEX_ROWID);. }e
2145e 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61 lse{. pX->iTa
2145f 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a ble = iTab;. }.
21460 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b 0a return eType;.
21461 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
21462 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f Generate code fo
21463 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 72 r scalar subquer
21464 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 65 ies used as an e
21465 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64 xpression.** and
21466 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 20 IN operators.
21467 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 Examples:.**.**
21468 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 52 (SELECT a FR
21469 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 2d OM b) -
2146a 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 - subquery.**
2146b 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 54 EXISTS (SELECT
2146c 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20 a FROM b) --
2146d 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79 0a EXISTS subquery.
2146e 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c 35 ** x IN (4,5
2146f 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20 20 ,11)
21470 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 -- IN operator
21471 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 69 with list on ri
21472 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a ght-hand side.**
21473 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 x IN (SELEC
21474 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 20 T a FROM b)
21475 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 -- IN operator w
21476 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e 20 ith subquery on
21477 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20 the right.**.**
21478 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d 65 The pExpr parame
21479 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 68 ter describes th
2147a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 e expression tha
2147b 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 t contains the I
2147c 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72 N.** operator or
2147d 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a subquery..**.**
2147e 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 73 If parameter is
2147f 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 Rowid is non-zer
21480 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73 69 o, then expressi
21481 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 61 72 on pExpr is guar
21482 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 anteed.** to be
21483 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72 6f of the form "<ro
21484 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 3f wid> IN (?, ?, ?
21485 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69 64 )", where <rowid
21486 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 > is a reference
21487 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74 65 .** to some inte
21488 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 6f ger key column o
21489 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65 65 f a table B-Tree
2148a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 . In this case,
2148b 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 use an.** intkey
2148c 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72 65 B-Tree to store
2148d 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28 2e the set of IN(.
2148e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74 65 ..) values inste
2148f 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c 0a ad of the usual.
21490 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72 69 ** (slower) vari
21491 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 73 able length keys
21492 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 B-Tree..**.** I
21493 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 f rMayHaveNull i
21494 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61 74 s non-zero, that
21495 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
21496 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 operation is an
21497 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 45 4c IN.** (not a SEL
21498 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 20 61 ECT or EXISTS) a
21499 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 53 20 nd that the RHS
2149a 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 20 4e might contains N
2149b 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 65 72 ULLs..** Further
2149c 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 73 20 more, the IN is
2149d 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 in a WHERE claus
2149e 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 72 65 e and that we re
2149f 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 6f 20 ally want.** to
214a0 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 68 65 iterate over the
214a1 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f RHS of the IN o
214a2 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 65 72 perator in order
214a3 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63 61 to quickly loca
214a4 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 73 te.** all corres
214a5 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65 6d ponding LHS elem
214a6 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73 20 ents. All this
214a7 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73 20 routine does is
214a8 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 initialize.** th
214a9 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65 6e e register given
214aa 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c by rMayHaveNull
214ab 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c 69 to NULL. Calli
214ac 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c ng routines will
214ad 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 6f 66 take.** care of
214ae 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20 72 changing this r
214af 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74 6f egister value to
214b0 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68 65 non-NULL if the
214b1 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72 65 RHS is NULL-fre
214b2 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 e..**.** If rMay
214b3 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 72 6f HaveNull is zero
214b4 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 , that means tha
214b5 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 t the subquery i
214b6 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a 2a 20 s being used.**
214b7 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 74 for membership t
214b8 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 esting only. Th
214b9 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 ere is no need t
214ba 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 o initialize any
214bb 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74 6f .** registers to
214bc 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 70 72 indicate the pr
214bd 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 6e 63 esense or absenc
214be 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 74 68 e of NULLs on th
214bf 65 20 52 48 53 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 e RHS..**.** For
214c0 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58 49 a SELECT or EXI
214c1 53 54 53 20 6f 70 65 72 61 74 6f 72 2c 20 72 65 STS operator, re
214c2 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 turn the registe
214c3 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 r that holds the
214c4 0a 2a 2a 20 72 65 73 75 6c 74 2e 20 20 46 6f 72 .** result. For
214c5 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 6f 72 IN operators or
214c6 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
214c7 75 72 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20 urs, the return
214c8 76 61 6c 75 65 20 69 73 20 30 2e 0a 2a 2f 0a 23 value is 0..*/.#
214c9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
214ca 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 IT_SUBQUERY.SQLI
214cb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
214cc 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c qlite3CodeSubsel
214cd 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ect(. Parse *pP
214ce 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f arse, /
214cf 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
214d0 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 t */. Expr *pEx
214d1 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f pr, /
214d2 2a 20 54 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 * The IN, SELECT
214d3 2c 20 6f 72 20 45 58 49 53 54 53 20 6f 70 65 72 , or EXISTS oper
214d4 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d ator */. int rM
214d5 61 79 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 ayHaveNull,
214d6 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 68 /* Register th
214d7 61 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 at records wheth
214d8 65 72 20 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 er NULLs exist i
214d9 6e 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 n RHS */. int i
214da 73 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 sRowid
214db 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c /* If true, L
214dc 48 53 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f HS of IN operato
214dd 72 20 69 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a r is a rowid */.
214de 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64 ){. int testAdd
214df 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 r = 0;
214e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
214e1 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64 One-time test ad
214e2 64 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 72 dress */. int r
214e3 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Reg = 0;
214e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214e5 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 73 /* Register s
214e6 74 6f 72 69 6e 67 20 72 65 73 75 6c 74 69 6e 67 toring resulting
214e7 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 */. Vdbe *v =
214e8 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 sqlite3GetVdbe(p
214e9 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 4e 45 Parse);. if( NE
214ea 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 VER(v==0) ) retu
214eb 72 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 rn 0;. sqlite3E
214ec 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 xprCachePush(pPa
214ed 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 rse);.. /* This
214ee 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 code must be ru
214ef 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 n in its entiret
214f0 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 y every time it
214f1 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 is encountered.
214f2 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 ** if any of th
214f3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 e following is t
214f4 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 rue:. **. **
214f5 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 * The right-h
214f6 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 6f and side is a co
214f7 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 rrelated subquer
214f8 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 y. ** * The
214f9 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
214fa 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
214fb 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e n list containin
214fc 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a g variables. **
214fd 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e * We are in
214fe 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 side a trigger.
214ff 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 **. ** If all
21500 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 of the above are
21501 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 false, then we
21502 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 can run this cod
21503 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a e just once. **
21504 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 save the result
21505 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 s, and reuse the
21506 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 same result on
21507 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 subsequent invoc
21508 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 ations.. */. i
21509 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 f( !ExprHasAnyPr
2150a 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
2150b 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 _VarSelect) && !
2150c 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 pParse->pTrigger
2150d 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d Tab ){. int m
2150e 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e em = ++pParse->n
2150f 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Mem;. sqlite3
21510 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
21511 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 _If, mem);. t
21512 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 65 estAddr = sqlite
21513 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21514 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65 P_Integer, 1, me
21515 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 m);. assert(
21516 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 50 testAddr>0 || pP
21517 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
21518 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 Failed );. }..
21519 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e switch( pExpr->
2151a 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 op ){. case T
2151b 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 K_IN: {. ch
2151c 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 ar affinity;.
2151d 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e KeyInfo keyIn
2151e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 fo;. int ad
2151f 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 64 dr; /* Ad
21520 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e dress of OP_Open
21521 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 Ephemeral instru
21522 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 ction */. E
21523 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 xpr *pLeft = pEx
21524 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20 pr->pLeft;..
21525 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e 75 if( rMayHaveNu
21526 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ll ){. sq
21527 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21528 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 v, OP_Null, 0, r
21529 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 MayHaveNull);.
2152a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 66 }.. aff
2152b 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45 inity = sqlite3E
2152c 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66 xprAffinity(pLef
2152d 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 t);.. /* Wh
2152e 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 6e ether this is an
2152f 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 'x IN(SELECT...
21530 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c )' or an 'x IN(<
21531 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 exprlist>)'.
21532 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
21533 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 68 it is handled th
21534 65 20 73 61 6d 65 20 77 61 79 2e 20 20 41 6e 20 e same way. An
21535 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 ephemeral table
21536 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c is . ** fil
21537 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 2d led with single-
21538 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 field index keys
21539 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
2153a 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 e results.
2153b 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 ** from the SELE
2153c 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 6c CT or the <exprl
2153d 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 ist>.. **.
2153e 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 27 ** If the '
2153f 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 x' expression is
21540 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c a column value,
21541 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e or the SELECT..
21542 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 .. ** state
21543 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 63 ment returns a c
21544 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 olumn value, the
21545 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f n the affinity o
21546 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 f that. **
21547 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 74 column is used t
21548 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 65 o build the inde
21549 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 20 x keys. If both
2154a 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 20 'x' and the.
2154b 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 ** SELECT... s
2154c 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c tatement are col
2154d 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 umns, then numer
2154e 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 ic affinity is u
2154f 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 sed. ** if
21550 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 either column ha
21551 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 s NUMERIC or INT
21552 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 49 EGER affinity. I
21553 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 20 f neither.
21554 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 53 ** 'x' nor the S
21555 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 ELECT... stateme
21556 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 nt are columns,
21557 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 then numeric aff
21558 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 inity. ** i
21559 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f s used.. */
2155a 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 . pExpr->iT
2155b 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e able = pParse->n
2155c 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 Tab++;. add
2155d 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
2155e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp2(v, OP_Open
2155f 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 Ephemeral, pExpr
21560 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 ->iTable, !isRow
21561 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 id);. memse
21562 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 t(&keyInfo, 0, s
21563 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b izeof(keyInfo));
21564 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e . keyInfo.n
21565 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 Field = 1;..
21566 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
21567 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21568 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
21569 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a /* Case 1:
2156a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53 45 expr IN (SE
2156b 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 LECT ...).
2156c 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 **. **
2156d 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
2156e 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c write the resul
2156f 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 ts of the select
21570 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 into the tempor
21571 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ary. ** t
21572 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 able allocated a
21573 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e nd opened above.
21574 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
21575 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 SelectDest d
21576 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78 70 est;. Exp
21577 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a rList *pEList;..
21578 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
21579 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 !isRowid );.
2157a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
2157b 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c tDestInit(&dest,
2157c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72 2d SRT_Set, pExpr-
2157d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 >iTable);.
2157e 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79 20 dest.affinity
2157f 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b 0a = (u8)affinity;.
21580 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
21581 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 (pExpr->iTable&0
21582 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78 70 x0000FFFF)==pExp
21583 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 r->iTable );.
21584 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
21585 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
21586 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c Expr->x.pSelect,
21587 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 &dest) ){.
21588 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
21589 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2158a 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d pEList = pExpr-
2158b 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 >x.pSelect->pELi
2158c 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 st;. if(
2158d 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d 30 ALWAYS(pEList!=0
2158e 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
2158f 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20 20 r>0) ){ .
21590 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c keyInfo.aColl
21591 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 6e [0] = sqlite3Bin
21592 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 aryCompareCollSe
21593 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d q(pParse, pExpr-
21594 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20 20 >pLeft,.
21595 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b pEList->a[
21596 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 0].pExpr);.
21597 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
21598 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c if( pExpr->x.pL
21599 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ist!=0 ){.
2159a 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20 /* Case 2:
2159b 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69 expr IN (exprli
2159c 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 st). **.
2159d 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 ** For ea
2159e 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 ch expression, b
2159f 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 uild an index ke
215a0 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 y from the evalu
215a1 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 ation and.
215a2 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e ** store it in
215a3 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 the temporary t
215a4 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 able. If <expr>
215a5 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 is a column, the
215a6 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a n use. **
215a7 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 that columns af
215a8 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c finity when buil
215a9 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e ding index keys.
215aa 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f If <expr> is no
215ab 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 t. ** a c
215ac 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 olumn, use numer
215ad 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 ic affinity..
215ae 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
215af 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45 int i;. E
215b0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d xprList *pList =
215b1 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b pExpr->x.pList;
215b2 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 . struct
215b3 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
215b4 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e Item;. in
215b5 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20 t r1, r2, r3;..
215b6 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69 if( !affi
215b7 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20 nity ){.
215b8 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c affinity = SQL
215b9 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 ITE_AFF_NONE;.
215ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
215bb 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d keyInfo.aColl[0]
215bc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
215bd 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 llSeq(pParse, pE
215be 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 xpr->pLeft);..
215bf 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 /* Loop th
215c0 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65 rough each expre
215c1 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 ssion in <exprli
215c2 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 st>. */.
215c3 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
215c4 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
215c5 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c r2 = sql
215c6 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
215c7 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
215c8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
215c9 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
215ca 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f r2);. fo
215cb 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 r(i=pList->nExpr
215cc 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 , pItem=pList->a
215cd 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
215ce 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 m++){.
215cf 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65 Expr *pE2 = pIte
215d0 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 m->pExpr;.
215d1 20 20 20 20 69 6e 74 20 69 56 61 6c 54 6f 49 6e int iValToIn
215d2 73 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a s;.. /*
215d3 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
215d4 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 on is not consta
215d5 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 nt then we will
215d6 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 need to.
215d7 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 65 ** disable the
215d8 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20 67 test that was g
215d9 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 74 enerated above t
215da 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 hat makes sure.
215db 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 ** this
215dc 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 code only execu
215dd 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 tes once. Becau
215de 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e se for a non-con
215df 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 20 stant.
215e0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 65 ** expression we
215e1 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 74 need to rerun t
215e2 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 69 his code each ti
215e3 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f me.. */
215e4 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 74 . if( t
215e5 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c 69 estAddr && !sqli
215e6 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
215e7 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 20 t(pE2) ){.
215e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
215e9 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c eChangeToNoop(v,
215ea 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 3b testAddr-1, 2);
215eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 . tes
215ec 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 tAddr = 0;.
215ed 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 }..
215ee 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 /* Evaluate th
215ef 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 e expression and
215f0 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f 20 insert it into
215f1 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 2a the temp table *
215f2 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
215f3 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c 69 74 isRowid && sqlit
215f4 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
215f5 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e 73 29 pE2, &iValToIns)
215f6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
215f7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
215f8 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 49 6e 3(v, OP_InsertIn
215f9 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 t, pExpr->iTable
215fa 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49 6e 73 29 , r2, iValToIns)
215fb 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
215fc 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 e{. r
215fd 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 3 = sqlite3ExprC
215fe 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 odeTarget(pParse
215ff 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 , pE2, r1);.
21600 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52 6f if( isRo
21601 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 wid ){.
21602 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21603 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 AddOp2(v, OP_Mus
21604 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20 20 20 20 tBeInt, r3,.
21605 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21606 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
21607 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 te3VdbeCurrentAd
21608 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 20 dr(v)+2);.
21609 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
2160a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
2160b 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 Insert, pExpr->i
2160c 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a Table, r2, r3);.
2160d 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 }els
2160e 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e{.
2160f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21610 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 p4(v, OP_MakeRec
21611 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 ord, r3, 1, r2,
21612 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 &affinity, 1);.
21613 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
21614 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 ite3ExprCacheAff
21615 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 inityChange(pPar
21616 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 se, r3, 1);.
21617 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21618 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21619 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 78 P_IdxInsert, pEx
2161a 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b pr->iTable, r2);
2161b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
2161c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
2161d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c }. sql
2161e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
2161f 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a eg(pParse, r1);.
21620 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
21621 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
21622 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 arse, r2);.
21623 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 }. if( !is
21624 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
21625 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
21626 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 76 geP4(v, addr, (v
21627 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 oid *)&keyInfo,
21628 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 P4_KEYINFO);.
21629 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
2162a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 ;. }.. cas
2162b 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 e TK_EXISTS:.
2162c 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a case TK_SELECT:
2162d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
2162e 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 /* If this
2162f 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63 61 has to be a sca
21630 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65 6e lar SELECT. Gen
21631 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70 75 erate code to pu
21632 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 t the. ** v
21633 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65 6c alue of this sel
21634 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 ect in a memory
21635 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 cell and record
21636 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 the number.
21637 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ** of the memor
21638 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d y cell in iColum
21639 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 61 n. If this is a
2163a 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 65 0a n EXISTS, write.
2163b 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 ** an inte
2163c 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73 74 ger 0 (not exist
2163d 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73 29 s) or 1 (exists)
2163e 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20 63 into a memory c
2163f 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 ell. ** and
21640 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65 6d record that mem
21641 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c ory cell in iCol
21642 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 umn.. */.
21643 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 static const
21644 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 22 Token one = { "
21645 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 6f 6b 1", 1 }; /* Tok
21646 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c 20 76 en for literal v
21647 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 20 20 alue 1 */.
21648 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 Select *pSel;
21649 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2164a 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 /* SELECT
2164b 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 6e 63 statement to enc
2164c 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c ode */. Sel
2164d 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 20 ectDest dest;
2164e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2164f 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 65 61 /* How to dea
21650 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 72 65 l with SELECt re
21651 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 20 74 sult */.. t
21652 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
21653 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b op==TK_EXISTS );
21654 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21655 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 pExpr->op==TK_S
21656 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 61 ELECT );. a
21657 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 ssert( pExpr->op
21658 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c 20 70 ==TK_EXISTS || p
21659 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c Expr->op==TK_SEL
2165a 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 61 73 ECT );.. as
2165b 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 6f sert( ExprHasPro
2165c 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
2165d 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 xIsSelect) );.
2165e 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72 pSel = pExpr
2165f 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 ->x.pSelect;.
21660 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 sqlite3Select
21661 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 DestInit(&dest,
21662 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 0, ++pParse->nMe
21663 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 m);. if( pE
21664 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 xpr->op==TK_SELE
21665 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 64 65 CT ){. de
21666 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 4d st.eDest = SRT_M
21667 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 em;. sqli
21668 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21669 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 OP_Null, 0, des
2166a 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 t.iParm);.
2166b 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 VdbeComment((v
2166c 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 72 79 , "Init subquery
2166d 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 result"));.
2166e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
2166f 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 dest.eDest = SR
21670 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 T_Exists;.
21671 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21672 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
21673 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d r, 0, dest.iParm
21674 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 );. VdbeC
21675 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 omment((v, "Init
21676 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 22 29 EXISTS result")
21677 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
21678 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 sqlite3ExprDele
21679 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 te(pParse->db, p
2167a 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 Sel->pLimit);.
2167b 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 pSel->pLimit
2167c 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 = sqlite3PExpr(
2167d 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 pParse, TK_INTEG
2167e 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b ER, 0, 0, &one);
2167f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
21680 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c e3Select(pParse,
21681 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 29 7b pSel, &dest) ){
21682 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
21683 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
21684 20 72 52 65 67 20 3d 20 64 65 73 74 2e 69 50 61 rReg = dest.iPa
21685 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 rm;. ExprSe
21686 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 tIrreducible(pEx
21687 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b pr);. break
21688 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
21689 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 f( testAddr ){.
2168a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
2168b 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 mpHere(v, testAd
2168c 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c dr-1);. }. sql
2168d 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 ite3ExprCachePop
2168e 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 (pParse, 1);..
2168f 72 65 74 75 72 6e 20 72 52 65 67 3b 0a 7d 0a 23 return rReg;.}.#
21690 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
21691 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f OMIT_SUBQUERY */
21692 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
21693 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f _OMIT_SUBQUERY./
21694 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
21695 64 65 20 66 6f 72 20 61 6e 20 49 4e 20 65 78 70 de for an IN exp
21696 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 ression..**.**
21697 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 x IN (SELECT
21698 20 2e 2e 2e 29 0a 2a 2a 20 20 20 20 20 20 78 20 ...).** x
21699 49 4e 20 28 76 61 6c 75 65 2c 20 76 61 6c 75 65 IN (value, value
2169a 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 , ...).**.** The
2169b 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 left-hand side
2169c 28 4c 48 53 29 20 69 73 20 61 20 73 63 61 6c 61 (LHS) is a scala
2169d 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 r expression. T
2169e 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 he right-hand si
2169f 64 65 20 28 52 48 53 29 0a 2a 2a 20 69 73 20 61 de (RHS).** is a
216a0 6e 20 61 72 72 61 79 20 6f 66 20 7a 65 72 6f 20 n array of zero
216a1 6f 72 20 6d 6f 72 65 20 76 61 6c 75 65 73 2e 20 or more values.
216a2 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
216a3 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 4c is true if the L
216a4 48 53 20 69 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e HS is.** contain
216a5 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 ed within the RH
216a6 53 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 S. The value of
216a7 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
216a8 69 73 20 75 6e 6b 6e 6f 77 6e 20 28 4e 55 4c 4c is unknown (NULL
216a9 29 0a 2a 2a 20 69 66 20 74 68 65 20 4c 48 53 20 ).** if the LHS
216aa 69 73 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 is NULL or if th
216ab 65 20 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e e LHS is not con
216ac 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 tained within th
216ad 65 20 52 48 53 20 61 6e 64 20 74 68 65 0a 2a 2a e RHS and the.**
216ae 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 6f 6e RHS contains on
216af 65 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 e or more NULL v
216b0 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 alues..**.** Thi
216b1 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 s routine genera
216b2 74 65 73 20 63 6f 64 65 20 77 69 6c 6c 20 6a 75 tes code will ju
216b3 6d 70 20 74 6f 20 64 65 73 74 49 66 46 61 6c 73 mp to destIfFals
216b4 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20 e if the LHS is
216b5 6e 6f 74 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 65 not .** containe
216b6 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 53 d within the RHS
216b7 2e 20 20 49 66 20 64 75 65 20 74 6f 20 4e 55 4c . If due to NUL
216b8 4c 73 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 74 Ls we cannot det
216b9 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 4c 48 ermine if the LH
216ba 53 0a 2a 2a 20 69 73 20 63 6f 6e 74 61 69 6e 65 S.** is containe
216bb 64 20 69 6e 20 74 68 65 20 52 48 53 20 74 68 65 d in the RHS the
216bc 6e 20 6a 75 6d 70 20 74 6f 20 64 65 73 74 49 66 n jump to destIf
216bd 4e 75 6c 6c 2e 20 20 49 66 20 74 68 65 20 4c 48 Null. If the LH
216be 53 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a S is contained.*
216bf 2a 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 53 * within the RHS
216c0 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 then fall throu
216c1 67 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f gh..*/.static vo
216c2 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f id sqlite3ExprCo
216c3 64 65 49 4e 28 0a 20 20 50 61 72 73 65 20 2a 70 deIN(. Parse *p
216c4 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Parse, /*
216c5 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
216c6 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
216c7 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a text */. Expr *
216c8 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 pExpr,
216c9 2f 2a 20 54 68 65 20 49 4e 20 65 78 70 72 65 73 /* The IN expres
216ca 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 sion */. int de
216cb 73 74 49 66 46 61 6c 73 65 2c 20 20 20 20 20 20 stIfFalse,
216cc 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20 /* Jump here if
216cd 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 LHS is not conta
216ce 69 6e 65 64 20 69 6e 20 74 68 65 20 52 48 53 20 ined in the RHS
216cf 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 49 66 4e */. int destIfN
216d0 75 6c 6c 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 ull /* Ju
216d1 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20 72 mp here if the r
216d2 65 73 75 6c 74 73 20 61 72 65 20 75 6e 6b 6e 6f esults are unkno
216d3 77 6e 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73 20 wn due to NULLs
216d4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 52 68 73 */.){. int rRhs
216d5 48 61 73 4e 75 6c 6c 20 3d 20 30 3b 20 20 2f 2a HasNull = 0; /*
216d6 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20 69 Register that i
216d7 73 20 74 72 75 65 20 69 66 20 52 48 53 20 63 6f s true if RHS co
216d8 6e 74 61 69 6e 73 20 4e 55 4c 4c 20 76 61 6c 75 ntains NULL valu
216d9 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 es */. char aff
216da 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 2f 2a inity; /*
216db 20 43 6f 6d 70 61 72 69 73 6f 6e 20 61 66 66 69 Comparison affi
216dc 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 nity to use */.
216dd 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20 20 int eType;
216de 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f /* Type o
216df 66 20 74 68 65 20 52 48 53 20 2a 2f 0a 20 20 69 f the RHS */. i
216e0 6e 74 20 72 31 3b 20 20 20 20 20 20 20 20 20 20 nt r1;
216e1 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 /* Temporar
216e2 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 2a y use register *
216e3 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 /. Vdbe *v;
216e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
216e5 74 65 6d 65 6e 74 20 75 6e 64 65 72 20 63 6f 6e tement under con
216e6 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 struction */..
216e7 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 52 /* Compute the R
216e8 48 53 2e 20 20 20 41 66 74 65 72 20 74 68 69 73 HS. After this
216e9 20 73 74 65 70 2c 20 74 68 65 20 74 61 62 6c 65 step, the table
216ea 20 77 69 74 68 20 63 75 72 73 6f 72 0a 20 20 2a with cursor. *
216eb 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 * pExpr->iTable
216ec 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 will contains th
216ed 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61 e values that ma
216ee 6b 65 20 75 70 20 74 68 65 20 52 48 53 2e 0a 20 ke up the RHS..
216ef 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73 65 */. v = pParse
216f0 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72 ->pVdbe;. asser
216f1 74 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20 20 t( v!=0 );
216f2 20 2f 2a 20 4f 4f 4d 20 64 65 74 65 63 74 65 64 /* OOM detected
216f3 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 72 prior to this r
216f4 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 56 64 62 65 outine */. Vdbe
216f5 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 NoopComment((v,
216f6 22 62 65 67 69 6e 20 49 4e 20 65 78 70 72 22 29 "begin IN expr")
216f7 29 3b 0a 20 20 65 54 79 70 65 20 3d 20 73 71 6c );. eType = sql
216f8 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 ite3FindInIndex(
216f9 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 pParse, pExpr, &
216fa 72 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 0a 20 rRhsHasNull);..
216fb 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 /* Figure out t
216fc 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75 he affinity to u
216fd 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b se to create a k
216fe 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 ey from the resu
216ff 6c 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 lts. ** of the
21700 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 expression. affi
21701 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20 61 nityStr stores a
21702 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 73 static string s
21703 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 2a 2a uitable for. **
21704 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 P4 of OP_MakeRe
21705 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 61 66 66 cord.. */. aff
21706 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 inity = comparis
21707 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 onAffinity(pExpr
21708 29 3b 0a 0a 20 20 2f 2a 20 43 6f 64 65 20 74 68 );.. /* Code th
21709 65 20 4c 48 53 2c 20 74 68 65 20 3c 65 78 70 72 e LHS, the <expr
2170a 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 > from "<expr> I
2170b 4e 20 28 2e 2e 2e 29 22 2e 0a 20 20 2a 2f 0a 20 N (...)".. */.
2170c 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
2170d 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 ePush(pParse);.
2170e 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r1 = sqlite3Get
2170f 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
21710 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f . sqlite3ExprCo
21711 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 de(pParse, pExpr
21712 2d 3e 70 4c 65 66 74 2c 20 72 31 29 3b 0a 20 20 ->pLeft, r1);.
21713 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21714 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 2(v, OP_IsNull,
21715 72 31 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b r1, destIfNull);
21716 0a 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d ... if( eType==
21717 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 IN_INDEX_ROWID )
21718 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 {. /* In this
21719 20 63 61 73 65 2c 20 74 68 65 20 52 48 53 20 69 case, the RHS i
2171a 73 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20 74 s the ROWID of t
2171b 61 62 6c 65 20 62 2d 74 72 65 65 0a 20 20 20 20 able b-tree.
2171c 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
2171d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d beAddOp2(v, OP_M
2171e 75 73 74 42 65 49 6e 74 2c 20 72 31 2c 20 64 65 ustBeInt, r1, de
2171f 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 stIfFalse);.
21720 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21721 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 3(v, OP_NotExist
21722 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 s, pExpr->iTable
21723 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 72 , destIfFalse, r
21724 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1);. }else{.
21725 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
21726 2c 20 74 68 65 20 52 48 53 20 69 73 20 61 6e 20 , the RHS is an
21727 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 0a 20 20 index b-tree..
21728 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
21729 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
2172a 5f 41 66 66 69 6e 69 74 79 2c 20 72 31 2c 20 31 _Affinity, r1, 1
2172b 2c 20 30 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 , 0, &affinity,
2172c 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 1);.. /* If t
2172d 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69 he set membershi
2172e 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74 68 p test fails, th
2172f 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 en the result of
21730 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 78 20 the . ** "x
21731 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 IN (...)" expres
21732 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74 sion must be eit
21733 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 her 0 or NULL. I
21734 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 2a 2a f the set. **
21735 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c contains no NUL
21736 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 L values, then t
21737 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 2e 20 he result is 0.
21738 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 20 20 If the set .
21739 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 ** contains one
2173a 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c or more NULL val
2173b 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 ues, then the re
2173c 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 sult of the.
2173d 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 ** expression is
2173e 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 also NULL..
2173f 2a 2f 0a 20 20 20 20 69 66 28 20 72 52 68 73 48 */. if( rRhsH
21740 61 73 4e 75 6c 6c 3d 3d 30 20 7c 7c 20 64 65 73 asNull==0 || des
21741 74 49 66 46 61 6c 73 65 3d 3d 64 65 73 74 49 66 tIfFalse==destIf
21742 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a Null ){. /*
21743 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e This branch run
21744 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e s if it is known
21745 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
21746 20 74 68 61 74 20 74 68 65 20 52 48 53 0a 20 20 that the RHS.
21747 20 20 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f ** cannot co
21748 6e 74 61 69 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 ntain NULL value
21749 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 s. This happens
2174a 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 as the result.
2174b 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 ** of a "NOT
2174c 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e NULL" constrain
2174d 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 t in the databas
2174e 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 20 20 e schema..
2174f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f **. ** Also
21750 20 72 75 6e 20 74 68 69 73 20 62 72 61 6e 63 68 run this branch
21751 20 69 66 20 4e 55 4c 4c 20 69 73 20 65 71 75 69 if NULL is equi
21752 76 61 6c 65 6e 74 20 74 6f 20 46 41 4c 53 45 0a valent to FALSE.
21753 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 69 ** for thi
21754 73 20 70 61 72 74 69 63 75 6c 61 72 20 49 4e 20 s particular IN
21755 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 20 20 operator..
21756 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
21757 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c VdbeAddOp4Int(v,
21758 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 70 45 OP_NotFound, pE
21759 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 64 65 73 xpr->iTable, des
2175a 74 49 66 46 61 6c 73 65 2c 20 72 31 2c 20 31 29 tIfFalse, r1, 1)
2175b 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 ;.. }else{.
2175c 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 62 /* In this b
2175d 72 61 6e 63 68 2c 20 74 68 65 20 52 48 53 20 6f ranch, the RHS o
2175e 66 20 74 68 65 20 49 4e 20 6d 69 67 68 74 20 63 f the IN might c
2175f 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 61 6e ontain a NULL an
21760 64 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 d. ** the p
21761 72 65 73 65 6e 63 65 20 6f 66 20 61 20 4e 55 4c resence of a NUL
21762 4c 20 6f 6e 20 74 68 65 20 52 48 53 20 6d 61 6b L on the RHS mak
21763 65 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 20 es a difference
21764 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 in the. **
21765 6f 75 74 63 6f 6d 65 2e 0a 20 20 20 20 20 20 2a outcome.. *
21766 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 2c 20 /. int j1,
21767 6a 32 2c 20 6a 33 3b 0a 0a 20 20 20 20 20 20 2f j2, j3;.. /
21768 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f * First check to
21769 20 73 65 65 20 69 66 20 74 68 65 20 4c 48 53 20 see if the LHS
2176a 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 is contained in
2176b 74 68 65 20 52 48 53 2e 20 20 49 66 20 73 6f 2c the RHS. If so,
2176c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 . ** then t
2176d 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 4e he presence of N
2176e 55 4c 4c 73 20 69 6e 20 74 68 65 20 52 48 53 20 ULLs in the RHS
2176f 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2c does not matter,
21770 20 73 6f 20 6a 75 6d 70 0a 20 20 20 20 20 20 2a so jump. *
21771 2a 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 * over all of th
21772 65 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c e code that foll
21773 6f 77 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ows.. */.
21774 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 j1 = sqlite3
21775 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c VdbeAddOp4Int(v,
21776 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 OP_Found, pExpr
21777 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 31 2c ->iTable, 0, r1,
21778 20 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 48 1);.. /* H
21779 65 72 65 20 77 65 20 62 65 67 69 6e 20 67 65 6e ere we begin gen
2177a 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 68 61 erating code tha
2177b 74 20 72 75 6e 73 20 69 66 20 74 68 65 20 4c 48 t runs if the LH
2177c 53 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a S is not. *
2177d 2a 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 * contained with
2177e 69 6e 20 74 68 65 20 52 48 53 2e 20 20 47 65 6e in the RHS. Gen
2177f 65 72 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c erate additional
21780 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20 20 code that.
21781 20 2a 2a 20 74 65 73 74 73 20 74 68 65 20 52 48 ** tests the RH
21782 53 20 66 6f 72 20 4e 55 4c 4c 73 2e 20 20 49 66 S for NULLs. If
21783 20 74 68 65 20 52 48 53 20 63 6f 6e 74 61 69 6e the RHS contain
21784 73 20 61 20 4e 55 4c 4c 20 74 68 65 6e 0a 20 20 s a NULL then.
21785 20 20 20 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 64 ** jump to d
21786 65 73 74 49 66 4e 75 6c 6c 2e 20 20 49 66 20 74 estIfNull. If t
21787 68 65 72 65 20 61 72 65 20 6e 6f 20 4e 55 4c 4c here are no NULL
21788 73 20 69 6e 20 74 68 65 20 52 48 53 20 74 68 65 s in the RHS the
21789 6e 0a 20 20 20 20 20 20 2a 2a 20 6a 75 6d 70 20 n. ** jump
2178a 74 6f 20 64 65 73 74 49 66 46 61 6c 73 65 2e 0a to destIfFalse..
2178b 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a */. j
2178c 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 2 = sqlite3VdbeA
2178d 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e ddOp1(v, OP_NotN
2178e 75 6c 6c 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c ull, rRhsHasNull
2178f 29 3b 0a 20 20 20 20 20 20 6a 33 20 3d 20 73 71 );. j3 = sq
21790 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 lite3VdbeAddOp4I
21791 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 nt(v, OP_Found,
21792 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 pExpr->iTable, 0
21793 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31 , rRhsHasNull, 1
21794 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21795 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21796 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 52 _Integer, -1, rR
21797 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 20 20 20 20 hsHasNull);.
21798 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21799 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 pHere(v, j3);.
2179a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2179b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 ddOp2(v, OP_AddI
2179c 6d 6d 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c mm, rRhsHasNull,
2179d 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 1);. sqlit
2179e 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
2179f 2c 20 6a 32 29 3b 0a 0a 20 20 20 20 20 20 2f 2a , j2);.. /*
217a0 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 61 70 70 Jump to the app
217a1 72 6f 70 72 69 61 74 65 20 74 61 72 67 65 74 20 ropriate target
217a2 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 depending on whe
217a3 74 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20 ther or not.
217a4 20 20 2a 2a 20 74 68 65 20 52 48 53 20 63 6f 6e ** the RHS con
217a5 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 20 20 20 tains a NULL.
217a6 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 */. sqli
217a7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
217a8 20 4f 50 5f 49 66 2c 20 72 52 68 73 48 61 73 4e OP_If, rRhsHasN
217a9 75 6c 6c 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 ull, destIfNull)
217aa 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
217ab 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
217ac 47 6f 74 6f 2c 20 30 2c 20 64 65 73 74 49 66 46 Goto, 0, destIfF
217ad 61 6c 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a alse);.. /*
217ae 20 54 68 65 20 4f 50 5f 46 6f 75 6e 64 20 61 74 The OP_Found at
217af 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69 73 the top of this
217b0 20 62 72 61 6e 63 68 20 6a 75 6d 70 73 20 68 65 branch jumps he
217b1 72 65 20 77 68 65 6e 20 74 72 75 65 2c 20 0a 20 re when true, .
217b2 20 20 20 20 20 2a 2a 20 63 61 75 73 69 6e 67 20 ** causing
217b3 74 68 65 20 6f 76 65 72 61 6c 6c 20 49 4e 20 65 the overall IN e
217b4 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 xpression evalua
217b5 74 69 6f 6e 20 74 6f 20 66 61 6c 6c 20 74 68 72 tion to fall thr
217b6 6f 75 67 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ough.. */.
217b7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
217b8 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b JumpHere(v, j1);
217b9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
217ba 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
217bb 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a eg(pParse, r1);.
217bc 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
217bd 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
217be 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 ;. VdbeComment(
217bf 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 (v, "end IN expr
217c0 22 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a "));.}.#endif /*
217c1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 SQLITE_OMIT_SUB
217c2 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 QUERY */../*.**
217c3 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 Duplicate an 8-b
217c4 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 yte value.*/.sta
217c5 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79 tic char *dup8by
217c6 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e tes(Vdbe *v, con
217c7 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 st char *in){.
217c8 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 char *out = sqli
217c9 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 te3DbMallocRaw(s
217ca 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c qlite3VdbeDb(v),
217cb 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 8);. if( out )
217cc 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 {. memcpy(out
217cd 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 , in, 8);. }.
217ce 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f return out;.}../
217cf 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
217d0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
217d1 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66 t will put the f
217d2 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a loating point.**
217d3 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64 value described
217d4 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e by z[0..n-1] in
217d5 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d to register iMem
217d6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 ..**.** The z[]
217d7 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 string will prob
217d8 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f ably not be zero
217d9 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 -terminated. Bu
217da 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 t the .** z[n] c
217db 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 haracter is guar
217dc 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d anteed to be som
217dd 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 ething that does
217de 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b not look.** lik
217df 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 e the continuati
217e0 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 on of the number
217e1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
217e2 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a codeReal(Vdbe *
217e3 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a v, const char *z
217e4 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 , int negateFlag
217e5 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 , int iMem){. i
217e6 66 28 20 41 4c 57 41 59 53 28 7a 21 3d 30 29 20 f( ALWAYS(z!=0)
217e7 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 ){. double va
217e8 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a lue;. char *z
217e9 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 V;. sqlite3At
217ea 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 oF(z, &value);.
217eb 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 assert( !sqli
217ec 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 te3IsNaN(value)
217ed 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 ); /* The new At
217ee 6f 46 20 6e 65 76 65 72 20 72 65 74 75 72 6e 73 oF never returns
217ef 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 NaN */. if(
217f0 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c negateFlag ) val
217f1 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 ue = -value;.
217f2 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 zV = dup8bytes(
217f3 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 v, (char*)&value
217f4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
217f5 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 beAddOp4(v, OP_R
217f6 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c eal, 0, iMem, 0,
217f7 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 zV, P4_REAL);.
217f8 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}.../*.** Gen
217f9 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 erate an instruc
217fa 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 tion that will p
217fb 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 64 ut the integer d
217fc 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 escribe by.** te
217fd 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 xt z[0..n-1] int
217fe 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e o register iMem.
217ff 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 .**.** The z[] s
21800 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 tring will proba
21801 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d bly not be zero-
21802 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 terminated. But
21803 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 the .** z[n] ch
21804 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 61 aracter is guara
21805 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 nteed to be some
21806 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 thing that does
21807 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 not look.** like
21808 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f the continuatio
21809 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e n of the number.
2180a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
2180b 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62 65 codeInteger(Vdbe
2180c 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 70 72 *v, Expr *pExpr
2180d 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69 , int negFlag, i
2180e 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 nt iMem){. if(
2180f 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 pExpr->flags & E
21810 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 P_IntValue ){.
21811 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72 2d int i = pExpr-
21812 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20 69 >u.iValue;. i
21813 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d f( negFlag ) i =
21814 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 -i;. sqlite3
21815 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21816 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 _Integer, i, iMe
21817 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 m);. }else{.
21818 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d const char *z =
21819 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
2181a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 21 ;. assert( z!
2181b 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 =0 );. if( sq
2181c 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 lite3FitsIn64Bit
2181d 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b s(z, negFlag) ){
2181e 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 . i64 value
2181f 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 ;. char *zV
21820 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 ;. sqlite3A
21821 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 toi64(z, &value)
21822 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 ;. if( negF
21823 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 lag ) value = -v
21824 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d alue;. zV =
21825 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 dup8bytes(v, (c
21826 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 har*)&value);.
21827 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21828 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 ddOp4(v, OP_Int6
21829 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 4, 0, iMem, 0, z
2182a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 V, P4_INT64);.
2182b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 }else{. c
2182c 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 65 odeReal(v, z, ne
2182d 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 gFlag, iMem);.
2182e 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
2182f 20 43 6c 65 61 72 20 61 20 63 61 63 68 65 20 65 Clear a cache e
21830 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ntry..*/.static
21831 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72 79 43 void cacheEntryC
21832 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 lear(Parse *pPar
21833 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 se, struct yColC
21834 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 ache *p){. if(
21835 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 p->tempReg ){.
21836 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 if( pParse->nT
21837 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 empReg<ArraySize
21838 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 (pParse->aTempRe
21839 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 g) ){. pPar
2183a 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 se->aTempReg[pPa
2183b 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d rse->nTempReg++]
2183c 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 = p->iReg;.
2183d 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 }. p->tempReg
2183e 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a = 0;. }.}.../*
2183f 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 .** Record in th
21840 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 e column cache t
21841 68 61 74 20 61 20 70 61 72 74 69 63 75 6c 61 72 hat a particular
21842 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a column from a.*
21843 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 * particular tab
21844 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 le is stored in
21845 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 a particular reg
21846 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ister..*/.SQLITE
21847 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
21848 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 lite3ExprCacheSt
21849 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ore(Parse *pPars
2184a 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 e, int iTab, int
2184b 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 iCol, int iReg)
2184c 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
2184d 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 minLru;. int i
2184e 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 20 dxLru;. struct
2184f 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 yColCache *p;..
21850 20 61 73 73 65 72 74 28 20 69 52 65 67 3e 30 20 assert( iReg>0
21851 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 ); /* Register
21852 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 77 61 numbers are alwa
21853 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 ys positive */.
21854 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 2d assert( iCol>=-
21855 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36 38 20 1 && iCol<32768
21856 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20 63 6f ); /* Finite co
21857 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a 2f 0a lumn numbers */.
21858 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 65 70 6c . /* First repl
21859 61 63 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67 ace any existing
2185a 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 entry */. for(
2185b 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
2185c 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
2185d 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
2185e 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
2185f 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e ( p->iReg && p->
21860 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 iTable==iTab &&
21861 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c p->iColumn==iCol
21862 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 ){. cacheE
21863 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 ntryClear(pParse
21864 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 , p);. p->i
21865 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e Level = pParse->
21866 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 iCacheLevel;.
21867 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 p->iReg = iRe
21868 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43 g;. p->affC
21869 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 hange = 0;.
2186a 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 p->lru = pParse
2186b 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 ->iCacheCnt++;.
2186c 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
2186d 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e }. }.. /* Fin
2186e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 d an empty slot
2186f 61 6e 64 20 72 65 70 6c 61 63 65 20 69 74 20 2a and replace it *
21870 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 /. for(i=0, p=p
21871 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
21872 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c ; i<SQLITE_N_COL
21873 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 CACHE; i++, p++)
21874 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 {. if( p->iRe
21875 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d g==0 ){. p-
21876 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 >iLevel = pParse
21877 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 ->iCacheLevel;.
21878 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d p->iTable =
21879 20 69 54 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e iTab;. p->
2187a 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a iColumn = iCol;.
2187b 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 p->iReg =
2187c 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 iReg;. p->a
2187d 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 ffChange = 0;.
2187e 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d p->tempReg =
2187f 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 0;. p->lru
21880 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 = pParse->iCach
21881 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 eCnt++;. re
21882 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a turn;. }. }.
21883 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 . /* Replace th
21884 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c 79 20 e last recently
21885 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 used */. minLru
21886 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 = 0x7fffffff;.
21887 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 idxLru = -1;.
21888 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
21889 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
2188a 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
2188b 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
2188c 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e if( p->lru<min
2188d 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69 64 78 Lru ){. idx
2188e 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d Lru = i;. m
2188f 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a inLru = p->lru;.
21890 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
21891 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e 3d 30 ALWAYS(idxLru>=0
21892 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 ) ){. p = &pP
21893 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b arse->aColCache[
21894 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e idxLru];. p->
21895 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d iLevel = pParse-
21896 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 >iCacheLevel;.
21897 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 p->iTable = iT
21898 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 ab;. p->iColu
21899 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 mn = iCol;. p
2189a 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 ->iReg = iReg;.
2189b 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 p->affChange
2189c 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 = 0;. p->temp
2189d 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e Reg = 0;. p->
2189e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 lru = pParse->iC
2189f 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 acheCnt++;. r
218a0 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a eturn;. }.}../*
218a1 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 .** Indicate tha
218a2 74 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 t a register is
218a3 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 being overwritte
218a4 6e 2e 20 20 50 75 72 67 65 20 74 68 65 20 72 65 n. Purge the re
218a5 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 gister.** from t
218a6 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e he column cache.
218a7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
218a8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
218a9 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 xprCacheRemove(P
218aa 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
218ab 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t iReg){. int i
218ac 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
218ad 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 ache *p;. for(i
218ae 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
218af 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
218b0 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
218b1 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 +, p++){. if(
218b2 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 p->iReg==iReg )
218b3 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 {. cacheEnt
218b4 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 ryClear(pParse,
218b5 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 p);. p->iRe
218b6 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d g = 0;. }. }
218b7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 .}../*.** Rememb
218b8 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 er the current c
218b9 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 olumn cache cont
218ba 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e ext. Any new en
218bb 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 tries added.** a
218bc 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 dded to the colu
218bd 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72 20 74 mn cache after t
218be 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72 65 6d his call are rem
218bf 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a oved when the.**
218c0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 corresponding p
218c1 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 op occurs..*/.SQ
218c2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
218c3 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
218c4 68 65 50 75 73 68 28 50 61 72 73 65 20 2a 70 50 hePush(Parse *pP
218c5 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73 65 2d arse){. pParse-
218c6 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a >iCacheLevel++;.
218c7 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 }../*.** Remove
218c8 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 from the column
218c9 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72 69 65 cache any entrie
218ca 73 20 74 68 61 74 20 77 65 72 65 20 61 64 64 65 s that were adde
218cb 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 d since the.** t
218cc 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20 50 75 he previous N Pu
218cd 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 sh operations.
218ce 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 In other words,
218cf 72 65 73 74 6f 72 65 20 74 68 65 20 63 61 63 68 restore the cach
218d0 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 74 e.** to the stat
218d1 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20 50 75 e it was in N Pu
218d2 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c shes ago..*/.SQL
218d3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
218d4 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
218d5 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50 61 72 ePop(Parse *pPar
218d6 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e se, int N){. in
218d7 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 t i;. struct yC
218d8 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 olCache *p;. as
218d9 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 sert( N>0 );. a
218da 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69 ssert( pParse->i
218db 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b CacheLevel>=N );
218dc 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 . pParse->iCach
218dd 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 eLevel -= N;. f
218de 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 or(i=0, p=pParse
218df 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 ->aColCache; i<S
218e0 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 QLITE_N_COLCACHE
218e1 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 ; i++, p++){.
218e2 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 if( p->iReg &&
218e3 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 p->iLevel>pParse
218e4 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 29 7b ->iCacheLevel ){
218e5 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 . cacheEntr
218e6 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 yClear(pParse, p
218e7 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 );. p->iReg
218e8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
218e9 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 }../*.** When a
218ea 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 cached column is
218eb 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 reused, make su
218ec 72 65 20 74 68 61 74 20 69 74 73 20 72 65 67 69 re that its regi
218ed 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f ster is.** no lo
218ee 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65 20 61 nger available a
218ef 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 s a temp registe
218f0 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38 37 39 r. ticket #3879
218f1 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 : that same.**
218f2 72 65 67 69 73 74 65 72 20 6d 69 67 68 74 20 62 register might b
218f3 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 69 e in the cache i
218f4 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 n multiple place
218f5 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20 74 6f s, so be sure to
218f6 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61 6c 6c .** get them all
218f7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
218f8 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
218f9 65 50 69 6e 52 65 67 69 73 74 65 72 28 50 61 72 ePinRegister(Par
218fa 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
218fb 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a iReg){. int i;.
218fc 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 struct yColCac
218fd 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 he *p;. for(i=0
218fe 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c , p=pParse->aCol
218ff 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f Cache; i<SQLITE_
21900 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c N_COLCACHE; i++,
21901 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 p++){. if( p
21902 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a ->iReg==iReg ){.
21903 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 p->tempReg
21904 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
21905 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
21906 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c e code that will
21907 20 65 78 74 72 61 63 74 20 74 68 65 20 69 43 6f extract the iCo
21908 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 lumn-th column f
21909 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 rom.** table pTa
2190a 62 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 b and store the
2190b 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 column value in
2190c 61 20 72 65 67 69 73 74 65 72 2e 20 20 41 6e 20 a register. An
2190d 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 effort.** is mad
2190e 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 e to store the c
2190f 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 olumn value in r
21910 65 67 69 73 74 65 72 20 69 52 65 67 2c 20 62 75 egister iReg, bu
21911 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 t this is.** not
21912 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20 54 68 guaranteed. Th
21913 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 e location of th
21914 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 e column value i
21915 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
21916 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 * There must be
21917 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 an open cursor t
21918 6f 20 70 54 61 62 20 69 6e 20 69 54 61 62 6c 65 o pTab in iTable
21919 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
2191a 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e ne.** is called.
2191b 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 If iColumn<0 t
2191c 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65 6e 65 hen code is gene
2191d 72 61 74 65 64 20 74 68 61 74 20 65 78 74 72 61 rated that extra
2191e 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a cts the rowid..*
2191f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
21920 65 20 6d 69 67 68 74 20 61 74 74 65 6d 70 74 20 e might attempt
21921 74 6f 20 72 65 75 73 65 20 74 68 65 20 76 61 6c to reuse the val
21922 75 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ue of the column
21923 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 that.** has alr
21924 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 eady been loaded
21925 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 into a register
21926 2e 20 20 54 68 65 20 76 61 6c 75 65 20 77 69 6c . The value wil
21927 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 l always.** be u
21928 73 65 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f sed if it has no
21929 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 t undergone any
2192a 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 affinity changes
2192b 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 . But if.** an
2192c 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 affinity change
2192d 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68 has occurred, th
2192e 65 6e 20 74 68 65 20 63 61 63 68 65 64 20 76 61 en the cached va
2192f 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 lue will only be
21930 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 6c 6c 6f .** used if allo
21931 77 41 66 66 43 68 6e 67 20 69 73 20 74 72 75 65 wAffChng is true
21932 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21933 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
21934 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e xprCodeGetColumn
21935 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
21936 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 e, /* Parsing
21937 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 and code generat
21938 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
21939 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 Table *pTab,
2193a 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69 6f 6e /* Description
2193b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 of the table we
2193c 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72 6f are reading fro
2193d 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 m */. int iColu
2193e 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 mn, /* Index
2193f 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f of the table co
21940 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 lumn */. int iT
21941 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 able, /* Th
21942 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e e cursor pointin
21943 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a g to the table *
21944 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 20 20 /. int iReg,
21945 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 /* Store re
21946 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 sults here */.
21947 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 int allowAffChng
21948 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 69 6f /* True if prio
21949 72 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 r affinity chang
2194a 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a es are OK */.){.
2194b 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
2194c 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 se->pVdbe;. int
2194d 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f i;. struct yCo
2194e 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f lCache *p;.. fo
2194f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d r(i=0, p=pParse-
21950 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 >aColCache; i<SQ
21951 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b LITE_N_COLCACHE;
21952 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 i++, p++){.
21953 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 if( p->iReg>0 &&
21954 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 p->iTable==iTab
21955 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e le && p->iColumn
21956 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 ==iColumn.
21957 20 20 20 20 20 26 26 20 28 21 70 2d 3e 61 66 66 && (!p->aff
21958 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 Change || allowA
21959 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 20 20 20 ffChng) ){.
2195a 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 p->lru = pParse
2195b 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 ->iCacheCnt++;.
2195c 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
2195d 43 61 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 CachePinRegister
2195e 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 52 65 67 (pParse, p->iReg
2195f 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
21960 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 p->iReg;. }.
21961 20 7d 20 20 0a 20 20 61 73 73 65 72 74 28 20 76 } . assert( v
21962 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f !=0 );. if( iCo
21963 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 71 lumn<0 ){. sq
21964 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21965 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 v, OP_Rowid, iTa
21966 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 ble, iReg);. }e
21967 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 lse if( ALWAYS(p
21968 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20 69 Tab!=0) ){. i
21969 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 74 75 61 nt op = IsVirtua
2196a 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f l(pTab) ? OP_VCo
2196b 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e lumn : OP_Column
2196c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
2196d 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 69 eAddOp3(v, op, i
2196e 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 Table, iColumn,
2196f 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 iReg);. sqlit
21970 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 e3ColumnDefault(
21971 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e v, pTab, iColumn
21972 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a 20 20 73 , iReg);. }. s
21973 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 qlite3ExprCacheS
21974 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 54 61 tore(pParse, iTa
21975 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 ble, iColumn, iR
21976 65 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 eg);. return iR
21977 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 eg;.}../*.** Cle
21978 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 ar all column ca
21979 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a che entries..*/.
2197a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
2197b 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
2197c 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 20 acheClear(Parse
2197d 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 *pParse){. int
2197e 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
2197f 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 Cache *p;.. for
21980 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
21981 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
21982 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
21983 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
21984 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 f( p->iReg ){.
21985 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c cacheEntryCl
21986 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a ear(pParse, p);.
21987 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 p->iReg =
21988 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
21989 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 /*.** Record the
2198a 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 fact that an af
2198b 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 finity change ha
2198c 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 s occurred on iC
2198d 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 ount.** register
2198e 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 s starting with
2198f 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 iStart..*/.SQLIT
21990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21991 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
21992 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 ffinityChange(Pa
21993 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
21994 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f iStart, int iCo
21995 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 unt){. int iEnd
21996 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 = iStart + iCou
21997 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b nt - 1;. int i;
21998 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 . struct yColCa
21999 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d che *p;. for(i=
2199a 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 0, p=pParse->aCo
2199b 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 lCache; i<SQLITE
2199c 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b _N_COLCACHE; i++
2199d 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 , p++){. int
2199e 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 r = p->iReg;.
2199f 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26 if( r>=iStart &
219a0 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 & r<=iEnd ){.
219a1 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 p->affChange
219a2 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d = 1;. }. }.}
219a3 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
219a4 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f code to move co
219a5 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 ntent from regis
219a6 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 ters iFrom...iFr
219a7 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 om+nReg-1.** ove
219a8 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 r to iTo..iTo+nR
219a9 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 63 eg-1. Keep the c
219aa 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 olumn cache up-t
219ab 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 o-date..*/.SQLIT
219ac 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
219ad 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f qlite3ExprCodeMo
219ae 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ve(Parse *pParse
219af 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 , int iFrom, int
219b0 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b iTo, int nReg){
219b1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
219b2 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b ct yColCache *p;
219b3 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 46 72 . if( NEVER(iFr
219b4 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 75 72 om==iTo) ) retur
219b5 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 n;. sqlite3Vdbe
219b6 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 AddOp3(pParse->p
219b7 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 Vdbe, OP_Move, i
219b8 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 29 From, iTo, nReg)
219b9 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 ;. for(i=0, p=p
219ba 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
219bb 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c ; i<SQLITE_N_COL
219bc 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 CACHE; i++, p++)
219bd 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 2d {. int x = p-
219be 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 78 >iReg;. if( x
219bf 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 46 72 >=iFrom && x<iFr
219c0 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 20 om+nReg ){.
219c1 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 54 6f 2d p->iReg += iTo-
219c2 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d iFrom;. }. }
219c3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
219c4 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 te code to copy
219c5 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 content from reg
219c6 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 isters iFrom...i
219c7 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f From+nReg-1.** o
219c8 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b ver to iTo..iTo+
219c9 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 nReg-1..*/.SQLIT
219ca 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
219cb 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f qlite3ExprCodeCo
219cc 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 py(Parse *pParse
219cd 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 , int iFrom, int
219ce 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b iTo, int nReg){
219cf 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
219d0 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f NEVER(iFrom==iTo
219d1 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f ) ) return;. fo
219d2 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 r(i=0; i<nReg; i
219d3 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
219d4 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 VdbeAddOp2(pPars
219d5 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 e->pVdbe, OP_Cop
219d6 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b y, iFrom+i, iTo+
219d7 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a i);. }.}../*.**
219d8 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
219d9 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e 20 any register in
219da 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e the range iFrom.
219db 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 .iTo (inclusive)
219dc 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20 70 .** is used as p
219dd 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d art of the colum
219de 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 n cache..*/.stat
219df 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f 6c ic int usedAsCol
219e0 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a umnCache(Parse *
219e1 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f pParse, int iFro
219e2 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 m, int iTo){. i
219e3 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 nt i;. struct y
219e4 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 ColCache *p;. f
219e5 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 or(i=0, p=pParse
219e6 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 ->aColCache; i<S
219e7 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 QLITE_N_COLCACHE
219e8 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 ; i++, p++){.
219e9 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 int r = p->iReg
219ea 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 ;. if( r>=iFr
219eb 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 om && r<=iTo ) r
219ec 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 eturn 1;. }. r
219ed 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
219ee 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69 6e * If the last in
219ef 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64 20 struction coded
219f0 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 is an ephemeral
219f1 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a 2a copy of any of.*
219f2 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73 20 * the registers
219f3 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67 69 in the nReg regi
219f4 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 sters beginning
219f5 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e 0a with iReg, then.
219f6 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20 6c ** convert the l
219f7 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ast instruction
219f8 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74 6f from OP_SCopy to
219f9 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c OP_Copy..*/.SQL
219fa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
219fb 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 sqlite3ExprHard
219fc 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 Copy(Parse *pPar
219fd 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e se, int iReg, in
219fe 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 62 65 4f t nReg){. VdbeO
219ff 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a p *pOp;. Vdbe *
21a00 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 v;.. assert( pP
21a01 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
21a02 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 76 Failed==0 );. v
21a03 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
21a04 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 ;. assert( v!=0
21a05 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 );. pOp = sqli
21a06 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 te3VdbeGetOp(v,
21a07 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 -1);. assert( p
21a08 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 Op!=0 );. if( p
21a09 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 Op->opcode==OP_S
21a0a 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e Copy && pOp->p1>
21a0b 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 =iReg && pOp->p1
21a0c 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 <iReg+nReg ){.
21a0d 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 pOp->opcode =
21a0e 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a OP_Copy;. }.}..
21a0f 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
21a10 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ode to store the
21a11 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 41 value of the iA
21a12 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 6e lias-th alias in
21a13 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 register.** tar
21a14 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 74 20 get. The first
21a15 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c time this is cal
21a16 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 65 76 led, pExpr is ev
21a17 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 aluated to compu
21a18 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 te.** the value
21a19 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 20 54 of the alias. T
21a1a 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 he value is stor
21a1b 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 ed in an auxilia
21a1c 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 ry register.** a
21a1d 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 nd the number of
21a1e 20 74 68 61 74 20 72 65 67 69 73 74 65 72 20 69 that register i
21a1f 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 s returned. On
21a20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
21a21 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 ,.** the registe
21a22 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 74 75 r number is retu
21a23 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 65 6e rned without gen
21a24 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 erating any code
21a25 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
21a26 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 t in order for t
21a27 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 his to work, cod
21a28 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 72 61 e must be genera
21a29 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 ted in the.** sa
21a2a 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 69 74 me order that it
21a2b 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a is executed..**
21a2c 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 65 20 .** Aliases are
21a2d 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e numbered startin
21a2e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 69 41 g with 1. So iA
21a2f 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 20 72 lias is in the r
21a30 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 ange.** of 1 to
21a31 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 pParse->nAlias i
21a32 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a nclusive. .**.*
21a33 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 * pParse->aAlias
21a34 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 [iAlias-1] recor
21a35 64 73 20 74 68 65 20 72 65 67 69 73 74 65 72 20 ds the register
21a36 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68 65 number where the
21a37 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 value.** of the
21a38 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 iAlias-th alias
21a39 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 66 20 is stored. If
21a3a 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 zero, that means
21a3b 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 that the.** ali
21a3c 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 as has not yet b
21a3d 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f een computed..*/
21a3e 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 .static int code
21a3f 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 50 61 Alias(Parse *pPa
21a40 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c rse, int iAlias,
21a41 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e Expr *pExpr, in
21a42 74 20 74 61 72 67 65 74 29 7b 0a 23 69 66 20 30 t target){.#if 0
21a43 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
21a44 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
21a45 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 28 20 70 nt iReg;. if( p
21a46 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c Parse->nAliasAll
21a47 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 oc<pParse->nAlia
21a48 73 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d s ){. pParse-
21a49 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 >aAlias = sqlite
21a4a 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
21a4b 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c (db, pParse->aAl
21a4c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ias,.
21a4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21a4e 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 50 61 sizeof(pPa
21a4f 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 2a rse->aAlias[0])*
21a50 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 pParse->nAlias )
21a51 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
21a52 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21a53 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 && pParse->nAli
21a54 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 asAlloc>0 );.
21a55 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( db->mallocF
21a56 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 ailed ) return 0
21a57 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 50 ;. memset(&pP
21a58 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 70 50 61 arse->aAlias[pPa
21a59 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 rse->nAliasAlloc
21a5a 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 ], 0,.
21a5b 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 (pParse->nAlias
21a5c 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 -pParse->nAliasA
21a5d 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 70 50 61 lloc)*sizeof(pPa
21a5e 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 29 rse->aAlias[0]))
21a5f 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 41 ;. pParse->nA
21a60 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 50 61 72 liasAlloc = pPar
21a61 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a se->nAlias;. }.
21a62 20 20 61 73 73 65 72 74 28 20 69 41 6c 69 61 73 assert( iAlias
21a63 3e 30 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 >0 && iAlias<=pP
21a64 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a arse->nAlias );.
21a65 20 20 69 52 65 67 20 3d 20 70 50 61 72 73 65 2d iReg = pParse-
21a66 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 >aAlias[iAlias-1
21a67 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 30 ];. if( iReg==0
21a68 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 72 ){. if( pPar
21a69 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e se->iCacheLevel>
21a6a 30 20 29 7b 0a 20 20 20 20 20 20 69 52 65 67 20 0 ){. iReg
21a6b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
21a6c 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 eTarget(pParse,
21a6d 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a pExpr, target);.
21a6e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21a6f 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 iReg = ++pParse
21a70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 ->nMem;. sq
21a71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 lite3ExprCode(pP
21a72 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 52 65 arse, pExpr, iRe
21a73 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 g);. pParse
21a74 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d ->aAlias[iAlias-
21a75 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 1] = iReg;. }
21a76 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 . }. return iR
21a77 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 eg;.#else. UNUS
21a78 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 41 6c ED_PARAMETER(iAl
21a79 69 61 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 ias);. return s
21a7a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
21a7b 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
21a7c 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 23 65 6e pr, target);.#en
21a7d 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e dif.}../*.** Gen
21a7e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 erate code into
21a7f 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 the current Vdbe
21a80 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 to evaluate the
21a81 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 given.** expres
21a82 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 sion. Attempt t
21a83 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 o store the resu
21a84 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 lts in register
21a85 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 "target"..** Ret
21a86 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 urn the register
21a87 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 where results a
21a88 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a re stored..**.**
21a89 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74 69 With this routi
21a8a 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ne, there is no
21a8b 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 72 guarantee that r
21a8c 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 esults will.** b
21a8d 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 72 67 e stored in targ
21a8e 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 et. The result
21a8f 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 64 20 might be stored
21a90 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a in some other.**
21a91 20 72 65 67 69 73 74 65 72 20 69 66 20 69 74 20 register if it
21a92 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f is convenient to
21a93 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 6c do so. The cal
21a94 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a ling function.**
21a95 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 must check the
21a96 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 return code and
21a97 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 move the results
21a98 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 0a to the desired.
21a99 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a ** register..*/.
21a9a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
21a9b 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
21a9c 64 65 54 61 72 67 65 74 28 50 61 72 73 65 20 2a deTarget(Parse *
21a9d 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
21a9e 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 xpr, int target)
21a9f 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
21aa0 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a arse->pVdbe; /*
21aa1 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 63 6f The VM under co
21aa2 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 nstruction */.
21aa3 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 int op;
21aa4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
21aa5 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 63 6f opcode being co
21aa6 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 ded */. int inR
21aa7 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 20 20 eg = target;
21aa8 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 /* Results st
21aa9 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 ored in register
21aaa 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 inReg */. int
21aab 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 20 20 regFree1 = 0;
21aac 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d /* If non-
21aad 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 zero free this t
21aae 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 emporary registe
21aaf 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 r */. int regFr
21ab0 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ee2 = 0;
21ab1 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 /* If non-zero
21ab2 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 free this tempor
21ab3 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a ary register */.
21ab4 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 int r1, r2, r3
21ab5 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 , r4; /* V
21ab6 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 72 20 arious register
21ab7 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c numbers */. sql
21ab8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
21ab9 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 61 e->db; /* The da
21aba 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
21abb 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 n */.. assert(
21abc 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 target>0 && targ
21abd 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d et<=pParse->nMem
21abe 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 );. if( v==0 )
21abf 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
21ac0 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
21ac1 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 Failed );. re
21ac2 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 turn 0;. }.. i
21ac3 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 f( pExpr==0 ){.
21ac4 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b op = TK_NULL;
21ac5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 . }else{. op
21ac6 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 = pExpr->op;.
21ac7 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 }. switch( op )
21ac8 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 {. case TK_AG
21ac9 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 G_COLUMN: {.
21aca 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 AggInfo *pAggI
21acb 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 nfo = pExpr->pAg
21acc 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 gInfo;. str
21acd 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 uct AggInfo_col
21ace 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 *pCol = &pAggInf
21acf 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 o->aCol[pExpr->i
21ad0 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 Agg];. if(
21ad1 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 !pAggInfo->direc
21ad2 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 tMode ){.
21ad3 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 assert( pCol->i
21ad4 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 Mem>0 );.
21ad5 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 inReg = pCol->i
21ad6 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 Mem;. bre
21ad7 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 ak;. }else
21ad8 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 if( pAggInfo->us
21ad9 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 eSortingIdx ){.
21ada 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21adb 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 beAddOp3(v, OP_C
21adc 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d olumn, pAggInfo-
21add 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 >sortingIdx,.
21ade 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21adf 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d pCol-
21ae0 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 >iSorterColumn,
21ae1 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 target);.
21ae2 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
21ae3 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 /* Otherwi
21ae4 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e se, fall thru in
21ae5 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e to the TK_COLUMN
21ae6 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 case */. }.
21ae7 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d case TK_COLUM
21ae8 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 N: {. if( p
21ae9 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 Expr->iTable<0 )
21aea 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 {. /* Thi
21aeb 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 s only happens w
21aec 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b hen coding check
21aed 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a constraints */.
21aee 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
21aef 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 pParse->ckBase>0
21af0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 );. inRe
21af1 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 g = pExpr->iColu
21af2 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 mn + pParse->ckB
21af3 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ase;. }else
21af4 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 {. testca
21af5 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 se( (pExpr->flag
21af6 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 21 3d s & EP_AnyAff)!=
21af7 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 0 );. inR
21af8 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 eg = sqlite3Expr
21af9 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 CodeGetColumn(pP
21afa 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 arse, pExpr->pTa
21afb 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b,.
21afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21afd 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 pExpr->iColu
21afe 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c mn, pExpr->iTabl
21aff 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 e, target,.
21b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b01 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
21b02 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e r->flags & EP_An
21b03 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 yAff);. }.
21b04 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21b05 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e }. case TK_IN
21b06 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 TEGER: {. c
21b07 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 45 odeInteger(v, pE
21b08 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b xpr, 0, target);
21b09 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21b0a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21b0b 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 FLOAT: {. a
21b0c 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
21b0d 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21b0e 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
21b0f 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c codeReal(v,
21b10 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
21b11 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
21b12 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21b13 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 . case TK_STR
21b14 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ING: {. ass
21b15 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
21b16 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21b17 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
21b18 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21b19 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e dOp4(v, OP_Strin
21b1a 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c 20 30 g8, 0, target, 0
21b1b 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 , pExpr->u.zToke
21b1c 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 n, 0);. bre
21b1d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
21b1e 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 se TK_NULL: {.
21b1f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21b20 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
21b21 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 0, target);.
21b22 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21b23 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
21b24 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
21b25 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c L. case TK_BL
21b26 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 OB: {. int
21b27 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 n;. const c
21b28 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 har *z;. ch
21b29 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 ar *zBlob;.
21b2a 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
21b2b 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
21b2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b EP_IntValue) );
21b2d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
21b2e 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 Expr->u.zToken[0
21b2f 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d ]=='x' || pExpr-
21b30 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 >u.zToken[0]=='X
21b31 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ' );. asser
21b32 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b t( pExpr->u.zTok
21b33 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 en[1]=='\'' );.
21b34 20 20 20 20 20 7a 20 3d 20 26 70 45 78 70 72 2d z = &pExpr-
21b35 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 >u.zToken[2];.
21b36 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 n = sqlite3S
21b37 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3b 0a trlen30(z) - 1;.
21b38 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 5b assert( z[
21b39 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 n]=='\'' );.
21b3a 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 zBlob = sqlite
21b3b 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 3HexToBlob(sqlit
21b3c 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 e3VdbeDb(v), z,
21b3d 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 n);. sqlite
21b3e 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
21b3f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 P_Blob, n/2, tar
21b40 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 get, 0, zBlob, P
21b41 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 4_DYNAMIC);.
21b42 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
21b43 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 endif. case T
21b44 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 K_VARIABLE: {.
21b45 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b VdbeOp *pOp;
21b46 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
21b47 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
21b48 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c pExpr, EP_IntVal
21b49 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ue) );. ass
21b4a 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 ert( pExpr->u.zT
21b4b 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 20 oken!=0 );.
21b4c 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
21b4d 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 u.zToken[0]!=0 )
21b4e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 ;. if( pExp
21b4f 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d r->u.zToken[1]==
21b50 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70 0. && (p
21b51 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 Op = sqlite3Vdbe
21b52 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 2d 3e 6f GetOp(v, -1))->o
21b53 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 pcode==OP_Variab
21b54 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 le. && p
21b55 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d Op->p1+pOp->p3==
21b56 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a 20 pExpr->iColumn.
21b57 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e && pOp->
21b58 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 p2+pOp->p3==targ
21b59 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 et. && p
21b5a 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 Op->p4.z==0.
21b5b 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
21b5c 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 If the previous
21b5d 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 instruction was
21b5e 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 72 a copy of the pr
21b5f 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 evious unnamed.
21b60 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 ** parame
21b61 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 72 65 ter into the pre
21b62 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 2c 20 vious register,
21b63 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 then simply incr
21b64 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 20 20 ement the.
21b65 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 6e ** repeat coun
21b66 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 69 t on the prior i
21b67 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 68 65 nstruction rathe
21b68 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 r than making a
21b69 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 new. ** i
21b6a 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 nstruction..
21b6b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 */. p
21b6c 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 Op->p3++;.
21b6d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
21b6e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
21b6f 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c (v, OP_Variable,
21b70 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c pExpr->iColumn,
21b71 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 target, 1);.
21b72 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
21b73 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29 u.zToken[1]!=0 )
21b74 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
21b75 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
21b76 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e v, -1, pExpr->u.
21b77 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 zToken, 0);.
21b78 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
21b79 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21b7a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 . case TK_REG
21b7b 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 ISTER: {. i
21b7c 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 nReg = pExpr->iT
21b7d 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61 able;. brea
21b7e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21b7f 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 e TK_AS: {.
21b80 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 inReg = codeAli
21b81 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 as(pParse, pExpr
21b82 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d ->iTable, pExpr-
21b83 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b >pLeft, target);
21b84 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21b85 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
21b86 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 TE_OMIT_CAST.
21b87 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b case TK_CAST: {
21b88 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 . /* Expres
21b89 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 sions of the for
21b8a 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 20 m: CAST(pLeft
21b8b 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 AS token) */.
21b8c 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f int aff, to_o
21b8d 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d p;. inReg =
21b8e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21b8f 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 Target(pParse, p
21b90 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 Expr->pLeft, tar
21b91 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 get);. asse
21b92 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
21b93 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
21b94 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
21b95 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41 aff = sqlite3A
21b96 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 ffinityType(pExp
21b97 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 r->u.zToken);.
21b98 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 to_op = aff
21b99 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 - SQLITE_AFF_TEX
21b9a 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 T + OP_ToText;.
21b9b 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
21b9c 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 op==OP_ToText
21b9d 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
21b9e 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 AFF_TEXT );.
21b9f 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
21ba0 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 op==OP_ToBlob
21ba1 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
21ba2 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 AFF_NONE );.
21ba3 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
21ba4 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 op==OP_ToNumeric
21ba5 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
21ba6 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 AFF_NUMERIC );.
21ba7 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
21ba8 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 op==OP_ToInt
21ba9 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
21baa 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 AFF_INTEGER );.
21bab 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f assert( to_
21bac 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 op==OP_ToReal
21bad 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f || aff!=SQLITE_
21bae 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 AFF_REAL );.
21baf 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
21bb0 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 o_op==OP_ToText
21bb1 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21bb2 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 e( to_op==OP_ToB
21bb3 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 lob );. tes
21bb4 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 tcase( to_op==OP
21bb5 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 _ToNumeric );.
21bb6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f testcase( to
21bb7 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b _op==OP_ToInt );
21bb8 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21bb9 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 to_op==OP_ToRea
21bba 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 l );. if( i
21bbb 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a nReg!=target ){.
21bbc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21bbd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21bbe 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 SCopy, inReg, ta
21bbf 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 rget);. i
21bc0 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 nReg = target;.
21bc1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
21bc2 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
21bc3 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b , to_op, inReg);
21bc4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21bc5 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 usedAsColumnCac
21bc6 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 he(pParse, inReg
21bc7 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 , inReg) );.
21bc8 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21bc9 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
21bca 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 (pParse, inReg,
21bcb 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1);. break;
21bcc 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a . }.#endif /*
21bcd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
21bce 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b T */. case TK
21bcf 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LT:. case TK
21bd0 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _LE:. case TK
21bd1 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GT:. case TK
21bd2 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _GE:. case TK
21bd3 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _NE:. case TK
21bd4 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 _EQ: {. ass
21bd5 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c ert( TK_LT==OP_L
21bd6 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
21bd7 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 t( TK_LE==OP_Le
21bd8 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21bd9 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b TK_GT==OP_Gt );
21bda 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21bdb 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 K_GE==OP_Ge );.
21bdc 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21bdd 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 EQ==OP_Eq );.
21bde 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 assert( TK_NE
21bdf 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 ==OP_Ne );.
21be0 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21be1 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LT );. te
21be2 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c stcase( op==TK_L
21be3 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 E );. testc
21be4 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 ase( op==TK_GT )
21be5 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21be6 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 ( op==TK_GE );.
21be7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21be8 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 p==TK_EQ );.
21be9 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21bea 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 TK_NE );. c
21beb 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e odeCompareOperan
21bec 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 ds(pParse, pExpr
21bed 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 ->pLeft, &r1, &r
21bee 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 egFree1,.
21bef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21bf0 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 pExpr
21bf1 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 ->pRight, &r2, &
21bf2 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
21bf3 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 codeCompare(pPa
21bf4 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
21bf5 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 t, pExpr->pRight
21bf6 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 , op,.
21bf7 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 r1, r2,
21bf8 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 inReg, SQLITE_ST
21bf9 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 OREP2);. te
21bfa 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
21bfb 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ==0 );. tes
21bfc 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
21bfd 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
21bfe 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21bff 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 e TK_IS:. cas
21c00 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 e TK_ISNOT: {.
21c01 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
21c02 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 ==TK_IS );.
21c03 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21c04 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 K_ISNOT );.
21c05 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 codeCompareOper
21c06 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 ands(pParse, pEx
21c07 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 pr->pLeft, &r1,
21c08 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 ®Free1,.
21c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 pEx
21c0b 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c pr->pRight, &r2,
21c0c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 ®Free2);.
21c0d 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f op = (op==TK_
21c0e 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b IS) ? TK_EQ : TK
21c0f 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 _NE;. codeC
21c10 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 ompare(pParse, p
21c11 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 Expr->pLeft, pEx
21c12 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a pr->pRight, op,.
21c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c14 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c r1, r2, inReg,
21c15 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 SQLITE_STOREP2
21c16 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 | SQLITE_NULLEQ)
21c17 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c18 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
21c19 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c1a 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a regFree2==0 );.
21c1b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21c1c 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 }. case TK_A
21c1d 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ND:. case TK_
21c1e 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f OR:. case TK_
21c1f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 PLUS:. case T
21c20 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 K_STAR:. case
21c21 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 TK_MINUS:. c
21c22 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 ase TK_REM:.
21c23 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a case TK_BITAND:.
21c24 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f case TK_BITO
21c25 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 R:. case TK_S
21c26 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 LASH:. case T
21c27 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 K_LSHIFT:. ca
21c28 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 se TK_RSHIFT: .
21c29 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 case TK_CONCA
21c2a 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 T: {. asser
21c2b 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e t( TK_AND==OP_An
21c2c 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 d );. asser
21c2d 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 t( TK_OR==OP_Or
21c2e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21c2f 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 TK_PLUS==OP_Add
21c30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21c31 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 ( TK_MINUS==OP_S
21c32 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 ubtract );.
21c33 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d assert( TK_REM=
21c34 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b =OP_Remainder );
21c35 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21c36 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 K_BITAND==OP_Bit
21c37 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 And );. ass
21c38 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f ert( TK_BITOR==O
21c39 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 P_BitOr );.
21c3a 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 assert( TK_SLAS
21c3b 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a H==OP_Divide );.
21c3c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21c3d 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 _LSHIFT==OP_Shif
21c3e 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 tLeft );. a
21c3f 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 ssert( TK_RSHIFT
21c40 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 ==OP_ShiftRight
21c41 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21c42 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 TK_CONCAT==OP_C
21c43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74 oncat );. t
21c44 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21c45 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 AND );. tes
21c46 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 tcase( op==TK_OR
21c47 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c48 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 se( op==TK_PLUS
21c49 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21c4a 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 e( op==TK_MINUS
21c4b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21c4c 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b e( op==TK_REM );
21c4d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c4e 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 op==TK_BITAND )
21c4f 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c50 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 ( op==TK_BITOR )
21c51 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c52 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 ( op==TK_SLASH )
21c53 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c54 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 ( op==TK_LSHIFT
21c55 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21c56 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 e( op==TK_RSHIFT
21c57 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c58 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 se( op==TK_CONCA
21c59 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 T );. r1 =
21c5a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
21c5b 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
21c5c 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
21c5d 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d ee1);. r2 =
21c5e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21c5f 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
21c60 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 pr->pRight, ®
21c61 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71 Free2);. sq
21c62 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21c63 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 v, op, r2, r1, t
21c64 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 arget);. te
21c65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
21c66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ==0 );. tes
21c67 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
21c68 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
21c69 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21c6a 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 e TK_UMINUS: {.
21c6b 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 Expr *pLeft
21c6c 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = pExpr->pLeft;
21c6d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
21c6e 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66 Left );. if
21c6f 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f ( pLeft->op==TK_
21c70 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 FLOAT ){.
21c71 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
21c72 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
21c73 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b EP_IntValue) );
21c74 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 61 . codeRea
21c75 6c 28 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 l(v, pLeft->u.zT
21c76 6f 6b 65 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 oken, 1, target)
21c77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
21c78 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f ( pLeft->op==TK_
21c79 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 INTEGER ){.
21c7a 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 codeInteger(v
21c7b 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 , pLeft, 1, targ
21c7c 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 et);. }else
21c7d 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 72 65 {. regFre
21c7e 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 e1 = r1 = sqlite
21c7f 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
21c80 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c se);. sql
21c81 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21c82 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
21c83 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 72 32 r1);. r2
21c84 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
21c85 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 deTemp(pParse, p
21c86 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 Expr->pLeft, &re
21c87 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 20 gFree2);.
21c88 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21c89 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 p3(v, OP_Subtrac
21c8a 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 t, r2, r1, targe
21c8b 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 t);. test
21c8c 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d case( regFree2==
21c8d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0 );. }.
21c8e 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 inReg = targe
21c8f 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a t;. break;.
21c90 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21c91 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 K_BITNOT:. ca
21c92 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 se TK_NOT: {.
21c93 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 assert( TK_BI
21c94 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 TNOT==OP_BitNot
21c95 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21c96 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 TK_NOT==OP_Not
21c97 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21c98 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 e( op==TK_BITNOT
21c99 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21c9a 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 se( op==TK_NOT )
21c9b 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c ;. r1 = sql
21c9c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
21c9d 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21c9e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 pLeft, ®Free1
21c9f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21ca0 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
21ca1 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 ;. inReg =
21ca2 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 73 71 target;. sq
21ca3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21ca4 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 v, op, r1, inReg
21ca5 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21ca6 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21ca7 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 K_ISNULL:. ca
21ca8 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b se TK_NOTNULL: {
21ca9 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b . int addr;
21caa 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21cab 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e K_ISNULL==OP_IsN
21cac 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 ull );. ass
21cad 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d ert( TK_NOTNULL=
21cae 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 =OP_NotNull );.
21caf 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21cb0 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a p==TK_ISNULL );.
21cb1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21cb2 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 op==TK_NOTNULL )
21cb3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
21cb4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21cb5 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 Integer, 1, targ
21cb6 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 et);. r1 =
21cb7 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
21cb8 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 emp(pParse, pExp
21cb9 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 r->pLeft, ®Fr
21cba 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee1);. test
21cbb 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
21cbc 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 0 );. addr
21cbd 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
21cbe 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a Op1(v, op, r1);.
21cbf 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21cc0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 eAddOp2(v, OP_Ad
21cc1 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 dImm, target, -1
21cc2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21cc3 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
21cc4 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72 65 addr);. bre
21cc5 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
21cc6 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 se TK_AGG_FUNCTI
21cc7 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 ON: {. AggI
21cc8 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 nfo *pInfo = pEx
21cc9 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 pr->pAggInfo;.
21cca 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 if( pInfo==0
21ccb 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
21ccc 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
21ccd 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
21cce 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
21ccf 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
21cd0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 Msg(pParse, "mis
21cd1 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 use of aggregate
21cd2 3a 20 25 73 28 29 22 2c 20 70 45 78 70 72 2d 3e : %s()", pExpr->
21cd3 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 u.zToken);.
21cd4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
21cd5 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 inReg = pInfo->a
21cd6 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 Func[pExpr->iAgg
21cd7 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a ].iMem;. }.
21cd8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21cd9 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 }. case TK_C
21cda 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 ONST_FUNC:. c
21cdb 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a ase TK_FUNCTION:
21cdc 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 {. ExprLis
21cdd 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 20 20 t *pFarg;
21cde 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 /* List of funct
21cdf 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f ion arguments */
21ce0 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 72 67 . int nFarg
21ce1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
21ce2 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 Number of funct
21ce3 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f ion arguments */
21ce4 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a . FuncDef *
21ce5 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 2f 2a pDef; /*
21ce6 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 The function de
21ce7 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 finition object
21ce8 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 */. int nId
21ce9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
21cea 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 /* Length of the
21ceb 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 function name i
21cec 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 n bytes */.
21ced 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 const char *zId
21cee 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 ; /* The f
21cef 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
21cf0 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d int constM
21cf1 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 ask = 0; /*
21cf2 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e Mask of function
21cf3 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 arguments that
21cf4 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a are constant */.
21cf5 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 int i;
21cf6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21cf7 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
21cf8 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 u8 enc = E
21cf9 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 NC(db); /*
21cfa 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e The text encodin
21cfb 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64 g used by this d
21cfc 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 atabase */.
21cfd 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 CollSeq *pColl
21cfe 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c = 0; /* A col
21cff 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
21d00 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 */.. assert
21d01 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21d02 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
21d03 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 Select) );.
21d04 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21d05 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a K_CONST_FUNC );.
21d06 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21d07 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 op==TK_FUNCTION
21d08 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 );. if( Exp
21d09 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
21d0a 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f pExpr, EP_TokenO
21d0b 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 nly) ){.
21d0c 70 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 pFarg = 0;.
21d0d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
21d0e 70 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 pFarg = pExpr->x
21d0f 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a .pList;. }.
21d10 20 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46 nFarg = pF
21d11 61 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 arg ? pFarg->nEx
21d12 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 61 73 pr : 0;. as
21d13 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21d14 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21d15 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 _IntValue) );.
21d16 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d zId = pExpr-
21d17 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 >u.zToken;.
21d18 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53 74 nId = sqlite3St
21d19 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 rlen30(zId);.
21d1a 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 pDef = sqlite
21d1b 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 3FindFunction(db
21d1c 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61 72 , zId, nId, nFar
21d1d 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 g, enc, 0);.
21d1e 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b if( pDef==0 ){
21d1f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21d20 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
21d21 20 22 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 "unknown functi
21d22 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 on: %.*s()", nId
21d23 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 , zId);.
21d24 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a break;. }..
21d25 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 /* Attempt
21d26 20 61 20 64 69 72 65 63 74 20 69 6d 70 6c 65 6d a direct implem
21d27 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
21d28 62 75 69 6c 74 2d 69 6e 20 43 4f 41 4c 45 53 43 built-in COALESC
21d29 45 28 29 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a E() and. **
21d2a 20 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 IFNULL() functi
21d2b 6f 6e 73 2e 20 20 54 68 69 73 20 61 76 6f 69 64 ons. This avoid
21d2c 73 20 75 6e 6e 65 63 65 73 73 61 72 79 20 65 76 s unnecessary ev
21d2d 61 6c 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 alation of.
21d2e 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61 ** arguments pa
21d2f 73 74 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e st the first non
21d30 2d 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a -NULL argument..
21d31 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 */. i
21d32 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 f( pDef->flags &
21d33 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 SQLITE_FUNC_COA
21d34 4c 45 53 43 45 20 29 7b 0a 20 20 20 20 20 20 20 LESCE ){.
21d35 20 69 6e 74 20 65 6e 64 43 6f 61 6c 65 73 63 65 int endCoalesce
21d36 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
21d37 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
21d38 20 20 20 20 61 73 73 65 72 74 28 20 6e 46 61 72 assert( nFar
21d39 67 3e 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20 g>=2 );.
21d3a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
21d3b 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 pParse, pFarg->a
21d3c 5b 30 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65 [0].pExpr, targe
21d3d 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 t);. for(
21d3e 69 3d 31 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b i=1; i<nFarg; i+
21d3f 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 +){. sq
21d40 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21d41 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 74 v, OP_NotNull, t
21d42 61 72 67 65 74 2c 20 65 6e 64 43 6f 61 6c 65 73 arget, endCoales
21d43 63 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 ce);. s
21d44 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 52 qlite3ExprCacheR
21d45 65 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 74 61 emove(pParse, ta
21d46 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 20 rget);.
21d47 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21d48 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 ePush(pParse);.
21d49 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21d4a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c ExprCode(pParse,
21d4b 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 pFarg->a[i].pEx
21d4c 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 pr, target);.
21d4d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
21d4e 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 prCachePop(pPars
21d4f 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 7d e, 1);. }
21d50 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21d51 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
21d52 28 76 2c 20 65 6e 64 43 6f 61 6c 65 73 63 65 29 (v, endCoalesce)
21d53 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
21d54 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20 20 20 . }...
21d55 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a 20 20 if( pFarg ){.
21d56 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 r1 = sqlit
21d57 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 e3GetTempRange(p
21d58 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 Parse, nFarg);.
21d59 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
21d5a 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 prCachePush(pPar
21d5b 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b se); /* Tick
21d5c 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 20 et 2ea2425d34be
21d5d 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
21d5e 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 e3ExprCodeExprLi
21d5f 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 st(pParse, pFarg
21d60 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 , r1, 1);.
21d61 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21d62 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
21d63 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 ; /* Ticket 2e
21d64 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 a2425d34be */.
21d65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21d66 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 r1 = 0;.
21d67 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
21d68 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
21d69 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 BLE. /* Pos
21d6a 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 sibly overload t
21d6b 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 he function if t
21d6c 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
21d6d 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 t is. ** a
21d6e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f virtual table co
21d6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 lumn.. **.
21d70 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 ** For infi
21d71 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b x functions (LIK
21d72 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c E, GLOB, REGEXP,
21d73 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 20 and MATCH) use
21d74 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 the. ** sec
21d75 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f ond argument, no
21d76 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 20 t the first, as
21d77 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 the argument to
21d78 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a test to. **
21d79 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 20 see if it is a
21d7a 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 column in a virt
21d7b 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 ual table. This
21d7c 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 65 is done because
21d7d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 . ** the le
21d7e 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e ft operand of in
21d7f 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 fix functions (t
21d80 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 61 he operand we wa
21d81 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 nt to. ** c
21d82 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 ontrol overloadi
21d83 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 74 ng) ends up as t
21d84 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
21d85 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 nt to the.
21d86 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 ** function. Th
21d87 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 20 e expression "A
21d88 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 76 glob B" is equiv
21d89 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 alent to .
21d8a 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 ** "glob(B,A).
21d8b 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 We want to use t
21d8c 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 he A in "A glob
21d8d 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 20 B" to test.
21d8e 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e ** for function
21d8f 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 overloading. B
21d90 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 20 ut we use the B
21d91 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c term in "glob(B,
21d92 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 A)".. */.
21d93 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e 3d 32 if( nFarg>=2
21d94 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 && (pExpr->flag
21d95 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 s & EP_InfixFunc
21d96 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 ) ){. pDe
21d97 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f f = sqlite3VtabO
21d98 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 verloadFunction(
21d99 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 2c db, pDef, nFarg,
21d9a 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 pFarg->a[1].pEx
21d9b 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 pr);. }else
21d9c 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a if( nFarg>0 ){.
21d9d 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 pDef = s
21d9e 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f qlite3VtabOverlo
21d9f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 adFunction(db, p
21da0 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 Def, nFarg, pFar
21da1 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a g->a[0].pExpr);.
21da2 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
21da3 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
21da4 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 nFarg; i++){.
21da5 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 26 26 if( i<32 &&
21da6 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f sqlite3ExprIsCo
21da7 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b nstant(pFarg->a[
21da8 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 i].pExpr) ){.
21da9 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b constMask
21daa 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 |= (1<<i);.
21dab 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
21dac 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 ( (pDef->flags &
21dad 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
21dae 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 DCOLL)!=0 && !pC
21daf 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 oll ){.
21db0 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 pColl = sqlite3
21db1 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 ExprCollSeq(pPar
21db2 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e se, pFarg->a[i].
21db3 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 pExpr);.
21db4 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
21db5 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 if( pDef->flags
21db6 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 & SQLITE_FUNC_NE
21db7 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 EDCOLL ){.
21db8 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 if( !pColl ) p
21db9 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 Coll = db->pDflt
21dba 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 Coll; . s
21dbb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
21dbc 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 (v, OP_CollSeq,
21dbd 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 0, 0, 0, (char *
21dbe 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 )pColl, P4_COLLS
21dbf 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 EQ);. }.
21dc0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21dc1 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 dOp4(v, OP_Funct
21dc2 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 ion, constMask,
21dc3 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20 r1, target,.
21dc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21dc5 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66 2c (char*)pDef,
21dc6 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 P4_FUNCDEF);.
21dc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
21dc8 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 6e hangeP5(v, (u8)n
21dc9 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 66 28 Farg);. if(
21dca 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20 nFarg ){.
21dcb 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
21dcc 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 TempRange(pParse
21dcd 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 , r1, nFarg);.
21dce 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
21dcf 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
21dd0 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
21dd1 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 e, r1, nFarg);.
21dd2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21dd3 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
21dd4 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 _OMIT_SUBQUERY.
21dd5 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 case TK_EXIST
21dd6 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 S:. case TK_S
21dd7 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 ELECT: {. t
21dd8 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21dd9 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 EXISTS );.
21dda 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21ddb 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 _SELECT );.
21ddc 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 inReg = sqlite3
21ddd 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 CodeSubselect(pP
21dde 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 arse, pExpr, 0,
21ddf 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0);. break;
21de0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21de1 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 TK_IN: {. i
21de2 6e 74 20 64 65 73 74 49 66 46 61 6c 73 65 20 3d nt destIfFalse =
21de3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
21de4 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 Label(v);.
21de5 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d int destIfNull =
21de6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
21de7 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 Label(v);.
21de8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21de9 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
21dea 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
21deb 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49 sqlite3ExprCodeI
21dec 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c N(pParse, pExpr,
21ded 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 64 65 destIfFalse, de
21dee 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 stIfNull);.
21def 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21df0 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
21df1 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 1, target);.
21df2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 sqlite3VdbeR
21df3 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 esolveLabel(v, d
21df4 65 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 estIfFalse);.
21df5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21df6 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d dOp2(v, OP_AddIm
21df7 6d 2c 20 74 61 72 67 65 74 2c 20 30 29 3b 0a 20 m, target, 0);.
21df8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21df9 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 ResolveLabel(v,
21dfa 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 destIfNull);.
21dfb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21dfc 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
21dfd 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a _OMIT_SUBQUERY *
21dfe 2f 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a /... /*. *
21dff 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 * x BETWEEN y
21e00 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 AND z. **.
21e01 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 ** This is equ
21e02 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a ivalent to. *
21e03 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 *. ** x>=y
21e04 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a AND x<=z. **
21e05 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f . ** X is sto
21e06 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c red in pExpr->pL
21e07 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 eft.. ** Y is
21e08 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
21e09 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 ->pList->a[0].pE
21e0a 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 xpr.. ** Z is
21e0b 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
21e0c 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 ->pList->a[1].pE
21e0d 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 xpr.. */.
21e0e 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a case TK_BETWEEN:
21e0f 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 {. Expr *p
21e10 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c Left = pExpr->pL
21e11 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 eft;. struc
21e12 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
21e13 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d *pLItem = pExpr-
21e14 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 >x.pList->a;.
21e15 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 Expr *pRight
21e16 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pLItem->pExpr;
21e17 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 .. codeComp
21e18 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 areOperands(pPar
21e19 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 se, pLeft, &r1,
21e1a 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 ®Free1,.
21e1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 pRi
21e1d 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 ght, &r2, ®Fr
21e1e 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee2);. test
21e1f 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
21e20 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
21e21 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
21e22 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 );. r3 = s
21e23 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
21e24 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
21e25 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r4 = sqlite3GetT
21e26 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
21e27 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 codeCompar
21e28 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c e(pParse, pLeft,
21e29 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a pRight, OP_Ge,.
21e2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e2b 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 r1, r2, r3, SQ
21e2c 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 LITE_STOREP2);.
21e2d 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 pLItem++;.
21e2e 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c pRight = pL
21e2f 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Item->pExpr;.
21e30 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
21e31 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
21e32 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 regFree2);.
21e33 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 r2 = sqlite3Ex
21e34 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
21e35 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 e, pRight, ®F
21e36 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 ree2);. tes
21e37 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
21e38 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 =0 );. code
21e39 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 Compare(pParse,
21e3a 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f pLeft, pRight, O
21e3b 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 P_Le, r1, r2, r4
21e3c 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 , SQLITE_STOREP2
21e3d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21e3e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21e3f 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 _And, r3, r4, ta
21e40 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c rget);. sql
21e41 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
21e42 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a eg(pParse, r3);.
21e43 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c sqlite3Rel
21e44 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
21e45 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 se, r4);. b
21e46 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21e47 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b case TK_UPLUS: {
21e48 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 . inReg = s
21e49 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
21e4a 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
21e4b 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 pr->pLeft, targe
21e4c 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b t);. break;
21e4d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 . }.. case
21e4e 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b 0a 20 TK_TRIGGER: {.
21e4f 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f /* If the o
21e50 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52 49 47 pcode is TK_TRIG
21e51 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20 65 78 GER, then the ex
21e52 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 72 65 pression is a re
21e53 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2a ference. **
21e54 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 to a column in
21e55 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 the new.* or old
21e56 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 .* pseudo-tables
21e57 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a 20 20 available to.
21e58 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 70 ** trigger p
21e59 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68 69 73 rograms. In this
21e5a 20 63 61 73 65 20 45 78 70 72 2e 69 54 61 62 6c case Expr.iTabl
21e5b 65 20 69 73 20 73 65 74 20 74 6f 20 31 20 66 6f e is set to 1 fo
21e5c 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e r the. ** n
21e5d 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c ew.* pseudo-tabl
21e5e 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68 65 20 e, or 0 for the
21e5f 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 old.* pseudo-tab
21e60 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e le. Expr.iColumn
21e61 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74 . ** is set
21e62 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f to the column o
21e63 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 f the pseudo-tab
21e64 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72 20 74 le to read, or t
21e65 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20 2a 2a o -1 to. **
21e66 20 72 65 61 64 20 74 68 65 20 72 6f 77 69 64 20 read the rowid
21e67 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2a 0a field.. **.
21e68 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 78 70 ** The exp
21e69 72 65 73 73 69 6f 6e 20 69 73 20 69 6d 70 6c 65 ression is imple
21e6a 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 61 6e 20 mented using an
21e6b 4f 50 5f 50 61 72 61 6d 20 6f 70 63 6f 64 65 2e OP_Param opcode.
21e6c 20 54 68 65 20 70 31 0a 20 20 20 20 20 20 2a 2a The p1. **
21e6d 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 73 65 parameter is se
21e6e 74 20 74 6f 20 30 20 66 6f 72 20 61 6e 20 6f 6c t to 0 for an ol
21e6f 64 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e 63 d.rowid referenc
21e70 65 2c 20 6f 72 20 74 6f 20 28 69 2b 31 29 0a 20 e, or to (i+1).
21e71 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 66 65 72 ** to refer
21e72 65 6e 63 65 20 61 6e 6f 74 68 65 72 20 63 6f 6c ence another col
21e73 75 6d 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a umn of the old.*
21e74 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 77 pseudo-table, w
21e75 68 65 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 here . ** i
21e76 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
21e77 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72 the column. For
21e78 20 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65 66 a new.rowid ref
21e79 65 72 65 6e 63 65 2c 20 70 31 20 69 73 0a 20 20 erence, p1 is.
21e7a 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28 6e ** set to (n
21e7b 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73 20 +1), where n is
21e7c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
21e7d 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70 73 lumns in each ps
21e7e 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 20 20 20 20 eudo-table..
21e7f 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65 72 ** For a refer
21e80 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68 65 ence to any othe
21e81 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 r column in the
21e82 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 new.* pseudo-tab
21e83 6c 65 2c 20 70 31 0a